Tests: Improve coverage for REST API term meta registration.

Introduce tests to validate that register_meta and register_term_meta work as expected in WP_REST_Terms_Controller.

props timmydcrawford.
Fixes #39122.


git-svn-id: https://develop.svn.wordpress.org/trunk@43567 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2018-08-12 00:05:39 +00:00
parent 245fb8e9b0
commit 20ab5ca79e
2 changed files with 133 additions and 0 deletions

View File

@ -38,6 +38,36 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
self::delete_user( self::$subscriber );
}
public function setUp() {
parent::setUp();
register_meta( 'term', 'test_single', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
register_meta( 'term', 'test_multi', array(
'show_in_rest' => true,
'single' => false,
'type' => 'string',
));
register_term_meta( 'category', 'test_cat_single', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
register_term_meta( 'category', 'test_cat_multi', array(
'show_in_rest' => true,
'single' => false,
'type' => 'string',
));
register_term_meta( 'post_tag', 'test_tag_meta', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
}
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/categories', $routes );
@ -634,6 +664,34 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
$this->check_get_taxonomy_term_response( $response );
}
public function test_get_item_meta() {
$request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$meta = (array) $data['meta'];
$this->assertArrayHasKey( 'test_single', $meta );
$this->assertEquals( $meta['test_single'], '' );
$this->assertArrayHasKey( 'test_multi', $meta );
$this->assertEquals( $meta['test_multi'], array() );
$this->assertArrayHasKey( 'test_cat_single', $meta );
$this->assertEquals( $meta['test_cat_single'], '' );
$this->assertArrayHasKey( 'test_cat_multi', $meta );
$this->assertEquals( $meta['test_cat_multi'], array() );
}
public function test_get_item_meta_registered_for_different_taxonomy() {
$request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertArrayHasKey( 'meta', $data );
$meta = (array) $data['meta'];
$this->assertEquals( false, isset( $meta['test_tag_meta'] ) );
}
public function test_get_term_invalid_taxonomy() {
$request = new WP_REST_Request( 'GET', '/wp/v2/invalid-taxonomy/1' );
$response = rest_get_server()->dispatch( $request );
@ -782,12 +840,20 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
$request->set_param( 'name', 'New Name' );
$request->set_param( 'description', 'New Description' );
$request->set_param( 'slug', 'new-slug' );
$request->set_param( 'meta', array(
'test_single' => 'just meta',
'test_cat_single' => 'category-specific meta',
'test_tag_meta' => 'tag-specific meta',
) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'New Name', $data['name'] );
$this->assertEquals( 'New Description', $data['description'] );
$this->assertEquals( 'new-slug', $data['slug'] );
$this->assertEquals( 'just meta', $data['meta']['test_single'] );
$this->assertEquals( 'category-specific meta', $data['meta']['test_cat_single'] );
$this->assertFalse( isset( $data['meta']['test_tag_meta'] ) );
}
public function test_update_item_invalid_taxonomy() {

View File

@ -55,6 +55,36 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
self::delete_user( self::$subscriber );
}
public function setUp() {
parent::setUp();
register_meta( 'term', 'test_single', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
register_meta( 'term', 'test_multi', array(
'show_in_rest' => true,
'single' => false,
'type' => 'string',
));
register_term_meta( 'post_tag', 'test_tag_single', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
register_term_meta( 'post_tag', 'test_tag_multi', array(
'show_in_rest' => true,
'single' => false,
'type' => 'string',
));
register_term_meta( 'category', 'test_cat_meta', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
}
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/tags', $routes );
@ -572,6 +602,35 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$this->check_get_taxonomy_term_response( $response, $id );
}
public function test_get_item_meta() {
$id = $this->factory->tag->create();
$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$meta = (array) $data['meta'];
$this->assertArrayHasKey( 'test_single', $meta );
$this->assertEquals( $meta['test_single'], '' );
$this->assertArrayHasKey( 'test_multi', $meta );
$this->assertEquals( $meta['test_multi'], array() );
$this->assertArrayHasKey( 'test_tag_single', $meta );
$this->assertEquals( $meta['test_tag_single'], '' );
$this->assertArrayHasKey( 'test_tag_multi', $meta );
$this->assertEquals( $meta['test_tag_multi'], array() );
}
public function test_get_item_meta_registered_for_different_taxonomy() {
$id = $this->factory->tag->create();
$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$meta = (array) $data['meta'];
$this->assertEquals( false, isset( $meta['test_cat_meta'] ) );
}
public function test_get_term_invalid_term() {
$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
$response = rest_get_server()->dispatch( $request );
@ -683,12 +742,20 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$request->set_param( 'name', 'New Name' );
$request->set_param( 'description', 'New Description' );
$request->set_param( 'slug', 'new-slug' );
$request->set_param( 'meta', array(
'test_single' => 'just meta',
'test_tag_single' => 'tag-specific meta',
'test_cat_meta' => 'category-specific meta',
) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'New Name', $data['name'] );
$this->assertEquals( 'New Description', $data['description'] );
$this->assertEquals( 'new-slug', $data['slug'] );
$this->assertEquals( 'just meta', $data['meta']['test_single'] );
$this->assertEquals( 'tag-specific meta', $data['meta']['test_tag_single'] );
$this->assertFalse( isset( $data['meta']['test_cat_meta'] ) );
}
public function test_update_item_no_change() {