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/rest-request.php

    r41139 r42343  
    3131                $this->assertNull( $this->request->get_header_as_array( 'missing' ) );
    3232        }
    33        
     33
    3434        public function test_remove_header() {
    3535                $this->request->add_header( 'Test-Header', 'value' );
    3636                $this->assertEquals( 'value', $this->request->get_header( 'Test-Header' ) );
    37                
     37
    3838                $this->request->remove_header( 'Test-Header' );
    3939                $this->assertNull( $this->request->get_header( 'Test-Header' ) );
     
    9797                $parsed = $this->request->get_content_type();
    9898
    99                 $this->assertEquals( $value,      $parsed['value'] );
    100                 $this->assertEquals( $type,       $parsed['type'] );
    101                 $this->assertEquals( $subtype,    $parsed['subtype'] );
     99                $this->assertEquals( $value, $parsed['value'] );
     100                $this->assertEquals( $type, $parsed['type'] );
     101                $this->assertEquals( $subtype, $parsed['subtype'] );
    102102                $this->assertEquals( $parameters, $parsed['parameters'] );
    103103        }
    104104
    105105        protected function request_with_parameters() {
    106                 $this->request->set_url_params( array(
    107                         'source'         => 'url',
    108                         'has_url_params' => true,
    109                 ) );
    110                 $this->request->set_query_params( array(
    111                         'source'           => 'query',
    112                         'has_query_params' => true,
    113                 ) );
    114                 $this->request->set_body_params( array(
    115                         'source'          => 'body',
    116                         'has_body_params' => true,
    117                 ) );
    118 
    119                 $json_data = wp_json_encode( array(
    120                         'source'          => 'json',
    121                         'has_json_params' => true,
    122                 ) );
     106                $this->request->set_url_params(
     107                        array(
     108                                'source'         => 'url',
     109                                'has_url_params' => true,
     110                        )
     111                );
     112                $this->request->set_query_params(
     113                        array(
     114                                'source'           => 'query',
     115                                'has_query_params' => true,
     116                        )
     117                );
     118                $this->request->set_body_params(
     119                        array(
     120                                'source'          => 'body',
     121                                'has_body_params' => true,
     122                        )
     123                );
     124
     125                $json_data = wp_json_encode(
     126                        array(
     127                                'source'          => 'json',
     128                                'has_json_params' => true,
     129                        )
     130                );
    123131                $this->request->set_body( $json_data );
    124132
    125                 $this->request->set_default_params( array(
    126                         'source'             => 'defaults',
    127                         'has_default_params' => true,
    128                 ) );
     133                $this->request->set_default_params(
     134                        array(
     135                                'source'             => 'defaults',
     136                                'has_default_params' => true,
     137                        )
     138                );
    129139        }
    130140
     
    229239        public function test_non_post_body_parameters( $request_method ) {
    230240                $data = array(
    231                         'foo' => 'bar',
     241                        'foo'  => 'bar',
    232242                        'alot' => array(
    233243                                'of' => 'parameters',
     
    249259        public function test_parameters_for_json_put() {
    250260                $data = array(
    251                         'foo' => 'bar',
     261                        'foo'  => 'bar',
    252262                        'alot' => array(
    253263                                'of' => 'parameters',
     
    271281        public function test_parameters_for_json_post() {
    272282                $data = array(
    273                         'foo' => 'bar',
     283                        'foo'  => 'bar',
    274284                        'alot' => array(
    275285                                'of' => 'parameters',
     
    307317
    308318        public function test_parameter_merging_with_numeric_keys() {
    309                 $this->request->set_query_params( array(
    310                         '1'           => 'hello',
    311                         '2'           => 'goodbye',
    312                 ) );
     319                $this->request->set_query_params(
     320                        array(
     321                                '1' => 'hello',
     322                                '2' => 'goodbye',
     323                        )
     324                );
    313325                $expected = array(
    314                         '1'           => 'hello',
    315                         '2'           => 'goodbye',
     326                        '1' => 'hello',
     327                        '2' => 'goodbye',
    316328                );
    317329                $this->assertEquals( $expected, $this->request->get_params() );
     
    319331
    320332        public function test_sanitize_params() {
    321                 $this->request->set_url_params( array(
    322                         'someinteger' => '123',
    323                         'somestring'  => 'hello',
    324                 ));
    325 
    326                 $this->request->set_attributes( array(
    327                         'args' => array(
    328                                 'someinteger' => array(
    329                                         'sanitize_callback' => 'absint',
    330                                 ),
    331                                 'somestring'  => array(
    332                                         'sanitize_callback' => 'absint',
    333                                 ),
    334                         ),
    335                 ) );
     333                $this->request->set_url_params(
     334                        array(
     335                                'someinteger' => '123',
     336                                'somestring'  => 'hello',
     337                        )
     338                );
     339
     340                $this->request->set_attributes(
     341                        array(
     342                                'args' => array(
     343                                        'someinteger' => array(
     344                                                'sanitize_callback' => 'absint',
     345                                        ),
     346                                        'somestring'  => array(
     347                                                'sanitize_callback' => 'absint',
     348                                        ),
     349                                ),
     350                        )
     351                );
    336352
    337353                $this->request->sanitize_params();
     
    342358
    343359        public function test_sanitize_params_error() {
    344                 $this->request->set_url_params( array(
    345                         'successparam' => '123',
    346                         'failparam'    => '123',
    347                 ));
    348                 $this->request->set_attributes( array(
    349                         'args' => array(
    350                                 'successparam' => array(
    351                                         'sanitize_callback' => 'absint',
    352                                 ),
    353                                 'failparam' => array(
    354                                         'sanitize_callback' => array( $this, '_return_wp_error_on_validate_callback' ),
    355                                 ),
    356                         ),
    357                 ));
     360                $this->request->set_url_params(
     361                        array(
     362                                'successparam' => '123',
     363                                'failparam'    => '123',
     364                        )
     365                );
     366                $this->request->set_attributes(
     367                        array(
     368                                'args' => array(
     369                                        'successparam' => array(
     370                                                'sanitize_callback' => 'absint',
     371                                        ),
     372                                        'failparam'    => array(
     373                                                'sanitize_callback' => array( $this, '_return_wp_error_on_validate_callback' ),
     374                                        ),
     375                                ),
     376                        )
     377                );
    358378
    359379                $valid = $this->request->sanitize_params();
     
    363383
    364384        public function test_sanitize_params_with_null_callback() {
    365                 $this->request->set_url_params( array(
    366                         'some_email' => '',
    367                 ) );
    368 
    369                 $this->request->set_attributes( array(
    370                         'args' => array(
    371                                 'some_email' => array(
    372                                         'type'              => 'string',
    373                                         'format'            => 'email',
    374                                         'sanitize_callback' => null,
    375                                 ),
    376                         ),
    377                 ) );
     385                $this->request->set_url_params(
     386                        array(
     387                                'some_email' => '',
     388                        )
     389                );
     390
     391                $this->request->set_attributes(
     392                        array(
     393                                'args' => array(
     394                                        'some_email' => array(
     395                                                'type'              => 'string',
     396                                                'format'            => 'email',
     397                                                'sanitize_callback' => null,
     398                                        ),
     399                                ),
     400                        )
     401                );
    378402
    379403                $this->assertTrue( $this->request->sanitize_params() );
     
    381405
    382406        public function test_sanitize_params_with_false_callback() {
    383                 $this->request->set_url_params( array(
    384                         'some_uri'   => 1.23422,
    385                 ) );
    386 
    387                 $this->request->set_attributes( array(
    388                         'args' => array(
    389                                 'some_uri' => array(
    390                                         'type'              => 'string',
    391                                         'format'            => 'uri',
    392                                         'sanitize_callback' => false,
    393                                 ),
    394                         ),
    395                 ) );
     407                $this->request->set_url_params(
     408                        array(
     409                                'some_uri' => 1.23422,
     410                        )
     411                );
     412
     413                $this->request->set_attributes(
     414                        array(
     415                                'args' => array(
     416                                        'some_uri' => array(
     417                                                'type'              => 'string',
     418                                                'format'            => 'uri',
     419                                                'sanitize_callback' => false,
     420                                        ),
     421                                ),
     422                        )
     423                );
    396424
    397425                $this->assertTrue( $this->request->sanitize_params() );
     
    399427
    400428        public function test_has_valid_params_required_flag() {
    401                 $this->request->set_attributes( array(
    402                         'args' => array(
    403                                 'someinteger' => array(
    404                                         'required' => true,
    405                                 ),
    406                         ),
    407                 ) );
     429                $this->request->set_attributes(
     430                        array(
     431                                'args' => array(
     432                                        'someinteger' => array(
     433                                                'required' => true,
     434                                        ),
     435                                ),
     436                        )
     437                );
    408438
    409439                $valid = $this->request->has_valid_params();
     
    414444
    415445        public function test_has_valid_params_required_flag_multiple() {
    416                 $this->request->set_attributes( array(
    417                         'args' => array(
    418                                 'someinteger' => array(
    419                                         'required' => true,
    420                                 ),
    421                                 'someotherinteger' => array(
    422                                         'required' => true,
    423                                 ),
    424                         ),
    425                 ));
     446                $this->request->set_attributes(
     447                        array(
     448                                'args' => array(
     449                                        'someinteger'      => array(
     450                                                'required' => true,
     451                                        ),
     452                                        'someotherinteger' => array(
     453                                                'required' => true,
     454                                        ),
     455                                ),
     456                        )
     457                );
    426458
    427459                $valid = $this->request->has_valid_params();
     
    437469
    438470        public function test_has_valid_params_validate_callback() {
    439                 $this->request->set_url_params( array(
    440                         'someinteger' => '123',
    441                 ));
    442 
    443                 $this->request->set_attributes( array(
    444                         'args' => array(
    445                                 'someinteger' => array(
    446                                         'validate_callback' => '__return_false',
    447                                 ),
    448                         ),
    449                 ));
     471                $this->request->set_url_params(
     472                        array(
     473                                'someinteger' => '123',
     474                        )
     475                );
     476
     477                $this->request->set_attributes(
     478                        array(
     479                                'args' => array(
     480                                        'someinteger' => array(
     481                                                'validate_callback' => '__return_false',
     482                                        ),
     483                                ),
     484                        )
     485                );
    450486
    451487                $valid = $this->request->has_valid_params();
     
    484520
    485521        public function test_has_multiple_invalid_params_validate_callback() {
    486                 $this->request->set_url_params( array(
    487                         'someinteger' => '123',
    488                         'someotherinteger' => '123',
    489                 ));
    490 
    491                 $this->request->set_attributes( array(
    492                         'args' => array(
    493                                 'someinteger' => array(
    494                                         'validate_callback' => '__return_false',
    495                                 ),
    496                                 'someotherinteger' => array(
    497                                         'validate_callback' => '__return_false',
    498                                 ),
    499                         ),
    500                 ));
     522                $this->request->set_url_params(
     523                        array(
     524                                'someinteger'      => '123',
     525                                'someotherinteger' => '123',
     526                        )
     527                );
     528
     529                $this->request->set_attributes(
     530                        array(
     531                                'args' => array(
     532                                        'someinteger'      => array(
     533                                                'validate_callback' => '__return_false',
     534                                        ),
     535                                        'someotherinteger' => array(
     536                                                'validate_callback' => '__return_false',
     537                                        ),
     538                                ),
     539                        )
     540                );
    501541
    502542                $valid = $this->request->has_valid_params();
     
    512552
    513553        public function test_invalid_params_error_response_format() {
    514                 $this->request->set_url_params( array(
    515                         'someinteger' => '123',
    516                         'someotherparams' => '123',
    517                 ));
    518 
    519                 $this->request->set_attributes( array(
    520                         'args' => array(
    521                                 'someinteger' => array(
    522                                         'validate_callback' => '__return_false',
    523                                 ),
    524                                 'someotherparams' => array(
    525                                         'validate_callback' => array( $this, '_return_wp_error_on_validate_callback' ),
    526                                 ),
    527                         ),
    528                 ));
     554                $this->request->set_url_params(
     555                        array(
     556                                'someinteger'     => '123',
     557                                'someotherparams' => '123',
     558                        )
     559                );
     560
     561                $this->request->set_attributes(
     562                        array(
     563                                'args' => array(
     564                                        'someinteger'     => array(
     565                                                'validate_callback' => '__return_false',
     566                                        ),
     567                                        'someotherparams' => array(
     568                                                'validate_callback' => array( $this, '_return_wp_error_on_validate_callback' ),
     569                                        ),
     570                                ),
     571                        )
     572                );
    529573
    530574                $valid = $this->request->has_valid_params();
     
    563607                $this->assertInstanceOf( 'WP_REST_Request', $request );
    564608                $this->assertEquals( '/wp/v2/posts/1', $request->get_route() );
    565                 $this->assertEqualSets( array(
    566                         'foo' => 'bar',
    567                 ), $request->get_query_params() );
     609                $this->assertEqualSets(
     610                        array(
     611                                'foo' => 'bar',
     612                        ), $request->get_query_params()
     613                );
    568614        }
    569615
     
    574620                update_option( 'permalink_structure', $permalink_structure );
    575621                $using_site = site_url( '/wp/v2/posts/1' );
    576                 $request = WP_REST_Request::from_url( $using_site );
     622                $request    = WP_REST_Request::from_url( $using_site );
    577623                $this->assertFalse( $request );
    578624
    579                 $using_home = home_url( '/wp/v2/posts/1' ) ;
    580                 $request = WP_REST_Request::from_url( $using_home );
     625                $using_home = home_url( '/wp/v2/posts/1' );
     626                $request    = WP_REST_Request::from_url( $using_home );
    581627                $this->assertFalse( $request );
    582628        }
     
    592638                $request->add_header( 'content-type', 'application/json' );
    593639                $request->set_method( 'POST' );
    594                 $request->set_body( wp_json_encode( array(
    595                         'param' => 'value'
    596                 ) ) );
     640                $request->set_body(
     641                        wp_json_encode(
     642                                array(
     643                                        'param' => 'value',
     644                                )
     645                        )
     646                );
    597647                $this->assertEquals( 'value', $request->get_param( 'param' ) );
    598648                $this->assertEquals(
Note: See TracChangeset for help on using the changeset viewer.