Skip to content

Commit 0653426

Browse files
committed
[SecurityBundle] Added UserPasswordValidator tests for the different Validation APIs
1 parent 0c13296 commit 0653426

File tree

4 files changed

+125
-57
lines changed

4 files changed

+125
-57
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1818
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
1919
use Symfony\Component\DependencyInjection\ContainerBuilder;
20-
use Symfony\Component\ExpressionLanguage\Expression;
2120

2221
abstract class CompleteConfigurationTest extends \PHPUnit_Framework_TestCase
2322
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Core\Tests\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Validation;
15+
16+
/**
17+
* @since 2.5.4
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*/
20+
class LegacyUserPasswordValidator2Dot4ApiTest extends UserPasswordValidatorTest
21+
{
22+
protected function getApiVersion()
23+
{
24+
return Validation::API_VERSION_2_4;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Core\Tests\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Validation;
15+
16+
/**
17+
* @since 2.5.4
18+
* @author Bernhard Schussek <bschussek@gmail.com>
19+
*/
20+
class LegacyUserPasswordValidatorLegacyApiTest extends UserPasswordValidatorTest
21+
{
22+
protected function getApiVersion()
23+
{
24+
return Validation::API_VERSION_2_5_BC;
25+
}
26+
}

src/Symfony/Component/Security/Core/Tests/Validator/Constraints/UserPasswordValidatorTest.php

+73-56
Original file line numberDiff line numberDiff line change
@@ -11,117 +11,134 @@
1111

1212
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
1313

14+
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
15+
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
16+
use Symfony\Component\Security\Core\SecurityContextInterface;
1417
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
1518
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
19+
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
20+
use Symfony\Component\Validator\Validation;
1621

17-
class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
22+
/**
23+
* @author Bernhard Schussek <bschussek@gmail.com>
24+
*/
25+
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
1826
{
19-
const PASSWORD_VALID = true;
20-
const PASSWORD_INVALID = false;
27+
const PASSWORD = 's3Cr3t';
2128

22-
protected $context;
29+
const SALT = '^S4lt$';
2330

24-
protected function setUp()
31+
/**
32+
* @var SecurityContextInterface
33+
*/
34+
protected $securityContext;
35+
36+
/**
37+
* @var PasswordEncoderInterface
38+
*/
39+
protected $encoder;
40+
41+
/**
42+
* @var EncoderFactoryInterface
43+
*/
44+
protected $encoderFactory;
45+
46+
protected function getApiVersion()
2547
{
26-
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
48+
return Validation::API_VERSION_2_5;
2749
}
2850

29-
protected function tearDown()
51+
protected function createValidator()
3052
{
31-
$this->context = null;
53+
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
3254
}
3355

34-
public function testPasswordIsValid()
56+
protected function setUp()
3557
{
3658
$user = $this->createUser();
37-
$securityContext = $this->createSecurityContext($user);
59+
$this->securityContext = $this->createSecurityContext($user);
60+
$this->encoder = $this->createPasswordEncoder();
61+
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
3862

39-
$encoder = $this->createPasswordEncoder(static::PASSWORD_VALID);
40-
$encoderFactory = $this->createEncoderFactory($encoder);
63+
parent::setUp();
64+
}
65+
66+
public function testPasswordIsValid()
67+
{
68+
$constraint = new UserPassword(array(
69+
'message' => 'myMessage',
70+
));
4171

42-
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
43-
$validator->initialize($this->context);
72+
$this->encoder->expects($this->once())
73+
->method('isPasswordValid')
74+
->with(static::PASSWORD, 'secret', static::SALT)
75+
->will($this->returnValue(true));
4476

45-
$this
46-
->context
47-
->expects($this->never())
48-
->method('addViolation')
49-
;
77+
$this->validator->validate('secret', $constraint);
5078

51-
$validator->validate('secret', new UserPassword());
79+
$this->assertNoViolation();
5280
}
5381

5482
public function testPasswordIsNotValid()
5583
{
56-
$user = $this->createUser();
57-
$securityContext = $this->createSecurityContext($user);
58-
59-
$encoder = $this->createPasswordEncoder(static::PASSWORD_INVALID);
60-
$encoderFactory = $this->createEncoderFactory($encoder);
84+
$constraint = new UserPassword(array(
85+
'message' => 'myMessage',
86+
));
6187

62-
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
63-
$validator->initialize($this->context);
88+
$this->encoder->expects($this->once())
89+
->method('isPasswordValid')
90+
->with(static::PASSWORD, 'secret', static::SALT)
91+
->will($this->returnValue(false));
6492

65-
$this
66-
->context
67-
->expects($this->once())
68-
->method('addViolation')
69-
;
93+
$this->validator->validate('secret', $constraint);
7094

71-
$validator->validate('secret', new UserPassword());
95+
$this->assertViolation('myMessage');
7296
}
7397

98+
/**
99+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
100+
*/
74101
public function testUserIsNotValid()
75102
{
76-
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
77-
78103
$user = $this->getMock('Foo\Bar\User');
79-
$encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
80-
$securityContext = $this->createSecurityContext($user);
81104

82-
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
83-
$validator->initialize($this->context);
84-
$validator->validate('secret', new UserPassword());
105+
$this->securityContext = $this->createSecurityContext($user);
106+
$this->validator = $this->createValidator();
107+
$this->validator->initialize($this->context);
108+
109+
$this->validator->validate('secret', new UserPassword());
85110
}
86111

87112
protected function createUser()
88113
{
89114
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
90115

91116
$mock
92-
->expects($this->once())
117+
->expects($this->any())
93118
->method('getPassword')
94-
->will($this->returnValue('s3Cr3t'))
119+
->will($this->returnValue(static::PASSWORD))
95120
;
96121

97122
$mock
98-
->expects($this->once())
123+
->expects($this->any())
99124
->method('getSalt')
100-
->will($this->returnValue('^S4lt$'))
125+
->will($this->returnValue(static::SALT))
101126
;
102127

103128
return $mock;
104129
}
105130

106131
protected function createPasswordEncoder($isPasswordValid = true)
107132
{
108-
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
109-
110-
$mock
111-
->expects($this->once())
112-
->method('isPasswordValid')
113-
->will($this->returnValue($isPasswordValid))
114-
;
115-
116-
return $mock;
133+
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
117134
}
118135

119136
protected function createEncoderFactory($encoder = null)
120137
{
121138
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
122139

123140
$mock
124-
->expects($this->once())
141+
->expects($this->any())
125142
->method('getEncoder')
126143
->will($this->returnValue($encoder))
127144
;
@@ -135,7 +152,7 @@ protected function createSecurityContext($user = null)
135152

136153
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
137154
$mock
138-
->expects($this->once())
155+
->expects($this->any())
139156
->method('getToken')
140157
->will($this->returnValue($token))
141158
;
@@ -147,7 +164,7 @@ protected function createAuthenticationToken($user = null)
147164
{
148165
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
149166
$mock
150-
->expects($this->once())
167+
->expects($this->any())
151168
->method('getUser')
152169
->will($this->returnValue($user))
153170
;

0 commit comments

Comments
 (0)