Make WordPress Core

Changeset 43567


Ignore:
Timestamp:
08/12/2018 12:05:39 AM (8 years ago)
Author:
kadamwhite
Message:

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.

Location:
trunk/tests/phpunit/tests/rest-api
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r43440 r43567  
    3737                self::delete_user( self::$administrator );
    3838                self::delete_user( self::$subscriber );
     39        }
     40
     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                ));
    3969        }
    4070
     
    635665        }
    636666
     667        public function test_get_item_meta() {
     668                $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
     669                $response = rest_get_server()->dispatch( $request );
     670                $data     = $response->get_data();
     671                $this->assertArrayHasKey( 'meta', $data );
     672
     673                $meta = (array) $data['meta'];
     674                $this->assertArrayHasKey( 'test_single', $meta );
     675                $this->assertEquals( $meta['test_single'], '' );
     676                $this->assertArrayHasKey( 'test_multi', $meta );
     677                $this->assertEquals( $meta['test_multi'], array() );
     678                $this->assertArrayHasKey( 'test_cat_single', $meta );
     679                $this->assertEquals( $meta['test_cat_single'], '' );
     680                $this->assertArrayHasKey( 'test_cat_multi', $meta );
     681                $this->assertEquals( $meta['test_cat_multi'], array() );
     682        }
     683
     684        public function test_get_item_meta_registered_for_different_taxonomy() {
     685                $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
     686                $response = rest_get_server()->dispatch( $request );
     687                $data = $response->get_data();
     688                $this->assertArrayHasKey( 'meta', $data );
     689                $this->assertArrayHasKey( 'meta', $data );
     690
     691                $meta = (array) $data['meta'];
     692                $this->assertEquals( false, isset( $meta['test_tag_meta'] ) );
     693        }
     694
    637695        public function test_get_term_invalid_taxonomy() {
    638696                $request  = new WP_REST_Request( 'GET', '/wp/v2/invalid-taxonomy/1' );
     
    783841                $request->set_param( 'description', 'New Description' );
    784842                $request->set_param( 'slug', 'new-slug' );
     843                $request->set_param( 'meta', array(
     844                        'test_single' => 'just meta',
     845                        'test_cat_single' => 'category-specific meta',
     846                        'test_tag_meta' => 'tag-specific meta',
     847                ) );
    785848                $response = rest_get_server()->dispatch( $request );
    786849                $this->assertEquals( 200, $response->get_status() );
     
    789852                $this->assertEquals( 'New Description', $data['description'] );
    790853                $this->assertEquals( 'new-slug', $data['slug'] );
     854                $this->assertEquals( 'just meta', $data['meta']['test_single'] );
     855                $this->assertEquals( 'category-specific meta', $data['meta']['test_cat_single'] );
     856                $this->assertFalse( isset( $data['meta']['test_tag_meta'] ) );
    791857        }
    792858
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r43440 r43567  
    5454                self::delete_user( self::$editor );
    5555                self::delete_user( self::$subscriber );
     56        }
     57
     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                ));
    5686        }
    5787
     
    573603        }
    574604
     605        public function test_get_item_meta() {
     606                $id       = $this->factory->tag->create();
     607                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
     608                $response = rest_get_server()->dispatch( $request );
     609                $data = $response->get_data();
     610                $this->assertArrayHasKey( 'meta', $data );
     611
     612                $meta = (array) $data['meta'];
     613                $this->assertArrayHasKey( 'test_single', $meta );
     614                $this->assertEquals( $meta['test_single'], '' );
     615                $this->assertArrayHasKey( 'test_multi', $meta );
     616                $this->assertEquals( $meta['test_multi'], array() );
     617                $this->assertArrayHasKey( 'test_tag_single', $meta );
     618                $this->assertEquals( $meta['test_tag_single'], '' );
     619                $this->assertArrayHasKey( 'test_tag_multi', $meta );
     620                $this->assertEquals( $meta['test_tag_multi'], array() );
     621        }
     622
     623        public function test_get_item_meta_registered_for_different_taxonomy() {
     624                $id       = $this->factory->tag->create();
     625                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
     626                $response = rest_get_server()->dispatch( $request );
     627                $data = $response->get_data();
     628                $this->assertArrayHasKey( 'meta', $data );
     629
     630                $meta = (array) $data['meta'];
     631                $this->assertEquals( false, isset( $meta['test_cat_meta'] ) );
     632        }
     633
    575634        public function test_get_term_invalid_term() {
    576635                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     
    684743                $request->set_param( 'description', 'New Description' );
    685744                $request->set_param( 'slug', 'new-slug' );
     745                $request->set_param( 'meta', array(
     746                        'test_single' => 'just meta',
     747                        'test_tag_single' => 'tag-specific meta',
     748                        'test_cat_meta' => 'category-specific meta',
     749                ) );
    686750                $response = rest_get_server()->dispatch( $request );
    687751                $this->assertEquals( 200, $response->get_status() );
     
    690754                $this->assertEquals( 'New Description', $data['description'] );
    691755                $this->assertEquals( 'new-slug', $data['slug'] );
     756                $this->assertEquals( 'just meta', $data['meta']['test_single'] );
     757                $this->assertEquals( 'tag-specific meta', $data['meta']['test_tag_single'] );
     758                $this->assertFalse( isset( $data['meta']['test_cat_meta'] ) );
    692759        }
    693760
Note: See TracChangeset for help on using the changeset viewer.