diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index f3d406c095..951c8232dd 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -70,15 +70,18 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { $this->namespace, '/' . $this->rest_base . '/(?P[\/\w-]+)', array( + 'args' => array( + 'id' => array( + 'description' => __( 'The id of a template' ), + 'type' => 'string', + ), + ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => array( - 'id' => array( - 'description' => __( 'The id of a template' ), - 'type' => 'string', - ), + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ), ), array( @@ -231,7 +234,12 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { if ( isset( $request['source'] ) && 'theme' === $request['source'] ) { wp_delete_post( $template->wp_id, true ); - return $this->prepare_item_for_response( get_block_file_template( $request['id'], $this->post_type ), $request ); + $request->set_param( 'context', 'edit' ); + + $template = get_block_template( $request['id'], $this->post_type ); + $response = $this->prepare_item_for_response( $template, $request ); + + return rest_ensure_response( $response ); } $changes = $this->prepare_item_for_database( $request ); @@ -241,7 +249,13 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { } else { $result = wp_insert_post( wp_slash( (array) $changes ), true ); } + if ( is_wp_error( $result ) ) { + if ( 'db_update_error' === $result->get_error_code() ) { + $result->add_data( array( 'status' => 500 ) ); + } else { + $result->add_data( array( 'status' => 400 ) ); + } return $result; } @@ -251,10 +265,11 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { return $fields_update; } - return $this->prepare_item_for_response( - get_block_template( $request['id'], $this->post_type ), - $request - ); + $request->set_param( 'context', 'edit' ); + + $response = $this->prepare_item_for_response( $template, $request ); + + return rest_ensure_response( $response ); } /** @@ -278,15 +293,21 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function create_item( $request ) { - $changes = $this->prepare_item_for_database( $request ); - $changes->post_name = $request['slug']; - $result = wp_insert_post( wp_slash( (array) $changes ), true ); - if ( is_wp_error( $result ) ) { - return $result; + $prepared_post = $this->prepare_item_for_database( $request ); + $prepared_post->post_name = $request['slug']; + $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true ); + if ( is_wp_error( $post_id ) ) { + if ( 'db_insert_error' === $post_id->get_error_code() ) { + $post_id->add_data( array( 'status' => 500 ) ); + } else { + $post_id->add_data( array( 'status' => 400 ) ); + } + + return $post_id; } - $posts = get_block_templates( array( 'wp_id' => $result ), $this->post_type ); + $posts = get_block_templates( array( 'wp_id' => $post_id ), $this->post_type ); if ( ! count( $posts ) ) { - return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ) ); + return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ), array( 'status' => 400 ) ); } $id = $posts[0]->id; $template = get_block_template( $id, $this->post_type ); @@ -295,10 +316,13 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { return $fields_update; } - return $this->prepare_item_for_response( - get_block_template( $id, $this->post_type ), - $request - ); + $response = $this->prepare_item_for_response( $template, $request ); + $response = rest_ensure_response( $response ); + + $response->set_status( 201 ); + $response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $template->id ) ) ); + + return $response; } /** @@ -333,10 +357,12 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { $id = $template->wp_id; $force = (bool) $request['force']; + $request->set_param( 'context', 'edit' ); + // If we're forcing, then delete permanently. if ( $force ) { $previous = $this->prepare_item_for_response( $template, $request ); - wp_delete_post( $id, true ); + $result = wp_delete_post( $id, true ); $response = new WP_REST_Response(); $response->set_data( array( @@ -344,22 +370,32 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { 'previous' => $previous->get_data(), ) ); + } else { + // Otherwise, only trash if we haven't already. + if ( 'trash' === $template->status ) { + return new WP_Error( + 'rest_template_already_trashed', + __( 'The template has already been deleted.' ), + array( 'status' => 410 ) + ); + } - return $response; + // (Note that internally this falls through to `wp_delete_post()` + // if the Trash is disabled.) + $result = wp_trash_post( $id ); + $template->status = 'trash'; + $response = $this->prepare_item_for_response( $template, $request ); } - // Otherwise, only trash if we haven't already. - if ( 'trash' === $template->status ) { + if ( ! $result ) { return new WP_Error( - 'rest_template_already_trashed', - __( 'The template has already been deleted.' ), - array( 'status' => 410 ) + 'rest_cannot_delete', + __( 'The template cannot be deleted.' ), + array( 'status' => 500 ) ); } - wp_trash_post( $id ); - $template->status = 'trash'; - return $this->prepare_item_for_response( $template, $request ); + return $response; } /** @@ -392,12 +428,20 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { $changes->post_status = 'publish'; } if ( isset( $request['content'] ) ) { - $changes->post_content = $request['content']; + if ( is_string( $request['content'] ) ) { + $changes->post_content = $request['content']; + } elseif ( isset( $request['content']['raw'] ) ) { + $changes->post_content = $request['content']['raw']; + } } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_content = $template->content; } if ( isset( $request['title'] ) ) { - $changes->post_title = $request['title']; + if ( is_string( $request['title'] ) ) { + $changes->post_title = $request['title']; + } elseif ( ! empty( $request['title']['raw'] ) ) { + $changes->post_title = $request['title']['raw']; + } } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_title = $template->title; } @@ -433,31 +477,88 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // Restores the more descriptive, specific name for use within this method. $template = $item; - $result = array( - 'id' => $template->id, - 'theme' => $template->theme, - 'content' => array( 'raw' => $template->content ), - 'slug' => $template->slug, - 'source' => $template->source, - 'type' => $template->type, - 'description' => $template->description, - 'title' => array( - 'raw' => $template->title, - 'rendered' => $template->title, - ), - 'status' => $template->status, - 'wp_id' => $template->wp_id, - 'has_theme_file' => $template->has_theme_file, - ); - if ( 'wp_template_part' === $template->type ) { - $result['area'] = $template->area; + $fields = $this->get_fields_for_response( $request ); + + // Base fields for every template. + $data = array(); + + if ( rest_is_field_included( 'id', $fields ) ) { + $data['id'] = $template->id; } - $result = $this->add_additional_fields_to_object( $result, $request ); + if ( rest_is_field_included( 'theme', $fields ) ) { + $data['theme'] = $template->theme; + } - $response = rest_ensure_response( $result ); - $links = $this->prepare_links( $template->id ); + if ( rest_is_field_included( 'content', $fields ) ) { + $data['content'] = array(); + } + if ( rest_is_field_included( 'content.raw', $fields ) ) { + $data['content']['raw'] = $template->content; + } + + if ( rest_is_field_included( 'content.block_version', $fields ) ) { + $data['content']['block_version'] = block_version( $template->content ); + } + + if ( rest_is_field_included( 'slug', $fields ) ) { + $data['slug'] = $template->slug; + } + + if ( rest_is_field_included( 'source', $fields ) ) { + $data['source'] = $template->source; + } + + if ( rest_is_field_included( 'type', $fields ) ) { + $data['type'] = $template->type; + } + + if ( rest_is_field_included( 'description', $fields ) ) { + $data['description'] = $template->description; + } + + if ( rest_is_field_included( 'title', $fields ) ) { + $data['title'] = array(); + } + + if ( rest_is_field_included( 'title.raw', $fields ) ) { + $data['title']['raw'] = $template->title; + } + + if ( rest_is_field_included( 'title.rendered', $fields ) ) { + if ( $template->wp_id ) { + /** This filter is documented in wp-includes/post-template.php */ + $data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id ); + } else { + $data['title']['rendered'] = $template->title; + } + } + + if ( rest_is_field_included( 'status', $fields ) ) { + $data['status'] = $template->status; + } + + if ( rest_is_field_included( 'wp_id', $fields ) ) { + $data['wp_id'] = (int) $template->wp_id; + } + + if ( rest_is_field_included( 'has_theme_file', $fields ) ) { + $data['has_theme_file'] = (bool) $template->has_theme_file; + } + + if ( rest_is_field_included( 'area', $fields ) && 'wp_template_part' === $template->type ) { + $data['area'] = $template->area; + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + $response = rest_ensure_response( $data ); + + $links = $this->prepare_links( $template->id ); $response->add_links( $links ); if ( ! empty( $links['self']['href'] ) ) { $actions = $this->get_available_actions(); @@ -530,7 +631,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { */ public function get_collection_params() { return array( - 'context' => $this->get_context_param(), + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 'wp_id' => array( 'description' => __( 'Limit to the specified post id.' ), 'type' => 'integer', @@ -583,6 +684,11 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), ), + 'type' => array( + 'description' => __( 'Type of template.' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + ), 'source' => array( 'description' => __( 'Source of template' ), 'type' => 'string', @@ -594,12 +700,38 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { 'type' => array( 'object', 'string' ), 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), + 'properties' => array( + 'raw' => array( + 'description' => __( 'Content for the template, as it exists in the database.' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + ), + 'block_version' => array( + 'description' => __( 'Version of the content block format used by the template.' ), + 'type' => 'integer', + 'context' => array( 'edit' ), + 'readonly' => true, + ), + ), ), 'title' => array( 'description' => __( 'Title of template.' ), 'type' => array( 'object', 'string' ), 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), + 'properties' => array( + 'raw' => array( + 'description' => __( 'Title for the template, as it exists in the database.' ), + 'type' => 'string', + 'context' => array( 'view', 'edit', 'embed' ), + ), + 'rendered' => array( + 'description' => __( 'HTML title for the template, transformed for display.' ), + 'type' => 'string', + 'context' => array( 'view', 'edit', 'embed' ), + 'readonly' => true, + ), + ), ), 'description' => array( 'description' => __( 'Description of template.' ), @@ -610,6 +742,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { 'status' => array( 'description' => __( 'Status of template.' ), 'type' => 'string', + 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), 'default' => 'publish', 'context' => array( 'embed', 'view', 'edit' ), ), diff --git a/tests/phpunit/tests/rest-api/rest-templates-controller.php b/tests/phpunit/tests/rest-api/rest-templates-controller.php deleted file mode 100644 index 356c868d76..0000000000 --- a/tests/phpunit/tests/rest-api/rest-templates-controller.php +++ /dev/null @@ -1,197 +0,0 @@ -user->create( - array( - 'role' => 'administrator', - ) - ); - - // Set up template post. - $args = array( - 'post_type' => 'wp_template', - 'post_name' => 'my_template', - 'post_title' => 'My Template', - 'post_content' => 'Content', - 'post_excerpt' => 'Description of my template.', - 'tax_input' => array( - 'wp_theme' => array( - get_stylesheet(), - ), - ), - ); - self::$post = self::factory()->post->create_and_get( $args ); - wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' ); - } - - public static function wpTearDownAfterClass() { - wp_delete_post( self::$post->ID ); - } - - - public function test_register_routes() { - $routes = rest_get_server()->get_routes(); - $this->assertArrayHasKey( '/wp/v2/templates', $routes ); - $this->assertArrayHasKey( '/wp/v2/templates/(?P[\/\w-]+)', $routes ); - } - - public function test_context_param() { - // TODO: Implement test_context_param() method. - } - - public function test_get_items() { - function find_and_normalize_template_by_id( $templates, $id ) { - foreach ( $templates as $template ) { - if ( $template['id'] === $id ) { - unset( $template['content'] ); - unset( $template['_links'] ); - return $template; - } - } - - return null; - } - - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/templates' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 ); - - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/templates' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - $this->assertSame( - array( - 'id' => 'default//my_template', - 'theme' => 'default', - 'slug' => 'my_template', - 'source' => 'custom', - 'type' => 'wp_template', - 'description' => 'Description of my template.', - 'title' => array( - 'raw' => 'My Template', - 'rendered' => 'My Template', - ), - 'status' => 'publish', - 'wp_id' => self::$post->ID, - 'has_theme_file' => false, - ), - find_and_normalize_template_by_id( $data, 'default//my_template' ) - ); - } - - public function test_get_item() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - unset( $data['content'] ); - unset( $data['_links'] ); - - $this->assertSame( - array( - 'id' => 'default//my_template', - 'theme' => 'default', - 'slug' => 'my_template', - 'source' => 'custom', - 'type' => 'wp_template', - 'description' => 'Description of my template.', - 'title' => array( - 'raw' => 'My Template', - 'rendered' => 'My Template', - ), - 'status' => 'publish', - 'wp_id' => self::$post->ID, - 'has_theme_file' => false, - ), - $data - ); - } - - public function test_create_item() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); - $request->set_body_params( - array( - 'slug' => 'my_custom_template', - 'description' => 'Just a description', - 'title' => 'My Template', - 'content' => 'Content', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - unset( $data['_links'] ); - unset( $data['wp_id'] ); - - $this->assertSame( - array( - 'id' => 'default//my_custom_template', - 'theme' => 'default', - 'content' => array( - 'raw' => 'Content', - ), - 'slug' => 'my_custom_template', - 'source' => 'custom', - 'type' => 'wp_template', - 'description' => 'Just a description', - 'title' => array( - 'raw' => 'My Template', - 'rendered' => 'My Template', - ), - 'status' => 'publish', - 'has_theme_file' => false, - ), - $data - ); - } - - public function test_update_item() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'PUT', '/wp/v2/templates/default//my_template' ); - $request->set_body_params( - array( - 'title' => 'My new Index Title', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 'My new Index Title', $data['title']['raw'] ); - $this->assertSame( 'custom', $data['source'] ); - } - - public function test_delete_item() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_template_not_found', $response, 404 ); - } - - public function test_prepare_item() { - // TODO: Implement test_prepare_item() method. - } - - public function test_get_item_schema() { - // TODO: Implement test_get_item_schema() method. - } -} diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php new file mode 100644 index 0000000000..3d1699e16f --- /dev/null +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -0,0 +1,400 @@ +user->create( + array( + 'role' => 'administrator', + ) + ); + + // Set up template post. + $args = array( + 'post_type' => 'wp_template', + 'post_name' => 'my_template', + 'post_title' => 'My Template', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template.', + 'tax_input' => array( + 'wp_theme' => array( + get_stylesheet(), + ), + ), + ); + self::$post = self::factory()->post->create_and_get( $args ); + wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post->ID ); + } + + + public function test_register_routes() { + $routes = rest_get_server()->get_routes(); + $this->assertArrayHasKey( '/wp/v2/templates', $routes ); + $this->assertArrayHasKey( '/wp/v2/templates/(?P[\/\w-]+)', $routes ); + } + + /** + * @covers WP_REST_Templates_Controller::get_context_param + */ + public function test_context_param() { + // Collection. + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); + $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + // Single. + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/default//my_template' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); + $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + } + + /** + * @covers WP_REST_Templates_Controller::get_items + */ + public function test_get_items() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/templates' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( + array( + 'id' => 'default//my_template', + 'theme' => 'default', + 'slug' => 'my_template', + 'source' => 'custom', + 'type' => 'wp_template', + 'description' => 'Description of my template.', + 'title' => array( + 'raw' => 'My Template', + 'rendered' => 'My Template', + ), + 'status' => 'publish', + 'wp_id' => self::$post->ID, + 'has_theme_file' => false, + ), + $this->find_and_normalize_template_by_id( $data, 'default//my_template' ) + ); + } + + /** + * @covers WP_REST_Templates_Controller::get_items + */ + public function test_get_items_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/templates' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, 401 ); + } + + /** + * @covers WP_REST_Templates_Controller::get_item + */ + public function test_get_item() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + unset( $data['content'] ); + unset( $data['_links'] ); + + $this->assertSame( + array( + 'id' => 'default//my_template', + 'theme' => 'default', + 'slug' => 'my_template', + 'source' => 'custom', + 'type' => 'wp_template', + 'description' => 'Description of my template.', + 'title' => array( + 'raw' => 'My Template', + 'rendered' => 'My Template', + ), + 'status' => 'publish', + 'wp_id' => self::$post->ID, + 'has_theme_file' => false, + ), + $data + ); + } + + /** + * @ticket 54422 + * @covers WP_REST_Templates_Controller::create_item + */ + public function test_create_item() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); + $request->set_body_params( + array( + 'slug' => 'my_custom_template', + 'description' => 'Just a description', + 'title' => 'My Template', + 'content' => 'Content', + ) + ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + unset( $data['_links'] ); + unset( $data['wp_id'] ); + + $this->assertSame( + array( + 'id' => 'default//my_custom_template', + 'theme' => 'default', + 'content' => array( + 'raw' => 'Content', + ), + 'slug' => 'my_custom_template', + 'source' => 'custom', + 'type' => 'wp_template', + 'description' => 'Just a description', + 'title' => array( + 'raw' => 'My Template', + 'rendered' => 'My Template', + ), + 'status' => 'publish', + 'has_theme_file' => false, + ), + $data + ); + } + + /** + * @ticket 54422 + * @covers WP_REST_Templates_Controller::create_item + */ + public function test_create_item_raw() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/templates' ); + $request->set_body_params( + array( + 'slug' => 'my_custom_template_raw', + 'description' => 'Just a description', + 'title' => array( + 'raw' => 'My Template', + ), + 'content' => array( + 'raw' => 'Content', + ), + ) + ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + unset( $data['_links'] ); + unset( $data['wp_id'] ); + + $this->assertSame( + array( + 'id' => 'default//my_custom_template_raw', + 'theme' => 'default', + 'content' => array( + 'raw' => 'Content', + ), + 'slug' => 'my_custom_template_raw', + 'source' => 'custom', + 'type' => 'wp_template', + 'description' => 'Just a description', + 'title' => array( + 'raw' => 'My Template', + 'rendered' => 'My Template', + ), + 'status' => 'publish', + 'has_theme_file' => false, + ), + $data + ); + } + + /** + * @covers WP_REST_Templates_Controller::update_item + */ + public function test_update_item() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'PUT', '/wp/v2/templates/default//my_template' ); + $request->set_body_params( + array( + 'title' => 'My new Index Title', + ) + ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 'My new Index Title', $data['title']['raw'] ); + $this->assertSame( 'custom', $data['source'] ); + } + + /** + * @covers WP_REST_Templates_Controller::update_item + */ + public function test_update_item_raw() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'PUT', '/wp/v2/templates/default//my_template' ); + $request->set_body_params( + array( + 'title' => array( 'raw' => 'My new raw Index Title' ), + ) + ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 'My new raw Index Title', $data['title']['raw'] ); + $this->assertSame( 'custom', $data['source'] ); + } + + /** + * @covers WP_REST_Templates_Controller::delete_item + */ + public function test_delete_item() { + // Set up template post. + $args = array( + 'post_type' => 'wp_template', + 'post_name' => 'my_test_template', + 'post_title' => 'My Template', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template.', + 'tax_input' => array( + 'wp_theme' => array( + get_stylesheet(), + ), + ), + ); + $post_id = self::factory()->post->create( $args ); + wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/default//my_test_template' ); + $request->set_param( 'force', 'false' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSame( 'My Template', $data['title']['raw'] ); + $this->assertSame( 'trash', $data['status'] ); + } + + /** + * @covers WP_REST_Templates_Controller::delete_item + */ + public function test_delete_item_skip_trash() { + // Set up template post. + $args = array( + 'post_type' => 'wp_template', + 'post_name' => 'my_test_template', + 'post_title' => 'My Template', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template.', + 'tax_input' => array( + 'wp_theme' => array( + get_stylesheet(), + ), + ), + ); + $post_id = self::factory()->post->create( $args ); + wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/default//my_test_template' ); + $request->set_param( 'force', 'true' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertSame( 200, $response->get_status() ); + $data = $response->get_data(); + $this->assertTrue( $data['deleted'] ); + $this->assertNotEmpty( $data['previous'] ); + } + + /** + * @covers WP_REST_Templates_Controller::delete_item + */ + public function test_delete_item_fail() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_template_not_found', $response, 404 ); + } + + public function test_prepare_item() { + // TODO: Implement test_prepare_item() method. + } + + public function test_prepare_item_limit_fields() { + wp_set_current_user( self::$admin_id ); + + $endpoint = new WP_REST_Templates_Controller( 'wp_template' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' ); + $request->set_param( 'context', 'edit' ); + $request->set_param( '_fields', 'id,slug' ); + $obj = get_block_template( 'default//my_template', 'wp_template' ); + $response = $endpoint->prepare_item_for_response( $obj, $request ); + $this->assertSame( + array( + 'id', + 'slug', + ), + array_keys( $response->get_data() ) + ); + } + + /** + * @ticket 54422 + * @covers WP_REST_Templates_Controller::get_item_schema + */ + public function test_get_item_schema() { + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $properties = $data['schema']['properties']; + $this->assertCount( 11, $properties ); + $this->assertArrayHasKey( 'id', $properties ); + $this->assertArrayHasKey( 'description', $properties ); + $this->assertArrayHasKey( 'slug', $properties ); + $this->assertArrayHasKey( 'theme', $properties ); + $this->assertArrayHasKey( 'type', $properties ); + $this->assertArrayHasKey( 'source', $properties ); + $this->assertArrayHasKey( 'content', $properties ); + $this->assertArrayHasKey( 'title', $properties ); + $this->assertArrayHasKey( 'description', $properties ); + $this->assertArrayHasKey( 'status', $properties ); + $this->assertArrayHasKey( 'wp_id', $properties ); + $this->assertArrayHasKey( 'has_theme_file', $properties ); + } + + protected function find_and_normalize_template_by_id( $templates, $id ) { + foreach ( $templates as $template ) { + if ( $template['id'] === $id ) { + unset( $template['content'] ); + unset( $template['_links'] ); + return $template; + } + } + + return null; + } + +} diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 8016a57d6a..3c6f4906af 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -5000,6 +5000,7 @@ mockedApiResponse.Schema = { "embed", "edit" ], + "default": "view", "required": false }, "wp_id": { @@ -5036,6 +5037,11 @@ mockedApiResponse.Schema = { "type": "string", "required": false }, + "type": { + "description": "Type of template.", + "type": "string", + "required": false + }, "content": { "default": "", "description": "Content of template.", @@ -5043,6 +5049,24 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Content for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit" + ] + }, + "block_version": { + "description": "Version of the content block format used by the template.", + "type": "integer", + "context": [ + "edit" + ], + "readonly": true + } + }, "required": false }, "title": { @@ -5052,6 +5076,27 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Title for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ] + }, + "rendered": { + "description": "HTML title for the template, transformed for display.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ], + "readonly": true + } + }, "required": false }, "description": { @@ -5064,6 +5109,13 @@ mockedApiResponse.Schema = { "default": "publish", "description": "Status of template.", "type": "string", + "enum": [ + "publish", + "future", + "draft", + "pending", + "private" + ], "required": false } } @@ -5096,6 +5148,17 @@ mockedApiResponse.Schema = { "description": "The id of a template", "type": "string", "required": false + }, + "context": { + "description": "Scope under which the request is made; determines fields present in response.", + "type": "string", + "enum": [ + "view", + "embed", + "edit" + ], + "default": "view", + "required": false } } }, @@ -5106,6 +5169,11 @@ mockedApiResponse.Schema = { "PATCH" ], "args": { + "id": { + "description": "The id of a template", + "type": "string", + "required": false + }, "slug": { "description": "Unique slug identifying the template.", "type": "string", @@ -5118,12 +5186,35 @@ mockedApiResponse.Schema = { "type": "string", "required": false }, + "type": { + "description": "Type of template.", + "type": "string", + "required": false + }, "content": { "description": "Content of template.", "type": [ "object", "string" ], + "properties": { + "raw": { + "description": "Content for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit" + ] + }, + "block_version": { + "description": "Version of the content block format used by the template.", + "type": "integer", + "context": [ + "edit" + ], + "readonly": true + } + }, "required": false }, "title": { @@ -5132,6 +5223,27 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Title for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ] + }, + "rendered": { + "description": "HTML title for the template, transformed for display.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ], + "readonly": true + } + }, "required": false }, "description": { @@ -5142,6 +5254,13 @@ mockedApiResponse.Schema = { "status": { "description": "Status of template.", "type": "string", + "enum": [ + "publish", + "future", + "draft", + "pending", + "private" + ], "required": false } } @@ -5151,6 +5270,11 @@ mockedApiResponse.Schema = { "DELETE" ], "args": { + "id": { + "description": "The id of a template", + "type": "string", + "required": false + }, "force": { "type": "boolean", "default": false, @@ -5371,12 +5495,35 @@ mockedApiResponse.Schema = { "type": "string", "required": false }, + "type": { + "description": "Type of template.", + "type": "string", + "required": false + }, "content": { "description": "Content of template.", "type": [ "object", "string" ], + "properties": { + "raw": { + "description": "Content for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit" + ] + }, + "block_version": { + "description": "Version of the content block format used by the template.", + "type": "integer", + "context": [ + "edit" + ], + "readonly": true + } + }, "required": false }, "title": { @@ -5385,6 +5532,27 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Title for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ] + }, + "rendered": { + "description": "HTML title for the template, transformed for display.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ], + "readonly": true + } + }, "required": false }, "description": { @@ -5395,6 +5563,13 @@ mockedApiResponse.Schema = { "status": { "description": "Status of template.", "type": "string", + "enum": [ + "publish", + "future", + "draft", + "pending", + "private" + ], "required": false } } @@ -5457,6 +5632,7 @@ mockedApiResponse.Schema = { "embed", "edit" ], + "default": "view", "required": false }, "wp_id": { @@ -5493,6 +5669,11 @@ mockedApiResponse.Schema = { "type": "string", "required": false }, + "type": { + "description": "Type of template.", + "type": "string", + "required": false + }, "content": { "default": "", "description": "Content of template.", @@ -5500,6 +5681,24 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Content for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit" + ] + }, + "block_version": { + "description": "Version of the content block format used by the template.", + "type": "integer", + "context": [ + "edit" + ], + "readonly": true + } + }, "required": false }, "title": { @@ -5509,6 +5708,27 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Title for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ] + }, + "rendered": { + "description": "HTML title for the template, transformed for display.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ], + "readonly": true + } + }, "required": false }, "description": { @@ -5521,6 +5741,13 @@ mockedApiResponse.Schema = { "default": "publish", "description": "Status of template.", "type": "string", + "enum": [ + "publish", + "future", + "draft", + "pending", + "private" + ], "required": false }, "area": { @@ -5558,6 +5785,17 @@ mockedApiResponse.Schema = { "description": "The id of a template", "type": "string", "required": false + }, + "context": { + "description": "Scope under which the request is made; determines fields present in response.", + "type": "string", + "enum": [ + "view", + "embed", + "edit" + ], + "default": "view", + "required": false } } }, @@ -5568,6 +5806,11 @@ mockedApiResponse.Schema = { "PATCH" ], "args": { + "id": { + "description": "The id of a template", + "type": "string", + "required": false + }, "slug": { "description": "Unique slug identifying the template.", "type": "string", @@ -5580,12 +5823,35 @@ mockedApiResponse.Schema = { "type": "string", "required": false }, + "type": { + "description": "Type of template.", + "type": "string", + "required": false + }, "content": { "description": "Content of template.", "type": [ "object", "string" ], + "properties": { + "raw": { + "description": "Content for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit" + ] + }, + "block_version": { + "description": "Version of the content block format used by the template.", + "type": "integer", + "context": [ + "edit" + ], + "readonly": true + } + }, "required": false }, "title": { @@ -5594,6 +5860,27 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Title for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ] + }, + "rendered": { + "description": "HTML title for the template, transformed for display.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ], + "readonly": true + } + }, "required": false }, "description": { @@ -5604,6 +5891,13 @@ mockedApiResponse.Schema = { "status": { "description": "Status of template.", "type": "string", + "enum": [ + "publish", + "future", + "draft", + "pending", + "private" + ], "required": false }, "area": { @@ -5618,6 +5912,11 @@ mockedApiResponse.Schema = { "DELETE" ], "args": { + "id": { + "description": "The id of a template", + "type": "string", + "required": false + }, "force": { "type": "boolean", "default": false, @@ -5838,12 +6137,35 @@ mockedApiResponse.Schema = { "type": "string", "required": false }, + "type": { + "description": "Type of template.", + "type": "string", + "required": false + }, "content": { "description": "Content of template.", "type": [ "object", "string" ], + "properties": { + "raw": { + "description": "Content for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit" + ] + }, + "block_version": { + "description": "Version of the content block format used by the template.", + "type": "integer", + "context": [ + "edit" + ], + "readonly": true + } + }, "required": false }, "title": { @@ -5852,6 +6174,27 @@ mockedApiResponse.Schema = { "object", "string" ], + "properties": { + "raw": { + "description": "Title for the template, as it exists in the database.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ] + }, + "rendered": { + "description": "HTML title for the template, transformed for display.", + "type": "string", + "context": [ + "view", + "edit", + "embed" + ], + "readonly": true + } + }, "required": false }, "description": { @@ -5862,6 +6205,13 @@ mockedApiResponse.Schema = { "status": { "description": "Status of template.", "type": "string", + "enum": [ + "publish", + "future", + "draft", + "pending", + "private" + ], "required": false }, "area": {