@@ -41,1000 +41,1000 @@ |
||
41 | 41 | * @package Test\Files\Node |
42 | 42 | */ |
43 | 43 | class FolderTest extends NodeTestCase { |
44 | - protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) { |
|
45 | - $view->expects($this->any()) |
|
46 | - ->method('getRoot') |
|
47 | - ->willReturn(''); |
|
48 | - if ($data || $internalPath || $storage) { |
|
49 | - return new Folder($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); |
|
50 | - } else { |
|
51 | - return new Folder($root, $view, $path); |
|
52 | - } |
|
53 | - } |
|
54 | - |
|
55 | - protected function getNodeClass() { |
|
56 | - return '\OC\Files\Node\Folder'; |
|
57 | - } |
|
58 | - |
|
59 | - protected function getNonExistingNodeClass() { |
|
60 | - return '\OC\Files\Node\NonExistingFolder'; |
|
61 | - } |
|
62 | - |
|
63 | - protected function getViewDeleteMethod() { |
|
64 | - return 'rmdir'; |
|
65 | - } |
|
66 | - |
|
67 | - public function testGetDirectoryContent(): void { |
|
68 | - $manager = $this->createMock(Manager::class); |
|
69 | - /** |
|
70 | - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view |
|
71 | - */ |
|
72 | - $root = $this->getMockBuilder(Root::class) |
|
73 | - ->setConstructorArgs([$manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
74 | - ->getMock(); |
|
75 | - $root->expects($this->any()) |
|
76 | - ->method('getUser') |
|
77 | - ->willReturn($this->user); |
|
78 | - |
|
79 | - $this->view->expects($this->any()) |
|
80 | - ->method('getDirectoryContent') |
|
81 | - ->with('/bar/foo') |
|
82 | - ->willReturn([ |
|
83 | - new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null), |
|
84 | - new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null), |
|
85 | - ]); |
|
86 | - $this->view->method('getFileInfo') |
|
87 | - ->willReturn($this->createMock(FileInfo::class)); |
|
88 | - $this->view->method('getRelativePath') |
|
89 | - ->willReturn('/bar/foo'); |
|
90 | - |
|
91 | - $node = new Folder($root, $this->view, '/bar/foo'); |
|
92 | - $children = $node->getDirectoryListing(); |
|
93 | - $this->assertEquals(2, count($children)); |
|
94 | - $this->assertInstanceOf('\OC\Files\Node\File', $children[0]); |
|
95 | - $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]); |
|
96 | - $this->assertEquals('asd', $children[0]->getName()); |
|
97 | - $this->assertEquals('qwerty', $children[1]->getName()); |
|
98 | - $this->assertEquals(2, $children[0]->getId()); |
|
99 | - $this->assertEquals(3, $children[1]->getId()); |
|
100 | - } |
|
101 | - |
|
102 | - public function testGet(): void { |
|
103 | - $manager = $this->createMock(Manager::class); |
|
104 | - $view = $this->getRootViewMock(); |
|
105 | - $root = $this->getMockBuilder(Root::class) |
|
106 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
107 | - ->getMock(); |
|
108 | - $root->expects($this->any()) |
|
109 | - ->method('getUser') |
|
110 | - ->willReturn($this->user); |
|
111 | - |
|
112 | - $node = new File($root, $view, '/bar/foo/asd'); |
|
113 | - $root->method('get') |
|
114 | - ->with('/bar/foo/asd') |
|
115 | - ->willReturn($node); |
|
116 | - |
|
117 | - $parentNode = new Folder($root, $view, '/bar/foo'); |
|
118 | - self::assertEquals($node, $parentNode->get('asd')); |
|
119 | - } |
|
120 | - |
|
121 | - public function testNodeExists(): void { |
|
122 | - $manager = $this->createMock(Manager::class); |
|
123 | - $view = $this->getRootViewMock(); |
|
124 | - $root = $this->getMockBuilder(Root::class) |
|
125 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
126 | - ->getMock(); |
|
127 | - $root->expects($this->any()) |
|
128 | - ->method('getUser') |
|
129 | - ->willReturn($this->user); |
|
130 | - |
|
131 | - $child = new Folder($root, $view, '/bar/foo/asd'); |
|
132 | - |
|
133 | - $root->method('get') |
|
134 | - ->with('/bar/foo/asd') |
|
135 | - ->willReturn($child); |
|
136 | - |
|
137 | - $node = new Folder($root, $view, '/bar/foo'); |
|
138 | - $this->assertTrue($node->nodeExists('asd')); |
|
139 | - } |
|
140 | - |
|
141 | - public function testNodeExistsNotExists(): void { |
|
142 | - $manager = $this->createMock(Manager::class); |
|
143 | - $view = $this->getRootViewMock(); |
|
144 | - $root = $this->getMockBuilder(Root::class) |
|
145 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
146 | - ->getMock(); |
|
147 | - $root->expects($this->any()) |
|
148 | - ->method('getUser') |
|
149 | - ->willReturn($this->user); |
|
150 | - |
|
151 | - $root->method('get') |
|
152 | - ->with('/bar/foo/asd') |
|
153 | - ->will($this->throwException(new NotFoundException())); |
|
154 | - |
|
155 | - $node = new Folder($root, $view, '/bar/foo'); |
|
156 | - $this->assertFalse($node->nodeExists('asd')); |
|
157 | - } |
|
158 | - |
|
159 | - public function testNewFolder(): void { |
|
160 | - $manager = $this->createMock(Manager::class); |
|
161 | - $view = $this->getRootViewMock(); |
|
162 | - $root = $this->getMockBuilder(Root::class) |
|
163 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
164 | - ->getMock(); |
|
165 | - $root->expects($this->any()) |
|
166 | - ->method('getUser') |
|
167 | - ->willReturn($this->user); |
|
168 | - |
|
169 | - $view->method('getFileInfo') |
|
170 | - ->with('/bar/foo') |
|
171 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
172 | - |
|
173 | - $view->method('mkdir') |
|
174 | - ->with('/bar/foo/asd') |
|
175 | - ->willReturn(true); |
|
176 | - |
|
177 | - $node = new Folder($root, $view, '/bar/foo'); |
|
178 | - $child = new Folder($root, $view, '/bar/foo/asd', null, $node); |
|
179 | - $result = $node->newFolder('asd'); |
|
180 | - $this->assertEquals($child, $result); |
|
181 | - } |
|
182 | - |
|
183 | - public function testNewFolderDeepParent(): void { |
|
184 | - $manager = $this->createMock(Manager::class); |
|
185 | - $view = $this->getRootViewMock(); |
|
186 | - $root = $this->getMockBuilder(Root::class) |
|
187 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
188 | - ->getMock(); |
|
189 | - $root->expects($this->any()) |
|
190 | - ->method('getUser') |
|
191 | - ->willReturn($this->user); |
|
192 | - |
|
193 | - $view->method('getFileInfo') |
|
194 | - ->with('/foobar') |
|
195 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
196 | - |
|
197 | - $view->method('mkdir') |
|
198 | - ->with('/foobar/asd/sdf') |
|
199 | - ->willReturn(true); |
|
200 | - |
|
201 | - $node = new Folder($root, $view, '/foobar'); |
|
202 | - $child = new Folder($root, $view, '/foobar/asd/sdf', null, null); |
|
203 | - $result = $node->newFolder('asd/sdf'); |
|
204 | - $this->assertEquals($child, $result); |
|
205 | - } |
|
206 | - |
|
207 | - |
|
208 | - public function testNewFolderNotPermitted(): void { |
|
209 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
210 | - |
|
211 | - $manager = $this->createMock(Manager::class); |
|
212 | - $view = $this->getRootViewMock(); |
|
213 | - $root = $this->getMockBuilder(Root::class) |
|
214 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
215 | - ->getMock(); |
|
216 | - $root->method('getUser') |
|
217 | - ->willReturn($this->user); |
|
218 | - |
|
219 | - $view->method('getFileInfo') |
|
220 | - ->with('/bar/foo') |
|
221 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
222 | - |
|
223 | - $node = new Folder($root, $view, '/bar/foo'); |
|
224 | - $node->newFolder('asd'); |
|
225 | - } |
|
226 | - |
|
227 | - public function testNewFile(): void { |
|
228 | - $manager = $this->createMock(Manager::class); |
|
229 | - $view = $this->getRootViewMock(); |
|
230 | - $root = $this->getMockBuilder(Root::class) |
|
231 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
232 | - ->getMock(); |
|
233 | - $root->expects($this->any()) |
|
234 | - ->method('getUser') |
|
235 | - ->willReturn($this->user); |
|
236 | - |
|
237 | - $view->method('getFileInfo') |
|
238 | - ->with('/bar/foo') |
|
239 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
240 | - |
|
241 | - $view->method('touch') |
|
242 | - ->with('/bar/foo/asd') |
|
243 | - ->willReturn(true); |
|
244 | - |
|
245 | - $node = new Folder($root, $view, '/bar/foo'); |
|
246 | - $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd', null, $node); |
|
247 | - $result = $node->newFile('asd'); |
|
248 | - $this->assertEquals($child, $result); |
|
249 | - } |
|
250 | - |
|
251 | - |
|
252 | - public function testNewFileNotPermitted(): void { |
|
253 | - $this->expectException(\OCP\Files\NotPermittedException::class); |
|
254 | - |
|
255 | - $manager = $this->createMock(Manager::class); |
|
256 | - $view = $this->getRootViewMock(); |
|
257 | - $root = $this->getMockBuilder(Root::class) |
|
258 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
259 | - ->getMock(); |
|
260 | - $root->method('getUser') |
|
261 | - ->willReturn($this->user); |
|
262 | - |
|
263 | - $view->method('getFileInfo') |
|
264 | - ->with('/bar/foo') |
|
265 | - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
266 | - |
|
267 | - $node = new Folder($root, $view, '/bar/foo'); |
|
268 | - $node->newFile('asd'); |
|
269 | - } |
|
270 | - |
|
271 | - public function testGetFreeSpace(): void { |
|
272 | - $manager = $this->createMock(Manager::class); |
|
273 | - $view = $this->getRootViewMock(); |
|
274 | - $root = $this->getMockBuilder(Root::class) |
|
275 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
276 | - ->getMock(); |
|
277 | - $root->method('getUser') |
|
278 | - ->willReturn($this->user); |
|
279 | - |
|
280 | - $view->method('free_space') |
|
281 | - ->with('/bar/foo') |
|
282 | - ->willReturn(100); |
|
283 | - |
|
284 | - $node = new Folder($root, $view, '/bar/foo'); |
|
285 | - $this->assertEquals(100, $node->getFreeSpace()); |
|
286 | - } |
|
287 | - |
|
288 | - public function testSearch(): void { |
|
289 | - $manager = $this->createMock(Manager::class); |
|
290 | - $view = $this->getRootViewMock(); |
|
291 | - $root = $this->getMockBuilder(Root::class) |
|
292 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
293 | - ->getMock(); |
|
294 | - $root->method('getUser') |
|
295 | - ->willReturn($this->user); |
|
296 | - /** @var Storage\IStorage&MockObject $storage */ |
|
297 | - $storage = $this->createMock(IStorage::class); |
|
298 | - $storage->method('getId')->willReturn('test::1'); |
|
299 | - $cache = new Cache($storage); |
|
300 | - |
|
301 | - $storage->method('getCache') |
|
302 | - ->willReturn($cache); |
|
303 | - |
|
304 | - $storage->expects($this->atLeastOnce()) |
|
305 | - ->method('getOwner') |
|
306 | - ->with('qwerty') |
|
307 | - ->willReturn(false); |
|
308 | - |
|
309 | - $mount = $this->createMock(IMountPoint::class); |
|
310 | - $mount->expects($this->atLeastOnce()) |
|
311 | - ->method('getStorage') |
|
312 | - ->willReturn($storage); |
|
313 | - $mount->expects($this->atLeastOnce()) |
|
314 | - ->method('getInternalPath') |
|
315 | - ->willReturn('foo'); |
|
316 | - |
|
317 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
318 | - $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
319 | - $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
320 | - |
|
321 | - $root->method('getMountsIn') |
|
322 | - ->with('/bar/foo') |
|
323 | - ->willReturn([]); |
|
324 | - |
|
325 | - $root->method('getMount') |
|
326 | - ->with('/bar/foo') |
|
327 | - ->willReturn($mount); |
|
328 | - |
|
329 | - $node = new Folder($root, $view, '/bar/foo'); |
|
330 | - $result = $node->search('qw'); |
|
331 | - $cache->clear(); |
|
332 | - $this->assertEquals(1, count($result)); |
|
333 | - $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
334 | - } |
|
335 | - |
|
336 | - public function testSearchInRoot(): void { |
|
337 | - $manager = $this->createMock(Manager::class); |
|
338 | - $view = $this->getRootViewMock(); |
|
339 | - $root = $this->getMockBuilder(Root::class) |
|
340 | - ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
341 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
342 | - ->getMock(); |
|
343 | - $root->expects($this->any()) |
|
344 | - ->method('getUser') |
|
345 | - ->willReturn($this->user); |
|
346 | - /** @var \PHPUnit\Framework\MockObject\MockObject|Storage $storage */ |
|
347 | - $storage = $this->createMock(IStorage::class); |
|
348 | - $storage->method('getId')->willReturn('test::2'); |
|
349 | - $cache = new Cache($storage); |
|
350 | - |
|
351 | - $mount = $this->createMock(IMountPoint::class); |
|
352 | - $mount->method('getStorage') |
|
353 | - ->willReturn($storage); |
|
354 | - $mount->method('getInternalPath') |
|
355 | - ->willReturn('files'); |
|
356 | - |
|
357 | - $storage->method('getCache') |
|
358 | - ->willReturn($cache); |
|
359 | - $storage->method('getOwner') |
|
360 | - ->willReturn('owner'); |
|
361 | - |
|
362 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
363 | - $cache->insert('files', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
364 | - $cache->insert('files/foo', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
365 | - |
|
366 | - $root->method('getMountsIn') |
|
367 | - ->with('') |
|
368 | - ->willReturn([]); |
|
369 | - |
|
370 | - $root->method('getMount') |
|
371 | - ->with('') |
|
372 | - ->willReturn($mount); |
|
373 | - |
|
374 | - $result = $root->search('foo'); |
|
375 | - $cache->clear(); |
|
376 | - $this->assertEquals(1, count($result)); |
|
377 | - $this->assertEquals('/foo', $result[0]->getPath()); |
|
378 | - } |
|
379 | - |
|
380 | - public function testSearchInStorageRoot(): void { |
|
381 | - $manager = $this->createMock(Manager::class); |
|
382 | - $view = $this->getRootViewMock(); |
|
383 | - $root = $this->getMockBuilder(Root::class) |
|
384 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
385 | - ->getMock(); |
|
386 | - $root->method('getUser') |
|
387 | - ->willReturn($this->user); |
|
388 | - $storage = $this->createMock(IStorage::class); |
|
389 | - $storage->method('getId')->willReturn('test::1'); |
|
390 | - $cache = new Cache($storage); |
|
391 | - |
|
392 | - $mount = $this->createMock(IMountPoint::class); |
|
393 | - $mount->method('getStorage') |
|
394 | - ->willReturn($storage); |
|
395 | - $mount->method('getInternalPath') |
|
396 | - ->willReturn(''); |
|
397 | - |
|
398 | - $storage->method('getCache') |
|
399 | - ->willReturn($cache); |
|
400 | - $storage->method('getOwner') |
|
401 | - ->willReturn('owner'); |
|
402 | - |
|
403 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
404 | - $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
405 | - $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
406 | - |
|
407 | - |
|
408 | - $root->method('getMountsIn') |
|
409 | - ->with('/bar') |
|
410 | - ->willReturn([]); |
|
411 | - |
|
412 | - $root->method('getMount') |
|
413 | - ->with('/bar') |
|
414 | - ->willReturn($mount); |
|
415 | - |
|
416 | - $node = new Folder($root, $view, '/bar'); |
|
417 | - $result = $node->search('qw'); |
|
418 | - $cache->clear(); |
|
419 | - $this->assertEquals(1, count($result)); |
|
420 | - $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
421 | - } |
|
422 | - |
|
423 | - public function testSearchSubStorages(): void { |
|
424 | - $manager = $this->createMock(Manager::class); |
|
425 | - $view = $this->getRootViewMock(); |
|
426 | - $root = $this->getMockBuilder(Root::class) |
|
427 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
428 | - ->getMock(); |
|
429 | - $root->expects($this->any()) |
|
430 | - ->method('getUser') |
|
431 | - ->willReturn($this->user); |
|
432 | - $storage = $this->createMock(IStorage::class); |
|
433 | - $storage->method('getId')->willReturn('test::1'); |
|
434 | - $cache = new Cache($storage); |
|
435 | - $subStorage = $this->createMock(IStorage::class); |
|
436 | - $subStorage->method('getId')->willReturn('test::2'); |
|
437 | - $subCache = new Cache($subStorage); |
|
438 | - $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); |
|
439 | - |
|
440 | - $mount = $this->createMock(IMountPoint::class); |
|
441 | - $mount->method('getStorage') |
|
442 | - ->willReturn($storage); |
|
443 | - $mount->method('getInternalPath') |
|
444 | - ->willReturn('foo'); |
|
445 | - |
|
446 | - $subMount->method('getStorage') |
|
447 | - ->willReturn($subStorage); |
|
448 | - |
|
449 | - $subMount->method('getMountPoint') |
|
450 | - ->willReturn('/bar/foo/bar/'); |
|
451 | - |
|
452 | - $storage->method('getCache') |
|
453 | - ->willReturn($cache); |
|
454 | - $storage->method('getOwner') |
|
455 | - ->willReturn('owner'); |
|
456 | - |
|
457 | - $subStorage->method('getCache') |
|
458 | - ->willReturn($subCache); |
|
459 | - $subStorage->method('getOwner') |
|
460 | - ->willReturn('owner'); |
|
461 | - |
|
462 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
463 | - $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
464 | - $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
465 | - |
|
466 | - $subCache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
467 | - $subCache->insert('asd', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
468 | - $subCache->insert('asd/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
469 | - |
|
470 | - |
|
471 | - $root->method('getMountsIn') |
|
472 | - ->with('/bar/foo') |
|
473 | - ->willReturn([$subMount]); |
|
474 | - |
|
475 | - $root->method('getMount') |
|
476 | - ->with('/bar/foo') |
|
477 | - ->willReturn($mount); |
|
478 | - |
|
479 | - |
|
480 | - $node = new Folder($root, $view, '/bar/foo'); |
|
481 | - $result = $node->search('qw'); |
|
482 | - $cache->clear(); |
|
483 | - $subCache->clear(); |
|
484 | - $this->assertEquals(2, count($result)); |
|
485 | - } |
|
486 | - |
|
487 | - public function testIsSubNode(): void { |
|
488 | - $rootFolderMock = $this->createMock(IRootFolder::class); |
|
489 | - $file = new Node($rootFolderMock, $this->view, '/foo/bar'); |
|
490 | - $folder = new Folder($rootFolderMock, $this->view, '/foo'); |
|
491 | - $this->assertTrue($folder->isSubNode($file)); |
|
492 | - $this->assertFalse($folder->isSubNode($folder)); |
|
493 | - |
|
494 | - $file = new Node($rootFolderMock, $this->view, '/foobar'); |
|
495 | - $this->assertFalse($folder->isSubNode($file)); |
|
496 | - } |
|
497 | - |
|
498 | - public function testGetById(): void { |
|
499 | - $manager = $this->createMock(Manager::class); |
|
500 | - $view = $this->getRootViewMock(); |
|
501 | - $root = $this->getMockBuilder(Root::class) |
|
502 | - ->onlyMethods(['getMountsIn', 'getMount']) |
|
503 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
504 | - ->getMock(); |
|
505 | - $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
506 | - $mount = new MountPoint($storage, '/bar'); |
|
507 | - $storage->method('getId')->willReturn(''); |
|
508 | - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
509 | - |
|
510 | - $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null); |
|
511 | - |
|
512 | - $storage->method('getCache') |
|
513 | - ->willReturn($cache); |
|
514 | - $storage->method('getOwner') |
|
515 | - ->willReturn('owner'); |
|
516 | - |
|
517 | - $this->userMountCache->expects($this->any()) |
|
518 | - ->method('getMountsForFileId') |
|
519 | - ->with(1) |
|
520 | - ->willReturn([new CachedMountInfo( |
|
521 | - $this->user, |
|
522 | - 1, |
|
523 | - 0, |
|
524 | - '/bar/', |
|
525 | - 'test', |
|
526 | - 1, |
|
527 | - '' |
|
528 | - )]); |
|
529 | - |
|
530 | - $cache->method('get') |
|
531 | - ->with(1) |
|
532 | - ->willReturn($fileInfo); |
|
533 | - |
|
534 | - $root->method('getMountsIn') |
|
535 | - ->with('/bar/foo') |
|
536 | - ->willReturn([]); |
|
537 | - |
|
538 | - $manager->method('getMountsByMountProvider') |
|
539 | - ->willReturn([$mount]); |
|
540 | - |
|
541 | - $node = new Folder($root, $view, '/bar/foo'); |
|
542 | - $result = $node->getById(1); |
|
543 | - $this->assertEquals(1, count($result)); |
|
544 | - $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
545 | - } |
|
546 | - |
|
547 | - public function testGetByIdMountRoot(): void { |
|
548 | - $manager = $this->createMock(Manager::class); |
|
549 | - $view = $this->getRootViewMock(); |
|
550 | - $root = $this->getMockBuilder(Root::class) |
|
551 | - ->onlyMethods(['getMountsIn', 'getMount']) |
|
552 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
553 | - ->getMock(); |
|
554 | - $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
555 | - $mount = new MountPoint($storage, '/bar'); |
|
556 | - $storage->method('getId')->willReturn(''); |
|
557 | - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
558 | - |
|
559 | - $fileInfo = new CacheEntry(['path' => '', 'mimetype' => 'text/plain'], null); |
|
560 | - |
|
561 | - $storage->method('getCache') |
|
562 | - ->willReturn($cache); |
|
563 | - $storage->method('getOwner') |
|
564 | - ->willReturn('owner'); |
|
565 | - |
|
566 | - $this->userMountCache->expects($this->any()) |
|
567 | - ->method('getMountsForFileId') |
|
568 | - ->with(1) |
|
569 | - ->willReturn([new CachedMountInfo( |
|
570 | - $this->user, |
|
571 | - 1, |
|
572 | - 0, |
|
573 | - '/bar/', |
|
574 | - 'test', |
|
575 | - 1, |
|
576 | - '' |
|
577 | - )]); |
|
578 | - |
|
579 | - $cache->method('get') |
|
580 | - ->with(1) |
|
581 | - ->willReturn($fileInfo); |
|
582 | - |
|
583 | - $manager->method('getMountsByMountProvider') |
|
584 | - ->willReturn([$mount]); |
|
585 | - |
|
586 | - $node = new Folder($root, $view, '/bar'); |
|
587 | - $result = $node->getById(1); |
|
588 | - $this->assertEquals(1, count($result)); |
|
589 | - $this->assertEquals('/bar', $result[0]->getPath()); |
|
590 | - } |
|
591 | - |
|
592 | - public function testGetByIdOutsideFolder(): void { |
|
593 | - $manager = $this->createMock(Manager::class); |
|
594 | - $view = $this->getRootViewMock(); |
|
595 | - $root = $this->getMockBuilder(Root::class) |
|
596 | - ->onlyMethods(['getMountsIn', 'getMount']) |
|
597 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
598 | - ->getMock(); |
|
599 | - $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
600 | - $mount = new MountPoint($storage, '/bar'); |
|
601 | - $storage->method('getId')->willReturn(''); |
|
602 | - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
603 | - |
|
604 | - $fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null); |
|
605 | - |
|
606 | - $storage->method('getCache') |
|
607 | - ->willReturn($cache); |
|
608 | - $storage->method('getOwner') |
|
609 | - ->willReturn('owner'); |
|
610 | - |
|
611 | - $this->userMountCache->expects($this->any()) |
|
612 | - ->method('getMountsForFileId') |
|
613 | - ->with(1) |
|
614 | - ->willReturn([new CachedMountInfo( |
|
615 | - $this->user, |
|
616 | - 1, |
|
617 | - 0, |
|
618 | - '/bar/', |
|
619 | - 'test', |
|
620 | - 1, |
|
621 | - '' |
|
622 | - )]); |
|
623 | - |
|
624 | - $cache->method('get') |
|
625 | - ->with(1) |
|
626 | - ->willReturn($fileInfo); |
|
627 | - |
|
628 | - $manager->method('getMountsByMountProvider') |
|
629 | - ->willReturn([$mount]); |
|
630 | - |
|
631 | - $node = new Folder($root, $view, '/bar/foo'); |
|
632 | - $result = $node->getById(1); |
|
633 | - $this->assertEquals(0, count($result)); |
|
634 | - } |
|
635 | - |
|
636 | - public function testGetByIdMultipleStorages(): void { |
|
637 | - $manager = $this->createMock(Manager::class); |
|
638 | - $view = $this->getRootViewMock(); |
|
639 | - $root = $this->getMockBuilder(Root::class) |
|
640 | - ->onlyMethods(['getMountsIn', 'getMount']) |
|
641 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
642 | - ->getMock(); |
|
643 | - $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
644 | - $mount1 = new MountPoint($storage, '/bar'); |
|
645 | - $mount2 = new MountPoint($storage, '/bar/foo/asd'); |
|
646 | - $storage->method('getId')->willReturn(''); |
|
647 | - $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
648 | - |
|
649 | - $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null); |
|
650 | - |
|
651 | - $storage->method('getCache') |
|
652 | - ->willReturn($cache); |
|
653 | - $storage->method('getOwner') |
|
654 | - ->willReturn('owner'); |
|
655 | - |
|
656 | - $this->userMountCache->method('getMountsForFileId') |
|
657 | - ->with(1) |
|
658 | - ->willReturn([ |
|
659 | - new CachedMountInfo( |
|
660 | - $this->user, |
|
661 | - 1, |
|
662 | - 0, |
|
663 | - '/bar/', |
|
664 | - 'test', |
|
665 | - 1, |
|
666 | - '' |
|
667 | - ), |
|
668 | - ]); |
|
669 | - |
|
670 | - $cache->method('get') |
|
671 | - ->with(1) |
|
672 | - ->willReturn($fileInfo); |
|
673 | - |
|
674 | - $manager->method('getMountsByMountProvider') |
|
675 | - ->willReturn([$mount1, $mount2]); |
|
676 | - |
|
677 | - $node = new Folder($root, $view, '/bar/foo'); |
|
678 | - $result = $node->getById(1); |
|
679 | - $this->assertEquals(2, count($result)); |
|
680 | - $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
681 | - $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath()); |
|
682 | - } |
|
683 | - |
|
684 | - public static function uniqueNameProvider(): array { |
|
685 | - return [ |
|
686 | - // input, existing, expected |
|
687 | - ['foo', [], 'foo'], |
|
688 | - ['foo', ['foo'], 'foo (2)'], |
|
689 | - ['foo', ['foo', 'foo (2)'], 'foo (3)'], |
|
690 | - ]; |
|
691 | - } |
|
692 | - |
|
693 | - /** |
|
694 | - * @dataProvider uniqueNameProvider |
|
695 | - */ |
|
696 | - public function testGetUniqueName($name, $existingFiles, $expected): void { |
|
697 | - $manager = $this->createMock(Manager::class); |
|
698 | - $folderPath = '/bar/foo'; |
|
699 | - $view = $this->getRootViewMock(); |
|
700 | - $root = $this->getMockBuilder(Root::class) |
|
701 | - ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
702 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
703 | - ->getMock(); |
|
704 | - |
|
705 | - $view->expects($this->any()) |
|
706 | - ->method('file_exists') |
|
707 | - ->willReturnCallback(function ($path) use ($existingFiles, $folderPath) { |
|
708 | - foreach ($existingFiles as $existing) { |
|
709 | - if ($folderPath . '/' . $existing === $path) { |
|
710 | - return true; |
|
711 | - } |
|
712 | - } |
|
713 | - return false; |
|
714 | - }); |
|
715 | - |
|
716 | - $node = new Folder($root, $view, $folderPath); |
|
717 | - $this->assertEquals($expected, $node->getNonExistingName($name)); |
|
718 | - } |
|
719 | - |
|
720 | - public function testRecent(): void { |
|
721 | - $manager = $this->createMock(Manager::class); |
|
722 | - $folderPath = '/bar/foo'; |
|
723 | - $view = $this->getRootViewMock(); |
|
724 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ |
|
725 | - $root = $this->getMockBuilder(Root::class) |
|
726 | - ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
727 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
728 | - ->getMock(); |
|
729 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ |
|
730 | - $folderInfo = $this->getMockBuilder(FileInfo::class) |
|
731 | - ->disableOriginalConstructor()->getMock(); |
|
732 | - |
|
733 | - $baseTime = time(); |
|
734 | - $storage = new Temporary(); |
|
735 | - $mount = new MountPoint($storage, ''); |
|
736 | - |
|
737 | - $folderInfo->expects($this->any()) |
|
738 | - ->method('getMountPoint') |
|
739 | - ->willReturn($mount); |
|
740 | - $root->method('getMount') |
|
741 | - ->willReturn($mount); |
|
742 | - $root->method('getMountsIn') |
|
743 | - ->willReturn([]); |
|
744 | - |
|
745 | - $cache = $storage->getCache(); |
|
746 | - |
|
747 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
748 | - $cache->insert('bar', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
749 | - $cache->insert('bar/foo', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
750 | - $cache->insert('bar/asd', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
751 | - $id1 = $cache->put('bar/foo/inside.txt', [ |
|
752 | - 'storage_mtime' => $baseTime, |
|
753 | - 'mtime' => $baseTime, |
|
754 | - 'mimetype' => 'text/plain', |
|
755 | - 'size' => 3, |
|
756 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
757 | - ]); |
|
758 | - $id2 = $cache->put('bar/foo/old.txt', [ |
|
759 | - 'storage_mtime' => $baseTime - 100, |
|
760 | - 'mtime' => $baseTime - 100, |
|
761 | - 'mimetype' => 'text/plain', |
|
762 | - 'size' => 3, |
|
763 | - 'permissions' => \OCP\Constants::PERMISSION_READ, |
|
764 | - ]); |
|
765 | - $cache->put('bar/asd/outside.txt', [ |
|
766 | - 'storage_mtime' => $baseTime, |
|
767 | - 'mtime' => $baseTime, |
|
768 | - 'mimetype' => 'text/plain', |
|
769 | - 'size' => 3, |
|
770 | - ]); |
|
771 | - $id3 = $cache->put('bar/foo/older.txt', [ |
|
772 | - 'storage_mtime' => $baseTime - 600, |
|
773 | - 'mtime' => $baseTime - 600, |
|
774 | - 'mimetype' => 'text/plain', |
|
775 | - 'size' => 3, |
|
776 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
777 | - ]); |
|
778 | - |
|
779 | - $node = new Folder($root, $view, $folderPath, $folderInfo); |
|
780 | - |
|
781 | - |
|
782 | - $nodes = $node->getRecent(5); |
|
783 | - $ids = array_map(function (Node $node) { |
|
784 | - return (int)$node->getId(); |
|
785 | - }, $nodes); |
|
786 | - $this->assertEquals([$id1, $id2, $id3], $ids); |
|
787 | - } |
|
788 | - |
|
789 | - public function testRecentFolder(): void { |
|
790 | - $manager = $this->createMock(Manager::class); |
|
791 | - $folderPath = '/bar/foo'; |
|
792 | - $view = $this->getRootViewMock(); |
|
793 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ |
|
794 | - $root = $this->getMockBuilder(Root::class) |
|
795 | - ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
796 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
797 | - ->getMock(); |
|
798 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ |
|
799 | - $folderInfo = $this->getMockBuilder(FileInfo::class) |
|
800 | - ->disableOriginalConstructor()->getMock(); |
|
801 | - |
|
802 | - $baseTime = time(); |
|
803 | - $storage = new Temporary(); |
|
804 | - $mount = new MountPoint($storage, ''); |
|
805 | - |
|
806 | - $folderInfo->expects($this->any()) |
|
807 | - ->method('getMountPoint') |
|
808 | - ->willReturn($mount); |
|
809 | - |
|
810 | - $root->method('getMount') |
|
811 | - ->willReturn($mount); |
|
812 | - $root->method('getMountsIn') |
|
813 | - ->willReturn([]); |
|
814 | - |
|
815 | - $cache = $storage->getCache(); |
|
816 | - |
|
817 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
818 | - $cache->insert('bar', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
819 | - $cache->insert('bar/foo', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
820 | - $id1 = $cache->put('bar/foo/folder', [ |
|
821 | - 'storage_mtime' => $baseTime, |
|
822 | - 'mtime' => $baseTime, |
|
823 | - 'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER, |
|
824 | - 'size' => 3, |
|
825 | - 'permissions' => 0, |
|
826 | - ]); |
|
827 | - $id2 = $cache->put('bar/foo/folder/bar.txt', [ |
|
828 | - 'storage_mtime' => $baseTime, |
|
829 | - 'mtime' => $baseTime, |
|
830 | - 'mimetype' => 'text/plain', |
|
831 | - 'size' => 3, |
|
832 | - 'parent' => $id1, |
|
833 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
834 | - ]); |
|
835 | - $id3 = $cache->put('bar/foo/folder/asd.txt', [ |
|
836 | - 'storage_mtime' => $baseTime - 100, |
|
837 | - 'mtime' => $baseTime - 100, |
|
838 | - 'mimetype' => 'text/plain', |
|
839 | - 'size' => 3, |
|
840 | - 'parent' => $id1, |
|
841 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
842 | - ]); |
|
843 | - |
|
844 | - $node = new Folder($root, $view, $folderPath, $folderInfo); |
|
845 | - |
|
846 | - |
|
847 | - $nodes = $node->getRecent(5); |
|
848 | - $ids = array_map(function (Node $node) { |
|
849 | - return (int)$node->getId(); |
|
850 | - }, $nodes); |
|
851 | - $this->assertEquals([$id2, $id3], $ids); |
|
852 | - $this->assertEquals($baseTime, $nodes[0]->getMTime()); |
|
853 | - $this->assertEquals($baseTime - 100, $nodes[1]->getMTime()); |
|
854 | - } |
|
855 | - |
|
856 | - public function testRecentJail(): void { |
|
857 | - $manager = $this->createMock(Manager::class); |
|
858 | - $folderPath = '/bar/foo'; |
|
859 | - $view = $this->getRootViewMock(); |
|
860 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ |
|
861 | - $root = $this->getMockBuilder(Root::class) |
|
862 | - ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
863 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
864 | - ->getMock(); |
|
865 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ |
|
866 | - $folderInfo = $this->getMockBuilder(FileInfo::class) |
|
867 | - ->disableOriginalConstructor()->getMock(); |
|
868 | - |
|
869 | - $baseTime = time(); |
|
870 | - $storage = new Temporary(); |
|
871 | - $jail = new Jail([ |
|
872 | - 'storage' => $storage, |
|
873 | - 'root' => 'folder', |
|
874 | - ]); |
|
875 | - $mount = new MountPoint($jail, '/bar/foo'); |
|
876 | - |
|
877 | - $folderInfo->expects($this->any()) |
|
878 | - ->method('getMountPoint') |
|
879 | - ->willReturn($mount); |
|
880 | - $root->method('getMount') |
|
881 | - ->willReturn($mount); |
|
882 | - $root->method('getMountsIn') |
|
883 | - ->willReturn([]); |
|
884 | - |
|
885 | - $cache = $storage->getCache(); |
|
886 | - |
|
887 | - $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
888 | - $cache->insert('folder', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
889 | - $id1 = $cache->put('folder/inside.txt', [ |
|
890 | - 'storage_mtime' => $baseTime, |
|
891 | - 'mtime' => $baseTime, |
|
892 | - 'mimetype' => 'text/plain', |
|
893 | - 'size' => 3, |
|
894 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
895 | - ]); |
|
896 | - |
|
897 | - $cache->put('outside.txt', [ |
|
898 | - 'storage_mtime' => $baseTime - 100, |
|
899 | - 'mtime' => $baseTime - 100, |
|
900 | - 'mimetype' => 'text/plain', |
|
901 | - 'size' => 3, |
|
902 | - ]); |
|
903 | - |
|
904 | - $node = new Folder($root, $view, $folderPath, $folderInfo); |
|
905 | - |
|
906 | - $nodes = $node->getRecent(5); |
|
907 | - $ids = array_map(function (Node $node) { |
|
908 | - return (int)$node->getId(); |
|
909 | - }, $nodes); |
|
910 | - $this->assertEquals([$id1], $ids); |
|
911 | - } |
|
912 | - |
|
913 | - public static function offsetLimitProvider(): array { |
|
914 | - return [ |
|
915 | - [0, 10, ['/bar/foo/foo1', '/bar/foo/foo2', '/bar/foo/foo3', '/bar/foo/foo4', '/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], |
|
916 | - [0, 5, ['/bar/foo/foo1', '/bar/foo/foo2', '/bar/foo/foo3', '/bar/foo/foo4', '/bar/foo/sub1/foo5'], []], |
|
917 | - [0, 2, ['/bar/foo/foo1', '/bar/foo/foo2'], []], |
|
918 | - [3, 2, ['/bar/foo/foo4', '/bar/foo/sub1/foo5'], []], |
|
919 | - [3, 5, ['/bar/foo/foo4', '/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], |
|
920 | - [5, 2, ['/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7'], []], |
|
921 | - [6, 2, ['/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], |
|
922 | - [7, 2, ['/bar/foo/sub2/foo8'], []], |
|
923 | - [10, 2, [], []], |
|
924 | - [0, 5, ['/bar/foo/sub2/foo7', '/bar/foo/foo1', '/bar/foo/sub1/foo5', '/bar/foo/foo2', '/bar/foo/foo3'], [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')]], |
|
925 | - [3, 2, ['/bar/foo/foo2', '/bar/foo/foo3'], [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')]], |
|
926 | - [0, 5, ['/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/foo1', '/bar/foo/foo2'], [ |
|
927 | - new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'size'), |
|
928 | - new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime') |
|
929 | - ]], |
|
930 | - ]; |
|
931 | - } |
|
932 | - |
|
933 | - /** |
|
934 | - * @dataProvider offsetLimitProvider |
|
935 | - * @param int $offset |
|
936 | - * @param int $limit |
|
937 | - * @param string[] $expectedPaths |
|
938 | - * @param ISearchOrder[] $ordering |
|
939 | - * @throws NotFoundException |
|
940 | - * @throws \OCP\Files\InvalidPathException |
|
941 | - */ |
|
942 | - public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array $expectedPaths, array $ordering): void { |
|
943 | - if (!$ordering) { |
|
944 | - $ordering = [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'fileid')]; |
|
945 | - } |
|
946 | - |
|
947 | - $manager = $this->createMock(Manager::class); |
|
948 | - $view = $this->getRootViewMock(); |
|
949 | - $root = $this->getMockBuilder(Root::class) |
|
950 | - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
951 | - ->getMock(); |
|
952 | - $root->expects($this->any()) |
|
953 | - ->method('getUser') |
|
954 | - ->willReturn($this->user); |
|
955 | - $storage = $this->createMock(IStorage::class); |
|
956 | - $storage->method('getId')->willReturn('test::1'); |
|
957 | - $cache = new Cache($storage); |
|
958 | - $subStorage1 = $this->createMock(IStorage::class); |
|
959 | - $subStorage1->method('getId')->willReturn('test::2'); |
|
960 | - $subCache1 = new Cache($subStorage1); |
|
961 | - $subMount1 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); |
|
962 | - $subStorage2 = $this->createMock(IStorage::class); |
|
963 | - $subStorage2->method('getId')->willReturn('test::3'); |
|
964 | - $subCache2 = new Cache($subStorage2); |
|
965 | - $subMount2 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); |
|
966 | - |
|
967 | - $mount = $this->createMock(IMountPoint::class); |
|
968 | - $mount->method('getStorage') |
|
969 | - ->willReturn($storage); |
|
970 | - $mount->method('getInternalPath') |
|
971 | - ->willReturn('foo'); |
|
972 | - |
|
973 | - $subMount1->method('getStorage') |
|
974 | - ->willReturn($subStorage1); |
|
975 | - |
|
976 | - $subMount1->method('getMountPoint') |
|
977 | - ->willReturn('/bar/foo/sub1/'); |
|
978 | - |
|
979 | - $storage->method('getCache') |
|
980 | - ->willReturn($cache); |
|
981 | - $storage->method('getOwner') |
|
982 | - ->willReturn('owner'); |
|
983 | - |
|
984 | - $subStorage1->method('getCache') |
|
985 | - ->willReturn($subCache1); |
|
986 | - $subStorage1->method('getOwner') |
|
987 | - ->willReturn('owner'); |
|
988 | - |
|
989 | - $subMount2->method('getStorage') |
|
990 | - ->willReturn($subStorage2); |
|
991 | - |
|
992 | - $subMount2->method('getMountPoint') |
|
993 | - ->willReturn('/bar/foo/sub2/'); |
|
994 | - |
|
995 | - $subStorage2->method('getCache') |
|
996 | - ->willReturn($subCache2); |
|
997 | - $subStorage2->method('getOwner') |
|
998 | - ->willReturn('owner'); |
|
999 | - |
|
1000 | - |
|
1001 | - $cache->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1002 | - $cache->insert('foo', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1003 | - $cache->insert('foo/foo1', ['size' => 200, 'mtime' => 10, 'mimetype' => 'text/plain']); |
|
1004 | - $cache->insert('foo/foo2', ['size' => 200, 'mtime' => 20, 'mimetype' => 'text/plain']); |
|
1005 | - $cache->insert('foo/foo3', ['size' => 200, 'mtime' => 30, 'mimetype' => 'text/plain']); |
|
1006 | - $cache->insert('foo/foo4', ['size' => 200, 'mtime' => 40, 'mimetype' => 'text/plain']); |
|
1007 | - |
|
1008 | - $subCache1->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1009 | - $subCache1->insert('foo5', ['size' => 300, 'mtime' => 15, 'mimetype' => 'text/plain']); |
|
1010 | - $subCache1->insert('foo6', ['size' => 300, 'mtime' => 50, 'mimetype' => 'text/plain']); |
|
1011 | - |
|
1012 | - $subCache2->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1013 | - $subCache2->insert('foo7', ['size' => 200, 'mtime' => 5, 'mimetype' => 'text/plain']); |
|
1014 | - $subCache2->insert('foo8', ['size' => 200, 'mtime' => 60, 'mimetype' => 'text/plain']); |
|
1015 | - |
|
1016 | - $root->method('getMountsIn') |
|
1017 | - ->with('/bar/foo') |
|
1018 | - ->willReturn([$subMount1, $subMount2]); |
|
1019 | - |
|
1020 | - $root->method('getMount') |
|
1021 | - ->with('/bar/foo') |
|
1022 | - ->willReturn($mount); |
|
1023 | - |
|
1024 | - $node = new Folder($root, $view, '/bar/foo'); |
|
1025 | - $comparison = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%foo%'); |
|
1026 | - $operator = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ |
|
1027 | - $comparison, |
|
1028 | - new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE)]), |
|
1029 | - ]); |
|
1030 | - $query = new SearchQuery($operator, $limit, $offset, $ordering); |
|
1031 | - $result = $node->search($query); |
|
1032 | - $cache->clear(); |
|
1033 | - $subCache1->clear(); |
|
1034 | - $subCache2->clear(); |
|
1035 | - $ids = array_map(function (Node $info) { |
|
1036 | - return $info->getPath(); |
|
1037 | - }, $result); |
|
1038 | - $this->assertEquals($expectedPaths, $ids); |
|
1039 | - } |
|
44 | + protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) { |
|
45 | + $view->expects($this->any()) |
|
46 | + ->method('getRoot') |
|
47 | + ->willReturn(''); |
|
48 | + if ($data || $internalPath || $storage) { |
|
49 | + return new Folder($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); |
|
50 | + } else { |
|
51 | + return new Folder($root, $view, $path); |
|
52 | + } |
|
53 | + } |
|
54 | + |
|
55 | + protected function getNodeClass() { |
|
56 | + return '\OC\Files\Node\Folder'; |
|
57 | + } |
|
58 | + |
|
59 | + protected function getNonExistingNodeClass() { |
|
60 | + return '\OC\Files\Node\NonExistingFolder'; |
|
61 | + } |
|
62 | + |
|
63 | + protected function getViewDeleteMethod() { |
|
64 | + return 'rmdir'; |
|
65 | + } |
|
66 | + |
|
67 | + public function testGetDirectoryContent(): void { |
|
68 | + $manager = $this->createMock(Manager::class); |
|
69 | + /** |
|
70 | + * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view |
|
71 | + */ |
|
72 | + $root = $this->getMockBuilder(Root::class) |
|
73 | + ->setConstructorArgs([$manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
74 | + ->getMock(); |
|
75 | + $root->expects($this->any()) |
|
76 | + ->method('getUser') |
|
77 | + ->willReturn($this->user); |
|
78 | + |
|
79 | + $this->view->expects($this->any()) |
|
80 | + ->method('getDirectoryContent') |
|
81 | + ->with('/bar/foo') |
|
82 | + ->willReturn([ |
|
83 | + new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null), |
|
84 | + new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null), |
|
85 | + ]); |
|
86 | + $this->view->method('getFileInfo') |
|
87 | + ->willReturn($this->createMock(FileInfo::class)); |
|
88 | + $this->view->method('getRelativePath') |
|
89 | + ->willReturn('/bar/foo'); |
|
90 | + |
|
91 | + $node = new Folder($root, $this->view, '/bar/foo'); |
|
92 | + $children = $node->getDirectoryListing(); |
|
93 | + $this->assertEquals(2, count($children)); |
|
94 | + $this->assertInstanceOf('\OC\Files\Node\File', $children[0]); |
|
95 | + $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]); |
|
96 | + $this->assertEquals('asd', $children[0]->getName()); |
|
97 | + $this->assertEquals('qwerty', $children[1]->getName()); |
|
98 | + $this->assertEquals(2, $children[0]->getId()); |
|
99 | + $this->assertEquals(3, $children[1]->getId()); |
|
100 | + } |
|
101 | + |
|
102 | + public function testGet(): void { |
|
103 | + $manager = $this->createMock(Manager::class); |
|
104 | + $view = $this->getRootViewMock(); |
|
105 | + $root = $this->getMockBuilder(Root::class) |
|
106 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
107 | + ->getMock(); |
|
108 | + $root->expects($this->any()) |
|
109 | + ->method('getUser') |
|
110 | + ->willReturn($this->user); |
|
111 | + |
|
112 | + $node = new File($root, $view, '/bar/foo/asd'); |
|
113 | + $root->method('get') |
|
114 | + ->with('/bar/foo/asd') |
|
115 | + ->willReturn($node); |
|
116 | + |
|
117 | + $parentNode = new Folder($root, $view, '/bar/foo'); |
|
118 | + self::assertEquals($node, $parentNode->get('asd')); |
|
119 | + } |
|
120 | + |
|
121 | + public function testNodeExists(): void { |
|
122 | + $manager = $this->createMock(Manager::class); |
|
123 | + $view = $this->getRootViewMock(); |
|
124 | + $root = $this->getMockBuilder(Root::class) |
|
125 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
126 | + ->getMock(); |
|
127 | + $root->expects($this->any()) |
|
128 | + ->method('getUser') |
|
129 | + ->willReturn($this->user); |
|
130 | + |
|
131 | + $child = new Folder($root, $view, '/bar/foo/asd'); |
|
132 | + |
|
133 | + $root->method('get') |
|
134 | + ->with('/bar/foo/asd') |
|
135 | + ->willReturn($child); |
|
136 | + |
|
137 | + $node = new Folder($root, $view, '/bar/foo'); |
|
138 | + $this->assertTrue($node->nodeExists('asd')); |
|
139 | + } |
|
140 | + |
|
141 | + public function testNodeExistsNotExists(): void { |
|
142 | + $manager = $this->createMock(Manager::class); |
|
143 | + $view = $this->getRootViewMock(); |
|
144 | + $root = $this->getMockBuilder(Root::class) |
|
145 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
146 | + ->getMock(); |
|
147 | + $root->expects($this->any()) |
|
148 | + ->method('getUser') |
|
149 | + ->willReturn($this->user); |
|
150 | + |
|
151 | + $root->method('get') |
|
152 | + ->with('/bar/foo/asd') |
|
153 | + ->will($this->throwException(new NotFoundException())); |
|
154 | + |
|
155 | + $node = new Folder($root, $view, '/bar/foo'); |
|
156 | + $this->assertFalse($node->nodeExists('asd')); |
|
157 | + } |
|
158 | + |
|
159 | + public function testNewFolder(): void { |
|
160 | + $manager = $this->createMock(Manager::class); |
|
161 | + $view = $this->getRootViewMock(); |
|
162 | + $root = $this->getMockBuilder(Root::class) |
|
163 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
164 | + ->getMock(); |
|
165 | + $root->expects($this->any()) |
|
166 | + ->method('getUser') |
|
167 | + ->willReturn($this->user); |
|
168 | + |
|
169 | + $view->method('getFileInfo') |
|
170 | + ->with('/bar/foo') |
|
171 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
172 | + |
|
173 | + $view->method('mkdir') |
|
174 | + ->with('/bar/foo/asd') |
|
175 | + ->willReturn(true); |
|
176 | + |
|
177 | + $node = new Folder($root, $view, '/bar/foo'); |
|
178 | + $child = new Folder($root, $view, '/bar/foo/asd', null, $node); |
|
179 | + $result = $node->newFolder('asd'); |
|
180 | + $this->assertEquals($child, $result); |
|
181 | + } |
|
182 | + |
|
183 | + public function testNewFolderDeepParent(): void { |
|
184 | + $manager = $this->createMock(Manager::class); |
|
185 | + $view = $this->getRootViewMock(); |
|
186 | + $root = $this->getMockBuilder(Root::class) |
|
187 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
188 | + ->getMock(); |
|
189 | + $root->expects($this->any()) |
|
190 | + ->method('getUser') |
|
191 | + ->willReturn($this->user); |
|
192 | + |
|
193 | + $view->method('getFileInfo') |
|
194 | + ->with('/foobar') |
|
195 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
196 | + |
|
197 | + $view->method('mkdir') |
|
198 | + ->with('/foobar/asd/sdf') |
|
199 | + ->willReturn(true); |
|
200 | + |
|
201 | + $node = new Folder($root, $view, '/foobar'); |
|
202 | + $child = new Folder($root, $view, '/foobar/asd/sdf', null, null); |
|
203 | + $result = $node->newFolder('asd/sdf'); |
|
204 | + $this->assertEquals($child, $result); |
|
205 | + } |
|
206 | + |
|
207 | + |
|
208 | + public function testNewFolderNotPermitted(): void { |
|
209 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
210 | + |
|
211 | + $manager = $this->createMock(Manager::class); |
|
212 | + $view = $this->getRootViewMock(); |
|
213 | + $root = $this->getMockBuilder(Root::class) |
|
214 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
215 | + ->getMock(); |
|
216 | + $root->method('getUser') |
|
217 | + ->willReturn($this->user); |
|
218 | + |
|
219 | + $view->method('getFileInfo') |
|
220 | + ->with('/bar/foo') |
|
221 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
222 | + |
|
223 | + $node = new Folder($root, $view, '/bar/foo'); |
|
224 | + $node->newFolder('asd'); |
|
225 | + } |
|
226 | + |
|
227 | + public function testNewFile(): void { |
|
228 | + $manager = $this->createMock(Manager::class); |
|
229 | + $view = $this->getRootViewMock(); |
|
230 | + $root = $this->getMockBuilder(Root::class) |
|
231 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
232 | + ->getMock(); |
|
233 | + $root->expects($this->any()) |
|
234 | + ->method('getUser') |
|
235 | + ->willReturn($this->user); |
|
236 | + |
|
237 | + $view->method('getFileInfo') |
|
238 | + ->with('/bar/foo') |
|
239 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); |
|
240 | + |
|
241 | + $view->method('touch') |
|
242 | + ->with('/bar/foo/asd') |
|
243 | + ->willReturn(true); |
|
244 | + |
|
245 | + $node = new Folder($root, $view, '/bar/foo'); |
|
246 | + $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd', null, $node); |
|
247 | + $result = $node->newFile('asd'); |
|
248 | + $this->assertEquals($child, $result); |
|
249 | + } |
|
250 | + |
|
251 | + |
|
252 | + public function testNewFileNotPermitted(): void { |
|
253 | + $this->expectException(\OCP\Files\NotPermittedException::class); |
|
254 | + |
|
255 | + $manager = $this->createMock(Manager::class); |
|
256 | + $view = $this->getRootViewMock(); |
|
257 | + $root = $this->getMockBuilder(Root::class) |
|
258 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
259 | + ->getMock(); |
|
260 | + $root->method('getUser') |
|
261 | + ->willReturn($this->user); |
|
262 | + |
|
263 | + $view->method('getFileInfo') |
|
264 | + ->with('/bar/foo') |
|
265 | + ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); |
|
266 | + |
|
267 | + $node = new Folder($root, $view, '/bar/foo'); |
|
268 | + $node->newFile('asd'); |
|
269 | + } |
|
270 | + |
|
271 | + public function testGetFreeSpace(): void { |
|
272 | + $manager = $this->createMock(Manager::class); |
|
273 | + $view = $this->getRootViewMock(); |
|
274 | + $root = $this->getMockBuilder(Root::class) |
|
275 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
276 | + ->getMock(); |
|
277 | + $root->method('getUser') |
|
278 | + ->willReturn($this->user); |
|
279 | + |
|
280 | + $view->method('free_space') |
|
281 | + ->with('/bar/foo') |
|
282 | + ->willReturn(100); |
|
283 | + |
|
284 | + $node = new Folder($root, $view, '/bar/foo'); |
|
285 | + $this->assertEquals(100, $node->getFreeSpace()); |
|
286 | + } |
|
287 | + |
|
288 | + public function testSearch(): void { |
|
289 | + $manager = $this->createMock(Manager::class); |
|
290 | + $view = $this->getRootViewMock(); |
|
291 | + $root = $this->getMockBuilder(Root::class) |
|
292 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
293 | + ->getMock(); |
|
294 | + $root->method('getUser') |
|
295 | + ->willReturn($this->user); |
|
296 | + /** @var Storage\IStorage&MockObject $storage */ |
|
297 | + $storage = $this->createMock(IStorage::class); |
|
298 | + $storage->method('getId')->willReturn('test::1'); |
|
299 | + $cache = new Cache($storage); |
|
300 | + |
|
301 | + $storage->method('getCache') |
|
302 | + ->willReturn($cache); |
|
303 | + |
|
304 | + $storage->expects($this->atLeastOnce()) |
|
305 | + ->method('getOwner') |
|
306 | + ->with('qwerty') |
|
307 | + ->willReturn(false); |
|
308 | + |
|
309 | + $mount = $this->createMock(IMountPoint::class); |
|
310 | + $mount->expects($this->atLeastOnce()) |
|
311 | + ->method('getStorage') |
|
312 | + ->willReturn($storage); |
|
313 | + $mount->expects($this->atLeastOnce()) |
|
314 | + ->method('getInternalPath') |
|
315 | + ->willReturn('foo'); |
|
316 | + |
|
317 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
318 | + $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
319 | + $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
320 | + |
|
321 | + $root->method('getMountsIn') |
|
322 | + ->with('/bar/foo') |
|
323 | + ->willReturn([]); |
|
324 | + |
|
325 | + $root->method('getMount') |
|
326 | + ->with('/bar/foo') |
|
327 | + ->willReturn($mount); |
|
328 | + |
|
329 | + $node = new Folder($root, $view, '/bar/foo'); |
|
330 | + $result = $node->search('qw'); |
|
331 | + $cache->clear(); |
|
332 | + $this->assertEquals(1, count($result)); |
|
333 | + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
334 | + } |
|
335 | + |
|
336 | + public function testSearchInRoot(): void { |
|
337 | + $manager = $this->createMock(Manager::class); |
|
338 | + $view = $this->getRootViewMock(); |
|
339 | + $root = $this->getMockBuilder(Root::class) |
|
340 | + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
341 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
342 | + ->getMock(); |
|
343 | + $root->expects($this->any()) |
|
344 | + ->method('getUser') |
|
345 | + ->willReturn($this->user); |
|
346 | + /** @var \PHPUnit\Framework\MockObject\MockObject|Storage $storage */ |
|
347 | + $storage = $this->createMock(IStorage::class); |
|
348 | + $storage->method('getId')->willReturn('test::2'); |
|
349 | + $cache = new Cache($storage); |
|
350 | + |
|
351 | + $mount = $this->createMock(IMountPoint::class); |
|
352 | + $mount->method('getStorage') |
|
353 | + ->willReturn($storage); |
|
354 | + $mount->method('getInternalPath') |
|
355 | + ->willReturn('files'); |
|
356 | + |
|
357 | + $storage->method('getCache') |
|
358 | + ->willReturn($cache); |
|
359 | + $storage->method('getOwner') |
|
360 | + ->willReturn('owner'); |
|
361 | + |
|
362 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
363 | + $cache->insert('files', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
364 | + $cache->insert('files/foo', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
365 | + |
|
366 | + $root->method('getMountsIn') |
|
367 | + ->with('') |
|
368 | + ->willReturn([]); |
|
369 | + |
|
370 | + $root->method('getMount') |
|
371 | + ->with('') |
|
372 | + ->willReturn($mount); |
|
373 | + |
|
374 | + $result = $root->search('foo'); |
|
375 | + $cache->clear(); |
|
376 | + $this->assertEquals(1, count($result)); |
|
377 | + $this->assertEquals('/foo', $result[0]->getPath()); |
|
378 | + } |
|
379 | + |
|
380 | + public function testSearchInStorageRoot(): void { |
|
381 | + $manager = $this->createMock(Manager::class); |
|
382 | + $view = $this->getRootViewMock(); |
|
383 | + $root = $this->getMockBuilder(Root::class) |
|
384 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
385 | + ->getMock(); |
|
386 | + $root->method('getUser') |
|
387 | + ->willReturn($this->user); |
|
388 | + $storage = $this->createMock(IStorage::class); |
|
389 | + $storage->method('getId')->willReturn('test::1'); |
|
390 | + $cache = new Cache($storage); |
|
391 | + |
|
392 | + $mount = $this->createMock(IMountPoint::class); |
|
393 | + $mount->method('getStorage') |
|
394 | + ->willReturn($storage); |
|
395 | + $mount->method('getInternalPath') |
|
396 | + ->willReturn(''); |
|
397 | + |
|
398 | + $storage->method('getCache') |
|
399 | + ->willReturn($cache); |
|
400 | + $storage->method('getOwner') |
|
401 | + ->willReturn('owner'); |
|
402 | + |
|
403 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
404 | + $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
405 | + $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
406 | + |
|
407 | + |
|
408 | + $root->method('getMountsIn') |
|
409 | + ->with('/bar') |
|
410 | + ->willReturn([]); |
|
411 | + |
|
412 | + $root->method('getMount') |
|
413 | + ->with('/bar') |
|
414 | + ->willReturn($mount); |
|
415 | + |
|
416 | + $node = new Folder($root, $view, '/bar'); |
|
417 | + $result = $node->search('qw'); |
|
418 | + $cache->clear(); |
|
419 | + $this->assertEquals(1, count($result)); |
|
420 | + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
421 | + } |
|
422 | + |
|
423 | + public function testSearchSubStorages(): void { |
|
424 | + $manager = $this->createMock(Manager::class); |
|
425 | + $view = $this->getRootViewMock(); |
|
426 | + $root = $this->getMockBuilder(Root::class) |
|
427 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
428 | + ->getMock(); |
|
429 | + $root->expects($this->any()) |
|
430 | + ->method('getUser') |
|
431 | + ->willReturn($this->user); |
|
432 | + $storage = $this->createMock(IStorage::class); |
|
433 | + $storage->method('getId')->willReturn('test::1'); |
|
434 | + $cache = new Cache($storage); |
|
435 | + $subStorage = $this->createMock(IStorage::class); |
|
436 | + $subStorage->method('getId')->willReturn('test::2'); |
|
437 | + $subCache = new Cache($subStorage); |
|
438 | + $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); |
|
439 | + |
|
440 | + $mount = $this->createMock(IMountPoint::class); |
|
441 | + $mount->method('getStorage') |
|
442 | + ->willReturn($storage); |
|
443 | + $mount->method('getInternalPath') |
|
444 | + ->willReturn('foo'); |
|
445 | + |
|
446 | + $subMount->method('getStorage') |
|
447 | + ->willReturn($subStorage); |
|
448 | + |
|
449 | + $subMount->method('getMountPoint') |
|
450 | + ->willReturn('/bar/foo/bar/'); |
|
451 | + |
|
452 | + $storage->method('getCache') |
|
453 | + ->willReturn($cache); |
|
454 | + $storage->method('getOwner') |
|
455 | + ->willReturn('owner'); |
|
456 | + |
|
457 | + $subStorage->method('getCache') |
|
458 | + ->willReturn($subCache); |
|
459 | + $subStorage->method('getOwner') |
|
460 | + ->willReturn('owner'); |
|
461 | + |
|
462 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
463 | + $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
464 | + $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
465 | + |
|
466 | + $subCache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
467 | + $subCache->insert('asd', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
468 | + $subCache->insert('asd/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); |
|
469 | + |
|
470 | + |
|
471 | + $root->method('getMountsIn') |
|
472 | + ->with('/bar/foo') |
|
473 | + ->willReturn([$subMount]); |
|
474 | + |
|
475 | + $root->method('getMount') |
|
476 | + ->with('/bar/foo') |
|
477 | + ->willReturn($mount); |
|
478 | + |
|
479 | + |
|
480 | + $node = new Folder($root, $view, '/bar/foo'); |
|
481 | + $result = $node->search('qw'); |
|
482 | + $cache->clear(); |
|
483 | + $subCache->clear(); |
|
484 | + $this->assertEquals(2, count($result)); |
|
485 | + } |
|
486 | + |
|
487 | + public function testIsSubNode(): void { |
|
488 | + $rootFolderMock = $this->createMock(IRootFolder::class); |
|
489 | + $file = new Node($rootFolderMock, $this->view, '/foo/bar'); |
|
490 | + $folder = new Folder($rootFolderMock, $this->view, '/foo'); |
|
491 | + $this->assertTrue($folder->isSubNode($file)); |
|
492 | + $this->assertFalse($folder->isSubNode($folder)); |
|
493 | + |
|
494 | + $file = new Node($rootFolderMock, $this->view, '/foobar'); |
|
495 | + $this->assertFalse($folder->isSubNode($file)); |
|
496 | + } |
|
497 | + |
|
498 | + public function testGetById(): void { |
|
499 | + $manager = $this->createMock(Manager::class); |
|
500 | + $view = $this->getRootViewMock(); |
|
501 | + $root = $this->getMockBuilder(Root::class) |
|
502 | + ->onlyMethods(['getMountsIn', 'getMount']) |
|
503 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
504 | + ->getMock(); |
|
505 | + $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
506 | + $mount = new MountPoint($storage, '/bar'); |
|
507 | + $storage->method('getId')->willReturn(''); |
|
508 | + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
509 | + |
|
510 | + $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null); |
|
511 | + |
|
512 | + $storage->method('getCache') |
|
513 | + ->willReturn($cache); |
|
514 | + $storage->method('getOwner') |
|
515 | + ->willReturn('owner'); |
|
516 | + |
|
517 | + $this->userMountCache->expects($this->any()) |
|
518 | + ->method('getMountsForFileId') |
|
519 | + ->with(1) |
|
520 | + ->willReturn([new CachedMountInfo( |
|
521 | + $this->user, |
|
522 | + 1, |
|
523 | + 0, |
|
524 | + '/bar/', |
|
525 | + 'test', |
|
526 | + 1, |
|
527 | + '' |
|
528 | + )]); |
|
529 | + |
|
530 | + $cache->method('get') |
|
531 | + ->with(1) |
|
532 | + ->willReturn($fileInfo); |
|
533 | + |
|
534 | + $root->method('getMountsIn') |
|
535 | + ->with('/bar/foo') |
|
536 | + ->willReturn([]); |
|
537 | + |
|
538 | + $manager->method('getMountsByMountProvider') |
|
539 | + ->willReturn([$mount]); |
|
540 | + |
|
541 | + $node = new Folder($root, $view, '/bar/foo'); |
|
542 | + $result = $node->getById(1); |
|
543 | + $this->assertEquals(1, count($result)); |
|
544 | + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
545 | + } |
|
546 | + |
|
547 | + public function testGetByIdMountRoot(): void { |
|
548 | + $manager = $this->createMock(Manager::class); |
|
549 | + $view = $this->getRootViewMock(); |
|
550 | + $root = $this->getMockBuilder(Root::class) |
|
551 | + ->onlyMethods(['getMountsIn', 'getMount']) |
|
552 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
553 | + ->getMock(); |
|
554 | + $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
555 | + $mount = new MountPoint($storage, '/bar'); |
|
556 | + $storage->method('getId')->willReturn(''); |
|
557 | + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
558 | + |
|
559 | + $fileInfo = new CacheEntry(['path' => '', 'mimetype' => 'text/plain'], null); |
|
560 | + |
|
561 | + $storage->method('getCache') |
|
562 | + ->willReturn($cache); |
|
563 | + $storage->method('getOwner') |
|
564 | + ->willReturn('owner'); |
|
565 | + |
|
566 | + $this->userMountCache->expects($this->any()) |
|
567 | + ->method('getMountsForFileId') |
|
568 | + ->with(1) |
|
569 | + ->willReturn([new CachedMountInfo( |
|
570 | + $this->user, |
|
571 | + 1, |
|
572 | + 0, |
|
573 | + '/bar/', |
|
574 | + 'test', |
|
575 | + 1, |
|
576 | + '' |
|
577 | + )]); |
|
578 | + |
|
579 | + $cache->method('get') |
|
580 | + ->with(1) |
|
581 | + ->willReturn($fileInfo); |
|
582 | + |
|
583 | + $manager->method('getMountsByMountProvider') |
|
584 | + ->willReturn([$mount]); |
|
585 | + |
|
586 | + $node = new Folder($root, $view, '/bar'); |
|
587 | + $result = $node->getById(1); |
|
588 | + $this->assertEquals(1, count($result)); |
|
589 | + $this->assertEquals('/bar', $result[0]->getPath()); |
|
590 | + } |
|
591 | + |
|
592 | + public function testGetByIdOutsideFolder(): void { |
|
593 | + $manager = $this->createMock(Manager::class); |
|
594 | + $view = $this->getRootViewMock(); |
|
595 | + $root = $this->getMockBuilder(Root::class) |
|
596 | + ->onlyMethods(['getMountsIn', 'getMount']) |
|
597 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
598 | + ->getMock(); |
|
599 | + $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
600 | + $mount = new MountPoint($storage, '/bar'); |
|
601 | + $storage->method('getId')->willReturn(''); |
|
602 | + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
603 | + |
|
604 | + $fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null); |
|
605 | + |
|
606 | + $storage->method('getCache') |
|
607 | + ->willReturn($cache); |
|
608 | + $storage->method('getOwner') |
|
609 | + ->willReturn('owner'); |
|
610 | + |
|
611 | + $this->userMountCache->expects($this->any()) |
|
612 | + ->method('getMountsForFileId') |
|
613 | + ->with(1) |
|
614 | + ->willReturn([new CachedMountInfo( |
|
615 | + $this->user, |
|
616 | + 1, |
|
617 | + 0, |
|
618 | + '/bar/', |
|
619 | + 'test', |
|
620 | + 1, |
|
621 | + '' |
|
622 | + )]); |
|
623 | + |
|
624 | + $cache->method('get') |
|
625 | + ->with(1) |
|
626 | + ->willReturn($fileInfo); |
|
627 | + |
|
628 | + $manager->method('getMountsByMountProvider') |
|
629 | + ->willReturn([$mount]); |
|
630 | + |
|
631 | + $node = new Folder($root, $view, '/bar/foo'); |
|
632 | + $result = $node->getById(1); |
|
633 | + $this->assertEquals(0, count($result)); |
|
634 | + } |
|
635 | + |
|
636 | + public function testGetByIdMultipleStorages(): void { |
|
637 | + $manager = $this->createMock(Manager::class); |
|
638 | + $view = $this->getRootViewMock(); |
|
639 | + $root = $this->getMockBuilder(Root::class) |
|
640 | + ->onlyMethods(['getMountsIn', 'getMount']) |
|
641 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
642 | + ->getMock(); |
|
643 | + $storage = $this->createMock(\OC\Files\Storage\Storage::class); |
|
644 | + $mount1 = new MountPoint($storage, '/bar'); |
|
645 | + $mount2 = new MountPoint($storage, '/bar/foo/asd'); |
|
646 | + $storage->method('getId')->willReturn(''); |
|
647 | + $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); |
|
648 | + |
|
649 | + $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null); |
|
650 | + |
|
651 | + $storage->method('getCache') |
|
652 | + ->willReturn($cache); |
|
653 | + $storage->method('getOwner') |
|
654 | + ->willReturn('owner'); |
|
655 | + |
|
656 | + $this->userMountCache->method('getMountsForFileId') |
|
657 | + ->with(1) |
|
658 | + ->willReturn([ |
|
659 | + new CachedMountInfo( |
|
660 | + $this->user, |
|
661 | + 1, |
|
662 | + 0, |
|
663 | + '/bar/', |
|
664 | + 'test', |
|
665 | + 1, |
|
666 | + '' |
|
667 | + ), |
|
668 | + ]); |
|
669 | + |
|
670 | + $cache->method('get') |
|
671 | + ->with(1) |
|
672 | + ->willReturn($fileInfo); |
|
673 | + |
|
674 | + $manager->method('getMountsByMountProvider') |
|
675 | + ->willReturn([$mount1, $mount2]); |
|
676 | + |
|
677 | + $node = new Folder($root, $view, '/bar/foo'); |
|
678 | + $result = $node->getById(1); |
|
679 | + $this->assertEquals(2, count($result)); |
|
680 | + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); |
|
681 | + $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath()); |
|
682 | + } |
|
683 | + |
|
684 | + public static function uniqueNameProvider(): array { |
|
685 | + return [ |
|
686 | + // input, existing, expected |
|
687 | + ['foo', [], 'foo'], |
|
688 | + ['foo', ['foo'], 'foo (2)'], |
|
689 | + ['foo', ['foo', 'foo (2)'], 'foo (3)'], |
|
690 | + ]; |
|
691 | + } |
|
692 | + |
|
693 | + /** |
|
694 | + * @dataProvider uniqueNameProvider |
|
695 | + */ |
|
696 | + public function testGetUniqueName($name, $existingFiles, $expected): void { |
|
697 | + $manager = $this->createMock(Manager::class); |
|
698 | + $folderPath = '/bar/foo'; |
|
699 | + $view = $this->getRootViewMock(); |
|
700 | + $root = $this->getMockBuilder(Root::class) |
|
701 | + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
702 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
703 | + ->getMock(); |
|
704 | + |
|
705 | + $view->expects($this->any()) |
|
706 | + ->method('file_exists') |
|
707 | + ->willReturnCallback(function ($path) use ($existingFiles, $folderPath) { |
|
708 | + foreach ($existingFiles as $existing) { |
|
709 | + if ($folderPath . '/' . $existing === $path) { |
|
710 | + return true; |
|
711 | + } |
|
712 | + } |
|
713 | + return false; |
|
714 | + }); |
|
715 | + |
|
716 | + $node = new Folder($root, $view, $folderPath); |
|
717 | + $this->assertEquals($expected, $node->getNonExistingName($name)); |
|
718 | + } |
|
719 | + |
|
720 | + public function testRecent(): void { |
|
721 | + $manager = $this->createMock(Manager::class); |
|
722 | + $folderPath = '/bar/foo'; |
|
723 | + $view = $this->getRootViewMock(); |
|
724 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ |
|
725 | + $root = $this->getMockBuilder(Root::class) |
|
726 | + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
727 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
728 | + ->getMock(); |
|
729 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ |
|
730 | + $folderInfo = $this->getMockBuilder(FileInfo::class) |
|
731 | + ->disableOriginalConstructor()->getMock(); |
|
732 | + |
|
733 | + $baseTime = time(); |
|
734 | + $storage = new Temporary(); |
|
735 | + $mount = new MountPoint($storage, ''); |
|
736 | + |
|
737 | + $folderInfo->expects($this->any()) |
|
738 | + ->method('getMountPoint') |
|
739 | + ->willReturn($mount); |
|
740 | + $root->method('getMount') |
|
741 | + ->willReturn($mount); |
|
742 | + $root->method('getMountsIn') |
|
743 | + ->willReturn([]); |
|
744 | + |
|
745 | + $cache = $storage->getCache(); |
|
746 | + |
|
747 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
748 | + $cache->insert('bar', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
749 | + $cache->insert('bar/foo', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
750 | + $cache->insert('bar/asd', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
751 | + $id1 = $cache->put('bar/foo/inside.txt', [ |
|
752 | + 'storage_mtime' => $baseTime, |
|
753 | + 'mtime' => $baseTime, |
|
754 | + 'mimetype' => 'text/plain', |
|
755 | + 'size' => 3, |
|
756 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
757 | + ]); |
|
758 | + $id2 = $cache->put('bar/foo/old.txt', [ |
|
759 | + 'storage_mtime' => $baseTime - 100, |
|
760 | + 'mtime' => $baseTime - 100, |
|
761 | + 'mimetype' => 'text/plain', |
|
762 | + 'size' => 3, |
|
763 | + 'permissions' => \OCP\Constants::PERMISSION_READ, |
|
764 | + ]); |
|
765 | + $cache->put('bar/asd/outside.txt', [ |
|
766 | + 'storage_mtime' => $baseTime, |
|
767 | + 'mtime' => $baseTime, |
|
768 | + 'mimetype' => 'text/plain', |
|
769 | + 'size' => 3, |
|
770 | + ]); |
|
771 | + $id3 = $cache->put('bar/foo/older.txt', [ |
|
772 | + 'storage_mtime' => $baseTime - 600, |
|
773 | + 'mtime' => $baseTime - 600, |
|
774 | + 'mimetype' => 'text/plain', |
|
775 | + 'size' => 3, |
|
776 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
777 | + ]); |
|
778 | + |
|
779 | + $node = new Folder($root, $view, $folderPath, $folderInfo); |
|
780 | + |
|
781 | + |
|
782 | + $nodes = $node->getRecent(5); |
|
783 | + $ids = array_map(function (Node $node) { |
|
784 | + return (int)$node->getId(); |
|
785 | + }, $nodes); |
|
786 | + $this->assertEquals([$id1, $id2, $id3], $ids); |
|
787 | + } |
|
788 | + |
|
789 | + public function testRecentFolder(): void { |
|
790 | + $manager = $this->createMock(Manager::class); |
|
791 | + $folderPath = '/bar/foo'; |
|
792 | + $view = $this->getRootViewMock(); |
|
793 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ |
|
794 | + $root = $this->getMockBuilder(Root::class) |
|
795 | + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
796 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
797 | + ->getMock(); |
|
798 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ |
|
799 | + $folderInfo = $this->getMockBuilder(FileInfo::class) |
|
800 | + ->disableOriginalConstructor()->getMock(); |
|
801 | + |
|
802 | + $baseTime = time(); |
|
803 | + $storage = new Temporary(); |
|
804 | + $mount = new MountPoint($storage, ''); |
|
805 | + |
|
806 | + $folderInfo->expects($this->any()) |
|
807 | + ->method('getMountPoint') |
|
808 | + ->willReturn($mount); |
|
809 | + |
|
810 | + $root->method('getMount') |
|
811 | + ->willReturn($mount); |
|
812 | + $root->method('getMountsIn') |
|
813 | + ->willReturn([]); |
|
814 | + |
|
815 | + $cache = $storage->getCache(); |
|
816 | + |
|
817 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
818 | + $cache->insert('bar', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
819 | + $cache->insert('bar/foo', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
820 | + $id1 = $cache->put('bar/foo/folder', [ |
|
821 | + 'storage_mtime' => $baseTime, |
|
822 | + 'mtime' => $baseTime, |
|
823 | + 'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER, |
|
824 | + 'size' => 3, |
|
825 | + 'permissions' => 0, |
|
826 | + ]); |
|
827 | + $id2 = $cache->put('bar/foo/folder/bar.txt', [ |
|
828 | + 'storage_mtime' => $baseTime, |
|
829 | + 'mtime' => $baseTime, |
|
830 | + 'mimetype' => 'text/plain', |
|
831 | + 'size' => 3, |
|
832 | + 'parent' => $id1, |
|
833 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
834 | + ]); |
|
835 | + $id3 = $cache->put('bar/foo/folder/asd.txt', [ |
|
836 | + 'storage_mtime' => $baseTime - 100, |
|
837 | + 'mtime' => $baseTime - 100, |
|
838 | + 'mimetype' => 'text/plain', |
|
839 | + 'size' => 3, |
|
840 | + 'parent' => $id1, |
|
841 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
842 | + ]); |
|
843 | + |
|
844 | + $node = new Folder($root, $view, $folderPath, $folderInfo); |
|
845 | + |
|
846 | + |
|
847 | + $nodes = $node->getRecent(5); |
|
848 | + $ids = array_map(function (Node $node) { |
|
849 | + return (int)$node->getId(); |
|
850 | + }, $nodes); |
|
851 | + $this->assertEquals([$id2, $id3], $ids); |
|
852 | + $this->assertEquals($baseTime, $nodes[0]->getMTime()); |
|
853 | + $this->assertEquals($baseTime - 100, $nodes[1]->getMTime()); |
|
854 | + } |
|
855 | + |
|
856 | + public function testRecentJail(): void { |
|
857 | + $manager = $this->createMock(Manager::class); |
|
858 | + $folderPath = '/bar/foo'; |
|
859 | + $view = $this->getRootViewMock(); |
|
860 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ |
|
861 | + $root = $this->getMockBuilder(Root::class) |
|
862 | + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) |
|
863 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
864 | + ->getMock(); |
|
865 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ |
|
866 | + $folderInfo = $this->getMockBuilder(FileInfo::class) |
|
867 | + ->disableOriginalConstructor()->getMock(); |
|
868 | + |
|
869 | + $baseTime = time(); |
|
870 | + $storage = new Temporary(); |
|
871 | + $jail = new Jail([ |
|
872 | + 'storage' => $storage, |
|
873 | + 'root' => 'folder', |
|
874 | + ]); |
|
875 | + $mount = new MountPoint($jail, '/bar/foo'); |
|
876 | + |
|
877 | + $folderInfo->expects($this->any()) |
|
878 | + ->method('getMountPoint') |
|
879 | + ->willReturn($mount); |
|
880 | + $root->method('getMount') |
|
881 | + ->willReturn($mount); |
|
882 | + $root->method('getMountsIn') |
|
883 | + ->willReturn([]); |
|
884 | + |
|
885 | + $cache = $storage->getCache(); |
|
886 | + |
|
887 | + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
888 | + $cache->insert('folder', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
889 | + $id1 = $cache->put('folder/inside.txt', [ |
|
890 | + 'storage_mtime' => $baseTime, |
|
891 | + 'mtime' => $baseTime, |
|
892 | + 'mimetype' => 'text/plain', |
|
893 | + 'size' => 3, |
|
894 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
895 | + ]); |
|
896 | + |
|
897 | + $cache->put('outside.txt', [ |
|
898 | + 'storage_mtime' => $baseTime - 100, |
|
899 | + 'mtime' => $baseTime - 100, |
|
900 | + 'mimetype' => 'text/plain', |
|
901 | + 'size' => 3, |
|
902 | + ]); |
|
903 | + |
|
904 | + $node = new Folder($root, $view, $folderPath, $folderInfo); |
|
905 | + |
|
906 | + $nodes = $node->getRecent(5); |
|
907 | + $ids = array_map(function (Node $node) { |
|
908 | + return (int)$node->getId(); |
|
909 | + }, $nodes); |
|
910 | + $this->assertEquals([$id1], $ids); |
|
911 | + } |
|
912 | + |
|
913 | + public static function offsetLimitProvider(): array { |
|
914 | + return [ |
|
915 | + [0, 10, ['/bar/foo/foo1', '/bar/foo/foo2', '/bar/foo/foo3', '/bar/foo/foo4', '/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], |
|
916 | + [0, 5, ['/bar/foo/foo1', '/bar/foo/foo2', '/bar/foo/foo3', '/bar/foo/foo4', '/bar/foo/sub1/foo5'], []], |
|
917 | + [0, 2, ['/bar/foo/foo1', '/bar/foo/foo2'], []], |
|
918 | + [3, 2, ['/bar/foo/foo4', '/bar/foo/sub1/foo5'], []], |
|
919 | + [3, 5, ['/bar/foo/foo4', '/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], |
|
920 | + [5, 2, ['/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7'], []], |
|
921 | + [6, 2, ['/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], |
|
922 | + [7, 2, ['/bar/foo/sub2/foo8'], []], |
|
923 | + [10, 2, [], []], |
|
924 | + [0, 5, ['/bar/foo/sub2/foo7', '/bar/foo/foo1', '/bar/foo/sub1/foo5', '/bar/foo/foo2', '/bar/foo/foo3'], [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')]], |
|
925 | + [3, 2, ['/bar/foo/foo2', '/bar/foo/foo3'], [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')]], |
|
926 | + [0, 5, ['/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/foo1', '/bar/foo/foo2'], [ |
|
927 | + new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'size'), |
|
928 | + new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime') |
|
929 | + ]], |
|
930 | + ]; |
|
931 | + } |
|
932 | + |
|
933 | + /** |
|
934 | + * @dataProvider offsetLimitProvider |
|
935 | + * @param int $offset |
|
936 | + * @param int $limit |
|
937 | + * @param string[] $expectedPaths |
|
938 | + * @param ISearchOrder[] $ordering |
|
939 | + * @throws NotFoundException |
|
940 | + * @throws \OCP\Files\InvalidPathException |
|
941 | + */ |
|
942 | + public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array $expectedPaths, array $ordering): void { |
|
943 | + if (!$ordering) { |
|
944 | + $ordering = [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'fileid')]; |
|
945 | + } |
|
946 | + |
|
947 | + $manager = $this->createMock(Manager::class); |
|
948 | + $view = $this->getRootViewMock(); |
|
949 | + $root = $this->getMockBuilder(Root::class) |
|
950 | + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) |
|
951 | + ->getMock(); |
|
952 | + $root->expects($this->any()) |
|
953 | + ->method('getUser') |
|
954 | + ->willReturn($this->user); |
|
955 | + $storage = $this->createMock(IStorage::class); |
|
956 | + $storage->method('getId')->willReturn('test::1'); |
|
957 | + $cache = new Cache($storage); |
|
958 | + $subStorage1 = $this->createMock(IStorage::class); |
|
959 | + $subStorage1->method('getId')->willReturn('test::2'); |
|
960 | + $subCache1 = new Cache($subStorage1); |
|
961 | + $subMount1 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); |
|
962 | + $subStorage2 = $this->createMock(IStorage::class); |
|
963 | + $subStorage2->method('getId')->willReturn('test::3'); |
|
964 | + $subCache2 = new Cache($subStorage2); |
|
965 | + $subMount2 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); |
|
966 | + |
|
967 | + $mount = $this->createMock(IMountPoint::class); |
|
968 | + $mount->method('getStorage') |
|
969 | + ->willReturn($storage); |
|
970 | + $mount->method('getInternalPath') |
|
971 | + ->willReturn('foo'); |
|
972 | + |
|
973 | + $subMount1->method('getStorage') |
|
974 | + ->willReturn($subStorage1); |
|
975 | + |
|
976 | + $subMount1->method('getMountPoint') |
|
977 | + ->willReturn('/bar/foo/sub1/'); |
|
978 | + |
|
979 | + $storage->method('getCache') |
|
980 | + ->willReturn($cache); |
|
981 | + $storage->method('getOwner') |
|
982 | + ->willReturn('owner'); |
|
983 | + |
|
984 | + $subStorage1->method('getCache') |
|
985 | + ->willReturn($subCache1); |
|
986 | + $subStorage1->method('getOwner') |
|
987 | + ->willReturn('owner'); |
|
988 | + |
|
989 | + $subMount2->method('getStorage') |
|
990 | + ->willReturn($subStorage2); |
|
991 | + |
|
992 | + $subMount2->method('getMountPoint') |
|
993 | + ->willReturn('/bar/foo/sub2/'); |
|
994 | + |
|
995 | + $subStorage2->method('getCache') |
|
996 | + ->willReturn($subCache2); |
|
997 | + $subStorage2->method('getOwner') |
|
998 | + ->willReturn('owner'); |
|
999 | + |
|
1000 | + |
|
1001 | + $cache->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1002 | + $cache->insert('foo', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1003 | + $cache->insert('foo/foo1', ['size' => 200, 'mtime' => 10, 'mimetype' => 'text/plain']); |
|
1004 | + $cache->insert('foo/foo2', ['size' => 200, 'mtime' => 20, 'mimetype' => 'text/plain']); |
|
1005 | + $cache->insert('foo/foo3', ['size' => 200, 'mtime' => 30, 'mimetype' => 'text/plain']); |
|
1006 | + $cache->insert('foo/foo4', ['size' => 200, 'mtime' => 40, 'mimetype' => 'text/plain']); |
|
1007 | + |
|
1008 | + $subCache1->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1009 | + $subCache1->insert('foo5', ['size' => 300, 'mtime' => 15, 'mimetype' => 'text/plain']); |
|
1010 | + $subCache1->insert('foo6', ['size' => 300, 'mtime' => 50, 'mimetype' => 'text/plain']); |
|
1011 | + |
|
1012 | + $subCache2->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); |
|
1013 | + $subCache2->insert('foo7', ['size' => 200, 'mtime' => 5, 'mimetype' => 'text/plain']); |
|
1014 | + $subCache2->insert('foo8', ['size' => 200, 'mtime' => 60, 'mimetype' => 'text/plain']); |
|
1015 | + |
|
1016 | + $root->method('getMountsIn') |
|
1017 | + ->with('/bar/foo') |
|
1018 | + ->willReturn([$subMount1, $subMount2]); |
|
1019 | + |
|
1020 | + $root->method('getMount') |
|
1021 | + ->with('/bar/foo') |
|
1022 | + ->willReturn($mount); |
|
1023 | + |
|
1024 | + $node = new Folder($root, $view, '/bar/foo'); |
|
1025 | + $comparison = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%foo%'); |
|
1026 | + $operator = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ |
|
1027 | + $comparison, |
|
1028 | + new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE)]), |
|
1029 | + ]); |
|
1030 | + $query = new SearchQuery($operator, $limit, $offset, $ordering); |
|
1031 | + $result = $node->search($query); |
|
1032 | + $cache->clear(); |
|
1033 | + $subCache1->clear(); |
|
1034 | + $subCache2->clear(); |
|
1035 | + $ids = array_map(function (Node $info) { |
|
1036 | + return $info->getPath(); |
|
1037 | + }, $result); |
|
1038 | + $this->assertEquals($expectedPaths, $ids); |
|
1039 | + } |
|
1040 | 1040 | } |
@@ -46,279 +46,279 @@ |
||
46 | 46 | * @package Test\Files\Node |
47 | 47 | */ |
48 | 48 | class HookConnectorTest extends TestCase { |
49 | - use UserTrait; |
|
50 | - use MountProviderTrait; |
|
51 | - |
|
52 | - /** @var IEventDispatcher */ |
|
53 | - protected $eventDispatcher; |
|
54 | - |
|
55 | - private LoggerInterface $logger; |
|
56 | - |
|
57 | - /** @var View */ |
|
58 | - private $view; |
|
59 | - |
|
60 | - /** @var Root */ |
|
61 | - private $root; |
|
62 | - |
|
63 | - /** @var string */ |
|
64 | - private $userId; |
|
65 | - |
|
66 | - protected function setUp(): void { |
|
67 | - parent::setUp(); |
|
68 | - $this->userId = $this->getUniqueID(); |
|
69 | - $this->createUser($this->userId, 'pass'); |
|
70 | - // this will setup the FS |
|
71 | - $this->loginAsUser($this->userId); |
|
72 | - $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/'); |
|
73 | - $cacheFactory = $this->createMock(ICacheFactory::class); |
|
74 | - $cacheFactory->method('createLocal') |
|
75 | - ->willReturnCallback(function () { |
|
76 | - return new ArrayCache(); |
|
77 | - }); |
|
78 | - $this->view = new View(); |
|
79 | - $this->root = new Root( |
|
80 | - Filesystem::getMountManager(), |
|
81 | - $this->view, |
|
82 | - \OC::$server->getUserManager()->get($this->userId), |
|
83 | - \OC::$server->getUserMountCache(), |
|
84 | - $this->createMock(LoggerInterface::class), |
|
85 | - $this->createMock(IUserManager::class), |
|
86 | - $this->createMock(IEventDispatcher::class), |
|
87 | - $cacheFactory, |
|
88 | - ); |
|
89 | - $this->eventDispatcher = \OC::$server->query(IEventDispatcher::class); |
|
90 | - $this->logger = \OC::$server->query(LoggerInterface::class); |
|
91 | - } |
|
92 | - |
|
93 | - protected function tearDown(): void { |
|
94 | - parent::tearDown(); |
|
95 | - \OC_Hook::clear('OC_Filesystem'); |
|
96 | - \OC_Util::tearDownFS(); |
|
97 | - } |
|
98 | - |
|
99 | - public static function viewToNodeProvider(): array { |
|
100 | - return [ |
|
101 | - [function () { |
|
102 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
103 | - }, 'preWrite', '\OCP\Files::preWrite', BeforeNodeWrittenEvent::class], |
|
104 | - [function () { |
|
105 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
106 | - }, 'postWrite', '\OCP\Files::postWrite', NodeWrittenEvent::class], |
|
107 | - [function () { |
|
108 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
109 | - }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
|
110 | - [function () { |
|
111 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
112 | - }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
|
113 | - [function () { |
|
114 | - Filesystem::mkdir('test.txt'); |
|
115 | - }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
|
116 | - [function () { |
|
117 | - Filesystem::mkdir('test.txt'); |
|
118 | - }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
|
119 | - [function () { |
|
120 | - Filesystem::touch('test.txt'); |
|
121 | - }, 'preTouch', '\OCP\Files::preTouch', BeforeNodeTouchedEvent::class], |
|
122 | - [function () { |
|
123 | - Filesystem::touch('test.txt'); |
|
124 | - }, 'postTouch', '\OCP\Files::postTouch', NodeTouchedEvent::class], |
|
125 | - [function () { |
|
126 | - Filesystem::touch('test.txt'); |
|
127 | - }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
|
128 | - [function () { |
|
129 | - Filesystem::touch('test.txt'); |
|
130 | - }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
|
131 | - [function () { |
|
132 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
133 | - Filesystem::unlink('test.txt'); |
|
134 | - }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], |
|
135 | - [function () { |
|
136 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
137 | - Filesystem::unlink('test.txt'); |
|
138 | - }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], |
|
139 | - [function () { |
|
140 | - Filesystem::mkdir('test.txt'); |
|
141 | - Filesystem::rmdir('test.txt'); |
|
142 | - }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], |
|
143 | - [function () { |
|
144 | - Filesystem::mkdir('test.txt'); |
|
145 | - Filesystem::rmdir('test.txt'); |
|
146 | - }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], |
|
147 | - ]; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param callable $operation |
|
152 | - * @param string $expectedHook |
|
153 | - * @dataProvider viewToNodeProvider |
|
154 | - */ |
|
155 | - public function testViewToNode(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent): void { |
|
156 | - $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); |
|
157 | - $connector->viewToNode(); |
|
158 | - $hookCalled = false; |
|
159 | - /** @var Node $hookNode */ |
|
160 | - $hookNode = null; |
|
161 | - |
|
162 | - $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) { |
|
163 | - $hookCalled = true; |
|
164 | - $hookNode = $node; |
|
165 | - }); |
|
166 | - |
|
167 | - $dispatcherCalled = false; |
|
168 | - /** @var Node $dispatcherNode */ |
|
169 | - $dispatcherNode = null; |
|
170 | - $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
171 | - /** @var GenericEvent|APIGenericEvent $event */ |
|
172 | - $dispatcherCalled = true; |
|
173 | - $dispatcherNode = $event->getSubject(); |
|
174 | - }); |
|
175 | - |
|
176 | - $newDispatcherCalled = false; |
|
177 | - $newDispatcherNode = null; |
|
178 | - $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode) { |
|
179 | - if ($event instanceof $expectedEvent) { |
|
180 | - /** @var AbstractNodeEvent $event */ |
|
181 | - $newDispatcherCalled = true; |
|
182 | - $newDispatcherNode = $event->getNode(); |
|
183 | - } |
|
184 | - }); |
|
185 | - |
|
186 | - $operation(); |
|
187 | - |
|
188 | - $this->assertTrue($hookCalled); |
|
189 | - $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath()); |
|
190 | - |
|
191 | - $this->assertTrue($dispatcherCalled); |
|
192 | - $this->assertEquals('/' . $this->userId . '/files/test.txt', $dispatcherNode->getPath()); |
|
193 | - |
|
194 | - $this->assertTrue($newDispatcherCalled); |
|
195 | - $this->assertEquals('/' . $this->userId . '/files/test.txt', $newDispatcherNode->getPath()); |
|
196 | - } |
|
197 | - |
|
198 | - public static function viewToNodeProviderCopyRename(): array { |
|
199 | - return [ |
|
200 | - [function () { |
|
201 | - Filesystem::file_put_contents('source', 'asd'); |
|
202 | - Filesystem::rename('source', 'target'); |
|
203 | - }, 'preRename', '\OCP\Files::preRename', BeforeNodeRenamedEvent::class], |
|
204 | - [function () { |
|
205 | - Filesystem::file_put_contents('source', 'asd'); |
|
206 | - Filesystem::rename('source', 'target'); |
|
207 | - }, 'postRename', '\OCP\Files::postRename', NodeRenamedEvent::class], |
|
208 | - [function () { |
|
209 | - Filesystem::file_put_contents('source', 'asd'); |
|
210 | - Filesystem::copy('source', 'target'); |
|
211 | - }, 'preCopy', '\OCP\Files::preCopy', BeforeNodeCopiedEvent::class], |
|
212 | - [function () { |
|
213 | - Filesystem::file_put_contents('source', 'asd'); |
|
214 | - Filesystem::copy('source', 'target'); |
|
215 | - }, 'postCopy', '\OCP\Files::postCopy', NodeCopiedEvent::class], |
|
216 | - ]; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * @param callable $operation |
|
221 | - * @param string $expectedHook |
|
222 | - * @dataProvider viewToNodeProviderCopyRename |
|
223 | - */ |
|
224 | - public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent): void { |
|
225 | - $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); |
|
226 | - $connector->viewToNode(); |
|
227 | - $hookCalled = false; |
|
228 | - /** @var Node $hookSourceNode */ |
|
229 | - $hookSourceNode = null; |
|
230 | - /** @var Node $hookTargetNode */ |
|
231 | - $hookTargetNode = null; |
|
232 | - |
|
233 | - $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { |
|
234 | - $hookCalled = true; |
|
235 | - $hookSourceNode = $sourceNode; |
|
236 | - $hookTargetNode = $targetNode; |
|
237 | - }); |
|
238 | - |
|
239 | - $dispatcherCalled = false; |
|
240 | - /** @var Node $dispatcherSourceNode */ |
|
241 | - $dispatcherSourceNode = null; |
|
242 | - /** @var Node $dispatcherTargetNode */ |
|
243 | - $dispatcherTargetNode = null; |
|
244 | - $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) { |
|
245 | - /** @var GenericEvent|APIGenericEvent $event */ |
|
246 | - $dispatcherCalled = true; |
|
247 | - [$dispatcherSourceNode, $dispatcherTargetNode] = $event->getSubject(); |
|
248 | - }); |
|
249 | - |
|
250 | - $newDispatcherCalled = false; |
|
251 | - /** @var Node $dispatcherSourceNode */ |
|
252 | - $newDispatcherSourceNode = null; |
|
253 | - /** @var Node $dispatcherTargetNode */ |
|
254 | - $newDispatcherTargetNode = null; |
|
255 | - $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled) { |
|
256 | - if ($event instanceof $expectedEvent) { |
|
257 | - /** @var AbstractNodesEvent$event */ |
|
258 | - $newDispatcherCalled = true; |
|
259 | - $newDispatcherSourceNode = $event->getSource(); |
|
260 | - $newDispatcherTargetNode = $event->getTarget(); |
|
261 | - } |
|
262 | - }); |
|
263 | - |
|
264 | - $operation(); |
|
265 | - |
|
266 | - $this->assertTrue($hookCalled); |
|
267 | - $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath()); |
|
268 | - $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath()); |
|
269 | - |
|
270 | - $this->assertTrue($dispatcherCalled); |
|
271 | - $this->assertEquals('/' . $this->userId . '/files/source', $dispatcherSourceNode->getPath()); |
|
272 | - $this->assertEquals('/' . $this->userId . '/files/target', $dispatcherTargetNode->getPath()); |
|
273 | - |
|
274 | - $this->assertTrue($newDispatcherCalled); |
|
275 | - $this->assertEquals('/' . $this->userId . '/files/source', $newDispatcherSourceNode->getPath()); |
|
276 | - $this->assertEquals('/' . $this->userId . '/files/target', $newDispatcherTargetNode->getPath()); |
|
277 | - } |
|
278 | - |
|
279 | - public function testPostDeleteMeta(): void { |
|
280 | - $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); |
|
281 | - $connector->viewToNode(); |
|
282 | - $hookCalled = false; |
|
283 | - /** @var Node $hookNode */ |
|
284 | - $hookNode = null; |
|
285 | - |
|
286 | - $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) { |
|
287 | - $hookCalled = true; |
|
288 | - $hookNode = $node; |
|
289 | - }); |
|
290 | - |
|
291 | - $dispatcherCalled = false; |
|
292 | - /** @var Node $dispatcherNode */ |
|
293 | - $dispatcherNode = null; |
|
294 | - $this->eventDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
295 | - /** @var GenericEvent|APIGenericEvent $event */ |
|
296 | - $dispatcherCalled = true; |
|
297 | - $dispatcherNode = $event->getSubject(); |
|
298 | - }); |
|
299 | - |
|
300 | - $newDispatcherCalled = false; |
|
301 | - /** @var Node $dispatcherNode */ |
|
302 | - $newDispatcherNode = null; |
|
303 | - $this->eventDispatcher->addListener(NodeDeletedEvent::class, function ($event) use (&$newDispatcherCalled, &$newDispatcherNode) { |
|
304 | - if ($event instanceof NodeDeletedEvent) { |
|
305 | - /** @var AbstractNodeEvent $event */ |
|
306 | - $newDispatcherCalled = true; |
|
307 | - $newDispatcherNode = $event->getNode(); |
|
308 | - } |
|
309 | - }); |
|
310 | - |
|
311 | - Filesystem::file_put_contents('test.txt', 'asd'); |
|
312 | - $info = Filesystem::getFileInfo('test.txt'); |
|
313 | - Filesystem::unlink('test.txt'); |
|
314 | - |
|
315 | - $this->assertTrue($hookCalled); |
|
316 | - $this->assertEquals($hookNode->getId(), $info->getId()); |
|
317 | - |
|
318 | - $this->assertTrue($dispatcherCalled); |
|
319 | - $this->assertEquals($dispatcherNode->getId(), $info->getId()); |
|
320 | - |
|
321 | - $this->assertTrue($newDispatcherCalled); |
|
322 | - $this->assertEquals($newDispatcherNode->getId(), $info->getId()); |
|
323 | - } |
|
49 | + use UserTrait; |
|
50 | + use MountProviderTrait; |
|
51 | + |
|
52 | + /** @var IEventDispatcher */ |
|
53 | + protected $eventDispatcher; |
|
54 | + |
|
55 | + private LoggerInterface $logger; |
|
56 | + |
|
57 | + /** @var View */ |
|
58 | + private $view; |
|
59 | + |
|
60 | + /** @var Root */ |
|
61 | + private $root; |
|
62 | + |
|
63 | + /** @var string */ |
|
64 | + private $userId; |
|
65 | + |
|
66 | + protected function setUp(): void { |
|
67 | + parent::setUp(); |
|
68 | + $this->userId = $this->getUniqueID(); |
|
69 | + $this->createUser($this->userId, 'pass'); |
|
70 | + // this will setup the FS |
|
71 | + $this->loginAsUser($this->userId); |
|
72 | + $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/'); |
|
73 | + $cacheFactory = $this->createMock(ICacheFactory::class); |
|
74 | + $cacheFactory->method('createLocal') |
|
75 | + ->willReturnCallback(function () { |
|
76 | + return new ArrayCache(); |
|
77 | + }); |
|
78 | + $this->view = new View(); |
|
79 | + $this->root = new Root( |
|
80 | + Filesystem::getMountManager(), |
|
81 | + $this->view, |
|
82 | + \OC::$server->getUserManager()->get($this->userId), |
|
83 | + \OC::$server->getUserMountCache(), |
|
84 | + $this->createMock(LoggerInterface::class), |
|
85 | + $this->createMock(IUserManager::class), |
|
86 | + $this->createMock(IEventDispatcher::class), |
|
87 | + $cacheFactory, |
|
88 | + ); |
|
89 | + $this->eventDispatcher = \OC::$server->query(IEventDispatcher::class); |
|
90 | + $this->logger = \OC::$server->query(LoggerInterface::class); |
|
91 | + } |
|
92 | + |
|
93 | + protected function tearDown(): void { |
|
94 | + parent::tearDown(); |
|
95 | + \OC_Hook::clear('OC_Filesystem'); |
|
96 | + \OC_Util::tearDownFS(); |
|
97 | + } |
|
98 | + |
|
99 | + public static function viewToNodeProvider(): array { |
|
100 | + return [ |
|
101 | + [function () { |
|
102 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
103 | + }, 'preWrite', '\OCP\Files::preWrite', BeforeNodeWrittenEvent::class], |
|
104 | + [function () { |
|
105 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
106 | + }, 'postWrite', '\OCP\Files::postWrite', NodeWrittenEvent::class], |
|
107 | + [function () { |
|
108 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
109 | + }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
|
110 | + [function () { |
|
111 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
112 | + }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
|
113 | + [function () { |
|
114 | + Filesystem::mkdir('test.txt'); |
|
115 | + }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
|
116 | + [function () { |
|
117 | + Filesystem::mkdir('test.txt'); |
|
118 | + }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
|
119 | + [function () { |
|
120 | + Filesystem::touch('test.txt'); |
|
121 | + }, 'preTouch', '\OCP\Files::preTouch', BeforeNodeTouchedEvent::class], |
|
122 | + [function () { |
|
123 | + Filesystem::touch('test.txt'); |
|
124 | + }, 'postTouch', '\OCP\Files::postTouch', NodeTouchedEvent::class], |
|
125 | + [function () { |
|
126 | + Filesystem::touch('test.txt'); |
|
127 | + }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
|
128 | + [function () { |
|
129 | + Filesystem::touch('test.txt'); |
|
130 | + }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
|
131 | + [function () { |
|
132 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
133 | + Filesystem::unlink('test.txt'); |
|
134 | + }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], |
|
135 | + [function () { |
|
136 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
137 | + Filesystem::unlink('test.txt'); |
|
138 | + }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], |
|
139 | + [function () { |
|
140 | + Filesystem::mkdir('test.txt'); |
|
141 | + Filesystem::rmdir('test.txt'); |
|
142 | + }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], |
|
143 | + [function () { |
|
144 | + Filesystem::mkdir('test.txt'); |
|
145 | + Filesystem::rmdir('test.txt'); |
|
146 | + }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], |
|
147 | + ]; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param callable $operation |
|
152 | + * @param string $expectedHook |
|
153 | + * @dataProvider viewToNodeProvider |
|
154 | + */ |
|
155 | + public function testViewToNode(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent): void { |
|
156 | + $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); |
|
157 | + $connector->viewToNode(); |
|
158 | + $hookCalled = false; |
|
159 | + /** @var Node $hookNode */ |
|
160 | + $hookNode = null; |
|
161 | + |
|
162 | + $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) { |
|
163 | + $hookCalled = true; |
|
164 | + $hookNode = $node; |
|
165 | + }); |
|
166 | + |
|
167 | + $dispatcherCalled = false; |
|
168 | + /** @var Node $dispatcherNode */ |
|
169 | + $dispatcherNode = null; |
|
170 | + $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
171 | + /** @var GenericEvent|APIGenericEvent $event */ |
|
172 | + $dispatcherCalled = true; |
|
173 | + $dispatcherNode = $event->getSubject(); |
|
174 | + }); |
|
175 | + |
|
176 | + $newDispatcherCalled = false; |
|
177 | + $newDispatcherNode = null; |
|
178 | + $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode) { |
|
179 | + if ($event instanceof $expectedEvent) { |
|
180 | + /** @var AbstractNodeEvent $event */ |
|
181 | + $newDispatcherCalled = true; |
|
182 | + $newDispatcherNode = $event->getNode(); |
|
183 | + } |
|
184 | + }); |
|
185 | + |
|
186 | + $operation(); |
|
187 | + |
|
188 | + $this->assertTrue($hookCalled); |
|
189 | + $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath()); |
|
190 | + |
|
191 | + $this->assertTrue($dispatcherCalled); |
|
192 | + $this->assertEquals('/' . $this->userId . '/files/test.txt', $dispatcherNode->getPath()); |
|
193 | + |
|
194 | + $this->assertTrue($newDispatcherCalled); |
|
195 | + $this->assertEquals('/' . $this->userId . '/files/test.txt', $newDispatcherNode->getPath()); |
|
196 | + } |
|
197 | + |
|
198 | + public static function viewToNodeProviderCopyRename(): array { |
|
199 | + return [ |
|
200 | + [function () { |
|
201 | + Filesystem::file_put_contents('source', 'asd'); |
|
202 | + Filesystem::rename('source', 'target'); |
|
203 | + }, 'preRename', '\OCP\Files::preRename', BeforeNodeRenamedEvent::class], |
|
204 | + [function () { |
|
205 | + Filesystem::file_put_contents('source', 'asd'); |
|
206 | + Filesystem::rename('source', 'target'); |
|
207 | + }, 'postRename', '\OCP\Files::postRename', NodeRenamedEvent::class], |
|
208 | + [function () { |
|
209 | + Filesystem::file_put_contents('source', 'asd'); |
|
210 | + Filesystem::copy('source', 'target'); |
|
211 | + }, 'preCopy', '\OCP\Files::preCopy', BeforeNodeCopiedEvent::class], |
|
212 | + [function () { |
|
213 | + Filesystem::file_put_contents('source', 'asd'); |
|
214 | + Filesystem::copy('source', 'target'); |
|
215 | + }, 'postCopy', '\OCP\Files::postCopy', NodeCopiedEvent::class], |
|
216 | + ]; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * @param callable $operation |
|
221 | + * @param string $expectedHook |
|
222 | + * @dataProvider viewToNodeProviderCopyRename |
|
223 | + */ |
|
224 | + public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent): void { |
|
225 | + $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); |
|
226 | + $connector->viewToNode(); |
|
227 | + $hookCalled = false; |
|
228 | + /** @var Node $hookSourceNode */ |
|
229 | + $hookSourceNode = null; |
|
230 | + /** @var Node $hookTargetNode */ |
|
231 | + $hookTargetNode = null; |
|
232 | + |
|
233 | + $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { |
|
234 | + $hookCalled = true; |
|
235 | + $hookSourceNode = $sourceNode; |
|
236 | + $hookTargetNode = $targetNode; |
|
237 | + }); |
|
238 | + |
|
239 | + $dispatcherCalled = false; |
|
240 | + /** @var Node $dispatcherSourceNode */ |
|
241 | + $dispatcherSourceNode = null; |
|
242 | + /** @var Node $dispatcherTargetNode */ |
|
243 | + $dispatcherTargetNode = null; |
|
244 | + $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) { |
|
245 | + /** @var GenericEvent|APIGenericEvent $event */ |
|
246 | + $dispatcherCalled = true; |
|
247 | + [$dispatcherSourceNode, $dispatcherTargetNode] = $event->getSubject(); |
|
248 | + }); |
|
249 | + |
|
250 | + $newDispatcherCalled = false; |
|
251 | + /** @var Node $dispatcherSourceNode */ |
|
252 | + $newDispatcherSourceNode = null; |
|
253 | + /** @var Node $dispatcherTargetNode */ |
|
254 | + $newDispatcherTargetNode = null; |
|
255 | + $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled) { |
|
256 | + if ($event instanceof $expectedEvent) { |
|
257 | + /** @var AbstractNodesEvent$event */ |
|
258 | + $newDispatcherCalled = true; |
|
259 | + $newDispatcherSourceNode = $event->getSource(); |
|
260 | + $newDispatcherTargetNode = $event->getTarget(); |
|
261 | + } |
|
262 | + }); |
|
263 | + |
|
264 | + $operation(); |
|
265 | + |
|
266 | + $this->assertTrue($hookCalled); |
|
267 | + $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath()); |
|
268 | + $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath()); |
|
269 | + |
|
270 | + $this->assertTrue($dispatcherCalled); |
|
271 | + $this->assertEquals('/' . $this->userId . '/files/source', $dispatcherSourceNode->getPath()); |
|
272 | + $this->assertEquals('/' . $this->userId . '/files/target', $dispatcherTargetNode->getPath()); |
|
273 | + |
|
274 | + $this->assertTrue($newDispatcherCalled); |
|
275 | + $this->assertEquals('/' . $this->userId . '/files/source', $newDispatcherSourceNode->getPath()); |
|
276 | + $this->assertEquals('/' . $this->userId . '/files/target', $newDispatcherTargetNode->getPath()); |
|
277 | + } |
|
278 | + |
|
279 | + public function testPostDeleteMeta(): void { |
|
280 | + $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); |
|
281 | + $connector->viewToNode(); |
|
282 | + $hookCalled = false; |
|
283 | + /** @var Node $hookNode */ |
|
284 | + $hookNode = null; |
|
285 | + |
|
286 | + $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) { |
|
287 | + $hookCalled = true; |
|
288 | + $hookNode = $node; |
|
289 | + }); |
|
290 | + |
|
291 | + $dispatcherCalled = false; |
|
292 | + /** @var Node $dispatcherNode */ |
|
293 | + $dispatcherNode = null; |
|
294 | + $this->eventDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
295 | + /** @var GenericEvent|APIGenericEvent $event */ |
|
296 | + $dispatcherCalled = true; |
|
297 | + $dispatcherNode = $event->getSubject(); |
|
298 | + }); |
|
299 | + |
|
300 | + $newDispatcherCalled = false; |
|
301 | + /** @var Node $dispatcherNode */ |
|
302 | + $newDispatcherNode = null; |
|
303 | + $this->eventDispatcher->addListener(NodeDeletedEvent::class, function ($event) use (&$newDispatcherCalled, &$newDispatcherNode) { |
|
304 | + if ($event instanceof NodeDeletedEvent) { |
|
305 | + /** @var AbstractNodeEvent $event */ |
|
306 | + $newDispatcherCalled = true; |
|
307 | + $newDispatcherNode = $event->getNode(); |
|
308 | + } |
|
309 | + }); |
|
310 | + |
|
311 | + Filesystem::file_put_contents('test.txt', 'asd'); |
|
312 | + $info = Filesystem::getFileInfo('test.txt'); |
|
313 | + Filesystem::unlink('test.txt'); |
|
314 | + |
|
315 | + $this->assertTrue($hookCalled); |
|
316 | + $this->assertEquals($hookNode->getId(), $info->getId()); |
|
317 | + |
|
318 | + $this->assertTrue($dispatcherCalled); |
|
319 | + $this->assertEquals($dispatcherNode->getId(), $info->getId()); |
|
320 | + |
|
321 | + $this->assertTrue($newDispatcherCalled); |
|
322 | + $this->assertEquals($newDispatcherNode->getId(), $info->getId()); |
|
323 | + } |
|
324 | 324 | } |
@@ -69,10 +69,10 @@ discard block |
||
69 | 69 | $this->createUser($this->userId, 'pass'); |
70 | 70 | // this will setup the FS |
71 | 71 | $this->loginAsUser($this->userId); |
72 | - $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/'); |
|
72 | + $this->registerMount($this->userId, new Temporary(), '/'.$this->userId.'/files/'); |
|
73 | 73 | $cacheFactory = $this->createMock(ICacheFactory::class); |
74 | 74 | $cacheFactory->method('createLocal') |
75 | - ->willReturnCallback(function () { |
|
75 | + ->willReturnCallback(function() { |
|
76 | 76 | return new ArrayCache(); |
77 | 77 | }); |
78 | 78 | $this->view = new View(); |
@@ -98,49 +98,49 @@ discard block |
||
98 | 98 | |
99 | 99 | public static function viewToNodeProvider(): array { |
100 | 100 | return [ |
101 | - [function () { |
|
101 | + [function() { |
|
102 | 102 | Filesystem::file_put_contents('test.txt', 'asd'); |
103 | 103 | }, 'preWrite', '\OCP\Files::preWrite', BeforeNodeWrittenEvent::class], |
104 | - [function () { |
|
104 | + [function() { |
|
105 | 105 | Filesystem::file_put_contents('test.txt', 'asd'); |
106 | 106 | }, 'postWrite', '\OCP\Files::postWrite', NodeWrittenEvent::class], |
107 | - [function () { |
|
107 | + [function() { |
|
108 | 108 | Filesystem::file_put_contents('test.txt', 'asd'); |
109 | 109 | }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
110 | - [function () { |
|
110 | + [function() { |
|
111 | 111 | Filesystem::file_put_contents('test.txt', 'asd'); |
112 | 112 | }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
113 | - [function () { |
|
113 | + [function() { |
|
114 | 114 | Filesystem::mkdir('test.txt'); |
115 | 115 | }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
116 | - [function () { |
|
116 | + [function() { |
|
117 | 117 | Filesystem::mkdir('test.txt'); |
118 | 118 | }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
119 | - [function () { |
|
119 | + [function() { |
|
120 | 120 | Filesystem::touch('test.txt'); |
121 | 121 | }, 'preTouch', '\OCP\Files::preTouch', BeforeNodeTouchedEvent::class], |
122 | - [function () { |
|
122 | + [function() { |
|
123 | 123 | Filesystem::touch('test.txt'); |
124 | 124 | }, 'postTouch', '\OCP\Files::postTouch', NodeTouchedEvent::class], |
125 | - [function () { |
|
125 | + [function() { |
|
126 | 126 | Filesystem::touch('test.txt'); |
127 | 127 | }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], |
128 | - [function () { |
|
128 | + [function() { |
|
129 | 129 | Filesystem::touch('test.txt'); |
130 | 130 | }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], |
131 | - [function () { |
|
131 | + [function() { |
|
132 | 132 | Filesystem::file_put_contents('test.txt', 'asd'); |
133 | 133 | Filesystem::unlink('test.txt'); |
134 | 134 | }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], |
135 | - [function () { |
|
135 | + [function() { |
|
136 | 136 | Filesystem::file_put_contents('test.txt', 'asd'); |
137 | 137 | Filesystem::unlink('test.txt'); |
138 | 138 | }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], |
139 | - [function () { |
|
139 | + [function() { |
|
140 | 140 | Filesystem::mkdir('test.txt'); |
141 | 141 | Filesystem::rmdir('test.txt'); |
142 | 142 | }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], |
143 | - [function () { |
|
143 | + [function() { |
|
144 | 144 | Filesystem::mkdir('test.txt'); |
145 | 145 | Filesystem::rmdir('test.txt'); |
146 | 146 | }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | /** @var Node $hookNode */ |
160 | 160 | $hookNode = null; |
161 | 161 | |
162 | - $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) { |
|
162 | + $this->root->listen('\OC\Files', $expectedHook, function($node) use (&$hookNode, &$hookCalled) { |
|
163 | 163 | $hookCalled = true; |
164 | 164 | $hookNode = $node; |
165 | 165 | }); |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $dispatcherCalled = false; |
168 | 168 | /** @var Node $dispatcherNode */ |
169 | 169 | $dispatcherNode = null; |
170 | - $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
170 | + $this->eventDispatcher->addListener($expectedLegacyEvent, function($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
171 | 171 | /** @var GenericEvent|APIGenericEvent $event */ |
172 | 172 | $dispatcherCalled = true; |
173 | 173 | $dispatcherNode = $event->getSubject(); |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | |
176 | 176 | $newDispatcherCalled = false; |
177 | 177 | $newDispatcherNode = null; |
178 | - $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode) { |
|
178 | + $this->eventDispatcher->addListener($expectedEvent, function($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode) { |
|
179 | 179 | if ($event instanceof $expectedEvent) { |
180 | 180 | /** @var AbstractNodeEvent $event */ |
181 | 181 | $newDispatcherCalled = true; |
@@ -186,30 +186,30 @@ discard block |
||
186 | 186 | $operation(); |
187 | 187 | |
188 | 188 | $this->assertTrue($hookCalled); |
189 | - $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath()); |
|
189 | + $this->assertEquals('/'.$this->userId.'/files/test.txt', $hookNode->getPath()); |
|
190 | 190 | |
191 | 191 | $this->assertTrue($dispatcherCalled); |
192 | - $this->assertEquals('/' . $this->userId . '/files/test.txt', $dispatcherNode->getPath()); |
|
192 | + $this->assertEquals('/'.$this->userId.'/files/test.txt', $dispatcherNode->getPath()); |
|
193 | 193 | |
194 | 194 | $this->assertTrue($newDispatcherCalled); |
195 | - $this->assertEquals('/' . $this->userId . '/files/test.txt', $newDispatcherNode->getPath()); |
|
195 | + $this->assertEquals('/'.$this->userId.'/files/test.txt', $newDispatcherNode->getPath()); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | public static function viewToNodeProviderCopyRename(): array { |
199 | 199 | return [ |
200 | - [function () { |
|
200 | + [function() { |
|
201 | 201 | Filesystem::file_put_contents('source', 'asd'); |
202 | 202 | Filesystem::rename('source', 'target'); |
203 | 203 | }, 'preRename', '\OCP\Files::preRename', BeforeNodeRenamedEvent::class], |
204 | - [function () { |
|
204 | + [function() { |
|
205 | 205 | Filesystem::file_put_contents('source', 'asd'); |
206 | 206 | Filesystem::rename('source', 'target'); |
207 | 207 | }, 'postRename', '\OCP\Files::postRename', NodeRenamedEvent::class], |
208 | - [function () { |
|
208 | + [function() { |
|
209 | 209 | Filesystem::file_put_contents('source', 'asd'); |
210 | 210 | Filesystem::copy('source', 'target'); |
211 | 211 | }, 'preCopy', '\OCP\Files::preCopy', BeforeNodeCopiedEvent::class], |
212 | - [function () { |
|
212 | + [function() { |
|
213 | 213 | Filesystem::file_put_contents('source', 'asd'); |
214 | 214 | Filesystem::copy('source', 'target'); |
215 | 215 | }, 'postCopy', '\OCP\Files::postCopy', NodeCopiedEvent::class], |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | /** @var Node $hookTargetNode */ |
231 | 231 | $hookTargetNode = null; |
232 | 232 | |
233 | - $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { |
|
233 | + $this->root->listen('\OC\Files', $expectedHook, function($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { |
|
234 | 234 | $hookCalled = true; |
235 | 235 | $hookSourceNode = $sourceNode; |
236 | 236 | $hookTargetNode = $targetNode; |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | $dispatcherSourceNode = null; |
242 | 242 | /** @var Node $dispatcherTargetNode */ |
243 | 243 | $dispatcherTargetNode = null; |
244 | - $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) { |
|
244 | + $this->eventDispatcher->addListener($expectedLegacyEvent, function($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) { |
|
245 | 245 | /** @var GenericEvent|APIGenericEvent $event */ |
246 | 246 | $dispatcherCalled = true; |
247 | 247 | [$dispatcherSourceNode, $dispatcherTargetNode] = $event->getSubject(); |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | $newDispatcherSourceNode = null; |
253 | 253 | /** @var Node $dispatcherTargetNode */ |
254 | 254 | $newDispatcherTargetNode = null; |
255 | - $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled) { |
|
255 | + $this->eventDispatcher->addListener($expectedEvent, function($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled) { |
|
256 | 256 | if ($event instanceof $expectedEvent) { |
257 | 257 | /** @var AbstractNodesEvent$event */ |
258 | 258 | $newDispatcherCalled = true; |
@@ -264,16 +264,16 @@ discard block |
||
264 | 264 | $operation(); |
265 | 265 | |
266 | 266 | $this->assertTrue($hookCalled); |
267 | - $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath()); |
|
268 | - $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath()); |
|
267 | + $this->assertEquals('/'.$this->userId.'/files/source', $hookSourceNode->getPath()); |
|
268 | + $this->assertEquals('/'.$this->userId.'/files/target', $hookTargetNode->getPath()); |
|
269 | 269 | |
270 | 270 | $this->assertTrue($dispatcherCalled); |
271 | - $this->assertEquals('/' . $this->userId . '/files/source', $dispatcherSourceNode->getPath()); |
|
272 | - $this->assertEquals('/' . $this->userId . '/files/target', $dispatcherTargetNode->getPath()); |
|
271 | + $this->assertEquals('/'.$this->userId.'/files/source', $dispatcherSourceNode->getPath()); |
|
272 | + $this->assertEquals('/'.$this->userId.'/files/target', $dispatcherTargetNode->getPath()); |
|
273 | 273 | |
274 | 274 | $this->assertTrue($newDispatcherCalled); |
275 | - $this->assertEquals('/' . $this->userId . '/files/source', $newDispatcherSourceNode->getPath()); |
|
276 | - $this->assertEquals('/' . $this->userId . '/files/target', $newDispatcherTargetNode->getPath()); |
|
275 | + $this->assertEquals('/'.$this->userId.'/files/source', $newDispatcherSourceNode->getPath()); |
|
276 | + $this->assertEquals('/'.$this->userId.'/files/target', $newDispatcherTargetNode->getPath()); |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | public function testPostDeleteMeta(): void { |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | /** @var Node $hookNode */ |
284 | 284 | $hookNode = null; |
285 | 285 | |
286 | - $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) { |
|
286 | + $this->root->listen('\OC\Files', 'postDelete', function($node) use (&$hookNode, &$hookCalled) { |
|
287 | 287 | $hookCalled = true; |
288 | 288 | $hookNode = $node; |
289 | 289 | }); |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | $dispatcherCalled = false; |
292 | 292 | /** @var Node $dispatcherNode */ |
293 | 293 | $dispatcherNode = null; |
294 | - $this->eventDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
294 | + $this->eventDispatcher->addListener('\OCP\Files::postDelete', function($event) use (&$dispatcherCalled, &$dispatcherNode) { |
|
295 | 295 | /** @var GenericEvent|APIGenericEvent $event */ |
296 | 296 | $dispatcherCalled = true; |
297 | 297 | $dispatcherNode = $event->getSubject(); |
@@ -300,7 +300,7 @@ discard block |
||
300 | 300 | $newDispatcherCalled = false; |
301 | 301 | /** @var Node $dispatcherNode */ |
302 | 302 | $newDispatcherNode = null; |
303 | - $this->eventDispatcher->addListener(NodeDeletedEvent::class, function ($event) use (&$newDispatcherCalled, &$newDispatcherNode) { |
|
303 | + $this->eventDispatcher->addListener(NodeDeletedEvent::class, function($event) use (&$newDispatcherCalled, &$newDispatcherNode) { |
|
304 | 304 | if ($event instanceof NodeDeletedEvent) { |
305 | 305 | /** @var AbstractNodeEvent $event */ |
306 | 306 | $newDispatcherCalled = true; |
@@ -17,25 +17,25 @@ discard block |
||
17 | 17 | use OCP\Server; |
18 | 18 | |
19 | 19 | class DummyMountProvider implements IMountProvider { |
20 | - private $mounts = []; |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array $mounts |
|
24 | - */ |
|
25 | - public function __construct(array $mounts) { |
|
26 | - $this->mounts = $mounts; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Get the pre-registered mount points |
|
31 | - * |
|
32 | - * @param IUser $user |
|
33 | - * @param IStorageFactory $loader |
|
34 | - * @return \OCP\Files\Mount\IMountPoint[] |
|
35 | - */ |
|
36 | - public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
37 | - return isset($this->mounts[$user->getUID()]) ? $this->mounts[$user->getUID()] : []; |
|
38 | - } |
|
20 | + private $mounts = []; |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array $mounts |
|
24 | + */ |
|
25 | + public function __construct(array $mounts) { |
|
26 | + $this->mounts = $mounts; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Get the pre-registered mount points |
|
31 | + * |
|
32 | + * @param IUser $user |
|
33 | + * @param IStorageFactory $loader |
|
34 | + * @return \OCP\Files\Mount\IMountPoint[] |
|
35 | + */ |
|
36 | + public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
37 | + return isset($this->mounts[$user->getUID()]) ? $this->mounts[$user->getUID()] : []; |
|
38 | + } |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
@@ -46,433 +46,433 @@ discard block |
||
46 | 46 | * @package Test\Files |
47 | 47 | */ |
48 | 48 | class FilesystemTest extends \Test\TestCase { |
49 | - public const TEST_FILESYSTEM_USER1 = 'test-filesystem-user1'; |
|
50 | - public const TEST_FILESYSTEM_USER2 = 'test-filesystem-user1'; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var array tmpDirs |
|
54 | - */ |
|
55 | - private $tmpDirs = []; |
|
56 | - |
|
57 | - /** |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - private function getStorageData() { |
|
61 | - $dir = \OC::$server->getTempManager()->getTemporaryFolder(); |
|
62 | - $this->tmpDirs[] = $dir; |
|
63 | - return ['datadir' => $dir]; |
|
64 | - } |
|
65 | - |
|
66 | - protected function setUp(): void { |
|
67 | - parent::setUp(); |
|
68 | - $userBackend = new \Test\Util\User\Dummy(); |
|
69 | - $userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1); |
|
70 | - $userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2); |
|
71 | - \OC::$server->getUserManager()->registerBackend($userBackend); |
|
72 | - $this->loginAsUser(); |
|
73 | - } |
|
74 | - |
|
75 | - protected function tearDown(): void { |
|
76 | - foreach ($this->tmpDirs as $dir) { |
|
77 | - \OC_Helper::rmdirr($dir); |
|
78 | - } |
|
79 | - |
|
80 | - $this->logout(); |
|
81 | - $this->invokePrivate('\OC\Files\Filesystem', 'normalizedPathCache', [null]); |
|
82 | - parent::tearDown(); |
|
83 | - } |
|
84 | - |
|
85 | - public function testMount(): void { |
|
86 | - \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/'); |
|
87 | - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/')); |
|
88 | - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/some/folder')); |
|
89 | - [, $internalPath] = \OC\Files\Filesystem::resolvePath('/'); |
|
90 | - $this->assertEquals('', $internalPath); |
|
91 | - [, $internalPath] = \OC\Files\Filesystem::resolvePath('/some/folder'); |
|
92 | - $this->assertEquals('some/folder', $internalPath); |
|
93 | - |
|
94 | - \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/some'); |
|
95 | - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/')); |
|
96 | - $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/folder')); |
|
97 | - $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/')); |
|
98 | - $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some')); |
|
99 | - [, $internalPath] = \OC\Files\Filesystem::resolvePath('/some/folder'); |
|
100 | - $this->assertEquals('folder', $internalPath); |
|
101 | - } |
|
102 | - |
|
103 | - public static function normalizePathData(): array { |
|
104 | - return [ |
|
105 | - ['/', ''], |
|
106 | - ['/', '/'], |
|
107 | - ['/', '//'], |
|
108 | - ['/', '/', false], |
|
109 | - ['/', '//', false], |
|
110 | - |
|
111 | - ['/path', '/path/'], |
|
112 | - ['/path/', '/path/', false], |
|
113 | - ['/path', 'path'], |
|
114 | - |
|
115 | - ['/foo/bar', '/foo//bar/'], |
|
116 | - ['/foo/bar/', '/foo//bar/', false], |
|
117 | - ['/foo/bar', '/foo////bar'], |
|
118 | - ['/foo/bar', '/foo/////bar'], |
|
119 | - ['/foo/bar', '/foo/bar/.'], |
|
120 | - ['/foo/bar', '/foo/bar/./'], |
|
121 | - ['/foo/bar/', '/foo/bar/./', false], |
|
122 | - ['/foo/bar', '/foo/bar/./.'], |
|
123 | - ['/foo/bar', '/foo/bar/././'], |
|
124 | - ['/foo/bar/', '/foo/bar/././', false], |
|
125 | - ['/foo/bar', '/foo/./bar/'], |
|
126 | - ['/foo/bar/', '/foo/./bar/', false], |
|
127 | - ['/foo/.bar', '/foo/.bar/'], |
|
128 | - ['/foo/.bar/', '/foo/.bar/', false], |
|
129 | - ['/foo/.bar/tee', '/foo/.bar/tee'], |
|
130 | - ['/foo/bar.', '/foo/bar./'], |
|
131 | - ['/foo/bar./', '/foo/bar./', false], |
|
132 | - ['/foo/bar./tee', '/foo/bar./tee'], |
|
133 | - ['/foo/.bar.', '/foo/.bar./'], |
|
134 | - ['/foo/.bar./', '/foo/.bar./', false], |
|
135 | - ['/foo/.bar./tee', '/foo/.bar./tee'], |
|
136 | - |
|
137 | - ['/foo/bar', '/.////././//./foo/.///././//./bar/././/./.'], |
|
138 | - ['/foo/bar/', '/.////././//./foo/.///././//./bar/./././.', false], |
|
139 | - ['/foo/bar', '/.////././//./foo/.///././//./bar/././/././'], |
|
140 | - ['/foo/bar/', '/.////././//./foo/.///././//./bar/././/././', false], |
|
141 | - ['/foo/.bar', '/.////././//./foo/./././/./.bar/././/././'], |
|
142 | - ['/foo/.bar/', '/.////././//./foo/./././/./.bar/././/././', false], |
|
143 | - ['/foo/.bar/tee./', '/.////././//./foo/./././/./.bar/tee././/././', false], |
|
144 | - ['/foo/bar.', '/.////././//./foo/./././/./bar./././/././'], |
|
145 | - ['/foo/bar./', '/.////././//./foo/./././/./bar./././/././', false], |
|
146 | - ['/foo/bar./tee./', '/.////././//./foo/./././/./bar./tee././/././', false], |
|
147 | - ['/foo/.bar.', '/.////././//./foo/./././/./.bar./././/././'], |
|
148 | - ['/foo/.bar./', '/.////././//./foo/./././/./.bar./././././', false], |
|
149 | - ['/foo/.bar./tee./', '/.////././//./foo/./././/./.bar./tee././././', false], |
|
150 | - |
|
151 | - // Windows paths |
|
152 | - ['/', ''], |
|
153 | - ['/', '\\'], |
|
154 | - ['/', '\\', false], |
|
155 | - ['/', '\\\\'], |
|
156 | - ['/', '\\\\', false], |
|
157 | - |
|
158 | - ['/path', '\\path'], |
|
159 | - ['/path', '\\path', false], |
|
160 | - ['/path', '\\path\\'], |
|
161 | - ['/path/', '\\path\\', false], |
|
162 | - |
|
163 | - ['/foo/bar', '\\foo\\\\bar\\'], |
|
164 | - ['/foo/bar/', '\\foo\\\\bar\\', false], |
|
165 | - ['/foo/bar', '\\foo\\\\\\\\bar'], |
|
166 | - ['/foo/bar', '\\foo\\\\\\\\\\bar'], |
|
167 | - ['/foo/bar', '\\foo\\bar\\.'], |
|
168 | - ['/foo/bar', '\\foo\\bar\\.\\'], |
|
169 | - ['/foo/bar/', '\\foo\\bar\\.\\', false], |
|
170 | - ['/foo/bar', '\\foo\\bar\\.\\.'], |
|
171 | - ['/foo/bar', '\\foo\\bar\\.\\.\\'], |
|
172 | - ['/foo/bar/', '\\foo\\bar\\.\\.\\', false], |
|
173 | - ['/foo/bar', '\\foo\\.\\bar\\'], |
|
174 | - ['/foo/bar/', '\\foo\\.\\bar\\', false], |
|
175 | - ['/foo/.bar', '\\foo\\.bar\\'], |
|
176 | - ['/foo/.bar/', '\\foo\\.bar\\', false], |
|
177 | - ['/foo/.bar/tee', '\\foo\\.bar\\tee'], |
|
178 | - |
|
179 | - // Absolute windows paths NOT marked as absolute |
|
180 | - ['/C:', 'C:\\'], |
|
181 | - ['/C:/', 'C:\\', false], |
|
182 | - ['/C:/tests', 'C:\\tests'], |
|
183 | - ['/C:/tests', 'C:\\tests', false], |
|
184 | - ['/C:/tests', 'C:\\tests\\'], |
|
185 | - ['/C:/tests/', 'C:\\tests\\', false], |
|
186 | - ['/C:/tests/bar', 'C:\\tests\\.\\.\\bar'], |
|
187 | - ['/C:/tests/bar/', 'C:\\tests\\.\\.\\bar\\.\\', false], |
|
188 | - |
|
189 | - // normalize does not resolve '..' (by design) |
|
190 | - ['/foo/..', '/foo/../'], |
|
191 | - ['/foo/../bar', '/foo/../bar/.'], |
|
192 | - ['/foo/..', '\\foo\\..\\'], |
|
193 | - ['/foo/../bar', '\\foo\\..\\bar'], |
|
194 | - ]; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @dataProvider normalizePathData |
|
199 | - */ |
|
200 | - public function testNormalizePath($expected, $path, $stripTrailingSlash = true): void { |
|
201 | - $this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash)); |
|
202 | - } |
|
203 | - |
|
204 | - public static function normalizePathKeepUnicodeData(): array { |
|
205 | - $nfdName = 'ümlaut'; |
|
206 | - $nfcName = 'ümlaut'; |
|
207 | - return [ |
|
208 | - ['/' . $nfcName, $nfcName, true], |
|
209 | - ['/' . $nfcName, $nfcName, false], |
|
210 | - ['/' . $nfdName, $nfdName, true], |
|
211 | - ['/' . $nfcName, $nfdName, false], |
|
212 | - ]; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @dataProvider normalizePathKeepUnicodeData |
|
217 | - */ |
|
218 | - public function testNormalizePathKeepUnicode($expected, $path, $keepUnicode = false): void { |
|
219 | - $this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, true, false, $keepUnicode)); |
|
220 | - } |
|
221 | - |
|
222 | - public function testNormalizePathKeepUnicodeCache(): void { |
|
223 | - $nfdName = 'ümlaut'; |
|
224 | - $nfcName = 'ümlaut'; |
|
225 | - // call in succession due to cache |
|
226 | - $this->assertEquals('/' . $nfcName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, false)); |
|
227 | - $this->assertEquals('/' . $nfdName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, true)); |
|
228 | - } |
|
229 | - |
|
230 | - public static function isValidPathData(): array { |
|
231 | - return [ |
|
232 | - ['/', true], |
|
233 | - ['/path', true], |
|
234 | - ['/foo/bar', true], |
|
235 | - ['/foo//bar/', true], |
|
236 | - ['/foo////bar', true], |
|
237 | - ['/foo//\///bar', true], |
|
238 | - ['/foo/bar/.', true], |
|
239 | - ['/foo/bar/./', true], |
|
240 | - ['/foo/bar/./.', true], |
|
241 | - ['/foo/bar/././', true], |
|
242 | - ['/foo/bar/././..bar', true], |
|
243 | - ['/foo/bar/././..bar/a', true], |
|
244 | - ['/foo/bar/././..', false], |
|
245 | - ['/foo/bar/././../', false], |
|
246 | - ['/foo/bar/.././', false], |
|
247 | - ['/foo/bar/../../', false], |
|
248 | - ['/foo/bar/../..\\', false], |
|
249 | - ['..', false], |
|
250 | - ['../', false], |
|
251 | - ['../foo/bar', false], |
|
252 | - ['..\foo/bar', false], |
|
253 | - ]; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @dataProvider isValidPathData |
|
258 | - */ |
|
259 | - public function testIsValidPath($path, $expected): void { |
|
260 | - $this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path)); |
|
261 | - } |
|
262 | - |
|
263 | - public static function isFileBlacklistedData(): array { |
|
264 | - return [ |
|
265 | - ['/etc/foo/bar/foo.txt', false], |
|
266 | - ['\etc\foo/bar\foo.txt', false], |
|
267 | - ['.htaccess', true], |
|
268 | - ['.htaccess/', true], |
|
269 | - ['.htaccess\\', true], |
|
270 | - ['/etc/foo\bar/.htaccess\\', true], |
|
271 | - ['/etc/foo\bar/.htaccess/', true], |
|
272 | - ['/etc/foo\bar/.htaccess/foo', false], |
|
273 | - ['//foo//bar/\.htaccess/', true], |
|
274 | - ['\foo\bar\.HTAccess', true], |
|
275 | - ]; |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @dataProvider isFileBlacklistedData |
|
280 | - */ |
|
281 | - public function testIsFileBlacklisted($path, $expected): void { |
|
282 | - $this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path)); |
|
283 | - } |
|
284 | - |
|
285 | - public function testNormalizePathUTF8(): void { |
|
286 | - if (!class_exists('Patchwork\PHP\Shim\Normalizer')) { |
|
287 | - $this->markTestSkipped('UTF8 normalizer Patchwork was not found'); |
|
288 | - } |
|
289 | - |
|
290 | - $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88")); |
|
291 | - $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88")); |
|
292 | - } |
|
293 | - |
|
294 | - public function testHooks(): void { |
|
295 | - if (\OC\Files\Filesystem::getView()) { |
|
296 | - $user = \OC_User::getUser(); |
|
297 | - } else { |
|
298 | - $user = self::TEST_FILESYSTEM_USER1; |
|
299 | - $backend = new \Test\Util\User\Dummy(); |
|
300 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
301 | - $backend->createUser($user, $user); |
|
302 | - $userObj = \OC::$server->getUserManager()->get($user); |
|
303 | - \OC::$server->getUserSession()->setUser($userObj); |
|
304 | - \OC\Files\Filesystem::init($user, '/' . $user . '/files'); |
|
305 | - } |
|
306 | - \OC_Hook::clear('OC_Filesystem'); |
|
307 | - \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
|
308 | - |
|
309 | - \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', [], '/'); |
|
310 | - |
|
311 | - $rootView = new \OC\Files\View(''); |
|
312 | - $rootView->mkdir('/' . $user); |
|
313 | - $rootView->mkdir('/' . $user . '/files'); |
|
314 | - |
|
315 | - // \OC\Files\Filesystem::file_put_contents('/foo', 'foo'); |
|
316 | - \OC\Files\Filesystem::mkdir('/bar'); |
|
317 | - // \OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo'); |
|
318 | - |
|
319 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile(); |
|
320 | - file_put_contents($tmpFile, 'foo'); |
|
321 | - $fh = fopen($tmpFile, 'r'); |
|
322 | - // \OC\Files\Filesystem::file_put_contents('/bar//foo', $fh); |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Tests that an exception is thrown when passed user does not exist. |
|
327 | - * |
|
328 | - */ |
|
329 | - public function testLocalMountWhenUserDoesNotExist(): void { |
|
330 | - $this->expectException(\OC\User\NoUserException::class); |
|
331 | - |
|
332 | - $userId = $this->getUniqueID('user_'); |
|
333 | - |
|
334 | - \OC\Files\Filesystem::initMountPoints($userId); |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - public function testNullUserThrows(): void { |
|
339 | - $this->expectException(\OC\User\NoUserException::class); |
|
340 | - |
|
341 | - \OC\Files\Filesystem::initMountPoints(null); |
|
342 | - } |
|
343 | - |
|
344 | - public function testNullUserThrowsTwice(): void { |
|
345 | - $thrown = 0; |
|
346 | - try { |
|
347 | - \OC\Files\Filesystem::initMountPoints(null); |
|
348 | - } catch (NoUserException $e) { |
|
349 | - $thrown++; |
|
350 | - } |
|
351 | - try { |
|
352 | - \OC\Files\Filesystem::initMountPoints(null); |
|
353 | - } catch (NoUserException $e) { |
|
354 | - $thrown++; |
|
355 | - } |
|
356 | - $this->assertEquals(2, $thrown); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Tests that an exception is thrown when passed user does not exist. |
|
361 | - */ |
|
362 | - public function testLocalMountWhenUserDoesNotExistTwice(): void { |
|
363 | - $thrown = 0; |
|
364 | - $userId = $this->getUniqueID('user_'); |
|
365 | - |
|
366 | - try { |
|
367 | - \OC\Files\Filesystem::initMountPoints($userId); |
|
368 | - } catch (NoUserException $e) { |
|
369 | - $thrown++; |
|
370 | - } |
|
371 | - |
|
372 | - try { |
|
373 | - \OC\Files\Filesystem::initMountPoints($userId); |
|
374 | - } catch (NoUserException $e) { |
|
375 | - $thrown++; |
|
376 | - } |
|
377 | - |
|
378 | - $this->assertEquals(2, $thrown); |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Tests that the home storage is used for the user's mount point |
|
383 | - */ |
|
384 | - public function testHomeMount(): void { |
|
385 | - $userId = $this->getUniqueID('user_'); |
|
386 | - |
|
387 | - \OC::$server->getUserManager()->createUser($userId, $userId); |
|
388 | - |
|
389 | - \OC\Files\Filesystem::initMountPoints($userId); |
|
390 | - |
|
391 | - $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); |
|
392 | - |
|
393 | - $this->assertTrue($homeMount->instanceOfStorage('\OCP\Files\IHomeStorage')); |
|
394 | - if ($homeMount->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
395 | - $this->assertEquals('object::user:' . $userId, $homeMount->getId()); |
|
396 | - } elseif ($homeMount->instanceOfStorage('\OC\Files\Storage\Home')) { |
|
397 | - $this->assertEquals('home::' . $userId, $homeMount->getId()); |
|
398 | - } |
|
399 | - |
|
400 | - $user = \OC::$server->getUserManager()->get($userId); |
|
401 | - if ($user !== null) { |
|
402 | - $user->delete(); |
|
403 | - } |
|
404 | - } |
|
405 | - |
|
406 | - public function dummyHook($arguments) { |
|
407 | - $path = $arguments['path']; |
|
408 | - $this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Test that the default cache dir is part of the user's home |
|
413 | - */ |
|
414 | - public function testMountDefaultCacheDir(): void { |
|
415 | - $userId = $this->getUniqueID('user_'); |
|
416 | - $config = \OC::$server->getConfig(); |
|
417 | - $oldCachePath = $config->getSystemValueString('cache_path', ''); |
|
418 | - // no cache path configured |
|
419 | - $config->setSystemValue('cache_path', ''); |
|
420 | - |
|
421 | - \OC::$server->getUserManager()->createUser($userId, $userId); |
|
422 | - \OC\Files\Filesystem::initMountPoints($userId); |
|
423 | - |
|
424 | - $this->assertEquals( |
|
425 | - '/' . $userId . '/', |
|
426 | - \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') |
|
427 | - ); |
|
428 | - [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); |
|
429 | - $this->assertTrue($storage->instanceOfStorage('\OCP\Files\IHomeStorage')); |
|
430 | - $this->assertEquals('cache', $internalPath); |
|
431 | - $user = \OC::$server->getUserManager()->get($userId); |
|
432 | - if ($user !== null) { |
|
433 | - $user->delete(); |
|
434 | - } |
|
435 | - |
|
436 | - $config->setSystemValue('cache_path', $oldCachePath); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Test that an external cache is mounted into |
|
441 | - * the user's home |
|
442 | - */ |
|
443 | - public function testMountExternalCacheDir(): void { |
|
444 | - $userId = $this->getUniqueID('user_'); |
|
445 | - |
|
446 | - $config = \OC::$server->getConfig(); |
|
447 | - $oldCachePath = $config->getSystemValueString('cache_path', ''); |
|
448 | - // set cache path to temp dir |
|
449 | - $cachePath = \OC::$server->getTempManager()->getTemporaryFolder() . '/extcache'; |
|
450 | - $config->setSystemValue('cache_path', $cachePath); |
|
451 | - |
|
452 | - \OC::$server->getUserManager()->createUser($userId, $userId); |
|
453 | - \OC\Files\Filesystem::initMountPoints($userId); |
|
454 | - |
|
455 | - $this->assertEquals( |
|
456 | - '/' . $userId . '/cache/', |
|
457 | - \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') |
|
458 | - ); |
|
459 | - [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); |
|
460 | - $this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Local')); |
|
461 | - $this->assertEquals('', $internalPath); |
|
462 | - $user = \OC::$server->getUserManager()->get($userId); |
|
463 | - if ($user !== null) { |
|
464 | - $user->delete(); |
|
465 | - } |
|
466 | - |
|
467 | - $config->setSystemValue('cache_path', $oldCachePath); |
|
468 | - } |
|
469 | - |
|
470 | - public function testRegisterMountProviderAfterSetup(): void { |
|
471 | - \OC\Files\Filesystem::initMountPoints(self::TEST_FILESYSTEM_USER2); |
|
472 | - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); |
|
473 | - $mount = new MountPoint(new Temporary([]), '/foo/bar'); |
|
474 | - $mountProvider = new DummyMountProvider([self::TEST_FILESYSTEM_USER2 => [$mount]]); |
|
475 | - \OC::$server->getMountProviderCollection()->registerProvider($mountProvider); |
|
476 | - $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); |
|
477 | - } |
|
49 | + public const TEST_FILESYSTEM_USER1 = 'test-filesystem-user1'; |
|
50 | + public const TEST_FILESYSTEM_USER2 = 'test-filesystem-user1'; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var array tmpDirs |
|
54 | + */ |
|
55 | + private $tmpDirs = []; |
|
56 | + |
|
57 | + /** |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + private function getStorageData() { |
|
61 | + $dir = \OC::$server->getTempManager()->getTemporaryFolder(); |
|
62 | + $this->tmpDirs[] = $dir; |
|
63 | + return ['datadir' => $dir]; |
|
64 | + } |
|
65 | + |
|
66 | + protected function setUp(): void { |
|
67 | + parent::setUp(); |
|
68 | + $userBackend = new \Test\Util\User\Dummy(); |
|
69 | + $userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1); |
|
70 | + $userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2); |
|
71 | + \OC::$server->getUserManager()->registerBackend($userBackend); |
|
72 | + $this->loginAsUser(); |
|
73 | + } |
|
74 | + |
|
75 | + protected function tearDown(): void { |
|
76 | + foreach ($this->tmpDirs as $dir) { |
|
77 | + \OC_Helper::rmdirr($dir); |
|
78 | + } |
|
79 | + |
|
80 | + $this->logout(); |
|
81 | + $this->invokePrivate('\OC\Files\Filesystem', 'normalizedPathCache', [null]); |
|
82 | + parent::tearDown(); |
|
83 | + } |
|
84 | + |
|
85 | + public function testMount(): void { |
|
86 | + \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/'); |
|
87 | + $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/')); |
|
88 | + $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/some/folder')); |
|
89 | + [, $internalPath] = \OC\Files\Filesystem::resolvePath('/'); |
|
90 | + $this->assertEquals('', $internalPath); |
|
91 | + [, $internalPath] = \OC\Files\Filesystem::resolvePath('/some/folder'); |
|
92 | + $this->assertEquals('some/folder', $internalPath); |
|
93 | + |
|
94 | + \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/some'); |
|
95 | + $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/')); |
|
96 | + $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/folder')); |
|
97 | + $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/')); |
|
98 | + $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some')); |
|
99 | + [, $internalPath] = \OC\Files\Filesystem::resolvePath('/some/folder'); |
|
100 | + $this->assertEquals('folder', $internalPath); |
|
101 | + } |
|
102 | + |
|
103 | + public static function normalizePathData(): array { |
|
104 | + return [ |
|
105 | + ['/', ''], |
|
106 | + ['/', '/'], |
|
107 | + ['/', '//'], |
|
108 | + ['/', '/', false], |
|
109 | + ['/', '//', false], |
|
110 | + |
|
111 | + ['/path', '/path/'], |
|
112 | + ['/path/', '/path/', false], |
|
113 | + ['/path', 'path'], |
|
114 | + |
|
115 | + ['/foo/bar', '/foo//bar/'], |
|
116 | + ['/foo/bar/', '/foo//bar/', false], |
|
117 | + ['/foo/bar', '/foo////bar'], |
|
118 | + ['/foo/bar', '/foo/////bar'], |
|
119 | + ['/foo/bar', '/foo/bar/.'], |
|
120 | + ['/foo/bar', '/foo/bar/./'], |
|
121 | + ['/foo/bar/', '/foo/bar/./', false], |
|
122 | + ['/foo/bar', '/foo/bar/./.'], |
|
123 | + ['/foo/bar', '/foo/bar/././'], |
|
124 | + ['/foo/bar/', '/foo/bar/././', false], |
|
125 | + ['/foo/bar', '/foo/./bar/'], |
|
126 | + ['/foo/bar/', '/foo/./bar/', false], |
|
127 | + ['/foo/.bar', '/foo/.bar/'], |
|
128 | + ['/foo/.bar/', '/foo/.bar/', false], |
|
129 | + ['/foo/.bar/tee', '/foo/.bar/tee'], |
|
130 | + ['/foo/bar.', '/foo/bar./'], |
|
131 | + ['/foo/bar./', '/foo/bar./', false], |
|
132 | + ['/foo/bar./tee', '/foo/bar./tee'], |
|
133 | + ['/foo/.bar.', '/foo/.bar./'], |
|
134 | + ['/foo/.bar./', '/foo/.bar./', false], |
|
135 | + ['/foo/.bar./tee', '/foo/.bar./tee'], |
|
136 | + |
|
137 | + ['/foo/bar', '/.////././//./foo/.///././//./bar/././/./.'], |
|
138 | + ['/foo/bar/', '/.////././//./foo/.///././//./bar/./././.', false], |
|
139 | + ['/foo/bar', '/.////././//./foo/.///././//./bar/././/././'], |
|
140 | + ['/foo/bar/', '/.////././//./foo/.///././//./bar/././/././', false], |
|
141 | + ['/foo/.bar', '/.////././//./foo/./././/./.bar/././/././'], |
|
142 | + ['/foo/.bar/', '/.////././//./foo/./././/./.bar/././/././', false], |
|
143 | + ['/foo/.bar/tee./', '/.////././//./foo/./././/./.bar/tee././/././', false], |
|
144 | + ['/foo/bar.', '/.////././//./foo/./././/./bar./././/././'], |
|
145 | + ['/foo/bar./', '/.////././//./foo/./././/./bar./././/././', false], |
|
146 | + ['/foo/bar./tee./', '/.////././//./foo/./././/./bar./tee././/././', false], |
|
147 | + ['/foo/.bar.', '/.////././//./foo/./././/./.bar./././/././'], |
|
148 | + ['/foo/.bar./', '/.////././//./foo/./././/./.bar./././././', false], |
|
149 | + ['/foo/.bar./tee./', '/.////././//./foo/./././/./.bar./tee././././', false], |
|
150 | + |
|
151 | + // Windows paths |
|
152 | + ['/', ''], |
|
153 | + ['/', '\\'], |
|
154 | + ['/', '\\', false], |
|
155 | + ['/', '\\\\'], |
|
156 | + ['/', '\\\\', false], |
|
157 | + |
|
158 | + ['/path', '\\path'], |
|
159 | + ['/path', '\\path', false], |
|
160 | + ['/path', '\\path\\'], |
|
161 | + ['/path/', '\\path\\', false], |
|
162 | + |
|
163 | + ['/foo/bar', '\\foo\\\\bar\\'], |
|
164 | + ['/foo/bar/', '\\foo\\\\bar\\', false], |
|
165 | + ['/foo/bar', '\\foo\\\\\\\\bar'], |
|
166 | + ['/foo/bar', '\\foo\\\\\\\\\\bar'], |
|
167 | + ['/foo/bar', '\\foo\\bar\\.'], |
|
168 | + ['/foo/bar', '\\foo\\bar\\.\\'], |
|
169 | + ['/foo/bar/', '\\foo\\bar\\.\\', false], |
|
170 | + ['/foo/bar', '\\foo\\bar\\.\\.'], |
|
171 | + ['/foo/bar', '\\foo\\bar\\.\\.\\'], |
|
172 | + ['/foo/bar/', '\\foo\\bar\\.\\.\\', false], |
|
173 | + ['/foo/bar', '\\foo\\.\\bar\\'], |
|
174 | + ['/foo/bar/', '\\foo\\.\\bar\\', false], |
|
175 | + ['/foo/.bar', '\\foo\\.bar\\'], |
|
176 | + ['/foo/.bar/', '\\foo\\.bar\\', false], |
|
177 | + ['/foo/.bar/tee', '\\foo\\.bar\\tee'], |
|
178 | + |
|
179 | + // Absolute windows paths NOT marked as absolute |
|
180 | + ['/C:', 'C:\\'], |
|
181 | + ['/C:/', 'C:\\', false], |
|
182 | + ['/C:/tests', 'C:\\tests'], |
|
183 | + ['/C:/tests', 'C:\\tests', false], |
|
184 | + ['/C:/tests', 'C:\\tests\\'], |
|
185 | + ['/C:/tests/', 'C:\\tests\\', false], |
|
186 | + ['/C:/tests/bar', 'C:\\tests\\.\\.\\bar'], |
|
187 | + ['/C:/tests/bar/', 'C:\\tests\\.\\.\\bar\\.\\', false], |
|
188 | + |
|
189 | + // normalize does not resolve '..' (by design) |
|
190 | + ['/foo/..', '/foo/../'], |
|
191 | + ['/foo/../bar', '/foo/../bar/.'], |
|
192 | + ['/foo/..', '\\foo\\..\\'], |
|
193 | + ['/foo/../bar', '\\foo\\..\\bar'], |
|
194 | + ]; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @dataProvider normalizePathData |
|
199 | + */ |
|
200 | + public function testNormalizePath($expected, $path, $stripTrailingSlash = true): void { |
|
201 | + $this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash)); |
|
202 | + } |
|
203 | + |
|
204 | + public static function normalizePathKeepUnicodeData(): array { |
|
205 | + $nfdName = 'ümlaut'; |
|
206 | + $nfcName = 'ümlaut'; |
|
207 | + return [ |
|
208 | + ['/' . $nfcName, $nfcName, true], |
|
209 | + ['/' . $nfcName, $nfcName, false], |
|
210 | + ['/' . $nfdName, $nfdName, true], |
|
211 | + ['/' . $nfcName, $nfdName, false], |
|
212 | + ]; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @dataProvider normalizePathKeepUnicodeData |
|
217 | + */ |
|
218 | + public function testNormalizePathKeepUnicode($expected, $path, $keepUnicode = false): void { |
|
219 | + $this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, true, false, $keepUnicode)); |
|
220 | + } |
|
221 | + |
|
222 | + public function testNormalizePathKeepUnicodeCache(): void { |
|
223 | + $nfdName = 'ümlaut'; |
|
224 | + $nfcName = 'ümlaut'; |
|
225 | + // call in succession due to cache |
|
226 | + $this->assertEquals('/' . $nfcName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, false)); |
|
227 | + $this->assertEquals('/' . $nfdName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, true)); |
|
228 | + } |
|
229 | + |
|
230 | + public static function isValidPathData(): array { |
|
231 | + return [ |
|
232 | + ['/', true], |
|
233 | + ['/path', true], |
|
234 | + ['/foo/bar', true], |
|
235 | + ['/foo//bar/', true], |
|
236 | + ['/foo////bar', true], |
|
237 | + ['/foo//\///bar', true], |
|
238 | + ['/foo/bar/.', true], |
|
239 | + ['/foo/bar/./', true], |
|
240 | + ['/foo/bar/./.', true], |
|
241 | + ['/foo/bar/././', true], |
|
242 | + ['/foo/bar/././..bar', true], |
|
243 | + ['/foo/bar/././..bar/a', true], |
|
244 | + ['/foo/bar/././..', false], |
|
245 | + ['/foo/bar/././../', false], |
|
246 | + ['/foo/bar/.././', false], |
|
247 | + ['/foo/bar/../../', false], |
|
248 | + ['/foo/bar/../..\\', false], |
|
249 | + ['..', false], |
|
250 | + ['../', false], |
|
251 | + ['../foo/bar', false], |
|
252 | + ['..\foo/bar', false], |
|
253 | + ]; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @dataProvider isValidPathData |
|
258 | + */ |
|
259 | + public function testIsValidPath($path, $expected): void { |
|
260 | + $this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path)); |
|
261 | + } |
|
262 | + |
|
263 | + public static function isFileBlacklistedData(): array { |
|
264 | + return [ |
|
265 | + ['/etc/foo/bar/foo.txt', false], |
|
266 | + ['\etc\foo/bar\foo.txt', false], |
|
267 | + ['.htaccess', true], |
|
268 | + ['.htaccess/', true], |
|
269 | + ['.htaccess\\', true], |
|
270 | + ['/etc/foo\bar/.htaccess\\', true], |
|
271 | + ['/etc/foo\bar/.htaccess/', true], |
|
272 | + ['/etc/foo\bar/.htaccess/foo', false], |
|
273 | + ['//foo//bar/\.htaccess/', true], |
|
274 | + ['\foo\bar\.HTAccess', true], |
|
275 | + ]; |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @dataProvider isFileBlacklistedData |
|
280 | + */ |
|
281 | + public function testIsFileBlacklisted($path, $expected): void { |
|
282 | + $this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path)); |
|
283 | + } |
|
284 | + |
|
285 | + public function testNormalizePathUTF8(): void { |
|
286 | + if (!class_exists('Patchwork\PHP\Shim\Normalizer')) { |
|
287 | + $this->markTestSkipped('UTF8 normalizer Patchwork was not found'); |
|
288 | + } |
|
289 | + |
|
290 | + $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88")); |
|
291 | + $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88")); |
|
292 | + } |
|
293 | + |
|
294 | + public function testHooks(): void { |
|
295 | + if (\OC\Files\Filesystem::getView()) { |
|
296 | + $user = \OC_User::getUser(); |
|
297 | + } else { |
|
298 | + $user = self::TEST_FILESYSTEM_USER1; |
|
299 | + $backend = new \Test\Util\User\Dummy(); |
|
300 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
301 | + $backend->createUser($user, $user); |
|
302 | + $userObj = \OC::$server->getUserManager()->get($user); |
|
303 | + \OC::$server->getUserSession()->setUser($userObj); |
|
304 | + \OC\Files\Filesystem::init($user, '/' . $user . '/files'); |
|
305 | + } |
|
306 | + \OC_Hook::clear('OC_Filesystem'); |
|
307 | + \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
|
308 | + |
|
309 | + \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', [], '/'); |
|
310 | + |
|
311 | + $rootView = new \OC\Files\View(''); |
|
312 | + $rootView->mkdir('/' . $user); |
|
313 | + $rootView->mkdir('/' . $user . '/files'); |
|
314 | + |
|
315 | + // \OC\Files\Filesystem::file_put_contents('/foo', 'foo'); |
|
316 | + \OC\Files\Filesystem::mkdir('/bar'); |
|
317 | + // \OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo'); |
|
318 | + |
|
319 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile(); |
|
320 | + file_put_contents($tmpFile, 'foo'); |
|
321 | + $fh = fopen($tmpFile, 'r'); |
|
322 | + // \OC\Files\Filesystem::file_put_contents('/bar//foo', $fh); |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Tests that an exception is thrown when passed user does not exist. |
|
327 | + * |
|
328 | + */ |
|
329 | + public function testLocalMountWhenUserDoesNotExist(): void { |
|
330 | + $this->expectException(\OC\User\NoUserException::class); |
|
331 | + |
|
332 | + $userId = $this->getUniqueID('user_'); |
|
333 | + |
|
334 | + \OC\Files\Filesystem::initMountPoints($userId); |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + public function testNullUserThrows(): void { |
|
339 | + $this->expectException(\OC\User\NoUserException::class); |
|
340 | + |
|
341 | + \OC\Files\Filesystem::initMountPoints(null); |
|
342 | + } |
|
343 | + |
|
344 | + public function testNullUserThrowsTwice(): void { |
|
345 | + $thrown = 0; |
|
346 | + try { |
|
347 | + \OC\Files\Filesystem::initMountPoints(null); |
|
348 | + } catch (NoUserException $e) { |
|
349 | + $thrown++; |
|
350 | + } |
|
351 | + try { |
|
352 | + \OC\Files\Filesystem::initMountPoints(null); |
|
353 | + } catch (NoUserException $e) { |
|
354 | + $thrown++; |
|
355 | + } |
|
356 | + $this->assertEquals(2, $thrown); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Tests that an exception is thrown when passed user does not exist. |
|
361 | + */ |
|
362 | + public function testLocalMountWhenUserDoesNotExistTwice(): void { |
|
363 | + $thrown = 0; |
|
364 | + $userId = $this->getUniqueID('user_'); |
|
365 | + |
|
366 | + try { |
|
367 | + \OC\Files\Filesystem::initMountPoints($userId); |
|
368 | + } catch (NoUserException $e) { |
|
369 | + $thrown++; |
|
370 | + } |
|
371 | + |
|
372 | + try { |
|
373 | + \OC\Files\Filesystem::initMountPoints($userId); |
|
374 | + } catch (NoUserException $e) { |
|
375 | + $thrown++; |
|
376 | + } |
|
377 | + |
|
378 | + $this->assertEquals(2, $thrown); |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Tests that the home storage is used for the user's mount point |
|
383 | + */ |
|
384 | + public function testHomeMount(): void { |
|
385 | + $userId = $this->getUniqueID('user_'); |
|
386 | + |
|
387 | + \OC::$server->getUserManager()->createUser($userId, $userId); |
|
388 | + |
|
389 | + \OC\Files\Filesystem::initMountPoints($userId); |
|
390 | + |
|
391 | + $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); |
|
392 | + |
|
393 | + $this->assertTrue($homeMount->instanceOfStorage('\OCP\Files\IHomeStorage')); |
|
394 | + if ($homeMount->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
395 | + $this->assertEquals('object::user:' . $userId, $homeMount->getId()); |
|
396 | + } elseif ($homeMount->instanceOfStorage('\OC\Files\Storage\Home')) { |
|
397 | + $this->assertEquals('home::' . $userId, $homeMount->getId()); |
|
398 | + } |
|
399 | + |
|
400 | + $user = \OC::$server->getUserManager()->get($userId); |
|
401 | + if ($user !== null) { |
|
402 | + $user->delete(); |
|
403 | + } |
|
404 | + } |
|
405 | + |
|
406 | + public function dummyHook($arguments) { |
|
407 | + $path = $arguments['path']; |
|
408 | + $this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Test that the default cache dir is part of the user's home |
|
413 | + */ |
|
414 | + public function testMountDefaultCacheDir(): void { |
|
415 | + $userId = $this->getUniqueID('user_'); |
|
416 | + $config = \OC::$server->getConfig(); |
|
417 | + $oldCachePath = $config->getSystemValueString('cache_path', ''); |
|
418 | + // no cache path configured |
|
419 | + $config->setSystemValue('cache_path', ''); |
|
420 | + |
|
421 | + \OC::$server->getUserManager()->createUser($userId, $userId); |
|
422 | + \OC\Files\Filesystem::initMountPoints($userId); |
|
423 | + |
|
424 | + $this->assertEquals( |
|
425 | + '/' . $userId . '/', |
|
426 | + \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') |
|
427 | + ); |
|
428 | + [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); |
|
429 | + $this->assertTrue($storage->instanceOfStorage('\OCP\Files\IHomeStorage')); |
|
430 | + $this->assertEquals('cache', $internalPath); |
|
431 | + $user = \OC::$server->getUserManager()->get($userId); |
|
432 | + if ($user !== null) { |
|
433 | + $user->delete(); |
|
434 | + } |
|
435 | + |
|
436 | + $config->setSystemValue('cache_path', $oldCachePath); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Test that an external cache is mounted into |
|
441 | + * the user's home |
|
442 | + */ |
|
443 | + public function testMountExternalCacheDir(): void { |
|
444 | + $userId = $this->getUniqueID('user_'); |
|
445 | + |
|
446 | + $config = \OC::$server->getConfig(); |
|
447 | + $oldCachePath = $config->getSystemValueString('cache_path', ''); |
|
448 | + // set cache path to temp dir |
|
449 | + $cachePath = \OC::$server->getTempManager()->getTemporaryFolder() . '/extcache'; |
|
450 | + $config->setSystemValue('cache_path', $cachePath); |
|
451 | + |
|
452 | + \OC::$server->getUserManager()->createUser($userId, $userId); |
|
453 | + \OC\Files\Filesystem::initMountPoints($userId); |
|
454 | + |
|
455 | + $this->assertEquals( |
|
456 | + '/' . $userId . '/cache/', |
|
457 | + \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') |
|
458 | + ); |
|
459 | + [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); |
|
460 | + $this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Local')); |
|
461 | + $this->assertEquals('', $internalPath); |
|
462 | + $user = \OC::$server->getUserManager()->get($userId); |
|
463 | + if ($user !== null) { |
|
464 | + $user->delete(); |
|
465 | + } |
|
466 | + |
|
467 | + $config->setSystemValue('cache_path', $oldCachePath); |
|
468 | + } |
|
469 | + |
|
470 | + public function testRegisterMountProviderAfterSetup(): void { |
|
471 | + \OC\Files\Filesystem::initMountPoints(self::TEST_FILESYSTEM_USER2); |
|
472 | + $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); |
|
473 | + $mount = new MountPoint(new Temporary([]), '/foo/bar'); |
|
474 | + $mountProvider = new DummyMountProvider([self::TEST_FILESYSTEM_USER2 => [$mount]]); |
|
475 | + \OC::$server->getMountProviderCollection()->registerProvider($mountProvider); |
|
476 | + $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); |
|
477 | + } |
|
478 | 478 | } |
@@ -205,10 +205,10 @@ discard block |
||
205 | 205 | $nfdName = 'ümlaut'; |
206 | 206 | $nfcName = 'ümlaut'; |
207 | 207 | return [ |
208 | - ['/' . $nfcName, $nfcName, true], |
|
209 | - ['/' . $nfcName, $nfcName, false], |
|
210 | - ['/' . $nfdName, $nfdName, true], |
|
211 | - ['/' . $nfcName, $nfdName, false], |
|
208 | + ['/'.$nfcName, $nfcName, true], |
|
209 | + ['/'.$nfcName, $nfcName, false], |
|
210 | + ['/'.$nfdName, $nfdName, true], |
|
211 | + ['/'.$nfcName, $nfdName, false], |
|
212 | 212 | ]; |
213 | 213 | } |
214 | 214 | |
@@ -223,8 +223,8 @@ discard block |
||
223 | 223 | $nfdName = 'ümlaut'; |
224 | 224 | $nfcName = 'ümlaut'; |
225 | 225 | // call in succession due to cache |
226 | - $this->assertEquals('/' . $nfcName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, false)); |
|
227 | - $this->assertEquals('/' . $nfdName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, true)); |
|
226 | + $this->assertEquals('/'.$nfcName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, false)); |
|
227 | + $this->assertEquals('/'.$nfdName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, true)); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | public static function isValidPathData(): array { |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | $backend->createUser($user, $user); |
302 | 302 | $userObj = \OC::$server->getUserManager()->get($user); |
303 | 303 | \OC::$server->getUserSession()->setUser($userObj); |
304 | - \OC\Files\Filesystem::init($user, '/' . $user . '/files'); |
|
304 | + \OC\Files\Filesystem::init($user, '/'.$user.'/files'); |
|
305 | 305 | } |
306 | 306 | \OC_Hook::clear('OC_Filesystem'); |
307 | 307 | \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
@@ -309,8 +309,8 @@ discard block |
||
309 | 309 | \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', [], '/'); |
310 | 310 | |
311 | 311 | $rootView = new \OC\Files\View(''); |
312 | - $rootView->mkdir('/' . $user); |
|
313 | - $rootView->mkdir('/' . $user . '/files'); |
|
312 | + $rootView->mkdir('/'.$user); |
|
313 | + $rootView->mkdir('/'.$user.'/files'); |
|
314 | 314 | |
315 | 315 | // \OC\Files\Filesystem::file_put_contents('/foo', 'foo'); |
316 | 316 | \OC\Files\Filesystem::mkdir('/bar'); |
@@ -388,13 +388,13 @@ discard block |
||
388 | 388 | |
389 | 389 | \OC\Files\Filesystem::initMountPoints($userId); |
390 | 390 | |
391 | - $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); |
|
391 | + $homeMount = \OC\Files\Filesystem::getStorage('/'.$userId.'/'); |
|
392 | 392 | |
393 | 393 | $this->assertTrue($homeMount->instanceOfStorage('\OCP\Files\IHomeStorage')); |
394 | 394 | if ($homeMount->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
395 | - $this->assertEquals('object::user:' . $userId, $homeMount->getId()); |
|
395 | + $this->assertEquals('object::user:'.$userId, $homeMount->getId()); |
|
396 | 396 | } elseif ($homeMount->instanceOfStorage('\OC\Files\Storage\Home')) { |
397 | - $this->assertEquals('home::' . $userId, $homeMount->getId()); |
|
397 | + $this->assertEquals('home::'.$userId, $homeMount->getId()); |
|
398 | 398 | } |
399 | 399 | |
400 | 400 | $user = \OC::$server->getUserManager()->get($userId); |
@@ -422,10 +422,10 @@ discard block |
||
422 | 422 | \OC\Files\Filesystem::initMountPoints($userId); |
423 | 423 | |
424 | 424 | $this->assertEquals( |
425 | - '/' . $userId . '/', |
|
426 | - \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') |
|
425 | + '/'.$userId.'/', |
|
426 | + \OC\Files\Filesystem::getMountPoint('/'.$userId.'/cache') |
|
427 | 427 | ); |
428 | - [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); |
|
428 | + [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/'.$userId.'/cache'); |
|
429 | 429 | $this->assertTrue($storage->instanceOfStorage('\OCP\Files\IHomeStorage')); |
430 | 430 | $this->assertEquals('cache', $internalPath); |
431 | 431 | $user = \OC::$server->getUserManager()->get($userId); |
@@ -446,17 +446,17 @@ discard block |
||
446 | 446 | $config = \OC::$server->getConfig(); |
447 | 447 | $oldCachePath = $config->getSystemValueString('cache_path', ''); |
448 | 448 | // set cache path to temp dir |
449 | - $cachePath = \OC::$server->getTempManager()->getTemporaryFolder() . '/extcache'; |
|
449 | + $cachePath = \OC::$server->getTempManager()->getTemporaryFolder().'/extcache'; |
|
450 | 450 | $config->setSystemValue('cache_path', $cachePath); |
451 | 451 | |
452 | 452 | \OC::$server->getUserManager()->createUser($userId, $userId); |
453 | 453 | \OC\Files\Filesystem::initMountPoints($userId); |
454 | 454 | |
455 | 455 | $this->assertEquals( |
456 | - '/' . $userId . '/cache/', |
|
457 | - \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') |
|
456 | + '/'.$userId.'/cache/', |
|
457 | + \OC\Files\Filesystem::getMountPoint('/'.$userId.'/cache') |
|
458 | 458 | ); |
459 | - [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); |
|
459 | + [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/'.$userId.'/cache'); |
|
460 | 460 | $this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Local')); |
461 | 461 | $this->assertEquals('', $internalPath); |
462 | 462 | $user = \OC::$server->getUserManager()->get($userId); |
@@ -10,16 +10,16 @@ |
||
10 | 10 | use OC\Archive\ZIP; |
11 | 11 | |
12 | 12 | class ZIPTest extends TestBase { |
13 | - protected function getExisting() { |
|
14 | - $dir = \OC::$SERVERROOT . '/tests/data'; |
|
15 | - return new ZIP($dir . '/data.zip'); |
|
16 | - } |
|
13 | + protected function getExisting() { |
|
14 | + $dir = \OC::$SERVERROOT . '/tests/data'; |
|
15 | + return new ZIP($dir . '/data.zip'); |
|
16 | + } |
|
17 | 17 | |
18 | - protected function getNew() { |
|
19 | - $newZip = \OC::$server->getTempManager()->getTempBaseDir() . '/newArchive.zip'; |
|
20 | - if (file_exists($newZip)) { |
|
21 | - unlink($newZip); |
|
22 | - } |
|
23 | - return new ZIP($newZip); |
|
24 | - } |
|
18 | + protected function getNew() { |
|
19 | + $newZip = \OC::$server->getTempManager()->getTempBaseDir() . '/newArchive.zip'; |
|
20 | + if (file_exists($newZip)) { |
|
21 | + unlink($newZip); |
|
22 | + } |
|
23 | + return new ZIP($newZip); |
|
24 | + } |
|
25 | 25 | } |
@@ -11,12 +11,12 @@ |
||
11 | 11 | |
12 | 12 | class ZIPTest extends TestBase { |
13 | 13 | protected function getExisting() { |
14 | - $dir = \OC::$SERVERROOT . '/tests/data'; |
|
15 | - return new ZIP($dir . '/data.zip'); |
|
14 | + $dir = \OC::$SERVERROOT.'/tests/data'; |
|
15 | + return new ZIP($dir.'/data.zip'); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | protected function getNew() { |
19 | - $newZip = \OC::$server->getTempManager()->getTempBaseDir() . '/newArchive.zip'; |
|
19 | + $newZip = \OC::$server->getTempManager()->getTempBaseDir().'/newArchive.zip'; |
|
20 | 20 | if (file_exists($newZip)) { |
21 | 21 | unlink($newZip); |
22 | 22 | } |
@@ -18,155 +18,155 @@ |
||
18 | 18 | use Psr\Log\LoggerInterface; |
19 | 19 | |
20 | 20 | class SetupTest extends \Test\TestCase { |
21 | - protected SystemConfig $config; |
|
22 | - private IniGetWrapper $iniWrapper; |
|
23 | - private IL10N $l10n; |
|
24 | - private IL10NFactory $l10nFactory; |
|
25 | - private Defaults $defaults; |
|
26 | - protected Setup $setupClass; |
|
27 | - protected LoggerInterface $logger; |
|
28 | - protected ISecureRandom $random; |
|
29 | - protected Installer $installer; |
|
30 | - |
|
31 | - protected function setUp(): void { |
|
32 | - parent::setUp(); |
|
33 | - |
|
34 | - $this->config = $this->createMock(SystemConfig::class); |
|
35 | - $this->iniWrapper = $this->createMock(IniGetWrapper::class); |
|
36 | - $this->l10n = $this->createMock(IL10N::class); |
|
37 | - $this->l10nFactory = $this->createMock(IL10NFactory::class); |
|
38 | - $this->l10nFactory->method('get') |
|
39 | - ->willReturn($this->l10n); |
|
40 | - $this->defaults = $this->createMock(Defaults::class); |
|
41 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
42 | - $this->random = $this->createMock(ISecureRandom::class); |
|
43 | - $this->installer = $this->createMock(Installer::class); |
|
44 | - $this->setupClass = $this->getMockBuilder(Setup::class) |
|
45 | - ->onlyMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo']) |
|
46 | - ->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10nFactory, $this->defaults, $this->logger, $this->random, $this->installer]) |
|
47 | - ->getMock(); |
|
48 | - } |
|
49 | - |
|
50 | - public function testGetSupportedDatabasesWithOneWorking(): void { |
|
51 | - $this->config |
|
52 | - ->expects($this->once()) |
|
53 | - ->method('getValue') |
|
54 | - ->willReturn( |
|
55 | - ['sqlite', 'mysql', 'oci'] |
|
56 | - ); |
|
57 | - $this->setupClass |
|
58 | - ->expects($this->once()) |
|
59 | - ->method('is_callable') |
|
60 | - ->willReturn(false); |
|
61 | - $this->setupClass |
|
62 | - ->expects($this->any()) |
|
63 | - ->method('getAvailableDbDriversForPdo') |
|
64 | - ->willReturn(['sqlite']); |
|
65 | - $result = $this->setupClass->getSupportedDatabases(); |
|
66 | - $expectedResult = [ |
|
67 | - 'sqlite' => 'SQLite' |
|
68 | - ]; |
|
69 | - |
|
70 | - $this->assertSame($expectedResult, $result); |
|
71 | - } |
|
72 | - |
|
73 | - public function testGetSupportedDatabasesWithNoWorking(): void { |
|
74 | - $this->config |
|
75 | - ->expects($this->once()) |
|
76 | - ->method('getValue') |
|
77 | - ->willReturn( |
|
78 | - ['sqlite', 'mysql', 'oci', 'pgsql'] |
|
79 | - ); |
|
80 | - $this->setupClass |
|
81 | - ->expects($this->any()) |
|
82 | - ->method('is_callable') |
|
83 | - ->willReturn(false); |
|
84 | - $this->setupClass |
|
85 | - ->expects($this->any()) |
|
86 | - ->method('getAvailableDbDriversForPdo') |
|
87 | - ->willReturn([]); |
|
88 | - $result = $this->setupClass->getSupportedDatabases(); |
|
89 | - |
|
90 | - $this->assertSame([], $result); |
|
91 | - } |
|
92 | - |
|
93 | - public function testGetSupportedDatabasesWithAllWorking(): void { |
|
94 | - $this->config |
|
95 | - ->expects($this->once()) |
|
96 | - ->method('getValue') |
|
97 | - ->willReturn( |
|
98 | - ['sqlite', 'mysql', 'pgsql', 'oci'] |
|
99 | - ); |
|
100 | - $this->setupClass |
|
101 | - ->expects($this->any()) |
|
102 | - ->method('is_callable') |
|
103 | - ->willReturn(true); |
|
104 | - $this->setupClass |
|
105 | - ->expects($this->any()) |
|
106 | - ->method('getAvailableDbDriversForPdo') |
|
107 | - ->willReturn(['sqlite', 'mysql', 'pgsql']); |
|
108 | - $result = $this->setupClass->getSupportedDatabases(); |
|
109 | - $expectedResult = [ |
|
110 | - 'sqlite' => 'SQLite', |
|
111 | - 'mysql' => 'MySQL/MariaDB', |
|
112 | - 'pgsql' => 'PostgreSQL', |
|
113 | - 'oci' => 'Oracle' |
|
114 | - ]; |
|
115 | - $this->assertSame($expectedResult, $result); |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - public function testGetSupportedDatabaseException(): void { |
|
120 | - $this->expectException(\Exception::class); |
|
121 | - $this->expectExceptionMessage('Supported databases are not properly configured.'); |
|
122 | - |
|
123 | - $this->config |
|
124 | - ->expects($this->once()) |
|
125 | - ->method('getValue') |
|
126 | - ->willReturn('NotAnArray'); |
|
127 | - $this->setupClass->getSupportedDatabases(); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @dataProvider findWebRootProvider |
|
132 | - * @param $url |
|
133 | - * @param $expected |
|
134 | - */ |
|
135 | - public function testFindWebRootCli($url, $expected): void { |
|
136 | - $cliState = \OC::$CLI; |
|
137 | - |
|
138 | - $this->config |
|
139 | - ->expects($this->once()) |
|
140 | - ->method('getValue') |
|
141 | - ->willReturn($url); |
|
142 | - \OC::$CLI = true; |
|
143 | - |
|
144 | - try { |
|
145 | - $webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]); |
|
146 | - } catch (\InvalidArgumentException $e) { |
|
147 | - $webRoot = false; |
|
148 | - } |
|
149 | - |
|
150 | - \OC::$CLI = $cliState; |
|
151 | - $this->assertSame($webRoot, $expected); |
|
152 | - } |
|
153 | - |
|
154 | - public static function findWebRootProvider(): array { |
|
155 | - return [ |
|
156 | - 'https://www.example.com/nextcloud/' => ['https://www.example.com/nextcloud/', '/nextcloud'], |
|
157 | - 'https://www.example.com/nextcloud' => ['https://www.example.com/nextcloud', '/nextcloud'], |
|
158 | - 'https://www.example.com/' => ['https://www.example.com/', ''], |
|
159 | - 'https://www.example.com' => ['https://www.example.com', ''], |
|
160 | - 'https://nctest13pgsql.lan/test123/' => ['https://nctest13pgsql.lan/test123/', '/test123'], |
|
161 | - 'https://nctest13pgsql.lan/test123' => ['https://nctest13pgsql.lan/test123', '/test123'], |
|
162 | - 'https://nctest13pgsql.lan/' => ['https://nctest13pgsql.lan/', ''], |
|
163 | - 'https://nctest13pgsql.lan' => ['https://nctest13pgsql.lan', ''], |
|
164 | - 'https://192.168.10.10/nc/' => ['https://192.168.10.10/nc/', '/nc'], |
|
165 | - 'https://192.168.10.10/nc' => ['https://192.168.10.10/nc', '/nc'], |
|
166 | - 'https://192.168.10.10/' => ['https://192.168.10.10/', ''], |
|
167 | - 'https://192.168.10.10' => ['https://192.168.10.10', ''], |
|
168 | - 'invalid' => ['invalid', false], |
|
169 | - 'empty' => ['', false], |
|
170 | - ]; |
|
171 | - } |
|
21 | + protected SystemConfig $config; |
|
22 | + private IniGetWrapper $iniWrapper; |
|
23 | + private IL10N $l10n; |
|
24 | + private IL10NFactory $l10nFactory; |
|
25 | + private Defaults $defaults; |
|
26 | + protected Setup $setupClass; |
|
27 | + protected LoggerInterface $logger; |
|
28 | + protected ISecureRandom $random; |
|
29 | + protected Installer $installer; |
|
30 | + |
|
31 | + protected function setUp(): void { |
|
32 | + parent::setUp(); |
|
33 | + |
|
34 | + $this->config = $this->createMock(SystemConfig::class); |
|
35 | + $this->iniWrapper = $this->createMock(IniGetWrapper::class); |
|
36 | + $this->l10n = $this->createMock(IL10N::class); |
|
37 | + $this->l10nFactory = $this->createMock(IL10NFactory::class); |
|
38 | + $this->l10nFactory->method('get') |
|
39 | + ->willReturn($this->l10n); |
|
40 | + $this->defaults = $this->createMock(Defaults::class); |
|
41 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
42 | + $this->random = $this->createMock(ISecureRandom::class); |
|
43 | + $this->installer = $this->createMock(Installer::class); |
|
44 | + $this->setupClass = $this->getMockBuilder(Setup::class) |
|
45 | + ->onlyMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo']) |
|
46 | + ->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10nFactory, $this->defaults, $this->logger, $this->random, $this->installer]) |
|
47 | + ->getMock(); |
|
48 | + } |
|
49 | + |
|
50 | + public function testGetSupportedDatabasesWithOneWorking(): void { |
|
51 | + $this->config |
|
52 | + ->expects($this->once()) |
|
53 | + ->method('getValue') |
|
54 | + ->willReturn( |
|
55 | + ['sqlite', 'mysql', 'oci'] |
|
56 | + ); |
|
57 | + $this->setupClass |
|
58 | + ->expects($this->once()) |
|
59 | + ->method('is_callable') |
|
60 | + ->willReturn(false); |
|
61 | + $this->setupClass |
|
62 | + ->expects($this->any()) |
|
63 | + ->method('getAvailableDbDriversForPdo') |
|
64 | + ->willReturn(['sqlite']); |
|
65 | + $result = $this->setupClass->getSupportedDatabases(); |
|
66 | + $expectedResult = [ |
|
67 | + 'sqlite' => 'SQLite' |
|
68 | + ]; |
|
69 | + |
|
70 | + $this->assertSame($expectedResult, $result); |
|
71 | + } |
|
72 | + |
|
73 | + public function testGetSupportedDatabasesWithNoWorking(): void { |
|
74 | + $this->config |
|
75 | + ->expects($this->once()) |
|
76 | + ->method('getValue') |
|
77 | + ->willReturn( |
|
78 | + ['sqlite', 'mysql', 'oci', 'pgsql'] |
|
79 | + ); |
|
80 | + $this->setupClass |
|
81 | + ->expects($this->any()) |
|
82 | + ->method('is_callable') |
|
83 | + ->willReturn(false); |
|
84 | + $this->setupClass |
|
85 | + ->expects($this->any()) |
|
86 | + ->method('getAvailableDbDriversForPdo') |
|
87 | + ->willReturn([]); |
|
88 | + $result = $this->setupClass->getSupportedDatabases(); |
|
89 | + |
|
90 | + $this->assertSame([], $result); |
|
91 | + } |
|
92 | + |
|
93 | + public function testGetSupportedDatabasesWithAllWorking(): void { |
|
94 | + $this->config |
|
95 | + ->expects($this->once()) |
|
96 | + ->method('getValue') |
|
97 | + ->willReturn( |
|
98 | + ['sqlite', 'mysql', 'pgsql', 'oci'] |
|
99 | + ); |
|
100 | + $this->setupClass |
|
101 | + ->expects($this->any()) |
|
102 | + ->method('is_callable') |
|
103 | + ->willReturn(true); |
|
104 | + $this->setupClass |
|
105 | + ->expects($this->any()) |
|
106 | + ->method('getAvailableDbDriversForPdo') |
|
107 | + ->willReturn(['sqlite', 'mysql', 'pgsql']); |
|
108 | + $result = $this->setupClass->getSupportedDatabases(); |
|
109 | + $expectedResult = [ |
|
110 | + 'sqlite' => 'SQLite', |
|
111 | + 'mysql' => 'MySQL/MariaDB', |
|
112 | + 'pgsql' => 'PostgreSQL', |
|
113 | + 'oci' => 'Oracle' |
|
114 | + ]; |
|
115 | + $this->assertSame($expectedResult, $result); |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + public function testGetSupportedDatabaseException(): void { |
|
120 | + $this->expectException(\Exception::class); |
|
121 | + $this->expectExceptionMessage('Supported databases are not properly configured.'); |
|
122 | + |
|
123 | + $this->config |
|
124 | + ->expects($this->once()) |
|
125 | + ->method('getValue') |
|
126 | + ->willReturn('NotAnArray'); |
|
127 | + $this->setupClass->getSupportedDatabases(); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @dataProvider findWebRootProvider |
|
132 | + * @param $url |
|
133 | + * @param $expected |
|
134 | + */ |
|
135 | + public function testFindWebRootCli($url, $expected): void { |
|
136 | + $cliState = \OC::$CLI; |
|
137 | + |
|
138 | + $this->config |
|
139 | + ->expects($this->once()) |
|
140 | + ->method('getValue') |
|
141 | + ->willReturn($url); |
|
142 | + \OC::$CLI = true; |
|
143 | + |
|
144 | + try { |
|
145 | + $webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]); |
|
146 | + } catch (\InvalidArgumentException $e) { |
|
147 | + $webRoot = false; |
|
148 | + } |
|
149 | + |
|
150 | + \OC::$CLI = $cliState; |
|
151 | + $this->assertSame($webRoot, $expected); |
|
152 | + } |
|
153 | + |
|
154 | + public static function findWebRootProvider(): array { |
|
155 | + return [ |
|
156 | + 'https://www.example.com/nextcloud/' => ['https://www.example.com/nextcloud/', '/nextcloud'], |
|
157 | + 'https://www.example.com/nextcloud' => ['https://www.example.com/nextcloud', '/nextcloud'], |
|
158 | + 'https://www.example.com/' => ['https://www.example.com/', ''], |
|
159 | + 'https://www.example.com' => ['https://www.example.com', ''], |
|
160 | + 'https://nctest13pgsql.lan/test123/' => ['https://nctest13pgsql.lan/test123/', '/test123'], |
|
161 | + 'https://nctest13pgsql.lan/test123' => ['https://nctest13pgsql.lan/test123', '/test123'], |
|
162 | + 'https://nctest13pgsql.lan/' => ['https://nctest13pgsql.lan/', ''], |
|
163 | + 'https://nctest13pgsql.lan' => ['https://nctest13pgsql.lan', ''], |
|
164 | + 'https://192.168.10.10/nc/' => ['https://192.168.10.10/nc/', '/nc'], |
|
165 | + 'https://192.168.10.10/nc' => ['https://192.168.10.10/nc', '/nc'], |
|
166 | + 'https://192.168.10.10/' => ['https://192.168.10.10/', ''], |
|
167 | + 'https://192.168.10.10' => ['https://192.168.10.10', ''], |
|
168 | + 'invalid' => ['invalid', false], |
|
169 | + 'empty' => ['', false], |
|
170 | + ]; |
|
171 | + } |
|
172 | 172 | } |
@@ -19,396 +19,396 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class ManagerTest extends TestCase { |
22 | - /** @var PublicKeyTokenProvider|MockObject */ |
|
23 | - private $publicKeyTokenProvider; |
|
24 | - /** @var Manager */ |
|
25 | - private $manager; |
|
26 | - |
|
27 | - protected function setUp(): void { |
|
28 | - parent::setUp(); |
|
29 | - |
|
30 | - $this->publicKeyTokenProvider = $this->createMock(PublicKeyTokenProvider::class); |
|
31 | - $this->manager = new Manager( |
|
32 | - $this->publicKeyTokenProvider |
|
33 | - ); |
|
34 | - } |
|
35 | - |
|
36 | - public function testGenerateToken(): void { |
|
37 | - $token = new PublicKeyToken(); |
|
38 | - |
|
39 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
40 | - ->method('generateToken') |
|
41 | - ->with( |
|
42 | - 'token', |
|
43 | - 'uid', |
|
44 | - 'loginName', |
|
45 | - 'password', |
|
46 | - 'name', |
|
47 | - IToken::TEMPORARY_TOKEN, |
|
48 | - IToken::REMEMBER |
|
49 | - )->willReturn($token); |
|
50 | - |
|
51 | - $actual = $this->manager->generateToken( |
|
52 | - 'token', |
|
53 | - 'uid', |
|
54 | - 'loginName', |
|
55 | - 'password', |
|
56 | - 'name', |
|
57 | - IToken::TEMPORARY_TOKEN, |
|
58 | - IToken::REMEMBER |
|
59 | - ); |
|
60 | - |
|
61 | - $this->assertSame($token, $actual); |
|
62 | - } |
|
63 | - |
|
64 | - public function testGenerateConflictingToken(): void { |
|
65 | - /** @var MockObject|UniqueConstraintViolationException $exception */ |
|
66 | - $exception = $this->createMock(UniqueConstraintViolationException::class); |
|
67 | - |
|
68 | - $token = new PublicKeyToken(); |
|
69 | - $token->setUid('uid'); |
|
70 | - |
|
71 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
72 | - ->method('generateToken') |
|
73 | - ->with( |
|
74 | - 'token', |
|
75 | - 'uid', |
|
76 | - 'loginName', |
|
77 | - 'password', |
|
78 | - 'name', |
|
79 | - IToken::TEMPORARY_TOKEN, |
|
80 | - IToken::REMEMBER |
|
81 | - )->willThrowException($exception); |
|
82 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
83 | - ->method('getToken') |
|
84 | - ->with('token') |
|
85 | - ->willReturn($token); |
|
86 | - |
|
87 | - $actual = $this->manager->generateToken( |
|
88 | - 'token', |
|
89 | - 'uid', |
|
90 | - 'loginName', |
|
91 | - 'password', |
|
92 | - 'name', |
|
93 | - IToken::TEMPORARY_TOKEN, |
|
94 | - IToken::REMEMBER |
|
95 | - ); |
|
96 | - |
|
97 | - $this->assertSame($token, $actual); |
|
98 | - } |
|
99 | - |
|
100 | - public function testGenerateTokenTooLongName(): void { |
|
101 | - $token = $this->createMock(IToken::class); |
|
102 | - $token->method('getName') |
|
103 | - ->willReturn(str_repeat('a', 120) . '…'); |
|
104 | - |
|
105 | - |
|
106 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
107 | - ->method('generateToken') |
|
108 | - ->with( |
|
109 | - 'token', |
|
110 | - 'uid', |
|
111 | - 'loginName', |
|
112 | - 'password', |
|
113 | - str_repeat('a', 120) . '…', |
|
114 | - IToken::TEMPORARY_TOKEN, |
|
115 | - IToken::REMEMBER |
|
116 | - )->willReturn($token); |
|
117 | - |
|
118 | - $actual = $this->manager->generateToken( |
|
119 | - 'token', |
|
120 | - 'uid', |
|
121 | - 'loginName', |
|
122 | - 'password', |
|
123 | - str_repeat('a', 200), |
|
124 | - IToken::TEMPORARY_TOKEN, |
|
125 | - IToken::REMEMBER |
|
126 | - ); |
|
127 | - |
|
128 | - $this->assertSame(121, mb_strlen($actual->getName())); |
|
129 | - } |
|
130 | - |
|
131 | - public static function tokenData(): array { |
|
132 | - return [ |
|
133 | - [new PublicKeyToken()], |
|
134 | - [IToken::class], |
|
135 | - ]; |
|
136 | - } |
|
137 | - |
|
138 | - protected function setNoCall(IToken $token) { |
|
139 | - if (!($token instanceof PublicKeyToken)) { |
|
140 | - $this->publicKeyTokenProvider->expects($this->never()) |
|
141 | - ->method($this->anything()); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - protected function setCall(IToken $token, string $function, $return = null) { |
|
146 | - if ($token instanceof PublicKeyToken) { |
|
147 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
148 | - ->method($function) |
|
149 | - ->with($token) |
|
150 | - ->willReturn($return); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - protected function setException(IToken $token) { |
|
155 | - if (!($token instanceof PublicKeyToken)) { |
|
156 | - $this->expectException(InvalidTokenException::class); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @dataProvider tokenData |
|
162 | - */ |
|
163 | - public function testUpdateToken(IToken|string $token): void { |
|
164 | - if (is_string($token)) { |
|
165 | - $token = $this->createMock($token); |
|
166 | - } |
|
167 | - |
|
168 | - $this->setNoCall($token); |
|
169 | - $this->setCall($token, 'updateToken'); |
|
170 | - $this->setException($token); |
|
171 | - |
|
172 | - $this->manager->updateToken($token); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @dataProvider tokenData |
|
177 | - */ |
|
178 | - public function testUpdateTokenActivity(IToken|string $token): void { |
|
179 | - if (is_string($token)) { |
|
180 | - $token = $this->createMock($token); |
|
181 | - } |
|
182 | - |
|
183 | - $this->setNoCall($token); |
|
184 | - $this->setCall($token, 'updateTokenActivity'); |
|
185 | - $this->setException($token); |
|
186 | - |
|
187 | - $this->manager->updateTokenActivity($token); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @dataProvider tokenData |
|
192 | - */ |
|
193 | - public function testGetPassword(IToken|string $token): void { |
|
194 | - if (is_string($token)) { |
|
195 | - $token = $this->createMock($token); |
|
196 | - } |
|
197 | - |
|
198 | - $this->setNoCall($token); |
|
199 | - $this->setCall($token, 'getPassword', 'password'); |
|
200 | - $this->setException($token); |
|
201 | - |
|
202 | - $result = $this->manager->getPassword($token, 'tokenId', 'password'); |
|
203 | - |
|
204 | - $this->assertSame('password', $result); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @dataProvider tokenData |
|
209 | - */ |
|
210 | - public function testSetPassword(IToken|string $token): void { |
|
211 | - if (is_string($token)) { |
|
212 | - $token = $this->createMock($token); |
|
213 | - } |
|
214 | - |
|
215 | - $this->setNoCall($token); |
|
216 | - $this->setCall($token, 'setPassword'); |
|
217 | - $this->setException($token); |
|
218 | - |
|
219 | - $this->manager->setPassword($token, 'tokenId', 'password'); |
|
220 | - } |
|
221 | - |
|
222 | - public function testInvalidateTokens(): void { |
|
223 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
224 | - ->method('invalidateToken') |
|
225 | - ->with('token'); |
|
226 | - |
|
227 | - $this->manager->invalidateToken('token'); |
|
228 | - } |
|
229 | - |
|
230 | - public function testInvalidateTokenById(): void { |
|
231 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
232 | - ->method('invalidateTokenById') |
|
233 | - ->with('uid', 42); |
|
234 | - |
|
235 | - $this->manager->invalidateTokenById('uid', 42); |
|
236 | - } |
|
237 | - |
|
238 | - public function testInvalidateOldTokens(): void { |
|
239 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
240 | - ->method('invalidateOldTokens'); |
|
241 | - |
|
242 | - $this->manager->invalidateOldTokens(); |
|
243 | - } |
|
244 | - |
|
245 | - public function testInvalidateLastUsedBefore(): void { |
|
246 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
247 | - ->method('invalidateLastUsedBefore') |
|
248 | - ->with('user', 946684800); |
|
249 | - |
|
250 | - $this->manager->invalidateLastUsedBefore('user', 946684800); |
|
251 | - } |
|
252 | - |
|
253 | - public function testGetTokenByUser(): void { |
|
254 | - $t1 = new PublicKeyToken(); |
|
255 | - $t2 = new PublicKeyToken(); |
|
256 | - |
|
257 | - $this->publicKeyTokenProvider |
|
258 | - ->method('getTokenByUser') |
|
259 | - ->willReturn([$t1, $t2]); |
|
260 | - |
|
261 | - $result = $this->manager->getTokenByUser('uid'); |
|
262 | - |
|
263 | - $this->assertEquals([$t1, $t2], $result); |
|
264 | - } |
|
22 | + /** @var PublicKeyTokenProvider|MockObject */ |
|
23 | + private $publicKeyTokenProvider; |
|
24 | + /** @var Manager */ |
|
25 | + private $manager; |
|
26 | + |
|
27 | + protected function setUp(): void { |
|
28 | + parent::setUp(); |
|
29 | + |
|
30 | + $this->publicKeyTokenProvider = $this->createMock(PublicKeyTokenProvider::class); |
|
31 | + $this->manager = new Manager( |
|
32 | + $this->publicKeyTokenProvider |
|
33 | + ); |
|
34 | + } |
|
35 | + |
|
36 | + public function testGenerateToken(): void { |
|
37 | + $token = new PublicKeyToken(); |
|
38 | + |
|
39 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
40 | + ->method('generateToken') |
|
41 | + ->with( |
|
42 | + 'token', |
|
43 | + 'uid', |
|
44 | + 'loginName', |
|
45 | + 'password', |
|
46 | + 'name', |
|
47 | + IToken::TEMPORARY_TOKEN, |
|
48 | + IToken::REMEMBER |
|
49 | + )->willReturn($token); |
|
50 | + |
|
51 | + $actual = $this->manager->generateToken( |
|
52 | + 'token', |
|
53 | + 'uid', |
|
54 | + 'loginName', |
|
55 | + 'password', |
|
56 | + 'name', |
|
57 | + IToken::TEMPORARY_TOKEN, |
|
58 | + IToken::REMEMBER |
|
59 | + ); |
|
60 | + |
|
61 | + $this->assertSame($token, $actual); |
|
62 | + } |
|
63 | + |
|
64 | + public function testGenerateConflictingToken(): void { |
|
65 | + /** @var MockObject|UniqueConstraintViolationException $exception */ |
|
66 | + $exception = $this->createMock(UniqueConstraintViolationException::class); |
|
67 | + |
|
68 | + $token = new PublicKeyToken(); |
|
69 | + $token->setUid('uid'); |
|
70 | + |
|
71 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
72 | + ->method('generateToken') |
|
73 | + ->with( |
|
74 | + 'token', |
|
75 | + 'uid', |
|
76 | + 'loginName', |
|
77 | + 'password', |
|
78 | + 'name', |
|
79 | + IToken::TEMPORARY_TOKEN, |
|
80 | + IToken::REMEMBER |
|
81 | + )->willThrowException($exception); |
|
82 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
83 | + ->method('getToken') |
|
84 | + ->with('token') |
|
85 | + ->willReturn($token); |
|
86 | + |
|
87 | + $actual = $this->manager->generateToken( |
|
88 | + 'token', |
|
89 | + 'uid', |
|
90 | + 'loginName', |
|
91 | + 'password', |
|
92 | + 'name', |
|
93 | + IToken::TEMPORARY_TOKEN, |
|
94 | + IToken::REMEMBER |
|
95 | + ); |
|
96 | + |
|
97 | + $this->assertSame($token, $actual); |
|
98 | + } |
|
99 | + |
|
100 | + public function testGenerateTokenTooLongName(): void { |
|
101 | + $token = $this->createMock(IToken::class); |
|
102 | + $token->method('getName') |
|
103 | + ->willReturn(str_repeat('a', 120) . '…'); |
|
104 | + |
|
105 | + |
|
106 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
107 | + ->method('generateToken') |
|
108 | + ->with( |
|
109 | + 'token', |
|
110 | + 'uid', |
|
111 | + 'loginName', |
|
112 | + 'password', |
|
113 | + str_repeat('a', 120) . '…', |
|
114 | + IToken::TEMPORARY_TOKEN, |
|
115 | + IToken::REMEMBER |
|
116 | + )->willReturn($token); |
|
117 | + |
|
118 | + $actual = $this->manager->generateToken( |
|
119 | + 'token', |
|
120 | + 'uid', |
|
121 | + 'loginName', |
|
122 | + 'password', |
|
123 | + str_repeat('a', 200), |
|
124 | + IToken::TEMPORARY_TOKEN, |
|
125 | + IToken::REMEMBER |
|
126 | + ); |
|
127 | + |
|
128 | + $this->assertSame(121, mb_strlen($actual->getName())); |
|
129 | + } |
|
130 | + |
|
131 | + public static function tokenData(): array { |
|
132 | + return [ |
|
133 | + [new PublicKeyToken()], |
|
134 | + [IToken::class], |
|
135 | + ]; |
|
136 | + } |
|
137 | + |
|
138 | + protected function setNoCall(IToken $token) { |
|
139 | + if (!($token instanceof PublicKeyToken)) { |
|
140 | + $this->publicKeyTokenProvider->expects($this->never()) |
|
141 | + ->method($this->anything()); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + protected function setCall(IToken $token, string $function, $return = null) { |
|
146 | + if ($token instanceof PublicKeyToken) { |
|
147 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
148 | + ->method($function) |
|
149 | + ->with($token) |
|
150 | + ->willReturn($return); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + protected function setException(IToken $token) { |
|
155 | + if (!($token instanceof PublicKeyToken)) { |
|
156 | + $this->expectException(InvalidTokenException::class); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @dataProvider tokenData |
|
162 | + */ |
|
163 | + public function testUpdateToken(IToken|string $token): void { |
|
164 | + if (is_string($token)) { |
|
165 | + $token = $this->createMock($token); |
|
166 | + } |
|
167 | + |
|
168 | + $this->setNoCall($token); |
|
169 | + $this->setCall($token, 'updateToken'); |
|
170 | + $this->setException($token); |
|
171 | + |
|
172 | + $this->manager->updateToken($token); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @dataProvider tokenData |
|
177 | + */ |
|
178 | + public function testUpdateTokenActivity(IToken|string $token): void { |
|
179 | + if (is_string($token)) { |
|
180 | + $token = $this->createMock($token); |
|
181 | + } |
|
182 | + |
|
183 | + $this->setNoCall($token); |
|
184 | + $this->setCall($token, 'updateTokenActivity'); |
|
185 | + $this->setException($token); |
|
186 | + |
|
187 | + $this->manager->updateTokenActivity($token); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @dataProvider tokenData |
|
192 | + */ |
|
193 | + public function testGetPassword(IToken|string $token): void { |
|
194 | + if (is_string($token)) { |
|
195 | + $token = $this->createMock($token); |
|
196 | + } |
|
197 | + |
|
198 | + $this->setNoCall($token); |
|
199 | + $this->setCall($token, 'getPassword', 'password'); |
|
200 | + $this->setException($token); |
|
201 | + |
|
202 | + $result = $this->manager->getPassword($token, 'tokenId', 'password'); |
|
203 | + |
|
204 | + $this->assertSame('password', $result); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @dataProvider tokenData |
|
209 | + */ |
|
210 | + public function testSetPassword(IToken|string $token): void { |
|
211 | + if (is_string($token)) { |
|
212 | + $token = $this->createMock($token); |
|
213 | + } |
|
214 | + |
|
215 | + $this->setNoCall($token); |
|
216 | + $this->setCall($token, 'setPassword'); |
|
217 | + $this->setException($token); |
|
218 | + |
|
219 | + $this->manager->setPassword($token, 'tokenId', 'password'); |
|
220 | + } |
|
221 | + |
|
222 | + public function testInvalidateTokens(): void { |
|
223 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
224 | + ->method('invalidateToken') |
|
225 | + ->with('token'); |
|
226 | + |
|
227 | + $this->manager->invalidateToken('token'); |
|
228 | + } |
|
229 | + |
|
230 | + public function testInvalidateTokenById(): void { |
|
231 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
232 | + ->method('invalidateTokenById') |
|
233 | + ->with('uid', 42); |
|
234 | + |
|
235 | + $this->manager->invalidateTokenById('uid', 42); |
|
236 | + } |
|
237 | + |
|
238 | + public function testInvalidateOldTokens(): void { |
|
239 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
240 | + ->method('invalidateOldTokens'); |
|
241 | + |
|
242 | + $this->manager->invalidateOldTokens(); |
|
243 | + } |
|
244 | + |
|
245 | + public function testInvalidateLastUsedBefore(): void { |
|
246 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
247 | + ->method('invalidateLastUsedBefore') |
|
248 | + ->with('user', 946684800); |
|
249 | + |
|
250 | + $this->manager->invalidateLastUsedBefore('user', 946684800); |
|
251 | + } |
|
252 | + |
|
253 | + public function testGetTokenByUser(): void { |
|
254 | + $t1 = new PublicKeyToken(); |
|
255 | + $t2 = new PublicKeyToken(); |
|
256 | + |
|
257 | + $this->publicKeyTokenProvider |
|
258 | + ->method('getTokenByUser') |
|
259 | + ->willReturn([$t1, $t2]); |
|
260 | + |
|
261 | + $result = $this->manager->getTokenByUser('uid'); |
|
262 | + |
|
263 | + $this->assertEquals([$t1, $t2], $result); |
|
264 | + } |
|
265 | 265 | |
266 | - public function testRenewSessionTokenPublicKey(): void { |
|
267 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
268 | - ->method('renewSessionToken') |
|
269 | - ->with('oldId', 'newId'); |
|
270 | - |
|
271 | - $this->manager->renewSessionToken('oldId', 'newId'); |
|
272 | - } |
|
273 | - |
|
274 | - public function testRenewSessionInvalid(): void { |
|
275 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
276 | - ->method('renewSessionToken') |
|
277 | - ->with('oldId', 'newId') |
|
278 | - ->willThrowException(new InvalidTokenException()); |
|
279 | - |
|
280 | - $this->expectException(InvalidTokenException::class); |
|
281 | - $this->manager->renewSessionToken('oldId', 'newId'); |
|
282 | - } |
|
283 | - |
|
284 | - public function testGetTokenByIdPublicKey(): void { |
|
285 | - $token = $this->createMock(IToken::class); |
|
286 | - |
|
287 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
288 | - ->method('getTokenById') |
|
289 | - ->with(42) |
|
290 | - ->willReturn($token); |
|
291 | - |
|
292 | - $this->assertSame($token, $this->manager->getTokenById(42)); |
|
293 | - } |
|
294 | - |
|
295 | - public function testGetTokenByIdInvalid(): void { |
|
296 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
297 | - ->method('getTokenById') |
|
298 | - ->with(42) |
|
299 | - ->willThrowException(new InvalidTokenException()); |
|
300 | - |
|
301 | - $this->expectException(InvalidTokenException::class); |
|
302 | - $this->manager->getTokenById(42); |
|
303 | - } |
|
304 | - |
|
305 | - public function testGetTokenPublicKey(): void { |
|
306 | - $token = new PublicKeyToken(); |
|
307 | - |
|
308 | - $this->publicKeyTokenProvider |
|
309 | - ->method('getToken') |
|
310 | - ->with('tokenId') |
|
311 | - ->willReturn($token); |
|
312 | - |
|
313 | - $this->assertSame($token, $this->manager->getToken('tokenId')); |
|
314 | - } |
|
315 | - |
|
316 | - public function testGetTokenInvalid(): void { |
|
317 | - $this->publicKeyTokenProvider |
|
318 | - ->method('getToken') |
|
319 | - ->with('tokenId') |
|
320 | - ->willThrowException(new InvalidTokenException()); |
|
321 | - |
|
322 | - $this->expectException(InvalidTokenException::class); |
|
323 | - $this->manager->getToken('tokenId'); |
|
324 | - } |
|
325 | - |
|
326 | - public function testRotateInvalid(): void { |
|
327 | - $this->expectException(InvalidTokenException::class); |
|
328 | - $this->manager->rotate($this->createMock(IToken::class), 'oldId', 'newId'); |
|
329 | - } |
|
330 | - |
|
331 | - public function testRotatePublicKey(): void { |
|
332 | - $token = new PublicKeyToken(); |
|
333 | - |
|
334 | - $this->publicKeyTokenProvider |
|
335 | - ->method('rotate') |
|
336 | - ->with($token, 'oldId', 'newId') |
|
337 | - ->willReturn($token); |
|
338 | - |
|
339 | - $this->assertSame($token, $this->manager->rotate($token, 'oldId', 'newId')); |
|
340 | - } |
|
341 | - |
|
342 | - public function testMarkPasswordInvalidPublicKey(): void { |
|
343 | - $token = $this->createMock(PublicKeyToken::class); |
|
344 | - |
|
345 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
346 | - ->method('markPasswordInvalid') |
|
347 | - ->with($token, 'tokenId'); |
|
348 | - |
|
349 | - $this->manager->markPasswordInvalid($token, 'tokenId'); |
|
350 | - } |
|
351 | - |
|
352 | - public function testMarkPasswordInvalidInvalidToken(): void { |
|
353 | - $this->expectException(InvalidTokenException::class); |
|
354 | - |
|
355 | - $this->manager->markPasswordInvalid($this->createMock(IToken::class), 'tokenId'); |
|
356 | - } |
|
357 | - |
|
358 | - public function testUpdatePasswords(): void { |
|
359 | - $this->publicKeyTokenProvider->expects($this->once()) |
|
360 | - ->method('updatePasswords') |
|
361 | - ->with('uid', 'pass'); |
|
362 | - |
|
363 | - $this->manager->updatePasswords('uid', 'pass'); |
|
364 | - } |
|
365 | - |
|
366 | - public function testInvalidateTokensOfUserNoClientName(): void { |
|
367 | - $t1 = new PublicKeyToken(); |
|
368 | - $t2 = new PublicKeyToken(); |
|
369 | - $t1->setId(123); |
|
370 | - $t2->setId(456); |
|
371 | - |
|
372 | - $this->publicKeyTokenProvider |
|
373 | - ->expects($this->once()) |
|
374 | - ->method('getTokenByUser') |
|
375 | - ->with('theUser') |
|
376 | - ->willReturn([$t1, $t2]); |
|
377 | - |
|
378 | - $calls = [ |
|
379 | - ['theUser', 123], |
|
380 | - ['theUser', 456], |
|
381 | - ]; |
|
382 | - $this->publicKeyTokenProvider |
|
383 | - ->expects($this->exactly(2)) |
|
384 | - ->method('invalidateTokenById') |
|
385 | - ->willReturnCallback(function () use (&$calls) { |
|
386 | - $expected = array_shift($calls); |
|
387 | - $this->assertEquals($expected, func_get_args()); |
|
388 | - }); |
|
389 | - $this->manager->invalidateTokensOfUser('theUser', null); |
|
390 | - } |
|
391 | - |
|
392 | - public function testInvalidateTokensOfUserClientNameGiven(): void { |
|
393 | - $t1 = new PublicKeyToken(); |
|
394 | - $t2 = new PublicKeyToken(); |
|
395 | - $t3 = new PublicKeyToken(); |
|
396 | - $t1->setId(123); |
|
397 | - $t1->setName('Firefox session'); |
|
398 | - $t2->setId(456); |
|
399 | - $t2->setName('My Client Name'); |
|
400 | - $t3->setId(789); |
|
401 | - $t3->setName('mobile client'); |
|
402 | - |
|
403 | - $this->publicKeyTokenProvider |
|
404 | - ->expects($this->once()) |
|
405 | - ->method('getTokenByUser') |
|
406 | - ->with('theUser') |
|
407 | - ->willReturn([$t1, $t2, $t3]); |
|
408 | - $this->publicKeyTokenProvider |
|
409 | - ->expects($this->once()) |
|
410 | - ->method('invalidateTokenById') |
|
411 | - ->with('theUser', 456); |
|
412 | - $this->manager->invalidateTokensOfUser('theUser', 'My Client Name'); |
|
413 | - } |
|
266 | + public function testRenewSessionTokenPublicKey(): void { |
|
267 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
268 | + ->method('renewSessionToken') |
|
269 | + ->with('oldId', 'newId'); |
|
270 | + |
|
271 | + $this->manager->renewSessionToken('oldId', 'newId'); |
|
272 | + } |
|
273 | + |
|
274 | + public function testRenewSessionInvalid(): void { |
|
275 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
276 | + ->method('renewSessionToken') |
|
277 | + ->with('oldId', 'newId') |
|
278 | + ->willThrowException(new InvalidTokenException()); |
|
279 | + |
|
280 | + $this->expectException(InvalidTokenException::class); |
|
281 | + $this->manager->renewSessionToken('oldId', 'newId'); |
|
282 | + } |
|
283 | + |
|
284 | + public function testGetTokenByIdPublicKey(): void { |
|
285 | + $token = $this->createMock(IToken::class); |
|
286 | + |
|
287 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
288 | + ->method('getTokenById') |
|
289 | + ->with(42) |
|
290 | + ->willReturn($token); |
|
291 | + |
|
292 | + $this->assertSame($token, $this->manager->getTokenById(42)); |
|
293 | + } |
|
294 | + |
|
295 | + public function testGetTokenByIdInvalid(): void { |
|
296 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
297 | + ->method('getTokenById') |
|
298 | + ->with(42) |
|
299 | + ->willThrowException(new InvalidTokenException()); |
|
300 | + |
|
301 | + $this->expectException(InvalidTokenException::class); |
|
302 | + $this->manager->getTokenById(42); |
|
303 | + } |
|
304 | + |
|
305 | + public function testGetTokenPublicKey(): void { |
|
306 | + $token = new PublicKeyToken(); |
|
307 | + |
|
308 | + $this->publicKeyTokenProvider |
|
309 | + ->method('getToken') |
|
310 | + ->with('tokenId') |
|
311 | + ->willReturn($token); |
|
312 | + |
|
313 | + $this->assertSame($token, $this->manager->getToken('tokenId')); |
|
314 | + } |
|
315 | + |
|
316 | + public function testGetTokenInvalid(): void { |
|
317 | + $this->publicKeyTokenProvider |
|
318 | + ->method('getToken') |
|
319 | + ->with('tokenId') |
|
320 | + ->willThrowException(new InvalidTokenException()); |
|
321 | + |
|
322 | + $this->expectException(InvalidTokenException::class); |
|
323 | + $this->manager->getToken('tokenId'); |
|
324 | + } |
|
325 | + |
|
326 | + public function testRotateInvalid(): void { |
|
327 | + $this->expectException(InvalidTokenException::class); |
|
328 | + $this->manager->rotate($this->createMock(IToken::class), 'oldId', 'newId'); |
|
329 | + } |
|
330 | + |
|
331 | + public function testRotatePublicKey(): void { |
|
332 | + $token = new PublicKeyToken(); |
|
333 | + |
|
334 | + $this->publicKeyTokenProvider |
|
335 | + ->method('rotate') |
|
336 | + ->with($token, 'oldId', 'newId') |
|
337 | + ->willReturn($token); |
|
338 | + |
|
339 | + $this->assertSame($token, $this->manager->rotate($token, 'oldId', 'newId')); |
|
340 | + } |
|
341 | + |
|
342 | + public function testMarkPasswordInvalidPublicKey(): void { |
|
343 | + $token = $this->createMock(PublicKeyToken::class); |
|
344 | + |
|
345 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
346 | + ->method('markPasswordInvalid') |
|
347 | + ->with($token, 'tokenId'); |
|
348 | + |
|
349 | + $this->manager->markPasswordInvalid($token, 'tokenId'); |
|
350 | + } |
|
351 | + |
|
352 | + public function testMarkPasswordInvalidInvalidToken(): void { |
|
353 | + $this->expectException(InvalidTokenException::class); |
|
354 | + |
|
355 | + $this->manager->markPasswordInvalid($this->createMock(IToken::class), 'tokenId'); |
|
356 | + } |
|
357 | + |
|
358 | + public function testUpdatePasswords(): void { |
|
359 | + $this->publicKeyTokenProvider->expects($this->once()) |
|
360 | + ->method('updatePasswords') |
|
361 | + ->with('uid', 'pass'); |
|
362 | + |
|
363 | + $this->manager->updatePasswords('uid', 'pass'); |
|
364 | + } |
|
365 | + |
|
366 | + public function testInvalidateTokensOfUserNoClientName(): void { |
|
367 | + $t1 = new PublicKeyToken(); |
|
368 | + $t2 = new PublicKeyToken(); |
|
369 | + $t1->setId(123); |
|
370 | + $t2->setId(456); |
|
371 | + |
|
372 | + $this->publicKeyTokenProvider |
|
373 | + ->expects($this->once()) |
|
374 | + ->method('getTokenByUser') |
|
375 | + ->with('theUser') |
|
376 | + ->willReturn([$t1, $t2]); |
|
377 | + |
|
378 | + $calls = [ |
|
379 | + ['theUser', 123], |
|
380 | + ['theUser', 456], |
|
381 | + ]; |
|
382 | + $this->publicKeyTokenProvider |
|
383 | + ->expects($this->exactly(2)) |
|
384 | + ->method('invalidateTokenById') |
|
385 | + ->willReturnCallback(function () use (&$calls) { |
|
386 | + $expected = array_shift($calls); |
|
387 | + $this->assertEquals($expected, func_get_args()); |
|
388 | + }); |
|
389 | + $this->manager->invalidateTokensOfUser('theUser', null); |
|
390 | + } |
|
391 | + |
|
392 | + public function testInvalidateTokensOfUserClientNameGiven(): void { |
|
393 | + $t1 = new PublicKeyToken(); |
|
394 | + $t2 = new PublicKeyToken(); |
|
395 | + $t3 = new PublicKeyToken(); |
|
396 | + $t1->setId(123); |
|
397 | + $t1->setName('Firefox session'); |
|
398 | + $t2->setId(456); |
|
399 | + $t2->setName('My Client Name'); |
|
400 | + $t3->setId(789); |
|
401 | + $t3->setName('mobile client'); |
|
402 | + |
|
403 | + $this->publicKeyTokenProvider |
|
404 | + ->expects($this->once()) |
|
405 | + ->method('getTokenByUser') |
|
406 | + ->with('theUser') |
|
407 | + ->willReturn([$t1, $t2, $t3]); |
|
408 | + $this->publicKeyTokenProvider |
|
409 | + ->expects($this->once()) |
|
410 | + ->method('invalidateTokenById') |
|
411 | + ->with('theUser', 456); |
|
412 | + $this->manager->invalidateTokensOfUser('theUser', 'My Client Name'); |
|
413 | + } |
|
414 | 414 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | public function testGenerateTokenTooLongName(): void { |
101 | 101 | $token = $this->createMock(IToken::class); |
102 | 102 | $token->method('getName') |
103 | - ->willReturn(str_repeat('a', 120) . '…'); |
|
103 | + ->willReturn(str_repeat('a', 120).'…'); |
|
104 | 104 | |
105 | 105 | |
106 | 106 | $this->publicKeyTokenProvider->expects($this->once()) |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | 'uid', |
111 | 111 | 'loginName', |
112 | 112 | 'password', |
113 | - str_repeat('a', 120) . '…', |
|
113 | + str_repeat('a', 120).'…', |
|
114 | 114 | IToken::TEMPORARY_TOKEN, |
115 | 115 | IToken::REMEMBER |
116 | 116 | )->willReturn($token); |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | /** |
161 | 161 | * @dataProvider tokenData |
162 | 162 | */ |
163 | - public function testUpdateToken(IToken|string $token): void { |
|
163 | + public function testUpdateToken(IToken | string $token): void { |
|
164 | 164 | if (is_string($token)) { |
165 | 165 | $token = $this->createMock($token); |
166 | 166 | } |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | /** |
176 | 176 | * @dataProvider tokenData |
177 | 177 | */ |
178 | - public function testUpdateTokenActivity(IToken|string $token): void { |
|
178 | + public function testUpdateTokenActivity(IToken | string $token): void { |
|
179 | 179 | if (is_string($token)) { |
180 | 180 | $token = $this->createMock($token); |
181 | 181 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | /** |
191 | 191 | * @dataProvider tokenData |
192 | 192 | */ |
193 | - public function testGetPassword(IToken|string $token): void { |
|
193 | + public function testGetPassword(IToken | string $token): void { |
|
194 | 194 | if (is_string($token)) { |
195 | 195 | $token = $this->createMock($token); |
196 | 196 | } |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | /** |
208 | 208 | * @dataProvider tokenData |
209 | 209 | */ |
210 | - public function testSetPassword(IToken|string $token): void { |
|
210 | + public function testSetPassword(IToken | string $token): void { |
|
211 | 211 | if (is_string($token)) { |
212 | 212 | $token = $this->createMock($token); |
213 | 213 | } |
@@ -382,7 +382,7 @@ discard block |
||
382 | 382 | $this->publicKeyTokenProvider |
383 | 383 | ->expects($this->exactly(2)) |
384 | 384 | ->method('invalidateTokenById') |
385 | - ->willReturnCallback(function () use (&$calls) { |
|
385 | + ->willReturnCallback(function() use (&$calls) { |
|
386 | 386 | $expected = array_shift($calls); |
387 | 387 | $this->assertEquals($expected, func_get_args()); |
388 | 388 | }); |
@@ -28,617 +28,617 @@ |
||
28 | 28 | use Test\TestCase; |
29 | 29 | |
30 | 30 | class PublicKeyTokenProviderTest extends TestCase { |
31 | - /** @var PublicKeyTokenProvider|\PHPUnit\Framework\MockObject\MockObject */ |
|
32 | - private $tokenProvider; |
|
33 | - /** @var PublicKeyTokenMapper|\PHPUnit\Framework\MockObject\MockObject */ |
|
34 | - private $mapper; |
|
35 | - /** @var IHasher|\PHPUnit\Framework\MockObject\MockObject */ |
|
36 | - private $hasher; |
|
37 | - /** @var ICrypto */ |
|
38 | - private $crypto; |
|
39 | - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
40 | - private $config; |
|
41 | - /** @var IDBConnection|MockObject */ |
|
42 | - private IDBConnection $db; |
|
43 | - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
44 | - private $logger; |
|
45 | - /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
46 | - private $timeFactory; |
|
47 | - /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
48 | - private $cacheFactory; |
|
49 | - /** @var int */ |
|
50 | - private $time; |
|
51 | - |
|
52 | - protected function setUp(): void { |
|
53 | - parent::setUp(); |
|
54 | - |
|
55 | - $this->mapper = $this->createMock(PublicKeyTokenMapper::class); |
|
56 | - $this->hasher = \OC::$server->get(IHasher::class); |
|
57 | - $this->crypto = \OC::$server->getCrypto(); |
|
58 | - $this->config = $this->createMock(IConfig::class); |
|
59 | - $this->config->method('getSystemValue') |
|
60 | - ->willReturnMap([ |
|
61 | - ['openssl', [], []], |
|
62 | - ]); |
|
63 | - $this->config->method('getSystemValueString') |
|
64 | - ->willReturnMap([ |
|
65 | - ['secret', '', '1f4h9s'], |
|
66 | - ]); |
|
67 | - $this->db = $this->createMock(IDBConnection::class); |
|
68 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
69 | - $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
70 | - $this->time = 1313131; |
|
71 | - $this->timeFactory->method('getTime') |
|
72 | - ->willReturn($this->time); |
|
73 | - $this->cacheFactory = $this->createMock(ICacheFactory::class); |
|
74 | - |
|
75 | - $this->tokenProvider = new PublicKeyTokenProvider( |
|
76 | - $this->mapper, |
|
77 | - $this->crypto, |
|
78 | - $this->config, |
|
79 | - $this->db, |
|
80 | - $this->logger, |
|
81 | - $this->timeFactory, |
|
82 | - $this->hasher, |
|
83 | - $this->cacheFactory, |
|
84 | - ); |
|
85 | - } |
|
86 | - |
|
87 | - public function testGenerateToken(): void { |
|
88 | - $token = 'tokentokentokentokentoken'; |
|
89 | - $uid = 'user'; |
|
90 | - $user = 'User'; |
|
91 | - $password = 'passme'; |
|
92 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
93 | - $type = IToken::PERMANENT_TOKEN; |
|
94 | - |
|
95 | - $this->config->method('getSystemValueBool') |
|
96 | - ->willReturnMap([ |
|
97 | - ['auth.storeCryptedPassword', true, true], |
|
98 | - ]); |
|
99 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
100 | - |
|
101 | - $this->assertInstanceOf(PublicKeyToken::class, $actual); |
|
102 | - $this->assertSame($uid, $actual->getUID()); |
|
103 | - $this->assertSame($user, $actual->getLoginName()); |
|
104 | - $this->assertSame($name, $actual->getName()); |
|
105 | - $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember()); |
|
106 | - $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); |
|
107 | - } |
|
108 | - |
|
109 | - public function testGenerateTokenNoPassword(): void { |
|
110 | - $token = 'tokentokentokentokentoken'; |
|
111 | - $uid = 'user'; |
|
112 | - $user = 'User'; |
|
113 | - $password = 'passme'; |
|
114 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
115 | - $type = IToken::PERMANENT_TOKEN; |
|
116 | - $this->config->method('getSystemValueBool') |
|
117 | - ->willReturnMap([ |
|
118 | - ['auth.storeCryptedPassword', true, false], |
|
119 | - ]); |
|
120 | - $this->expectException(PasswordlessTokenException::class); |
|
121 | - |
|
122 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
123 | - |
|
124 | - $this->assertInstanceOf(PublicKeyToken::class, $actual); |
|
125 | - $this->assertSame($uid, $actual->getUID()); |
|
126 | - $this->assertSame($user, $actual->getLoginName()); |
|
127 | - $this->assertSame($name, $actual->getName()); |
|
128 | - $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember()); |
|
129 | - $this->tokenProvider->getPassword($actual, $token); |
|
130 | - } |
|
131 | - |
|
132 | - public function testGenerateTokenLongPassword(): void { |
|
133 | - $token = 'tokentokentokentokentoken'; |
|
134 | - $uid = 'user'; |
|
135 | - $user = 'User'; |
|
136 | - $password = ''; |
|
137 | - for ($i = 0; $i < 500; $i++) { |
|
138 | - $password .= 'e'; |
|
139 | - } |
|
140 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
141 | - $type = IToken::PERMANENT_TOKEN; |
|
142 | - $this->config->method('getSystemValueBool') |
|
143 | - ->willReturnMap([ |
|
144 | - ['auth.storeCryptedPassword', true, true], |
|
145 | - ]); |
|
146 | - $this->expectException(\RuntimeException::class); |
|
147 | - |
|
148 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
149 | - } |
|
150 | - |
|
151 | - public function testGenerateTokenInvalidName(): void { |
|
152 | - $token = 'tokentokentokentokentoken'; |
|
153 | - $uid = 'user'; |
|
154 | - $user = 'User'; |
|
155 | - $password = 'passme'; |
|
156 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' |
|
157 | - . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' |
|
158 | - . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' |
|
159 | - . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
160 | - $type = IToken::PERMANENT_TOKEN; |
|
161 | - $this->config->method('getSystemValueBool') |
|
162 | - ->willReturnMap([ |
|
163 | - ['auth.storeCryptedPassword', true, true], |
|
164 | - ]); |
|
165 | - |
|
166 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
167 | - |
|
168 | - $this->assertInstanceOf(PublicKeyToken::class, $actual); |
|
169 | - $this->assertSame($uid, $actual->getUID()); |
|
170 | - $this->assertSame($user, $actual->getLoginName()); |
|
171 | - $this->assertSame('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12User-Agent: Mozill…', $actual->getName()); |
|
172 | - $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember()); |
|
173 | - $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); |
|
174 | - } |
|
175 | - |
|
176 | - public function testUpdateToken(): void { |
|
177 | - $tk = new PublicKeyToken(); |
|
178 | - $this->mapper->expects($this->once()) |
|
179 | - ->method('updateActivity') |
|
180 | - ->with($tk, $this->time); |
|
181 | - $tk->setLastActivity($this->time - 200); |
|
182 | - $this->config->method('getSystemValueBool') |
|
183 | - ->willReturnMap([ |
|
184 | - ['auth.storeCryptedPassword', true, true], |
|
185 | - ]); |
|
186 | - |
|
187 | - $this->tokenProvider->updateTokenActivity($tk); |
|
188 | - |
|
189 | - $this->assertEquals($this->time, $tk->getLastActivity()); |
|
190 | - } |
|
191 | - |
|
192 | - public function testUpdateTokenDebounce(): void { |
|
193 | - $tk = new PublicKeyToken(); |
|
194 | - $this->config->method('getSystemValueInt') |
|
195 | - ->willReturnCallback(function ($value, $default) { |
|
196 | - return $default; |
|
197 | - }); |
|
198 | - $tk->setLastActivity($this->time - 30); |
|
199 | - |
|
200 | - $this->mapper->expects($this->never()) |
|
201 | - ->method('updateActivity') |
|
202 | - ->with($tk, $this->time); |
|
203 | - |
|
204 | - $this->tokenProvider->updateTokenActivity($tk); |
|
205 | - } |
|
206 | - |
|
207 | - public function testGetTokenByUser(): void { |
|
208 | - $this->mapper->expects($this->once()) |
|
209 | - ->method('getTokenByUser') |
|
210 | - ->with('uid') |
|
211 | - ->willReturn(['token']); |
|
212 | - |
|
213 | - $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid')); |
|
214 | - } |
|
215 | - |
|
216 | - public function testGetPassword(): void { |
|
217 | - $token = 'tokentokentokentokentoken'; |
|
218 | - $uid = 'user'; |
|
219 | - $user = 'User'; |
|
220 | - $password = 'passme'; |
|
221 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
222 | - $type = IToken::PERMANENT_TOKEN; |
|
223 | - $this->config->method('getSystemValueBool') |
|
224 | - ->willReturnMap([ |
|
225 | - ['auth.storeCryptedPassword', true, true], |
|
226 | - ]); |
|
227 | - |
|
228 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
229 | - |
|
230 | - $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); |
|
231 | - } |
|
232 | - |
|
233 | - |
|
234 | - public function testGetPasswordPasswordLessToken(): void { |
|
235 | - $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class); |
|
236 | - |
|
237 | - $token = 'token1234'; |
|
238 | - $tk = new PublicKeyToken(); |
|
239 | - $tk->setPassword(null); |
|
240 | - |
|
241 | - $this->tokenProvider->getPassword($tk, $token); |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - public function testGetPasswordInvalidToken(): void { |
|
246 | - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); |
|
247 | - |
|
248 | - $token = 'tokentokentokentokentoken'; |
|
249 | - $uid = 'user'; |
|
250 | - $user = 'User'; |
|
251 | - $password = 'passme'; |
|
252 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
253 | - $type = IToken::PERMANENT_TOKEN; |
|
254 | - |
|
255 | - $this->config->method('getSystemValueBool') |
|
256 | - ->willReturnMap([ |
|
257 | - ['auth.storeCryptedPassword', true, true], |
|
258 | - ]); |
|
259 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
260 | - |
|
261 | - $this->tokenProvider->getPassword($actual, 'wrongtoken'); |
|
262 | - } |
|
263 | - |
|
264 | - public function testSetPassword(): void { |
|
265 | - $token = 'tokentokentokentokentoken'; |
|
266 | - $uid = 'user'; |
|
267 | - $user = 'User'; |
|
268 | - $password = 'passme'; |
|
269 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
270 | - $type = IToken::PERMANENT_TOKEN; |
|
271 | - $this->config->method('getSystemValueBool') |
|
272 | - ->willReturnMap([ |
|
273 | - ['auth.storeCryptedPassword', true, true], |
|
274 | - ]); |
|
275 | - |
|
276 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
277 | - |
|
278 | - $this->mapper->method('getTokenByUser') |
|
279 | - ->with('user') |
|
280 | - ->willReturn([$actual]); |
|
281 | - |
|
282 | - $newpass = 'newpass'; |
|
283 | - $this->mapper->expects($this->once()) |
|
284 | - ->method('update') |
|
285 | - ->with($this->callback(function ($token) use ($newpass) { |
|
286 | - return $newpass === $this->tokenProvider->getPassword($token, 'tokentokentokentokentoken'); |
|
287 | - })); |
|
288 | - |
|
289 | - |
|
290 | - $this->tokenProvider->setPassword($actual, $token, $newpass); |
|
291 | - |
|
292 | - $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'tokentokentokentokentoken')); |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - public function testSetPasswordInvalidToken(): void { |
|
297 | - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); |
|
298 | - |
|
299 | - $token = $this->createMock(IToken::class); |
|
300 | - $tokenId = 'token123'; |
|
301 | - $password = '123456'; |
|
302 | - |
|
303 | - $this->tokenProvider->setPassword($token, $tokenId, $password); |
|
304 | - } |
|
305 | - |
|
306 | - public function testInvalidateToken(): void { |
|
307 | - $calls = [ |
|
308 | - [hash('sha512', 'token7' . '1f4h9s')], |
|
309 | - [hash('sha512', 'token7')] |
|
310 | - ]; |
|
311 | - |
|
312 | - $this->mapper->expects($this->exactly(2)) |
|
313 | - ->method('invalidate') |
|
314 | - ->willReturnCallback(function () use (&$calls) { |
|
315 | - $expected = array_shift($calls); |
|
316 | - $this->assertEquals($expected, func_get_args()); |
|
317 | - }); |
|
318 | - |
|
319 | - $this->tokenProvider->invalidateToken('token7'); |
|
320 | - } |
|
321 | - |
|
322 | - public function testInvalidateTokenById(): void { |
|
323 | - $id = 123; |
|
324 | - |
|
325 | - $this->mapper->expects($this->once()) |
|
326 | - ->method('getTokenById') |
|
327 | - ->with($id); |
|
328 | - |
|
329 | - $this->tokenProvider->invalidateTokenById('uid', $id); |
|
330 | - } |
|
331 | - |
|
332 | - public function testInvalidateOldTokens(): void { |
|
333 | - $defaultSessionLifetime = 60 * 60 * 24; |
|
334 | - $defaultRememberMeLifetime = 60 * 60 * 24 * 15; |
|
335 | - $wipeTokenLifetime = 60 * 60 * 24 * 60; |
|
336 | - $this->config->expects($this->exactly(4)) |
|
337 | - ->method('getSystemValueInt') |
|
338 | - ->willReturnMap([ |
|
339 | - ['session_lifetime', $defaultSessionLifetime, 150], |
|
340 | - ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300], |
|
341 | - ['token_auth_wipe_token_retention', $wipeTokenLifetime, 500], |
|
342 | - ['token_auth_token_retention', 60 * 60 * 24 * 365, 800], |
|
343 | - ]); |
|
344 | - |
|
345 | - $calls = [ |
|
346 | - [$this->time - 150, IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER], |
|
347 | - [$this->time - 300, IToken::TEMPORARY_TOKEN, IToken::REMEMBER], |
|
348 | - [$this->time - 500, IToken::WIPE_TOKEN, null], |
|
349 | - [$this->time - 800, IToken::PERMANENT_TOKEN, null], |
|
350 | - ]; |
|
351 | - $this->mapper->expects($this->exactly(4)) |
|
352 | - ->method('invalidateOld') |
|
353 | - ->willReturnCallback(function () use (&$calls) { |
|
354 | - $expected = array_shift($calls); |
|
355 | - $this->assertEquals($expected, func_get_args()); |
|
356 | - }); |
|
357 | - |
|
358 | - $this->tokenProvider->invalidateOldTokens(); |
|
359 | - } |
|
360 | - |
|
361 | - public function testInvalidateLastUsedBefore(): void { |
|
362 | - $this->mapper->expects($this->once()) |
|
363 | - ->method('invalidateLastUsedBefore') |
|
364 | - ->with('user', 946684800); |
|
365 | - |
|
366 | - $this->tokenProvider->invalidateLastUsedBefore('user', 946684800); |
|
367 | - } |
|
368 | - |
|
369 | - public function testRenewSessionTokenWithoutPassword(): void { |
|
370 | - $token = 'oldIdtokentokentokentoken'; |
|
371 | - $uid = 'user'; |
|
372 | - $user = 'User'; |
|
373 | - $password = null; |
|
374 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
375 | - $type = IToken::PERMANENT_TOKEN; |
|
376 | - |
|
377 | - $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
378 | - |
|
379 | - $this->mapper |
|
380 | - ->expects($this->once()) |
|
381 | - ->method('getToken') |
|
382 | - ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) |
|
383 | - ->willReturn($oldToken); |
|
384 | - $this->mapper |
|
385 | - ->expects($this->once()) |
|
386 | - ->method('insert') |
|
387 | - ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) { |
|
388 | - return $token->getUID() === $uid && |
|
389 | - $token->getLoginName() === $user && |
|
390 | - $token->getName() === $name && |
|
391 | - $token->getType() === IToken::DO_NOT_REMEMBER && |
|
392 | - $token->getLastActivity() === $this->time && |
|
393 | - $token->getPassword() === null; |
|
394 | - })); |
|
395 | - $this->mapper |
|
396 | - ->expects($this->once()) |
|
397 | - ->method('delete') |
|
398 | - ->with($this->callback(function ($token) use ($oldToken) { |
|
399 | - return $token === $oldToken; |
|
400 | - })); |
|
401 | - |
|
402 | - $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken'); |
|
403 | - } |
|
404 | - |
|
405 | - public function testRenewSessionTokenWithPassword(): void { |
|
406 | - $token = 'oldIdtokentokentokentoken'; |
|
407 | - $uid = 'user'; |
|
408 | - $user = 'User'; |
|
409 | - $password = 'password'; |
|
410 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
411 | - $type = IToken::PERMANENT_TOKEN; |
|
412 | - |
|
413 | - $this->config->method('getSystemValueBool') |
|
414 | - ->willReturnMap([ |
|
415 | - ['auth.storeCryptedPassword', true, true], |
|
416 | - ]); |
|
417 | - $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
418 | - |
|
419 | - $this->mapper |
|
420 | - ->expects($this->once()) |
|
421 | - ->method('getToken') |
|
422 | - ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) |
|
423 | - ->willReturn($oldToken); |
|
424 | - $this->mapper |
|
425 | - ->expects($this->once()) |
|
426 | - ->method('insert') |
|
427 | - ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name): bool { |
|
428 | - return $token->getUID() === $uid && |
|
429 | - $token->getLoginName() === $user && |
|
430 | - $token->getName() === $name && |
|
431 | - $token->getType() === IToken::DO_NOT_REMEMBER && |
|
432 | - $token->getLastActivity() === $this->time && |
|
433 | - $token->getPassword() !== null && |
|
434 | - $this->tokenProvider->getPassword($token, 'newIdtokentokentokentoken') === 'password'; |
|
435 | - })); |
|
436 | - $this->mapper |
|
437 | - ->expects($this->once()) |
|
438 | - ->method('delete') |
|
439 | - ->with($this->callback(function ($token) use ($oldToken): bool { |
|
440 | - return $token === $oldToken; |
|
441 | - })); |
|
442 | - |
|
443 | - $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken'); |
|
444 | - } |
|
445 | - |
|
446 | - public function testGetToken(): void { |
|
447 | - $token = new PublicKeyToken(); |
|
448 | - |
|
449 | - $this->config->method('getSystemValue') |
|
450 | - ->with('secret') |
|
451 | - ->willReturn('mysecret'); |
|
452 | - |
|
453 | - $this->mapper->method('getToken') |
|
454 | - ->with( |
|
455 | - $this->callback(function (string $token) { |
|
456 | - return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token; |
|
457 | - }) |
|
458 | - )->willReturn($token); |
|
459 | - |
|
460 | - $this->assertSame($token, $this->tokenProvider->getToken('unhashedTokentokentokentokentoken')); |
|
461 | - } |
|
462 | - |
|
463 | - public function testGetInvalidToken(): void { |
|
464 | - $this->expectException(InvalidTokenException::class); |
|
465 | - |
|
466 | - $calls = [ |
|
467 | - 'unhashedTokentokentokentokentoken' . '1f4h9s', |
|
468 | - 'unhashedTokentokentokentokentoken', |
|
469 | - ]; |
|
470 | - $this->mapper->expects($this->exactly(2)) |
|
471 | - ->method('getToken') |
|
472 | - ->willReturnCallback(function (string $token) use (&$calls) { |
|
473 | - $expected = array_shift($calls); |
|
474 | - $this->assertEquals(hash('sha512', $expected), $token); |
|
475 | - throw new DoesNotExistException('nope'); |
|
476 | - }); |
|
477 | - |
|
478 | - $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'); |
|
479 | - } |
|
480 | - |
|
481 | - public function testGetExpiredToken(): void { |
|
482 | - $token = 'tokentokentokentokentoken'; |
|
483 | - $uid = 'user'; |
|
484 | - $user = 'User'; |
|
485 | - $password = 'passme'; |
|
486 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
487 | - $type = IToken::PERMANENT_TOKEN; |
|
488 | - |
|
489 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
490 | - $actual->setExpires(42); |
|
491 | - |
|
492 | - $this->mapper->method('getToken') |
|
493 | - ->with( |
|
494 | - $this->callback(function (string $token) { |
|
495 | - return hash('sha512', 'tokentokentokentokentoken' . '1f4h9s') === $token; |
|
496 | - }) |
|
497 | - )->willReturn($actual); |
|
498 | - |
|
499 | - try { |
|
500 | - $this->tokenProvider->getToken('tokentokentokentokentoken'); |
|
501 | - $this->fail(); |
|
502 | - } catch (ExpiredTokenException $e) { |
|
503 | - $this->assertSame($actual, $e->getToken()); |
|
504 | - } |
|
505 | - } |
|
506 | - |
|
507 | - public function testGetTokenById(): void { |
|
508 | - $token = $this->createMock(PublicKeyToken::class); |
|
509 | - |
|
510 | - $this->mapper->expects($this->once()) |
|
511 | - ->method('getTokenById') |
|
512 | - ->with($this->equalTo(42)) |
|
513 | - ->willReturn($token); |
|
514 | - |
|
515 | - $this->assertSame($token, $this->tokenProvider->getTokenById(42)); |
|
516 | - } |
|
517 | - |
|
518 | - public function testGetInvalidTokenById(): void { |
|
519 | - $this->expectException(InvalidTokenException::class); |
|
520 | - |
|
521 | - $this->mapper->expects($this->once()) |
|
522 | - ->method('getTokenById') |
|
523 | - ->with($this->equalTo(42)) |
|
524 | - ->willThrowException(new DoesNotExistException('nope')); |
|
525 | - |
|
526 | - $this->tokenProvider->getTokenById(42); |
|
527 | - } |
|
528 | - |
|
529 | - public function testGetExpiredTokenById(): void { |
|
530 | - $token = new PublicKeyToken(); |
|
531 | - $token->setExpires(42); |
|
532 | - |
|
533 | - $this->mapper->expects($this->once()) |
|
534 | - ->method('getTokenById') |
|
535 | - ->with($this->equalTo(42)) |
|
536 | - ->willReturn($token); |
|
537 | - |
|
538 | - try { |
|
539 | - $this->tokenProvider->getTokenById(42); |
|
540 | - $this->fail(); |
|
541 | - } catch (ExpiredTokenException $e) { |
|
542 | - $this->assertSame($token, $e->getToken()); |
|
543 | - } |
|
544 | - } |
|
545 | - |
|
546 | - public function testRotate(): void { |
|
547 | - $token = 'oldtokentokentokentokentoken'; |
|
548 | - $uid = 'user'; |
|
549 | - $user = 'User'; |
|
550 | - $password = 'password'; |
|
551 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
552 | - $type = IToken::PERMANENT_TOKEN; |
|
553 | - |
|
554 | - $this->config->method('getSystemValueBool') |
|
555 | - ->willReturnMap([ |
|
556 | - ['auth.storeCryptedPassword', true, true], |
|
557 | - ]); |
|
558 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
559 | - |
|
560 | - $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken'); |
|
561 | - |
|
562 | - $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtokentokentokentokentoken')); |
|
563 | - } |
|
564 | - |
|
565 | - public function testRotateNoPassword(): void { |
|
566 | - $token = 'oldtokentokentokentokentoken'; |
|
567 | - $uid = 'user'; |
|
568 | - $user = 'User'; |
|
569 | - $password = null; |
|
570 | - $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
571 | - $type = IToken::PERMANENT_TOKEN; |
|
572 | - |
|
573 | - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
574 | - |
|
575 | - $oldPrivate = $actual->getPrivateKey(); |
|
576 | - |
|
577 | - $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken'); |
|
578 | - |
|
579 | - $newPrivate = $new->getPrivateKey(); |
|
580 | - |
|
581 | - $this->assertNotSame($newPrivate, $oldPrivate); |
|
582 | - $this->assertNull($new->getPassword()); |
|
583 | - } |
|
584 | - |
|
585 | - public function testMarkPasswordInvalidInvalidToken(): void { |
|
586 | - $token = $this->createMock(IToken::class); |
|
587 | - |
|
588 | - $this->expectException(InvalidTokenException::class); |
|
589 | - |
|
590 | - $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); |
|
591 | - } |
|
592 | - |
|
593 | - public function testMarkPasswordInvalid(): void { |
|
594 | - $token = $this->createMock(PublicKeyToken::class); |
|
595 | - |
|
596 | - $token->expects($this->once()) |
|
597 | - ->method('setPasswordInvalid') |
|
598 | - ->with(true); |
|
599 | - $this->mapper->expects($this->once()) |
|
600 | - ->method('update') |
|
601 | - ->with($token); |
|
602 | - |
|
603 | - $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); |
|
604 | - } |
|
605 | - |
|
606 | - public function testUpdatePasswords(): void { |
|
607 | - $uid = 'myUID'; |
|
608 | - $token1 = $this->tokenProvider->generateToken( |
|
609 | - 'foobetokentokentokentoken', |
|
610 | - $uid, |
|
611 | - $uid, |
|
612 | - 'bar', |
|
613 | - 'random1', |
|
614 | - IToken::PERMANENT_TOKEN, |
|
615 | - IToken::REMEMBER); |
|
616 | - $token2 = $this->tokenProvider->generateToken( |
|
617 | - 'foobartokentokentokentoken', |
|
618 | - $uid, |
|
619 | - $uid, |
|
620 | - 'bar', |
|
621 | - 'random2', |
|
622 | - IToken::PERMANENT_TOKEN, |
|
623 | - IToken::REMEMBER); |
|
624 | - $this->config->method('getSystemValueBool') |
|
625 | - ->willReturnMap([ |
|
626 | - ['auth.storeCryptedPassword', true, true], |
|
627 | - ]); |
|
628 | - |
|
629 | - $this->mapper->method('hasExpiredTokens') |
|
630 | - ->with($uid) |
|
631 | - ->willReturn(true); |
|
632 | - $this->mapper->expects($this->once()) |
|
633 | - ->method('getTokenByUser') |
|
634 | - ->with($uid) |
|
635 | - ->willReturn([$token1, $token2]); |
|
636 | - $this->mapper->expects($this->exactly(2)) |
|
637 | - ->method('update') |
|
638 | - ->with($this->callback(function (PublicKeyToken $t) use ($token1, $token2) { |
|
639 | - return $t === $token1 || $t === $token2; |
|
640 | - })); |
|
641 | - |
|
642 | - $this->tokenProvider->updatePasswords($uid, 'bar2'); |
|
643 | - } |
|
31 | + /** @var PublicKeyTokenProvider|\PHPUnit\Framework\MockObject\MockObject */ |
|
32 | + private $tokenProvider; |
|
33 | + /** @var PublicKeyTokenMapper|\PHPUnit\Framework\MockObject\MockObject */ |
|
34 | + private $mapper; |
|
35 | + /** @var IHasher|\PHPUnit\Framework\MockObject\MockObject */ |
|
36 | + private $hasher; |
|
37 | + /** @var ICrypto */ |
|
38 | + private $crypto; |
|
39 | + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
40 | + private $config; |
|
41 | + /** @var IDBConnection|MockObject */ |
|
42 | + private IDBConnection $db; |
|
43 | + /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
44 | + private $logger; |
|
45 | + /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
46 | + private $timeFactory; |
|
47 | + /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ |
|
48 | + private $cacheFactory; |
|
49 | + /** @var int */ |
|
50 | + private $time; |
|
51 | + |
|
52 | + protected function setUp(): void { |
|
53 | + parent::setUp(); |
|
54 | + |
|
55 | + $this->mapper = $this->createMock(PublicKeyTokenMapper::class); |
|
56 | + $this->hasher = \OC::$server->get(IHasher::class); |
|
57 | + $this->crypto = \OC::$server->getCrypto(); |
|
58 | + $this->config = $this->createMock(IConfig::class); |
|
59 | + $this->config->method('getSystemValue') |
|
60 | + ->willReturnMap([ |
|
61 | + ['openssl', [], []], |
|
62 | + ]); |
|
63 | + $this->config->method('getSystemValueString') |
|
64 | + ->willReturnMap([ |
|
65 | + ['secret', '', '1f4h9s'], |
|
66 | + ]); |
|
67 | + $this->db = $this->createMock(IDBConnection::class); |
|
68 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
69 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
70 | + $this->time = 1313131; |
|
71 | + $this->timeFactory->method('getTime') |
|
72 | + ->willReturn($this->time); |
|
73 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
|
74 | + |
|
75 | + $this->tokenProvider = new PublicKeyTokenProvider( |
|
76 | + $this->mapper, |
|
77 | + $this->crypto, |
|
78 | + $this->config, |
|
79 | + $this->db, |
|
80 | + $this->logger, |
|
81 | + $this->timeFactory, |
|
82 | + $this->hasher, |
|
83 | + $this->cacheFactory, |
|
84 | + ); |
|
85 | + } |
|
86 | + |
|
87 | + public function testGenerateToken(): void { |
|
88 | + $token = 'tokentokentokentokentoken'; |
|
89 | + $uid = 'user'; |
|
90 | + $user = 'User'; |
|
91 | + $password = 'passme'; |
|
92 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
93 | + $type = IToken::PERMANENT_TOKEN; |
|
94 | + |
|
95 | + $this->config->method('getSystemValueBool') |
|
96 | + ->willReturnMap([ |
|
97 | + ['auth.storeCryptedPassword', true, true], |
|
98 | + ]); |
|
99 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
100 | + |
|
101 | + $this->assertInstanceOf(PublicKeyToken::class, $actual); |
|
102 | + $this->assertSame($uid, $actual->getUID()); |
|
103 | + $this->assertSame($user, $actual->getLoginName()); |
|
104 | + $this->assertSame($name, $actual->getName()); |
|
105 | + $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember()); |
|
106 | + $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); |
|
107 | + } |
|
108 | + |
|
109 | + public function testGenerateTokenNoPassword(): void { |
|
110 | + $token = 'tokentokentokentokentoken'; |
|
111 | + $uid = 'user'; |
|
112 | + $user = 'User'; |
|
113 | + $password = 'passme'; |
|
114 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
115 | + $type = IToken::PERMANENT_TOKEN; |
|
116 | + $this->config->method('getSystemValueBool') |
|
117 | + ->willReturnMap([ |
|
118 | + ['auth.storeCryptedPassword', true, false], |
|
119 | + ]); |
|
120 | + $this->expectException(PasswordlessTokenException::class); |
|
121 | + |
|
122 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
123 | + |
|
124 | + $this->assertInstanceOf(PublicKeyToken::class, $actual); |
|
125 | + $this->assertSame($uid, $actual->getUID()); |
|
126 | + $this->assertSame($user, $actual->getLoginName()); |
|
127 | + $this->assertSame($name, $actual->getName()); |
|
128 | + $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember()); |
|
129 | + $this->tokenProvider->getPassword($actual, $token); |
|
130 | + } |
|
131 | + |
|
132 | + public function testGenerateTokenLongPassword(): void { |
|
133 | + $token = 'tokentokentokentokentoken'; |
|
134 | + $uid = 'user'; |
|
135 | + $user = 'User'; |
|
136 | + $password = ''; |
|
137 | + for ($i = 0; $i < 500; $i++) { |
|
138 | + $password .= 'e'; |
|
139 | + } |
|
140 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
141 | + $type = IToken::PERMANENT_TOKEN; |
|
142 | + $this->config->method('getSystemValueBool') |
|
143 | + ->willReturnMap([ |
|
144 | + ['auth.storeCryptedPassword', true, true], |
|
145 | + ]); |
|
146 | + $this->expectException(\RuntimeException::class); |
|
147 | + |
|
148 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
149 | + } |
|
150 | + |
|
151 | + public function testGenerateTokenInvalidName(): void { |
|
152 | + $token = 'tokentokentokentokentoken'; |
|
153 | + $uid = 'user'; |
|
154 | + $user = 'User'; |
|
155 | + $password = 'passme'; |
|
156 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' |
|
157 | + . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' |
|
158 | + . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' |
|
159 | + . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
160 | + $type = IToken::PERMANENT_TOKEN; |
|
161 | + $this->config->method('getSystemValueBool') |
|
162 | + ->willReturnMap([ |
|
163 | + ['auth.storeCryptedPassword', true, true], |
|
164 | + ]); |
|
165 | + |
|
166 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
167 | + |
|
168 | + $this->assertInstanceOf(PublicKeyToken::class, $actual); |
|
169 | + $this->assertSame($uid, $actual->getUID()); |
|
170 | + $this->assertSame($user, $actual->getLoginName()); |
|
171 | + $this->assertSame('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12User-Agent: Mozill…', $actual->getName()); |
|
172 | + $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember()); |
|
173 | + $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); |
|
174 | + } |
|
175 | + |
|
176 | + public function testUpdateToken(): void { |
|
177 | + $tk = new PublicKeyToken(); |
|
178 | + $this->mapper->expects($this->once()) |
|
179 | + ->method('updateActivity') |
|
180 | + ->with($tk, $this->time); |
|
181 | + $tk->setLastActivity($this->time - 200); |
|
182 | + $this->config->method('getSystemValueBool') |
|
183 | + ->willReturnMap([ |
|
184 | + ['auth.storeCryptedPassword', true, true], |
|
185 | + ]); |
|
186 | + |
|
187 | + $this->tokenProvider->updateTokenActivity($tk); |
|
188 | + |
|
189 | + $this->assertEquals($this->time, $tk->getLastActivity()); |
|
190 | + } |
|
191 | + |
|
192 | + public function testUpdateTokenDebounce(): void { |
|
193 | + $tk = new PublicKeyToken(); |
|
194 | + $this->config->method('getSystemValueInt') |
|
195 | + ->willReturnCallback(function ($value, $default) { |
|
196 | + return $default; |
|
197 | + }); |
|
198 | + $tk->setLastActivity($this->time - 30); |
|
199 | + |
|
200 | + $this->mapper->expects($this->never()) |
|
201 | + ->method('updateActivity') |
|
202 | + ->with($tk, $this->time); |
|
203 | + |
|
204 | + $this->tokenProvider->updateTokenActivity($tk); |
|
205 | + } |
|
206 | + |
|
207 | + public function testGetTokenByUser(): void { |
|
208 | + $this->mapper->expects($this->once()) |
|
209 | + ->method('getTokenByUser') |
|
210 | + ->with('uid') |
|
211 | + ->willReturn(['token']); |
|
212 | + |
|
213 | + $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid')); |
|
214 | + } |
|
215 | + |
|
216 | + public function testGetPassword(): void { |
|
217 | + $token = 'tokentokentokentokentoken'; |
|
218 | + $uid = 'user'; |
|
219 | + $user = 'User'; |
|
220 | + $password = 'passme'; |
|
221 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
222 | + $type = IToken::PERMANENT_TOKEN; |
|
223 | + $this->config->method('getSystemValueBool') |
|
224 | + ->willReturnMap([ |
|
225 | + ['auth.storeCryptedPassword', true, true], |
|
226 | + ]); |
|
227 | + |
|
228 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
229 | + |
|
230 | + $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token)); |
|
231 | + } |
|
232 | + |
|
233 | + |
|
234 | + public function testGetPasswordPasswordLessToken(): void { |
|
235 | + $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class); |
|
236 | + |
|
237 | + $token = 'token1234'; |
|
238 | + $tk = new PublicKeyToken(); |
|
239 | + $tk->setPassword(null); |
|
240 | + |
|
241 | + $this->tokenProvider->getPassword($tk, $token); |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + public function testGetPasswordInvalidToken(): void { |
|
246 | + $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); |
|
247 | + |
|
248 | + $token = 'tokentokentokentokentoken'; |
|
249 | + $uid = 'user'; |
|
250 | + $user = 'User'; |
|
251 | + $password = 'passme'; |
|
252 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
253 | + $type = IToken::PERMANENT_TOKEN; |
|
254 | + |
|
255 | + $this->config->method('getSystemValueBool') |
|
256 | + ->willReturnMap([ |
|
257 | + ['auth.storeCryptedPassword', true, true], |
|
258 | + ]); |
|
259 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
260 | + |
|
261 | + $this->tokenProvider->getPassword($actual, 'wrongtoken'); |
|
262 | + } |
|
263 | + |
|
264 | + public function testSetPassword(): void { |
|
265 | + $token = 'tokentokentokentokentoken'; |
|
266 | + $uid = 'user'; |
|
267 | + $user = 'User'; |
|
268 | + $password = 'passme'; |
|
269 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
270 | + $type = IToken::PERMANENT_TOKEN; |
|
271 | + $this->config->method('getSystemValueBool') |
|
272 | + ->willReturnMap([ |
|
273 | + ['auth.storeCryptedPassword', true, true], |
|
274 | + ]); |
|
275 | + |
|
276 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
277 | + |
|
278 | + $this->mapper->method('getTokenByUser') |
|
279 | + ->with('user') |
|
280 | + ->willReturn([$actual]); |
|
281 | + |
|
282 | + $newpass = 'newpass'; |
|
283 | + $this->mapper->expects($this->once()) |
|
284 | + ->method('update') |
|
285 | + ->with($this->callback(function ($token) use ($newpass) { |
|
286 | + return $newpass === $this->tokenProvider->getPassword($token, 'tokentokentokentokentoken'); |
|
287 | + })); |
|
288 | + |
|
289 | + |
|
290 | + $this->tokenProvider->setPassword($actual, $token, $newpass); |
|
291 | + |
|
292 | + $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'tokentokentokentokentoken')); |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + public function testSetPasswordInvalidToken(): void { |
|
297 | + $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); |
|
298 | + |
|
299 | + $token = $this->createMock(IToken::class); |
|
300 | + $tokenId = 'token123'; |
|
301 | + $password = '123456'; |
|
302 | + |
|
303 | + $this->tokenProvider->setPassword($token, $tokenId, $password); |
|
304 | + } |
|
305 | + |
|
306 | + public function testInvalidateToken(): void { |
|
307 | + $calls = [ |
|
308 | + [hash('sha512', 'token7' . '1f4h9s')], |
|
309 | + [hash('sha512', 'token7')] |
|
310 | + ]; |
|
311 | + |
|
312 | + $this->mapper->expects($this->exactly(2)) |
|
313 | + ->method('invalidate') |
|
314 | + ->willReturnCallback(function () use (&$calls) { |
|
315 | + $expected = array_shift($calls); |
|
316 | + $this->assertEquals($expected, func_get_args()); |
|
317 | + }); |
|
318 | + |
|
319 | + $this->tokenProvider->invalidateToken('token7'); |
|
320 | + } |
|
321 | + |
|
322 | + public function testInvalidateTokenById(): void { |
|
323 | + $id = 123; |
|
324 | + |
|
325 | + $this->mapper->expects($this->once()) |
|
326 | + ->method('getTokenById') |
|
327 | + ->with($id); |
|
328 | + |
|
329 | + $this->tokenProvider->invalidateTokenById('uid', $id); |
|
330 | + } |
|
331 | + |
|
332 | + public function testInvalidateOldTokens(): void { |
|
333 | + $defaultSessionLifetime = 60 * 60 * 24; |
|
334 | + $defaultRememberMeLifetime = 60 * 60 * 24 * 15; |
|
335 | + $wipeTokenLifetime = 60 * 60 * 24 * 60; |
|
336 | + $this->config->expects($this->exactly(4)) |
|
337 | + ->method('getSystemValueInt') |
|
338 | + ->willReturnMap([ |
|
339 | + ['session_lifetime', $defaultSessionLifetime, 150], |
|
340 | + ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300], |
|
341 | + ['token_auth_wipe_token_retention', $wipeTokenLifetime, 500], |
|
342 | + ['token_auth_token_retention', 60 * 60 * 24 * 365, 800], |
|
343 | + ]); |
|
344 | + |
|
345 | + $calls = [ |
|
346 | + [$this->time - 150, IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER], |
|
347 | + [$this->time - 300, IToken::TEMPORARY_TOKEN, IToken::REMEMBER], |
|
348 | + [$this->time - 500, IToken::WIPE_TOKEN, null], |
|
349 | + [$this->time - 800, IToken::PERMANENT_TOKEN, null], |
|
350 | + ]; |
|
351 | + $this->mapper->expects($this->exactly(4)) |
|
352 | + ->method('invalidateOld') |
|
353 | + ->willReturnCallback(function () use (&$calls) { |
|
354 | + $expected = array_shift($calls); |
|
355 | + $this->assertEquals($expected, func_get_args()); |
|
356 | + }); |
|
357 | + |
|
358 | + $this->tokenProvider->invalidateOldTokens(); |
|
359 | + } |
|
360 | + |
|
361 | + public function testInvalidateLastUsedBefore(): void { |
|
362 | + $this->mapper->expects($this->once()) |
|
363 | + ->method('invalidateLastUsedBefore') |
|
364 | + ->with('user', 946684800); |
|
365 | + |
|
366 | + $this->tokenProvider->invalidateLastUsedBefore('user', 946684800); |
|
367 | + } |
|
368 | + |
|
369 | + public function testRenewSessionTokenWithoutPassword(): void { |
|
370 | + $token = 'oldIdtokentokentokentoken'; |
|
371 | + $uid = 'user'; |
|
372 | + $user = 'User'; |
|
373 | + $password = null; |
|
374 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
375 | + $type = IToken::PERMANENT_TOKEN; |
|
376 | + |
|
377 | + $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
378 | + |
|
379 | + $this->mapper |
|
380 | + ->expects($this->once()) |
|
381 | + ->method('getToken') |
|
382 | + ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) |
|
383 | + ->willReturn($oldToken); |
|
384 | + $this->mapper |
|
385 | + ->expects($this->once()) |
|
386 | + ->method('insert') |
|
387 | + ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) { |
|
388 | + return $token->getUID() === $uid && |
|
389 | + $token->getLoginName() === $user && |
|
390 | + $token->getName() === $name && |
|
391 | + $token->getType() === IToken::DO_NOT_REMEMBER && |
|
392 | + $token->getLastActivity() === $this->time && |
|
393 | + $token->getPassword() === null; |
|
394 | + })); |
|
395 | + $this->mapper |
|
396 | + ->expects($this->once()) |
|
397 | + ->method('delete') |
|
398 | + ->with($this->callback(function ($token) use ($oldToken) { |
|
399 | + return $token === $oldToken; |
|
400 | + })); |
|
401 | + |
|
402 | + $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken'); |
|
403 | + } |
|
404 | + |
|
405 | + public function testRenewSessionTokenWithPassword(): void { |
|
406 | + $token = 'oldIdtokentokentokentoken'; |
|
407 | + $uid = 'user'; |
|
408 | + $user = 'User'; |
|
409 | + $password = 'password'; |
|
410 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
411 | + $type = IToken::PERMANENT_TOKEN; |
|
412 | + |
|
413 | + $this->config->method('getSystemValueBool') |
|
414 | + ->willReturnMap([ |
|
415 | + ['auth.storeCryptedPassword', true, true], |
|
416 | + ]); |
|
417 | + $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
418 | + |
|
419 | + $this->mapper |
|
420 | + ->expects($this->once()) |
|
421 | + ->method('getToken') |
|
422 | + ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) |
|
423 | + ->willReturn($oldToken); |
|
424 | + $this->mapper |
|
425 | + ->expects($this->once()) |
|
426 | + ->method('insert') |
|
427 | + ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name): bool { |
|
428 | + return $token->getUID() === $uid && |
|
429 | + $token->getLoginName() === $user && |
|
430 | + $token->getName() === $name && |
|
431 | + $token->getType() === IToken::DO_NOT_REMEMBER && |
|
432 | + $token->getLastActivity() === $this->time && |
|
433 | + $token->getPassword() !== null && |
|
434 | + $this->tokenProvider->getPassword($token, 'newIdtokentokentokentoken') === 'password'; |
|
435 | + })); |
|
436 | + $this->mapper |
|
437 | + ->expects($this->once()) |
|
438 | + ->method('delete') |
|
439 | + ->with($this->callback(function ($token) use ($oldToken): bool { |
|
440 | + return $token === $oldToken; |
|
441 | + })); |
|
442 | + |
|
443 | + $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken'); |
|
444 | + } |
|
445 | + |
|
446 | + public function testGetToken(): void { |
|
447 | + $token = new PublicKeyToken(); |
|
448 | + |
|
449 | + $this->config->method('getSystemValue') |
|
450 | + ->with('secret') |
|
451 | + ->willReturn('mysecret'); |
|
452 | + |
|
453 | + $this->mapper->method('getToken') |
|
454 | + ->with( |
|
455 | + $this->callback(function (string $token) { |
|
456 | + return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token; |
|
457 | + }) |
|
458 | + )->willReturn($token); |
|
459 | + |
|
460 | + $this->assertSame($token, $this->tokenProvider->getToken('unhashedTokentokentokentokentoken')); |
|
461 | + } |
|
462 | + |
|
463 | + public function testGetInvalidToken(): void { |
|
464 | + $this->expectException(InvalidTokenException::class); |
|
465 | + |
|
466 | + $calls = [ |
|
467 | + 'unhashedTokentokentokentokentoken' . '1f4h9s', |
|
468 | + 'unhashedTokentokentokentokentoken', |
|
469 | + ]; |
|
470 | + $this->mapper->expects($this->exactly(2)) |
|
471 | + ->method('getToken') |
|
472 | + ->willReturnCallback(function (string $token) use (&$calls) { |
|
473 | + $expected = array_shift($calls); |
|
474 | + $this->assertEquals(hash('sha512', $expected), $token); |
|
475 | + throw new DoesNotExistException('nope'); |
|
476 | + }); |
|
477 | + |
|
478 | + $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'); |
|
479 | + } |
|
480 | + |
|
481 | + public function testGetExpiredToken(): void { |
|
482 | + $token = 'tokentokentokentokentoken'; |
|
483 | + $uid = 'user'; |
|
484 | + $user = 'User'; |
|
485 | + $password = 'passme'; |
|
486 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
487 | + $type = IToken::PERMANENT_TOKEN; |
|
488 | + |
|
489 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
490 | + $actual->setExpires(42); |
|
491 | + |
|
492 | + $this->mapper->method('getToken') |
|
493 | + ->with( |
|
494 | + $this->callback(function (string $token) { |
|
495 | + return hash('sha512', 'tokentokentokentokentoken' . '1f4h9s') === $token; |
|
496 | + }) |
|
497 | + )->willReturn($actual); |
|
498 | + |
|
499 | + try { |
|
500 | + $this->tokenProvider->getToken('tokentokentokentokentoken'); |
|
501 | + $this->fail(); |
|
502 | + } catch (ExpiredTokenException $e) { |
|
503 | + $this->assertSame($actual, $e->getToken()); |
|
504 | + } |
|
505 | + } |
|
506 | + |
|
507 | + public function testGetTokenById(): void { |
|
508 | + $token = $this->createMock(PublicKeyToken::class); |
|
509 | + |
|
510 | + $this->mapper->expects($this->once()) |
|
511 | + ->method('getTokenById') |
|
512 | + ->with($this->equalTo(42)) |
|
513 | + ->willReturn($token); |
|
514 | + |
|
515 | + $this->assertSame($token, $this->tokenProvider->getTokenById(42)); |
|
516 | + } |
|
517 | + |
|
518 | + public function testGetInvalidTokenById(): void { |
|
519 | + $this->expectException(InvalidTokenException::class); |
|
520 | + |
|
521 | + $this->mapper->expects($this->once()) |
|
522 | + ->method('getTokenById') |
|
523 | + ->with($this->equalTo(42)) |
|
524 | + ->willThrowException(new DoesNotExistException('nope')); |
|
525 | + |
|
526 | + $this->tokenProvider->getTokenById(42); |
|
527 | + } |
|
528 | + |
|
529 | + public function testGetExpiredTokenById(): void { |
|
530 | + $token = new PublicKeyToken(); |
|
531 | + $token->setExpires(42); |
|
532 | + |
|
533 | + $this->mapper->expects($this->once()) |
|
534 | + ->method('getTokenById') |
|
535 | + ->with($this->equalTo(42)) |
|
536 | + ->willReturn($token); |
|
537 | + |
|
538 | + try { |
|
539 | + $this->tokenProvider->getTokenById(42); |
|
540 | + $this->fail(); |
|
541 | + } catch (ExpiredTokenException $e) { |
|
542 | + $this->assertSame($token, $e->getToken()); |
|
543 | + } |
|
544 | + } |
|
545 | + |
|
546 | + public function testRotate(): void { |
|
547 | + $token = 'oldtokentokentokentokentoken'; |
|
548 | + $uid = 'user'; |
|
549 | + $user = 'User'; |
|
550 | + $password = 'password'; |
|
551 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
552 | + $type = IToken::PERMANENT_TOKEN; |
|
553 | + |
|
554 | + $this->config->method('getSystemValueBool') |
|
555 | + ->willReturnMap([ |
|
556 | + ['auth.storeCryptedPassword', true, true], |
|
557 | + ]); |
|
558 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
559 | + |
|
560 | + $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken'); |
|
561 | + |
|
562 | + $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtokentokentokentokentoken')); |
|
563 | + } |
|
564 | + |
|
565 | + public function testRotateNoPassword(): void { |
|
566 | + $token = 'oldtokentokentokentokentoken'; |
|
567 | + $uid = 'user'; |
|
568 | + $user = 'User'; |
|
569 | + $password = null; |
|
570 | + $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'; |
|
571 | + $type = IToken::PERMANENT_TOKEN; |
|
572 | + |
|
573 | + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); |
|
574 | + |
|
575 | + $oldPrivate = $actual->getPrivateKey(); |
|
576 | + |
|
577 | + $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken'); |
|
578 | + |
|
579 | + $newPrivate = $new->getPrivateKey(); |
|
580 | + |
|
581 | + $this->assertNotSame($newPrivate, $oldPrivate); |
|
582 | + $this->assertNull($new->getPassword()); |
|
583 | + } |
|
584 | + |
|
585 | + public function testMarkPasswordInvalidInvalidToken(): void { |
|
586 | + $token = $this->createMock(IToken::class); |
|
587 | + |
|
588 | + $this->expectException(InvalidTokenException::class); |
|
589 | + |
|
590 | + $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); |
|
591 | + } |
|
592 | + |
|
593 | + public function testMarkPasswordInvalid(): void { |
|
594 | + $token = $this->createMock(PublicKeyToken::class); |
|
595 | + |
|
596 | + $token->expects($this->once()) |
|
597 | + ->method('setPasswordInvalid') |
|
598 | + ->with(true); |
|
599 | + $this->mapper->expects($this->once()) |
|
600 | + ->method('update') |
|
601 | + ->with($token); |
|
602 | + |
|
603 | + $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); |
|
604 | + } |
|
605 | + |
|
606 | + public function testUpdatePasswords(): void { |
|
607 | + $uid = 'myUID'; |
|
608 | + $token1 = $this->tokenProvider->generateToken( |
|
609 | + 'foobetokentokentokentoken', |
|
610 | + $uid, |
|
611 | + $uid, |
|
612 | + 'bar', |
|
613 | + 'random1', |
|
614 | + IToken::PERMANENT_TOKEN, |
|
615 | + IToken::REMEMBER); |
|
616 | + $token2 = $this->tokenProvider->generateToken( |
|
617 | + 'foobartokentokentokentoken', |
|
618 | + $uid, |
|
619 | + $uid, |
|
620 | + 'bar', |
|
621 | + 'random2', |
|
622 | + IToken::PERMANENT_TOKEN, |
|
623 | + IToken::REMEMBER); |
|
624 | + $this->config->method('getSystemValueBool') |
|
625 | + ->willReturnMap([ |
|
626 | + ['auth.storeCryptedPassword', true, true], |
|
627 | + ]); |
|
628 | + |
|
629 | + $this->mapper->method('hasExpiredTokens') |
|
630 | + ->with($uid) |
|
631 | + ->willReturn(true); |
|
632 | + $this->mapper->expects($this->once()) |
|
633 | + ->method('getTokenByUser') |
|
634 | + ->with($uid) |
|
635 | + ->willReturn([$token1, $token2]); |
|
636 | + $this->mapper->expects($this->exactly(2)) |
|
637 | + ->method('update') |
|
638 | + ->with($this->callback(function (PublicKeyToken $t) use ($token1, $token2) { |
|
639 | + return $t === $token1 || $t === $token2; |
|
640 | + })); |
|
641 | + |
|
642 | + $this->tokenProvider->updatePasswords($uid, 'bar2'); |
|
643 | + } |
|
644 | 644 | } |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | public function testUpdateTokenDebounce(): void { |
193 | 193 | $tk = new PublicKeyToken(); |
194 | 194 | $this->config->method('getSystemValueInt') |
195 | - ->willReturnCallback(function ($value, $default) { |
|
195 | + ->willReturnCallback(function($value, $default) { |
|
196 | 196 | return $default; |
197 | 197 | }); |
198 | 198 | $tk->setLastActivity($this->time - 30); |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | $newpass = 'newpass'; |
283 | 283 | $this->mapper->expects($this->once()) |
284 | 284 | ->method('update') |
285 | - ->with($this->callback(function ($token) use ($newpass) { |
|
285 | + ->with($this->callback(function($token) use ($newpass) { |
|
286 | 286 | return $newpass === $this->tokenProvider->getPassword($token, 'tokentokentokentokentoken'); |
287 | 287 | })); |
288 | 288 | |
@@ -305,13 +305,13 @@ discard block |
||
305 | 305 | |
306 | 306 | public function testInvalidateToken(): void { |
307 | 307 | $calls = [ |
308 | - [hash('sha512', 'token7' . '1f4h9s')], |
|
308 | + [hash('sha512', 'token7'.'1f4h9s')], |
|
309 | 309 | [hash('sha512', 'token7')] |
310 | 310 | ]; |
311 | 311 | |
312 | 312 | $this->mapper->expects($this->exactly(2)) |
313 | 313 | ->method('invalidate') |
314 | - ->willReturnCallback(function () use (&$calls) { |
|
314 | + ->willReturnCallback(function() use (&$calls) { |
|
315 | 315 | $expected = array_shift($calls); |
316 | 316 | $this->assertEquals($expected, func_get_args()); |
317 | 317 | }); |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | ]; |
351 | 351 | $this->mapper->expects($this->exactly(4)) |
352 | 352 | ->method('invalidateOld') |
353 | - ->willReturnCallback(function () use (&$calls) { |
|
353 | + ->willReturnCallback(function() use (&$calls) { |
|
354 | 354 | $expected = array_shift($calls); |
355 | 355 | $this->assertEquals($expected, func_get_args()); |
356 | 356 | }); |
@@ -379,12 +379,12 @@ discard block |
||
379 | 379 | $this->mapper |
380 | 380 | ->expects($this->once()) |
381 | 381 | ->method('getToken') |
382 | - ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) |
|
382 | + ->with(hash('sha512', 'oldIdtokentokentokentoken'.'1f4h9s')) |
|
383 | 383 | ->willReturn($oldToken); |
384 | 384 | $this->mapper |
385 | 385 | ->expects($this->once()) |
386 | 386 | ->method('insert') |
387 | - ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) { |
|
387 | + ->with($this->callback(function(PublicKeyToken $token) use ($user, $uid, $name) { |
|
388 | 388 | return $token->getUID() === $uid && |
389 | 389 | $token->getLoginName() === $user && |
390 | 390 | $token->getName() === $name && |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | $this->mapper |
396 | 396 | ->expects($this->once()) |
397 | 397 | ->method('delete') |
398 | - ->with($this->callback(function ($token) use ($oldToken) { |
|
398 | + ->with($this->callback(function($token) use ($oldToken) { |
|
399 | 399 | return $token === $oldToken; |
400 | 400 | })); |
401 | 401 | |
@@ -419,12 +419,12 @@ discard block |
||
419 | 419 | $this->mapper |
420 | 420 | ->expects($this->once()) |
421 | 421 | ->method('getToken') |
422 | - ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s')) |
|
422 | + ->with(hash('sha512', 'oldIdtokentokentokentoken'.'1f4h9s')) |
|
423 | 423 | ->willReturn($oldToken); |
424 | 424 | $this->mapper |
425 | 425 | ->expects($this->once()) |
426 | 426 | ->method('insert') |
427 | - ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name): bool { |
|
427 | + ->with($this->callback(function(PublicKeyToken $token) use ($user, $uid, $name): bool { |
|
428 | 428 | return $token->getUID() === $uid && |
429 | 429 | $token->getLoginName() === $user && |
430 | 430 | $token->getName() === $name && |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | $this->mapper |
437 | 437 | ->expects($this->once()) |
438 | 438 | ->method('delete') |
439 | - ->with($this->callback(function ($token) use ($oldToken): bool { |
|
439 | + ->with($this->callback(function($token) use ($oldToken): bool { |
|
440 | 440 | return $token === $oldToken; |
441 | 441 | })); |
442 | 442 | |
@@ -452,8 +452,8 @@ discard block |
||
452 | 452 | |
453 | 453 | $this->mapper->method('getToken') |
454 | 454 | ->with( |
455 | - $this->callback(function (string $token) { |
|
456 | - return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token; |
|
455 | + $this->callback(function(string $token) { |
|
456 | + return hash('sha512', 'unhashedTokentokentokentokentoken'.'1f4h9s') === $token; |
|
457 | 457 | }) |
458 | 458 | )->willReturn($token); |
459 | 459 | |
@@ -464,12 +464,12 @@ discard block |
||
464 | 464 | $this->expectException(InvalidTokenException::class); |
465 | 465 | |
466 | 466 | $calls = [ |
467 | - 'unhashedTokentokentokentokentoken' . '1f4h9s', |
|
467 | + 'unhashedTokentokentokentokentoken'.'1f4h9s', |
|
468 | 468 | 'unhashedTokentokentokentokentoken', |
469 | 469 | ]; |
470 | 470 | $this->mapper->expects($this->exactly(2)) |
471 | 471 | ->method('getToken') |
472 | - ->willReturnCallback(function (string $token) use (&$calls) { |
|
472 | + ->willReturnCallback(function(string $token) use (&$calls) { |
|
473 | 473 | $expected = array_shift($calls); |
474 | 474 | $this->assertEquals(hash('sha512', $expected), $token); |
475 | 475 | throw new DoesNotExistException('nope'); |
@@ -491,8 +491,8 @@ discard block |
||
491 | 491 | |
492 | 492 | $this->mapper->method('getToken') |
493 | 493 | ->with( |
494 | - $this->callback(function (string $token) { |
|
495 | - return hash('sha512', 'tokentokentokentokentoken' . '1f4h9s') === $token; |
|
494 | + $this->callback(function(string $token) { |
|
495 | + return hash('sha512', 'tokentokentokentokentoken'.'1f4h9s') === $token; |
|
496 | 496 | }) |
497 | 497 | )->willReturn($actual); |
498 | 498 | |
@@ -635,7 +635,7 @@ discard block |
||
635 | 635 | ->willReturn([$token1, $token2]); |
636 | 636 | $this->mapper->expects($this->exactly(2)) |
637 | 637 | ->method('update') |
638 | - ->with($this->callback(function (PublicKeyToken $t) use ($token1, $token2) { |
|
638 | + ->with($this->callback(function(PublicKeyToken $t) use ($token1, $token2) { |
|
639 | 639 | return $t === $token1 || $t === $token2; |
640 | 640 | })); |
641 | 641 |
@@ -21,83 +21,83 @@ |
||
21 | 21 | use Test\TestCase; |
22 | 22 | |
23 | 23 | class UserDeletedTokenCleanupListenerTest extends TestCase { |
24 | - /** @var Manager|MockObject */ |
|
25 | - private $manager; |
|
26 | - |
|
27 | - /** @var LoggerInterface|MockObject */ |
|
28 | - private $logger; |
|
29 | - |
|
30 | - /** @var UserDeletedTokenCleanupListener */ |
|
31 | - private $listener; |
|
32 | - |
|
33 | - protected function setUp(): void { |
|
34 | - parent::setUp(); |
|
35 | - |
|
36 | - $this->manager = $this->createMock(Manager::class); |
|
37 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
38 | - |
|
39 | - $this->listener = new UserDeletedTokenCleanupListener( |
|
40 | - $this->manager, |
|
41 | - $this->logger |
|
42 | - ); |
|
43 | - } |
|
44 | - |
|
45 | - public function testHandleUnrelated(): void { |
|
46 | - $event = new Event(); |
|
47 | - $this->manager->expects($this->never())->method('getTokenByUser'); |
|
48 | - $this->logger->expects($this->never())->method('error'); |
|
49 | - |
|
50 | - $this->listener->handle($event); |
|
51 | - } |
|
52 | - |
|
53 | - public function testHandleWithErrors(): void { |
|
54 | - $user = $this->createMock(IUser::class); |
|
55 | - $user->method('getUID')->willReturn('user123'); |
|
56 | - $event = new UserDeletedEvent($user); |
|
57 | - $exception = new Exception('nope'); |
|
58 | - $this->manager->expects($this->once()) |
|
59 | - ->method('getTokenByUser') |
|
60 | - ->with('user123') |
|
61 | - ->willThrowException($exception); |
|
62 | - $this->logger->expects($this->once()) |
|
63 | - ->method('error'); |
|
64 | - |
|
65 | - $this->listener->handle($event); |
|
66 | - } |
|
67 | - |
|
68 | - public function testHandle(): void { |
|
69 | - $user = $this->createMock(IUser::class); |
|
70 | - $user->method('getUID')->willReturn('user123'); |
|
71 | - $event = new UserDeletedEvent($user); |
|
72 | - $token1 = $this->createMock(IToken::class); |
|
73 | - $token1->method('getId')->willReturn(1); |
|
74 | - $token2 = $this->createMock(IToken::class); |
|
75 | - $token2->method('getId')->willReturn(2); |
|
76 | - $token3 = $this->createMock(IToken::class); |
|
77 | - $token3->method('getId')->willReturn(3); |
|
78 | - $this->manager->expects($this->once()) |
|
79 | - ->method('getTokenByUser') |
|
80 | - ->with('user123') |
|
81 | - ->willReturn([ |
|
82 | - $token1, |
|
83 | - $token2, |
|
84 | - $token3, |
|
85 | - ]); |
|
86 | - |
|
87 | - $calls = [ |
|
88 | - ['user123', 1], |
|
89 | - ['user123', 2], |
|
90 | - ['user123', 3], |
|
91 | - ]; |
|
92 | - $this->manager->expects($this->exactly(3)) |
|
93 | - ->method('invalidateTokenById') |
|
94 | - ->willReturnCallback(function () use (&$calls) { |
|
95 | - $expected = array_shift($calls); |
|
96 | - $this->assertEquals($expected, func_get_args()); |
|
97 | - }); |
|
98 | - $this->logger->expects($this->never()) |
|
99 | - ->method('error'); |
|
100 | - |
|
101 | - $this->listener->handle($event); |
|
102 | - } |
|
24 | + /** @var Manager|MockObject */ |
|
25 | + private $manager; |
|
26 | + |
|
27 | + /** @var LoggerInterface|MockObject */ |
|
28 | + private $logger; |
|
29 | + |
|
30 | + /** @var UserDeletedTokenCleanupListener */ |
|
31 | + private $listener; |
|
32 | + |
|
33 | + protected function setUp(): void { |
|
34 | + parent::setUp(); |
|
35 | + |
|
36 | + $this->manager = $this->createMock(Manager::class); |
|
37 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
38 | + |
|
39 | + $this->listener = new UserDeletedTokenCleanupListener( |
|
40 | + $this->manager, |
|
41 | + $this->logger |
|
42 | + ); |
|
43 | + } |
|
44 | + |
|
45 | + public function testHandleUnrelated(): void { |
|
46 | + $event = new Event(); |
|
47 | + $this->manager->expects($this->never())->method('getTokenByUser'); |
|
48 | + $this->logger->expects($this->never())->method('error'); |
|
49 | + |
|
50 | + $this->listener->handle($event); |
|
51 | + } |
|
52 | + |
|
53 | + public function testHandleWithErrors(): void { |
|
54 | + $user = $this->createMock(IUser::class); |
|
55 | + $user->method('getUID')->willReturn('user123'); |
|
56 | + $event = new UserDeletedEvent($user); |
|
57 | + $exception = new Exception('nope'); |
|
58 | + $this->manager->expects($this->once()) |
|
59 | + ->method('getTokenByUser') |
|
60 | + ->with('user123') |
|
61 | + ->willThrowException($exception); |
|
62 | + $this->logger->expects($this->once()) |
|
63 | + ->method('error'); |
|
64 | + |
|
65 | + $this->listener->handle($event); |
|
66 | + } |
|
67 | + |
|
68 | + public function testHandle(): void { |
|
69 | + $user = $this->createMock(IUser::class); |
|
70 | + $user->method('getUID')->willReturn('user123'); |
|
71 | + $event = new UserDeletedEvent($user); |
|
72 | + $token1 = $this->createMock(IToken::class); |
|
73 | + $token1->method('getId')->willReturn(1); |
|
74 | + $token2 = $this->createMock(IToken::class); |
|
75 | + $token2->method('getId')->willReturn(2); |
|
76 | + $token3 = $this->createMock(IToken::class); |
|
77 | + $token3->method('getId')->willReturn(3); |
|
78 | + $this->manager->expects($this->once()) |
|
79 | + ->method('getTokenByUser') |
|
80 | + ->with('user123') |
|
81 | + ->willReturn([ |
|
82 | + $token1, |
|
83 | + $token2, |
|
84 | + $token3, |
|
85 | + ]); |
|
86 | + |
|
87 | + $calls = [ |
|
88 | + ['user123', 1], |
|
89 | + ['user123', 2], |
|
90 | + ['user123', 3], |
|
91 | + ]; |
|
92 | + $this->manager->expects($this->exactly(3)) |
|
93 | + ->method('invalidateTokenById') |
|
94 | + ->willReturnCallback(function () use (&$calls) { |
|
95 | + $expected = array_shift($calls); |
|
96 | + $this->assertEquals($expected, func_get_args()); |
|
97 | + }); |
|
98 | + $this->logger->expects($this->never()) |
|
99 | + ->method('error'); |
|
100 | + |
|
101 | + $this->listener->handle($event); |
|
102 | + } |
|
103 | 103 | } |
@@ -91,7 +91,7 @@ |
||
91 | 91 | ]; |
92 | 92 | $this->manager->expects($this->exactly(3)) |
93 | 93 | ->method('invalidateTokenById') |
94 | - ->willReturnCallback(function () use (&$calls) { |
|
94 | + ->willReturnCallback(function() use (&$calls) { |
|
95 | 95 | $expected = array_shift($calls); |
96 | 96 | $this->assertEquals($expected, func_get_args()); |
97 | 97 | }); |
@@ -15,58 +15,58 @@ |
||
15 | 15 | use PHPUnit\Framework\MockObject\MockObject; |
16 | 16 | |
17 | 17 | class SetUserTimezoneCommandTest extends ALoginTestCommand { |
18 | - /** @var IConfig|MockObject */ |
|
19 | - private $config; |
|
18 | + /** @var IConfig|MockObject */ |
|
19 | + private $config; |
|
20 | 20 | |
21 | - /** @var ISession|MockObject */ |
|
22 | - private $session; |
|
21 | + /** @var ISession|MockObject */ |
|
22 | + private $session; |
|
23 | 23 | |
24 | - protected function setUp(): void { |
|
25 | - parent::setUp(); |
|
24 | + protected function setUp(): void { |
|
25 | + parent::setUp(); |
|
26 | 26 | |
27 | - $this->config = $this->createMock(IConfig::class); |
|
28 | - $this->session = $this->createMock(ISession::class); |
|
27 | + $this->config = $this->createMock(IConfig::class); |
|
28 | + $this->session = $this->createMock(ISession::class); |
|
29 | 29 | |
30 | - $this->cmd = new SetUserTimezoneCommand( |
|
31 | - $this->config, |
|
32 | - $this->session |
|
33 | - ); |
|
34 | - } |
|
30 | + $this->cmd = new SetUserTimezoneCommand( |
|
31 | + $this->config, |
|
32 | + $this->session |
|
33 | + ); |
|
34 | + } |
|
35 | 35 | |
36 | - public function testProcessNoTimezoneSet(): void { |
|
37 | - $data = $this->getLoggedInLoginData(); |
|
38 | - $this->config->expects($this->never()) |
|
39 | - ->method('setUserValue'); |
|
40 | - $this->session->expects($this->never()) |
|
41 | - ->method('set'); |
|
36 | + public function testProcessNoTimezoneSet(): void { |
|
37 | + $data = $this->getLoggedInLoginData(); |
|
38 | + $this->config->expects($this->never()) |
|
39 | + ->method('setUserValue'); |
|
40 | + $this->session->expects($this->never()) |
|
41 | + ->method('set'); |
|
42 | 42 | |
43 | - $result = $this->cmd->process($data); |
|
43 | + $result = $this->cmd->process($data); |
|
44 | 44 | |
45 | - $this->assertTrue($result->isSuccess()); |
|
46 | - } |
|
45 | + $this->assertTrue($result->isSuccess()); |
|
46 | + } |
|
47 | 47 | |
48 | - public function testProcess(): void { |
|
49 | - $data = $this->getLoggedInLoginDataWithTimezone(); |
|
50 | - $this->user->expects($this->once()) |
|
51 | - ->method('getUID') |
|
52 | - ->willReturn($this->username); |
|
53 | - $this->config->expects($this->once()) |
|
54 | - ->method('setUserValue') |
|
55 | - ->with( |
|
56 | - $this->username, |
|
57 | - 'core', |
|
58 | - 'timezone', |
|
59 | - $this->timezone |
|
60 | - ); |
|
61 | - $this->session->expects($this->once()) |
|
62 | - ->method('set') |
|
63 | - ->with( |
|
64 | - 'timezone', |
|
65 | - $this->timeZoneOffset |
|
66 | - ); |
|
48 | + public function testProcess(): void { |
|
49 | + $data = $this->getLoggedInLoginDataWithTimezone(); |
|
50 | + $this->user->expects($this->once()) |
|
51 | + ->method('getUID') |
|
52 | + ->willReturn($this->username); |
|
53 | + $this->config->expects($this->once()) |
|
54 | + ->method('setUserValue') |
|
55 | + ->with( |
|
56 | + $this->username, |
|
57 | + 'core', |
|
58 | + 'timezone', |
|
59 | + $this->timezone |
|
60 | + ); |
|
61 | + $this->session->expects($this->once()) |
|
62 | + ->method('set') |
|
63 | + ->with( |
|
64 | + 'timezone', |
|
65 | + $this->timeZoneOffset |
|
66 | + ); |
|
67 | 67 | |
68 | - $result = $this->cmd->process($data); |
|
68 | + $result = $this->cmd->process($data); |
|
69 | 69 | |
70 | - $this->assertTrue($result->isSuccess()); |
|
71 | - } |
|
70 | + $this->assertTrue($result->isSuccess()); |
|
71 | + } |
|
72 | 72 | } |