Skip to content

[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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Mathieu Lechat <mathieu.lechat@les-tilleuls.coop>
* @author Mathieu Lechat <math.lechat@gmail.com>
*/
class NoSuspiciousCharactersValidator extends ConstraintValidator
{
Expand Down Expand Up @@ -94,18 +94,12 @@ public function validate(mixed $value, Constraint $constraint): void

$checker->setChecks($checks);

if (!$checker->isSuspicious($value)) {
if (!$checker->isSuspicious($value, $errorCode)) {
return;
}

foreach (self::CHECK_ERROR as $check => $error) {
if (!($checks & $check)) {
continue;
}

$checker->setChecks($check);

if (!$checker->isSuspicious($value)) {
if (!($errorCode & $check)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

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)

$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' => [
Expand All @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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' => [
Copy link
Member

Choose a reason for hiding this comment

The 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.',
],
];
}

Expand Down