1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\HTTP;
6
7class TemporaryFileFilterTest extends AbstractServer {
8
9    function setUp() {
10
11        parent::setUp();
12        $plugin = new TemporaryFileFilterPlugin(SABRE_TEMPDIR . '/tff');
13        $this->server->addPlugin($plugin);
14
15    }
16
17    function testPutNormal() {
18
19        $request = new HTTP\Request('PUT', '/testput.txt', [], 'Testing new file');
20
21        $this->server->httpRequest = ($request);
22        $this->server->exec();
23
24        $this->assertEquals('', $this->response->body);
25        $this->assertEquals(201, $this->response->status);
26        $this->assertEquals('0', $this->response->getHeader('Content-Length'));
27
28        $this->assertEquals('Testing new file',file_get_contents(SABRE_TEMPDIR . '/testput.txt'));
29
30    }
31
32    function testPutTemp() {
33
34        // mimicking an OS/X resource fork
35        $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
36
37        $this->server->httpRequest = ($request);
38        $this->server->exec();
39
40        $this->assertEquals('', $this->response->body);
41        $this->assertEquals(201, $this->response->status);
42        $this->assertEquals(array(
43            'X-Sabre-Temp' => ['true'],
44        ),$this->response->getHeaders());
45
46        $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'),'._testput.txt should not exist in the regular file structure.');
47
48    }
49
50    function testPutTempIfNoneMatch() {
51
52        // mimicking an OS/X resource fork
53        $request = new HTTP\Request('PUT', '/._testput.txt', ['If-None-Match' => '*'], 'Testing new file');
54
55        $this->server->httpRequest = ($request);
56        $this->server->exec();
57
58        $this->assertEquals('', $this->response->body);
59        $this->assertEquals(201, $this->response->status);
60        $this->assertEquals(array(
61            'X-Sabre-Temp' => ['true'],
62        ),$this->response->getHeaders());
63
64        $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'),'._testput.txt should not exist in the regular file structure.');
65
66
67        $this->server->exec();
68
69        $this->assertEquals(412, $this->response->status);
70        $this->assertEquals(array(
71            'X-Sabre-Temp' => ['true'],
72            'Content-Type' => ['application/xml; charset=utf-8'],
73        ),$this->response->getHeaders());
74
75    }
76
77    function testPutGet() {
78
79        // mimicking an OS/X resource fork
80        $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
81        $this->server->httpRequest = ($request);
82        $this->server->exec();
83
84        $this->assertEquals('', $this->response->body);
85        $this->assertEquals(201, $this->response->status);
86        $this->assertEquals(array(
87            'X-Sabre-Temp' => ['true'],
88        ),$this->response->getHeaders());
89
90        $request = new HTTP\Request('GET', '/._testput.txt');
91
92        $this->server->httpRequest = $request;
93        $this->server->exec();
94
95        $this->assertEquals(200, $this->response->status);
96        $this->assertEquals(array(
97            'X-Sabre-Temp' => ['true'],
98            'Content-Length' => [16],
99            'Content-Type' => ['application/octet-stream'],
100        ),$this->response->getHeaders());
101
102        $this->assertEquals('Testing new file',stream_get_contents($this->response->body));
103
104    }
105
106    function testLockNonExistant() {
107
108        mkdir(SABRE_TEMPDIR . '/locksdir');
109        $locksBackend = new Locks\Backend\File(SABRE_TEMPDIR . '/locks');
110        $locksPlugin = new Locks\Plugin($locksBackend);
111        $this->server->addPlugin($locksPlugin);
112
113        // mimicking an OS/X resource fork
114        $request = new HTTP\Request('LOCK', '/._testput.txt');
115        $request->setBody('<?xml version="1.0"?>
116<D:lockinfo xmlns:D="DAV:">
117    <D:lockscope><D:exclusive/></D:lockscope>
118    <D:locktype><D:write/></D:locktype>
119    <D:owner>
120        <D:href>http://example.org/~ejw/contact.html</D:href>
121    </D:owner>
122</D:lockinfo>');
123
124        $this->server->httpRequest = ($request);
125        $this->server->exec();
126
127        $this->assertEquals(201, $this->response->status);
128        $this->assertEquals('application/xml; charset=utf-8',$this->response->getHeader('Content-Type'));
129        $this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->getHeader('Lock-Token'))===1,'We did not get a valid Locktoken back (' . $this->response->getHeader('Lock-Token') . ')');
130        $this->assertEquals('true',$this->response->getHeader('X-Sabre-Temp'));
131
132        $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testlock.txt'),'._testlock.txt should not exist in the regular file structure.');
133
134    }
135
136    function testPutDelete() {
137
138        // mimicking an OS/X resource fork
139        $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
140
141        $this->server->httpRequest = $request;
142        $this->server->exec();
143
144        $this->assertEquals('', $this->response->body);
145        $this->assertEquals(201, $this->response->status);
146        $this->assertEquals(array(
147            'X-Sabre-Temp' => ['true'],
148        ),$this->response->getHeaders());
149
150        $request = new HTTP\Request('DELETE', '/._testput.txt');
151        $this->server->httpRequest = $request;
152        $this->server->exec();
153
154        $this->assertEquals(204, $this->response->status, "Incorrect status code received. Full body:\n". $this->response->body);
155        $this->assertEquals(array(
156            'X-Sabre-Temp' => ['true'],
157        ),$this->response->getHeaders());
158
159        $this->assertEquals('',$this->response->body);
160
161    }
162
163    function testPutPropfind() {
164
165        // mimicking an OS/X resource fork
166        $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
167        $this->server->httpRequest = $request;
168        $this->server->exec();
169
170        $this->assertEquals('', $this->response->body);
171        $this->assertEquals(201, $this->response->status);
172        $this->assertEquals(array(
173            'X-Sabre-Temp' => ['true'],
174        ),$this->response->getHeaders());
175
176        $request = new HTTP\Request('PROPFIND', '/._testput.txt');
177
178        $this->server->httpRequest = ($request);
179        $this->server->exec();
180
181        $this->assertEquals(207, $this->response->status,'Incorrect status code returned. Body: ' . $this->response->body);
182        $this->assertEquals(array(
183            'X-Sabre-Temp' => ['true'],
184            'Content-Type' => ['application/xml; charset=utf-8'],
185        ),$this->response->getHeaders());
186
187        $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
188        $xml = simplexml_load_string($body);
189        $xml->registerXPathNamespace('d','urn:DAV');
190
191        list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
192        $this->assertEquals('/._testput.txt',(string)$data,'href element should have been /._testput.txt');
193
194        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
195        $this->assertEquals(1,count($data));
196
197    }
198
199}
200