Skip to content

Commit

Permalink
Tests: Improve coverage for REST API term meta registration.
Browse files Browse the repository at this point in the history
Introduce tests to validate that register_meta and register_term_meta work as expected in WP_REST_Terms_Controller.

Props timmydcrawford.
Merges [43567] to the 4.9 branch.
See #39122.

git-svn-id: https://develop.svn.wordpress.org/branches/4.9@43646 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 18, 2018
1 parent 13bd954 commit c50b216
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 4 deletions.
66 changes: 66 additions & 0 deletions tests/phpunit/tests/rest-api/rest-categories-controller.php
Expand Up @@ -38,6 +38,36 @@ public static function wpTearDownAfterClass() {
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 = $this->server->get_routes();
$this->assertArrayHasKey( '/wp/v2/categories', $routes );
Expand Down Expand Up @@ -575,6 +605,34 @@ public function test_get_item() {
$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 = $this->server->dispatch( $request );
Expand Down Expand Up @@ -713,12 +771,20 @@ public function test_update_item() {
$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 = $this->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() {
Expand Down
67 changes: 67 additions & 0 deletions tests/phpunit/tests/rest-api/rest-tags-controller.php
Expand Up @@ -55,6 +55,36 @@ public static function wpTearDownAfterClass() {
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 = $this->server->get_routes();
$this->assertArrayHasKey( '/wp/v2/tags', $routes );
Expand Down Expand Up @@ -527,6 +557,35 @@ public function test_get_item() {
$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 = $this->server->dispatch( $request );
Expand Down Expand Up @@ -659,12 +718,20 @@ public function test_update_item() {
$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 = $this->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() {
Expand Down
22 changes: 18 additions & 4 deletions tests/qunit/fixtures/wp-api-generated.js
Expand Up @@ -4278,7 +4278,11 @@ mockedApiResponse.CategoriesCollection = [
"taxonomy": "category",
"parent": 0,
"meta": {
"meta_key": ""
"test_single": "",
"test_multi": [],
"meta_key": "",
"test_cat_single": "",
"test_cat_multi": []
},
"_links": {
"self": [
Expand Down Expand Up @@ -4322,7 +4326,11 @@ mockedApiResponse.CategoryModel = {
"taxonomy": "category",
"parent": 0,
"meta": {
"meta_key": ""
"test_single": "",
"test_multi": [],
"meta_key": "",
"test_cat_single": "",
"test_cat_multi": []
}
};

Expand All @@ -4336,7 +4344,10 @@ mockedApiResponse.TagsCollection = [
"slug": "restapi-client-fixture-tag",
"taxonomy": "post_tag",
"meta": {
"meta_key": "meta_value"
"test_single": "",
"test_multi": [],
"meta_key": "meta_value",
"test_tag_meta": ""
},
"_links": {
"self": [
Expand Down Expand Up @@ -4379,7 +4390,10 @@ mockedApiResponse.TagModel = {
"slug": "restapi-client-fixture-tag",
"taxonomy": "post_tag",
"meta": {
"meta_key": "meta_value"
"test_single": "",
"test_multi": [],
"meta_key": "meta_value",
"test_tag_meta": ""
}
};

Expand Down

0 comments on commit c50b216

Please sign in to comment.