|
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
|
|
|
use kalanis\kw_storage\StorageException; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class GroupsTest extends CommonTestClass |
|
18
|
|
|
{ |
|
19
|
|
|
protected $sourcePath = '.groups'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @throws AuthException |
|
23
|
|
|
* @throws LockException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testNotExistsData(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$lib = $this->emptyGroupSources(); |
|
28
|
|
|
$this->assertNull($lib->getGroupDataOnly(15)); |
|
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
|
|
|
* @throws StorageException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testGroupManipulation(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$lib = $this->groupSources(); |
|
71
|
|
|
$group = $this->wantedGroup(); |
|
72
|
|
|
|
|
73
|
|
|
// create |
|
74
|
|
|
$lib->createGroup($group); |
|
75
|
|
|
// check data |
|
76
|
|
|
$saved = $lib->getGroupDataOnly(3); |
|
77
|
|
|
$this->assertEquals('another', $saved->getGroupName()); |
|
78
|
|
|
$this->assertEquals('Testing group', $saved->getGroupDesc()); |
|
79
|
|
|
$this->assertEquals(1001, $saved->getGroupAuthorId()); |
|
80
|
|
|
|
|
81
|
|
|
// update |
|
82
|
|
|
$group->setData( |
|
83
|
|
|
$group->getGroupId(), |
|
84
|
|
|
$group->getGroupName(), |
|
85
|
|
|
1002, |
|
86
|
|
|
'WheĐn yoĐu dđo nođt knđow' |
|
87
|
|
|
); |
|
88
|
|
|
$lib->updateGroup($group); |
|
89
|
|
|
|
|
90
|
|
|
// check data - again with new values |
|
91
|
|
|
$saved = $lib->getGroupDataOnly(3); |
|
92
|
|
|
$this->assertEquals('When you do not know', $saved->getGroupDesc()); // overwrite this |
|
93
|
|
|
$this->assertEquals(1001, $saved->getGroupAuthorId()); // cannot overwrite this |
|
94
|
|
|
|
|
95
|
|
|
// remove |
|
96
|
|
|
$lib->deleteGroup($group->getGroupId()); |
|
97
|
|
|
// check for existence |
|
98
|
|
|
$this->assertEmpty($lib->getGroupDataOnly(3)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @throws AuthException |
|
103
|
|
|
* @throws LockException |
|
104
|
|
|
* @throws StorageException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testCreateFail(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$lib = $this->groupSources(); |
|
109
|
|
|
$group = $this->wantedGroup(''); |
|
110
|
|
|
$this->expectException(AuthException::class); |
|
111
|
|
|
$lib->createGroup($group); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws AuthException |
|
116
|
|
|
* @throws LockException |
|
117
|
|
|
* @throws StorageException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testAllGroups(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$lib = $this->groupSources(); |
|
122
|
|
|
$data = $lib->readGroup(); |
|
123
|
|
|
$this->assertEquals('Maintainers', $data[0]->getGroupDesc()); |
|
124
|
|
|
$this->assertEquals(1000, $data[1]->getGroupAuthorId()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @throws AuthException |
|
129
|
|
|
* @throws LockException |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testRemoveGroupOnEmptyInstance(): void |
|
132
|
|
|
{ |
|
133
|
|
|
$lib = $this->emptyGroupSources(); |
|
134
|
|
|
|
|
135
|
|
|
// delete |
|
136
|
|
|
$lib->deleteGroup(41); |
|
137
|
|
|
$this->assertNull($lib->getGroupDataOnly(41)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @throws AuthException |
|
142
|
|
|
* @throws LockException |
|
143
|
|
|
*/ |
|
144
|
|
|
public function testCreateStorageFail(): void |
|
145
|
|
|
{ |
|
146
|
|
|
$lib = $this->failedGroupSources(); |
|
147
|
|
|
$group = $this->wantedGroup(''); |
|
148
|
|
|
$this->expectException(AuthException::class); |
|
149
|
|
|
$lib->createGroup($group); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @throws AuthException |
|
154
|
|
|
* @throws LockException |
|
155
|
|
|
*/ |
|
156
|
|
|
public function testCreateStorageFailSave(): void |
|
157
|
|
|
{ |
|
158
|
|
|
$lib = $this->failedGroupSources(true); |
|
159
|
|
|
$group = $this->wantedGroup(''); |
|
160
|
|
|
$this->expectException(AuthException::class); |
|
161
|
|
|
$lib->createGroup($group); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @throws AuthException |
|
166
|
|
|
* @throws LockException |
|
167
|
|
|
*/ |
|
168
|
|
|
public function testReadGroupsStorageFail(): void |
|
169
|
|
|
{ |
|
170
|
|
|
$lib = $this->failedGroupSources(); |
|
171
|
|
|
$this->expectException(AuthException::class); |
|
172
|
|
|
$lib->readGroup(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @throws AuthException |
|
177
|
|
|
* @throws LockException |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testUpdateGroupStorageFail(): void |
|
180
|
|
|
{ |
|
181
|
|
|
$lib = $this->failedGroupSources(); |
|
182
|
|
|
$this->expectException(AuthException::class); |
|
183
|
|
|
$lib->updateGroup($this->wantedGroup()); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @throws AuthException |
|
188
|
|
|
* @throws LockException |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testUpdateGroupStorageFailSave(): void |
|
191
|
|
|
{ |
|
192
|
|
|
$lib = $this->failedGroupSources(true); |
|
193
|
|
|
$this->expectException(AuthException::class); |
|
194
|
|
|
$lib->updateGroup($this->wantedGroup()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @throws AuthException |
|
199
|
|
|
* @throws LockException |
|
200
|
|
|
*/ |
|
201
|
|
|
public function testRemoveGroupStorageFail(): void |
|
202
|
|
|
{ |
|
203
|
|
|
$lib = $this->failedGroupSources(); |
|
204
|
|
|
$this->assertNull($lib->deleteGroup(41)); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @throws AuthException |
|
209
|
|
|
* @throws LockException |
|
210
|
|
|
*/ |
|
211
|
|
|
public function testRemoveGroupStorageFailSave(): void |
|
212
|
|
|
{ |
|
213
|
|
|
$lib = $this->failedGroupSources(true, '0:all:1000:Main:' . "\r\n" . '2:folks:1000:Dirt:' . "\r\n"); |
|
214
|
|
|
$this->expectException(AuthException::class); |
|
215
|
|
|
$lib->deleteGroup(2); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Contains a full comedy/tragedy of work with locks |
|
220
|
|
|
* @throws LockException |
|
221
|
|
|
* @throws StorageException |
|
222
|
|
|
* @return Groups |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function groupSources(): Groups |
|
225
|
|
|
{ |
|
226
|
|
|
$storage = new Storage(new DefaultKey(), new Memory()); |
|
227
|
|
|
$file = new Groups( |
|
228
|
|
|
$storage, |
|
229
|
|
|
$this->getLockPath(), |
|
230
|
|
|
$this->sourcePath |
|
231
|
|
|
); |
|
232
|
|
|
$storage->write($this->sourcePath, |
|
233
|
|
|
'0:root:1000:Maintainers:' . "\r\n" |
|
234
|
|
|
. '1:admin:1000:Administrators:' . "\r\n" |
|
235
|
|
|
. '# commented out' . "\r\n" |
|
236
|
|
|
. '2:user:1000:All users:' . "\r\n" |
|
237
|
|
|
// last line is intentionally empty one |
|
238
|
|
|
); |
|
239
|
|
|
return $file; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @throws LockException |
|
244
|
|
|
* @return Groups |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function emptyGroupSources(): Groups |
|
247
|
|
|
{ |
|
248
|
|
|
return new Groups( |
|
249
|
|
|
new Storage(new DefaultKey(), new Memory()), |
|
250
|
|
|
$this->getLockPath(), |
|
251
|
|
|
$this->sourcePath |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param bool $canOpen |
|
257
|
|
|
* @param string $content |
|
258
|
|
|
* @throws LockException |
|
259
|
|
|
* @return Groups |
|
260
|
|
|
*/ |
|
261
|
|
|
protected function failedGroupSources(bool $canOpen = false, string $content = ''): Groups |
|
262
|
|
|
{ |
|
263
|
|
|
return new Groups( |
|
264
|
|
|
new \XFailedStorage($canOpen, $content), |
|
265
|
|
|
$this->getLockPath(), |
|
266
|
|
|
$this->sourcePath |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
protected function wantedGroup($name = 'another'): FileGroup |
|
271
|
|
|
{ |
|
272
|
|
|
$user = new FileGroup(); |
|
273
|
|
|
$user->setData(3, $name, 1001, 'Testing group'); |
|
274
|
|
|
return $user; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.