Icons: Enforce strict name validation in register method#76079
Icons: Enforce strict name validation in register method#76079im3dabasia wants to merge 13 commits intoWordPress:trunkfrom
register method#76079Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
im3dabasia
left a comment
There was a problem hiding this comment.
Hey @mcsf ,
I picked up one of the follow-up related issue from here: #72215 (comment). When you have a chance, I’d love to hear your thoughts on this PR.
There was a problem hiding this comment.
I am not sure if this is the best filename. Please guide me in naming this correctly; I will update it.
| * | ||
| * @group icons | ||
| */ | ||
| class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase { |
There was a problem hiding this comment.
Similarly, I think the class name is not the correct one here.
There was a problem hiding this comment.
I think we should use Gutenberg_Icons_Registry_7_1 instead.
phpunit/experimental/class-gutenberg-icons-registry-7-1-test.php
Outdated
Show resolved
Hide resolved
t-hamano
left a comment
There was a problem hiding this comment.
Thanks for the PR!
We're currently working on some structural changes to further develop the icon registry on the Gutenberg plugin, so it might be worth coming back to this PR once that's complete.
t-hamano
left a comment
There was a problem hiding this comment.
Hi @im3dabasia, since #75878 has been merged, can you update this PR accordingly? We should update lib/compat/wordpress-7.1/class-gutenberg-icons-registry-7-1.php instead.
We also need to submit a core PR. See this core ticket: https://core.trac.wordpress.org/ticket/64847
If there's anything you don't understand, please feel free to ask 👍
| * | ||
| * @group icons | ||
| */ | ||
| class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase { |
There was a problem hiding this comment.
I think we should use Gutenberg_Icons_Registry_7_1 instead.
c091bf3 to
6f1de67
Compare
t-hamano
left a comment
There was a problem hiding this comment.
Thanks for the update!
We also need to submit a core PR. See this core ticket: https://core.trac.wordpress.org/ticket/64847
Can you confirm this point? Specifically, the following steps are required:
- Create a core PR that is the same as this PR. Link the pull request you created to https://core.trac.wordpress.org/ticket/64847.
- Create a backport changelog (ref)
register methodregister method
I added the exact same test cases in Core under a more suitable file name: I chose to keep the tests in a separate file because they are not related to the REST API. The other file in that directory contains tests related to the REST API, whereas my tests directly test the functions without using the API. I believe having a separate file for this should not be an issue, but please let me know what you think. I would appreciate some quick feedback on my PR (the Trac one). This is my first time doing this, and I really appreciate your constant guidance. Thanks!
I did this in the following commit: 6b9cd32 |
`core/plus` is a real icon, so use the `test/` namespace instead.
mcsf
left a comment
There was a problem hiding this comment.
Thanks for your contribution, @im3dabasia!
I pushed some commits directly to your fork. Have a look: it was all related to the test suite.
I also spot some small issues with the registration checks themselves. Once you've addressed those and once CI clears, we should be good to merge.
| return false; | ||
| } | ||
|
|
||
| $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/'; |
There was a problem hiding this comment.
It's best to use the same pattern found in the REST controller, not just for consistency but because it's stricter:
| return false; | ||
| } | ||
|
|
||
| if ( preg_match( '/[A-Z]+/', $icon_name ) ) { |
There was a problem hiding this comment.
Nit: the + is unnecessary.
| if ( preg_match( '/[A-Z]+/', $icon_name ) ) { | |
| if ( preg_match( '/[A-Z]/', $icon_name ) ) { |
What?
Follow-up: #72215 (comment) (3rd point)
Improves validation in
WP_Icons_Registry::registerand adds comprehensive unit tests for icon registration, including duplicate and invalid name handling.Why?
This PR ensures icon names are strictly validated (must be strings, namespace-prefixed, and lowercase), mirroring the approach used in
WP_Block_Type_Registry. It also adds tests to verify that invalid names and duplicate registrations are correctly rejected, improving reliability and consistency.How?
Refactored register() to enforce:
Added PHPUnit tests for: