|
| 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\PasswordStrength; |
| 15 | +use Symfony\Component\Validator\Constraints\PasswordStrengthValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +class PasswordStrengthValidatorTest extends ConstraintValidatorTestCase |
| 19 | +{ |
| 20 | + protected function createValidator(): PasswordStrengthValidator |
| 21 | + { |
| 22 | + return new PasswordStrengthValidator(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @dataProvider getValidValues |
| 27 | + */ |
| 28 | + public function testValidValues(string $value) |
| 29 | + { |
| 30 | + $this->validator->validate($value, new PasswordStrength()); |
| 31 | + |
| 32 | + $this->assertNoViolation(); |
| 33 | + } |
| 34 | + |
| 35 | + public static function getValidValues(): iterable |
| 36 | + { |
| 37 | + yield ['This 1s a very g00d Pa55word! ;-)']; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @dataProvider provideInvalidConstraints |
| 42 | + */ |
| 43 | + public function testThePasswordIsWeak(PasswordStrength $constraint, string $password, string $expectedMessage, string $expectedCode, array $parameters = []) |
| 44 | + { |
| 45 | + $this->validator->validate($password, $constraint); |
| 46 | + |
| 47 | + $this->buildViolation($expectedMessage) |
| 48 | + ->setCode($expectedCode) |
| 49 | + ->setParameters($parameters) |
| 50 | + ->assertRaised(); |
| 51 | + } |
| 52 | + |
| 53 | + public static function provideInvalidConstraints(): iterable |
| 54 | + { |
| 55 | + yield [ |
| 56 | + new PasswordStrength(), |
| 57 | + 'password', |
| 58 | + 'The password strength is too low. Please use a stronger password.', |
| 59 | + PasswordStrength::PASSWORD_STRENGTH_ERROR, |
| 60 | + ]; |
| 61 | + yield [ |
| 62 | + new PasswordStrength([ |
| 63 | + 'minScore' => 4, |
| 64 | + ]), |
| 65 | + 'Good password?', |
| 66 | + 'The password strength is too low. Please use a stronger password.', |
| 67 | + PasswordStrength::PASSWORD_STRENGTH_ERROR, |
| 68 | + ]; |
| 69 | + yield [ |
| 70 | + new PasswordStrength([ |
| 71 | + 'restrictedData' => ['symfony'], |
| 72 | + ]), |
| 73 | + 'SyMfONY-framework-john', |
| 74 | + 'The password contains the following restricted data: {{ wordList }}.', |
| 75 | + PasswordStrength::RESTRICTED_USER_INPUT_ERROR, |
| 76 | + [ |
| 77 | + '{{ wordList }}' => 'SyMfONY', |
| 78 | + ], |
| 79 | + ]; |
| 80 | + } |
| 81 | +} |
0 commit comments