3 require_once 'Sabre/HTTP/ResponseMock.php';
5 class Sabre_DAV_ServerCopyMoveTest extends PHPUnit_Framework_TestCase {
9 * @var Sabre_DAV_Server
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');
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)) {
35 if (is_dir($tmpFile)) {
47 function testCopyOverWrite() {
50 'REQUEST_URI' => '/test.txt',
51 'REQUEST_METHOD' => 'COPY',
52 'HTTP_DESTINATION' => '/test2.txt',
55 $request = new Sabre_HTTP_Request($serverVars);
56 $this->server->httpRequest = ($request);
57 $this->server->exec();
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',
63 $this->response->headers
66 $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test2.txt'));
70 function testCopyToSelf() {
73 'REQUEST_URI' => '/test.txt',
74 'REQUEST_METHOD' => 'COPY',
75 'HTTP_DESTINATION' => '/test.txt',
78 $request = new Sabre_HTTP_Request($serverVars);
79 $this->server->httpRequest = ($request);
80 $this->server->exec();
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'));
87 function testMoveToSelf() {
90 'REQUEST_URI' => '/test.txt',
91 'REQUEST_METHOD' => 'MOVE',
92 'HTTP_DESTINATION' => '/test.txt',
95 $request = new Sabre_HTTP_Request($serverVars);
96 $this->server->httpRequest = ($request);
97 $this->server->exec();
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'));
104 function testMoveOverWrite() {
107 'REQUEST_URI' => '/test.txt',
108 'REQUEST_METHOD' => 'MOVE',
109 'HTTP_DESTINATION' => '/test2.txt',
112 $request = new Sabre_HTTP_Request($serverVars);
113 $this->server->httpRequest = ($request);
114 $this->server->exec();
116 $this->assertEquals(array(
117 'Content-Length' => 0,
119 $this->response->headers
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');
128 function testBlockedOverWrite() {
131 'REQUEST_URI' => '/test.txt',
132 'REQUEST_METHOD' => 'COPY',
133 'HTTP_DESTINATION' => '/test2.txt',
134 'HTTP_OVERWRITE' => 'F',
137 $request = new Sabre_HTTP_Request($serverVars);
138 $this->server->httpRequest = ($request);
139 $this->server->exec();
141 $this->assertEquals(array(
142 'Content-Type' => 'application/xml; charset=utf-8',
144 $this->response->headers
147 $this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
148 $this->assertEquals('Test contents2',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
153 function testNonExistantParent() {
156 'REQUEST_URI' => '/test.txt',
157 'REQUEST_METHOD' => 'COPY',
158 'HTTP_DESTINATION' => '/testcol2/test2.txt',
159 'HTTP_OVERWRITE' => 'F',
162 $request = new Sabre_HTTP_Request($serverVars);
163 $this->server->httpRequest = ($request);
164 $this->server->exec();
166 $this->assertEquals(array(
167 'Content-Type' => 'application/xml; charset=utf-8',
169 $this->response->headers
172 $this->assertEquals('HTTP/1.1 409 Conflict',$this->response->status);
176 function testRandomOverwriteHeader() {
179 'REQUEST_URI' => '/test.txt',
180 'REQUEST_METHOD' => 'COPY',
181 'HTTP_DESTINATION' => '/testcol2/test2.txt',
182 'HTTP_OVERWRITE' => 'SURE!',
185 $request = new Sabre_HTTP_Request($serverVars);
186 $this->server->httpRequest = ($request);
187 $this->server->exec();
189 $this->assertEquals('HTTP/1.1 400 Bad request',$this->response->status);
193 function testCopyDirectory() {
196 'REQUEST_URI' => '/col',
197 'REQUEST_METHOD' => 'COPY',
198 'HTTP_DESTINATION' => '/col2',
201 $request = new Sabre_HTTP_Request($serverVars);
202 $this->server->httpRequest = ($request);
203 $this->server->exec();
205 $this->assertEquals(array(
206 'Content-Length' => '0',
208 $this->response->headers
211 $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
212 $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
216 function testSimpleCopyFile() {
219 'REQUEST_URI' => '/test.txt',
220 'REQUEST_METHOD' => 'COPY',
221 'HTTP_DESTINATION' => '/test3.txt',
224 $request = new Sabre_HTTP_Request($serverVars);
225 $this->server->httpRequest = ($request);
226 $this->server->exec();
228 $this->assertEquals(array(
229 'Content-Length' => '0',
231 $this->response->headers
234 $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
235 $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/test3.txt'));
239 function testSimpleCopyCollection() {
242 'REQUEST_URI' => '/col',
243 'REQUEST_METHOD' => 'COPY',
244 'HTTP_DESTINATION' => '/col2',
247 $request = new Sabre_HTTP_Request($serverVars);
248 $this->server->httpRequest = ($request);
249 $this->server->exec();
251 $this->assertEquals('HTTP/1.1 201 Created',$this->response->status,'Incorrect status received. Full response body: ' . $this->response->body);
253 $this->assertEquals(array(
254 'Content-Length' => '0',
256 $this->response->headers
260 $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));