From 298b19d9527eadc02a9975e38db9c39738bc457b Mon Sep 17 00:00:00 2001 From: noruzzamans Date: Sun, 11 Oct 2026 22:17:56 +0600 Subject: [PATCH 1/2] REST API: Add unit test coverage for WP_REST_Icon_Collections_Controller. See #66287. --- .../icons/wpRestIconCollectionsController.php | 83 ++++++++++++++++++- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/icons/wpRestIconCollectionsController.php b/tests/phpunit/tests/icons/wpRestIconCollectionsController.php index 7ed2473a30652..5b316fce28a05 100644 --- a/tests/phpunit/tests/icons/wpRestIconCollectionsController.php +++ b/tests/phpunit/tests/icons/wpRestIconCollectionsController.php @@ -201,6 +201,22 @@ public function test_get_item_requires_authentication() { $this->assertErrorResponse( 'rest_cannot_view', $response, 401 ); } + /** + * Tests that getting a specific collection requires edit_posts capability. + * + * @ticket 66287 + * + * @covers ::get_item_permissions_check + */ + public function test_get_item_requires_permissions() { + wp_set_current_user( self::$subscriber_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/core' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_cannot_view', $response, 403 ); + } + /** * @ticket 64847 * @@ -222,6 +238,7 @@ public function test_prepare_item() { /** * @ticket 64847 + * @ticket 66287 * * @covers ::prepare_item_for_response */ @@ -229,18 +246,42 @@ public function test_get_items_fields_parameter() { wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections' ); - $request->set_param( '_fields', 'slug' ); + $request->set_param( '_fields', 'slug,label' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertSame( 200, $response->get_status() ); + $this->assertIsArray( $data ); + $this->assertNotEmpty( $data ); foreach ( $data as $collection ) { $this->assertArrayHasKey( 'slug', $collection ); - $this->assertArrayNotHasKey( 'label', $collection ); + $this->assertArrayHasKey( 'label', $collection ); + $this->assertArrayNotHasKey( 'description', $collection ); } } + /** + * Tests that the _fields parameter filters single collection response fields. + * + * @ticket 66287 + * + * @covers ::prepare_item_for_response + */ + public function test_get_item_fields_parameter() { + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/core' ); + $request->set_param( '_fields', 'label' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( 200, $response->get_status() ); + $this->assertArrayHasKey( 'label', $data ); + $this->assertArrayNotHasKey( 'slug', $data ); + $this->assertArrayNotHasKey( 'description', $data ); + } + /** * @ticket 64847 * @@ -306,6 +347,7 @@ public function test_delete_item() { * Test that non-public collections are omitted from the collection list. * * @ticket 66087 + * @ticket 66287 * * @covers ::get_items */ @@ -322,27 +364,45 @@ public function test_get_items_omits_non_public_collections() { $slugs = wp_list_pluck( $response->get_data(), 'slug' ); $this->assertContains( 'rest-visibility-public', $slugs ); $this->assertNotContains( 'rest-visibility-private', $slugs ); + $this->assertNotContains( 'core-admin', $slugs ); } /** * Test that a non-public collection is reported as not found by slug. * * @ticket 66087 + * @ticket 66287 + * + * @dataProvider data_non_public_collections * * @covers ::get_item * @covers ::get_icon_collection + * + * @param string $slug Icon collection slug. */ - public function test_get_item_returns_404_for_non_public_collection() { + public function test_get_item_returns_404_for_non_public_collection( $slug ) { $this->register_visibility_collections(); wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/rest-visibility-private' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/' . $slug ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 ); } + /** + * Data provider of non-public collection slugs. + * + * @return array[] + */ + public function data_non_public_collections() { + return array( + 'custom non-public collection' => array( 'rest-visibility-private' ), + 'built-in core-admin' => array( 'core-admin' ), + ); + } + /** * Registers a public and a non-public collection. */ @@ -376,4 +436,19 @@ public function test_context_param() { $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); } + + /** + * Tests that collection query parameters include context with view default. + * + * @ticket 66287 + * + * @covers ::get_collection_params + */ + public function test_get_collection_params() { + $controller = new WP_REST_Icon_Collections_Controller(); + $params = $controller->get_collection_params(); + + $this->assertArrayHasKey( 'context', $params ); + $this->assertSame( 'view', $params['context']['default'] ); + } } From ff96a4e16114ee659c3d13472805d90efb06734b Mon Sep 17 00:00:00 2001 From: noruzzamans Date: Mon, 12 Oct 2026 15:38:40 +0600 Subject: [PATCH 2/2] Align icon collections schema test assertions with Gutenberg. See #66287. --- .../tests/icons/wpRestIconCollectionsController.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/icons/wpRestIconCollectionsController.php b/tests/phpunit/tests/icons/wpRestIconCollectionsController.php index 5b316fce28a05..d12d2222ed8a9 100644 --- a/tests/phpunit/tests/icons/wpRestIconCollectionsController.php +++ b/tests/phpunit/tests/icons/wpRestIconCollectionsController.php @@ -284,6 +284,7 @@ public function test_get_item_fields_parameter() { /** * @ticket 64847 + * @ticket 66287 * * @covers ::get_item_schema */ @@ -291,12 +292,20 @@ public function test_get_item_schema() { $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/icon-collections' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + $schema = $data['schema']; + + $this->assertSame( 'icon-collection', $schema['title'] ); + $this->assertSame( 'object', $schema['type'] ); - $properties = $data['schema']['properties']; + $properties = $schema['properties']; $this->assertCount( 3, $properties ); $this->assertArrayHasKey( 'slug', $properties ); $this->assertArrayHasKey( 'label', $properties ); $this->assertArrayHasKey( 'description', $properties ); + + $this->assertTrue( $properties['slug']['readonly'] ); + $this->assertTrue( $properties['label']['readonly'] ); + $this->assertTrue( $properties['description']['readonly'] ); } /**