]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/ServerCopyMoveTest.php
Merge pull request #57 from CatoTH/master
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / ServerCopyMoveTest.php
1 <?php
2
3 require_once 'Sabre/HTTP/ResponseMock.php';
4
5 class Sabre_DAV_ServerCopyMoveTest extends PHPUnit_Framework_TestCase {
6
7     private $response;
8     /**
9      * @var Sabre_DAV_Server
10      */
11     private $server;
12
13     function setUp() {
14
15         $this->response = new Sabre_HTTP_ResponseMock();
16         $dir = new Sabre_DAV_FS_Directory(SABRE_TEMPDIR);
17         $tree = new Sabre_DAV_ObjectTree($dir);
18         $this->server = new Sabre_DAV_Server($tree);
19         $this->server->debugExceptions = true;
20         $this->server->httpResponse = $this->response;
21         file_put_contents(SABRE_TEMPDIR . '/test.txt', 'Test contents');
22         file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
23         mkdir(SABRE_TEMPDIR . '/col');
24         file_put_contents(SABRE_TEMPDIR . 'col/test.txt', 'Test contents');
25
26     }
27
28     function tearDown() {
29
30         $cleanUp = array('test.txt','testput.txt','testcol','test2.txt','test3.txt','col/test.txt','col','col2/test.txt','col2');
31         foreach($cleanUp as $file) {
32             $tmpFile = SABRE_TEMPDIR . '/' . $file;
33             if (file_exists($tmpFile)) {
34
35                 if (is_dir($tmpFile)) {
36                     rmdir($tmpFile);
37                 } else {
38                     unlink($tmpFile);
39                 }
40
41             }
42         }
43
44     }
45
46
47     function testCopyOverWrite() {
48
49         $serverVars = array(
50             'REQUEST_URI'    => '/test.txt',
51             'REQUEST_METHOD' => 'COPY',
52             'HTTP_DESTINATION' => '/test2.txt',
53         );
54
55         $request = new Sabre_HTTP_Request($serverVars);
56         $this->server->httpRequest = ($request);
57         $this->server->exec();
58
59         $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
60         $this->assertEquals(array(
61                 'Content-Length' => '0',
62             ),
63             $this->response->headers
64          );
65
66         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test2.txt'));
67
68     }
69
70     function testCopyToSelf() {
71
72         $serverVars = array(
73             'REQUEST_URI'    => '/test.txt',
74             'REQUEST_METHOD' => 'COPY',
75             'HTTP_DESTINATION' => '/test.txt',
76         );
77
78         $request = new Sabre_HTTP_Request($serverVars);
79         $this->server->httpRequest = ($request);
80         $this->server->exec();
81
82         $this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
83         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test.txt'));
84
85     }
86
87     function testMoveToSelf() {
88
89         $serverVars = array(
90             'REQUEST_URI'    => '/test.txt',
91             'REQUEST_METHOD' => 'MOVE',
92             'HTTP_DESTINATION' => '/test.txt',
93         );
94
95         $request = new Sabre_HTTP_Request($serverVars);
96         $this->server->httpRequest = ($request);
97         $this->server->exec();
98
99         $this->assertEquals('HTTP/1.1 403 Forbidden',$this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
100         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test.txt'));
101
102     }
103
104     function testMoveOverWrite() {
105
106         $serverVars = array(
107             'REQUEST_URI'    => '/test.txt',
108             'REQUEST_METHOD' => 'MOVE',
109             'HTTP_DESTINATION' => '/test2.txt',
110         );
111
112         $request = new Sabre_HTTP_Request($serverVars);
113         $this->server->httpRequest = ($request);
114         $this->server->exec();
115
116         $this->assertEquals(array(
117                 'Content-Length' => 0,
118             ),
119             $this->response->headers
120          );
121
122         $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
123         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
124         $this->assertFalse(file_exists(SABRE_TEMPDIR . '/test.txt'),'The sourcefile test.txt should no longer exist at this point');
125
126     }
127
128     function testBlockedOverWrite() {
129
130         $serverVars = array(
131             'REQUEST_URI'      => '/test.txt',
132             'REQUEST_METHOD'   => 'COPY',
133             'HTTP_DESTINATION' => '/test2.txt',
134             'HTTP_OVERWRITE'   => 'F',
135         );
136
137         $request = new Sabre_HTTP_Request($serverVars);
138         $this->server->httpRequest = ($request);
139         $this->server->exec();
140
141         $this->assertEquals(array(
142                 'Content-Type' => 'application/xml; charset=utf-8',
143             ),
144             $this->response->headers
145          );
146
147         $this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
148         $this->assertEquals('Test contents2',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
149
150
151     }
152
153     function testNonExistantParent() {
154
155         $serverVars = array(
156             'REQUEST_URI'      => '/test.txt',
157             'REQUEST_METHOD'   => 'COPY',
158             'HTTP_DESTINATION' => '/testcol2/test2.txt',
159             'HTTP_OVERWRITE'   => 'F',
160         );
161
162         $request = new Sabre_HTTP_Request($serverVars);
163         $this->server->httpRequest = ($request);
164         $this->server->exec();
165
166         $this->assertEquals(array(
167                 'Content-Type' => 'application/xml; charset=utf-8',
168             ),
169             $this->response->headers
170          );
171
172         $this->assertEquals('HTTP/1.1 409 Conflict',$this->response->status);
173
174     }
175
176     function testRandomOverwriteHeader() {
177
178         $serverVars = array(
179             'REQUEST_URI'      => '/test.txt',
180             'REQUEST_METHOD'   => 'COPY',
181             'HTTP_DESTINATION' => '/testcol2/test2.txt',
182             'HTTP_OVERWRITE'   => 'SURE!',
183         );
184
185         $request = new Sabre_HTTP_Request($serverVars);
186         $this->server->httpRequest = ($request);
187         $this->server->exec();
188
189         $this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status);
190
191     }
192
193     function testCopyDirectory() {
194
195         $serverVars = array(
196             'REQUEST_URI'    => '/col',
197             'REQUEST_METHOD' => 'COPY',
198             'HTTP_DESTINATION' => '/col2',
199         );
200
201         $request = new Sabre_HTTP_Request($serverVars);
202         $this->server->httpRequest = ($request);
203         $this->server->exec();
204
205         $this->assertEquals(array(
206                 'Content-Length' => '0',
207             ),
208             $this->response->headers
209          );
210
211         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
212         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
213
214     }
215
216     function testSimpleCopyFile() {
217
218         $serverVars = array(
219             'REQUEST_URI'    => '/test.txt',
220             'REQUEST_METHOD' => 'COPY',
221             'HTTP_DESTINATION' => '/test3.txt',
222         );
223
224         $request = new Sabre_HTTP_Request($serverVars);
225         $this->server->httpRequest = ($request);
226         $this->server->exec();
227
228         $this->assertEquals(array(
229             'Content-Length' => '0',
230             ),
231             $this->response->headers
232          );
233
234         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
235         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/test3.txt'));
236
237     }
238
239     function testSimpleCopyCollection() {
240
241         $serverVars = array(
242             'REQUEST_URI'    => '/col',
243             'REQUEST_METHOD' => 'COPY',
244             'HTTP_DESTINATION' => '/col2',
245         );
246
247         $request = new Sabre_HTTP_Request($serverVars);
248         $this->server->httpRequest = ($request);
249         $this->server->exec();
250
251         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'Incorrect status received. Full response body: ' . $this->response->body);
252
253         $this->assertEquals(array(
254             'Content-Length' => '0',
255             ),
256             $this->response->headers
257          );
258
259
260         $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
261
262     }
263
264 }