1<?php
2
3namespace Sabre\DAV\FSExt;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/DAV/AbstractServer.php';
9
10class ServerTest extends DAV\AbstractServer{
11
12    protected function getRootNode() {
13
14        return new Directory($this->tempDir);
15
16    }
17
18    function testGet() {
19
20        $request = new HTTP\Request('GET', '/test.txt');
21        $filename = $this->tempDir . '/test.txt';
22        $this->server->httpRequest = $request;
23        $this->server->exec();
24
25        $this->assertEquals(200, $this->response->getStatus(), 'Invalid status code received.');
26        $this->assertEquals([
27            'X-Sabre-Version' => [DAV\Version::VERSION],
28            'Content-Type'    => ['application/octet-stream'],
29            'Content-Length'  => [13],
30            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($filename)))],
31            'ETag'            => ['"' . sha1(fileinode($filename ) . filesize($filename) . filemtime($filename)) . '"'],
32            ],
33            $this->response->getHeaders()
34         );
35
36
37        $this->assertEquals('Test contents', stream_get_contents($this->response->body));
38
39    }
40
41    function testHEAD() {
42
43        $request = new HTTP\Request('HEAD', '/test.txt');
44        $filename = $this->tempDir . '/test.txt';
45        $this->server->httpRequest = ($request);
46        $this->server->exec();
47
48        $this->assertEquals([
49            'X-Sabre-Version' => [DAV\Version::VERSION],
50            'Content-Type'    => ['application/octet-stream'],
51            'Content-Length'  => [13],
52            'Last-Modified'   => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))],
53            'ETag'            => ['"' . sha1(fileinode($filename ) . filesize($filename) . filemtime($filename)) . '"'],
54            ],
55            $this->response->getHeaders()
56         );
57
58        $this->assertEquals(200,$this->response->status);
59        $this->assertEquals('', $this->response->body);
60
61    }
62
63    function testPut() {
64
65        $request = new HTTP\Request('PUT', '/testput.txt');
66        $filename = $this->tempDir . '/testput.txt';
67        $request->setBody('Testing new file');
68        $this->server->httpRequest = ($request);
69        $this->server->exec();
70
71        $this->assertEquals([
72            'X-Sabre-Version' => [DAV\Version::VERSION],
73            'Content-Length'  => [0],
74            'ETag'            => ['"' . sha1(fileinode($filename ) . filesize($filename) . filemtime($filename)) . '"'],
75        ], $this->response->getHeaders());
76
77        $this->assertEquals(201, $this->response->status);
78        $this->assertEquals('', $this->response->body);
79        $this->assertEquals('Testing new file',file_get_contents($filename));
80
81    }
82
83    function testPutAlreadyExists() {
84
85        $request = new HTTP\Request('PUT', '/test.txt', ['If-None-Match' => '*']);
86        $request->setBody('Testing new file');
87        $this->server->httpRequest = ($request);
88        $this->server->exec();
89
90        $this->assertEquals([
91            'X-Sabre-Version' => [DAV\Version::VERSION],
92            'Content-Type' => ['application/xml; charset=utf-8'],
93        ],$this->response->getHeaders());
94
95        $this->assertEquals(412, $this->response->status);
96        $this->assertNotEquals('Testing new file',file_get_contents($this->tempDir . '/test.txt'));
97
98    }
99
100    function testMkcol() {
101
102        $request = new HTTP\Request('MKCOL', '/testcol');
103        $this->server->httpRequest = ($request);
104        $this->server->exec();
105
106        $this->assertEquals([
107            'X-Sabre-Version' => [DAV\Version::VERSION],
108            'Content-Length' => ['0'],
109        ],$this->response->getHeaders());
110
111        $this->assertEquals(201, $this->response->status);
112        $this->assertEquals('', $this->response->body);
113        $this->assertTrue(is_dir($this->tempDir . '/testcol'));
114
115    }
116
117    function testPutUpdate() {
118
119        $request = new HTTP\Request('PUT', '/test.txt');
120        $request->setBody('Testing updated file');
121        $this->server->httpRequest = ($request);
122        $this->server->exec();
123
124        $this->assertEquals('0', $this->response->getHeader('Content-Length'));
125
126        $this->assertEquals(204, $this->response->status);
127        $this->assertEquals('', $this->response->body);
128        $this->assertEquals('Testing updated file',file_get_contents($this->tempDir . '/test.txt'));
129
130    }
131
132    function testDelete() {
133
134        $request = new HTTP\Request('DELETE', '/test.txt');
135        $this->server->httpRequest = ($request);
136        $this->server->exec();
137
138        $this->assertEquals([
139            'X-Sabre-Version' => [DAV\Version::VERSION],
140            'Content-Length' => ['0'],
141        ],$this->response->getHeaders());
142
143        $this->assertEquals(204, $this->response->status);
144        $this->assertEquals('', $this->response->body);
145        $this->assertFalse(file_exists($this->tempDir . '/test.txt'));
146
147    }
148
149    function testDeleteDirectory() {
150
151        mkdir($this->tempDir.'/testcol');
152        file_put_contents($this->tempDir.'/testcol/test.txt','Hi! I\'m a file with a short lifespan');
153
154        $request = new HTTP\Request('DELETE', '/testcol');
155        $this->server->httpRequest = ($request);
156        $this->server->exec();
157
158        $this->assertEquals([
159            'X-Sabre-Version' => [DAV\Version::VERSION],
160            'Content-Length' => ['0'],
161        ],$this->response->getHeaders());
162        $this->assertEquals(204, $this->response->status);
163        $this->assertEquals('', $this->response->body);
164        $this->assertFalse(file_exists($this->tempDir . '/col'));
165
166    }
167
168    function testOptions() {
169
170        $request = new HTTP\Request('OPTIONS', '/');
171        $this->server->httpRequest = ($request);
172        $this->server->exec();
173
174        $this->assertEquals([
175            'DAV'            => ['1, 3, extended-mkcol'],
176            'MS-Author-Via'  => ['DAV'],
177            'Allow'          => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT'],
178            'Accept-Ranges'  => ['bytes'],
179            'Content-Length' => ['0'],
180            'X-Sabre-Version'=> [DAV\Version::VERSION],
181        ], $this->response->getHeaders());
182
183        $this->assertEquals(200, $this->response->status);
184        $this->assertEquals('', $this->response->body);
185
186    }
187
188    function testMove() {
189
190        mkdir($this->tempDir.'/testcol');
191
192        $request = new HTTP\Request('MOVE', '/test.txt', ['Destination' => '/testcol/test2.txt']);
193        $this->server->httpRequest = ($request);
194        $this->server->exec();
195
196        $this->assertEquals(201, $this->response->status);
197        $this->assertEquals('', $this->response->body);
198
199        $this->assertEquals([
200            'Content-Length' => ['0'],
201            'X-Sabre-Version'=> [DAV\Version::VERSION],
202        ],$this->response->getHeaders());
203
204        $this->assertTrue(
205            is_file($this->tempDir . '/testcol/test2.txt')
206        );
207
208
209    }
210
211    /**
212     * This test checks if it's possible to move a non-FSExt collection into a
213     * FSExt collection.
214     *
215     * The moveInto function *should* ignore the object and let sabredav itself
216     * execute the slow move.
217     */
218    function testMoveOtherObject() {
219
220        mkdir($this->tempDir.'/tree1');
221        mkdir($this->tempDir.'/tree2');
222
223        $tree = new DAV\Tree(new DAV\SimpleCollection('root', [
224            new DAV\FS\Directory($this->tempDir . '/tree1'),
225            new DAV\FSExt\Directory($this->tempDir . '/tree2'),
226        ]));
227        $this->server->tree = $tree;
228
229        $request = new HTTP\Request('MOVE', '/tree1', ['Destination' => '/tree2/tree1']);
230        $this->server->httpRequest = ($request);
231        $this->server->exec();
232
233        $this->assertEquals(201, $this->response->status);
234        $this->assertEquals('', $this->response->body);
235
236        $this->assertEquals([
237            'Content-Length' => ['0'],
238            'X-Sabre-Version'=> [DAV\Version::VERSION],
239        ],$this->response->getHeaders());
240
241        $this->assertTrue(
242            is_dir($this->tempDir . '/tree2/tree1')
243        );
244
245    }
246}
247