Skip to content

Tests: Add unit test coverage for WP_REST_Icon_Collections_Controller - #83168

Open
noruzzamans wants to merge 8 commits into
WordPress:trunkfrom
noruzzamans:test/icon-collections-controller-unit-tests
Open

noruzzamans wants to merge 8 commits into
WordPress:trunkfrom
noruzzamans:test/icon-collections-controller-unit-tests

Conversation

@noruzzamans

Copy link
Copy Markdown
Contributor

Description

Adds unit test coverage for WP_REST_Icon_Collections_Controller (/wp/v2/icon-collections), verifying REST API route registration, permission gates, item and collection retrieval, field filtering, context filtering, schema compliance, and query parameters.

Follow-up to #79686 and #77260.

Context & Problem

WP_REST_Icon_Collections_Controller provides the REST endpoint used to query registered icon collections in the icon picker. While the registry (WP_Icon_Collections_Registry) and icon endpoint (/wp/v2/icons) have automated tests, the icon collections REST controller itself previously had no dedicated unit tests.

Test Coverage Added

This test suite (phpunit/class-wp-rest-icon-collections-controller-test.php) covers:

  1. Route Registration: Verifies /wp/v2/icon-collections and /wp/v2/icon-collections/<slug> routes are registered.
  2. Permission Checks:
    • Authorizes users with edit_posts capability (Administrator, Editor, Contributor).
    • Denies unauthenticated requests (401 rest_cannot_view).
    • Denies unauthorized users without edit capabilities (403 rest_cannot_view).
  3. Collection Retrieval (GET /wp/v2/icon-collections):
    • Returns list of registered collections (core and custom registered collections).
    • Validates response item structure (slug, label, description).
    • Supports field filtering via _fields parameter.
    • Respects embed context.
  4. Item Retrieval (GET /wp/v2/icon-collections/<slug>):
    • Returns single collection data for core and custom registered collections.
    • Returns 404 rest_icon_collection_not_found for non-existent collections.
    • Requires proper authentication and authorization.
    • Supports _fields parameter filtering on single items.
  5. Schema & Parameters:
    • Validates JSON schema definition against item schema requirements.
    • Confirms collection query parameter defaults (context => view).

Testing Instructions

Run PHPUnit tests for the new test class:

npm run test:unit:php:base -- --filter WP_Test_REST_Icon_Collections_Controller

Verify PHPCS coding standards:

vendor/bin/phpcs --standard=phpcs.xml.dist phpunit/class-wp-rest-icon-collections-controller-test.php

Scope

  • 100% test-only change (0 production code modifications).
  • 0 regression risk.

@noruzzamans
noruzzamans marked this pull request as ready for review September 19, 2026 06:16
@t-hamano t-hamano added [Type] Code Quality Issues or PRs that relate to code quality [Feature] Icons Related to Icon registration API and Icon REST API labels Sep 20, 2026
@t-hamano t-hamano mentioned this pull request Sep 30, 2026
26 tasks
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@t-hamano t-hamano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Since this PR was opened, a few changes to the Icons API landed in trunk that affect these tests. Now that trunk has been merged into this branch, could you update the tests to reflect them?

In #83277, the public property moved from icons to collections, and /wp/v2/icon-collections is now served by WP_REST_Icon_Collections_Controller_Gutenberg, a subclass of the controller tested here. It hides collections registered with 'public' => false.

This is now one of the most important behaviors of the endpoint, so it would be great to cover it. For example:

public function set_up() {
	parent::set_up();

	wp_register_icon_collection( 'test-public', array( 'label' => 'Test Public' ) );
	wp_register_icon_collection(
		'test-private',
		array(
			'label'  => 'Test Private',
			'public' => false,
		)
	);
}

public function tear_down() {
	wp_unregister_icon_collection( 'test-public' );
	wp_unregister_icon_collection( 'test-private' );
	parent::tear_down();
}

With this in place, the custom collections the individual tests currently register could probably be replaced by test-public, which would also simplify tear_down().

Tests worth adding:

  • The list includes test-public but not test-private or the built-in core-admin collection.
  • Requesting test-private directly returns a 404 with the rest_icon_collection_not_found code.

Once this PR is ready, it would be good to open a Core Trac ticket and a Core PR to apply the same changes to WordPress Core, and link it to this PR through the backport changelog. See the Core Backport Changelog documentation for details.

Comment thread phpunit/class-wp-rest-icon-collections-controller-test.php Outdated
Comment thread phpunit/class-wp-rest-icon-collections-controller-test.php Outdated
Comment thread phpunit/class-wp-rest-icon-collections-controller-test.php Outdated
noruzzamans and others added 4 commits October 4, 2026 13:17
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
@noruzzamans

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and helpful guidance, @t-hamano!

I have updated the test suite in 2f452ce:

  1. Fixture Setup & Cleanup: Registered test-public and test-private (with 'public' => false) in set_up(), and simplified tear_down() to unregister both collections.
  2. Replaced Custom Collections: Replaced individual test registrations with test-public across the suite.
  3. Visibility Filtering in get_items: Added test_get_items_omits_non_public_collections() asserting that GET /wp/v2/icon-collections includes test-public while omitting test-private and the built-in core-admin collection.
  4. 404 for Non-Public Collections: Added test_get_item_returns_404_for_non_public_collection() with a data provider verifying that requesting non-public collections (test-private and core-admin) returns a 404 with the rest_icon_collection_not_found error code.
  5. All tests pass PHP syntax verification and WordPress Coding Standards (phpcs clean).

@t-hamano t-hamano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noruzzamans Thanks for the update!

I noticed that two tests identical to the ones added in this PR already exist in another file. The code below can probably be removed as redundant.

/**
* Test that non-public collections are omitted from the collection list.
*/
public function test_get_collections_omits_non_public_collections() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$slugs = wp_list_pluck( $response->get_data(), 'slug' );
$this->assertContains( 'test-public', $slugs );
$this->assertNotContains( 'test-private', $slugs );
}
/**
* Test that non-public collections are not readable by slug.
*/
public function test_get_collection_returns_404_for_non_public_collection() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/test-private' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 );
}

Regarding the core backport PR, it will likely be difficult to prepare one right now since Core is slightly behind Gutenberg. I will let you know when the time comes.

@noruzzamans

Copy link
Copy Markdown
Contributor Author

Good catch, @t-hamano! Thank you for pointing that out.

In b1a270e, I have removed the two redundant collection tests (test_get_collections_omits_non_public_collections() and test_get_collection_returns_404_for_non_public_collection()) from phpunit/class-wp-rest-icon-controller-test.php, as they are now properly housed in phpunit/class-wp-rest-icon-collections-controller-test.php. Both test files pass PHP syntax checks and WordPress Coding Standards (phpcs clean).

Understood regarding the WordPress Core backport PR — whenever Core catches up and is ready for the backport, feel free to ping me and I will gladly prepare the Trac ticket and Core PR!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Icons Related to Icon registration API and Icon REST API [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants