-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Simplify NoSuspiciousCharactersValidator
#54062
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
nicolas-grekas
merged 1 commit into
symfony:7.0
from
MatTheCat:simplify_no-suspicious-characters-validator
Feb 27, 2024
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -56,23 +56,31 @@ public static function provideNonSuspiciousStrings(): iterable | |
/** | ||
* @dataProvider provideSuspiciousStrings | ||
*/ | ||
public function testSuspiciousStrings(string $string, array $options, string $errorCode, string $errorMessage) | ||
public function testSuspiciousStrings(string $string, array $options, array $errors) | ||
{ | ||
$this->validator->validate($string, new NoSuspiciousCharacters($options)); | ||
|
||
$this->buildViolation($errorMessage) | ||
->setCode($errorCode) | ||
$violations = $this->buildViolation(reset($errors)) | ||
->setCode(key($errors)) | ||
->setParameter('{{ value }}', '"'.$string.'"') | ||
->assertRaised(); | ||
; | ||
|
||
while ($message = next($errors)) { | ||
$violations = $violations->buildNextViolation($message) | ||
->setCode(key($errors)) | ||
->setParameter('{{ value }}', '"'.$string.'"') | ||
; | ||
} | ||
|
||
$violations->assertRaised(); | ||
} | ||
|
||
public static function provideSuspiciousStrings(): iterable | ||
{ | ||
yield 'Fails RESTRICTION_LEVEL check because of character outside ASCII range' => [ | ||
'à', | ||
['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII], | ||
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, | ||
'This value contains characters that are not allowed by the current restriction-level.', | ||
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'], | ||
]; | ||
|
||
yield 'Fails RESTRICTION_LEVEL check because of mixed-script string' => [ | ||
|
@@ -81,8 +89,7 @@ public static function provideSuspiciousStrings(): iterable | |
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT, | ||
'locales' => ['en', 'zh_Hant_TW'], | ||
], | ||
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, | ||
'This value contains characters that are not allowed by the current restriction-level.', | ||
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'], | ||
]; | ||
|
||
yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_HIGH disallows Armenian script' => [ | ||
|
@@ -91,8 +98,7 @@ public static function provideSuspiciousStrings(): iterable | |
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH, | ||
'locales' => ['en', 'hy_AM'], | ||
], | ||
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, | ||
'This value contains characters that are not allowed by the current restriction-level.', | ||
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'], | ||
]; | ||
|
||
yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_MODERATE disallows Greek script' => [ | ||
|
@@ -101,8 +107,7 @@ public static function provideSuspiciousStrings(): iterable | |
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE, | ||
'locales' => ['en', 'el_GR'], | ||
], | ||
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, | ||
'This value contains characters that are not allowed by the current restriction-level.', | ||
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'], | ||
]; | ||
|
||
yield 'Fails RESTRICTION_LEVEL check because of characters missing from the configured locales’ scripts' => [ | ||
|
@@ -111,35 +116,43 @@ public static function provideSuspiciousStrings(): iterable | |
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL, | ||
'locales' => ['en'], | ||
], | ||
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, | ||
'This value contains characters that are not allowed by the current restriction-level.', | ||
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'], | ||
]; | ||
|
||
yield 'Fails INVISIBLE check because of duplicated non-spacing mark' => [ | ||
'à̀', | ||
[ | ||
'checks' => NoSuspiciousCharacters::CHECK_INVISIBLE, | ||
], | ||
NoSuspiciousCharacters::INVISIBLE_ERROR, | ||
'Using invisible characters is not allowed.', | ||
[NoSuspiciousCharacters::INVISIBLE_ERROR => 'Using invisible characters is not allowed.'], | ||
]; | ||
|
||
yield 'Fails MIXED_NUMBERS check because of different numbering systems' => [ | ||
'8৪', | ||
[ | ||
'checks' => NoSuspiciousCharacters::CHECK_MIXED_NUMBERS, | ||
], | ||
NoSuspiciousCharacters::MIXED_NUMBERS_ERROR, | ||
'Mixing numbers from different scripts is not allowed.', | ||
[NoSuspiciousCharacters::MIXED_NUMBERS_ERROR => 'Mixing numbers from different scripts is not allowed.'], | ||
]; | ||
|
||
yield 'Fails HIDDEN_OVERLAY check because of hidden combining character' => [ | ||
'i̇', | ||
[ | ||
'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY, | ||
], | ||
NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR, | ||
'Using hidden overlay characters is not allowed.', | ||
[NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR => 'Using hidden overlay characters is not allowed.'], | ||
]; | ||
|
||
yield 'Fails both HIDDEN_OVERLAY and RESTRICTION_LEVEL checks' => [ | ||
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. shouldn't this test be added in older branches as well (and so all the test refactoring) ? |
||
'i̇', | ||
[ | ||
'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY, | ||
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII, | ||
], | ||
[ | ||
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.', | ||
NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR => 'Using hidden overlay characters is not allowed.', | ||
], | ||
]; | ||
} | ||
|
||
|
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.
foreach ($errors as $code => $message)
would be a lot more readable IMO (you will need a special handling of the first item s next ones to create$violations
)