mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
REST API: Make the templates controller follow core REST patterns.
The templates controller now respects the `_fields` parameter and filters the response accordingly. The schema has been updated to include all the fields returned. The `content.block_version` field has been added. The controller now returns WP_Error objects for improved error handling. Add new unit tests. Props TimothyBlynJacobs, hellofromtonya, zieladam. Fixes #54422. git-svn-id: https://develop.svn.wordpress.org/trunk@52186 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1,197 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering the templates endpoint..
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
class WP_REST_Template_Controller_Test extends WP_Test_REST_Controller_Testcase {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
private static $post;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetupBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->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<id>[\/\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.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering the templates endpoint..
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for REST API for templates.
|
||||
*
|
||||
* @covers WP_REST_Templates_Controller
|
||||
*
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_WpRestTemplatesController extends WP_Test_REST_Controller_Testcase {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
private static $post;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetupBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->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<id>[\/\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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user