Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r42250 r42343  
    7878         */
    7979        public function test_route_canonicalized() {
    80                 register_rest_route( 'test-ns', '/test', array(
    81                         'methods'  => array( 'GET' ),
    82                         'callback' => '__return_null',
    83                 ) );
     80                register_rest_route(
     81                        'test-ns', '/test', array(
     82                                'methods'  => array( 'GET' ),
     83                                'callback' => '__return_null',
     84                        )
     85                );
    8486
    8587                // Check the route was registered correctly.
     
    99101                $this->assertCount( 1, $endpoint );
    100102                $this->assertArrayHasKey( 'callback', $endpoint[0] );
    101                 $this->assertArrayHasKey( 'methods',  $endpoint[0] );
    102                 $this->assertArrayHasKey( 'args',     $endpoint[0] );
     103                $this->assertArrayHasKey( 'methods', $endpoint[0] );
     104                $this->assertArrayHasKey( 'args', $endpoint[0] );
    103105        }
    104106
     
    109111         */
    110112        public function test_route_canonicalized_multiple() {
    111                 register_rest_route( 'test-ns', '/test', array(
    112                         array(
    113                                 'methods'  => array( 'GET' ),
    114                                 'callback' => '__return_null',
    115                         ),
    116                         array(
    117                                 'methods'  => array( 'POST' ),
    118                                 'callback' => '__return_null',
    119                         ),
    120                 ) );
     113                register_rest_route(
     114                        'test-ns', '/test', array(
     115                                array(
     116                                        'methods'  => array( 'GET' ),
     117                                        'callback' => '__return_null',
     118                                ),
     119                                array(
     120                                        'methods'  => array( 'POST' ),
     121                                        'callback' => '__return_null',
     122                                ),
     123                        )
     124                );
    121125
    122126                // Check the route was registered correctly.
     
    131135
    132136                $filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
    133                 $endpoint = $filtered_endpoints['/test-ns/test'];
     137                $endpoint           = $filtered_endpoints['/test-ns/test'];
    134138                $this->assertCount( 2, $endpoint );
    135139
     
    137141                foreach ( array( 0, 1 ) as $key ) {
    138142                        $this->assertArrayHasKey( 'callback', $endpoint[ $key ] );
    139                         $this->assertArrayHasKey( 'methods',  $endpoint[ $key ] );
    140                         $this->assertArrayHasKey( 'args',     $endpoint[ $key ] );
     143                        $this->assertArrayHasKey( 'methods', $endpoint[ $key ] );
     144                        $this->assertArrayHasKey( 'args', $endpoint[ $key ] );
    141145                }
    142146        }
     
    146150         */
    147151        public function test_route_merge() {
    148                 register_rest_route( 'test-ns', '/test', array(
    149                         'methods'  => array( 'GET' ),
    150                         'callback' => '__return_null',
    151                 ) );
    152                 register_rest_route( 'test-ns', '/test', array(
    153                         'methods'  => array( 'POST' ),
    154                         'callback' => '__return_null',
    155                 ) );
     152                register_rest_route(
     153                        'test-ns', '/test', array(
     154                                'methods'  => array( 'GET' ),
     155                                'callback' => '__return_null',
     156                        )
     157                );
     158                register_rest_route(
     159                        'test-ns', '/test', array(
     160                                'methods'  => array( 'POST' ),
     161                                'callback' => '__return_null',
     162                        )
     163                );
    156164
    157165                // Check both routes exist.
    158166                $endpoints = $GLOBALS['wp_rest_server']->get_routes();
    159                 $endpoint = $endpoints['/test-ns/test'];
     167                $endpoint  = $endpoints['/test-ns/test'];
    160168                $this->assertCount( 2, $endpoint );
    161169        }
     
    165173         */
    166174        public function test_route_override() {
    167                 register_rest_route( 'test-ns', '/test', array(
    168                         'methods'      => array( 'GET' ),
    169                         'callback'     => '__return_null',
    170                         'should_exist' => false,
    171                 ) );
    172                 register_rest_route( 'test-ns', '/test', array(
    173                         'methods'      => array( 'POST' ),
    174                         'callback'     => '__return_null',
    175                         'should_exist' => true,
    176                 ), true );
     175                register_rest_route(
     176                        'test-ns', '/test', array(
     177                                'methods'      => array( 'GET' ),
     178                                'callback'     => '__return_null',
     179                                'should_exist' => false,
     180                        )
     181                );
     182                register_rest_route(
     183                        'test-ns', '/test', array(
     184                                'methods'      => array( 'POST' ),
     185                                'callback'     => '__return_null',
     186                                'should_exist' => true,
     187                        ), true
     188                );
    177189
    178190                // Check we only have one route.
    179191                $endpoints = $GLOBALS['wp_rest_server']->get_routes();
    180                 $endpoint = $endpoints['/test-ns/test'];
     192                $endpoint  = $endpoints['/test-ns/test'];
    181193                $this->assertCount( 1, $endpoint );
    182194
     
    192204         */
    193205        public function test_route_reject_empty_namespace() {
    194                 register_rest_route( '', '/test-empty-namespace', array(
    195                         'methods'      => array( 'POST' ),
    196                         'callback'     => '__return_null',
    197                 ), true );
     206                register_rest_route(
     207                        '', '/test-empty-namespace', array(
     208                                'methods'  => array( 'POST' ),
     209                                'callback' => '__return_null',
     210                        ), true
     211                );
    198212                $endpoints = $GLOBALS['wp_rest_server']->get_routes();
    199213                $this->assertFalse( isset( $endpoints['/test-empty-namespace'] ) );
     
    206220         */
    207221        public function test_route_reject_empty_route() {
    208                 register_rest_route( '/test-empty-route', '', array(
    209                         'methods'      => array( 'POST' ),
    210                         'callback'     => '__return_null',
    211                 ), true );
     222                register_rest_route(
     223                        '/test-empty-route', '', array(
     224                                'methods'  => array( 'POST' ),
     225                                'callback' => '__return_null',
     226                        ), true
     227                );
    212228                $endpoints = $GLOBALS['wp_rest_server']->get_routes();
    213229                $this->assertFalse( isset( $endpoints['/test-empty-route'] ) );
     
    223239
    224240        public function test_route_method() {
    225                 register_rest_route( 'test-ns', '/test', array(
    226                         'methods'  => array( 'GET' ),
    227                         'callback' => '__return_null',
    228                 ) );
     241                register_rest_route(
     242                        'test-ns', '/test', array(
     243                                'methods'  => array( 'GET' ),
     244                                'callback' => '__return_null',
     245                        )
     246                );
    229247
    230248                $routes = $GLOBALS['wp_rest_server']->get_routes();
     
    237255         */
    238256        public function test_route_method_string() {
    239                 register_rest_route( 'test-ns', '/test', array(
    240                         'methods'  => 'GET',
    241                         'callback' => '__return_null',
    242                 ) );
     257                register_rest_route(
     258                        'test-ns', '/test', array(
     259                                'methods'  => 'GET',
     260                                'callback' => '__return_null',
     261                        )
     262                );
    243263
    244264                $routes = $GLOBALS['wp_rest_server']->get_routes();
     
    251271         */
    252272        public function test_route_method_array() {
    253                 register_rest_route( 'test-ns', '/test', array(
    254                         'methods'  => array( 'GET', 'POST' ),
    255                         'callback' => '__return_null',
    256                 ) );
     273                register_rest_route(
     274                        'test-ns', '/test', array(
     275                                'methods'  => array( 'GET', 'POST' ),
     276                                'callback' => '__return_null',
     277                        )
     278                );
    257279
    258280                $routes = $GLOBALS['wp_rest_server']->get_routes();
    259281
    260                 $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true, 'POST' => true ) );
     282                $this->assertEquals(
     283                        $routes['/test-ns/test'][0]['methods'], array(
     284                                'GET'  => true,
     285                                'POST' => true,
     286                        )
     287                );
    261288        }
    262289
     
    265292         */
    266293        public function test_route_method_comma_seperated() {
    267                 register_rest_route( 'test-ns', '/test', array(
    268                         'methods'  => 'GET,POST',
    269                         'callback' => '__return_null',
    270                 ) );
     294                register_rest_route(
     295                        'test-ns', '/test', array(
     296                                'methods'  => 'GET,POST',
     297                                'callback' => '__return_null',
     298                        )
     299                );
    271300
    272301                $routes = $GLOBALS['wp_rest_server']->get_routes();
    273302
    274                 $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true, 'POST' => true ) );
     303                $this->assertEquals(
     304                        $routes['/test-ns/test'][0]['methods'], array(
     305                                'GET'  => true,
     306                                'POST' => true,
     307                        )
     308                );
    275309        }
    276310
    277311        public function test_options_request() {
    278                 register_rest_route( 'test-ns', '/test', array(
    279                         'methods'  => 'GET,POST',
    280                         'callback' => '__return_null',
    281                 ) );
    282 
    283                 $request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' );
     312                register_rest_route(
     313                        'test-ns', '/test', array(
     314                                'methods'  => 'GET,POST',
     315                                'callback' => '__return_null',
     316                        )
     317                );
     318
     319                $request  = new WP_REST_Request( 'OPTIONS', '/test-ns/test' );
    284320                $response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request );
    285321                $response = rest_send_allow_header( $response, $GLOBALS['wp_rest_server'], $request );
    286                 $headers = $response->get_headers();
     322                $headers  = $response->get_headers();
    287323                $this->assertArrayHasKey( 'Allow', $headers );
    288324
     
    294330         */
    295331        public function test_options_request_not_options() {
    296                 register_rest_route( 'test-ns', '/test', array(
    297                         'methods'  => 'GET,POST',
    298                         'callback' => '__return_true',
    299                 ) );
    300 
    301                 $request = new WP_REST_Request( 'GET', '/test-ns/test' );
     332                register_rest_route(
     333                        'test-ns', '/test', array(
     334                                'methods'  => 'GET,POST',
     335                                'callback' => '__return_true',
     336                        )
     337                );
     338
     339                $request  = new WP_REST_Request( 'GET', '/test-ns/test' );
    302340                $response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request );
    303341
     
    322360        public function test_rest_filter_response_fields_single_field_filter() {
    323361                $response = new WP_REST_Response();
    324                 $response->set_data( array(
    325                         'a' => 0,
    326                         'b' => 1,
    327                         'c' => 2,
    328                 ) );
    329                 $request = array(
    330                         '_fields' => 'b'
    331                 );
    332 
    333                 $response = rest_filter_response_fields( $response, null, $request );
    334                 $this->assertEquals( array( 'b' => 1 ), $response->get_data() );
    335         }
    336 
    337         /**
    338          * Ensure that multiple comma-separated fields may be whitelisted with request['_fields'].
    339          */
    340         public function test_rest_filter_response_fields_multi_field_filter() {
    341                 $response = new WP_REST_Response();
    342                 $response->set_data( array(
    343                         'a' => 0,
    344                         'b' => 1,
    345                         'c' => 2,
    346                         'd' => 3,
    347                         'e' => 4,
    348                         'f' => 5,
    349                 ) );
    350                 $request = array(
    351                         '_fields' => 'b,c,e'
    352                 );
    353 
    354                 $response = rest_filter_response_fields( $response, null, $request );
    355                 $this->assertEquals( array(
    356                         'b' => 1,
    357                         'c' => 2,
    358                         'e' => 4,
    359                 ), $response->get_data() );
    360         }
    361 
    362         /**
    363          * Ensure that multiple comma-separated fields may be whitelisted
    364          * with request['_fields'] using query parameter array syntax.
    365          */
    366         public function test_rest_filter_response_fields_multi_field_filter_array() {
    367                 $response = new WP_REST_Response();
    368 
    369                 $response->set_data( array(
    370                         'a' => 0,
    371                         'b' => 1,
    372                         'c' => 2,
    373                         'd' => 3,
    374                         'e' => 4,
    375                         'f' => 5,
    376                 ) );
    377                 $request = array(
    378                         '_fields' => array( 'b', 'c', 'e' )
    379                 );
    380 
    381                 $response = rest_filter_response_fields( $response, null, $request );
    382                 $this->assertEquals( array(
    383                         'b' => 1,
    384                         'c' => 2,
    385                         'e' => 4,
    386                 ), $response->get_data() );
    387         }
    388 
    389         /**
    390          * Ensure that request['_fields'] whitelists apply to items in response collections.
    391          */
    392         public function test_rest_filter_response_fields_numeric_array() {
    393                 $response = new WP_REST_Response();
    394                 $response->set_data( array(
     362                $response->set_data(
    395363                        array(
    396364                                'a' => 0,
    397365                                'b' => 1,
    398366                                'c' => 2,
    399                         ),
     367                        )
     368                );
     369                $request = array(
     370                        '_fields' => 'b',
     371                );
     372
     373                $response = rest_filter_response_fields( $response, null, $request );
     374                $this->assertEquals( array( 'b' => 1 ), $response->get_data() );
     375        }
     376
     377        /**
     378         * Ensure that multiple comma-separated fields may be whitelisted with request['_fields'].
     379         */
     380        public function test_rest_filter_response_fields_multi_field_filter() {
     381                $response = new WP_REST_Response();
     382                $response->set_data(
    400383                        array(
    401                                 'a' => 3,
    402                                 'b' => 4,
    403                                 'c' => 5,
    404                         ),
    405                         array(
    406                                 'a' => 6,
    407                                 'b' => 7,
    408                                 'c' => 8,
    409                         ),
    410                 ) );
     384                                'a' => 0,
     385                                'b' => 1,
     386                                'c' => 2,
     387                                'd' => 3,
     388                                'e' => 4,
     389                                'f' => 5,
     390                        )
     391                );
    411392                $request = array(
    412                         '_fields' => 'b,c'
     393                        '_fields' => 'b,c,e',
    413394                );
    414395
    415396                $response = rest_filter_response_fields( $response, null, $request );
    416                 $this->assertEquals( array(
     397                $this->assertEquals(
    417398                        array(
    418399                                'b' => 1,
    419400                                'c' => 2,
    420                         ),
     401                                'e' => 4,
     402                        ), $response->get_data()
     403                );
     404        }
     405
     406        /**
     407         * Ensure that multiple comma-separated fields may be whitelisted
     408         * with request['_fields'] using query parameter array syntax.
     409         */
     410        public function test_rest_filter_response_fields_multi_field_filter_array() {
     411                $response = new WP_REST_Response();
     412
     413                $response->set_data(
    421414                        array(
    422                                 'b' => 4,
    423                                 'c' => 5,
    424                         ),
     415                                'a' => 0,
     416                                'b' => 1,
     417                                'c' => 2,
     418                                'd' => 3,
     419                                'e' => 4,
     420                                'f' => 5,
     421                        )
     422                );
     423                $request = array(
     424                        '_fields' => array( 'b', 'c', 'e' ),
     425                );
     426
     427                $response = rest_filter_response_fields( $response, null, $request );
     428                $this->assertEquals(
    425429                        array(
    426                                 'b' => 7,
    427                                 'c' => 8,
    428                         ),
    429                 ), $response->get_data() );
     430                                'b' => 1,
     431                                'c' => 2,
     432                                'e' => 4,
     433                        ), $response->get_data()
     434                );
     435        }
     436
     437        /**
     438         * Ensure that request['_fields'] whitelists apply to items in response collections.
     439         */
     440        public function test_rest_filter_response_fields_numeric_array() {
     441                $response = new WP_REST_Response();
     442                $response->set_data(
     443                        array(
     444                                array(
     445                                        'a' => 0,
     446                                        'b' => 1,
     447                                        'c' => 2,
     448                                ),
     449                                array(
     450                                        'a' => 3,
     451                                        'b' => 4,
     452                                        'c' => 5,
     453                                ),
     454                                array(
     455                                        'a' => 6,
     456                                        'b' => 7,
     457                                        'c' => 8,
     458                                ),
     459                        )
     460                );
     461                $request = array(
     462                        '_fields' => 'b,c',
     463                );
     464
     465                $response = rest_filter_response_fields( $response, null, $request );
     466                $this->assertEquals(
     467                        array(
     468                                array(
     469                                        'b' => 1,
     470                                        'c' => 2,
     471                                ),
     472                                array(
     473                                        'b' => 4,
     474                                        'c' => 5,
     475                                ),
     476                                array(
     477                                        'b' => 7,
     478                                        'c' => 8,
     479                                ),
     480                        ), $response->get_data()
     481                );
    430482        }
    431483
     
    453505        public function test_rest_url_scheme() {
    454506                $_SERVER['SERVER_NAME'] = parse_url( home_url(), PHP_URL_HOST );
    455                 $_siteurl = get_option( 'siteurl' );
     507                $_siteurl               = get_option( 'siteurl' );
    456508
    457509                set_current_screen( 'edit.php' );
     
    465517                // Test an HTTPS URL
    466518                $_SERVER['HTTPS'] = 'on';
    467                 $url = get_rest_url();
     519                $url              = get_rest_url();
    468520                $this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
    469521
     
    480532                // // Test an HTTPS URL
    481533                $_SERVER['HTTPS'] = 'on';
    482                 $url = get_rest_url();
     534                $url              = get_rest_url();
    483535                $this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
    484536
     
    544596                return array(
    545597                        // Valid dates with timezones
    546                         array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30,  0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
    547                         array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30,  0, 1, 16, 2017 ) + 5.5 * HOUR_IN_SECONDS ),
    548                         array( '2017-01-16T11:30:00-05'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
    549                         array( '2017-01-16T11:30:00+05'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) - 5 * HOUR_IN_SECONDS ),
    550                         array( '2017-01-16T11:30:00-00'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    551                         array( '2017-01-16T11:30:00+00'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    552                         array( '2017-01-16T11:30:00Z'     , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
     598                        array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
     599                        array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5.5 * HOUR_IN_SECONDS ),
     600                        array( '2017-01-16T11:30:00-05', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
     601                        array( '2017-01-16T11:30:00+05', gmmktime( 11, 30, 0, 1, 16, 2017 ) - 5 * HOUR_IN_SECONDS ),
     602                        array( '2017-01-16T11:30:00-00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     603                        array( '2017-01-16T11:30:00+00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     604                        array( '2017-01-16T11:30:00Z', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
    553605
    554606                        // Valid dates without timezones
    555                         array( '2017-01-16T11:30:00'      , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
     607                        array( '2017-01-16T11:30:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
    556608
    557609                        // Invalid dates (TODO: support parsing partial dates as ranges, see #38641)
     
    576628                return array(
    577629                        // Valid dates with timezones
    578                         array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    579                         array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    580                         array( '2017-01-16T11:30:00-05'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    581                         array( '2017-01-16T11:30:00+05'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    582                         array( '2017-01-16T11:30:00-00'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    583                         array( '2017-01-16T11:30:00+00'   , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
    584                         array( '2017-01-16T11:30:00Z'     , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
     630                        array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     631                        array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     632                        array( '2017-01-16T11:30:00-05', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     633                        array( '2017-01-16T11:30:00+05', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     634                        array( '2017-01-16T11:30:00-00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     635                        array( '2017-01-16T11:30:00+00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
     636                        array( '2017-01-16T11:30:00Z', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
    585637
    586638                        // Valid dates without timezones
    587                         array( '2017-01-16T11:30:00'      , gmmktime( 11, 30,  0, 1, 16, 2017 ) ),
     639                        array( '2017-01-16T11:30:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
    588640
    589641                        // Invalid dates (TODO: support parsing partial dates as ranges, see #38641)
     
    613665                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    614666
    615                 register_rest_route( 'test-ns', '/test', array(
    616                         'methods'  => array( 'GET' ),
    617                         'callback' => '__return_null',
    618                 ) );
     667                register_rest_route(
     668                        'test-ns', '/test', array(
     669                                'methods'  => array( 'GET' ),
     670                                'callback' => '__return_null',
     671                        )
     672                );
    619673
    620674                $routes = $GLOBALS['wp_rest_server']->get_routes();
Note: See TracChangeset for help on using the changeset viewer.