-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Icons: Enforce strict name validation in register method
#76079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5e9eca6
feat: Updated registry method with rigid checks
im3dabasia 7693957
fix: Updated test_register_icon_twice test
im3dabasia 5c559bd
fix: PHP lint errors fix
im3dabasia 9bedf37
chore: Move register regex checks to 7.1 and renamed tests class/file…
im3dabasia 6f1de67
chore: Added register function in wp-7.1/
im3dabasia 398b9d0
chore: Update tests
im3dabasia 6b9cd32
docs: Add changelog for backport
im3dabasia ef7d67c
chore: Feedbacks addressed as per upstream suggestions
im3dabasia 8d42b86
Remove broken code and comment use of PHP_VERSION_ID
mcsf f455376
Fix test case to avoid name collisions
mcsf 23ca8a2
Actually test the cases in `data_invalid_icon_names`
mcsf a7c0f37
Fix comments
mcsf ac6fe80
Remove test in favour of test_register_invalid_name
mcsf 3be4b1a
chore: Update fix feedbacks
im3dabasia e02cd8f
Add missin ReflectionMethod::setAccessible for older PHPs
mcsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/11245 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/76079 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
phpunit/experimental/class-gutenberg-icons-registry-7-1-test.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <?php | ||
| /** | ||
| * Unit tests covering Gutenberg_Icons_Registry_7_1::register functionality. | ||
| * | ||
| * @package Gutenberg | ||
| */ | ||
| class Gutenberg_Icons_Registry_7_1_Test extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Registry instance for testing. | ||
| * | ||
| * @var Gutenberg_Icons_Registry_7_1 | ||
| */ | ||
| private $registry; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| $this->registry = Gutenberg_Icons_Registry_7_1::get_instance(); | ||
| } | ||
|
|
||
| public function tear_down() { | ||
| $instance_property = new ReflectionProperty( Gutenberg_Icons_Registry_7_1::class, 'instance' ); | ||
|
|
||
| /* | ||
| * ReflectionProperty::setAccessible is: | ||
| * - redundant as of 8.1.0, which made all properties accessible | ||
| * - deprecated as of 8.5.0 | ||
| * - needed until 8.1.0, as property `instance` is private | ||
| */ | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $instance_property->setAccessible( true ); | ||
| } | ||
|
|
||
| $instance_property->setValue( null, null ); | ||
|
|
||
| $this->registry = null; | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Invokes Gutenberg_Icons_Registry_7_1::register despite it being private | ||
| * | ||
| * @param string $icon_name Icon name including namespace. | ||
| * @param array $icon_properties Icon properties (label, content, filePath). | ||
| * @return bool True if the icon was registered successfully. | ||
| */ | ||
| private function register( $icon_name, $icon_properties ) { | ||
| $method = new ReflectionMethod( $this->registry, 'register' ); | ||
|
|
||
| /* | ||
| * ReflectionMethod::setAccessible is: | ||
| * - redundant as of 8.1.0, which made all properties accessible | ||
| * - deprecated as of 8.5.0 | ||
| * - needed until 8.1.0, as property `instance` is private | ||
| */ | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $method->setAccessible( true ); | ||
| } | ||
|
|
||
| return $method->invoke( $this->registry, $icon_name, $icon_properties ); | ||
| } | ||
|
|
||
| /** | ||
| * Should accept valid icon names. | ||
| */ | ||
| public function test_register_icon() { | ||
| $name = 'test-plugin/my-icon'; | ||
| $settings = array( | ||
| 'label' => 'My Icon', | ||
| 'content' => '<svg></svg>', | ||
| ); | ||
|
|
||
| $result = $this->register( $name, $settings ); | ||
| $this->assertTrue( $result ); | ||
| $this->assertTrue( $this->registry->is_registered( $name ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Provides invalid icon names. | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public function data_invalid_icon_names() { | ||
| return array( | ||
| 'non-string name' => array( 1 ), | ||
| 'no namespace' => array( 'plus' ), | ||
| 'uppercase characters' => array( 'Test/Plus' ), | ||
| 'invalid characters' => array( 'test/_doing_it_wrong' ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Should fail to re-register the same icon. | ||
| * | ||
| * @expectedIncorrectUsage Gutenberg_Icons_Registry_7_1::register | ||
| */ | ||
| public function test_register_icon_twice() { | ||
| $name = 'test-plugin/duplicate'; | ||
| $settings = array( | ||
| 'label' => 'Icon', | ||
| 'content' => '<svg></svg>', | ||
| ); | ||
|
|
||
| $result = $this->register( $name, $settings ); | ||
| $this->assertTrue( $result ); | ||
| $result2 = $this->register( $name, $settings ); | ||
| $this->assertFalse( $result2 ); | ||
| } | ||
|
|
||
| /** | ||
| * Should fail to register icon with invalid names. | ||
| * | ||
| * @dataProvider data_invalid_icon_names | ||
| * @expectedIncorrectUsage Gutenberg_Icons_Registry_7_1::register | ||
| */ | ||
| public function test_register_invalid_name() { | ||
| foreach ( $this->data_invalid_icon_names() as $name ) { | ||
| $settings = array( | ||
| 'label' => 'Icon', | ||
| 'content' => '<svg></svg>', | ||
| ); | ||
|
|
||
| $result = $this->register( $name, $settings ); | ||
| $this->assertFalse( $result ); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this is the best filename. Please guide me in naming this correctly; I will update it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine, thanks. :)