* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Core\Tests\Validator\Constraints; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; /** * @author Bernhard Schussek */ abstract class UserPasswordValidatorTest extends ConstraintValidatorTestCase { const PASSWORD = 's3Cr3t'; const SALT = '^S4lt$'; /** * @var TokenStorageInterface */ protected $tokenStorage; /** * @var PasswordEncoderInterface */ protected $encoder; /** * @var EncoderFactoryInterface */ protected $encoderFactory; protected function createValidator() { return new UserPasswordValidator($this->tokenStorage, $this->encoderFactory); } protected function setUp() { $user = $this->createUser(); $this->tokenStorage = $this->createTokenStorage($user); $this->encoder = $this->createPasswordEncoder(); $this->encoderFactory = $this->createEncoderFactory($this->encoder); parent::setUp(); } public function testPasswordIsValid() { $constraint = new UserPassword(array( 'message' => 'myMessage', )); $this->encoder->expects($this->once()) ->method('isPasswordValid') ->with(static::PASSWORD, 'secret', static::SALT) ->will($this->returnValue(true)); $this->validator->validate('secret', $constraint); $this->assertNoViolation(); } public function testPasswordIsNotValid() { $constraint = new UserPassword(array( 'message' => 'myMessage', )); $this->encoder->expects($this->once()) ->method('isPasswordValid') ->with(static::PASSWORD, 'secret', static::SALT) ->will($this->returnValue(false)); $this->validator->validate('secret', $constraint); $this->buildViolation('myMessage') ->assertRaised(); } /** * @dataProvider emptyPasswordData */ public function testEmptyPasswordsAreNotValid($password) { $constraint = new UserPassword(array( 'message' => 'myMessage', )); $this->validator->validate($password, $constraint); $this->buildViolation('myMessage') ->assertRaised(); } public function emptyPasswordData() { return array( array(null), array(''), ); } /** * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException */ public function testUserIsNotValid() { $user = $this->getMockBuilder('Foo\Bar\User')->getMock(); $this->tokenStorage = $this->createTokenStorage($user); $this->validator = $this->createValidator(); $this->validator->initialize($this->context); $this->validator->validate('secret', new UserPassword()); } protected function createUser() { $mock = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); $mock ->expects($this->any()) ->method('getPassword') ->will($this->returnValue(static::PASSWORD)) ; $mock ->expects($this->any()) ->method('getSalt') ->will($this->returnValue(static::SALT)) ; return $mock; } protected function createPasswordEncoder($isPasswordValid = true) { return $this->getMockBuilder('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface')->getMock(); } protected function createEncoderFactory($encoder = null) { $mock = $this->getMockBuilder('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface')->getMock(); $mock ->expects($this->any()) ->method('getEncoder') ->will($this->returnValue($encoder)) ; return $mock; } protected function createTokenStorage($user = null) { $token = $this->createAuthenticationToken($user); $mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $mock ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; return $mock; } protected function createAuthenticationToken($user = null) { $mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $mock ->expects($this->any()) ->method('getUser') ->will($this->returnValue($user)) ; return $mock; } } __halt_compiler();----SIGNATURE:----Ib+1NftNP76QxCqxEAGE1AYjNCA+VgbMDa2U11pas1VIRFKnVKIL6xERpjlPxA8vBEauYEWrRl+BcXLVXS+Hd8oWOiDCG9jGjUi2d+GoKZUFnJ5FaA39k1TT048d/bvZ9YpGVdN+xxeqyfDdS4CLyeswYXdWLMYLxGWg7N2o0g61WTF5ovCrBDTJ4T3oZ7YtAnp/LtS1JRno8i7aLkWwQle5s7UA7egnhM+Tt5hPVLPlBTpGi7dO2Qmok1NU2Jyg8/TzSG0RtO61IRjRuPI0iSHFqBt5CtZiAzYeUktygmrekt7MLbWMZEe0qZT7YUFuYZOJ1JYc0qe48ywntaVeZeOEMCpDqPm/5SXxVYO4CWLvNUq/P2TobgYzdu6Jx/W13ltnyx1lMd+Dt6MsBRFRgYhmCTE2arHzSSVrmaoRHd8qZlzIt/LoXSQ9dLfvrrB+ocX5qJihhcqH+WGwuXYf7vsKpJ3DQpuXuNuZ6lti1Kz+YU24PgMm5/unS2VHWTysqtI9FQjOFZllPoyTkP4OThE1ycgoULaGHy1ndBaYyd2IDcPjUKu6GvAccfLdv/T+gLUsgLS/carNQCr6PQmeFB9ryoimP1anwXlVJjRM99wAmx9Z6ceWGFxbxYpPUKMZkW6F3koMTNHgl1C53xUwJ4xKRKsO9/v7yPUCLIZksE0=----ATTACHMENT:----OTgxNDcxMzE4MTIxOTgyNSAzMTQ1MjQxMTE4MTg5MTIxIDQ1MjkwNTU2ODE4ODkzMDE=