Make WordPress Core


Ignore:
Timestamp:
10/11/2018 06:31:43 AM (8 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 5.0 branch.
Fixes #39122.

Location:
branches/5.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r43445 r43713  
    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
     
    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' );
     
    714772                $request->set_param( 'description', 'New Description' );
    715773                $request->set_param( 'slug', 'new-slug' );
    716                 $response = $this->server->dispatch( $request );
     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                ) );
     779                $response = rest_get_server()->dispatch( $request );
    717780                $this->assertEquals( 200, $response->get_status() );
    718781                $data = $response->get_data();
     
    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
Note: See TracChangeset for help on using the changeset viewer.