diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php index 27c66ba2fc..c1ddfeb2f1 100644 --- a/tests/phpunit/tests/rest-api/rest-categories-controller.php +++ b/tests/phpunit/tests/rest-api/rest-categories-controller.php @@ -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 = $this->server->get_routes(); $this->assertArrayHasKey( '/wp/v2/categories', $routes ); @@ -575,6 +605,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 = $this->server->dispatch( $request ); @@ -713,12 +771,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 = $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() { diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php index efec26077e..6dcfbde3cc 100644 --- a/tests/phpunit/tests/rest-api/rest-tags-controller.php +++ b/tests/phpunit/tests/rest-api/rest-tags-controller.php @@ -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 = $this->server->get_routes(); $this->assertArrayHasKey( '/wp/v2/tags', $routes ); @@ -527,6 +557,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 = $this->server->dispatch( $request ); @@ -659,12 +718,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 = $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() { diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 576fc67e7e..a454e15f50 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -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": [ @@ -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": [] } }; @@ -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": [ @@ -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": "" } };