8000 minor #13061 [Security] Fixed tests (saro0h) · symfony/symfony@8dc7478 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dc7478

Browse files
committed
minor #13061 [Security] Fixed tests (saro0h)
This PR was merged into the 2.5 branch. Discussion ---------- [Security] Fixed tests | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ Fixes the tests failing (https://travis-ci.org/symfony/symfony/jobs/44732074#L896) This class have been removed in 2.3 (#13033) but is actually used since 2.4 (when the split of the Security component has been made). Commits ------- 5aa3ef4 [Security] Fixed tests
2 parents 51aa13a + 5aa3ef4 commit 8dc7478

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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\Security\Core\Encoder\EncoderFactoryInterface;
15+
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
16+
use Symfony\Component\Security\Core\SecurityContextInterface;
17+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
18+
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
19+
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
20+
21+
/**
22+
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*/
24+
abstract class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
25+
{
26+
const PASSWORD = 's3Cr3t';
27+
28+
const SALT = '^S4lt$';
29+
30+
/**
31+
* @var SecurityContextInterface
32+
*/
33+
protected $securityContext;
34+
35+
/**
36+
* @var PasswordEncoderInterface
37+
*/
38+
protected $encoder;
39+
40+
/**
41+
* @var EncoderFactoryInterface
42+
*/
43+
protected $encoderFactory;
44+
45+
protected function createValidator()
46+
{
47+
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
48+
}
49+
50+
protected function setUp()
51+
{
52+
$user = $this->createUser();
53+
$this->securityContext = $this->createSecurityContext($user);
54+
$this->encoder = $this->createPasswordEncoder();
55+
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
56+
57+
parent::setUp();
58+
}
59+
60+
public function testPasswordIsValid()
61+
{
62+
$constraint = new UserPassword(array(
63+
'message' => 'myMessage',
64+
));
65+
66+
$this->encoder->expects($this->once())
67+
->method('isPasswordValid')
68+
->with(static::PASSWORD, 'secret', static::SALT)
69+
->will($this->returnValue(true));
70+
71+
$this->validator->validate('secret', $constraint);
72+
73+
$this->assertNoViolation();
74+
}
75+
76+
public function testPasswordIsNotValid()
77+
{
78+
$constraint = new UserPassword(array(
79+
'message' => 'myMessage',
80+
));
81+
82+
$this->encoder->expects($this->once())
83+
->method('isPasswordValid')
84+
->with(static::PASSWORD, 'secret', static::SALT)
85+
->will($this->returnValue(false));
86+
87+
$this->validator->validate('secret', $constraint);
88+
89+
$this->assertViolation('myMessage');
90+
}
91+
92+
/**
93+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
94+
*/
95+
public function testUserIsNotValid()
96+
{
97+
$user = $this->getMock('Foo\Bar\User');
98+
99+
$this->securityContext = $this->createSecurityContext($user);
100+
$this->validator = $this->createValidator();
101+
$this->validator->initialize($this->context);
102+
103+
$this->validator->validate('secret', new UserPassword());
104+
}
105+
106+
protected function createUser()
107+
{
108+
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
109+
110+
$mock
111+
->expects($this->any())
112+
->method('getPassword')
113+
->will($this->returnValue(static::PASSWORD))
114+
;
115+
116+
$mock
117+
->expects($this->any())
118+
->method('getSalt')
119+
->will($this->returnValue(static::SALT))
120+
;
121+
122+
return $mock;
123+
}
124+
125+
protected function createPasswordEncoder($isPasswordValid = true)
126+
{
127+
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
128+
}
129+
130+
protected function createEncoderFactory($encoder = null)
131+
{
132+
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
133+
134+
$mock
135+
->expects($this->any())
136+
->method('getEncoder')
137+
->will($this->returnValue($encoder))
138+
;
139+
140+
return $mock;
141+
}
142+
143+
protected function createSecurityContext($user = null)
144+
{
145+
$token = $this->createAuthenticationToken($user);
146+
147+
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
148+
$mock
149+
->expects($this->any())
150+
->method('getToken')
151+
->will($this->returnValue($token))
152+
;
153+
154+
return $mock;
155+
}
156+
157+
protected function createAuthenticationToken($user = null)
158+
{
159+
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
160+
$mock
161+
->expects($this->any())
162+
->method('getUser')
163+
->will($this->returnValue($user))
164+
;
165+
166+
return $mock;
167+
}
168+
}

0 commit comments

Comments
 (0)
0