1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SourcesTests\Files\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_auth\AuthException; |
8
|
|
|
use kalanis\kw_auth\Data\FileGroup; |
9
|
|
|
use kalanis\kw_auth\Sources\Files\Storage\Groups; |
10
|
|
|
use kalanis\kw_locks\LockException; |
11
|
|
|
use kalanis\kw_storage\Storage\Key\DefaultKey; |
12
|
|
|
use kalanis\kw_storage\Storage\Storage; |
13
|
|
|
use kalanis\kw_storage\Storage\Target\Memory; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class GroupsTest extends CommonTestClass |
17
|
|
|
{ |
18
|
|
|
protected $sourcePath = '.groups'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @throws AuthException |
22
|
|
|
* @throws LockException |
23
|
|
|
*/ |
24
|
|
|
public function testNotExistsData(): void |
25
|
|
|
{ |
26
|
|
|
$lib = $this->emptyGroupSources(); |
27
|
|
|
$this->assertNull($lib->getGroupDataOnly(15)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws AuthException |
33
|
|
|
* @throws LockException |
34
|
|
|
*/ |
35
|
|
|
public function testCreateGroupOnEmptyInstance(): void |
36
|
|
|
{ |
37
|
|
|
$lib = $this->emptyGroupSources(); |
38
|
|
|
$group = $this->wantedGroup(); |
39
|
|
|
|
40
|
|
|
// create |
41
|
|
|
$lib->createGroup($group); |
42
|
|
|
// check data |
43
|
|
|
$saved = $lib->getGroupDataOnly(1); |
44
|
|
|
$this->assertEquals('another', $saved->getGroupName()); |
45
|
|
|
$this->assertEquals('Testing group', $saved->getGroupDesc()); |
46
|
|
|
$this->assertEquals(1001, $saved->getGroupAuthorId()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws AuthException |
51
|
|
|
* @throws LockException |
52
|
|
|
*/ |
53
|
|
|
public function testUpdateGroupOnEmptyInstance(): void |
54
|
|
|
{ |
55
|
|
|
$lib = $this->emptyGroupSources(); |
56
|
|
|
$group = $this->wantedGroup(); |
57
|
|
|
|
58
|
|
|
// update |
59
|
|
|
$this->expectException(AuthException::class); |
60
|
|
|
$lib->updateGroup($group); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws AuthException |
65
|
|
|
* @throws LockException |
66
|
|
|
*/ |
67
|
|
|
public function testGroupManipulation(): void |
68
|
|
|
{ |
69
|
|
|
$lib = $this->groupSources(); |
70
|
|
|
$group = $this->wantedGroup(); |
71
|
|
|
|
72
|
|
|
// create |
73
|
|
|
$lib->createGroup($group); |
74
|
|
|
// check data |
75
|
|
|
$saved = $lib->getGroupDataOnly(3); |
76
|
|
|
$this->assertEquals('another', $saved->getGroupName()); |
77
|
|
|
$this->assertEquals('Testing group', $saved->getGroupDesc()); |
78
|
|
|
$this->assertEquals(1001, $saved->getGroupAuthorId()); |
79
|
|
|
|
80
|
|
|
// update |
81
|
|
|
$group->setData( |
82
|
|
|
$group->getGroupId(), |
83
|
|
|
$group->getGroupName(), |
84
|
|
|
1002, |
85
|
|
|
'WheĐn yoĐu dđo nođt knđow' |
86
|
|
|
); |
87
|
|
|
$lib->updateGroup($group); |
88
|
|
|
|
89
|
|
|
// check data - again with new values |
90
|
|
|
$saved = $lib->getGroupDataOnly(3); |
91
|
|
|
$this->assertEquals('When you do not know', $saved->getGroupDesc()); // overwrite this |
92
|
|
|
$this->assertEquals(1001, $saved->getGroupAuthorId()); // cannot overwrite this |
93
|
|
|
|
94
|
|
|
// remove |
95
|
|
|
$lib->deleteGroup($group->getGroupId()); |
96
|
|
|
// check for existence |
97
|
|
|
$this->assertEmpty($lib->getGroupDataOnly(3)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws AuthException |
102
|
|
|
* @throws LockException |
103
|
|
|
*/ |
104
|
|
|
public function testCreateFail(): void |
105
|
|
|
{ |
106
|
|
|
$lib = $this->groupSources(); |
107
|
|
|
$group = $this->wantedGroup(''); |
108
|
|
|
$this->expectException(AuthException::class); |
109
|
|
|
$lib->createGroup($group); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws AuthException |
114
|
|
|
* @throws LockException |
115
|
|
|
*/ |
116
|
|
|
public function testAllGroups(): void |
117
|
|
|
{ |
118
|
|
|
$lib = $this->groupSources(); |
119
|
|
|
$data = $lib->readGroup(); |
120
|
|
|
$this->assertEquals('Maintainers', $data[0]->getGroupDesc()); |
121
|
|
|
$this->assertEquals(1000, $data[1]->getGroupAuthorId()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws AuthException |
127
|
|
|
* @throws LockException |
128
|
|
|
*/ |
129
|
|
|
public function testRemoveGroupOnEmptyInstance(): void |
130
|
|
|
{ |
131
|
|
|
$lib = $this->emptyGroupSources(); |
132
|
|
|
|
133
|
|
|
// delete |
134
|
|
|
$lib->deleteGroup(41); |
135
|
|
|
$this->assertNull($lib->getGroupDataOnly(41)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Contains a full comedy/tragedy of work with locks |
140
|
|
|
* @throws LockException |
141
|
|
|
* @throws AuthException |
142
|
|
|
* @return Groups |
143
|
|
|
*/ |
144
|
|
|
protected function groupSources(): Groups |
145
|
|
|
{ |
146
|
|
|
$storage = new Storage(new DefaultKey(), new Memory()); |
147
|
|
|
$file = new Groups( |
148
|
|
|
$storage, |
149
|
|
|
$this->getLockPath(), |
150
|
|
|
$this->sourcePath |
151
|
|
|
); |
152
|
|
|
$storage->write($this->sourcePath, |
153
|
|
|
'0:root:1000:Maintainers:' . "\r\n" |
154
|
|
|
. '1:admin:1000:Administrators:' . "\r\n" |
155
|
|
|
. '# commented out' . "\r\n" |
156
|
|
|
. '2:user:1000:All users:' . "\r\n" |
157
|
|
|
// last line is intentionally empty one |
158
|
|
|
); |
159
|
|
|
return $file; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Contains a full comedy/tragedy of work with locks |
164
|
|
|
* @throws LockException |
165
|
|
|
* @return Groups |
166
|
|
|
*/ |
167
|
|
|
protected function emptyGroupSources(): Groups |
168
|
|
|
{ |
169
|
|
|
return new Groups( |
170
|
|
|
new Storage(new DefaultKey(), new Memory()), |
171
|
|
|
$this->getLockPath(), |
172
|
|
|
$this->sourcePath |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
protected function wantedGroup($name = 'another'): FileGroup |
178
|
|
|
{ |
179
|
|
|
$user = new FileGroup(); |
180
|
|
|
$user->setData(3, $name, 1001, 'Testing group'); |
181
|
|
|
return $user; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|