Skip to content

Commit c50b216

Browse files
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. 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
1 parent 13bd954 commit c50b216

File tree

3 files changed

+151
-4
lines changed

3 files changed

+151
-4
lines changed

tests/phpunit/tests/rest-api/rest-categories-controller.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@ public static function wpTearDownAfterClass() {
3838
self::delete_user( self::$subscriber );
3939
}
4040

41+
public function setUp() {
42+
parent::setUp();
43+
44+
register_meta( 'term', 'test_single', array(
45+
'show_in_rest' => true,
46+
'single' => true,
47+
'type' => 'string',
48+
));
49+
register_meta( 'term', 'test_multi', array(
50+
'show_in_rest' => true,
51+
'single' => false,
52+
'type' => 'string',
53+
));
54+
register_term_meta( 'category', 'test_cat_single', array(
55+
'show_in_rest' => true,
56+
'single' => true,
57+
'type' => 'string',
58+
));
59+
register_term_meta( 'category', 'test_cat_multi', array(
60+
'show_in_rest' => true,
61+
'single' => false,
62+
'type' => 'string',
63+
));
64+
register_term_meta( 'post_tag', 'test_tag_meta', array(
65+
'show_in_rest' => true,
66+
'single' => true,
67+
'type' => 'string',
68+
));
69+
}
70+
4171
public function test_register_routes() {
4272
$routes = $this->server->get_routes();
4373
$this->assertArrayHasKey( '/wp/v2/categories', $routes );
@@ -575,6 +605,34 @@ public function test_get_item() {
575605
$this->check_get_taxonomy_term_response( $response );
576606
}
577607

608+
public function test_get_item_meta() {
609+
$request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
610+
$response = rest_get_server()->dispatch( $request );
611+
$data = $response->get_data();
612+
$this->assertArrayHasKey( 'meta', $data );
613+
614+
$meta = (array) $data['meta'];
615+
$this->assertArrayHasKey( 'test_single', $meta );
616+
$this->assertEquals( $meta['test_single'], '' );
617+
$this->assertArrayHasKey( 'test_multi', $meta );
618+
$this->assertEquals( $meta['test_multi'], array() );
619+
$this->assertArrayHasKey( 'test_cat_single', $meta );
620+
$this->assertEquals( $meta['test_cat_single'], '' );
621+
$this->assertArrayHasKey( 'test_cat_multi', $meta );
622+
$this->assertEquals( $meta['test_cat_multi'], array() );
623+
}
624+
625+
public function test_get_item_meta_registered_for_different_taxonomy() {
626+
$request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
627+
$response = rest_get_server()->dispatch( $request );
628+
$data = $response->get_data();
629+
$this->assertArrayHasKey( 'meta', $data );
630+
$this->assertArrayHasKey( 'meta', $data );
631+
632+
$meta = (array) $data['meta'];
633+
$this->assertEquals( false, isset( $meta['test_tag_meta'] ) );
634+
}
635+
578636
public function test_get_term_invalid_taxonomy() {
579637
$request = new WP_REST_Request( 'GET', '/wp/v2/invalid-taxonomy/1' );
580638
$response = $this->server->dispatch( $request );
@@ -713,12 +771,20 @@ public function test_update_item() {
713771
$request->set_param( 'name', 'New Name' );
714772
$request->set_param( 'description', 'New Description' );
715773
$request->set_param( 'slug', 'new-slug' );
774+
$request->set_param( 'meta', array(
775+
'test_single' => 'just meta',
776+
'test_cat_single' => 'category-specific meta',
777+
'test_tag_meta' => 'tag-specific meta',
778+
) );
716779
$response = $this->server->dispatch( $request );
717780
$this->assertEquals( 200, $response->get_status() );
718781
$data = $response->get_data();
719782
$this->assertEquals( 'New Name', $data['name'] );
720783
$this->assertEquals( 'New Description', $data['description'] );
721784
$this->assertEquals( 'new-slug', $data['slug'] );
785+
$this->assertEquals( 'just meta', $data['meta']['test_single'] );
786+
$this->assertEquals( 'category-specific meta', $data['meta']['test_cat_single'] );
787+
$this->assertFalse( isset( $data['meta']['test_tag_meta'] ) );
722788
}
723789

724790
public function test_update_item_invalid_taxonomy() {

tests/phpunit/tests/rest-api/rest-tags-controller.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ public static function wpTearDownAfterClass() {
5555
self::delete_user( self::$subscriber );
5656
}
5757

58+
public function setUp() {
59+
parent::setUp();
60+
61+
register_meta( 'term', 'test_single', array(
62+
'show_in_rest' => true,
63+
'single' => true,
64+
'type' => 'string',
65+
));
66+
register_meta( 'term', 'test_multi', array(
67+
'show_in_rest' => true,
68+
'single' => false,
69+
'type' => 'string',
70+
));
71+
register_term_meta( 'post_tag', 'test_tag_single', array(
72+
'show_in_rest' => true,
73+
'single' => true,
74+
'type' => 'string',
75+
));
76+
register_term_meta( 'post_tag', 'test_tag_multi', array(
77+
'show_in_rest' => true,
78+
'single' => false,
79+
'type' => 'string',
80+
));
81+
register_term_meta( 'category', 'test_cat_meta', array(
82+
'show_in_rest' => true,
83+
'single' => true,
84+
'type' => 'string',
85+
));
86+
}
87+
5888
public function test_register_routes() {
5989
$routes = $this->server->get_routes();
6090
$this->assertArrayHasKey( '/wp/v2/tags', $routes );
@@ -527,6 +557,35 @@ public function test_get_item() {
527557
$this->check_get_taxonomy_term_response( $response, $id );
528558
}
529559

560+
public function test_get_item_meta() {
561+
$id = $this->factory->tag->create();
562+
$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
563+
$response = rest_get_server()->dispatch( $request );
564+
$data = $response->get_data();
565+
$this->assertArrayHasKey( 'meta', $data );
566+
567+
$meta = (array) $data['meta'];
568+
$this->assertArrayHasKey( 'test_single', $meta );
569+
$this->assertEquals( $meta['test_single'], '' );
570+
$this->assertArrayHasKey( 'test_multi', $meta );
571+
$this->assertEquals( $meta['test_multi'], array() );
572+
$this->assertArrayHasKey( 'test_tag_single', $meta );
573+
$this->assertEquals( $meta['test_tag_single'], '' );
574+
$this->assertArrayHasKey( 'test_tag_multi', $meta );
575+
$this->assertEquals( $meta['test_tag_multi'], array() );
576+
}
577+
578+
public function test_get_item_meta_registered_for_different_taxonomy() {
579+
$id = $this->factory->tag->create();
580+
$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
581+
$response = rest_get_server()->dispatch( $request );
582+
$data = $response->get_data();
583+
$this->assertArrayHasKey( 'meta', $data );
584+
585+
$meta = (array) $data['meta'];
586+
$this->assertEquals( false, isset( $meta['test_cat_meta'] ) );
587+
}
588+
530589
public function test_get_term_invalid_term() {
531590
$request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
532591
$response = $this->server->dispatch( $request );
@@ -659,12 +718,20 @@ public function test_update_item() {
659718
$request->set_param( 'name', 'New Name' );
660719
$request->set_param( 'description', 'New Description' );
661720
$request->set_param( 'slug', 'new-slug' );
721+
$request->set_param( 'meta', array(
722+
'test_single' => 'just meta',
723+
'test_tag_single' => 'tag-specific meta',
724+
'test_cat_meta' => 'category-specific meta',
725+
) );
662726
$response = $this->server->dispatch( $request );
663727
$this->assertEquals( 200, $response->get_status() );
664728
$data = $response->get_data();
665729
$this->assertEquals( 'New Name', $data['name'] );
666730
$this->assertEquals( 'New Description', $data['description'] );
667731
$this->assertEquals( 'new-slug', $data['slug'] );
732+
$this->assertEquals( 'just meta', $data['meta']['test_single'] );
733+
$this->assertEquals( 'tag-specific meta', $data['meta']['test_tag_single'] );
734+
$this->assertFalse( isset( $data['meta']['test_cat_meta'] ) );
668735
}
669736

670737
public function test_update_item_no_change() {

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,7 +4278,11 @@ mockedApiResponse.CategoriesCollection = [
42784278
"taxonomy": "category",
42794279
"parent": 0,
42804280
"meta": {
4281-
"meta_key": ""
4281+
"test_single": "",
4282+
"test_multi": [],
4283+
"meta_key": "",
4284+
"test_cat_single": "",
4285+
"test_cat_multi": []
42824286
},
42834287
"_links": {
42844288
"self": [
@@ -4322,7 +4326,11 @@ mockedApiResponse.CategoryModel = {
43224326
"taxonomy": "category",
43234327
"parent": 0,
43244328
"meta": {
4325-
"meta_key": ""
4329+
"test_single": "",
4330+
"test_multi": [],
4331+
"meta_key": "",
4332+
"test_cat_single": "",
4333+
"test_cat_multi": []
43264334
}
43274335
};
43284336

@@ -4336,7 +4344,10 @@ mockedApiResponse.TagsCollection = [
43364344
"slug": "restapi-client-fixture-tag",
43374345
"taxonomy": "post_tag",
43384346
"meta": {
4339-
"meta_key": "meta_value"
4347+
"test_single": "",
4348+
"test_multi": [],
4349+
"meta_key": "meta_value",
4350+
"test_tag_meta": ""
43404351
},
43414352
"_links": {
43424353
"self": [
@@ -4379,7 +4390,10 @@ mockedApiResponse.TagModel = {
43794390
"slug": "restapi-client-fixture-tag",
43804391
"taxonomy": "post_tag",
43814392
"meta": {
4382-
"meta_key": "meta_value"
4393+
"test_single": "",
4394+
"test_multi": [],
4395+
"meta_key": "meta_value",
4396+
"test_tag_meta": ""
43834397
}
43844398
};
43854399

0 commit comments

Comments
 (0)