From 6a73d479191a0bbbd9ffa3886af6e6ff6e79fb86 Mon Sep 17 00:00:00 2001 From: MatTheCat Date: Mon, 26 Feb 2024 12:10:02 +0100 Subject: [PATCH] [Validator] Simplify `NoSuspiciousCharactersValidator` --- .../NoSuspiciousCharactersValidator.php | 12 ++--- .../NoSuspiciousCharactersValidatorTest.php | 53 ++++++++++++------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/Constraints/NoSuspiciousCharactersValidator.php b/Constraints/NoSuspiciousCharactersValidator.php index d55a765f5..d82a62d57 100644 --- a/Constraints/NoSuspiciousCharactersValidator.php +++ b/Constraints/NoSuspiciousCharactersValidator.php @@ -18,7 +18,7 @@ use Symfony\Component\Validator\Exception\UnexpectedValueException; /** - * @author Mathieu Lechat + * @author Mathieu Lechat */ class NoSuspiciousCharactersValidator extends ConstraintValidator { @@ -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; } diff --git a/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php b/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php index 6894d2f95..edacf3312 100644 --- a/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php +++ b/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php @@ -56,14 +56,23 @@ 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 @@ -71,8 +80,7 @@ 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,8 +116,7 @@ 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' => [ @@ -120,8 +124,7 @@ public static function provideSuspiciousStrings(): iterable [ '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' => [ @@ -129,8 +132,7 @@ public static function provideSuspiciousStrings(): iterable [ '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' => [ @@ -138,8 +140,19 @@ public static function provideSuspiciousStrings(): iterable [ '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' => [ + '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.', + ], ]; }