Make WordPress Core

Ticket #42094: 42094.1.diff

File 42094.1.diff, 1.1 KB (added by kadamwhite, 7 years ago)

Basic unit test exploring the proposed dot-nested ?_fields syntax

  • tests/phpunit/tests/rest-api.php

     
    430430        }
    431431
    432432        /**
     433                * Ensure that nested fields may be whitelisted with request['_fields'].
     434                */
     435        public function test_rest_filter_response_fields_nested_field_filter() {
     436                $response = new WP_REST_Response();
     437
     438                $response->set_data( array(
     439                        'a' => 0,
     440                        'b' => array(
     441                                '1' => 1,
     442                                '2' => 2,
     443                        ),
     444                        'c' => 3,
     445                        'd' => array(
     446                                '4' => 4,
     447                                '5' => 5,
     448                        ),
     449                ) );
     450                $request = array(
     451                        '_fields' => array( 'b.1', 'c', 'd.5' )
     452                );
     453
     454                $response = rest_filter_response_fields( $response, null, $request );
     455                $this->assertEquals( array(
     456                        'b' => array(
     457                                '1' => 1,
     458                        ),
     459                        'c' => 3,
     460                        'd' => array(
     461                                '5' => 5,
     462                        ),
     463                ), $response->get_data() );
     464        }
     465
     466        /**
    433467         * The get_rest_url function should return a URL consistently terminated with a "/",
    434468         * whether the blog is configured with pretty permalink support or not.
    435469         */