3 require_once 'Sabre/HTTP/ResponseMock.php';
5 class Sabre_DAV_ServerPreconditionsTest extends PHPUnit_Framework_TestCase {
8 * @covers Sabre_DAV_Server::checkPreconditions
9 * @expectedException Sabre_DAV_Exception_PreconditionFailed
11 function testIfMatchNoNode() {
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'
19 $server->httpRequest = $httpRequest;
21 $server->checkPreconditions();
26 * @covers Sabre_DAV_Server::checkPreconditions
28 function testIfMatchHasNode() {
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'
36 $server->httpRequest = $httpRequest;
38 $this->assertTrue($server->checkPreconditions());
43 * @covers Sabre_DAV_Server::checkPreconditions
44 * @expectedException Sabre_DAV_Exception_PreconditionFailed
46 function testIfMatchWrongEtag() {
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'
54 $server->httpRequest = $httpRequest;
56 $server->checkPreconditions();
61 * @covers Sabre_DAV_Server::checkPreconditions
63 function testIfMatchCorrectEtag() {
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'
71 $server->httpRequest = $httpRequest;
73 $this->assertTrue($server->checkPreconditions());
78 * Evolution sometimes uses \" instead of " for If-Match headers.
80 * @covers Sabre_DAV_Server::checkPreconditions
81 * @depends testIfMatchCorrectEtag
83 function testIfMatchEvolutionEtag() {
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'
91 $server->httpRequest = $httpRequest;
93 $this->assertTrue($server->checkPreconditions());
98 * @covers Sabre_DAV_Server::checkPreconditions
100 function testIfMatchMultiple() {
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'
108 $server->httpRequest = $httpRequest;
110 $this->assertTrue($server->checkPreconditions());
115 * @covers Sabre_DAV_Server::checkPreconditions
117 function testIfNoneMatchNoNode() {
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'
125 $server->httpRequest = $httpRequest;
127 $this->assertTrue($server->checkPreconditions());
132 * @covers Sabre_DAV_Server::checkPreconditions
133 * @expectedException Sabre_DAV_Exception_PreconditionFailed
135 function testIfNoneMatchHasNode() {
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'
143 $server->httpRequest = $httpRequest;
145 $server->checkPreconditions();
150 * @covers Sabre_DAV_Server::checkPreconditions
152 function testIfNoneMatchWrongEtag() {
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'
160 $server->httpRequest = $httpRequest;
162 $this->assertTrue($server->checkPreconditions());
167 * @covers Sabre_DAV_Server::checkPreconditions
169 function testIfNoneMatchWrongEtagMultiple() {
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'
177 $server->httpRequest = $httpRequest;
179 $this->assertTrue($server->checkPreconditions());
184 * @covers Sabre_DAV_Server::checkPreconditions
185 * @expectedException Sabre_DAV_Exception_PreconditionFailed
187 public function testIfNoneMatchCorrectEtag() {
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'
195 $server->httpRequest = $httpRequest;
197 $server->checkPreconditions();
202 * @covers Sabre_DAV_Server::checkPreconditions
203 * @expectedException Sabre_DAV_Exception_PreconditionFailed
205 public function testIfNoneMatchCorrectEtagMultiple() {
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'
213 $server->httpRequest = $httpRequest;
215 $server->checkPreconditions();
220 * @covers Sabre_DAV_Server::checkPreconditions
222 public function testIfNoneMatchCorrectEtagAsGet() {
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'
230 $server->httpRequest = $httpRequest;
231 $server->httpResponse = new Sabre_HTTP_ResponseMock();
233 $this->assertFalse($server->checkPreconditions(true));
234 $this->assertEquals('HTTP/1.1 304 Not Modified',$server->httpResponse->status);
239 * @covers Sabre_DAV_Server::checkPreconditions
241 public function testIfModifiedSinceUnModified() {
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'
249 $server->httpRequest = $httpRequest;
250 $server->httpResponse = new Sabre_HTTP_ResponseMock();
251 $this->assertFalse($server->checkPreconditions());
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);
262 * @covers Sabre_DAV_Server::checkPreconditions
264 public function testIfModifiedSinceModified() {
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'
272 $server->httpRequest = $httpRequest;
273 $server->httpResponse = new Sabre_HTTP_ResponseMock();
274 $this->assertTrue($server->checkPreconditions());
279 * @covers Sabre_DAV_Server::checkPreconditions
281 public function testIfModifiedSinceInvalidDate() {
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'
289 $server->httpRequest = $httpRequest;
290 $server->httpResponse = new Sabre_HTTP_ResponseMock();
292 // Invalid dates must be ignored, so this should return true
293 $this->assertTrue($server->checkPreconditions());
298 * @covers Sabre_DAV_Server::checkPreconditions
300 public function testIfModifiedSinceInvalidDate2() {
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'
308 $server->httpRequest = $httpRequest;
309 $server->httpResponse = new Sabre_HTTP_ResponseMock();
310 $this->assertTrue($server->checkPreconditions());
316 * @covers Sabre_DAV_Server::checkPreconditions
318 public function testIfUnmodifiedSinceUnModified() {
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'
326 $server->httpRequest = $httpRequest;
327 $this->assertTrue($server->checkPreconditions());
333 * @covers Sabre_DAV_Server::checkPreconditions
334 * @expectedException Sabre_DAV_Exception_PreconditionFailed
336 public function testIfUnmodifiedSinceModified() {
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'
344 $server->httpRequest = $httpRequest;
345 $server->httpResponse = new Sabre_HTTP_ResponseMock();
346 $server->checkPreconditions();
351 * @covers Sabre_DAV_Server::checkPreconditions
353 public function testIfUnmodifiedSinceInvalidDate() {
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'
361 $server->httpRequest = $httpRequest;
362 $server->httpResponse = new Sabre_HTTP_ResponseMock();
363 $this->assertTrue($server->checkPreconditions());
370 class Sabre_DAV_ServerPreconditionsNode extends Sabre_DAV_File {
378 function getLastModified() {
380 /* my birthday & time, I believe */
381 return strtotime('1985-04-07 01:30 +02:00');