|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\NoSuspiciousCharacters; |
| 15 | +use Symfony\Component\Validator\Constraints\NoSuspiciousCharactersValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @requires extension intl |
| 20 | + * |
| 21 | + * @extends ConstraintValidatorTestCase<NoSuspiciousCharactersValidator> |
| 22 | + */ |
| 23 | +class NoSuspiciousCharactersValidatorTest extends ConstraintValidatorTestCase |
| 24 | +{ |
| 25 | + protected function createValidator(): NoSuspiciousCharactersValidator |
| 26 | + { |
| 27 | + return new NoSuspiciousCharactersValidator(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @dataProvider provideNonSuspiciousStrings |
| 32 | + */ |
| 33 | + public function testNonSuspiciousStrings(string $string, array $options = []) |
| 34 | + { |
| 35 | + $this->validator->validate($string, new NoSuspiciousCharacters($options)); |
| 36 | + |
| 37 | + $this->assertNoViolation(); |
| 38 | + } |
| 39 | + |
| 40 | + public static function provideNonSuspiciousStrings(): iterable |
| 41 | + { |
| 42 | + yield 'Characters from Common script can only fail RESTRICTION_LEVEL_ASCII' => [ |
| 43 | + 'I ❤️ Unicode', |
| 44 | + ['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT], |
| 45 | + ]; |
| 46 | + |
| 47 | + yield 'RESTRICTION_LEVEL_MINIMAL cannot fail without configured locales' => [ |
| 48 | + 'àㄚԱπ৪', |
| 49 | + [ |
| 50 | + 'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL, |
| 51 | + 'locales' => [], |
| 52 | + ], |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider provideSuspiciousStrings |
| 58 | + */ |
| 59 | + public function testSuspiciousStrings(string $string, array $options, string $errorCode, string $errorMessage) |
| 60 | + { |
| 61 | + $this->validator->validate($string, new NoSuspiciousCharacters($options)); |
| 62 | + |
| 63 | + $this->buildViolation($errorMessage) |
| 64 | + ->setCode($errorCode) |
| 65 | + ->setParameter('{{ value }}', '"'.$string.'"') |
| 66 | + ->assertRaised(); |
| 67 | + } |
| 68 | + |
| 69 | + public static function provideSuspiciousStrings(): iterable |
| 70 | + { |
| 71 | + yield 'Fails RESTRICTION_LEVEL check because of character outside ASCII range' => [ |
| 72 | + 'à', |
| 73 | + ['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII], |
| 74 | + NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, |
| 75 | + 'This value contains characters that are not allowed by the current restriction-level.', |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'Fails RESTRICTION_LEVEL check because of mixed-script string' => [ |
| 79 | + 'àㄚ', |
| 80 | + [ |
| 81 | + 'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT, |
| 82 | + 'locales' => ['en', 'zh_Hant_TW'], |
| 83 | + ], |
| 84 | + NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, |
| 85 | + 'This value contains characters that are not allowed by the current restriction-level.', |
| 86 | + ]; |
| 87 | + |
| 88 | + yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_HIGH disallows Armenian script' => [ |
| 89 | + 'àԱ', |
| 90 | + [ |
| 91 | + 'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH, |
| 92 | + 'locales' => ['en', 'hy_AM'], |
| 93 | + ], |
| 94 | + NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, |
| 95 | + 'This value contains characters that are not allowed by the current restriction-level.', |
| 96 | + ]; |
| 97 | + |
| 98 | + yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_MODERATE disallows Greek script' => [ |
| 99 | + 'àπ', |
| 100 | + [ |
| 101 | + 'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE, |
| 102 | + 'locales' => ['en', 'el_GR'], |
| 103 | + ], |
| 104 | + NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, |
| 105 | + 'This value contains characters that are not allowed by the current restriction-level.', |
| 106 | + ]; |
| 107 | + |
| 108 | + yield 'Fails RESTRICTION_LEVEL check because of characters missing from the configured locales’ scripts' => [ |
| 109 | + 'àπ', |
| 110 | + [ |
| 111 | + 'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL, |
| 112 | + 'locales' => ['en'], |
| 113 | + ], |
| 114 | + NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR, |
| 115 | + 'This value contains characters that are not allowed by the current restriction-level.', |
| 116 | + ]; |
| 117 | + |
| 118 | + yield 'Fails INVISIBLE check because of duplicated non-spacing mark' => [ |
| 119 | + 'à̀', |
| 120 | + [ |
| 121 | + 'checks' => NoSuspiciousCharacters::CHECK_INVISIBLE, |
| 122 | + ], |
| 123 | + NoSuspiciousCharacters::INVISIBLE_ERROR, |
| 124 | + 'Using invisible characters is not allowed.', |
| 125 | + ]; |
| 126 | + |
| 127 | + yield 'Fails MIXED_NUMBERS check because of different numbering systems' => [ |
| 128 | + '8৪', |
| 129 | + [ |
| 130 | + 'checks' => NoSuspiciousCharacters::CHECK_MIXED_NUMBERS, |
| 131 | + ], |
| 132 | + NoSuspiciousCharacters::MIXED_NUMBERS_ERROR, |
| 133 | + 'Mixing numbers from different scripts is not allowed.', |
| 134 | + ]; |
| 135 | + |
| 136 | + yield 'Fails HIDDEN_OVERLAY check because of hidden combining character' => [ |
| 137 | + 'i̇', |
| 138 | + [ |
| 139 | + 'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY, |
| 140 | + ], |
| 141 | + NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR, |
| 142 | + 'Using hidden overlay characters is not allowed.', |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + public function testConstants() |
| 147 | + { |
| 148 | + $this->assertSame(\Spoofchecker::INVISIBLE, NoSuspiciousCharacters::CHECK_INVISIBLE); |
| 149 | + $this->assertSame(\Spoofchecker::ASCII, NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII); |
| 150 | + $this->assertSame(\Spoofchecker::SINGLE_SCRIPT_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT); |
| 151 | + $this->assertSame(\Spoofchecker::HIGHLY_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH); |
| 152 | + $this->assertSame(\Spoofchecker::MODERATELY_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE); |
| 153 | + $this->assertSame(\Spoofchecker::MINIMALLY_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL); |
| 154 | + $this->assertSame(\Spoofchecker::UNRESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_NONE); |
| 155 | + } |
| 156 | +} |
0 commit comments