-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Icons: Enforce strict name validation in register method #11245
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
Open
im3dabasia
wants to merge
7
commits into
WordPress:trunk
Choose a base branch
from
im3dabasia:try/add-register-name-validation
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−0
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15c342e
feat: Update register() method to have more strict checks
im3dabasia fbcf54f
chore: Add tests
im3dabasia a5c4eea
fix: Feedbacks address related to dataProvider usage
im3dabasia c97b0b7
chore: Remove positive test, was not adding value
im3dabasia bf30902
chore: Fix tests
im3dabasia 5552f68
chore: Aligns PR with the GB PR
im3dabasia 40359b0
chore: Fix class name
im3dabasia 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
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,116 @@ | ||
| <?php | ||
| /** | ||
| * Tests for WP_Icons_Registry::register(). | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Icons | ||
| * | ||
| * @group icons | ||
| * @covers WP_Icons_Registry::register | ||
| * @covers WP_Icons_Registry::is_registered | ||
| */ | ||
| class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Registry instance for testing. | ||
| * | ||
| * @var WP_Icons_Registry | ||
| */ | ||
| private $registry; | ||
|
|
||
| /** | ||
| * Sets up the test fixture. | ||
| */ | ||
| public function set_up() { | ||
| parent::set_up(); | ||
| $this->registry = WP_Icons_Registry::get_instance(); | ||
| } | ||
|
|
||
| /** | ||
| * Tear down each test method. | ||
| */ | ||
| public function tear_down() { | ||
| $instance_property = new ReflectionProperty( WP_Icons_Registry::class, 'instance' ); | ||
| $instance_property->setAccessible( true ); | ||
| $instance_property->setValue( null, null ); | ||
|
|
||
| $this->registry = null; | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Invokes the WP_Icons_Registry::register method on the registry instance. | ||
| * | ||
| * @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' ); | ||
| $method->setAccessible( true ); | ||
|
im3dabasia marked this conversation as resolved.
Outdated
|
||
| return $method->invoke( $this->registry, $icon_name, $icon_properties ); | ||
| } | ||
|
|
||
| /** | ||
| * Should reject non-string names. | ||
| * | ||
| * @expectedIncorrectUsage WP_Icons_Registry::register | ||
| */ | ||
| public function test_invalid_non_string_names() { | ||
| $result = $this->register( 1, array() ); | ||
| $this->assertFalse( $result ); | ||
| } | ||
|
|
||
| /** | ||
| * Should reject icons without a namespace. | ||
| * | ||
| * @expectedIncorrectUsage WP_Icons_Registry::register | ||
| */ | ||
| public function test_invalid_names_without_namespace() { | ||
| $result = $this->register( 'plus', array() ); | ||
| $this->assertFalse( $result ); | ||
| } | ||
|
|
||
| /** | ||
| * Should reject icons with uppercase characters. | ||
| * | ||
| * @expectedIncorrectUsage WP_Icons_Registry::register | ||
| */ | ||
| public function test_uppercase_characters() { | ||
| $result = $this->register( 'Core/Plus', array() ); | ||
| $this->assertFalse( $result ); | ||
| } | ||
|
|
||
| /** | ||
| * 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 ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Should fail to re-register the same icon. | ||
| * | ||
| * @expectedIncorrectUsage WP_Icons_Registry::register | ||
| */ | ||
| public function test_register_icon_twice() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
| $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 ); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.