Make WordPress Core


Ignore:
Timestamp:
09/18/2018 03:29:41 AM (7 years ago)
Author:
SergeyBiryukov
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.
Merges [43567] to the 4.9 branch.
See #39122.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r43637 r43646  
    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();
     
    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 );
     
    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() );
     
    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
Note: See TracChangeset for help on using the changeset viewer.