|
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\InMemoryBaseUserRepository; |
|
10
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection; |
|
11
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser; |
|
12
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
|
17
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
19
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactory; |
|
20
|
|
|
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder; |
|
21
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
|
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
23
|
|
|
|
|
24
|
|
|
class FormAuthenticatorTest extends PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
private $formAuthenticator; |
|
27
|
|
|
private $router; |
|
28
|
|
|
private $flashBag; |
|
29
|
|
|
private $translator; |
|
30
|
|
|
|
|
31
|
|
|
public function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->router = $this->getMockBuilder(RouterInterface::class)->getMock(); |
|
34
|
|
|
$this->flashBag = $this->getMockBuilder(FlashBagInterface::class)->getMock(); |
|
35
|
|
|
$this->translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); |
|
36
|
|
|
$encoders[ 'SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser' ] = [ |
|
|
|
|
|
|
37
|
|
|
'class' => 'Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder', |
|
38
|
|
|
'arguments' => [12], |
|
39
|
|
|
]; |
|
40
|
|
|
$encoder = new UserPasswordEncoder(new EncoderFactory($encoders)); |
|
41
|
|
|
$redirectRoutes = []; |
|
42
|
|
|
$this->formAuthenticator = new FormAuthenticator( |
|
43
|
|
|
$encoder, |
|
44
|
|
|
$this->router, |
|
45
|
|
|
$this->flashBag, |
|
46
|
|
|
$this->translator, |
|
47
|
|
|
$redirectRoutes |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testFormAuthenticatorGetUser(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$userRepositoryCollection = new BaseUserRepositoryCollection( |
|
54
|
|
|
[ |
|
55
|
|
|
new InMemoryBaseUserRepository( |
|
56
|
|
|
new EncoderFactory([BaseUser::class => new PlaintextPasswordEncoder()]) |
|
57
|
|
|
), |
|
58
|
|
|
] |
|
59
|
|
|
); |
|
60
|
|
|
$provider = new ObjectUserProvider($userRepositoryCollection); |
|
61
|
|
|
$user = $this->formAuthenticator->getUser($this->getCredentials(), $provider); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals($this->getUser()->getId(), $user->getId()); |
|
64
|
|
|
$this->assertEquals($this->getUser()->getUsername(), $user->getUsername()); |
|
65
|
|
|
$this->assertEquals($this->getUser()->getEmail(), $user->getEmail()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testCheckCredentials(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$user = $this->getUser(); |
|
71
|
|
|
$user->encodePassword(new PlaintextPasswordEncoder()); |
|
72
|
|
|
$this->assertTrue( |
|
73
|
|
|
$this->formAuthenticator->checkCredentials($this->getCredentials(), $user) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @expectedException Symfony\Component\Security\Core\Exception\BadCredentialsException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testBadCredentialsException(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\BadCredentialsException'); |
|
83
|
|
|
$this->formAuthenticator->checkCredentials( |
|
84
|
|
|
$this->getCredentials('wouter', 'wrongPassword'), |
|
85
|
|
|
$this->getUser() |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testOnAuthenticationSuccess(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$request = new Request(); |
|
92
|
|
|
$providerKey = 'main'; |
|
93
|
|
|
$token = new UsernamePasswordToken('wouter', null, $providerKey, ['ROLE_USER']); |
|
94
|
|
|
|
|
95
|
|
|
$session = new Session(new MockArraySessionStorage()); |
|
96
|
|
|
$session->set('_security_'.$providerKey, serialize($token)); |
|
97
|
|
|
$session->set('_security.'.$providerKey.'.target_path', '/randomURL'); |
|
98
|
|
|
$session->save(); |
|
99
|
|
|
$request->setSession($session); |
|
100
|
|
|
|
|
101
|
|
|
$this->formAuthenticator->onAuthenticationSuccess($request, $token, $providerKey); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function getCredentials($username = 'wouter', $password = 'test'): FormCredentials |
|
105
|
|
|
{ |
|
106
|
|
|
$mock = $this->getMockBuilder(FormCredentials::class)->disableOriginalConstructor()->getMock(); |
|
107
|
|
|
$mock->method('getUserName')->willReturn($username); |
|
108
|
|
|
$mock->method('getPlainPassword')->willReturn($password); |
|
109
|
|
|
|
|
110
|
|
|
return $mock; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function getUser(): User |
|
114
|
|
|
{ |
|
115
|
|
|
$inMemoryBaseUserRepository = new InMemoryBaseUserRepository( |
|
116
|
|
|
new EncoderFactory([BaseUser::class => new PlaintextPasswordEncoder()]) |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
return $inMemoryBaseUserRepository->find(1); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.