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-settings-controller.php

    r42000 r42343  
    1414
    1515        public static function wpSetUpBeforeClass( $factory ) {
    16                 self::$administrator = $factory->user->create( array(
    17                         'role' => 'administrator',
    18                 ) );
     16                self::$administrator = $factory->user->create(
     17                        array(
     18                                'role' => 'administrator',
     19                        )
     20                );
    1921        }
    2022
    … …  
    3436
    3537        public function test_get_item() {
    36                 /** Individual settings can't be gotten **/
    37                 wp_set_current_user( self::$administrator );
    38                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings/title' );
     38                /** Individual settings can't be gotten */
     39                wp_set_current_user( self::$administrator );
     40                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings/title' );
    3941                $response = $this->server->dispatch( $request );
    4042                $this->assertEquals( 404, $response->get_status() );
    … …  
    4547
    4648        public function test_get_item_is_not_public() {
    47                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     49                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    4850                $response = $this->server->dispatch( $request );
    4951                $this->assertEquals( 403, $response->get_status() );
    … …  
    5254        public function test_get_items() {
    5355                wp_set_current_user( self::$administrator );
    54                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    55                 $response = $this->server->dispatch( $request );
    56                 $data = $response->get_data();
    57                 $actual = array_keys( $data );
     56                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     57                $response = $this->server->dispatch( $request );
     58                $data     = $response->get_data();
     59                $actual   = array_keys( $data );
    5860
    5961                $expected = array(
    … …  
    8890                wp_set_current_user( self::$administrator );
    8991                update_option( 'posts_per_page', 'invalid_number' ); // this is cast to (int) 1
    90                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    91                 $response = $this->server->dispatch( $request );
    92                 $data = $response->get_data();
     92                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     93                $response = $this->server->dispatch( $request );
     94                $data     = $response->get_data();
    9395
    9496                $this->assertEquals( 200, $response->get_status() );
    … …  
    99101                wp_set_current_user( self::$administrator );
    100102
    101                 register_setting( 'somegroup', 'mycustomsetting', array(
    102                         'show_in_rest' => array(
    103                                 'name'   => 'mycustomsettinginrest',
    104                                 'schema' => array(
    105                                         'enum'    => array( 'validvalue1', 'validvalue2' ),
    106                                         'default' => 'validvalue1',
    107                                 ),
    108                         ),
    109                         'type'         => 'string',
    110                 ) );
    111 
    112                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    113                 $response = $this->server->dispatch( $request );
    114                 $data = $response->get_data();
     103                register_setting(
     104                        'somegroup', 'mycustomsetting', array(
     105                                'show_in_rest' => array(
     106                                        'name'   => 'mycustomsettinginrest',
     107                                        'schema' => array(
     108                                                'enum'    => array( 'validvalue1', 'validvalue2' ),
     109                                                'default' => 'validvalue1',
     110                                        ),
     111                                ),
     112                                'type'         => 'string',
     113                        )
     114                );
     115
     116                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     117                $response = $this->server->dispatch( $request );
     118                $data     = $response->get_data();
    115119
    116120                $this->assertEquals( 200, $response->get_status() );
    … …  
    120124                update_option( 'mycustomsetting', 'validvalue2' );
    121125
    122                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    123                 $response = $this->server->dispatch( $request );
    124                 $data = $response->get_data();
     126                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     127                $response = $this->server->dispatch( $request );
     128                $data     = $response->get_data();
    125129                $this->assertEquals( 'validvalue2', $data['mycustomsettinginrest'] );
    126130
    … …  
    131135                wp_set_current_user( self::$administrator );
    132136
    133                 register_setting( 'somegroup', 'mycustomsetting', array(
    134                         'show_in_rest' => array(
    135                                 'schema' => array(
    136                                         'type'    => 'array',
    137                                         'items'   => array(
    138                                                 'type' => 'integer',
    139                                         ),
    140                                 ),
    141                         ),
    142                         'type'         => 'array',
    143                 ) );
     137                register_setting(
     138                        'somegroup', 'mycustomsetting', array(
     139                                'show_in_rest' => array(
     140                                        'schema' => array(
     141                                                'type'  => 'array',
     142                                                'items' => array(
     143                                                        'type' => 'integer',
     144                                                ),
     145                                        ),
     146                                ),
     147                                'type'         => 'array',
     148                        )
     149                );
    144150
    145151                // Array is cast to correct types.
    146152                update_option( 'mycustomsetting', array( '1', '2' ) );
    147                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    148                 $response = $this->server->dispatch( $request );
    149                 $data = $response->get_data();
     153                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     154                $response = $this->server->dispatch( $request );
     155                $data     = $response->get_data();
    150156                $this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] );
    151157
    152158                // Empty array works as expected.
    153159                update_option( 'mycustomsetting', array() );
    154                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    155                 $response = $this->server->dispatch( $request );
    156                 $data = $response->get_data();
     160                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     161                $response = $this->server->dispatch( $request );
     162                $data     = $response->get_data();
    157163                $this->assertEquals( array(), $data['mycustomsetting'] );
    158164
    159165                // Invalid value
    160166                update_option( 'mycustomsetting', array( array( 1 ) ) );
    161                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    162                 $response = $this->server->dispatch( $request );
    163                 $data = $response->get_data();
     167                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     168                $response = $this->server->dispatch( $request );
     169                $data     = $response->get_data();
    164170                $this->assertEquals( null, $data['mycustomsetting'] );
    165171
    166172                // No option value
    167173                delete_option( 'mycustomsetting' );
    168                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    169                 $response = $this->server->dispatch( $request );
    170                 $data = $response->get_data();
     174                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     175                $response = $this->server->dispatch( $request );
     176                $data     = $response->get_data();
    171177                $this->assertEquals( null, $data['mycustomsetting'] );
    172178
    … …  
    177183                wp_set_current_user( self::$administrator );
    178184
    179                 register_setting( 'somegroup', 'mycustomsetting', array(
    180                         'show_in_rest' => array(
    181                                 'schema' => array(
    182                                         'type'    => 'object',
    183                                         'properties' => array(
    184                                                 'a' => array(
    185                                                         'type' => 'integer',
     185                register_setting(
     186                        'somegroup', 'mycustomsetting', array(
     187                                'show_in_rest' => array(
     188                                        'schema' => array(
     189                                                'type'       => 'object',
     190                                                'properties' => array(
     191                                                        'a' => array(
     192                                                                'type' => 'integer',
     193                                                        ),
    186194                                                ),
    187195                                        ),
    188196                                ),
    189                         ),
    190                         'type'         => 'object',
    191                 ) );
     197                                'type'         => 'object',
     198                        )
     199                );
    192200
    193201                // We have to re-register the route, as the args changes based off registered settings.
    … …  
    197205                // Object is cast to correct types.
    198206                update_option( 'mycustomsetting', array( 'a' => '1' ) );
    199                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    200                 $response = $this->server->dispatch( $request );
    201                 $data = $response->get_data();
     207                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     208                $response = $this->server->dispatch( $request );
     209                $data     = $response->get_data();
    202210                $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
    203211
    204212                // Empty array works as expected.
    205213                update_option( 'mycustomsetting', array() );
    206                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    207                 $response = $this->server->dispatch( $request );
    208                 $data = $response->get_data();
     214                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     215                $response = $this->server->dispatch( $request );
     216                $data     = $response->get_data();
    209217                $this->assertEquals( array(), $data['mycustomsetting'] );
    210218
    211219                // Invalid value
    212                 update_option( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) );
    213                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    214                 $response = $this->server->dispatch( $request );
    215                 $data = $response->get_data();
     220                update_option(
     221                        'mycustomsetting', array(
     222                                'a' => 1,
     223                                'b' => 2,
     224                        )
     225                );
     226                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     227                $response = $this->server->dispatch( $request );
     228                $data     = $response->get_data();
    216229                $this->assertEquals( null, $data['mycustomsetting'] );
    217230
    … …  
    232245                add_filter( 'rest_pre_get_setting', array( $this, 'get_setting_custom_callback' ), 10, 3 );
    233246
    234                 register_setting( 'somegroup', 'mycustomsetting1', array(
    235                         'show_in_rest' => array(
    236                                 'name'   => 'mycustomsettinginrest1',
    237                         ),
    238                         'type'         => 'string',
    239                 ) );
    240 
    241                 register_setting( 'somegroup', 'mycustomsetting2', array(
    242                         'show_in_rest' => array(
    243                                 'name'   => 'mycustomsettinginrest2',
    244                         ),
    245                         'type'         => 'string',
    246                 ) );
     247                register_setting(
     248                        'somegroup', 'mycustomsetting1', array(
     249                                'show_in_rest' => array(
     250                                        'name' => 'mycustomsettinginrest1',
     251                                ),
     252                                'type'         => 'string',
     253                        )
     254                );
     255
     256                register_setting(
     257                        'somegroup', 'mycustomsetting2', array(
     258                                'show_in_rest' => array(
     259                                        'name' => 'mycustomsettinginrest2',
     260                                ),
     261                                'type'         => 'string',
     262                        )
     263                );
    247264
    248265                update_option( 'mycustomsetting1', 'unfiltered1' );
    249266                update_option( 'mycustomsetting2', 'unfiltered2' );
    250267
    251                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    252                 $response = $this->server->dispatch( $request );
    253                 $data = $response->get_data();
     268                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     269                $response = $this->server->dispatch( $request );
     270                $data     = $response->get_data();
    254271
    255272                $this->assertEquals( 200, $response->get_status() );
    … …  
    268285                wp_set_current_user( self::$administrator );
    269286
    270                 register_setting( 'somegroup', 'mycustomsetting', array(
    271                         'show_in_rest' => array(
    272                                 'name'   => 'mycustomsettinginrest',
    273                                 'schema' => array(
    274                                         'enum'    => array( 'validvalue1', 'validvalue2' ),
    275                                         'default' => 'validvalue1',
    276                                 ),
    277                         ),
    278                         'type'         => 'string',
    279                 ) );
     287                register_setting(
     288                        'somegroup', 'mycustomsetting', array(
     289                                'show_in_rest' => array(
     290                                        'name'   => 'mycustomsettinginrest',
     291                                        'schema' => array(
     292                                                'enum'    => array( 'validvalue1', 'validvalue2' ),
     293                                                'default' => 'validvalue1',
     294                                        ),
     295                                ),
     296                                'type'         => 'string',
     297                        )
     298                );
    280299
    281300                update_option( 'mycustomsetting', array( 'A sneaky array!' ) );
    282301
    283                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    284                 $response = $this->server->dispatch( $request );
    285                 $data = $response->get_data();
     302                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     303                $response = $this->server->dispatch( $request );
     304                $data     = $response->get_data();
    286305                $this->assertEquals( null, $data['mycustomsettinginrest'] );
    287306        }
    … …  
    290309                wp_set_current_user( self::$administrator );
    291310
    292                 register_setting( 'somegroup', 'mycustomsetting', array(
    293                         'show_in_rest' => array(
    294                                 'name'   => 'mycustomsettinginrest',
    295                                 'schema' => array(
    296                                         'enum'    => array( 'validvalue1', 'validvalue2' ),
    297                                         'default' => 'validvalue1',
    298                                 ),
    299                         ),
    300                         'type'         => 'string',
    301                 ) );
     311                register_setting(
     312                        'somegroup', 'mycustomsetting', array(
     313                                'show_in_rest' => array(
     314                                        'name'   => 'mycustomsettinginrest',
     315                                        'schema' => array(
     316                                                'enum'    => array( 'validvalue1', 'validvalue2' ),
     317                                                'default' => 'validvalue1',
     318                                        ),
     319                                ),
     320                                'type'         => 'string',
     321                        )
     322                );
    302323
    303324                update_option( 'mycustomsetting', (object) array( 'A sneaky array!' ) );
    304325
    305                 $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    306                 $response = $this->server->dispatch( $request );
    307                 $data = $response->get_data();
     326                $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     327                $response = $this->server->dispatch( $request );
     328                $data     = $response->get_data();
    308329                $this->assertEquals( null, $data['mycustomsettinginrest'] );
    309330                unregister_setting( 'somegroup', 'mycustomsetting' );
    … …  
    319340                $request->set_param( 'title', 'The new title!' );
    320341                $response = $this->server->dispatch( $request );
    321                 $data = $response->get_data();
     342                $data     = $response->get_data();
    322343
    323344                $this->assertEquals( 200, $response->get_status() );
    … …  
    336357
    337358        public function test_update_item_with_array() {
    338                 register_setting( 'somegroup', 'mycustomsetting', array(
    339                         'show_in_rest' => array(
    340                                 'schema' => array(
    341                                         'type'  => 'array',
    342                                         'items' => array(
    343                                                 'type' => 'integer',
    344                                         ),
    345                                 ),
    346                         ),
    347                         'type'         => 'array',
    348                 ) );
     359                register_setting(
     360                        'somegroup', 'mycustomsetting', array(
     361                                'show_in_rest' => array(
     362                                        'schema' => array(
     363                                                'type'  => 'array',
     364                                                'items' => array(
     365                                                        'type' => 'integer',
     366                                                ),
     367                                        ),
     368                                ),
     369                                'type'         => 'array',
     370                        )
     371                );
    349372
    350373                // We have to re-register the route, as the args changes based off registered settings.
    … …  
    356379                $request->set_param( 'mycustomsetting', array( '1', '2' ) );
    357380                $response = $this->server->dispatch( $request );
    358                 $data = $response->get_data();
     381                $data     = $response->get_data();
    359382                $this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] );
    360383                $this->assertEquals( array( 1, 2 ), get_option( 'mycustomsetting' ) );
    … …  
    364387                $request->set_param( 'mycustomsetting', array() );
    365388                $response = $this->server->dispatch( $request );
    366                 $data = $response->get_data();
     389                $data     = $response->get_data();
    367390                $this->assertEquals( array(), $data['mycustomsetting'] );
    368391                $this->assertEquals( array(), get_option( 'mycustomsetting' ) );
    … …  
    378401
    379402        public function test_update_item_with_nested_object() {
    380                 register_setting( 'somegroup', 'mycustomsetting', array(
    381                         'show_in_rest' => array(
    382                                 'schema' => array(
    383                                         'type'       => 'object',
    384                                         'properties' => array(
    385                                                 'a' => array(
    386                                                         'type' => 'object',
    387                                                         'properties' => array(
    388                                                                 'b' => array(
    389                                                                         'type' => 'number',
     403                register_setting(
     404                        'somegroup', 'mycustomsetting', array(
     405                                'show_in_rest' => array(
     406                                        'schema' => array(
     407                                                'type'       => 'object',
     408                                                'properties' => array(
     409                                                        'a' => array(
     410                                                                'type'       => 'object',
     411                                                                'properties' => array(
     412                                                                        'b' => array(
     413                                                                                'type' => 'number',
     414                                                                        ),
    390415                                                                ),
    391416                                                        ),
    … …  
    393418                                        ),
    394419                                ),
    395                         ),
    396                         'type'         => 'object',
    397                 ) );
     420                                'type'         => 'object',
     421                        )
     422                );
    398423
    399424                // We have to re-register the route, as the args changes based off registered settings.
    … …  
    403428
    404429                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    405                 $request->set_param( 'mycustomsetting', array( 'a' => array( 'b' => 1, 'c' => 1 ) ) );
     430                $request->set_param(
     431                        'mycustomsetting', array(
     432                                'a' => array(
     433                                        'b' => 1,
     434                                        'c' => 1,
     435                                ),
     436                        )
     437                );
    406438                $response = $this->server->dispatch( $request );
    407439                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    … …  
    409441
    410442        public function test_update_item_with_object() {
    411                 register_setting( 'somegroup', 'mycustomsetting', array(
    412                         'show_in_rest' => array(
    413                                 'schema' => array(
    414                                         'type'       => 'object',
    415                                         'properties' => array(
    416                                                 'a' => array(
    417                                                         'type' => 'integer',
     443                register_setting(
     444                        'somegroup', 'mycustomsetting', array(
     445                                'show_in_rest' => array(
     446                                        'schema' => array(
     447                                                'type'       => 'object',
     448                                                'properties' => array(
     449                                                        'a' => array(
     450                                                                'type' => 'integer',
     451                                                        ),
    418452                                                ),
    419453                                        ),
    420454                                ),
    421                         ),
    422                         'type'         => 'object',
    423                 ) );
     455                                'type'         => 'object',
     456                        )
     457                );
    424458
    425459                // We have to re-register the route, as the args changes based off registered settings.
    … …  
    431465                $request->set_param( 'mycustomsetting', array( 'a' => 1 ) );
    432466                $response = $this->server->dispatch( $request );
    433                 $data = $response->get_data();
     467                $data     = $response->get_data();
    434468                $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
    435469                $this->assertEquals( array( 'a' => 1 ), get_option( 'mycustomsetting' ) );
    … …  
    439473                $request->set_param( 'mycustomsetting', array() );
    440474                $response = $this->server->dispatch( $request );
    441                 $data = $response->get_data();
     475                $data     = $response->get_data();
    442476                $this->assertEquals( array(), $data['mycustomsetting'] );
    443477                $this->assertEquals( array(), get_option( 'mycustomsetting' ) );
    … …  
    445479                // Provide more keys.
    446480                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    447                 $request->set_param( 'mycustomsetting', array( 'a' => 1, 'b' => 2 ) );
     481                $request->set_param(
     482                        'mycustomsetting', array(
     483                                'a' => 1,
     484                                'b' => 2,
     485                        )
     486                );
    448487                $response = $this->server->dispatch( $request );
    449488
    … …  
    465504                $request->set_param( 'description', 'The old description!' );
    466505                $response = $this->server->dispatch( $request );
    467                 $data = $response->get_data();
     506                $data     = $response->get_data();
    468507                $this->assertEquals( 200, $response->get_status() );
    469508                $this->assertEquals( 'The old title!', $data['title'] );
    … …  
    478517                $request->set_param( 'description', 'The new description!' );
    479518                $response = $this->server->dispatch( $request );
    480                 $data = $response->get_data();
     519                $data     = $response->get_data();
    481520
    482521                $this->assertEquals( 200, $response->get_status() );
    … …  
    523562                $request->set_param( 'posts_per_page', null );
    524563                $response = $this->server->dispatch( $request );
    525                 $data = $response->get_data();
     564                $data     = $response->get_data();
    526565
    527566                $this->assertEquals( 200, $response->get_status() );
    … …  
    542581                wp_set_current_user( self::$administrator );
    543582
    544                 register_setting( 'somegroup', 'mycustomsetting', array(
    545                         'show_in_rest' => true,
    546                         'type'         => 'string',
    547                 ) );
     583                register_setting(
     584                        'somegroup', 'mycustomsetting', array(
     585                                'show_in_rest' => true,
     586                                'type'         => 'string',
     587                        )
     588                );
    548589                update_option( 'mycustomsetting', array( 'A sneaky array!' ) );
    549590
    … …  
    557598
    558599        public function test_delete_item() {
    559                 /** Settings can't be deleted **/
    560                 $request = new WP_REST_Request( 'DELETE', '/wp/v2/settings/title' );
     600                /** Settings can't be deleted */
     601                $request  = new WP_REST_Request( 'DELETE', '/wp/v2/settings/title' );
    561602                $response = $this->server->dispatch( $request );
    562603                $this->assertEquals( 404, $response->get_status() );
Note: See TracChangeset for help on using the changeset viewer.