Make WordPress Core


Ignore:
Timestamp:
03/26/2020 05:50:39 PM (6 years ago)
Author:
kadamwhite
Message:

REST API: Fix _fields filtering of registered rest fields.

Use rest_is_field_included when determining which additional fields to include to permit filtering by nested field properties.

Props Dudo, kadamwhite, TimothyBlynJacobs.
Fixes #49648.

File:
1 edited

Legend:

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

    r47450 r47511  
    4747                        )
    4848                );
     49        }
     50
     51        public function tearDown() {
     52                parent::tearDown();
     53
     54                global $wp_rest_additional_fields;
     55                $wp_rest_additional_fields = array();
    4956        }
    5057
     
    438445                $this->assertTrue( $listener->get_call_count( $method ) > $first_call_count );
    439446        }
     447
     448        /**
     449         * @dataProvider data_filter_nested_registered_rest_fields
     450         * @ticket 49648
     451         */
     452        public function test_filter_nested_registered_rest_fields( $filter, $expected ) {
     453                $controller = new WP_REST_Test_Controller();
     454
     455                register_rest_field(
     456                        'type',
     457                        'field',
     458                        array(
     459                                'schema'       => array(
     460                                        'type'        => 'object',
     461                                        'description' => 'A complex object',
     462                                        'context'     => array( 'view', 'edit' ),
     463                                        'properties'  => array(
     464                                                'a' => array(
     465                                                        'i'  => 'string',
     466                                                        'ii' => 'string',
     467                                                ),
     468                                                'b' => array(
     469                                                        'iii' => 'string',
     470                                                        'iv'  => 'string',
     471                                                ),
     472                                        ),
     473                                ),
     474                                'get_callback' => array( $this, 'register_nested_rest_field_get_callback' ),
     475                        )
     476                );
     477
     478                $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     479                $request->set_param( '_fields', $filter );
     480
     481                $response = $controller->prepare_item_for_response( array(), $request );
     482                $response = rest_filter_response_fields( $response, rest_get_server(), $request );
     483
     484                $this->assertEquals( $expected, $response->get_data() );
     485        }
     486
     487        public function register_nested_rest_field_get_callback() {
     488                return array(
     489                        'a' => array(
     490                                'i'  => 'value i',
     491                                'ii' => 'value ii',
     492                        ),
     493                        'b' => array(
     494                                'iii' => 'value iii',
     495                                'iv'  => 'value iv',
     496                        ),
     497                );
     498        }
     499
     500        public function data_filter_nested_registered_rest_fields() {
     501                return array(
     502                        array(
     503                                'field',
     504                                array(
     505                                        'field' => array(
     506                                                'a' => array(
     507                                                        'i'  => 'value i',
     508                                                        'ii' => 'value ii',
     509                                                ),
     510                                                'b' => array(
     511                                                        'iii' => 'value iii',
     512                                                        'iv'  => 'value iv',
     513                                                ),
     514                                        ),
     515                                ),
     516                        ),
     517                        array(
     518                                'field.a',
     519                                array(
     520                                        'field' => array(
     521                                                'a' => array(
     522                                                        'i'  => 'value i',
     523                                                        'ii' => 'value ii',
     524                                                ),
     525                                        ),
     526                                ),
     527                        ),
     528                        array(
     529                                'field.b',
     530                                array(
     531                                        'field' => array(
     532                                                'b' => array(
     533                                                        'iii' => 'value iii',
     534                                                        'iv'  => 'value iv',
     535                                                ),
     536                                        ),
     537                                ),
     538                        ),
     539                        array(
     540                                'field.a.i,field.b.iv',
     541                                array(
     542                                        'field' => array(
     543                                                'a' => array(
     544                                                        'i' => 'value i',
     545                                                ),
     546                                                'b' => array(
     547                                                        'iv' => 'value iv',
     548                                                ),
     549                                        ),
     550                                ),
     551                        ),
     552                        array(
     553                                'field.a,field.b.iii',
     554                                array(
     555                                        'field' => array(
     556                                                'a' => array(
     557                                                        'i'  => 'value i',
     558                                                        'ii' => 'value ii',
     559                                                ),
     560                                                'b' => array(
     561                                                        'iii' => 'value iii',
     562                                                ),
     563                                        ),
     564                                ),
     565                        ),
     566                );
     567        }
    440568}
Note: See TracChangeset for help on using the changeset viewer.