Debian Codegraph @

tests/classes/CoreTest.php

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\Users\User;
6 use Liuch\DmarcSrg\Users\DbUser;
7 use Liuch\DmarcSrg\Users\AdminUser;
8 use Liuch\DmarcSrg\Exception\SoftException;
9 use Liuch\DmarcSrg\Exception\ForbiddenException;
10 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
11 use Liuch\DmarcSrg\Database\DatabaseController;
12 use Liuch\DmarcSrg\Database\UserMapperInterface;
13 use PHPUnit\Framework\Attributes\RunInSeparateProcess;
14
15 class CoreTest extends \PHPUnit\Framework\TestCase
16 {
17 /** @var Core */
18 private $core = null;
19
20 public function setUp(): void
21 {
22 $this->core = Core::instance();
23 $_SERVER['REQUEST_URI'] = '/';
24 }
25
26 public function testSelfInstance(): void
27 {
28 $this->assertSame($this->core, Core::instance());
29 }
30
31 public function testRequestMethod(): void
32 {
33 $_SERVER['REQUEST_METHOD'] = 'some_method';
34 $this->assertSame('some_method', $this->core->requestMethod());
35 unset($_SERVER['REQUEST_METHOD']);
36 }
37
38 public function testGetCurrentUserWhenAuthIsDisabled(): void
39 {
40 $auth = $this->getAuthDisabledMock();
41
42 $sess = $this->getSessionMock([ 'getData', 'destroy' ]);
43 $sess->expects($this->never())->method('destroy');
44 $sess->expects($this->never())->method('getData');
45
46 $core = new Core([ 'auth' => $auth, 'session' => $sess ]);
47
48 $usr = $core->getCurrentUser();
49 $this->assertInstanceOf(AdminUser::class, $usr);
50 }
51
52 public function testGetCurrentUserWhenNobodyLogin(): void
53 {
54 $auth = $this->getAuthEnabledMock();
55
56 $sess = $this->getSessionMock([ 'getData' ]);
57 $sess->expects($this->once())->method('getData')->willReturn([]);
58
59 $core = new Core([ 'auth' => $auth, 'session' => $sess ]);
60
61 $this->assertNull($core->getCurrentUser());
62 }
63
64 public function testGetCurrentUserWhenAdminSessionHasWrongId(): void
65 {
66 $auth = $this->getAuthEnabledMock();
67
68 $sess = $this->getSessionMock([ 'getData' ]);
69 $sess->expects($this->once())->method('getData')->willReturn([
70 'user' => [ 'id' => 1, 'name' => 'admin', 'level' => User::LEVEL_ADMIN ]
71 ]);
72
73 $core = new Core([ 'auth' => $auth, 'session' => $sess ]);
74
75 $this->expectException(ForbiddenException::class);
76 $this->expectExceptionMessage('The user session has been broken!');
77 $core->getCurrentUser();
78 }
79
80 public function testGetCurrentUserWhenAdminSessionHasWrongLevel(): void
81 {
82 $auth = $this->getAuthEnabledMock();
83
84 $sess = $this->getSessionMock([ 'getData' ]);
85 $sess->expects($this->once())->method('getData')->willReturn([
86 'user' => [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_USER ]
87 ]);
88
89 $core = new Core([ 'auth' => $auth, 'session' => $sess ]);
90
91 $this->expectException(ForbiddenException::class);
92 $this->expectExceptionMessage('The user session has been broken!');
93 $core->getCurrentUser();
94 }
95
96 public function testGetCurrentUserWhenAdminSessionIsOk(): void
97 {
98 $auth = $this->getAuthEnabledMock();
99
100 $sess = $this->getSessionMock([ 'getData' ]);
101 $sess->expects($this->once())->method('getData')->willReturn([
102 'user' => [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_ADMIN ]
103 ]);
104
105 $core = new Core([ 'auth' => $auth, 'session' => $sess ]);
106
107 $this->assertInstanceOf(AdminUser::class, $core->getCurrentUser());
108 }
109
110 public function testGetCurrentNotAdminUserWhenUserManagerIsDisabled(): void
111 {
112 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
113 $sess->expects($this->once())->method('getData')->willReturn([
114 'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ]
115 ]);
116 $sess->expects($this->once())->method('destroy');
117
118 $core = new Core([
119 'auth' => $this->getAuthEnabledMock(),
120 'config' => $this->getConfig('users/user_management', false, false),
121 'session' => $sess
122 ]);
123
124 $this->assertNull($core->getCurrentUser());
125 }
126
127 public function testGetCurrentNotAdminUserWhenUserManagerIsEnabledAndUserDoesNotExist(): void
128 {
129 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
130 $sess->expects($this->once())->method('getData')->willReturn([
131 'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ],
132 's_id' => 1, 's_time' => (new DateTime())->getTimestamp() - 999999
133 ]);
134 $sess->expects($this->once())->method('destroy');
135
136 $core = new Core([
137 'auth' => $this->getAuthEnabledMock(),
138 'config' => $this->getConfig('users/user_management', false, true),
139 'database' => $this->getDbMapperUserNotFound(),
140 'session' => $sess
141 ]);
142
143 $this->expectException(SoftException::class);
144 $core->getCurrentUser();
145 }
146
147 public function testGetCurrentNotAdminUserWhenUserManagerIsEnabledAndUserIsDisabled(): void
148 {
149 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
150 $sess->expects($this->once())->method('getData')->willReturn([
151 'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ],
152 's_id' => 1, 's_time' => (new DateTime())->getTimestamp() - 999999
153 ]);
154 $sess->expects($this->once())->method('destroy');
155
156 $core = new Core([
157 'auth' => $this->getAuthEnabledMock(),
158 'config' => $this->getConfig('users/user_management', false, true),
159 'database' => $this->getDbMapperUserDisabled(),
160 'session' => $sess
161 ]);
162
163 $this->assertNull($core->getCurrentUser());
164 }
165
166 public function testGetCurrentNotAdminUserWhenUserManagerIsEnabledAndUserExists(): void
167 {
168 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
169 $sess->expects($this->once())->method('getData')->willReturn([
170 'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ],
171 's_id' => 1, 's_time' => (new DateTime())->getTimestamp() - 999999
172 ]);
173 $sess->expects($this->never())->method('destroy');
174
175 $core = new Core([
176 'auth' => $this->getAuthEnabledMock(),
177 'config' => $this->getConfig('users/user_management', false, true),
178 'database' => $this->getDbMapperUserEnabled(),
179 'session' => $sess
180 ]);
181
182 $usr = $core->getCurrentUser();
183 $this->assertInstanceOf(DbUser::class, $usr);
184 $this->assertSame(User::LEVEL_MANAGER, $usr->level());
185 }
186
187 public function testGetCurrentNotAdminUserWhenUserManagerIsEnabledAndUserGotNewStatus(): void
188 {
189 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
190 $sess->expects($this->once())->method('getData')->willReturn([
191 'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ],
192 's_id' => 1, 's_time' => (new DateTime())->getTimestamp() - 999999
193 ]);
194 $sess->expects($this->once())->method('destroy');
195
196 $core = new Core([
197 'auth' => $this->getAuthEnabledMock(),
198 'config' => $this->getConfig('users/user_management', false, true),
199 'database' => $this->getDbMapperUserChangedStatus(),
200 'session' => $sess
201 ]);
202
203 $this->assertNull($core->getCurrentUser());
204 }
205
206 public function testSetAdminUserWithName(): void
207 {
208 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
209 $sess->expects($this->never())->method('destroy');
210 $sess->expects($this->never())->method('getData');
211
212 $core = new Core([
213 'auth' => $this->getAuthEnabledMock(),
214 'session' => $sess
215 ]);
216
217 $core->setCurrentUser('admin');
218 $this->assertInstanceOf(AdminUser::class, $core->getCurrentUser());
219 }
220
221 public function testSetAdminUserWithInstance(): void
222 {
223 $sess = $this->getSessionMock([ 'destroy', 'getData' ]);
224 $sess->expects($this->never())->method('destroy');
225 $sess->expects($this->never())->method('getData');
226
227 $core = new Core([
228 'auth' => $this->getAuthEnabledMock(),
229 'session' => $sess
230 ]);
231
232 $admin = new AdminUser();
233 $core->setCurrentUser($admin);
234 $this->assertSame($admin, $core->getCurrentUser());
235 }
236
237 public function testUnsetUserWithNullValueWithWeb(): void
238 {
239 $_SERVER['REQUEST_METHOD'] = 'GET'; // self::isWEB() -> true
240
241 $sess = $this->getSessionMock([ 'destroy', 'getData', 'setData' ]);
242 $sess->expects($this->once())->method('destroy');
243 $sess->expects($this->never())->method('getData');
244 $sess->expects($this->never())->method('setData');
245
246 $core = new Core([
247 'session' => $sess
248 ]);
249
250 $core->setCurrentUser(null);
251
252 unset($_SERVER['REQUEST_METHOD']);
253 }
254
255 public function testSetDbUserWithWeb(): void
256 {
257 $_SERVER['REQUEST_METHOD'] = 'GET'; // self::isWEB() -> true
258
259 $sess = $this->getSessionMock([ 'destroy', 'getData', 'setData' ]);
260 $sess->expects($this->once())->method('destroy');
261 $sess->expects($this->never())->method('getData');
262 $sess->expects($this->once())->method('setData');
263
264 $db = $this->getDbMapperUserEnabled();
265
266 $core = new Core([
267 'session' => $sess,
268 'database' => $db
269 ]);
270
271 $core->setCurrentUser(new DbUser([ 'id' => 1, 'name' => 'User' ], $db));
272
273 unset($_SERVER['REQUEST_METHOD']);
274 }
275
276 public function testSetDbUserWithNoWeb(): void
277 {
278 $sess = $this->getSessionMock([ 'destroy', 'getData', 'setData' ]);
279 $sess->expects($this->never())->method('destroy');
280 $sess->expects($this->never())->method('getData');
281 $sess->expects($this->never())->method('setData');
282
283 $core = new Core([
284 'session' => $sess
285 ]);
286
287 $core->setCurrentUser(new DbUser([ 'id' => 1, 'name' => 'User' ]));
288 }
289
290 public function testSendHtml(): void
291 {
292 foreach ([
293 [ '', false, 'Empty custom CSS' ],
294 [ 'custom.css', true, 'Correct custom CSS' ],
295 [ 'custom.csss', false, 'Incorrect custom CSS' ]
296 ] as $it) {
297 $core = new Core([
298 'config' => $this->getConfig('custom_css', '', $it[0]),
299 'template' => realpath(__DIR__ . '/../..') . '/template.html'
300 ]);
301 ob_start();
302 $core->sendHtml();
303 $output = ob_get_contents();
304 ob_end_clean();
305 $this->assertIsString($output);
306 $this->assertStringNotContainsString('<!-- Custom CSS -->', $output, $it[2]);
307 if ($it[1]) {
308 $this->assertStringContainsString($it[0], $output, $it[2]);
309 } elseif (!empty($it[0])) {
310 $this->assertStringNotContainsString($it[0], $output, $it[2]);
311 }
312 }
313 }
314
315 public function testSendJson(): void
316 {
317 $data = [ 'key1' => 'value1', [ 'key2' => 'value2' ] ];
318 ob_start();
319 $this->core->sendJson($data);
320 $output = ob_get_contents();
321 ob_end_clean();
322 $this->assertJsonStringEqualsJsonString(json_encode($data), $output);
323 }
324
325 public function testAuthInstance(): void
326 {
327 $this->assertSame($this->core->auth(), $this->core->auth());
328 }
329
330 public function testStatusInstance(): void
331 {
332 $this->assertSame($this->core->status(), $this->core->status());
333 }
334
335 public function testAdminIntance(): void
336 {
337 $this->assertSame($this->core->admin(), $this->core->admin());
338 }
339
340 public function testErrorHandlerInstance(): void
341 {
342 $this->assertSame($this->core->errorHandler(), $this->core->errorHandler());
343 }
344
345 public function testLoggerInstance(): void
346 {
347 $this->assertSame($this->core->logger(), $this->core->logger());
348 }
349
350 public function testCheckingOneDependencyWithNoCurrentUser(): void
351 {
352 $sess = $this->getSessionMock([ 'getData' ]);
353 $sess->expects($this->once())->method('getData')->willReturn([]);
354
355 $core = new Core([
356 'auth' => $this->getAuthEnabledMock(),
357 'session' => $sess
358 ]);
359
360 $this->expectException(SoftException::class);
361 $this->expectExceptionMessage('Required dependency is missing. Contact the administrator.');
362 $core->checkDependencies('fake');
363 }
364
365 public function testCheckingOneDependencyWithNotAdminCurrentUser(): void
366 {
367 $sess = $this->getSessionMock([ 'getData' ]);
368 $sess->expects($this->once())->method('getData')->willReturn([
369 'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ]
370 ]);
371
372 $db = $this->getMockBuilder(DatabaseController::class)
373 ->disableOriginalConstructor()
374 ->getMock();
375
376 $core = new Core([
377 'auth' => $this->getAuthEnabledMock(),
378 'config' => $this->getConfig('users/user_management', false, true),
379 'database' => $db,
380 'session' => $sess
381 ]);
382
383 $this->expectException(SoftException::class);
384 $this->expectExceptionMessage('Required dependency is missing. Contact the administrator.');
385 $core->checkDependencies('fake');
386 }
387
388 public function testCheckingOneDependencyWithAdminCurrentUser(): void
389 {
390 $sess = $this->getSessionMock([ 'getData' ]);
391 $sess->expects($this->once())->method('getData')->willReturn([
392 'user' => [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_ADMIN ]
393 ]);
394
395 $core = new Core([
396 'auth' => $this->getAuthEnabledMock(),
397 'session' => $sess
398 ]);
399
400 $this->expectException(SoftException::class);
401 $this->expectExceptionMessage('Required dependency is missing: ext-fake.');
402 $core->checkDependencies('fake');
403 }
404
405 public function testCheckingSeveralDependencies(): void
406 {
407 $sess = $this->getSessionMock([ 'getData' ]);
408 $sess->expects($this->once())->method('getData')->willReturn([
409 'user' => [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_ADMIN ]
410 ]);
411
412 $core = new Core([
413 'auth' => $this->getAuthEnabledMock(),
414 'session' => $sess
415 ]);
416
417 $this->expectException(SoftException::class);
418 $this->expectExceptionMessage('Required dependencies are missing: ext-fake1, ext-fake2.');
419 $core->checkDependencies('fake1,fake2');
420 }
421
422 public function testConfigExistingParameters(): void
423 {
424 $core = new Core([ 'config' => $this->getConfig('some/param', 'default', 'some_value') ]);
425 $this->assertSame('some_value', $core->config('some/param', 'default'));
426 }
427
428 public function testConfigNonexistentParameters(): void
429 {
430 $core = new Core([ 'config' => [ 'Liuch\DmarcSrg\Config', [ 'tests/conf_test_file.php' ] ] ]);
431 $config = new Config('tests/conf_test_file.php');
432 $this->assertNull($core->config('some_unknown_parameter'));
433 $this->assertIsString($core->config('some_unknown_parameter', ''));
434 }
435
436 private function getAuthEnabledMock()
437 {
438 $auth = $this->getMockBuilder(Auth::class)
439 ->disableOriginalConstructor()
440 ->onlyMethods([ 'isEnabled' ])
441 ->getMock();
442 $auth->expects($this->once())->method('isEnabled')->willReturn(true);
443 return $auth;
444 }
445
446 private function getAuthDisabledMock()
447 {
448 $auth = $this->getMockBuilder(Auth::class)
449 ->disableOriginalConstructor()
450 ->onlyMethods([ 'isEnabled' ])
451 ->getMock();
452 $auth->expects($this->once())->method('isEnabled')->willReturn(false);
453 return $auth;
454 }
455
456 private function getSessionMock(array $methods)
457 {
458 return $this->getMockBuilder(Session::class)->onlyMethods($methods)->getMock();
459 }
460
461 private function getConfig(string $param, $defval, $value)
462 {
463 $config = $this->getMockBuilder(Config::class)
464 ->disableOriginalConstructor()
465 ->onlyMethods([ 'get' ])
466 ->getMock();
467 $config->expects($this->once())
468 ->method('get')
469 ->with($this->equalTo($param), $this->equalTo($defval))
470 ->willReturn($value);
471 return $config;
472 }
473
474 private function getDbMapperUserNotFound(): object
475 {
476 $mapper = $this->getMockBuilder(UserMapperInterface::class)->disableOriginalConstructor()->getMock();
477 $mapper->expects($this->once())->method('fetch')->willThrowException(new DatabaseNotFoundException());
478
479 $db = $this->getMockBuilder(DatabaseController::class)
480 ->disableOriginalConstructor()
481 ->onlyMethods([ 'getMapper' ])
482 ->getMock();
483 $db->expects($this->once())->method('getMapper')->with('user')->willReturn($mapper);
484 return $db;
485 }
486
487 private function getDbMapperUserDisabled(): object
488 {
489 return $this->getDbMapperUserOnce('fetch', [ 'enabled', 'session' ], [ false, 1 ]);
490 }
491
492 private function getDbMapperUserEnabled(): object
493 {
494 return $this->getDbMapperUserOnce('fetch', [ 'enabled', 'session', 'level' ], [ true, 1, User::LEVEL_MANAGER ]);
495 }
496
497 private function getDbMapperUserChangedStatus(): object
498 {
499 return $this->getDbMapperUserOnce('fetch', [ 'enabled', 'session', 'level' ], [ true, 2, User::LEVEL_MANAGER ]);
500 }
501
502 private function getDbMapperUserOnce(string $method, array $keys, array $values): object
503 {
504 $mapper = $this->getMockBuilder(UserMapperInterface::class)
505 ->disableOriginalConstructor()
506 ->onlyMethods([
507 'exists', 'save', 'delete', 'fetch', 'list', 'getPasswordHash',
508 'savePasswordHash', 'setUserKey'
509 ])
510 ->getMock();
511 $mapper->expects($this->once())->method($method)->willReturnCallback(function (&$data) use ($keys, $values) {
512 for ($i = 0; $i < count($keys); ++$i) {
513 $data[$keys[$i]] = $values[$i];
514 }
515 });
516
517 $db = $this->getMockBuilder(DatabaseController::class)
518 ->disableOriginalConstructor()
519 ->onlyMethods([ 'getMapper' ])
520 ->getMock();
521 $db->expects($this->once())->method('getMapper')->with('user')->willReturn($mapper);
522 return $db;
523 }
524 }