|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Tests\Security; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\FormAuthenticator; |
|
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\FormCredentials; |
|
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\ObjectUserProvider; |
|
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\InMemoryUserRepository; |
|
10
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\User; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
|
15
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
17
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactory; |
|
18
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
|
19
|
|
|
|
|
20
|
|
|
class FormAuthenticatorTest extends PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
private $formAuthenticator; |
|
23
|
|
|
private $router; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->router = $this->getMock(RouterInterface::class); |
|
|
|
|
|
|
28
|
|
|
$encoders['SumoCoders\FrameworkMultiUserBundle\User\User'] = [ |
|
|
|
|
|
|
29
|
|
|
'class' => 'Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder', |
|
30
|
|
|
'arguments' => [12], |
|
31
|
|
|
]; |
|
32
|
|
|
$encoder = new UserPasswordEncoder(new EncoderFactory($encoders)); |
|
33
|
|
|
$redirectRoutes = []; |
|
34
|
|
|
$this->formAuthenticator = new FormAuthenticator($encoder, $this->router, $redirectRoutes); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testFormAuthenticatorGetUser() |
|
38
|
|
|
{ |
|
39
|
|
|
$provider = new ObjectUserProvider(new InMemoryUserRepository()); |
|
40
|
|
|
$user = $this->formAuthenticator->getUser($this->getCredentials(), $provider); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals($this->getUser(), $user); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testCheckCredentials() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->assertTrue( |
|
48
|
|
|
$this->formAuthenticator->checkCredentials($this->getCredentials(), $this->getUser()) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @expectedException Symfony\Component\Security\Core\Exception\BadCredentialsException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testBadCredentialsException() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->setExpectedException('Symfony\Component\Security\Core\Exception\BadCredentialsException'); |
|
|
|
|
|
|
58
|
|
|
$this->formAuthenticator->checkCredentials( |
|
59
|
|
|
$this->getCredentials('wouter', 'wrongPassword'), |
|
60
|
|
|
$this->getUser() |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testOnAuthenticationSuccess() |
|
65
|
|
|
{ |
|
66
|
|
|
$request = new Request(); |
|
67
|
|
|
$providerKey = 'main'; |
|
68
|
|
|
$token = new UsernamePasswordToken('wouter', null, $providerKey, ['ROLE_USER']); |
|
69
|
|
|
|
|
70
|
|
|
$session = new Session(new MockArraySessionStorage()); |
|
71
|
|
|
$session->set('_security_'.$providerKey, serialize($token)); |
|
72
|
|
|
$session->set('_security.'.$providerKey.'.target_path', '/randomURL'); |
|
73
|
|
|
$session->save(); |
|
74
|
|
|
$request->setSession($session); |
|
75
|
|
|
|
|
76
|
|
|
$this->formAuthenticator->onAuthenticationSuccess($request, $token, $providerKey); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function getCredentials($username = 'wouter', $password = 'test') |
|
80
|
|
|
{ |
|
81
|
|
|
$mock = $this->getMock(FormCredentials::class, [], [$username, $password]); |
|
|
|
|
|
|
82
|
|
|
$mock->method('getUserName')->willReturn($username); |
|
83
|
|
|
$mock->method('getPlainPassword')->willReturn($password); |
|
84
|
|
|
|
|
85
|
|
|
return $mock; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function getUser($username = 'wouter', $password = 'test', $displayName = 'Wouter Sioen') |
|
89
|
|
|
{ |
|
90
|
|
|
return new User($username, $password, $displayName); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.