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