1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\HTTP;
6
7class ServerMKCOLTest extends AbstractServer {
8
9    function testMkcol() {
10
11        $serverVars = array(
12            'REQUEST_URI'    => '/testcol',
13            'REQUEST_METHOD' => 'MKCOL',
14        );
15
16        $request = HTTP\Sapi::createFromServerArray($serverVars);
17        $request->setBody("");
18        $this->server->httpRequest = ($request);
19        $this->server->exec();
20
21        $this->assertEquals(array(
22            'X-Sabre-Version' => [Version::VERSION],
23            'Content-Length' => ['0'],
24        ),$this->response->getHeaders());
25
26        $this->assertEquals(201, $this->response->status);
27        $this->assertEquals('', $this->response->body);
28        $this->assertTrue(is_dir($this->tempDir . '/testcol'));
29
30    }
31
32    /**
33     * @depends testMkcol
34     */
35    function testMKCOLUnknownBody() {
36
37        $serverVars = array(
38            'REQUEST_URI'    => '/testcol',
39            'REQUEST_METHOD' => 'MKCOL',
40        );
41
42        $request = HTTP\Sapi::createFromServerArray($serverVars);
43        $request->setBody("Hello");
44        $this->server->httpRequest = ($request);
45        $this->server->exec();
46
47        $this->assertEquals(array(
48            'X-Sabre-Version' => [Version::VERSION],
49            'Content-Type' => ['application/xml; charset=utf-8'],
50        ),$this->response->getHeaders());
51
52        $this->assertEquals(415, $this->response->status);
53
54    }
55
56    /**
57     * @depends testMkcol
58     */
59    function testMKCOLBrokenXML() {
60
61        $serverVars = array(
62            'REQUEST_URI'    => '/testcol',
63            'REQUEST_METHOD' => 'MKCOL',
64            'HTTP_CONTENT_TYPE' => 'application/xml',
65        );
66
67        $request = HTTP\Sapi::createFromServerArray($serverVars);
68        $request->setBody("Hello");
69        $this->server->httpRequest = ($request);
70        $this->server->exec();
71
72        $this->assertEquals(array(
73            'X-Sabre-Version' => [Version::VERSION],
74            'Content-Type' => ['application/xml; charset=utf-8'],
75        ),$this->response->getHeaders());
76
77        $this->assertEquals(400, $this->response->getStatus(), $this->response->getBodyAsString() );
78
79    }
80
81    /**
82     * @depends testMkcol
83     */
84    function testMKCOLUnknownXML() {
85
86        $serverVars = array(
87            'REQUEST_URI'    => '/testcol',
88            'REQUEST_METHOD' => 'MKCOL',
89            'HTTP_CONTENT_TYPE' => 'application/xml',
90        );
91
92        $request = HTTP\Sapi::createFromServerArray($serverVars);
93        $request->setBody('<?xml version="1.0"?><html></html>');
94        $this->server->httpRequest = ($request);
95        $this->server->exec();
96
97        $this->assertEquals(array(
98            'X-Sabre-Version' => [Version::VERSION],
99            'Content-Type' => ['application/xml; charset=utf-8'],
100        ),$this->response->getHeaders());
101
102        $this->assertEquals(400, $this->response->getStatus());
103
104    }
105
106    /**
107     * @depends testMkcol
108     */
109    function testMKCOLNoResourceType() {
110
111        $serverVars = array(
112            'REQUEST_URI'    => '/testcol',
113            'REQUEST_METHOD' => 'MKCOL',
114            'HTTP_CONTENT_TYPE' => 'application/xml',
115        );
116
117        $request = HTTP\Sapi::createFromServerArray($serverVars);
118        $request->setBody('<?xml version="1.0"?>
119<mkcol xmlns="DAV:">
120  <set>
121    <prop>
122        <displayname>Evert</displayname>
123    </prop>
124  </set>
125</mkcol>');
126        $this->server->httpRequest = ($request);
127        $this->server->exec();
128
129        $this->assertEquals(array(
130            'X-Sabre-Version' => [Version::VERSION],
131            'Content-Type' => ['application/xml; charset=utf-8'],
132        ),$this->response->getHeaders());
133
134        $this->assertEquals(400, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
135
136    }
137
138    /**
139     * @depends testMkcol
140     */
141    function testMKCOLIncorrectResourceType() {
142
143        $serverVars = array(
144            'REQUEST_URI'    => '/testcol',
145            'REQUEST_METHOD' => 'MKCOL',
146            'HTTP_CONTENT_TYPE' => 'application/xml',
147        );
148
149        $request = HTTP\Sapi::createFromServerArray($serverVars);
150        $request->setBody('<?xml version="1.0"?>
151<mkcol xmlns="DAV:">
152  <set>
153    <prop>
154        <resourcetype><collection /><blabla /></resourcetype>
155    </prop>
156  </set>
157</mkcol>');
158        $this->server->httpRequest = ($request);
159        $this->server->exec();
160
161        $this->assertEquals(array(
162            'X-Sabre-Version' => [Version::VERSION],
163            'Content-Type' => ['application/xml; charset=utf-8'],
164        ),$this->response->getHeaders());
165
166        $this->assertEquals(403, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
167
168    }
169
170    /**
171     * @depends testMKCOLIncorrectResourceType
172     */
173    function testMKCOLSuccess() {
174
175        $serverVars = array(
176            'REQUEST_URI'    => '/testcol',
177            'REQUEST_METHOD' => 'MKCOL',
178            'HTTP_CONTENT_TYPE' => 'application/xml',
179        );
180
181        $request = HTTP\Sapi::createFromServerArray($serverVars);
182        $request->setBody('<?xml version="1.0"?>
183<mkcol xmlns="DAV:">
184  <set>
185    <prop>
186        <resourcetype><collection /></resourcetype>
187    </prop>
188  </set>
189</mkcol>');
190        $this->server->httpRequest = ($request);
191        $this->server->exec();
192
193        $this->assertEquals(array(
194            'X-Sabre-Version' => [Version::VERSION],
195            'Content-Length' => ['0'],
196        ),$this->response->getHeaders());
197
198        $this->assertEquals(201, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
199
200    }
201
202    /**
203     * @depends testMKCOLIncorrectResourceType
204     */
205    function testMKCOLWhiteSpaceResourceType() {
206
207        $serverVars = array(
208            'REQUEST_URI'    => '/testcol',
209            'REQUEST_METHOD' => 'MKCOL',
210            'HTTP_CONTENT_TYPE' => 'application/xml',
211        );
212
213        $request = HTTP\Sapi::createFromServerArray($serverVars);
214        $request->setBody('<?xml version="1.0"?>
215<mkcol xmlns="DAV:">
216  <set>
217    <prop>
218        <resourcetype>
219            <collection />
220        </resourcetype>
221    </prop>
222  </set>
223</mkcol>');
224        $this->server->httpRequest = ($request);
225        $this->server->exec();
226
227        $this->assertEquals(array(
228            'X-Sabre-Version' => [Version::VERSION],
229            'Content-Length' => ['0'],
230        ),$this->response->getHeaders());
231
232        $this->assertEquals(201, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
233
234    }
235
236    /**
237     * @depends testMKCOLIncorrectResourceType
238     */
239    function testMKCOLNoParent() {
240
241        $serverVars = array(
242            'REQUEST_URI'    => '/testnoparent/409me',
243            'REQUEST_METHOD' => 'MKCOL',
244        );
245
246        $request = HTTP\Sapi::createFromServerArray($serverVars);
247        $request->setBody('');
248
249        $this->server->httpRequest = ($request);
250        $this->server->exec();
251
252        $this->assertEquals(array(
253            'X-Sabre-Version' => [Version::VERSION],
254            'Content-Type' => ['application/xml; charset=utf-8'],
255        ),$this->response->getHeaders());
256
257        $this->assertEquals(409, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
258
259    }
260
261    /**
262     * @depends testMKCOLIncorrectResourceType
263     */
264    function testMKCOLParentIsNoCollection() {
265
266        $serverVars = array(
267            'REQUEST_URI'    => '/test.txt/409me',
268            'REQUEST_METHOD' => 'MKCOL',
269        );
270
271        $request = HTTP\Sapi::createFromServerArray($serverVars);
272        $request->setBody('');
273
274        $this->server->httpRequest = ($request);
275        $this->server->exec();
276
277        $this->assertEquals(array(
278            'X-Sabre-Version' => [Version::VERSION],
279            'Content-Type' => ['application/xml; charset=utf-8'],
280        ),$this->response->getHeaders());
281
282        $this->assertEquals(409, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
283
284    }
285
286    /**
287     * @depends testMKCOLIncorrectResourceType
288     */
289    function testMKCOLAlreadyExists() {
290
291        $serverVars = array(
292            'REQUEST_URI'    => '/test.txt',
293            'REQUEST_METHOD' => 'MKCOL',
294        );
295
296        $request = HTTP\Sapi::createFromServerArray($serverVars);
297        $request->setBody('');
298
299        $this->server->httpRequest = ($request);
300        $this->server->exec();
301
302        $this->assertEquals(array(
303            'X-Sabre-Version' => [Version::VERSION],
304            'Content-Type' => ['application/xml; charset=utf-8'],
305            'Allow'        => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT'],
306        ),$this->response->getHeaders());
307
308        $this->assertEquals(405, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
309
310    }
311
312    /**
313     * @depends testMKCOLSuccess
314     * @depends testMKCOLAlreadyExists
315     */
316    function testMKCOLAndProps() {
317
318        $serverVars = array(
319            'REQUEST_URI'    => '/testcol',
320            'REQUEST_METHOD' => 'MKCOL',
321            'HTTP_CONTENT_TYPE' => 'application/xml',
322        );
323
324        $request = HTTP\Sapi::createFromServerArray($serverVars);
325        $request->setBody('<?xml version="1.0"?>
326<mkcol xmlns="DAV:">
327  <set>
328    <prop>
329        <resourcetype><collection /></resourcetype>
330        <displayname>my new collection</displayname>
331    </prop>
332  </set>
333</mkcol>');
334        $this->server->httpRequest = ($request);
335        $this->server->exec();
336
337        $this->assertEquals(207, $this->response->status, 'Wrong statuscode received. Full response body: ' .$this->response->body);
338
339        $this->assertEquals(array(
340            'X-Sabre-Version' => [Version::VERSION],
341            'Content-Type' => ['application/xml; charset=utf-8'],
342        ),$this->response->getHeaders());
343
344    }
345
346}
347