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
|
|
|
/** |
16
|
|
|
* @var UserRepository |
17
|
|
|
*/ |
18
|
|
|
private $repository; |
19
|
|
|
|
20
|
|
|
protected function setUp(): void |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
$this->repository = _em()->getRepository(User::class); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function providerGetAccessibleSubQuery(): array |
27
|
|
|
{ |
28
|
|
|
$all = [1000, 1001, 1002, 1003]; |
29
|
|
|
|
30
|
|
|
return [ |
31
|
|
|
['anonymous', [1001]], |
32
|
|
|
['member', $all], |
33
|
|
|
['facilitator', $all], |
34
|
|
|
['administrator', $all], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetOneByEmailPassword(): void |
39
|
|
|
{ |
40
|
|
|
self::assertNull($this->repository->getOneByEmailPassword('[email protected]', 'bar'), 'wrong user'); |
41
|
|
|
self::assertNull($this->repository->getOneByEmailPassword('[email protected]', 'bar'), 'wrong password'); |
42
|
|
|
|
43
|
|
|
$user = $this->repository->getOneByEmailPassword('[email protected]', 'administrator'); |
44
|
|
|
self::assertNotNull($user); |
45
|
|
|
self::assertSame(1000, $user->getId()); |
46
|
|
|
|
47
|
|
|
$hash = _em()->getConnection()->executeQuery('SELECT password FROM `user` WHERE id = 1000')->fetchOne(); |
48
|
|
|
self::assertStringStartsWith('$', $hash, 'password should have been re-hashed automatically'); |
|
|
|
|
49
|
|
|
self::assertNotSame(md5('administrator'), $hash, 'password should have been re-hashed automatically'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetOneById(): void |
53
|
|
|
{ |
54
|
|
|
self::assertNull($this->repository->getOneById(1), 'wrong user'); |
55
|
|
|
|
56
|
|
|
$user = $this->repository->getOneById(1000); |
57
|
|
|
self::assertNotNull($user); |
58
|
|
|
self::assertSame(1000, $user->getId()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testRecordLogin(): void |
62
|
|
|
{ |
63
|
|
|
User::setCurrent($this->repository->getOneById(1000)); |
64
|
|
|
|
65
|
|
|
/** @var User $user */ |
66
|
|
|
$user = $this->getEntityManager()->getReference(User::class, 1002); |
67
|
|
|
|
68
|
|
|
self::assertNull($user->getFirstLogin()); |
69
|
|
|
self::assertNull($user->getLastLogin()); |
70
|
|
|
$this->assertNoStamp($user); |
71
|
|
|
|
72
|
|
|
$user->recordLogin(); |
73
|
|
|
$this->getEntityManager()->flush(); |
74
|
|
|
|
75
|
|
|
$firstLogin = $user->getFirstLogin(); |
76
|
|
|
$lastLogin = $user->getLastLogin(); |
77
|
|
|
self::assertNotNull($firstLogin); |
78
|
|
|
self::assertNotNull($lastLogin); |
79
|
|
|
$this->assertNoStamp($user); |
80
|
|
|
|
81
|
|
|
$user->recordLogin(); |
82
|
|
|
$this->getEntityManager()->flush(); |
83
|
|
|
|
84
|
|
|
$firstLogin2 = $user->getFirstLogin(); |
85
|
|
|
$lastLogin2 = $user->getLastLogin(); |
86
|
|
|
self::assertSame($firstLogin, $firstLogin2); |
87
|
|
|
self::assertNotSame($lastLogin, $lastLogin2); |
88
|
|
|
self::assertNotNull($firstLogin2); |
89
|
|
|
self::assertNotNull($lastLogin2); |
90
|
|
|
$this->assertNoStamp($user); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function assertNoStamp(User $user): void |
94
|
|
|
{ |
95
|
|
|
$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'); |
96
|
|
|
self::assertSame(1, $count); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|