ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PluginTest.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\DAV\Sync;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once __DIR__ . '/MockSyncCollection.php';
9
11
12 protected $collection;
13
14 function setUp() {
15
16 parent::setUp();
17 $this->server->addPlugin(new Plugin());
18
19 }
20
21 function testGetInfo() {
22
23 $this->assertArrayHasKey(
24 'name',
25 (new Plugin())->getPluginInfo()
26 );
27
28 }
29
30 function setUpTree() {
31
32 $this->collection =
33 new MockSyncCollection('coll', [
34 new DAV\SimpleFile('file1.txt', 'foo'),
35 new DAV\SimpleFile('file2.txt', 'bar'),
36 ]);
37 $this->tree = [
39 new DAV\SimpleCollection('normalcoll', [])
40 ];
41
42 }
43
45
46 $result = $this->server->getProperties('/coll', ['{DAV:}supported-report-set']);
47 $this->assertFalse($result['{DAV:}supported-report-set']->has('{DAV:}sync-collection'));
48
49 // Making a change
50 $this->collection->addChange(['file1.txt'], [], []);
51
52 $result = $this->server->getProperties('/coll', ['{DAV:}supported-report-set']);
53 $this->assertTrue($result['{DAV:}supported-report-set']->has('{DAV:}sync-collection'));
54
55 }
56
57 function testGetSyncToken() {
58
59 $result = $this->server->getProperties('/coll', ['{DAV:}sync-token']);
60 $this->assertFalse(isset($result['{DAV:}sync-token']));
61
62 // Making a change
63 $this->collection->addChange(['file1.txt'], [], []);
64
65 $result = $this->server->getProperties('/coll', ['{DAV:}sync-token']);
66 $this->assertTrue(isset($result['{DAV:}sync-token']));
67
68 // non-sync-enabled collection
69 $this->collection->addChange(['file1.txt'], [], []);
70
71 $result = $this->server->getProperties('/normalcoll', ['{DAV:}sync-token']);
72 $this->assertFalse(isset($result['{DAV:}sync-token']));
73 }
74
76
77 // Making a change
78 $this->collection->addChange(['file1.txt'], [], []);
79
80 $request = new HTTP\Request('REPORT', '/coll/', ['Content-Type' => 'application/xml']);
81
82 $body = <<<BLA
83<?xml version="1.0" encoding="utf-8" ?>
84<D:sync-collection xmlns:D="DAV:">
85 <D:sync-token/>
86 <D:sync-level>1</D:sync-level>
87 <D:prop>
88 <D:getcontentlength/>
89 </D:prop>
90</D:sync-collection>
91BLA;
92
93 $request->setBody($body);
94
95 $response = $this->request($request);
96
97 $this->assertEquals(207, $response->status, 'Full response body:' . $response->body);
98
99 $multiStatus = $this->server->xml->parse($response->getBodyAsString());
100
101 // Checking the sync-token
102 $this->assertEquals(
103 'http://sabre.io/ns/sync/1',
104 $multiStatus->getSyncToken()
105 );
106
107 $responses = $multiStatus->getResponses();
108 $this->assertEquals(2, count($responses), 'We expected exactly 2 {DAV:}response');
109
110 $response = $responses[0];
111
112 $this->assertNull($response->getHttpStatus());
113 $this->assertEquals('/coll/file1.txt', $response->getHref());
114 $this->assertEquals([
115 200 => [
116 '{DAV:}getcontentlength' => 3,
117 ]
118 ], $response->getResponseProperties());
119
120 $response = $responses[1];
121
122 $this->assertNull($response->getHttpStatus());
123 $this->assertEquals('/coll/file2.txt', $response->getHref());
124 $this->assertEquals([
125 200 => [
126 '{DAV:}getcontentlength' => 3,
127 ]
128 ], $response->getResponseProperties());
129
130 }
131
133
134 // Making a change
135 $this->collection->addChange(['file1.txt'], [], []);
136 // Making another change
137 $this->collection->addChange([], ['file2.txt'], ['file3.txt']);
138
140 'REQUEST_METHOD' => 'REPORT',
141 'REQUEST_URI' => '/coll/',
142 'CONTENT_TYPE' => 'application/xml',
143 ]);
144
145 $body = <<<BLA
146<?xml version="1.0" encoding="utf-8" ?>
147<D:sync-collection xmlns:D="DAV:">
148 <D:sync-token>http://sabre.io/ns/sync/1</D:sync-token>
149 <D:sync-level>infinite</D:sync-level>
150 <D:prop>
151 <D:getcontentlength/>
152 </D:prop>
153</D:sync-collection>
154BLA;
155
156 $request->setBody($body);
157
158 $response = $this->request($request);
159
160 $this->assertEquals(207, $response->status, 'Full response body:' . $response->body);
161
162 $multiStatus = $this->server->xml->parse($response->getBodyAsString());
163
164 // Checking the sync-token
165 $this->assertEquals(
166 'http://sabre.io/ns/sync/2',
167 $multiStatus->getSyncToken()
168 );
169
170 $responses = $multiStatus->getResponses();
171 $this->assertEquals(2, count($responses), 'We expected exactly 2 {DAV:}response');
172
173 $response = $responses[0];
174
175 $this->assertNull($response->getHttpStatus());
176 $this->assertEquals('/coll/file2.txt', $response->getHref());
177 $this->assertEquals([
178 200 => [
179 '{DAV:}getcontentlength' => 3,
180 ]
181 ], $response->getResponseProperties());
182
183 $response = $responses[1];
184
185 $this->assertEquals('404', $response->getHttpStatus());
186 $this->assertEquals('/coll/file3.txt', $response->getHref());
187 $this->assertEquals([], $response->getResponseProperties());
188
189 }
190
192
193 // Making a change
194 $this->collection->addChange(['file1.txt'], [], []);
195 // Making another change
196 $this->collection->addChange([], ['file2.txt'], ['file3.txt']);
197
199 'REQUEST_METHOD' => 'REPORT',
200 'REQUEST_URI' => '/coll/',
201 'CONTENT_TYPE' => 'application/xml',
202 ]);
203
204 $body = <<<BLA
205<?xml version="1.0" encoding="utf-8" ?>
206<D:sync-collection xmlns:D="DAV:">
207 <D:sync-token>http://sabre.io/ns/sync/1</D:sync-token>
208 <D:sync-level>infinite</D:sync-level>
209 <D:prop>
210 <D:getcontentlength/>
211 </D:prop>
212 <D:limit><D:nresults>1</D:nresults></D:limit>
213</D:sync-collection>
214BLA;
215
216 $request->setBody($body);
217
218 $response = $this->request($request);
219
220 $this->assertEquals(207, $response->status, 'Full response body:' . $response->body);
221
222 $multiStatus = $this->server->xml->parse(
223 $response->getBodyAsString()
224 );
225
226 // Checking the sync-token
227 $this->assertEquals(
228 'http://sabre.io/ns/sync/2',
229 $multiStatus->getSyncToken()
230 );
231
232 $responses = $multiStatus->getResponses();
233 $this->assertEquals(1, count($responses), 'We expected exactly 1 {DAV:}response');
234
235 $response = $responses[0];
236
237 $this->assertEquals('404', $response->getHttpStatus());
238 $this->assertEquals('/coll/file3.txt', $response->getHref());
239 $this->assertEquals([], $response->getResponseProperties());
240
241 }
242
244
245 // Making a change
246 $this->collection->addChange(['file1.txt'], [], []);
247 // Making another change
248 $this->collection->addChange([], ['file2.txt'], ['file3.txt']);
249
251 'REQUEST_METHOD' => 'REPORT',
252 'REQUEST_URI' => '/coll/',
253 'CONTENT_TYPE' => 'application/xml',
254 'HTTP_DEPTH' => "1",
255 ]);
256
257 $body = <<<BLA
258<?xml version="1.0" encoding="utf-8" ?>
259<D:sync-collection xmlns:D="DAV:">
260 <D:sync-token>http://sabre.io/ns/sync/1</D:sync-token>
261 <D:prop>
262 <D:getcontentlength/>
263 </D:prop>
264</D:sync-collection>
265BLA;
266
267 $request->setBody($body);
268
269 $response = $this->request($request);
270
271 $this->assertEquals(207, $response->status, 'Full response body:' . $response->body);
272
273 $multiStatus = $this->server->xml->parse(
274 $response->getBodyAsString()
275 );
276
277 // Checking the sync-token
278 $this->assertEquals(
279 'http://sabre.io/ns/sync/2',
280 $multiStatus->getSyncToken()
281 );
282
283 $responses = $multiStatus->getResponses();
284 $this->assertEquals(2, count($responses), 'We expected exactly 2 {DAV:}response');
285
286 $response = $responses[0];
287
288 $this->assertNull($response->getHttpStatus());
289 $this->assertEquals('/coll/file2.txt', $response->getHref());
290 $this->assertEquals([
291 200 => [
292 '{DAV:}getcontentlength' => 3,
293 ]
294 ], $response->getResponseProperties());
295
296 $response = $responses[1];
297
298 $this->assertEquals('404', $response->getHttpStatus());
299 $this->assertEquals('/coll/file3.txt', $response->getHref());
300 $this->assertEquals([], $response->getResponseProperties());
301
302 }
303
305
307 'REQUEST_METHOD' => 'REPORT',
308 'REQUEST_URI' => '/coll/',
309 'CONTENT_TYPE' => 'application/xml',
310 ]);
311
312 $body = <<<BLA
313<?xml version="1.0" encoding="utf-8" ?>
314<D:sync-collection xmlns:D="DAV:">
315 <D:sync-token/>
316 <D:sync-level>1</D:sync-level>
317 <D:prop>
318 <D:getcontentlength/>
319 </D:prop>
320</D:sync-collection>
321BLA;
322
323 $request->setBody($body);
324
325 $response = $this->request($request);
326
327 // The default state has no sync-token, so this report should not yet
328 // be supported.
329 $this->assertEquals(415, $response->status, 'Full response body:' . $response->body);
330
331 }
332
334
336 'REQUEST_METHOD' => 'REPORT',
337 'REQUEST_URI' => '/normalcoll/',
338 'CONTENT_TYPE' => 'application/xml',
339 ]);
340
341 $body = <<<BLA
342<?xml version="1.0" encoding="utf-8" ?>
343<D:sync-collection xmlns:D="DAV:">
344 <D:sync-token/>
345 <D:sync-level>1</D:sync-level>
346 <D:prop>
347 <D:getcontentlength/>
348 </D:prop>
349</D:sync-collection>
350BLA;
351
352 $request->setBody($body);
353
354 $response = $this->request($request);
355
356 // The default state has no sync-token, so this report should not yet
357 // be supported.
358 $this->assertEquals(415, $response->status, 'Full response body:' . $response->body);
359
360 }
361
363
364 $this->collection->addChange(['file1.txt'], [], []);
366 'REQUEST_METHOD' => 'REPORT',
367 'REQUEST_URI' => '/coll/',
368 'CONTENT_TYPE' => 'application/xml',
369 ]);
370
371 $body = <<<BLA
372<?xml version="1.0" encoding="utf-8" ?>
373<D:sync-collection xmlns:D="DAV:">
374 <D:sync-token>http://sabre.io/ns/sync/invalid</D:sync-token>
375 <D:sync-level>1</D:sync-level>
376 <D:prop>
377 <D:getcontentlength/>
378 </D:prop>
379</D:sync-collection>
380BLA;
381
382 $request->setBody($body);
383
384 $response = $this->request($request);
385
386 // The default state has no sync-token, so this report should not yet
387 // be supported.
388 $this->assertEquals(403, $response->status, 'Full response body:' . $response->body);
389
390 }
392
393 $this->collection->addChange(['file1.txt'], [], []);
395 'REQUEST_METHOD' => 'REPORT',
396 'REQUEST_URI' => '/coll/',
397 'CONTENT_TYPE' => 'application/xml',
398 ]);
399
400 $body = <<<BLA
401<?xml version="1.0" encoding="utf-8" ?>
402<D:sync-collection xmlns:D="DAV:">
403 <D:sync-token>invalid</D:sync-token>
404 <D:sync-level>1</D:sync-level>
405 <D:prop>
406 <D:getcontentlength/>
407 </D:prop>
408</D:sync-collection>
409BLA;
410
411 $request->setBody($body);
412
413 $response = $this->request($request);
414
415 // The default state has no sync-token, so this report should not yet
416 // be supported.
417 $this->assertEquals(403, $response->status, 'Full response body:' . $response->body);
418
419 }
420
422
424 'REQUEST_METHOD' => 'REPORT',
425 'REQUEST_URI' => '/coll/',
426 'CONTENT_TYPE' => 'application/xml',
427 ]);
428
429 $body = <<<BLA
430<?xml version="1.0" encoding="utf-8" ?>
431<D:sync-collection xmlns:D="DAV:">
432 <D:sync-level>1</D:sync-level>
433 <D:prop>
434 <D:getcontentlength/>
435 </D:prop>
436</D:sync-collection>
437BLA;
438
439 $request->setBody($body);
440
441 $response = $this->request($request);
442
443 // The default state has no sync-token, so this report should not yet
444 // be supported.
445 $this->assertEquals(400, $response->status, 'Full response body:' . $response->body);
446
447 }
448
449 function testSyncNoProp() {
450
451 $this->collection->addChange(['file1.txt'], [], []);
453 'REQUEST_METHOD' => 'REPORT',
454 'REQUEST_URI' => '/coll/',
455 'CONTENT_TYPE' => 'application/xml',
456 ]);
457
458 $body = <<<BLA
459<?xml version="1.0" encoding="utf-8" ?>
460<D:sync-collection xmlns:D="DAV:">
461 <D:sync-token />
462 <D:sync-level>1</D:sync-level>
463</D:sync-collection>
464BLA;
465
466 $request->setBody($body);
467
468 $response = $this->request($request);
469
470 // The default state has no sync-token, so this report should not yet
471 // be supported.
472 $this->assertEquals(400, $response->status, 'Full response body:' . $response->body);
473
474 }
475
476 function testIfConditions() {
477
478 $this->collection->addChange(['file1.txt'], [], []);
480 'REQUEST_METHOD' => 'DELETE',
481 'REQUEST_URI' => '/coll/file1.txt',
482 'HTTP_IF' => '</coll> (<http://sabre.io/ns/sync/1>)',
483 ]);
484 $response = $this->request($request);
485
486 // If a 403 is thrown this works correctly. The file in questions
487 // doesn't allow itself to be deleted.
488 // If the If conditions failed, it would have been a 412 instead.
489 $this->assertEquals(403, $response->status);
490
491 }
492
494
495 $this->collection->addChange(['file1.txt'], [], []);
497 'REQUEST_METHOD' => 'DELETE',
498 'REQUEST_URI' => '/coll/file1.txt',
499 'HTTP_IF' => '</coll> (Not <http://sabre.io/ns/sync/2>)',
500 ]);
501 $response = $this->request($request);
502
503 // If a 403 is thrown this works correctly. The file in questions
504 // doesn't allow itself to be deleted.
505 // If the If conditions failed, it would have been a 412 instead.
506 $this->assertEquals(403, $response->status);
507
508 }
509
511
512 $this->collection->addChange(['file1.txt'], [], []);
514 'REQUEST_METHOD' => 'DELETE',
515 'REQUEST_URI' => '/coll/file1.txt',
516 'HTTP_IF' => '</coll> (<opaquelocktoken:foo>)',
517 ]);
518 $response = $this->request($request);
519
520 $this->assertEquals(412, $response->status);
521
522 }
523}
$result
foreach($paths as $path) $request
Definition: asyncclient.php:32
An exception for terminatinating execution or to throw for unit testing.
This class may be used as a basis for other webdav-related unittests.
request($request, $expectedStatus=null)
Makes a request, and returns a response object.
This mocks a ISyncCollection, for unittesting.
setUpTree()
Override this to provide your own Tree for your test-case.
Definition: PluginTest.php:30
This plugin all WebDAV-sync capabilities to the Server.
Definition: Plugin.php:21
static createFromServerArray(array $serverArray)
This static method will create a new Request object, based on a PHP $_SERVER array.
Definition: Sapi.php:107
static http()
Fetches the global http state from ILIAS.
$response