Skip to content

Commit

Permalink
[Validator] Simplify NoSuspiciousCharactersValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
MatTheCat committed Feb 27, 2024
1 parent 104bc36 commit 6a73d47
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
12 changes: 3 additions & 9 deletions Constraints/NoSuspiciousCharactersValidator.php
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
53 changes: 33 additions & 20 deletions Tests/Constraints/NoSuspiciousCharactersValidatorTest.php
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)) {
$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' => [
'',
[
'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' => [
'',
[
'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

0 comments on commit 6a73d47

Please sign in to comment.