| 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 |
|
| 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'; |
| 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'; |
| 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 |
$this->markTestSkipped('Does not work on Debian.'); |
| 279 |
$sess = $this->getSessionMock([ 'destroy', 'getData', 'setData' ]); |
| 280 |
$sess->expects($this->never())->method('destroy'); |
| 281 |
$sess->expects($this->never())->method('getData'); |
| 282 |
$sess->expects($this->never())->method('setData'); |
| 283 |
|
| 284 |
$core = new Core([ |
| 285 |
'session' => $sess |
| 286 |
]); |
| 287 |
|
| 288 |
$core->setCurrentUser(new DbUser([ 'id' => 1, 'name' => 'User' ])); |
| 289 |
} |
| 290 |
|
| 291 |
public function testSendHtml(): void |
| 292 |
{ |
| 293 |
foreach ([ |
| 294 |
[ '', false, 'Empty custom CSS' ], |
| 295 |
[ 'custom.css', true, 'Correct custom CSS' ], |
| 296 |
[ 'custom.csss', false, 'Incorrect custom CSS' ] |
| 297 |
] as $it) { |
| 298 |
$core = new Core([ |
| 299 |
'config' => $this->getConfig('custom_css', '', $it[0]), |
| 300 |
'template' => realpath(__DIR__ . '/../..') . '/template.html' |
| 301 |
]); |
| 302 |
ob_start(); |
| 303 |
$core->sendHtml(); |
| 304 |
$output = ob_get_contents(); |
| 305 |
ob_end_clean(); |
| 306 |
$this->assertIsString($output); |
| 307 |
$this->assertStringNotContainsString('<!-- Custom CSS -->', $output, $it[2]); |
| 308 |
if ($it[1]) { |
| 309 |
$this->assertStringContainsString($it[0], $output, $it[2]); |
| 310 |
} elseif (!empty($it[0])) { |
| 311 |
$this->assertStringNotContainsString($it[0], $output, $it[2]); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
public function testSendJson(): void |
| 317 |
{ |
| 318 |
$data = [ 'key1' => 'value1', [ 'key2' => 'value2' ] ]; |
| 319 |
ob_start(); |
| 320 |
$this->core->sendJson($data); |
| 321 |
$output = ob_get_contents(); |
| 322 |
ob_end_clean(); |
| 323 |
$this->assertJsonStringEqualsJsonString(json_encode($data), $output); |
| 324 |
} |
| 325 |
|
| 326 |
public function testAuthInstance(): void |
| 327 |
{ |
| 328 |
$this->assertSame($this->core->auth(), $this->core->auth()); |
| 329 |
} |
| 330 |
|
| 331 |
public function testStatusInstance(): void |
| 332 |
{ |
| 333 |
$this->assertSame($this->core->status(), $this->core->status()); |
| 334 |
} |
| 335 |
|
| 336 |
public function testAdminIntance(): void |
| 337 |
{ |
| 338 |
$this->assertSame($this->core->admin(), $this->core->admin()); |
| 339 |
} |
| 340 |
|
| 341 |
public function testErrorHandlerInstance(): void |
| 342 |
{ |
| 343 |
$this->assertSame($this->core->errorHandler(), $this->core->errorHandler()); |
| 344 |
} |
| 345 |
|
| 346 |
public function testLoggerInstance(): void |
| 347 |
{ |
| 348 |
$this->assertSame($this->core->logger(), $this->core->logger()); |
| 349 |
} |
| 350 |
|
| 351 |
public function testCheckingOneDependencyWithNoCurrentUser(): void |
| 352 |
{ |
| 353 |
$sess = $this->getSessionMock([ 'getData' ]); |
| 354 |
$sess->expects($this->once())->method('getData')->willReturn([]); |
| 355 |
|
| 356 |
$core = new Core([ |
| 357 |
'auth' => $this->getAuthEnabledMock(), |
| 358 |
'session' => $sess |
| 359 |
]); |
| 360 |
|
| 361 |
$this->expectException(SoftException::class); |
| 362 |
$this->expectExceptionMessage('Required dependency is missing. Contact the administrator.'); |
| 363 |
$core->checkDependencies('fake'); |
| 364 |
} |
| 365 |
|
| 366 |
public function testCheckingOneDependencyWithNotAdminCurrentUser(): void |
| 367 |
{ |
| 368 |
$sess = $this->getSessionMock([ 'getData' ]); |
| 369 |
$sess->expects($this->once())->method('getData')->willReturn([ |
| 370 |
'user' => [ 'id' => 1, 'name' => 'user', 'level' => User::LEVEL_USER ] |
| 371 |
]); |
| 372 |
|
| 373 |
$db = $this->getMockBuilder(DatabaseController::class) |
| 374 |
->disableOriginalConstructor() |
| 375 |
->getMock(); |
| 376 |
|
| 377 |
$core = new Core([ |
| 378 |
'auth' => $this->getAuthEnabledMock(), |
| 379 |
'config' => $this->getConfig('users/user_management', false, true), |
| 380 |
'database' => $db, |
| 381 |
'session' => $sess |
| 382 |
]); |
| 383 |
|
| 384 |
$this->expectException(SoftException::class); |
| 385 |
$this->expectExceptionMessage('Required dependency is missing. Contact the administrator.'); |
| 386 |
$core->checkDependencies('fake'); |
| 387 |
} |
| 388 |
|
| 389 |
public function testCheckingOneDependencyWithAdminCurrentUser(): void |
| 390 |
{ |
| 391 |
$sess = $this->getSessionMock([ 'getData' ]); |
| 392 |
$sess->expects($this->once())->method('getData')->willReturn([ |
| 393 |
'user' => [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_ADMIN ] |
| 394 |
]); |
| 395 |
|
| 396 |
$core = new Core([ |
| 397 |
'auth' => $this->getAuthEnabledMock(), |
| 398 |
'session' => $sess |
| 399 |
]); |
| 400 |
|
| 401 |
$this->expectException(SoftException::class); |
| 402 |
$this->expectExceptionMessage('Required dependency is missing: ext-fake.'); |
| 403 |
$core->checkDependencies('fake'); |
| 404 |
} |
| 405 |
|
| 406 |
public function testCheckingSeveralDependencies(): void |
| 407 |
{ |
| 408 |
$sess = $this->getSessionMock([ 'getData' ]); |
| 409 |
$sess->expects($this->once())->method('getData')->willReturn([ |
| 410 |
'user' => [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_ADMIN ] |
| 411 |
]); |
| 412 |
|
| 413 |
$core = new Core([ |
| 414 |
'auth' => $this->getAuthEnabledMock(), |
| 415 |
'session' => $sess |
| 416 |
]); |
| 417 |
|
| 418 |
$this->expectException(SoftException::class); |
| 419 |
$this->expectExceptionMessage('Required dependencies are missing: ext-fake1, ext-fake2.'); |
| 420 |
$core->checkDependencies('fake1,fake2'); |
| 421 |
} |
| 422 |
|
| 423 |
public function testConfigExistingParameters(): void |
| 424 |
{ |
| 425 |
$core = new Core([ 'config' => $this->getConfig('some/param', 'default', 'some_value') ]); |
| 426 |
$this->assertSame('some_value', $core->config('some/param', 'default')); |
| 427 |
} |
| 428 |
|
| 429 |
public function testConfigNonexistentParameters(): void |
| 430 |
{ |
| 431 |
$core = new Core([ 'config' => [ 'Liuch\DmarcSrg\Config', [ 'tests/conf_test_file.php' ] ] ]); |
| 432 |
$config = new Config('tests/conf_test_file.php'); |
| 433 |
$this->assertNull($core->config('some_unknown_parameter')); |
| 434 |
$this->assertIsString($core->config('some_unknown_parameter', '')); |
| 435 |
} |
| 436 |
|
| 437 |
private function getAuthEnabledMock() |
| 438 |
{ |
| 439 |
$auth = $this->getMockBuilder(Auth::class) |
| 440 |
->disableOriginalConstructor() |
| 441 |
->onlyMethods([ 'isEnabled' ]) |
| 442 |
->getMock(); |
| 443 |
$auth->expects($this->once())->method('isEnabled')->willReturn(true); |
| 444 |
return $auth; |
| 445 |
} |
| 446 |
|
| 447 |
private function getAuthDisabledMock() |
| 448 |
{ |
| 449 |
$auth = $this->getMockBuilder(Auth::class) |
| 450 |
->disableOriginalConstructor() |
| 451 |
->onlyMethods([ 'isEnabled' ]) |
| 452 |
->getMock(); |
| 453 |
$auth->expects($this->once())->method('isEnabled')->willReturn(false); |
| 454 |
return $auth; |
| 455 |
} |
| 456 |
|
| 457 |
private function getSessionMock(array $methods) |
| 458 |
{ |
| 459 |
return $this->getMockBuilder(Session::class)->onlyMethods($methods)->getMock(); |
| 460 |
} |
| 461 |
|
| 462 |
private function getConfig(string $param, $defval, $value) |
| 463 |
{ |
| 464 |
$config = $this->getMockBuilder(Config::class) |
| 465 |
->disableOriginalConstructor() |
| 466 |
->onlyMethods([ 'get' ]) |
| 467 |
->getMock(); |
| 468 |
$config->expects($this->once()) |
| 469 |
->method('get') |
| 470 |
->with($this->equalTo($param), $this->equalTo($defval)) |
| 471 |
->willReturn($value); |
| 472 |
return $config; |
| 473 |
} |
| 474 |
|
| 475 |
private function getDbMapperUserNotFound(): object |
| 476 |
{ |
| 477 |
$mapper = $this->getMockBuilder(UserMapperInterface::class)->disableOriginalConstructor()->getMock(); |
| 478 |
$mapper->expects($this->once())->method('fetch')->willThrowException(new DatabaseNotFoundException()); |
| 479 |
|
| 480 |
$db = $this->getMockBuilder(DatabaseController::class) |
| 481 |
->disableOriginalConstructor() |
| 482 |
->onlyMethods([ 'getMapper' ]) |
| 483 |
->getMock(); |
| 484 |
$db->expects($this->once())->method('getMapper')->with('user')->willReturn($mapper); |
| 485 |
return $db; |
| 486 |
} |
| 487 |
|
| 488 |
private function getDbMapperUserDisabled(): object |
| 489 |
{ |
| 490 |
return $this->getDbMapperUserOnce('fetch', [ 'enabled', 'session' ], [ false, 1 ]); |
| 491 |
} |
| 492 |
|
| 493 |
private function getDbMapperUserEnabled(): object |
| 494 |
{ |
| 495 |
return $this->getDbMapperUserOnce('fetch', [ 'enabled', 'session', 'level' ], [ true, 1, User::LEVEL_MANAGER ]); |
| 496 |
} |
| 497 |
|
| 498 |
private function getDbMapperUserChangedStatus(): object |
| 499 |
{ |
| 500 |
return $this->getDbMapperUserOnce('fetch', [ 'enabled', 'session', 'level' ], [ true, 2, User::LEVEL_MANAGER ]); |
| 501 |
} |
| 502 |
|
| 503 |
private function getDbMapperUserOnce(string $method, array $keys, array $values): object |
| 504 |
{ |
| 505 |
$mapper = $this->getMockBuilder(UserMapperInterface::class) |
| 506 |
->disableOriginalConstructor() |
| 507 |
->onlyMethods([ |
| 508 |
'exists', 'save', 'delete', 'fetch', 'list', 'getPasswordHash', |
| 509 |
'savePasswordHash', 'setUserKey' |
| 510 |
]) |
| 511 |
->getMock(); |
| 512 |
$mapper->expects($this->once())->method($method)->willReturnCallback(function (&$data) use ($keys, $values) { |
| 513 |
for ($i = 0; $i < count($keys); ++$i) { |
| 514 |
$data[$keys[$i]] = $values[$i]; |
| 515 |
} |
| 516 |
}); |
| 517 |
|
| 518 |
$db = $this->getMockBuilder(DatabaseController::class) |
| 519 |
->disableOriginalConstructor() |
| 520 |
->onlyMethods([ 'getMapper' ]) |
| 521 |
->getMock(); |
| 522 |
$db->expects($this->once())->method('getMapper')->with('user')->willReturn($mapper); |
| 523 |
return $db; |
| 524 |
} |
| 525 |
} |