ILIAS  release_8 Revision v8.24
class.ilDAVContainerTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use PHPUnit\Framework\TestCase;
22use Sabre\DAV\INode;
23use Sabre\DAV\Exception\NotFound;
24use Sabre\DAV\Exception\Forbidden;
25
26require_once "./Services/WebDAV/test/webdav_overrides.php";
27
28class ilDAVContainerTest extends TestCase
29{
30 use ilWebDAVCheckValidTitleTrait;
31
32 public function testGetNameGetsObjectTitle(): void
33 {
34 $object = $this->createMock(ilObjFolder::class);
35 $object->expects($this->once())->method('getTitle')->willReturn('Some random Title');
36
37 $user = $this->createStub(ilObjUser::class);
38 $request = $this->createStub('Psr\Http\Message\RequestInterface');
39 $dav_factory = $this->createStub(ilWebDAVObjFactory::class);
40 $repository_helper = $this->createStub(ilWebDAVRepositoryHelper::class);
41
42 $dav_container = new ilDAVContainer($object, $user, $request, $dav_factory, $repository_helper);
43
44 $this->assertEquals('Some random Title', $dav_container->getName());
45 }
46
48 {
49 $ref_ids = [
50 '7' => [
51 'name' => 'Second Fourth Child',
52 'class' => ilDAVContainer::class,
53 'expects_objects' => 7
54 ],
55 '23356343' => [
56 'name' => 'Last Last Child',
57 'class' => ilDAVFile::class,
58 'expects_objects' => 4
59 ]
60 ];
61
62 foreach ($ref_ids as $ref_id => $additional_information) {
63 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
64 (int) $ref_id,
65 $additional_information['expects_objects']
66 );
67 $object = $dav_container->getChild($additional_information['name']);
68 $this->assertInstanceOf($additional_information['class'], $object);
69 $this->assertEquals($additional_information['name'], $object->getName());
70 }
71 }
72
74 {
75 $ref_id = '22';
76 $webdav_test_helper = new ilWebDAVTestHelper();
77 $tree = $webdav_test_helper->getTree();
78
79 foreach ($tree[$ref_id]['children'] as $child_ref) {
80 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
81 (int) $ref_id,
82 11
83 );
84 try {
85 $dav_container->getChild($tree[$child_ref]['title']);
86 $this->assertFalse('This should never happen');
87 } catch (NotFound $e) {
88 $this->assertEquals($tree[$child_ref]['title'] . ' not found', $e->getMessage());
89 ;
90 }
91 }
92 }
93
95 {
96 $ref_ids = [
97 '7' => [
98 'name' => 'Second Third Child',
99 'expects_objects' => 7
100 ],
101 '23356343' => [
102 'name' => 'Last Third Child',
103 'expects_objects' => 4
104 ]
105 ];
106
107 foreach ($ref_ids as $ref_id => $additional_information) {
108 try {
109 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
110 (int) $ref_id,
111 $additional_information['expects_objects']
112 );
113 $dav_container->getChild($additional_information['name']);
114 $this->assertFalse('This should not happen');
115 } catch (NotFound $e) {
116 $this->assertEquals($additional_information['name'] . ' not found', $e->getMessage());
117 }
118 }
119 }
120
122 {
123 $ref_id = 7;
124 $name = 'None existent name';
125
126 try {
127 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
128 $ref_id,
129 7
130 );
131 $dav_container->getChild($name);
132 $this->assertFalse('This should not happen');
133 } catch (NotFound $e) {
134 $this->assertEquals("$name not found", $e->getMessage());
135 }
136 }
137
139 {
140 $ref_ids = [
141 '7' => [
142 'name' => 'Second Last Child',
143 'expects_objects' => 7
144 ],
145 '23356343' => [
146 'name' => 'Last Second Child',
147 'expects_objects' => 4
148 ]
149 ];
150
151 foreach ($ref_ids as $ref_id => $additional_information) {
152 try {
153 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
154 (int) $ref_id,
155 $additional_information['expects_objects']
156 );
157 $dav_container->getChild($additional_information['name']);
158 $this->assertFalse('This should not happen');
159 } catch (NotFound $e) {
160 $this->assertEquals($additional_information['name'] . ' not found', $e->getMessage());
161 }
162 }
163 }
164
166 {
167 $ref_id = 23356343;
168 $additional_information = [
169 'names' => ['Last First Child', 'Last Last Child'],
170 'classes' => [ilDAVContainer::class, ilDAVFile::class],
171 'ref_id' => ['233563432', '2335634323356343'],
172 'expects_objects' => 4,
173 'expects_problem_info_file' => 0
174 ];
175
176 $this->getChildrenTest($ref_id, $additional_information);
177 }
178
180 {
181 $ref_id = 7;
182 $additional_information = [
183 'names' => ['Second First Child', 'Second Second Child', 'Second Fourth Child', 'Problem Info File'],
184 'classes' => [ilDAVFile::class, ilDAVFile::class, ilDAVContainer::class, ilDAVProblemInfoFile::class],
185 'ref_id' => ['72', '78', '7221', '7222'],
186 'expects_objects' => 7,
187 'expects_problem_info_file' => 1,
188 ];
189
190 $this->getChildrenTest($ref_id, $additional_information);
191 }
192
196 protected function getChildrenTest(int $ref_id, array $additional_information): void
197 {
198 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
199 $ref_id,
200 $additional_information['expects_objects'],
201 $additional_information['expects_problem_info_file']
202 );
203 $children = $dav_container->getChildren();
204 $this->assertEquals(count($additional_information['names']), count($children));
205 for ($i = 0; $i < count($children); $i++) {
206 $this->assertInstanceOf($additional_information['classes'][$i], $children[$additional_information['ref_id'][$i]]);
207 $this->assertEquals($additional_information['names'][$i], $children[$additional_information['ref_id'][$i]]->getName());
208 }
209 }
210
212 {
213 $ref_id = 22;
214
215 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
216 $ref_id,
217 11,
218 1
219 );
220 $children = $dav_container->getChildren();
221 $this->assertEquals(1, count($children));
222 }
223
225 {
226 $ref_ids = [
227 '7' => [
228 'name' => 'Second Fourth Child',
229 'expects_objects' => 6
230 ],
231 '23356343' => [
232 'name' => 'Last Last Child',
233 'expects_objects' => 4
234 ]
235 ];
236
237 foreach ($ref_ids as $ref_id => $additional_information) {
238 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
239 (int) $ref_id,
240 $additional_information['expects_objects']
241 );
242 $this->assertTrue($dav_container->childExists($additional_information['name']));
243 }
244 }
245
247 {
248 $ref_id = 7;
249 $additional_information = [
250 'name' => 'Second Second Child',
251 'expects_objects' => 4
252 ];
253
254 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
255 $ref_id,
256 $additional_information['expects_objects']
257 );
258 $this->assertTrue($dav_container->childExists($additional_information['name']));
259 }
260
262 {
263 $ref_id = '22';
264 $webdav_test_helper = new ilWebDAVTestHelper();
265 $tree = $webdav_test_helper->getTree();
266
267 foreach ($tree[$ref_id]['children'] as $child_ref) {
268 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
269 (int) $ref_id,
270 11
271 );
272 $this->assertFalse($dav_container->childExists($tree[$child_ref]['title']));
273 }
274 }
275
277 {
278 $ref_ids = [
279 '7' => [
280 'name' => 'Second Third Child',
281 'expects_objects' => 7
282 ],
283 '23356343' => [
284 'name' => 'Last Third Child',
285 'expects_objects' => 4
286 ]
287 ];
288
289 foreach ($ref_ids as $ref_id => $additional_information) {
290 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
291 (int) $ref_id,
292 $additional_information['expects_objects']
293 );
294 $this->assertFalse($dav_container->childExists($additional_information['name']));
295 }
296 }
297
299 {
300 $ref_id = 7;
301 $name = 'None existent name';
302
303 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
304 $ref_id,
305 7
306 );
307
308 $this->assertFalse($dav_container->childExists($name));
309 }
310
312 {
313 $ref_ids = [
314 '7' => [
315 'name' => 'Second Last Child',
316 'expects_objects' => 7
317 ],
318 '23356343' => [
319 'name' => 'Last Second Child',
320 'expects_objects' => 4
321 ]
322 ];
323
324 foreach ($ref_ids as $ref_id => $additional_information) {
325 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
326 (int) $ref_id,
327 $additional_information['expects_objects']
328 );
329
330 $this->assertFalse($dav_container->childExists($additional_information['name']));
331 }
332 }
333
335 {
336 $parent_ref = 233563432;
337
338 $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0);
339
340 try {
341 $dav_container->setName('My Valid Name');
342 $this->assertFalse('This should not happen!');
343 } catch (Forbidden $e) {
344 $this->assertEquals('Permission denied', $e->getMessage());
345 }
346 }
347
349 {
350 $ref_id = 7221;
351 $webdav_test_helper = new ilWebDAVTestHelper();
352 $tree = $webdav_test_helper->getTree();
353
354 foreach ($tree['22']['children'] as $invalid_object) {
355 $dav_container = $this->getDAVContainerWithExpectationForFunctions($ref_id, 0, 0, 0);
356
357 try {
358 $dav_container->setName($tree[$invalid_object]['title']);
359 $this->assertFalse('This should not happen!');
360 } catch (Forbidden $e) {
361 $this->assertEquals('Forbidden characters in title', $e->getMessage());
362 }
363 }
364 }
365
367 {
368 $parent_ref = 233563432;
369
370 $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0, false);
371
372 try {
373 $dav_container->createFile('My New File.txt');
374 $this->assertFalse('This should not happen!');
375 } catch (Forbidden $e) {
376 $this->assertEquals('Permission denied', $e->getMessage());
377 }
378 }
379
381 {
382 $parent_ref = 233563432;
383
384 $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0, true);
385
386 try {
387 $dav_container->createDirectory('My New Folder');
388 $this->assertFalse('This should not happen!');
389 } catch (Forbidden $e) {
390 $this->assertEquals('Permission denied', $e->getMessage());
391 }
392 }
393
395 {
396 $ref_id = 7221;
397 $webdav_test_helper = new ilWebDAVTestHelper();
398 $tree = $webdav_test_helper->getTree();
399
400 foreach ($tree['22']['children'] as $invalid_object) {
401 $dav_container = $this->getDAVContainerWithExpectationForFunctions($ref_id, 0, 0, 0, true);
402
403 try {
404 $dav_container->createDirectory($tree[$invalid_object]['title']);
405 $this->assertFalse('This should not happen!');
406 } catch (Forbidden $e) {
407 $this->assertEquals('Forbidden characters in title', $e->getMessage());
408 }
409 }
410 }
411
413 {
414 $parent_ref = 233563432;
415
416 $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0);
417
418 try {
419 $dav_container->delete();
420 $this->assertFalse('This should not happen!');
421 } catch (Forbidden $e) {
422 $this->assertEquals('Permission denied', $e->getMessage());
423 }
424 }
425
427 int $object_ref_id,
428 int $expects_object,
429 int $expects_problem_info_file = 0,
430 int $expects_child_ref = 1,
431 bool $for_create = false
432 ): ilDAVContainer {
433 $object_folder = $this->createPartialMock(ilObjFolder::class, []);
434 $object_folder->setRefId($object_ref_id);
435 $user = $this->createStub(ilObjUser::class);
436 $request = $this->createStub('Psr\Http\Message\RequestInterface');
437
438 $webdav_test_helper = new ilWebDAVTestHelper();
439 $tree = $webdav_test_helper->getTree();
440
441 $mocked_dav_factory = $this->createPartialMock(ilWebDAVObjFactory::class, ['retrieveDAVObjectByRefID', 'getProblemInfoFile']);
442 $mocked_dav_factory->expects($this->exactly($expects_object))
443 ->method('retrieveDAVObjectByRefID')->willReturnCallback(
444 function (int $ref_id) use ($tree): INode {
445 if ($tree[$ref_id]['access'] === 'none') {
446 throw new Forbidden("No read permission for object with reference ID $ref_id");
447 }
448
449 if ($tree[$ref_id]['type'] === 'fold') {
450 $obj_class = ilDAVContainer::class;
451 } elseif ($tree[$ref_id]['type'] === 'file') {
452 $obj_class = ilDAVFile::class;
453 } else {
455 }
456
457 if ($this->hasTitleForbiddenChars($tree[$ref_id]['title'])) {
459 }
460
461 if ($this->isHiddenFile($tree[$ref_id]['title'])) {
463 }
464
465 $object = $this->createMock($obj_class);
466 $object->expects($this->atMost(3))->method('getName')->willReturn($tree[$ref_id]['title']);
467 return $object;
468 }
469 );
470 $mocked_dav_factory->expects($this->exactly($expects_problem_info_file))
471 ->method('getProblemInfoFile')->willReturnCallback(
472 function (int $ref_id): ilDAVProblemInfoFile {
473 $problem_info_file = $this->createMock(ilDAVProblemInfoFile::class);
474 $problem_info_file->expects($this->atMost(2))->method('getName')->willReturn('Problem Info File');
475 return $problem_info_file;
476 }
477 );
478
479 $mocked_repo_helper = $this->createPartialMock(ilWebDAVRepositoryHelper::class, ['getChildrenOfRefId', 'checkcreateAccessForType', 'checkAccess']);
480 $mocked_repo_helper->expects($this->exactly($expects_child_ref))
481 ->method('getChildrenOfRefId')->willReturnCallback(
482 function (int $parent_ref) use ($tree): array {
483 return $tree[$parent_ref]['children'];
484 }
485 );
486 $mocked_repo_helper->expects($this->atMost(1))
487 ->method('checkcreateAccessForType')->willReturnCallback(
488 function ($parent_ref, $type) use ($tree) {
489 if ($tree[$parent_ref]['access'] === 'write') {
490 return true;
491 }
492
493 return false;
494 }
495 );
496 $mocked_repo_helper->expects($this->atMost(1))
497 ->method('checkAccess')->willReturnCallback(
498 function (string $permission, int $ref_id) use ($tree) {
499 if (in_array($permission, ['write', 'delete']) && $tree[$ref_id]['access'] === 'write') {
500 return true;
501 }
502
503 return false;
504 }
505 );
506
507 if ($for_create) {
508 $object_child = $this->createPartialMock(ilObjFolder::class, []);
509 $object_child->setType('fold');
510 $dav_container = new ilDAVContainerWithOverridenGetChildCollection($object_folder, $user, $request, $mocked_dav_factory, $mocked_repo_helper);
511 $dav_container->setChildcollection($object_child);
512 return $dav_container;
513 }
514
515 return new ilDAVContainer($object_folder, $user, $request, $mocked_dav_factory, $mocked_repo_helper);
516 }
517}
getDAVContainerWithExpectationForFunctions(int $object_ref_id, int $expects_object, int $expects_problem_info_file=0, int $expects_child_ref=1, bool $for_create=false)
testGetChildWithExistingNameOfOtherObjectTypeThrowsNotFoundError()
testGetChilrendWithExistingNameOfFolderOrFileReturnsArrayWithProblemInfoFile()
testGetChildrenFromFolderWithOnlyNonDavableNamedContentReturnsEmptyArray()
testGetChilrendWithExistingNameOfFolderOrFileReturnsArrayOfObjects()
testGetChildWithExistingNameOfFolderOrFileWithoutAccessThrowsNotFoundError()
testGetChildWithNonExistentNameOfFolderOrFileThrowsNotFoundError()
testChildExistsWithExistingNameOfFolderOrFileWhenOtherObjectOfSameNameExistsReturnsTrue()
testChildExistsWithNonExistentNameOfFolderOrFileReturnsFalse()
testChildExistsWithExistingNameOfFolderOrFileWithoutAccessReturnsFalse()
getChildrenTest(int $ref_id, array $additional_information)
testChildExistsWithExistingNameOfOtherObjectTypeReturnsFalse()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:67
if($format !==null) $name
Definition: metadata.php:247
$i
Definition: metadata.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AsyncNode.php:21
$type