1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\HTTP;
6
7require_once 'Sabre/HTTP/ResponseMock.php';
8
9class ServerCopyMoveTest extends \PHPUnit_Framework_TestCase {
10
11    private $response;
12    /**
13     * @var Server
14     */
15    private $server;
16
17    function setUp() {
18
19        $this->response = new HTTP\ResponseMock();
20        $dir = new FS\Directory(SABRE_TEMPDIR);
21        $tree = new Tree($dir);
22        $this->server = new Server($tree);
23        $this->server->sapi = new HTTP\SapiMock();
24        $this->server->debugExceptions = true;
25        $this->server->httpResponse = $this->response;
26        file_put_contents(SABRE_TEMPDIR . '/test.txt', 'Test contents');
27        file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
28        mkdir(SABRE_TEMPDIR . '/col');
29        file_put_contents(SABRE_TEMPDIR . 'col/test.txt', 'Test contents');
30
31    }
32
33    function tearDown() {
34
35        $cleanUp = array('test.txt','testput.txt','testcol','test2.txt','test3.txt','col/test.txt','col','col2/test.txt','col2');
36        foreach($cleanUp as $file) {
37            $tmpFile = SABRE_TEMPDIR . '/' . $file;
38            if (file_exists($tmpFile)) {
39
40                if (is_dir($tmpFile)) {
41                    rmdir($tmpFile);
42                } else {
43                    unlink($tmpFile);
44                }
45
46            }
47        }
48
49    }
50
51
52    function testCopyOverWrite() {
53
54        $serverVars = array(
55            'REQUEST_URI'    => '/test.txt',
56            'REQUEST_METHOD' => 'COPY',
57            'HTTP_DESTINATION' => '/test2.txt',
58        );
59
60        $request = HTTP\Sapi::createFromServerArray($serverVars);
61        $this->server->httpRequest = ($request);
62        $this->server->exec();
63
64        $this->assertEquals(204, $this->response->status, 'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
65        $this->assertEquals(array(
66                'X-Sabre-Version' => [Version::VERSION],
67                'Content-Length' => ['0'],
68            ),
69            $this->response->getHeaders()
70         );
71
72        $this->assertEquals('Test contents', file_get_contents(SABRE_TEMPDIR. '/test2.txt'));
73
74    }
75
76    function testCopyToSelf() {
77
78        $serverVars = array(
79            'REQUEST_URI'    => '/test.txt',
80            'REQUEST_METHOD' => 'COPY',
81            'HTTP_DESTINATION' => '/test.txt',
82        );
83
84        $request = HTTP\Sapi::createFromServerArray($serverVars);
85        $this->server->httpRequest = ($request);
86        $this->server->exec();
87
88        $this->assertEquals(403, $this->response->status, 'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
89        $this->assertEquals('Test contents', file_get_contents(SABRE_TEMPDIR. '/test.txt'));
90
91    }
92
93    function testNonExistantParent() {
94
95        $serverVars = array(
96            'REQUEST_URI'      => '/test.txt',
97            'REQUEST_METHOD'   => 'COPY',
98            'HTTP_DESTINATION' => '/testcol2/test2.txt',
99            'HTTP_OVERWRITE'   => 'F',
100        );
101
102        $request = HTTP\Sapi::createFromServerArray($serverVars);
103        $this->server->httpRequest = ($request);
104        $this->server->exec();
105
106        $this->assertEquals(array(
107                'X-Sabre-Version' => [Version::VERSION],
108                'Content-Type' => ['application/xml; charset=utf-8'],
109            ),
110            $this->response->getHeaders()
111         );
112
113        $this->assertEquals(409, $this->response->status);
114
115    }
116
117    function testRandomOverwriteHeader() {
118
119        $serverVars = array(
120            'REQUEST_URI'      => '/test.txt',
121            'REQUEST_METHOD'   => 'COPY',
122            'HTTP_DESTINATION' => '/testcol2/test2.txt',
123            'HTTP_OVERWRITE'   => 'SURE!',
124        );
125
126        $request = HTTP\Sapi::createFromServerArray($serverVars);
127        $this->server->httpRequest = ($request);
128        $this->server->exec();
129
130        $this->assertEquals(400, $this->response->status);
131
132    }
133
134    function testCopyDirectory() {
135
136        $serverVars = array(
137            'REQUEST_URI'    => '/col',
138            'REQUEST_METHOD' => 'COPY',
139            'HTTP_DESTINATION' => '/col2',
140        );
141
142        $request = HTTP\Sapi::createFromServerArray($serverVars);
143        $this->server->httpRequest = ($request);
144        $this->server->exec();
145
146        $this->assertEquals(201, $this->response->status, 'Full response: ' . $this->response->getBody(true));
147
148        $this->assertEquals(array(
149                'X-Sabre-Version' => [Version::VERSION],
150                'Content-Length' => ['0'],
151            ),
152            $this->response->getHeaders()
153         );
154
155        $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
156
157    }
158
159    function testSimpleCopyFile() {
160
161        $serverVars = array(
162            'REQUEST_URI'    => '/test.txt',
163            'REQUEST_METHOD' => 'COPY',
164            'HTTP_DESTINATION' => '/test3.txt',
165        );
166
167        $request = HTTP\Sapi::createFromServerArray($serverVars);
168        $this->server->httpRequest = ($request);
169        $this->server->exec();
170
171        $this->assertEquals(array(
172            'X-Sabre-Version' => [Version::VERSION],
173            'Content-Length' => ['0'],
174            ),
175            $this->response->getHeaders()
176         );
177
178        $this->assertEquals(201, $this->response->status);
179        $this->assertEquals('Test contents', file_get_contents(SABRE_TEMPDIR . '/test3.txt'));
180
181    }
182
183    function testSimpleCopyCollection() {
184
185        $serverVars = array(
186            'REQUEST_URI'    => '/col',
187            'REQUEST_METHOD' => 'COPY',
188            'HTTP_DESTINATION' => '/col2',
189        );
190
191        $request = HTTP\Sapi::createFromServerArray($serverVars);
192        $this->server->httpRequest = ($request);
193        $this->server->exec();
194
195        $this->assertEquals(201, $this->response->status, 'Incorrect status received. Full response body: ' . $this->response->body);
196
197        $this->assertEquals(array(
198            'X-Sabre-Version' => [Version::VERSION],
199            'Content-Length' => ['0'],
200            ),
201            $this->response->getHeaders()
202         );
203
204
205        $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/col2/test.txt'));
206
207    }
208
209}
210