]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/ServerPreconditionTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / ServerPreconditionTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4
5 class Sabre_DAV_ServerPreconditionsTest extends PHPUnit_Framework_TestCase {
6
7     /**
8      * @covers Sabre_DAV_Server::checkPreconditions
9      * @expectedException Sabre_DAV_Exception_PreconditionFailed
10      */
11     function testIfMatchNoNode() {
12
13         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
14         $server = new Sabre_DAV_Server($root);
15         $httpRequest = new Sabre_HTTP_Request(array(
16             'HTTP_IF_MATCH' => '*',
17             'REQUEST_URI'   => '/bar'
18         ));
19         $server->httpRequest = $httpRequest;
20
21         $server->checkPreconditions();
22
23     }
24
25     /**
26      * @covers Sabre_DAV_Server::checkPreconditions
27      */
28     function testIfMatchHasNode() {
29
30         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
31         $server = new Sabre_DAV_Server($root);
32         $httpRequest = new Sabre_HTTP_Request(array(
33             'HTTP_IF_MATCH' => '*',
34             'REQUEST_URI'   => '/foo'
35         ));
36         $server->httpRequest = $httpRequest;
37
38         $this->assertTrue($server->checkPreconditions());
39
40     }
41
42     /**
43      * @covers Sabre_DAV_Server::checkPreconditions
44      * @expectedException Sabre_DAV_Exception_PreconditionFailed
45      */
46     function testIfMatchWrongEtag() {
47
48         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
49         $server = new Sabre_DAV_Server($root);
50         $httpRequest = new Sabre_HTTP_Request(array(
51             'HTTP_IF_MATCH' => '1234',
52             'REQUEST_URI'   => '/foo'
53         ));
54         $server->httpRequest = $httpRequest;
55
56         $server->checkPreconditions();
57
58     }
59
60     /**
61      * @covers Sabre_DAV_Server::checkPreconditions
62      */
63     function testIfMatchCorrectEtag() {
64
65         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
66         $server = new Sabre_DAV_Server($root);
67         $httpRequest = new Sabre_HTTP_Request(array(
68             'HTTP_IF_MATCH' => '"abc123"',
69             'REQUEST_URI'   => '/foo'
70         ));
71         $server->httpRequest = $httpRequest;
72
73         $this->assertTrue($server->checkPreconditions());
74
75     }
76
77     /**
78      * Evolution sometimes uses \" instead of " for If-Match headers.
79      *
80      * @covers Sabre_DAV_Server::checkPreconditions
81      * @depends testIfMatchCorrectEtag
82      */
83     function testIfMatchEvolutionEtag() {
84
85         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
86         $server = new Sabre_DAV_Server($root);
87         $httpRequest = new Sabre_HTTP_Request(array(
88             'HTTP_IF_MATCH' => '\\"abc123\\"',
89             'REQUEST_URI'   => '/foo'
90         ));
91         $server->httpRequest = $httpRequest;
92
93         $this->assertTrue($server->checkPreconditions());
94
95     }
96
97     /**
98      * @covers Sabre_DAV_Server::checkPreconditions
99      */
100     function testIfMatchMultiple() {
101
102         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
103         $server = new Sabre_DAV_Server($root);
104         $httpRequest = new Sabre_HTTP_Request(array(
105             'HTTP_IF_MATCH' => '"hellothere", "abc123"',
106             'REQUEST_URI'   => '/foo'
107         ));
108         $server->httpRequest = $httpRequest;
109
110         $this->assertTrue($server->checkPreconditions());
111
112     }
113
114     /**
115      * @covers Sabre_DAV_Server::checkPreconditions
116      */
117     function testIfNoneMatchNoNode() {
118
119         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
120         $server = new Sabre_DAV_Server($root);
121         $httpRequest = new Sabre_HTTP_Request(array(
122             'HTTP_IF_NONE_MATCH' => '*',
123             'REQUEST_URI'   => '/bar'
124         ));
125         $server->httpRequest = $httpRequest;
126
127         $this->assertTrue($server->checkPreconditions());
128
129     }
130
131     /**
132      * @covers Sabre_DAV_Server::checkPreconditions
133      * @expectedException Sabre_DAV_Exception_PreconditionFailed
134      */
135     function testIfNoneMatchHasNode() {
136
137         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
138         $server = new Sabre_DAV_Server($root);
139         $httpRequest = new Sabre_HTTP_Request(array(
140             'HTTP_IF_NONE_MATCH' => '*',
141             'REQUEST_URI'   => '/foo'
142         ));
143         $server->httpRequest = $httpRequest;
144
145         $server->checkPreconditions();
146
147     }
148
149     /**
150      * @covers Sabre_DAV_Server::checkPreconditions
151      */
152     function testIfNoneMatchWrongEtag() {
153
154         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
155         $server = new Sabre_DAV_Server($root);
156         $httpRequest = new Sabre_HTTP_Request(array(
157             'HTTP_IF_NONE_MATCH' => '"1234"',
158             'REQUEST_URI'   => '/foo'
159         ));
160         $server->httpRequest = $httpRequest;
161
162         $this->assertTrue($server->checkPreconditions());
163
164     }
165
166     /**
167      * @covers Sabre_DAV_Server::checkPreconditions
168      */
169     function testIfNoneMatchWrongEtagMultiple() {
170
171         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
172         $server = new Sabre_DAV_Server($root);
173         $httpRequest = new Sabre_HTTP_Request(array(
174             'HTTP_IF_NONE_MATCH' => '"1234", "5678"',
175             'REQUEST_URI'   => '/foo'
176         ));
177         $server->httpRequest = $httpRequest;
178
179         $this->assertTrue($server->checkPreconditions());
180
181     }
182
183     /**
184      * @covers Sabre_DAV_Server::checkPreconditions
185      * @expectedException Sabre_DAV_Exception_PreconditionFailed
186      */
187     public function testIfNoneMatchCorrectEtag() {
188
189         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
190         $server = new Sabre_DAV_Server($root);
191         $httpRequest = new Sabre_HTTP_Request(array(
192             'HTTP_IF_NONE_MATCH' => '"abc123"',
193             'REQUEST_URI'   => '/foo'
194         ));
195         $server->httpRequest = $httpRequest;
196
197         $server->checkPreconditions();
198
199     }
200
201     /**
202      * @covers Sabre_DAV_Server::checkPreconditions
203      * @expectedException Sabre_DAV_Exception_PreconditionFailed
204      */
205     public function testIfNoneMatchCorrectEtagMultiple() {
206
207         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
208         $server = new Sabre_DAV_Server($root);
209         $httpRequest = new Sabre_HTTP_Request(array(
210             'HTTP_IF_NONE_MATCH' => '"1234", "abc123"',
211             'REQUEST_URI'   => '/foo'
212         ));
213         $server->httpRequest = $httpRequest;
214
215         $server->checkPreconditions();
216
217     }
218
219     /**
220      * @covers Sabre_DAV_Server::checkPreconditions
221      */
222     public function testIfNoneMatchCorrectEtagAsGet() {
223
224         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
225         $server = new Sabre_DAV_Server($root);
226         $httpRequest = new Sabre_HTTP_Request(array(
227             'HTTP_IF_NONE_MATCH' => '"abc123"',
228             'REQUEST_URI'   => '/foo'
229         ));
230         $server->httpRequest = $httpRequest;
231         $server->httpResponse = new Sabre_HTTP_ResponseMock();
232
233         $this->assertFalse($server->checkPreconditions(true));
234         $this->assertEquals('HTTP/1.1 304 Not Modified',$server->httpResponse->status);
235
236     }
237
238     /**
239      * @covers Sabre_DAV_Server::checkPreconditions
240      */
241     public function testIfModifiedSinceUnModified() {
242
243         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
244         $server = new Sabre_DAV_Server($root);
245         $httpRequest = new Sabre_HTTP_Request(array(
246             'HTTP_IF_MODIFIED_SINCE' => 'Sun, 06 Nov 1994 08:49:37 GMT',
247             'REQUEST_URI'   => '/foo'
248         ));
249         $server->httpRequest = $httpRequest;
250         $server->httpResponse = new Sabre_HTTP_ResponseMock();
251         $this->assertFalse($server->checkPreconditions());
252
253         $this->assertEquals('HTTP/1.1 304 Not Modified',$server->httpResponse->status);
254         $this->assertEquals(array(
255             'Last-Modified' => 'Sat, 06 Apr 1985 23:30:00 GMT',
256         ), $server->httpResponse->headers);
257
258     }
259
260
261     /**
262      * @covers Sabre_DAV_Server::checkPreconditions
263      */
264     public function testIfModifiedSinceModified() {
265
266         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
267         $server = new Sabre_DAV_Server($root);
268         $httpRequest = new Sabre_HTTP_Request(array(
269             'HTTP_IF_MODIFIED_SINCE' => 'Tue, 06 Nov 1984 08:49:37 GMT',
270             'REQUEST_URI'   => '/foo'
271         ));
272         $server->httpRequest = $httpRequest;
273         $server->httpResponse = new Sabre_HTTP_ResponseMock();
274         $this->assertTrue($server->checkPreconditions());
275
276     }
277
278     /**
279      * @covers Sabre_DAV_Server::checkPreconditions
280      */
281     public function testIfModifiedSinceInvalidDate() {
282
283         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
284         $server = new Sabre_DAV_Server($root);
285         $httpRequest = new Sabre_HTTP_Request(array(
286             'HTTP_IF_MODIFIED_SINCE' => 'Your mother',
287             'REQUEST_URI'   => '/foo'
288         ));
289         $server->httpRequest = $httpRequest;
290         $server->httpResponse = new Sabre_HTTP_ResponseMock();
291
292         // Invalid dates must be ignored, so this should return true
293         $this->assertTrue($server->checkPreconditions());
294
295     }
296
297     /**
298      * @covers Sabre_DAV_Server::checkPreconditions
299      */
300     public function testIfModifiedSinceInvalidDate2() {
301
302         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
303         $server = new Sabre_DAV_Server($root);
304         $httpRequest = new Sabre_HTTP_Request(array(
305             'HTTP_IF_MODIFIED_SINCE' => 'Sun, 06 Nov 1994 08:49:37 EST',
306             'REQUEST_URI'   => '/foo'
307         ));
308         $server->httpRequest = $httpRequest;
309         $server->httpResponse = new Sabre_HTTP_ResponseMock();
310         $this->assertTrue($server->checkPreconditions());
311
312     }
313
314
315     /**
316      * @covers Sabre_DAV_Server::checkPreconditions
317      */
318     public function testIfUnmodifiedSinceUnModified() {
319
320         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
321         $server = new Sabre_DAV_Server($root);
322         $httpRequest = new Sabre_HTTP_Request(array(
323             'HTTP_IF_UNMODIFIED_SINCE' => 'Sun, 06 Nov 1994 08:49:37 GMT',
324             'REQUEST_URI'   => '/foo'
325         ));
326         $server->httpRequest = $httpRequest;
327         $this->assertTrue($server->checkPreconditions());
328
329     }
330
331
332     /**
333      * @covers Sabre_DAV_Server::checkPreconditions
334      * @expectedException Sabre_DAV_Exception_PreconditionFailed
335      */
336     public function testIfUnmodifiedSinceModified() {
337
338         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
339         $server = new Sabre_DAV_Server($root);
340         $httpRequest = new Sabre_HTTP_Request(array(
341             'HTTP_IF_UNMODIFIED_SINCE' => 'Tue, 06 Nov 1984 08:49:37 GMT',
342             'REQUEST_URI'   => '/foo'
343         ));
344         $server->httpRequest = $httpRequest;
345         $server->httpResponse = new Sabre_HTTP_ResponseMock();
346         $server->checkPreconditions();
347
348     }
349
350     /**
351      * @covers Sabre_DAV_Server::checkPreconditions
352      */
353     public function testIfUnmodifiedSinceInvalidDate() {
354
355         $root = new Sabre_DAV_SimpleCollection('root',array(new Sabre_DAV_ServerPreconditionsNode()));
356         $server = new Sabre_DAV_Server($root);
357         $httpRequest = new Sabre_HTTP_Request(array(
358             'HTTP_IF_UNMODIFIED_SINCE' => 'Sun, 06 Nov 1984 08:49:37 CET',
359             'REQUEST_URI'   => '/foo'
360         ));
361         $server->httpRequest = $httpRequest;
362         $server->httpResponse = new Sabre_HTTP_ResponseMock();
363         $this->assertTrue($server->checkPreconditions());
364
365     }
366
367
368 }
369
370 class Sabre_DAV_ServerPreconditionsNode extends Sabre_DAV_File {
371
372     function getETag() {
373
374         return '"abc123"';
375
376     }
377
378     function getLastModified() {
379
380         /* my birthday & time, I believe */
381         return strtotime('1985-04-07 01:30 +02:00');
382
383     }
384
385     function getName() {
386
387         return 'foo';
388
389     }
390
391 }