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