10000 [Security] [Tests] added unit tests for the UserPasswordValidator cla… · 77web/symfony@a433ba2 · GitHub
[go: up one dir, main page]

Skip to content

Commit a433ba2

Browse files
author
Hugo Hamon
committed
[Security] [Tests] added unit tests for the UserPasswordValidator class and made the validator service for the UserPassword constraint configurable.
1 parent 98752f6 commit a433ba2

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

src/Symfony/Component/Security/Core/Validator/Constraint/UserPassword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
class UserPassword extends Constraint
2020
{
2121
public $message = 'This value should be the user current password.';
22+
public $service = 'security.validator.user_password';
2223

2324
public function validatedBy()
2425
{
25-
return 'security.validator.user_password';
26+
return $this->service;
2627
}
2728
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Security\Tests\Core\Validator\Constraint;
13+
14+
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
15+
use Symfony\Component\Security\Core\Validator\Constraint\UserPasswordValidator;
16+
17+
class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
18+
{
19+
const PASSWORD_VALID = true;
20+
const PASSWORD_INVALID = false;
21+
22+
protected $context;
23+
24+
protected function setUp()
25+
{
26+
if (false === class_exists('Symfony\Component\Validator\Validator')) {
27+
$this->markTestSkipped('The Validator component is required for this test.');
28+
}
29+
30+
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
31+
}
32+
33+
protected function tearDown()
34+
{
35+
$this->context = null;
36+
}
37+
38+
public function testPasswordIsValid()
39+
{
40+
$user = $this->createUser();
41+
$securityContext = $this->createSecurityContext($user);
42+
43+
$encoder = $this->createPasswordEncoder(static::PASSWORD_VALID);
44+
$encoderFactory = $this->createEncoderFactory($encoder);
45+
46+
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
47+
$validator->initialize($this->context);
48+
49+
$this
50+
->context
51+
->expects($this->never())
52+
->method('addViolation')
53+
;
54+
55+
$validator->validate('secret', new UserPassword());
56+
}
57+
58+
public function testPasswordIsNotValid()
59+
{
60+
$user = $this->createUser();
61+
$securityContext = $this->createSecurityContext($user);
62+
63+
$encoder = $this->createPasswordEncoder(static::PASSWORD_INVALID);
64+
$encoderFactory = $this->createEncoderFactory($encoder);
65+
66+
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
67+
$validator->initialize($this->context);
68+
69+
$this
70+
->context
71+
->expects($this->once())
72+
->method('addViolation')
73+
;
74+
75+
$validator->validate('secret', new UserPassword());
76+
}
77+
78+
public function testUserIsNotValid()
79+
{
80+
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
81+
82+
$user = $this->getMock('Foo\Bar\User');
83+
$encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
84+
$securityContext = $this->createSecurityContext($user);
85+
86+
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
87+
$validator->initialize($this->context);
88+
$validator->validate('secret', new UserPassword());
89+
}
90+
91+
protected function createUser()
92+
{
93+
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
94+
95+
$mock
96+
->expects($this->once())
97+
->method('getPassword')
98+
->will($this->returnValue('s3Cr3t'))
99+
;
100+
101+
$mock
102+
->expects($this->once())
103+
->method('getSalt')
104+
->will($this->returnValue('^S4lt$'))
105+
;
106+
107+
return $mock;
108+
}
109+
110+
protected function createPasswordEncoder($isPasswordValid = true)
111+
{
112+
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
113+
114+
$mock
115+
->expects($this->once())
116+
->method('isPasswordValid')
117+
->will($this->returnValue($isPasswordValid))
118+
;
119+
120+
return $mock;
121+
}
122+
123+
protected function createEncoderFactory($encoder = null)
124+
{
125+
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
126+
127+
$mock
128+
->expects($this->once())
129+
->method('getEncoder')
130+
->will($this->returnValue($encoder))
131+
;
132+
133+
return $mock;
134+
}
135+
136+
protected function createSecurityContext($user = null)
137+
{
138+
$token = $this->createAuthenticationToken($user);
139+
140+
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
141+
$mock
142+
->expects($this->once())
143+
->method('getToken')
144+
->will($this->returnValue($token))
145+
;
146+
147+
return $mock;
148+
}
149+
150+
protected function createAuthenticationToken($user = null)
151+
{
152+
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
153+
$mock
154+
->expects($this->once())
155+
->method('getUser')
156+
->will($this->returnValue($user))
157+
;
158+
159+
return $mock;
160+
}
161+
}

0 commit comments

Comments
 (0)
0