Make WordPress Core


Ignore:
Timestamp:
01/10/2019 09:05:50 PM (6 years ago)
Author:
flixos90
Message:

General: Fix problematic string to array parsing.

WordPress has historically often used code like preg_split( '/[\s,]+/', $var ) to parse a string of comma-separated values into an array. However, this approach was causing an empty string to not be parsed into an empty array as expected, but rather into an array with the empty string as its sole element.

This was among other areas causing problems in the REST API where passing an empty request parameter could cause that request to fail because, instead of it being ignored, that parameter would be compared against the valid values for it, which typically do not include an empty string.

Props david.binda, sstoqnov.
Fixes #43977.

File:
1 edited

Legend:

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

    r43986 r44546  
    204204    }
    205205
    206     public function test_get_fields_for_response() {
     206    /**
     207     * @dataProvider data_get_fields_for_response,
     208     */
     209    public function test_get_fields_for_response( $param, $expected ) {
    207210        $controller = new WP_REST_Test_Controller();
    208211        $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     
    222225            $fields
    223226        );
    224         $request->set_param( '_fields', 'somestring,someinteger' );
     227        $request->set_param( '_fields', $param );
    225228        $fields = $controller->get_fields_for_response( $request );
    226         $this->assertEquals(
    227             array(
    228                 'somestring',
    229                 'someinteger',
     229        $this->assertEquals( $expected, $fields );
     230    }
     231
     232    public function data_get_fields_for_response() {
     233        return array(
     234            array(
     235                'somestring,someinteger',
     236                array(
     237                    'somestring',
     238                    'someinteger',
     239                ),
    230240            ),
    231             $fields
     241            array(
     242                ',,',
     243                array(
     244                    'somestring',
     245                    'someinteger',
     246                    'someboolean',
     247                    'someurl',
     248                    'somedate',
     249                    'someemail',
     250                    'someenum',
     251                    'someargoptions',
     252                    'somedefault',
     253                ),
     254            ),
    232255        );
    233256    }
Note: See TracChangeset for help on using the changeset viewer.