Skip to content

Commit 6a73d47

Browse files
committed
[Validator] Simplify NoSuspiciousCharactersValidator
1 parent 104bc36 commit 6a73d47

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

Constraints/NoSuspiciousCharactersValidator.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1919

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

9595
$checker->setChecks($checks);
9696

97-
if (!$checker->isSuspicious($value)) {
97+
if (!$checker->isSuspicious($value, $errorCode)) {
9898
return;
9999
}
100100

101101
foreach (self::CHECK_ERROR as $check => $error) {
102-
if (!($checks & $check)) {
103-
continue;
104-
}
105-
106-
$checker->setChecks($check);
107-
108-
if (!$checker->isSuspicious($value)) {
102+
if (!($errorCode & $check)) {
109103
continue;
110104
}
111105

Tests/Constraints/NoSuspiciousCharactersValidatorTest.php

+33-20
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,31 @@ public static function provideNonSuspiciousStrings(): iterable
5656
/**
5757
* @dataProvider provideSuspiciousStrings
5858
*/
59-
public function testSuspiciousStrings(string $string, array $options, string $errorCode, string $errorMessage)
59+
public function testSuspiciousStrings(string $string, array $options, array $errors)
6060
{
6161
$this->validator->validate($string, new NoSuspiciousCharacters($options));
6262

63-
$this->buildViolation($errorMessage)
64-
->setCode($errorCode)
63+
$violations = $this->buildViolation(reset($errors))
64+
->setCode(key($errors))
6565
->setParameter('{{ value }}', '"'.$string.'"')
66-
->assertRaised();
66+
;
67+
68+
while ($message = next($errors)) {
69+
$violations = $violations->buildNextViolation($message)
70+
->setCode(key($errors))
71+
->setParameter('{{ value }}', '"'.$string.'"')
72+
;
73+
}
74+
75+
$violations->assertRaised();
6776
}
6877

6978
public static function provideSuspiciousStrings(): iterable
7079
{
7180
yield 'Fails RESTRICTION_LEVEL check because of character outside ASCII range' => [
7281
'à',
7382
['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII],
74-
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
75-
'This value contains characters that are not allowed by the current restriction-level.',
83+
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'],
7684
];
7785

7886
yield 'Fails RESTRICTION_LEVEL check because of mixed-script string' => [
@@ -81,8 +89,7 @@ public static function provideSuspiciousStrings(): iterable
8189
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT,
8290
'locales' => ['en', 'zh_Hant_TW'],
8391
],
84-
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
85-
'This value contains characters that are not allowed by the current restriction-level.',
92+
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'],
8693
];
8794

8895
yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_HIGH disallows Armenian script' => [
@@ -91,8 +98,7 @@ public static function provideSuspiciousStrings(): iterable
9198
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH,
9299
'locales' => ['en', 'hy_AM'],
93100
],
94-
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
95-
'This value contains characters that are not allowed by the current restriction-level.',
101+
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'],
96102
];
97103

98104
yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_MODERATE disallows Greek script' => [
@@ -101,8 +107,7 @@ public static function provideSuspiciousStrings(): iterable
101107
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE,
102108
'locales' => ['en', 'el_GR'],
103109
],
104-
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
105-
'This value contains characters that are not allowed by the current restriction-level.',
110+
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'],
106111
];
107112

108113
yield 'Fails RESTRICTION_LEVEL check because of characters missing from the configured locales’ scripts' => [
@@ -111,35 +116,43 @@ public static function provideSuspiciousStrings(): iterable
111116
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL,
112117
'locales' => ['en'],
113118
],
114-
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
115-
'This value contains characters that are not allowed by the current restriction-level.',
119+
[NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.'],
116120
];
117121

118122
yield 'Fails INVISIBLE check because of duplicated non-spacing mark' => [
119123
'à̀',
120124
[
121125
'checks' => NoSuspiciousCharacters::CHECK_INVISIBLE,
122126
],
123-
NoSuspiciousCharacters::INVISIBLE_ERROR,
124-
'Using invisible characters is not allowed.',
127+
[NoSuspiciousCharacters::INVISIBLE_ERROR => 'Using invisible characters is not allowed.'],
125128
];
126129

127130
yield 'Fails MIXED_NUMBERS check because of different numbering systems' => [
128131
'8৪',
129132
[
130133
'checks' => NoSuspiciousCharacters::CHECK_MIXED_NUMBERS,
131134
],
132-
NoSuspiciousCharacters::MIXED_NUMBERS_ERROR,
133-
'Mixing numbers from different scripts is not allowed.',
135+
[NoSuspiciousCharacters::MIXED_NUMBERS_ERROR => 'Mixing numbers from different scripts is not allowed.'],
134136
];
135137

136138
yield 'Fails HIDDEN_OVERLAY check because of hidden combining character' => [
137139
'',
138140
[
139141
'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY,
140142
],
141-
NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR,
142-
'Using hidden overlay characters is not allowed.',
143+
[NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR => 'Using hidden overlay characters is not allowed.'],
144+
];
145+
146+
yield 'Fails both HIDDEN_OVERLAY and RESTRICTION_LEVEL checks' => [
147+
'',
148+
[
149+
'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY,
150+
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII,
151+
],
152+
[
153+
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR => 'This value contains characters that are not allowed by the current restriction-level.',
154+
NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR => 'Using hidden overlay characters is not allowed.',
155+
],
143156
];
144157
}
145158

0 commit comments

Comments
 (0)