From 6b8e9807277ee2100ffea33ab1854999b52a980b Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 26 May 2020 14:39:52 +0100 Subject: [PATCH] Add block types REST API. (#21065) * Add block types REST API. * Remove space Co-authored-by: Andrew Duthie * Change comment. Co-authored-by: Andrew Duthie * Change comment. Co-authored-by: Andrew Duthie * Change title. Co-authored-by: Andrew Duthie * Change comment. Co-authored-by: Andrew Duthie * Remove array_shift. Co-authored-by: Andrew Duthie * Change comment to 5.5.0 * Fixes. * Formatting. * More tweaks * Fix lints * Tweak fields. * Feedback. * Add block category. * Feedback * Add back composer.lock. * Tweak descriptions. * Add supports. * Add unit tests. * More tweaks * Fix tests * Tweak test. * Reuse fake block * Add in new fields and improve tests. * Fix lint * Reuse wp_error. * Add more tests * Improve asserts. * Refactor permision check * Add more tests. * Remove unused method. * Fix tests. * Use correct name. * Improve tests. Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 475 ++++++++++++++++++ lib/load.php | 3 + lib/rest-api.php | 8 + ...ss-wp-rest-block-types-controller-test.php | 318 ++++++++++++ 4 files changed, 804 insertions(+) create mode 100644 lib/class-wp-rest-block-types-controller.php create mode 100644 phpunit/class-wp-rest-block-types-controller-test.php diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php new file mode 100644 index 00000000000000..e6fde1ed11b161 --- /dev/null +++ b/lib/class-wp-rest-block-types-controller.php @@ -0,0 +1,475 @@ +namespace = '__experimental'; + $this->rest_base = 'block-types'; + $this->registry = WP_Block_Type_Registry::get_instance(); + } + + /** + * Registers the routes for the objects of the controller. + * + * @see register_rest_route() + */ + public function register_routes() { + + register_rest_route( + $this->namespace, + '/' . $this->rest_base, + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_items' ), + 'permission_callback' => array( $this, 'get_items_permissions_check' ), + 'args' => $this->get_collection_params(), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[a-zA-Z0-9_-]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_items' ), + 'permission_callback' => array( $this, 'get_items_permissions_check' ), + 'args' => $this->get_collection_params(), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', + array( + 'args' => array( + 'name' => array( + 'description' => __( 'Block name', 'gutenberg' ), + 'type' => 'string', + ), + 'namespace' => array( + 'description' => __( 'Block namespace', 'gutenberg' ), + 'type' => 'string', + ), + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'args' => array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + ), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + } + + /** + * Checks whether a given request has permission to read post block types. + * + * @param WP_REST_Request $request Full details about the request. + * + * @return WP_Error|bool True if the request has read access, WP_Error object otherwise. + */ + public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + return $this->check_read_permission(); + } + + /** + * Retrieves all post block types, depending on user context. + * + * @param WP_REST_Request $request Full details about the request. + * + * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. + */ + public function get_items( $request ) { + $data = array(); + $block_types = $this->registry->get_all_registered(); + + // Retrieve the list of registered collection query parameters. + $registered = $this->get_collection_params(); + $namespace = ''; + if ( isset( $registered['namespace'] ) && ! empty( $request['namespace'] ) ) { + $namespace = $request['namespace']; + } + + foreach ( $block_types as $slug => $obj ) { + if ( $namespace ) { + $pieces = explode( '/', $obj->name ); + $block_namespace = $pieces[0]; + if ( $namespace !== $block_namespace ) { + continue; + } + } + $block_type = $this->prepare_item_for_response( $obj, $request ); + $data[] = $this->prepare_response_for_collection( $block_type ); + } + + return rest_ensure_response( $data ); + } + + /** + * Checks if a given request has access to read a block type. + * + * @param WP_REST_Request $request Full details about the request. + * + * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. + */ + public function get_item_permissions_check( $request ) { + $check = $this->check_read_permission(); + if ( is_wp_error( $check ) ) { + return $check; + } + $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] ); + $block_type = $this->get_block( $block_name ); + if ( is_wp_error( $block_type ) ) { + return $block_type; + } + + return true; + } + + /** + * Checks whether a given block type should be visible. + * + * @return WP_Error|bool True if the block type is visible, otherwise false. + */ + protected function check_read_permission() { + if ( current_user_can( 'edit_posts' ) ) { + return true; + } + foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { + if ( current_user_can( $post_type->cap->edit_posts ) ) { + return true; + } + } + + return new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); + } + + /** + * Get the block, if the name is valid. + * + * @param string $name Block name. + * @return WP_Block_Type|WP_Error Block type object if name is valid, WP_Error otherwise. + */ + protected function get_block( $name ) { + $block_type = $this->registry->get_registered( $name ); + if ( empty( $block_type ) ) { + return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.', 'gutenberg' ), array( 'status' => 404 ) ); + } + + return $block_type; + } + + /** + * Retrieves a specific block type. + * + * @param WP_REST_Request $request Full details about the request. + * + * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. + */ + public function get_item( $request ) { + $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] ); + $block_type = $this->get_block( $block_name ); + if ( is_wp_error( $block_type ) ) { + return $block_type; + } + $data = $this->prepare_item_for_response( $block_type, $request ); + + return rest_ensure_response( $data ); + } + + /** + * Prepares a block type object for serialization. + * + * @param WP_Block_Type $block_type block type data. + * @param WP_REST_Request $request Full details about the request. + * + * @return WP_REST_Response block type data. + */ + public function prepare_item_for_response( $block_type, $request ) { + + $fields = $this->get_fields_for_response( $request ); + $data = array(); + + if ( rest_is_field_included( 'attributes', $fields ) ) { + $data['attributes'] = $block_type->get_attributes(); + } + + if ( rest_is_field_included( 'is_dynamic', $fields ) ) { + $data['is_dynamic'] = $block_type->is_dynamic(); + } + + $schema = $this->get_item_schema(); + $extra_fields = array( + 'name' => 'name', + 'category' => 'category', + 'editor_script' => 'editor_script', + 'script' => 'script', + 'editor_style' => 'editor_style', + 'style' => 'style', + 'supports' => 'supports', + 'title' => 'title', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'styles' => 'styleVariations', + 'text_domain' => 'textDomain', + ); + foreach ( $extra_fields as $key => $extra_field ) { + if ( rest_is_field_included( $key, $fields ) ) { + if ( isset( $block_type->$extra_field ) ) { + $field = $block_type->$extra_field; + } elseif ( isset( $schema['properties'][ $key ]['default'] ) ) { + $field = $schema['properties'][ $key ]['default']; + } else { + $field = ''; + } + $data[ $key ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $key ] ); + } + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + $response = rest_ensure_response( $data ); + + $response->add_links( $this->prepare_links( $block_type ) ); + + /** + * Filters a block type returned from the REST API. + * + * Allows modification of the block type data right before it is returned. + * + * @param WP_REST_Response $response The response object. + * @param object $block_type The original block type object. + * @param WP_REST_Request $request Request used to generate the response. + */ + return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request ); + } + + /** + * Prepares links for the request. + * + * @param WP_Block_Type $block_type block type data. + * @return array Links for the given block type. + */ + protected function prepare_links( $block_type ) { + $pieces = explode( '/', $block_type->name ); + $namespace = $pieces[0]; + $links = array( + 'collection' => array( + 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), + ), + 'self' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), + ), + 'up' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $namespace ) ), + ), + ); + + if ( $block_type->is_dynamic() ) { + $links['https://api.w.org/render-block']['href'] = add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ); + } + + return $links; + } + + /** + * Retrieves the block type' schema, conforming to JSON Schema. + * + * @return array Item schema data. + */ + public function get_item_schema() { + if ( $this->schema ) { + return $this->add_additional_fields_schema( $this->schema ); + } + + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'block-type', + 'type' => 'object', + 'properties' => array( + 'title' => array( + 'description' => __( 'Title of block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'name' => array( + 'description' => __( 'Unique name identifying the block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'description' => array( + 'description' => __( 'Description of block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'icon' => array( + 'description' => __( 'Icon of block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'attributes' => array( + 'description' => __( 'Block attributes.', 'gutenberg' ), + 'type' => 'object', + 'properties' => array(), + 'default' => array(), + 'additionalProperties' => array( + 'type' => 'object', + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'supports' => array( + 'description' => __( 'Block supports.', 'gutenberg' ), + 'type' => 'object', + 'default' => array(), + 'properties' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'category' => array( + 'description' => __( 'Block category.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'is_dynamic' => array( + 'description' => __( 'Is the block dynamically rendered.', 'gutenberg' ), + 'type' => 'boolean', + 'default' => false, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'editor_script' => array( + 'description' => __( 'Editor script handle.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'script' => array( + 'description' => __( 'Public facing script handle.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'editor_style' => array( + 'description' => __( 'Editor style handle.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'style' => array( + 'description' => __( 'Public facing style handle.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'styles' => array( + 'description' => __( 'Block style variations.', 'gutenberg' ), + 'type' => 'object', + 'properties' => array(), + 'additionalProperties' => array( + 'type' => 'object', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'text_domain' => array( + 'description' => __( 'Public text domain.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'parent' => array( + 'description' => __( 'Parent blocks, defaults to empty it no parents', 'gutenberg' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'keywords' => array( + 'description' => __( 'Block keywords.', 'gutenberg' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + ), + ); + + $this->schema = $schema; + + return $this->add_additional_fields_schema( $this->schema ); + } + + /** + * Retrieves the query params for collections. + * + * @return array Collection parameters. + */ + public function get_collection_params() { + $new_params = array(); + $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); + $new_params['namespace'] = array( + 'description' => __( 'Block namespace.', 'gutenberg' ), + 'type' => 'string', + ); + return $new_params; + } + +} diff --git a/lib/load.php b/lib/load.php index 71f5c0191068b7..09e9f1b677eba1 100644 --- a/lib/load.php +++ b/lib/load.php @@ -39,6 +39,9 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_REST_Block_Directory_Controller' ) ) { require dirname( __FILE__ ) . '/class-wp-rest-block-directory-controller.php'; } + if ( ! class_exists( 'WP_REST_Block_Types_Controller' ) ) { + require dirname( __FILE__ ) . '/class-wp-rest-block-types-controller.php'; + } if ( ! class_exists( 'WP_REST_Menus_Controller' ) ) { require_once dirname( __FILE__ ) . '/class-wp-rest-menus-controller.php'; } diff --git a/lib/rest-api.php b/lib/rest-api.php index 6ca1e10cd00a42..d351bebc37c60e 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -128,6 +128,14 @@ function gutenberg_register_rest_block_directory() { } add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' ); +/** + * Registers the Block types REST API routes. + */ +function gutenberg_register_block_type() { + $block_types = new WP_REST_Block_Types_Controller(); + $block_types->register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_block_type' ); /** * Registers the menu locations area REST API routes. */ diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php new file mode 100644 index 00000000000000..952d8892e4547b --- /dev/null +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -0,0 +1,318 @@ +user->create( + array( + 'role' => 'administrator', + ) + ); + self::$subscriber_id = $factory->user->create( + array( + 'role' => 'subscriber', + ) + ); + + $name = 'fake/test'; + $settings = array( + 'icon' => 'text', + ); + + register_block_type( $name, $settings ); + } + + /** + * + */ + public static function wpTearDownAfterClass() { + self::delete_user( self::$admin_id ); + self::delete_user( self::$subscriber_id ); + unregister_block_type( 'fake/test' ); + } + + /** + * + */ + public function test_register_routes() { + $routes = rest_get_server()->get_routes(); + $this->assertArrayHasKey( '/__experimental/block-types', $routes ); + $this->assertCount( 1, $routes['/__experimental/block-types'] ); + $this->assertArrayHasKey( '/__experimental/block-types/(?P[a-zA-Z0-9_-]+)', $routes ); + $this->assertCount( 1, $routes['/__experimental/block-types/(?P[a-zA-Z0-9_-]+)'] ); + $this->assertArrayHasKey( '/__experimental/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', $routes ); + $this->assertCount( 1, $routes['/__experimental/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)'] ); + } + + /** + * + */ + public function test_context_param() { + // Collection. + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); + $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + // Single. + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/fake/test' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); + $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + } + + /** + * + */ + public function test_get_items() { + $block_name = 'fake/test'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertCount( 1, $data ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); + $this->check_block_type_object( $block_type, $data[0], $data[0]['_links'] ); + } + + /** + * + */ + public function test_get_item() { + $block_name = 'fake/test'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_name ); + $response = rest_get_server()->dispatch( $request ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); + $this->check_block_type_object( $block_type, $response->get_data(), $response->get_links() ); + } + + public function test_get_block_invalid_name() { + $block_type = 'fake/block'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_block_type_invalid', $response, 404 ); + } + + /** + * + */ + public function test_get_item_invalid() { + $block_type = 'fake/invalid'; + $settings = array( + 'keywords' => 'invalid_keywords', + 'parent' => 'invalid_parent', + 'supports' => 'invalid_supports', + 'styleVariations' => 'invalid_styles', + 'render_callback' => 'invalid_callback', + ); + register_block_type( $block_type, $settings ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( $block_type, $data['name'] ); + $this->assertEqualSets( array( 'invalid_keywords' ), $data['keywords'] ); + $this->assertEqualSets( array( 'invalid_parent' ), $data['parent'] ); + $this->assertEqualSets( array(), $data['supports'] ); + $this->assertEqualSets( array(), $data['styles'] ); + $this->assertEquals( false, $data['is_dynamic'] ); + } + + /** + * + */ + public function test_get_item_schema() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $properties = $data['schema']['properties']; + $this->assertCount( 16, $properties ); + $this->assertArrayHasKey( 'title', $properties ); + $this->assertArrayHasKey( 'icon', $properties ); + $this->assertArrayHasKey( 'description', $properties ); + $this->assertArrayHasKey( 'keywords', $properties ); + $this->assertArrayHasKey( 'styles', $properties ); + $this->assertArrayHasKey( 'text_domain', $properties ); + $this->assertArrayHasKey( 'name', $properties ); + $this->assertArrayHasKey( 'attributes', $properties ); + $this->assertArrayHasKey( 'supports', $properties ); + $this->assertArrayHasKey( 'category', $properties ); + $this->assertArrayHasKey( 'is_dynamic', $properties ); + $this->assertArrayHasKey( 'editor_script', $properties ); + $this->assertArrayHasKey( 'script', $properties ); + $this->assertArrayHasKey( 'editor_style', $properties ); + $this->assertArrayHasKey( 'style', $properties ); + $this->assertArrayHasKey( 'parent', $properties ); + } + + /** + * + */ + public function test_get_items_wrong_permission() { + wp_set_current_user( self::$subscriber_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); + } + + /** + * + */ + public function test_get_item_wrong_permission() { + wp_set_current_user( self::$subscriber_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); + } + + /** + * + */ + public function test_get_items_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); + } + + /** + * + */ + public function test_get_item_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); + } + + /** + * + */ + public function test_prepare_item() { + $registry = new WP_Block_Type_Registry; + $settings = array( + 'icon' => 'text', + 'render_callback' => '__return_null', + ); + $registry->register( 'fake/line', $settings ); + $block_type = $registry->get_registered( 'fake/line' ); + $endpoint = new WP_REST_Block_Types_Controller; + $request = new WP_REST_Request; + $request->set_param( 'context', 'edit' ); + $response = $endpoint->prepare_item_for_response( $block_type, $request ); + $this->check_block_type_object( $block_type, $response->get_data(), $response->get_links() ); + } + + /** + * + */ + public function test_prepare_item_limit_fields() { + $registry = new WP_Block_Type_Registry; + $settings = array( + 'icon' => 'text', + 'render_callback' => '__return_null', + ); + $registry->register( 'fake/line', $settings ); + $block_type = $registry->get_registered( 'fake/line' ); + $request = new WP_REST_Request; + $endpoint = new WP_REST_Block_Types_Controller; + $request->set_param( 'context', 'edit' ); + $request->set_param( '_fields', 'name' ); + $response = $endpoint->prepare_item_for_response( $block_type, $request ); + $this->assertEquals( + array( + 'name', + ), + array_keys( $response->get_data() ) + ); + } + + /** + * Util check block type object against. + * + * @param WP_Block_Type $block_type Sample block type. + * @param array $data Data to compare against. + * @param array $links Links to compare again. + */ + public function check_block_type_object( $block_type, $data, $links ) { + // Test data. + $this->assertEquals( $data['attributes'], $block_type->get_attributes() ); + $this->assertEquals( $data['is_dynamic'], $block_type->is_dynamic() ); + + $extra_fields = array( + 'name' => 'name', + 'category' => 'category', + 'editor_script' => 'editor_script', + 'script' => 'script', + 'editor_style' => 'editor_style', + 'style' => 'style', + 'supports' => 'supports', + 'title' => 'title', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'styles' => 'styleVariations', + 'text_domain' => 'textDomain', + ); + + foreach ( $extra_fields as $key => $extra_field ) { + if ( isset( $block_type->$extra_field ) ) { + $this->assertEquals( $data[ $key ], $block_type->$extra_field ); + } + } + + // Test links. + $this->assertEquals( rest_url( '__experimental/block-types' ), $links['collection'][0]['href'] ); + $this->assertEquals( rest_url( '__experimental/block-types/' . $block_type->name ), $links['self'][0]['href'] ); + if ( $block_type->is_dynamic() ) { + $this->assertArrayHasKey( 'https://api.w.org/render-block', $links ); + } + } + + /** + * The test_create_item() method does not exist for block types. + */ + public function test_create_item() {} + + /** + * The test_update_item() method does not exist for block types. + */ + public function test_update_item() {} + + /** + * The test_delete_item() method does not exist for block types. + */ + public function test_delete_item() {} +}