1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Repository; |
6
|
|
|
|
7
|
|
|
use Application\Model\User; |
8
|
|
|
use Application\Repository\UserRepository; |
9
|
|
|
use ApplicationTest\Traits\LimitedAccessSubQuery; |
10
|
|
|
|
11
|
|
|
class UserRepositoryTest extends AbstractRepositoryTest |
12
|
|
|
{ |
13
|
|
|
use LimitedAccessSubQuery; |
14
|
|
|
|
15
|
|
|
private UserRepository $repository; |
16
|
|
|
|
17
|
|
|
protected function setUp(): void |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
$this->repository = _em()->getRepository(User::class); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function providerGetAccessibleSubQuery(): iterable |
24
|
|
|
{ |
25
|
|
|
$all = [1000, 1001, 1002, 1003]; |
26
|
|
|
yield ['anonymous', [1001]]; |
27
|
|
|
yield ['member', $all]; |
28
|
|
|
yield ['facilitator', $all]; |
29
|
|
|
yield ['administrator', $all]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testGetOneByEmailPassword(): void |
33
|
|
|
{ |
34
|
|
|
self::assertNull($this->repository->getOneByEmailPassword('[email protected]', 'bar'), 'wrong user'); |
35
|
|
|
self::assertNull($this->repository->getOneByEmailPassword('[email protected]', 'bar'), 'wrong password'); |
36
|
|
|
|
37
|
|
|
$user = $this->repository->getOneByEmailPassword('[email protected]', 'administrator'); |
38
|
|
|
self::assertNotNull($user); |
39
|
|
|
self::assertSame(1000, $user->getId()); |
40
|
|
|
|
41
|
|
|
$hash = _em()->getConnection()->executeQuery('SELECT password FROM `user` WHERE id = 1000')->fetchOne(); |
42
|
|
|
self::assertStringStartsWith('$', $hash, 'password should have been re-hashed automatically'); |
43
|
|
|
self::assertNotSame(md5('administrator'), $hash, 'password should have been re-hashed automatically'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetOneById(): void |
47
|
|
|
{ |
48
|
|
|
self::assertNull($this->repository->getOneById(1), 'wrong user'); |
49
|
|
|
|
50
|
|
|
$user = $this->repository->getOneById(1000); |
51
|
|
|
self::assertNotNull($user); |
52
|
|
|
self::assertSame(1000, $user->getId()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testRecordLogin(): void |
56
|
|
|
{ |
57
|
|
|
User::setCurrent($this->repository->getOneById(1000)); |
58
|
|
|
|
59
|
|
|
/** @var User $user */ |
60
|
|
|
$user = $this->getEntityManager()->getReference(User::class, 1002); |
61
|
|
|
|
62
|
|
|
self::assertNull($user->getFirstLogin()); |
63
|
|
|
self::assertNull($user->getLastLogin()); |
64
|
|
|
$this->assertNoStamp($user); |
65
|
|
|
|
66
|
|
|
$user->recordLogin(); |
67
|
|
|
$this->getEntityManager()->flush(); |
68
|
|
|
|
69
|
|
|
$firstLogin = $user->getFirstLogin(); |
70
|
|
|
$lastLogin = $user->getLastLogin(); |
71
|
|
|
self::assertNotNull($firstLogin); |
72
|
|
|
self::assertNotNull($lastLogin); |
73
|
|
|
$this->assertNoStamp($user); |
74
|
|
|
|
75
|
|
|
$user->recordLogin(); |
76
|
|
|
$this->getEntityManager()->flush(); |
77
|
|
|
|
78
|
|
|
$firstLogin2 = $user->getFirstLogin(); |
79
|
|
|
$lastLogin2 = $user->getLastLogin(); |
80
|
|
|
self::assertSame($firstLogin, $firstLogin2); |
81
|
|
|
self::assertNotSame($lastLogin, $lastLogin2); |
82
|
|
|
self::assertNotNull($lastLogin2); |
83
|
|
|
$this->assertNoStamp($user); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function assertNoStamp(User $user): void |
87
|
|
|
{ |
88
|
|
|
$count = $this->getEntityManager()->getConnection()->fetchOne('SELECT COUNT(*) FROM user WHERE id = ' . $user->getId() . ' AND creation_date IS NULL AND creator_id IS NULL AND update_date IS NULL AND updater_id IS NULL'); |
89
|
|
|
self::assertSame(1, $count); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|