Make WordPress Core

Changeset 64310


Ignore:
Timestamp:
10/12/2026 11:30:08 AM (4 weeks ago)
Author:
wildworks
Message:

Tests: Expand coverage for the icon collections REST endpoint.

Adds tests for permission checks, _fields filtering, the schema, and collection parameters, and verifies that the built-in non-public core-admin collection is not exposed.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14195

Props noruzzaman, rejaulalomkhan, wildworks.
Fixes #66287.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/icons/wpRestIconCollectionsController.php

    r64224 r64310  
    203203
    204204        /**
     205         * Tests that getting a specific collection requires edit_posts capability.
     206         *
     207         * @ticket 66287
     208         *
     209         * @covers ::get_item_permissions_check
     210         */
     211        public function test_get_item_requires_permissions() {
     212                wp_set_current_user( self::$subscriber_id );
     213
     214                $request  = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/core' );
     215                $response = rest_get_server()->dispatch( $request );
     216
     217                $this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
     218        }
     219
     220        /**
    205221         * @ticket 64847
    206222         *
    … …  
    223239        /**
    224240         * @ticket 64847
     241         * @ticket 66287
    225242         *
    226243         * @covers ::prepare_item_for_response
    … …  
    230247
    231248                $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections' );
    232                 $request->set_param( '_fields', 'slug' );
    233                 $response = rest_get_server()->dispatch( $request );
    234                 $data     = $response->get_data();
    235 
    236                 $this->assertSame( 200, $response->get_status() );
     249                $request->set_param( '_fields', 'slug,label' );
     250                $response = rest_get_server()->dispatch( $request );
     251                $data     = $response->get_data();
     252
     253                $this->assertSame( 200, $response->get_status() );
     254                $this->assertIsArray( $data );
     255                $this->assertNotEmpty( $data );
    237256
    238257                foreach ( $data as $collection ) {
    239258                        $this->assertArrayHasKey( 'slug', $collection );
    240                         $this->assertArrayNotHasKey( 'label', $collection );
     259                        $this->assertArrayHasKey( 'label', $collection );
     260                        $this->assertArrayNotHasKey( 'description', $collection );
    241261                }
    242262        }
    243263
    244264        /**
    245          * @ticket 64847
     265         * Tests that the _fields parameter filters single collection response fields.
     266         *
     267         * @ticket 66287
     268         *
     269         * @covers ::prepare_item_for_response
     270         */
     271        public function test_get_item_fields_parameter() {
     272                wp_set_current_user( self::$editor_id );
     273
     274                $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/core' );
     275                $request->set_param( '_fields', 'label' );
     276                $response = rest_get_server()->dispatch( $request );
     277                $data     = $response->get_data();
     278
     279                $this->assertSame( 200, $response->get_status() );
     280                $this->assertArrayHasKey( 'label', $data );
     281                $this->assertArrayNotHasKey( 'slug', $data );
     282                $this->assertArrayNotHasKey( 'description', $data );
     283        }
     284
     285        /**
     286         * @ticket 64847
     287         * @ticket 66287
    246288         *
    247289         * @covers ::get_item_schema
    … …  
    251293                $response = rest_get_server()->dispatch( $request );
    252294                $data     = $response->get_data();
    253 
    254                 $properties = $data['schema']['properties'];
     295                $schema   = $data['schema'];
     296
     297                $this->assertSame( 'icon-collection', $schema['title'] );
     298                $this->assertSame( 'object', $schema['type'] );
     299
     300                $properties = $schema['properties'];
    255301                $this->assertCount( 3, $properties );
    256302                $this->assertArrayHasKey( 'slug', $properties );
    257303                $this->assertArrayHasKey( 'label', $properties );
    258304                $this->assertArrayHasKey( 'description', $properties );
     305
     306                $this->assertTrue( $properties['slug']['readonly'] );
     307                $this->assertTrue( $properties['label']['readonly'] );
     308                $this->assertTrue( $properties['description']['readonly'] );
    259309        }
    260310
    … …  
    307357         *
    308358         * @ticket 66087
     359         * @ticket 66287
    309360         *
    310361         * @covers ::get_items
    … …  
    323374                $this->assertContains( 'rest-visibility-public', $slugs );
    324375                $this->assertNotContains( 'rest-visibility-private', $slugs );
     376                $this->assertNotContains( 'core-admin', $slugs );
    325377        }
    326378
    … …  
    329381         *
    330382         * @ticket 66087
     383         * @ticket 66287
     384         *
     385         * @dataProvider data_non_public_collections
    331386         *
    332387         * @covers ::get_item
    333388         * @covers ::get_icon_collection
    334          */
    335         public function test_get_item_returns_404_for_non_public_collection() {
     389         *
     390         * @param string $slug Icon collection slug.
     391         */
     392        public function test_get_item_returns_404_for_non_public_collection( $slug ) {
    336393                $this->register_visibility_collections();
    337394
    338395                wp_set_current_user( self::$editor_id );
    339396
    340                 $request  = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/rest-visibility-private' );
     397                $request  = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/' . $slug );
    341398                $response = rest_get_server()->dispatch( $request );
    342399
    343400                $this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 );
     401        }
     402
     403        /**
     404         * Data provider of non-public collection slugs.
     405         *
     406         * @return array[]
     407         */
     408        public function data_non_public_collections() {
     409                return array(
     410                        'custom non-public collection' => array( 'rest-visibility-private' ),
     411                        'built-in core-admin'          => array( 'core-admin' ),
     412                );
    344413        }
    345414
    … …  
    377446                $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    378447        }
     448
     449        /**
     450         * Tests that collection query parameters include context with view default.
     451         *
     452         * @ticket 66287
     453         *
     454         * @covers ::get_collection_params
     455         */
     456        public function test_get_collection_params() {
     457                $controller = new WP_REST_Icon_Collections_Controller();
     458                $params     = $controller->get_collection_params();
     459
     460                $this->assertArrayHasKey( 'context', $params );
     461                $this->assertSame( 'view', $params['context']['default'] );
     462        }
    379463}
Note: See TracChangeset for help on using the changeset viewer.