Make WordPress Core


Ignore:
Timestamp:
02/20/2024 09:53:11 AM (3 years ago)
Author:
youknowriad
Message:

Tests: Synchronize Theme.JSON unit test between Core and Gutenberg.

Merges the changes from Core and Gutenberg for the following tests:

  • WP_REST_Global_Styles_Controller_Test
  • Tests_Theme_wpThemeJsonResolver
  • Tests_Theme_wpThemeJsonSchema
  • Tests_Theme_wpThemeJson

This will help ensure the stability of the theme.json style generation.

Props ajlende, scruffian, aaronrobertshaw, get_dave, youknowriad.
Fixes #60387.

File:
1 edited

Legend:

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

    r55457 r57662  
    3232        protected static $post_id;
    3333
    34         private function find_and_normalize_global_styles_by_id( $global_styles, $id ) {
    35                 foreach ( $global_styles as $style ) {
    36                         if ( $style['id'] === $id ) {
    37                                 unset( $style['_links'] );
    38                                 return $style;
    39                         }
    40                 }
    41 
    42                 return null;
    43         }
    44 
    4534        public function set_up() {
    4635                parent::set_up();
    … …  
    131120        }
    132121
    133         /**
    134          * @doesNotPerformAssertions
    135          */
    136         public function test_get_items() {
    137                 // Controller does not implement get_items().
    138         }
    139 
    140         /**
    141          * @covers WP_REST_Global_Styles_Controller::get_theme_item
    142          * @ticket 54516
    143          */
    144         public function test_get_theme_item_no_user() {
    145                 wp_set_current_user( 0 );
    146                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
    147                 $response = rest_get_server()->dispatch( $request );
    148                 $this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 401 );
    149         }
    150 
    151         /**
    152          * @covers WP_REST_Global_Styles_Controller::get_theme_item
    153          * @ticket 54516
    154          */
    155         public function test_get_theme_item_permission_check() {
    156                 wp_set_current_user( self::$subscriber_id );
    157                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
    158                 $response = rest_get_server()->dispatch( $request );
    159                 $this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 403 );
    160         }
    161 
    162         /**
    163          * @covers WP_REST_Global_Styles_Controller::get_theme_item
    164          * @ticket 54516
    165          */
    166         public function test_get_theme_item_invalid() {
    167                 wp_set_current_user( self::$admin_id );
    168                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/invalid' );
    169                 $response = rest_get_server()->dispatch( $request );
    170                 $this->assertErrorResponse( 'rest_theme_not_found', $response, 404 );
    171         }
    172 
    173         /**
    174          * @dataProvider data_get_theme_item_invalid_theme_dirname
    175          * @covers WP_REST_Global_Styles_Controller::get_theme_item
    176          * @ticket 54596
    177          *
    178          * @param string $theme_dirname Theme directory to test.
    179          * @param string $expected      Expected error code.
    180          */
    181         public function test_get_theme_item_invalid_theme_dirname( $theme_dirname, $expected ) {
    182                 wp_set_current_user( self::$admin_id );
    183                 switch_theme( $theme_dirname );
    184 
    185                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/' . $theme_dirname );
    186                 $response = rest_get_server()->dispatch( $request );
    187                 $this->assertErrorResponse( $expected, $response, 404 );
    188         }
    189 
    190         /**
    191          * Data provider.
    192          *
    193          * @return array
    194          */
    195         public function data_get_theme_item_invalid_theme_dirname() {
    196                 return array(
    197                         '+'                      => array(
    198                                 'theme_dirname' => 'my+theme+',
    199                                 'expected'      => 'rest_theme_not_found',
    200                         ),
    201                         ':'                      => array(
    202                                 'theme_dirname' => 'my:theme:',
    203                                 'expected'      => 'rest_no_route',
    204                         ),
    205                         '<>'                     => array(
    206                                 'theme_dirname' => 'my<theme>',
    207                                 'expected'      => 'rest_no_route',
    208                         ),
    209                         '*'                      => array(
    210                                 'theme_dirname' => 'my*theme*',
    211                                 'expected'      => 'rest_no_route',
    212                         ),
    213                         '?'                      => array(
    214                                 'theme_dirname' => 'my?theme?',
    215                                 'expected'      => 'rest_no_route',
    216                         ),
    217                         '"'                      => array(
    218                                 'theme_dirname' => 'my"theme?"',
    219                                 'expected'      => 'rest_no_route',
    220                         ),
    221                         '| (invalid on Windows)' => array(
    222                                 'theme_dirname' => 'my|theme|',
    223                                 'expected'      => 'rest_no_route',
    224                         ),
    225                         // Themes deep in subdirectories.
    226                         '2 subdirectories deep'  => array(
    227                                 'theme_dirname' => 'subdir/subsubdir/mytheme',
    228                                 'expected'      => 'rest_global_styles_not_found',
    229                         ),
    230                 );
    231         }
    232 
    233         /**
    234          * @dataProvider data_get_theme_item
    235          * @covers WP_REST_Global_Styles_Controller::get_theme_item
    236          * @ticket 54596
    237          *
    238          * @param string $theme Theme directory to test.
    239          */
    240         public function test_get_theme_item( $theme ) {
    241                 wp_set_current_user( self::$admin_id );
    242                 switch_theme( $theme );
    243 
    244                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/' . $theme );
    245                 $response = rest_get_server()->dispatch( $request );
    246                 $data     = $response->get_data();
    247                 $links    = $response->get_links();
    248                 $this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
    249                 $this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
    250                 $this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
    251                 $this->assertStringContainsString( '/wp/v2/global-styles/themes/' . $theme, $links['self'][0]['href'] );
    252         }
    253 
    254         /**
    255          * Data provider.
    256          *
    257          * @return array
    258          */
    259         public function data_get_theme_item() {
    260                 return array(
    261                         'alphabetic'                     => array( 'mytheme' ),
    262                         'alphanumeric'                   => array( 'mythemev1' ),
    263                         'àáâãäåæç'                       => array( 'àáâãäåæç' ),
    264                         'space'                          => array( 'my theme' ),
    265                         '-_.'                            => array( 'my_theme-0.1' ),
    266                         '[]'                             => array( 'my[theme]' ),
    267                         '()'                             => array( 'my(theme)' ),
    268                         '{}'                             => array( 'my{theme}' ),
    269                         '&=#@!$,^~%'                     => array( 'theme &=#@!$,^~%' ),
    270                         'all combined'                   => array( 'thémé {}&=@!$,^~%[0.1](-_-)' ),
    271 
    272                         // Themes in a subdirectory.
    273                         'subdir: alphabetic'             => array( 'subdir/mytheme' ),
    274                         'subdir: alphanumeric in theme'  => array( 'subdir/mythemev1' ),
    275                         'subdir: alphanumeric in subdir' => array( 'subdirv1/mytheme' ),
    276                         'subdir: alphanumeric in both'   => array( 'subdirv1/mythemev1' ),
    277                         'subdir: àáâãäåæç in theme'      => array( 'subdir/àáâãäåæç' ),
    278                         'subdir: àáâãäåæç in subdir'     => array( 'àáâãäåæç/mythemev1' ),
    279                         'subdir: àáâãäåæç in both'       => array( 'àáâãäåæç/àáâãäåæç' ),
    280                         'subdir: space in theme'         => array( 'subdir/my theme' ),
    281                         'subdir: space in subdir'        => array( 'sub dir/mytheme' ),
    282                         'subdir: space in both'          => array( 'sub dir/my theme' ),
    283                         'subdir: -_. in theme'           => array( 'subdir/my_theme-0.1' ),
    284                         'subdir: -_. in subdir'          => array( 'sub_dir-0.1/mytheme' ),
    285                         'subdir: -_. in both'            => array( 'sub_dir-0.1/my_theme-0.1' ),
    286                         'subdir: all combined in theme'  => array( 'subdir/thémé {}&=@!$,^~%[0.1](-_-)' ),
    287                         'subdir: all combined in subdir' => array( 'sűbdīr {}&=@!$,^~%[0.1](-_-)/mytheme' ),
    288                         'subdir: all combined in both'   => array( 'sűbdīr {}&=@!$,^~%[0.1](-_-)/thémé {}&=@!$,^~%[0.1](-_-)' ),
    289                 );
    290         }
    291 
    292         /**
    293          * @covers WP_REST_Global_Styles_Controller::get_theme_item
    294          * @ticket 54595
    295          */
    296         public function test_get_theme_item_fields() {
    297                 wp_set_current_user( self::$admin_id );
    298                 $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
    299                 $request->set_param( '_fields', 'settings' );
    300                 $response = rest_get_server()->dispatch( $request );
    301                 $data     = $response->get_data();
    302                 $this->assertArrayHasKey( 'settings', $data );
    303                 $this->assertArrayNotHasKey( 'styles', $data );
    304         }
    305 
    306         /**
    307          * @covers WP_REST_Global_Styles_Controller::get_item
    308          * @ticket 54516
    309          */
    310         public function test_get_item_no_user() {
    311                 wp_set_current_user( 0 );
    312                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
    313                 $response = rest_get_server()->dispatch( $request );
    314                 $this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
    315         }
    316 
    317         /**
    318          * @covers WP_REST_Global_Styles_Controller::get_item
    319          * @ticket 54516
    320          */
    321         public function test_get_item_invalid_post() {
    322                 wp_set_current_user( self::$admin_id );
    323                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$post_id );
    324                 $response = rest_get_server()->dispatch( $request );
    325                 $this->assertErrorResponse( 'rest_global_styles_not_found', $response, 404 );
    326         }
    327 
    328         /**
    329          * @covers WP_REST_Global_Styles_Controller::get_item
    330          * @ticket 54516
    331          */
    332         public function test_get_item_permission_check() {
    333                 wp_set_current_user( self::$subscriber_id );
    334                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
    335                 $response = rest_get_server()->dispatch( $request );
    336                 $this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
    337         }
    338 
    339         /**
    340          * @covers WP_REST_Global_Styles_Controller::get_item
    341          * @ticket 54516
    342          */
    343         public function test_get_item_no_user_edit() {
    344                 wp_set_current_user( 0 );
    345                 $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
    346                 $request->set_param( 'context', 'edit' );
    347                 $response = rest_get_server()->dispatch( $request );
    348                 $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
    349         }
    350 
    351         /**
    352          * @covers WP_REST_Global_Styles_Controller::get_item
    353          * @ticket 54516
    354          */
    355         public function test_get_item_permission_check_edit() {
    356                 wp_set_current_user( self::$subscriber_id );
    357                 $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
    358                 $request->set_param( 'context', 'edit' );
    359                 $response = rest_get_server()->dispatch( $request );
    360                 $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 );
    361         }
    362 
    363         /**
    364          * @covers WP_REST_Global_Styles_Controller::get_item
    365          */
    366         public function test_get_item() {
    367                 wp_set_current_user( self::$admin_id );
    368                 $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
    369                 $response = rest_get_server()->dispatch( $request );
    370                 $data     = $response->get_data();
    371                 $links    = $response->get_links();
    372 
    373                 $this->assertEqualSets(
    374                         array(
    375                                 'id'       => self::$global_styles_id,
    376                                 'title'    => array(
    377                                         'raw'      => 'Custom Styles',
    378                                         'rendered' => 'Custom Styles',
    379                                 ),
    380                                 'settings' => new stdClass(),
    381                                 'styles'   => new stdClass(),
    382                         ),
    383                         $data
    384                 );
    385 
    386                 $this->assertArrayHasKey( 'self', $links );
    387                 $this->assertStringContainsString( '/wp/v2/global-styles/' . self::$global_styles_id, $links['self'][0]['href'] );
    388         }
    389 
    390         /**
    391          * @doesNotPerformAssertions
    392          */
    393         public function test_create_item() {
    394                 // Controller does not implement create_item().
    395         }
    396 
    397         /**
    398          * @covers WP_REST_Global_Styles_Controller::update_item
    399          * @ticket 54516
    400          */
    401         public function test_update_item() {
    402                 wp_set_current_user( self::$admin_id );
    403                 $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
    404                 $request->set_body_params(
    405                         array(
    406                                 'title' => 'My new global styles title',
    407                         )
    408                 );
    409                 $response = rest_get_server()->dispatch( $request );
    410                 $data     = $response->get_data();
    411                 $this->assertSame( 'My new global styles title', $data['title']['raw'] );
    412         }
    413 
    414 
    415         /**
    416          * @covers WP_REST_Global_Styles_Controller::update_item
    417          * @ticket 54516
    418          */
    419         public function test_update_item_no_user() {
    420                 wp_set_current_user( 0 );
    421                 $request  = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
    422                 $response = rest_get_server()->dispatch( $request );
    423                 $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
    424         }
    425 
    426         /**
    427          * @covers WP_REST_Global_Styles_Controller::update_item
    428          * @ticket 54516
    429          */
    430         public function test_update_item_invalid_post() {
    431                 wp_set_current_user( self::$admin_id );
    432                 $request  = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$post_id );
    433                 $response = rest_get_server()->dispatch( $request );
    434                 $this->assertErrorResponse( 'rest_global_styles_not_found', $response, 404 );
    435         }
    436 
    437         /**
    438          * @covers WP_REST_Global_Styles_Controller::update_item
    439          * @ticket 54516
    440          */
    441         public function test_update_item_permission_check() {
    442                 wp_set_current_user( self::$subscriber_id );
    443                 $request  = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
    444                 $response = rest_get_server()->dispatch( $request );
    445                 $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
    446         }
    447 
    448         /**
    449          * @doesNotPerformAssertions
    450          */
    451         public function test_delete_item() {
    452                 // Controller does not implement delete_item().
    453         }
    454 
    455         /**
    456          * @doesNotPerformAssertions
    457          */
    458         public function test_prepare_item() {
    459                 // Controller does not implement prepare_item().
    460         }
    461 
    462         /**
    463          * @covers WP_REST_Global_Styles_Controller::get_item_schema
    464          * @ticket 54516
    465          */
    466         public function test_get_item_schema() {
    467                 $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
    468                 $response   = rest_get_server()->dispatch( $request );
    469                 $data       = $response->get_data();
    470                 $properties = $data['schema']['properties'];
    471                 $this->assertCount( 4, $properties, 'Schema properties array does not have exactly 4 elements' );
    472                 $this->assertArrayHasKey( 'id', $properties, 'Schema properties array does not have "id" key' );
    473                 $this->assertArrayHasKey( 'styles', $properties, 'Schema properties array does not have "styles" key' );
    474                 $this->assertArrayHasKey( 'settings', $properties, 'Schema properties array does not have "settings" key' );
    475                 $this->assertArrayHasKey( 'title', $properties, 'Schema properties array does not have "title" key' );
    476         }
    477 
    478 
    479122        public function test_get_theme_items() {
    480123                wp_set_current_user( self::$admin_id );
    … …  
    484127                $data     = $response->get_data();
    485128                $expected = array(
     129                        array(
     130                                'version'  => 2,
     131                                'title'    => 'variation-a',
     132                                'settings' => array(
     133                                        'blocks' => array(
     134                                                'core/paragraph' => array(
     135                                                        'color' => array(
     136                                                                'palette' => array(
     137                                                                        'theme' => array(
     138                                                                                array(
     139                                                                                        'slug'  => 'light',
     140                                                                                        'name'  => 'Light',
     141                                                                                        'color' => '#f2f2f2',
     142                                                                                ),
     143                                                                        ),
     144                                                                ),
     145                                                        ),
     146                                                ),
     147                                        ),
     148                                ),
     149                        ),
    486150                        array(
    487151                                'version'  => 2,
    … …  
    536200                wp_recursive_ksort( $expected );
    537201
    538                 $this->assertSameSets( $data, $expected );
     202                $this->assertSameSets( $expected, $data );
     203        }
     204
     205        /**
     206         * @doesNotPerformAssertions
     207         */
     208        public function test_get_items() {
     209                // Controller does not implement get_items().
     210        }
     211
     212        /**
     213         * @covers WP_REST_Global_Styles_Controller::get_theme_item
     214         * @ticket 54516
     215         */
     216        public function test_get_theme_item_no_user() {
     217                wp_set_current_user( 0 );
     218                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
     219                $response = rest_get_server()->dispatch( $request );
     220                $this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 401 );
     221        }
     222
     223        /**
     224         * @covers WP_REST_Global_Styles_Controller::get_theme_item
     225         * @ticket 54516
     226         */
     227        public function test_get_theme_item_permission_check() {
     228                wp_set_current_user( self::$subscriber_id );
     229                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
     230                $response = rest_get_server()->dispatch( $request );
     231                $this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 403 );
     232        }
     233
     234        /**
     235         * @covers WP_REST_Global_Styles_Controller::get_theme_item
     236         * @ticket 54516
     237         */
     238        public function test_get_theme_item_invalid() {
     239                wp_set_current_user( self::$admin_id );
     240                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/invalid' );
     241                $response = rest_get_server()->dispatch( $request );
     242                $this->assertErrorResponse( 'rest_theme_not_found', $response, 404 );
     243        }
     244
     245        /**
     246         * @dataProvider data_get_theme_item_invalid_theme_dirname
     247         * @covers WP_REST_Global_Styles_Controller::get_theme_item
     248         * @ticket 54596
     249         *
     250         * @param string $theme_dirname Theme directory to test.
     251         * @param string $expected      Expected error code.
     252         */
     253        public function test_get_theme_item_invalid_theme_dirname( $theme_dirname, $expected ) {
     254                wp_set_current_user( self::$admin_id );
     255                switch_theme( $theme_dirname );
     256
     257                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/' . $theme_dirname );
     258                $response = rest_get_server()->dispatch( $request );
     259                $this->assertErrorResponse( $expected, $response, 404 );
     260        }
     261
     262        /**
     263         * Data provider.
     264         *
     265         * @return array
     266         */
     267        public function data_get_theme_item_invalid_theme_dirname() {
     268                return array(
     269                        '+'                      => array(
     270                                'theme_dirname' => 'my+theme+',
     271                                'expected'      => 'rest_theme_not_found',
     272                        ),
     273                        ':'                      => array(
     274                                'theme_dirname' => 'my:theme:',
     275                                'expected'      => 'rest_no_route',
     276                        ),
     277                        '<>'                     => array(
     278                                'theme_dirname' => 'my<theme>',
     279                                'expected'      => 'rest_no_route',
     280                        ),
     281                        '*'                      => array(
     282                                'theme_dirname' => 'my*theme*',
     283                                'expected'      => 'rest_no_route',
     284                        ),
     285                        '?'                      => array(
     286                                'theme_dirname' => 'my?theme?',
     287                                'expected'      => 'rest_no_route',
     288                        ),
     289                        '"'                      => array(
     290                                'theme_dirname' => 'my"theme?"',
     291                                'expected'      => 'rest_no_route',
     292                        ),
     293                        '| (invalid on Windows)' => array(
     294                                'theme_dirname' => 'my|theme|',
     295                                'expected'      => 'rest_no_route',
     296                        ),
     297                        // Themes deep in subdirectories.
     298                        '2 subdirectories deep'  => array(
     299                                'theme_dirname' => 'subdir/subsubdir/mytheme',
     300                                'expected'      => 'rest_global_styles_not_found',
     301                        ),
     302                );
     303        }
     304
     305        /**
     306         * @dataProvider data_get_theme_item
     307         * @covers WP_REST_Global_Styles_Controller::get_theme_item
     308         * @ticket 54596
     309         *
     310         * @param string $theme Theme directory to test.
     311         */
     312        public function test_get_theme_item( $theme ) {
     313                wp_set_current_user( self::$admin_id );
     314                switch_theme( $theme );
     315
     316                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/' . $theme );
     317                $response = rest_get_server()->dispatch( $request );
     318                $data     = $response->get_data();
     319                $links    = $response->get_links();
     320                $this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
     321                $this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
     322                $this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
     323                $this->assertStringContainsString( '/wp/v2/global-styles/themes/' . $theme, $links['self'][0]['href'] );
     324        }
     325
     326        /**
     327         * Data provider.
     328         *
     329         * @return array
     330         */
     331        public function data_get_theme_item() {
     332                return array(
     333                        'alphabetic'                     => array( 'mytheme' ),
     334                        'alphanumeric'                   => array( 'mythemev1' ),
     335                        'àáâãäåæç'                       => array( 'àáâãäåæç' ),
     336                        'space'                          => array( 'my theme' ),
     337                        '-_.'                            => array( 'my_theme-0.1' ),
     338                        '[]'                             => array( 'my[theme]' ),
     339                        '()'                             => array( 'my(theme)' ),
     340                        '{}'                             => array( 'my{theme}' ),
     341                        '&=#@!$,^~%'                     => array( 'theme &=#@!$,^~%' ),
     342                        'all combined'                   => array( 'thémé {}&=@!$,^~%[0.1](-_-)' ),
     343
     344                        // Themes in a subdirectory.
     345                        'subdir: alphabetic'             => array( 'subdir/mytheme' ),
     346                        'subdir: alphanumeric in theme'  => array( 'subdir/mythemev1' ),
     347                        'subdir: alphanumeric in subdir' => array( 'subdirv1/mytheme' ),
     348                        'subdir: alphanumeric in both'   => array( 'subdirv1/mythemev1' ),
     349                        'subdir: àáâãäåæç in theme'      => array( 'subdir/àáâãäåæç' ),
     350                        'subdir: àáâãäåæç in subdir'     => array( 'àáâãäåæç/mythemev1' ),
     351                        'subdir: àáâãäåæç in both'       => array( 'àáâãäåæç/àáâãäåæç' ),
     352                        'subdir: space in theme'         => array( 'subdir/my theme' ),
     353                        'subdir: space in subdir'        => array( 'sub dir/mytheme' ),
     354                        'subdir: space in both'          => array( 'sub dir/my theme' ),
     355                        'subdir: -_. in theme'           => array( 'subdir/my_theme-0.1' ),
     356                        'subdir: -_. in subdir'          => array( 'sub_dir-0.1/mytheme' ),
     357                        'subdir: -_. in both'            => array( 'sub_dir-0.1/my_theme-0.1' ),
     358                        'subdir: all combined in theme'  => array( 'subdir/thémé {}&=@!$,^~%[0.1](-_-)' ),
     359                        'subdir: all combined in subdir' => array( 'sűbdīr {}&=@!$,^~%[0.1](-_-)/mytheme' ),
     360                        'subdir: all combined in both'   => array( 'sűbdīr {}&=@!$,^~%[0.1](-_-)/thémé {}&=@!$,^~%[0.1](-_-)' ),
     361                );
     362        }
     363
     364        /**
     365         * @covers WP_REST_Global_Styles_Controller::get_theme_item
     366         * @ticket 54595
     367         */
     368        public function test_get_theme_item_fields() {
     369                wp_set_current_user( self::$admin_id );
     370                $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
     371                $request->set_param( '_fields', 'settings' );
     372                $response = rest_get_server()->dispatch( $request );
     373                $data     = $response->get_data();
     374                $this->assertArrayHasKey( 'settings', $data );
     375                $this->assertArrayNotHasKey( 'styles', $data );
     376        }
     377
     378        /**
     379         * @covers WP_REST_Global_Styles_Controller::get_item
     380         * @ticket 54516
     381         */
     382        public function test_get_item_no_user() {
     383                wp_set_current_user( 0 );
     384                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
     385                $response = rest_get_server()->dispatch( $request );
     386                $this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
     387        }
     388
     389        /**
     390         * @covers WP_REST_Global_Styles_Controller::get_item
     391         * @ticket 54516
     392         */
     393        public function test_get_item_invalid_post() {
     394                wp_set_current_user( self::$admin_id );
     395                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$post_id );
     396                $response = rest_get_server()->dispatch( $request );
     397                $this->assertErrorResponse( 'rest_global_styles_not_found', $response, 404 );
     398        }
     399
     400        /**
     401         * @covers WP_REST_Global_Styles_Controller::get_item
     402         * @ticket 54516
     403         */
     404        public function test_get_item_permission_check() {
     405                wp_set_current_user( self::$subscriber_id );
     406                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
     407                $response = rest_get_server()->dispatch( $request );
     408                $this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
     409        }
     410
     411        /**
     412         * @covers WP_REST_Global_Styles_Controller::get_item
     413         * @ticket 54516
     414         */
     415        public function test_get_item_no_user_edit() {
     416                wp_set_current_user( 0 );
     417                $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
     418                $request->set_param( 'context', 'edit' );
     419                $response = rest_get_server()->dispatch( $request );
     420                $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
     421        }
     422
     423        /**
     424         * @covers WP_REST_Global_Styles_Controller::get_item
     425         * @ticket 54516
     426         */
     427        public function test_get_item_permission_check_edit() {
     428                wp_set_current_user( self::$subscriber_id );
     429                $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
     430                $request->set_param( 'context', 'edit' );
     431                $response = rest_get_server()->dispatch( $request );
     432                $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 );
     433        }
     434
     435        /**
     436         * @covers WP_REST_Global_Styles_Controller::get_item
     437         */
     438        public function test_get_item() {
     439                wp_set_current_user( self::$admin_id );
     440                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
     441                $response = rest_get_server()->dispatch( $request );
     442                $data     = $response->get_data();
     443                $links    = $response->get_links();
     444
     445                $this->assertEqualSets(
     446                        array(
     447                                'id'       => self::$global_styles_id,
     448                                'title'    => array(
     449                                        'raw'      => 'Custom Styles',
     450                                        'rendered' => 'Custom Styles',
     451                                ),
     452                                'settings' => new stdClass(),
     453                                'styles'   => new stdClass(),
     454                        ),
     455                        $data
     456                );
     457
     458                $this->assertArrayHasKey( 'self', $links );
     459                $this->assertStringContainsString( '/wp/v2/global-styles/' . self::$global_styles_id, $links['self'][0]['href'] );
     460        }
     461
     462        /**
     463         * @doesNotPerformAssertions
     464         */
     465        public function test_create_item() {
     466                // Controller does not implement create_item().
     467        }
     468
     469        /**
     470         * @covers WP_REST_Global_Styles_Controller::update_item
     471         * @ticket 54516
     472         */
     473        public function test_update_item() {
     474                wp_set_current_user( self::$admin_id );
     475                $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     476                $request->set_body_params(
     477                        array(
     478                                'title' => 'My new global styles title',
     479                        )
     480                );
     481                $response = rest_get_server()->dispatch( $request );
     482                $data     = $response->get_data();
     483                $this->assertSame( 'My new global styles title', $data['title']['raw'] );
     484        }
     485
     486        /**
     487         * @covers WP_REST_Global_Styles_Controller::update_item
     488         * @ticket 54516
     489         */
     490        public function test_update_item_no_user() {
     491                wp_set_current_user( 0 );
     492                $request  = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     493                $response = rest_get_server()->dispatch( $request );
     494                $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
     495        }
     496
     497        /**
     498         * @covers WP_REST_Global_Styles_Controller::update_item
     499         * @ticket 54516
     500         */
     501        public function test_update_item_invalid_post() {
     502                wp_set_current_user( self::$admin_id );
     503                $request  = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$post_id );
     504                $response = rest_get_server()->dispatch( $request );
     505                $this->assertErrorResponse( 'rest_global_styles_not_found', $response, 404 );
     506        }
     507
     508        /**
     509         * @covers WP_REST_Global_Styles_Controller::update_item
     510         * @ticket 54516
     511         */
     512        public function test_update_item_permission_check() {
     513                wp_set_current_user( self::$subscriber_id );
     514                $request  = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     515                $response = rest_get_server()->dispatch( $request );
     516                $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
     517        }
     518
     519        /**
     520         * @covers WP_REST_Global_Styles_Controller::update_item
     521         * @ticket 57536
     522         */
     523        public function test_update_item_valid_styles_css() {
     524                wp_set_current_user( self::$admin_id );
     525                if ( is_multisite() ) {
     526                        grant_super_admin( self::$admin_id );
     527                }
     528                $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     529                $request->set_body_params(
     530                        array(
     531                                'styles' => array( 'css' => 'body { color: red; }' ),
     532                        )
     533                );
     534                $response = rest_get_server()->dispatch( $request );
     535                $data     = $response->get_data();
     536                $this->assertSame( 'body { color: red; }', $data['styles']['css'] );
     537        }
     538
     539        /**
     540         * @covers WP_REST_Global_Styles_Controller::update_item
     541         * @ticket 57536
     542         */
     543        public function test_update_item_invalid_styles_css() {
     544                wp_set_current_user( self::$admin_id );
     545                if ( is_multisite() ) {
     546                        grant_super_admin( self::$admin_id );
     547                }
     548                $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     549                $request->set_body_params(
     550                        array(
     551                                'styles' => array( 'css' => '<p>test</p> body { color: red; }' ),
     552                        )
     553                );
     554                $response = rest_get_server()->dispatch( $request );
     555                $this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
     556        }
     557
     558        /**
     559         * @doesNotPerformAssertions
     560         */
     561        public function test_delete_item() {
     562                // Controller does not implement delete_item().
     563        }
     564
     565        /**
     566         * @doesNotPerformAssertions
     567         */
     568        public function test_prepare_item() {
     569                // Controller does not implement prepare_item().
     570        }
     571
     572        /**
     573         * @covers WP_REST_Global_Styles_Controller::get_item_schema
     574         * @ticket 54516
     575         */
     576        public function test_get_item_schema() {
     577                $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
     578                $response   = rest_get_server()->dispatch( $request );
     579                $data       = $response->get_data();
     580                $properties = $data['schema']['properties'];
     581                $this->assertCount( 4, $properties, 'Schema properties array does not have exactly 4 elements' );
     582                $this->assertArrayHasKey( 'id', $properties, 'Schema properties array does not have "id" key' );
     583                $this->assertArrayHasKey( 'styles', $properties, 'Schema properties array does not have "styles" key' );
     584                $this->assertArrayHasKey( 'settings', $properties, 'Schema properties array does not have "settings" key' );
     585                $this->assertArrayHasKey( 'title', $properties, 'Schema properties array does not have "title" key' );
    539586        }
    540587
    … …  
    557604                }
    558605        }
    559 
    560         /**
    561          * @covers WP_REST_Global_Styles_Controller::update_item
    562          * @ticket 57536
    563          */
    564         public function test_update_item_valid_styles_css() {
    565                 wp_set_current_user( self::$admin_id );
    566                 if ( is_multisite() ) {
    567                         grant_super_admin( self::$admin_id );
    568                 }
    569                 $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
    570                 $request->set_body_params(
    571                         array(
    572                                 'styles' => array( 'css' => 'body { color: red; }' ),
    573                         )
    574                 );
    575                 $response = rest_get_server()->dispatch( $request );
    576                 $data     = $response->get_data();
    577                 $this->assertSame( 'body { color: red; }', $data['styles']['css'] );
    578         }
    579 
    580         /**
    581          * @covers WP_REST_Global_Styles_Controller::update_item
    582          * @ticket 57536
    583          */
    584         public function test_update_item_invalid_styles_css() {
    585                 wp_set_current_user( self::$admin_id );
    586                 if ( is_multisite() ) {
    587                         grant_super_admin( self::$admin_id );
    588                 }
    589                 $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
    590                 $request->set_body_params(
    591                         array(
    592                                 'styles' => array( 'css' => '<p>test</p> body { color: red; }' ),
    593                         )
    594                 );
    595                 $response = rest_get_server()->dispatch( $request );
    596                 $this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
    597         }
    598606}
Note: See TracChangeset for help on using the changeset viewer.