ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilDAVContainerTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 use Sabre\DAV\INode;
32 
33 require_once "./components/ILIAS/WebDAV/tests/webdav_overrides.php";
34 require_once "./components/ILIAS/WebDAV/tests/ilWebDAVTestHelper.php";
35 
37 {
39 
40  protected ?Container $dic = null;
41 
42  protected function setUp(): void
43  {
45  global $DIC;
46 
47  $this->dic = is_object($DIC) ? clone $DIC : $DIC;
48 
49  $DIC = new Container();
50  $DIC['tree'] = $this->createMock(ilTree::class);
51  $DIC['lng'] = $this->createMock(ilLanguage::class);
52  $DIC['http'] = $this->createMock(Services::class);
53  $DIC['rbacsystem'] = $this->createMock(ilRbacSystem::class);
54  $DIC['rbacreview'] = $this->createMock(ilRbacReview::class);
55  $DIC['ilAccess'] = $this->createMock(ilAccess::class);
56  $DIC['ilDB'] = $this->createMock(ilDBInterface::class);
57  $DIC['ilLog'] = $this->createMock(ilLogger::class);
58  $DIC['ilErr'] = $this->createMock(ilErrorHandling::class);
59  $DIC['ilUser'] = $this->createMock(ilObjUser::class);
60  $DIC['objDefinition'] = $this->createMock(ilObjectDefinition::class);
61  $DIC['ilSetting'] = $this->createMock(ilSetting::class);
62  $DIC['ilias'] = $this->createMock(ILIAS::class);
63  $DIC['ilAppEventHandler'] = $this->createMock(ilAppEventHandler::class);
64  $DIC['filesystem'] = $this->createMock(Filesystems::class);
65  $DIC['upload'] = $this->createMock(FileUpload::class);
66  $DIC['resource_storage'] = $this->createMock(\ILIAS\ResourceStorage\Services::class);
67  $DIC['refinery'] = $this->createMock(Factory::class);
68  $DIC['object.customicons.factory'] = $this->createMock(ILIAS\ILIASObject\Properties\AdditionalProperties\Icon\Factory::class);
69  $DIC['learning_object_metadata'] = $this->createMock(LOMServices::class);
70  $DIC['resource_storage'] = $this->createMock(ILIAS\ResourceStorage\Services::class);
71  }
72 
73  protected function tearDown(): void
74  {
75  global $DIC;
76 
77  $DIC = $this->dic;
78 
79  parent::tearDown();
80  }
81 
82  public function testGetNameGetsObjectTitle(): void
83  {
84  $object = $this->createMock(ilObjFolder::class);
85  $object->expects($this->once())->method('getTitle')->willReturn('Some random Title');
86 
87  $user = $this->createStub(ilObjUser::class);
88  $request = $this->createStub(RequestInterface::class);
89  $dav_factory = $this->createStub(ilWebDAVObjFactory::class);
90  $repository_helper = $this->createStub(ilWebDAVRepositoryHelper::class);
91 
92  $dav_container = new ilDAVContainer($object, $user, $request, $dav_factory, $repository_helper);
93 
94  $this->assertEquals('Some random Title', $dav_container->getName());
95  }
96 
98  {
99  $ref_ids = [
100  '7' => [
101  'name' => 'Second Fourth Child',
102  'class' => ilDAVContainer::class,
103  'expects_objects' => 7
104  ],
105  '23356343' => [
106  'name' => 'Last Last Child',
107  'class' => ilDAVFile::class,
108  'expects_objects' => 4
109  ]
110  ];
111 
112  foreach ($ref_ids as $ref_id => $additional_information) {
113  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
114  (int) $ref_id,
115  $additional_information['expects_objects']
116  );
117  $object = $dav_container->getChild($additional_information['name']);
118  $this->assertInstanceOf($additional_information['class'], $object);
119  $this->assertEquals($additional_information['name'], $object->getName());
120  }
121  }
122 
124  {
125  $ref_id = '22';
126  $webdav_test_helper = new ilWebDAVTestHelper();
127  $tree = $webdav_test_helper->getTree();
128 
129  foreach ($tree[$ref_id]['children'] as $child_ref) {
130  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
131  (int) $ref_id,
132  11
133  );
134  try {
135  $dav_container->getChild($tree[$child_ref]['title']);
136  $this->assertFalse('This should never happen');
137  } catch (NotFound $e) {
138  $this->assertEquals($tree[$child_ref]['title'] . ' not found', $e->getMessage());
139  ;
140  }
141  }
142  }
143 
145  {
146  $ref_ids = [
147  '7' => [
148  'name' => 'Second Third Child',
149  'expects_objects' => 7
150  ],
151  '23356343' => [
152  'name' => 'Last Third Child',
153  'expects_objects' => 4
154  ]
155  ];
156 
157  foreach ($ref_ids as $ref_id => $additional_information) {
158  try {
159  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
160  (int) $ref_id,
161  $additional_information['expects_objects']
162  );
163  $dav_container->getChild($additional_information['name']);
164  $this->assertFalse('This should not happen');
165  } catch (NotFound $e) {
166  $this->assertEquals($additional_information['name'] . ' not found', $e->getMessage());
167  }
168  }
169  }
170 
172  {
173  $ref_id = 7;
174  $name = 'None existent name';
175 
176  try {
177  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
178  $ref_id,
179  7
180  );
181  $dav_container->getChild($name);
182  $this->assertFalse('This should not happen');
183  } catch (NotFound $e) {
184  $this->assertEquals("$name not found", $e->getMessage());
185  }
186  }
187 
189  {
190  $ref_ids = [
191  '7' => [
192  'name' => 'Second Last Child',
193  'expects_objects' => 7
194  ],
195  '23356343' => [
196  'name' => 'Last Second Child',
197  'expects_objects' => 4
198  ]
199  ];
200 
201  foreach ($ref_ids as $ref_id => $additional_information) {
202  try {
203  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
204  (int) $ref_id,
205  $additional_information['expects_objects']
206  );
207  $dav_container->getChild($additional_information['name']);
208  $this->assertFalse('This should not happen');
209  } catch (NotFound $e) {
210  $this->assertEquals($additional_information['name'] . ' not found', $e->getMessage());
211  }
212  }
213  }
214 
216  {
217  $ref_id = 23356343;
218  $additional_information = [
219  'names' => ['Last First Child', 'Last Last Child'],
220  'classes' => [ilDAVContainer::class, ilDAVFile::class],
221  'ref_id' => ['233563432', '2335634323356343'],
222  'expects_objects' => 4,
223  'expects_problem_info_file' => 0
224  ];
225 
226  $this->getChildrenTest($ref_id, $additional_information);
227  }
228 
230  {
231  $ref_id = 7;
232  $additional_information = [
233  'names' => ['Second First Child', 'Second Second Child', 'Second Fourth Child', 'Problem Info File'],
234  'classes' => [ilDAVFile::class, ilDAVFile::class, ilDAVContainer::class, ilDAVProblemInfoFile::class],
235  'ref_id' => ['72', '78', '7221', '7222'],
236  'expects_objects' => 7,
237  'expects_problem_info_file' => 1,
238  ];
239 
240  $this->getChildrenTest($ref_id, $additional_information);
241  }
242 
246  protected function getChildrenTest(int $ref_id, array $additional_information): void
247  {
248  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
249  $ref_id,
250  $additional_information['expects_objects'],
251  $additional_information['expects_problem_info_file']
252  );
253  $children = $dav_container->getChildren();
254  $this->assertEquals(count($additional_information['names']), count($children));
255  for ($i = 0, $i_max = count($children); $i < $i_max; $i++) {
256  $this->assertInstanceOf(
257  $additional_information['classes'][$i],
258  $children[$additional_information['ref_id'][$i]]
259  );
260  $this->assertEquals(
261  $additional_information['names'][$i],
262  $children[$additional_information['ref_id'][$i]]->getName()
263  );
264  }
265  }
266 
268  {
269  $ref_id = 22;
270 
271  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
272  $ref_id,
273  11,
274  1
275  );
276  $children = $dav_container->getChildren();
277  $this->assertEquals(1, count($children));
278  }
279 
281  {
282  $ref_ids = [
283  '7' => [
284  'name' => 'Second Fourth Child',
285  'expects_objects' => 6
286  ],
287  '23356343' => [
288  'name' => 'Last Last Child',
289  'expects_objects' => 4
290  ]
291  ];
292 
293  foreach ($ref_ids as $ref_id => $additional_information) {
294  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
295  (int) $ref_id,
296  $additional_information['expects_objects']
297  );
298  $this->assertTrue($dav_container->childExists($additional_information['name']));
299  }
300  }
301 
303  {
304  $ref_id = 7;
305  $additional_information = [
306  'name' => 'Second Second Child',
307  'expects_objects' => 4
308  ];
309 
310  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
311  $ref_id,
312  $additional_information['expects_objects']
313  );
314  $this->assertTrue($dav_container->childExists($additional_information['name']));
315  }
316 
318  {
319  $ref_id = '22';
320  $webdav_test_helper = new ilWebDAVTestHelper();
321  $tree = $webdav_test_helper->getTree();
322 
323  foreach ($tree[$ref_id]['children'] as $child_ref) {
324  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
325  (int) $ref_id,
326  11
327  );
328  $this->assertFalse($dav_container->childExists($tree[$child_ref]['title']));
329  }
330  }
331 
333  {
334  $ref_ids = [
335  '7' => [
336  'name' => 'Second Third Child',
337  'expects_objects' => 7
338  ],
339  '23356343' => [
340  'name' => 'Last Third Child',
341  'expects_objects' => 4
342  ]
343  ];
344 
345  foreach ($ref_ids as $ref_id => $additional_information) {
346  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
347  (int) $ref_id,
348  $additional_information['expects_objects']
349  );
350  $this->assertFalse($dav_container->childExists($additional_information['name']));
351  }
352  }
353 
355  {
356  $ref_id = 7;
357  $name = 'None existent name';
358 
359  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
360  $ref_id,
361  7
362  );
363 
364  $this->assertFalse($dav_container->childExists($name));
365  }
366 
368  {
369  $ref_ids = [
370  '7' => [
371  'name' => 'Second Last Child',
372  'expects_objects' => 7
373  ],
374  '23356343' => [
375  'name' => 'Last Second Child',
376  'expects_objects' => 4
377  ]
378  ];
379 
380  foreach ($ref_ids as $ref_id => $additional_information) {
381  $dav_container = $this->getDAVContainerWithExpectationForFunctions(
382  (int) $ref_id,
383  $additional_information['expects_objects']
384  );
385 
386  $this->assertFalse($dav_container->childExists($additional_information['name']));
387  }
388  }
389 
391  {
392  $parent_ref = 233563432;
393 
394  $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0);
395 
396  try {
397  $dav_container->setName('My Valid Name');
398  $this->assertFalse('This should not happen!');
399  } catch (Forbidden $e) {
400  $this->assertEquals('Permission denied', $e->getMessage());
401  }
402  }
403 
405  {
406  $ref_id = 7221;
407  $webdav_test_helper = new ilWebDAVTestHelper();
408  $tree = $webdav_test_helper->getTree();
409 
410  foreach ($tree['22']['children'] as $invalid_object) {
411  $dav_container = $this->getDAVContainerWithExpectationForFunctions($ref_id, 0, 0, 0);
412 
413  try {
414  $dav_container->setName($tree[$invalid_object]['title']);
415  $this->assertFalse('This should not happen!');
416  } catch (Forbidden $e) {
417  $this->assertEquals('Forbidden characters in title', $e->getMessage());
418  }
419  }
420  }
421 
423  {
424  $parent_ref = 233563432;
425 
426  $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0, false);
427 
428  try {
429  $dav_container->createFile('My New File.txt');
430  $this->assertFalse('This should not happen!');
431  } catch (Forbidden $e) {
432  $this->assertEquals('Permission denied', $e->getMessage());
433  }
434  }
435 
437  {
438  $parent_ref = 233563432;
439 
440  $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0, true);
441 
442  try {
443  $dav_container->createDirectory('My New Folder');
444  $this->assertFalse('This should not happen!');
445  } catch (Forbidden $e) {
446  $this->assertEquals('Permission denied', $e->getMessage());
447  }
448  }
449 
450  #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
451  #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
453  {
454  define('ILIAS_LOG_ENABLED', false);
455  $ref_id = 7221;
456  $webdav_test_helper = new ilWebDAVTestHelper();
457  $tree = $webdav_test_helper->getTree();
458 
459  foreach ($tree['22']['children'] as $invalid_object) {
460  $dav_container = $this->getDAVContainerWithExpectationForFunctions($ref_id, 0, 0, 0, true);
461 
462  try {
463  $dav_container->createDirectory($tree[$invalid_object]['title']);
464  $this->assertFalse('This should not happen!');
465  } catch (Forbidden $e) {
466  $this->assertEquals('Forbidden characters in title', $e->getMessage());
467  }
468  }
469  }
470 
472  {
473  $parent_ref = 233563432;
474 
475  $dav_container = $this->getDAVContainerWithExpectationForFunctions($parent_ref, 0, 0, 0);
476 
477  try {
478  $dav_container->delete();
479  $this->assertFalse('This should not happen!');
480  } catch (Forbidden $e) {
481  $this->assertEquals('Permission denied', $e->getMessage());
482  }
483  }
484 
486  int $object_ref_id,
487  int $expects_object,
488  int $expects_problem_info_file = 0,
489  int $expects_child_ref = 1,
490  bool $for_create = false
491  ): ilDAVContainer {
492  $object_folder = new ilObjFolder();
493  $object_folder->setRefId($object_ref_id);
494  $user = $this->createStub(ilObjUser::class);
495  $request = $this->createStub(RequestInterface::class);
496 
497  $webdav_test_helper = new ilWebDAVTestHelper();
498  $tree = $webdav_test_helper->getTree();
499 
500  $mocked_dav_factory = $this->createPartialMock(
501  ilWebDAVObjFactory::class,
502  ['retrieveDAVObjectByRefID', 'getProblemInfoFile']
503  );
504  $mocked_dav_factory->expects($this->exactly($expects_object))
505  ->method('retrieveDAVObjectByRefID')->willReturnCallback(
506  function (int $ref_id) use ($tree): INode {
507  if ($tree[$ref_id]['access'] === 'none') {
508  throw new Forbidden("No read permission for object with reference ID $ref_id");
509  }
510 
511  if ($tree[$ref_id]['type'] === 'fold') {
512  $obj_class = ilDAVContainer::class;
513  } elseif ($tree[$ref_id]['type'] === 'file') {
514  $obj_class = ilDAVFile::class;
515  } else {
517  }
518 
519  if ($this->hasTitleForbiddenChars($tree[$ref_id]['title'])) {
521  }
522 
523  if ($this->isHiddenFile($tree[$ref_id]['title'])) {
525  }
526 
527  $object = $this->createMock($obj_class);
528  $object->expects($this->atMost(3))->method('getName')->willReturn($tree[$ref_id]['title']);
529  return $object;
530  }
531  );
532  $mocked_dav_factory->expects($this->exactly($expects_problem_info_file))
533  ->method('getProblemInfoFile')->willReturnCallback(
534  function (int $ref_id): ilDAVProblemInfoFile {
535  $problem_info_file = $this->createMock(ilDAVProblemInfoFile::class);
536  $problem_info_file->expects($this->atMost(2))->method('getName')->willReturn('Problem Info File');
537  return $problem_info_file;
538  }
539  );
540 
541  $mocked_repo_helper = $this->createPartialMock(
542  ilWebDAVRepositoryHelper::class,
543  ['getChildrenOfRefId', 'checkcreateAccessForType', 'checkAccess']
544  );
545  $mocked_repo_helper->expects($this->exactly($expects_child_ref))
546  ->method('getChildrenOfRefId')->willReturnCallback(
547  fn(int $parent_ref): array => $tree[$parent_ref]['children']
548  );
549  $mocked_repo_helper->expects($this->atMost(1))
550  ->method('checkcreateAccessForType')->willReturnCallback(
551  fn($parent_ref, $type): bool => $tree[$parent_ref]['access'] === 'write'
552  );
553  $mocked_repo_helper->expects($this->atMost(1))
554  ->method('checkAccess')->willReturnCallback(
555  fn(string $permission, int $ref_id): bool => in_array(
556  $permission,
557  ['write', 'delete']
558  ) && $tree[$ref_id]['access'] === 'write'
559  );
560 
561  if ($for_create) {
562  $object_child = new ilObjFolder();
563  $object_child->setType('fold');
565  $object_folder,
566  $user,
567  $request,
568  $mocked_dav_factory,
569  $mocked_repo_helper
570  );
571  $dav_container->setChildcollection($object_child);
572  return $dav_container;
573  }
574 
575  return new ilDAVContainer($object_folder, $user, $request, $mocked_dav_factory, $mocked_repo_helper);
576  }
577 }
testGetChilrendWithExistingNameOfFolderOrFileReturnsArrayOfObjects()
Class ilObjFolder.
testChildExistsWithExistingNonDavableNameReturnsFalse()
getChildrenTest(int $ref_id, array $additional_information)
Interface Observer Contains several chained tasks and infos about them.
testChildExistsWithExistingNameOfFolderOrFileReturnsTrue()
testGetChildWithExistingNameOfFolderOrFileReturnsIlObject()
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
testChildExistsWithExistingNameOfFolderOrFileWithoutAccessReturnsFalse()
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testGetChildWithExistingNonDavableNameThrowsNotFoundError()
$ref_id
Definition: ltiauth.php:65
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getDAVContainerWithExpectationForFunctions(int $object_ref_id, int $expects_object, int $expects_problem_info_file=0, int $expects_child_ref=1, bool $for_create=false)
testChildExistsWithExistingNameOfFolderOrFileWhenOtherObjectOfSameNameExistsReturnsTrue()
testGetChildWithExistingNameOfOtherObjectTypeThrowsNotFoundError()
testGetChildWithNonExistentNameOfFolderOrFileThrowsNotFoundError()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testGetChildWithExistingNameOfFolderOrFileWithoutAccessThrowsNotFoundError()
global $DIC
Definition: shib_login.php:26
testSetNameWithNonDavableNameThrowsForbiddenError()
testCreateFileWithoutPermissionsThrowsForbiddenError()
testCreateDirectoryWithoutPermissionsThrowsForbiddenError()
testChildExistsWithNonExistentNameOfFolderOrFileReturnsFalse()
testCreateDirectoryWithNonDavableNameThrowsForbiddenError()
testChildExistsWithExistingNameOfOtherObjectTypeReturnsFalse()
testGetChildrenFromFolderWithOnlyNonDavableNamedContentReturnsEmptyArray()
testSetNameWithoutPermissionsThrowsForbiddenError()
testDeleteWithoutPermissionsThrowsForbiddenError()
testGetChilrendWithExistingNameOfFolderOrFileReturnsArrayWithProblemInfoFile()