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