1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Gvera\Helpers\validation\ValidationService; |
4
|
|
|
use Gvera\Models\User; |
5
|
|
|
use Gvera\Models\UserRole; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Gvera\Helpers\session\Session; |
8
|
|
|
use Gvera\Helpers\entities\GvEntityManager; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class UserServiceTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $userService; |
15
|
|
|
/** |
16
|
|
|
* @test |
17
|
|
|
*/ |
18
|
|
|
private $user; |
19
|
|
|
/** |
20
|
|
|
* @var EntityRepository|\PHPUnit\Framework\MockObject\MockObject |
21
|
|
|
*/ |
22
|
|
|
private $repo; |
23
|
|
|
/** |
24
|
|
|
* @var GvEntityManager|\PHPUnit\Framework\MockObject\MockObject |
25
|
|
|
*/ |
26
|
|
|
private $gvEntityManager; |
27
|
|
|
|
28
|
|
|
public function validateEmail() |
29
|
|
|
{ |
30
|
|
|
$invalidEmail = 'test'; |
31
|
|
|
$this->assertFalse($this->userService->validateEmail($invalidEmail)); |
32
|
|
|
$validEmail = '[email protected]'; |
33
|
|
|
$this->assertTrue($this->userService->validateEmail($validEmail)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setUp():void |
37
|
|
|
{ |
38
|
|
|
$this->repo = $this->createMock(EntityRepository::class); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->gvEntityManager = $this->createMock(GvEntityManager::class); |
42
|
|
|
$gvEntityManager = $this->gvEntityManager; |
43
|
|
|
$gvEntityManager->expects($this->any()) |
44
|
|
|
->method('getRepository') |
45
|
|
|
->willReturn($this->repo); |
46
|
|
|
|
47
|
|
|
$session = $this->createMock(Session::class); |
48
|
|
|
$session->expects($this->any()) |
49
|
|
|
->method('set') |
50
|
|
|
->with($this->isType('string'), $this->isType('array')) |
51
|
|
|
->willReturn(true); |
52
|
|
|
|
53
|
|
|
$this->userService = new \Gvera\Services\UserService($gvEntityManager, $session, new ValidationService()); |
54
|
|
|
|
55
|
|
|
$role = new UserRole(); |
56
|
|
|
$role->setName("testRole"); |
57
|
|
|
$role->setRolePriority(5); |
58
|
|
|
|
59
|
|
|
$user = new User(); |
60
|
|
|
$user->setEmail("[email protected]"); |
61
|
|
|
$user->setUsername("test"); |
62
|
|
|
$user->setRole($role); |
63
|
|
|
|
64
|
|
|
$this->user = $user; |
65
|
|
|
$passHash = $this->userService->generatePassword('test'); |
66
|
|
|
$user->setPassword($passHash); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
$session->expects($this->any()) |
71
|
|
|
->method('get') |
72
|
|
|
->with('user') |
73
|
|
|
->willReturn(['role' => $role->getRolePriority(), 'username' => $user->getUsername()]); |
74
|
|
|
|
75
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function generateAndValidatePassword() |
82
|
|
|
{ |
83
|
|
|
$passHash = $this->userService->generatePassword('password'); |
84
|
|
|
$this->assertNotNull($passHash); |
85
|
|
|
|
86
|
|
|
$this->assertTrue($this->userService->validatePassword('password', $passHash)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function login() |
93
|
|
|
{ |
94
|
|
|
$this->expectException(Exception::class); |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
$user = new User(); |
98
|
|
|
$user->setEmail("[email protected]"); |
99
|
|
|
$user->setUsername("test"); |
100
|
|
|
$passHash = $this->userService->generatePassword('test'); |
101
|
|
|
$user->setPassword($passHash); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$repo = $this->createMock(EntityRepository::class); |
105
|
|
|
$repo->expects($this->any()) |
106
|
|
|
->method('findOneBy') |
107
|
|
|
->willReturn($user); |
108
|
|
|
|
109
|
|
|
$gvEntityManager = $this->createMock(GvEntityManager::class); |
110
|
|
|
$gvEntityManager->expects($this->any()) |
111
|
|
|
->method('getRepository') |
112
|
|
|
->willReturn($repo); |
113
|
|
|
|
114
|
|
|
$role = new UserRole(); |
115
|
|
|
$role->setName("testRole"); |
116
|
|
|
$role->setRolePriority(5); |
117
|
|
|
|
118
|
|
|
$user->setRole($role); |
119
|
|
|
|
120
|
|
|
$session = $this->createMock(Session::class); |
121
|
|
|
$session->expects($this->any()) |
122
|
|
|
->method('set') |
123
|
|
|
->with($this->isType('string'), $this->isType('array')) |
124
|
|
|
->willReturn(true); |
125
|
|
|
|
126
|
|
|
$session->expects($this->any()) |
127
|
|
|
->method('get') |
128
|
|
|
->with('user') |
129
|
|
|
->willReturn(['role' => $role->getRolePriority(), 'username' => $user->getUsername()]); |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->userService->entityManager = $gvEntityManager; |
133
|
|
|
$this->userService->session = $session; |
134
|
|
|
|
135
|
|
|
$this->userService->login($user->getUsername(), $passHash); |
136
|
|
|
$this->assertTrue($session->get('user')['username'] === $user->getUsername()); |
|
|
|
|
137
|
|
|
$this->assertTrue($this->userService->getUserRole() === 5); |
138
|
|
|
$this->userService->login($user->getUsername(), 'failedpass'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
*/ |
144
|
|
|
public function logout() |
145
|
|
|
{ |
146
|
|
|
$session = $this->createMock(Session::class); |
147
|
|
|
$session->expects($this->any()) |
148
|
|
|
->method('unsetByKey') |
149
|
|
|
->with('user') |
150
|
|
|
->willReturn(true); |
151
|
|
|
|
152
|
|
|
$session->expects($this->any()) |
153
|
|
|
->method('get') |
154
|
|
|
->with('user') |
155
|
|
|
->willReturn(null); |
156
|
|
|
|
157
|
|
|
$this->userService->session = $session; |
158
|
|
|
|
159
|
|
|
$this->userService->logout(); |
160
|
|
|
|
161
|
|
|
$this->assertFalse($this->userService->isUserLoggedIn()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testUserAuthorization() |
165
|
|
|
{ |
166
|
|
|
|
167
|
|
|
$roleAction = new \Gvera\Models\UserRoleAction(); |
168
|
|
|
$roleAction->setActionName('test'); |
169
|
|
|
|
170
|
|
|
$role = new UserRole(); |
171
|
|
|
$role->setName("testRole"); |
172
|
|
|
$role->setRolePriority(5); |
173
|
|
|
$role->addRoleAction($roleAction); |
174
|
|
|
|
175
|
|
|
$this->gvEntityManager->expects($this->any())->method('getRepository') |
|
|
|
|
176
|
|
|
->willReturn($this->repo); |
177
|
|
|
|
178
|
|
|
$this->repo->expects($this->any())->method('findOneBy') |
|
|
|
|
179
|
|
|
->willReturn($roleAction); |
180
|
|
|
|
181
|
|
|
$user = new User(); |
182
|
|
|
$user->setEmail("[email protected]"); |
183
|
|
|
$user->setUsername("test"); |
184
|
|
|
$user->setRole($role); |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
$this->assertTrue($this->userService->userCan($user, 'test')); |
188
|
|
|
$this->assertFalse($this->userService->userCan(null, 'asd')); |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.