ILIAS  release_8 Revision v8.24
class.ilDAVClientNodeTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use PHPUnit\Framework\TestCase;
22use Sabre\DAV\Exception\Forbidden;
23
24require_once "./Services/WebDAV/test/ilWebDAVTestHelper.php";
25
26class ilDAVClientNodeTest extends TestCase
27{
28 public function testGetNameGetsObjectTitle(): void
29 {
30 $webdav_test_helper = new ilWebDAVTestHelper();
32
33 $this->assertEquals($webdav_test_helper->getClientId(), $dav_client->getName());
34 }
35
36 /*public function testGetChildWithWellformedSlugContainingRefIdReturnsCorrespondingObject() : void
37 {
38 $slug = 'ref_7';
39 }
40
41 public function testGetChildWithExistingNameOfFolderOrFileReturnsIlObject()
42 {
43 $ref_ids = [
44 '7' => [
45 'name' => 'Second Fourth Child',
46 'class' => ilDAVContainer::class,
47 'expects_objects' => 7
48 ],
49 '23356343' => [
50 'name' => 'Last Last Child',
51 'class' => ilDAVFile::class,
52 'expects_objects' => 4
53 ]
54 ];
55
56 foreach ($ref_ids as $ref_id => $additional_information) {
57 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
58 (int) $ref_id,
59 $additional_information['expects_objects']
60 );
61 $object = $dav_container->getChild($additional_information['name']);
62 $this->assertInstanceOf($additional_information['class'], $object);
63 $this->assertEquals($additional_information['name'], $object->getName());
64 }
65 }
66
67 public function testGetChildWithExistingNonDavableNameThrowsNotFoundError()
68 {
69 $ref_id = '22';
70 $webdav_test_helper = new ilWebDAVTestHelper();
71 $tree = $webdav_test_helper->getTree();
72
73 foreach ($tree[$ref_id]['children'] as $child_ref) {
74 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
75 (int) $ref_id,
76 11
77 );
78 try {
79 $dav_container->getChild($tree[$child_ref]['title']);
80 $this->assertFalse('This should never happen');
81 } catch (NotFound $e) {
82 $this->assertEquals($tree[$child_ref]['title'] . ' not found', $e->getMessage());
83 ;
84 }
85 }
86 }
87
88 public function testGetChildWithExistingNameOfFolderOrFileWithoutAccessThrowsNotFoundError() : void
89 {
90 $ref_ids = [
91 '7' => [
92 'name' => 'Second Third Child',
93 'expects_objects' => 7
94 ],
95 '23356343' => [
96 'name' => 'Last Third Child',
97 'expects_objects' => 4
98 ]
99 ];
100
101 foreach ($ref_ids as $ref_id => $additional_information) {
102 try {
103 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
104 (int) $ref_id,
105 $additional_information['expects_objects']
106 );
107 $dav_container->getChild($additional_information['name']);
108 $this->assertFalse('This should not happen');
109 } catch (NotFound $e) {
110 $this->assertEquals($additional_information['name'] . ' not found', $e->getMessage());
111 }
112 }
113 }
114
115 public function testGetChildWithNonExistentNameOfFolderOrFileThrowsNotFoundError() : void
116 {
117 $ref_id = 7;
118 $name = 'None existent name';
119
120 try {
121 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
122 (int) $ref_id,
123 7
124 );
125
126 $dav_container->getChild($name);
127 $this->assertFalse('This should not happen');
128 } catch (NotFound $e) {
129 $this->assertEquals("$name not found", $e->getMessage());
130 }
131 }
132
133 public function testGetChildWithExistingNameOfOtherObjectTypeThrowsNotFoundError() : void
134 {
135 $ref_ids = [
136 '7' => [
137 'name' => 'Second Last Child',
138 'expects_objects' => 7
139 ],
140 '23356343' => [
141 'name' => 'Last Second Child',
142 'expects_objects' => 4
143 ]
144 ];
145
146 foreach ($ref_ids as $ref_id => $additional_information) {
147 try {
148 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
149 (int) $ref_id,
150 $additional_information['expects_objects']
151 );
152
153 $dav_container->getChild($additional_information['name']);
154 $this->assertFalse('This should not happen');
155 } catch (NotFound $e) {
156 $this->assertEquals($additional_information['name'] . ' not found', $e->getMessage());
157 }
158 }
159 }
160
161 public function testGetChilrendWithExistingNameOfFolderOrFileReturnsArrayOfObjects() : void
162 {
163 $ref_id = 23356343;
164 $additional_information = [
165 'names' => ['Last First Child', 'Last Last Child'],
166 'classes' => [ilDAVContainer::class, ilDAVFile::class],
167 'ref_id' => ['233563432', '2335634323356343'],
168 'expects_objects' => 4,
169 'expects_problem_info_file' => 0
170 ];
171
172 $this->getChildrenTest($ref_id, $additional_information);
173 }
174
175 public function testGetChilrendWithExistingNameOfFolderOrFileReturnsArrayWithProblemInfoFile() : void
176 {
177 $ref_id = 7;
178 $additional_information = [
179 'names' => ['Second First Child', 'Second Second Child', 'Second Fourth Child', 'Problem Info File'],
180 'classes' => [ilDAVFile::class, ilDAVFile::class, ilDAVContainer::class, ilDAVProblemInfoFile::class],
181 'ref_id' => ['72', '78', '7221', '7222'],
182 'expects_objects' => 7,
183 'expects_problem_info_file' => 1,
184 ];
185
186 $this->getChildrenTest($ref_id, $additional_information);
187 }
188
189 protected function getChildrenTest(int $ref_id, array $additional_information) : void
190 {
191 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
192 $ref_id,
193 $additional_information['expects_objects'],
194 $additional_information['expects_problem_info_file']
195 );
196 $children = $dav_container->getChildren();
197 $this->assertEquals(count($additional_information['names']), count($children));
198 for ($i = 0; $i < count($children); $i++) {
199 $this->assertInstanceOf($additional_information['classes'][$i], $children[$additional_information['ref_id'][$i]]);
200 $this->assertEquals($additional_information['names'][$i], $children[$additional_information['ref_id'][$i]]->getName());
201 }
202 }
203
204 public function testGetChildrenFromFolderWithOnlyNonDavableNamedContentReturnsEmptyArray() : void
205 {
206 $ref_id = 22;
207
208 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
209 $ref_id,
210 11
211 );
212 $children = $dav_container->getChildren();
213 $this->assertEquals(0, count($children));
214 }
215
216 public function testChildExistsWithExistingNameOfFolderOrFileReturnsTrue() : void
217 {
218 $ref_ids = [
219 '7' => [
220 'name' => 'Second Fourth Child',
221 'expects_objects' => 6
222 ],
223 '23356343' => [
224 'name' => 'Last Last Child',
225 'expects_objects' => 4
226 ]
227 ];
228
229 foreach ($ref_ids as $ref_id => $additional_information) {
230 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
231 (int) $ref_id,
232 $additional_information['expects_objects']
233 );
234 $this->assertTrue($dav_container->childExists($additional_information['name']));
235 }
236 }
237
238 public function testChildExistsWithExistingNameOfFolderOrFileWhenOtherObjectOfSameNameExistsReturnsTrue() : void
239 {
240 $ref_id = 7;
241 $additional_information = [
242 'name' => 'Second Second Child',
243 'expects_objects' => 4
244 ];
245
246 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
247 $ref_id,
248 $additional_information['expects_objects']
249 );
250 $this->assertTrue($dav_container->childExists($additional_information['name']));
251 }
252
253 public function testChildExistsWithExistingNonDavableNameReturnsFalse() : void
254 {
255 $ref_id = '22';
256 $webdav_test_helper = new ilWebDAVTestHelper();
257 $tree = $webdav_test_helper->getTree();
258
259 foreach ($tree[$ref_id]['children'] as $child_ref) {
260 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
261 (int) $ref_id,
262 11
263 );
264 $this->assertFalse($dav_container->childExists($tree[$child_ref]['title']));
265 }
266 }
267
268 public function testChildExistsWithExistingNameOfFolderOrFileWithoutAccessReturnsFalse() : void
269 {
270 $ref_ids = [
271 '7' => [
272 'name' => 'Second Third Child',
273 'expects_objects' => 7
274 ],
275 '23356343' => [
276 'name' => 'Last Third Child',
277 'expects_objects' => 4
278 ]
279 ];
280
281 foreach ($ref_ids as $ref_id => $additional_information) {
282 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
283 (int) $ref_id,
284 $additional_information['expects_objects']
285 );
286 $this->assertFalse($dav_container->childExists($additional_information['name']));
287 }
288 }
289
290 public function testChildExistsWithNonExistentNameOfFolderOrFileReturnsFalse() : void
291 {
292 $ref_id = 7;
293 $name = 'None existent name';
294
295 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
296 (int) $ref_id,
297 7
298 );
299
300 $this->assertFalse($dav_container->childExists($name));
301 }
302
303 public function testChildExistsWithExistingNameOfOtherObjectTypeReturnsFalse() : void
304 {
305 $ref_ids = [
306 '7' => [
307 'name' => 'Second Last Child',
308 'expects_objects' => 7
309 ],
310 '23356343' => [
311 'name' => 'Last Second Child',
312 'expects_objects' => 4
313 ]
314 ];
315
316 foreach ($ref_ids as $ref_id => $additional_information) {
317 $dav_container = $this->getDAVContainerWithExpectationForFunctions(
318 (int) $ref_id,
319 $additional_information['expects_objects']
320 );
321
322 $this->assertFalse($dav_container->childExists($additional_information['name']));
323 }
324 } */
325
326 public function testSetNameThrowsForbiddenError(): void
327 {
328 $dav_client = $this->getDAVClientNodeWithExpectationForFunctions();
329
330 try {
331 $dav_client->setName('My Valid Name');
332 $this->assertFalse('This should not happen!');
333 } catch (Forbidden $e) {
334 $this->assertEquals('It is not possible to change the name of this node.', $e->getMessage());
335 }
336 }
337
339 {
340 $dav_client = $this->getDAVClientNodeWithExpectationForFunctions();
341
342 try {
343 $dav_client->createFile('My New File.txt');
344 $this->assertFalse('This should not happen!');
345 } catch (Forbidden $e) {
346 $this->assertEquals('It is not possible to create a file here.', $e->getMessage());
347 }
348 }
349
351 {
352 $dav_client = $this->getDAVClientNodeWithExpectationForFunctions();
353
354 try {
355 $dav_client->createDirectory('My New Folder');
356 $this->assertFalse('This should not happen!');
357 } catch (Forbidden $e) {
358 $this->assertEquals('It is not possible to create a directory here.', $e->getMessage());
359 }
360 }
361
362 public function testDeleteThrowsForbiddenError(): void
363 {
364 $dav_client = $this->getDAVClientNodeWithExpectationForFunctions();
365
366 try {
367 $dav_client->delete();
368 $this->assertFalse('This should not happen!');
369 } catch (Forbidden $e) {
370 $this->assertEquals('It is not possible to delete this node.', $e->getMessage());
371 }
372 }
373
375 ): ilDAVClientNode {
376 $webdav_test_helper = new ilWebDAVTestHelper();
377 return new ilDAVClientNode($webdav_test_helper->getClientId(), $this->createStub(ilWebDAVObjFactory::class), $this->createStub(ilWebDAVRepositoryHelper::class));
378 }
379}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...