Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.