]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/TemporaryFileFilterTest.php
removed community home addon
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / TemporaryFileFilterTest.php
1 <?php
2
3 class Sabre_DAV_TemporaryFileFilterTest extends Sabre_DAV_AbstractServer {
4
5     function setUp() {
6
7         parent::setUp();
8         $plugin = new Sabre_DAV_TemporaryFileFilterPlugin(SABRE_TEMPDIR . '/tff');
9         $this->server->addPlugin($plugin);
10
11     }
12
13     function testPutNormal() {
14
15         $serverVars = array(
16             'REQUEST_URI'    => '/testput.txt',
17             'REQUEST_METHOD' => 'PUT',
18         );
19
20         $request = new Sabre_HTTP_Request($serverVars);
21         $request->setBody('Testing new file');
22         $this->server->httpRequest = ($request);
23         $this->server->exec();
24
25         $this->assertEquals('', $this->response->body);
26         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
27         $this->assertEquals('0', $this->response->headers['Content-Length']);
28
29         $this->assertEquals('Testing new file',file_get_contents(SABRE_TEMPDIR . '/testput.txt'));
30
31     }
32
33     function testPutTemp() {
34
35         // mimicking an OS/X resource fork
36         $serverVars = array(
37             'REQUEST_URI'    => '/._testput.txt',
38             'REQUEST_METHOD' => 'PUT',
39         );
40
41         $request = new Sabre_HTTP_Request($serverVars);
42         $request->setBody('Testing new file');
43         $this->server->httpRequest = ($request);
44         $this->server->exec();
45
46         $this->assertEquals('', $this->response->body);
47         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
48         $this->assertEquals(array(
49             'X-Sabre-Temp' => 'true',
50         ),$this->response->headers);
51
52         $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'),'._testput.txt should not exist in the regular file structure.');
53
54     }
55
56     function testPutTempIfNoneMatch() {
57
58         // mimicking an OS/X resource fork
59         $serverVars = array(
60             'REQUEST_URI'        => '/._testput.txt',
61             'REQUEST_METHOD'     => 'PUT',
62             'HTTP_IF_NONE_MATCH' => '*',
63         );
64
65         $request = new Sabre_HTTP_Request($serverVars);
66         $request->setBody('Testing new file');
67         $this->server->httpRequest = ($request);
68         $this->server->exec();
69
70         $this->assertEquals('', $this->response->body);
71         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
72         $this->assertEquals(array(
73             'X-Sabre-Temp' => 'true',
74         ),$this->response->headers);
75
76         $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testput.txt'),'._testput.txt should not exist in the regular file structure.');
77
78
79         $this->server->exec();
80
81         $this->assertEquals('HTTP/1.1 412 Precondition failed',$this->response->status);
82         $this->assertEquals(array(
83             'X-Sabre-Temp' => 'true',
84             'Content-Type' => 'application/xml; charset=utf-8',
85         ),$this->response->headers);
86
87     }
88
89     function testPutGet() {
90
91         // mimicking an OS/X resource fork
92         $serverVars = array(
93             'REQUEST_URI'    => '/._testput.txt',
94             'REQUEST_METHOD' => 'PUT',
95         );
96
97         $request = new Sabre_HTTP_Request($serverVars);
98         $request->setBody('Testing new file');
99         $this->server->httpRequest = ($request);
100         $this->server->exec();
101
102         $this->assertEquals('', $this->response->body);
103         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
104         $this->assertEquals(array(
105             'X-Sabre-Temp' => 'true',
106         ),$this->response->headers);
107
108         $serverVars = array(
109             'REQUEST_URI'    => '/._testput.txt',
110             'REQUEST_METHOD' => 'GET',
111         );
112
113         $request = new Sabre_HTTP_Request($serverVars);
114         $this->server->httpRequest = ($request);
115         $this->server->exec();
116
117         $this->assertEquals('HTTP/1.1 200 OK',$this->response->status);
118         $this->assertEquals(array(
119             'X-Sabre-Temp' => 'true',
120             'Content-Length' => 16,
121             'Content-Type' => 'application/octet-stream',
122         ),$this->response->headers);
123
124         $this->assertEquals('Testing new file',stream_get_contents($this->response->body));
125
126     }
127
128     function testLockNonExistant() {
129
130         mkdir(SABRE_TEMPDIR . '/locksdir');
131         $locksBackend = new Sabre_DAV_Locks_Backend_FS(SABRE_TEMPDIR . '/locksdir');
132         $locksPlugin = new Sabre_DAV_Locks_Plugin($locksBackend);
133         $this->server->addPlugin($locksPlugin);
134
135         // mimicking an OS/X resource fork
136         $serverVars = array(
137             'REQUEST_URI'    => '/._testlock.txt',
138             'REQUEST_METHOD' => 'LOCK',
139         );
140
141         $request = new Sabre_HTTP_Request($serverVars);
142
143         $request->setBody('<?xml version="1.0"?>
144 <D:lockinfo xmlns:D="DAV:">
145     <D:lockscope><D:exclusive/></D:lockscope>
146     <D:locktype><D:write/></D:locktype>
147     <D:owner>
148         <D:href>http://example.org/~ejw/contact.html</D:href>
149     </D:owner>
150 </D:lockinfo>');
151
152         $this->server->httpRequest = ($request);
153         $this->server->exec();
154
155         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
156         $this->assertEquals('application/xml; charset=utf-8',$this->response->headers['Content-Type']);
157         $this->assertTrue(preg_match('/^<opaquelocktoken:(.*)>$/',$this->response->headers['Lock-Token'])===1,'We did not get a valid Locktoken back (' . $this->response->headers['Lock-Token'] . ')');
158         $this->assertEquals('true',$this->response->headers['X-Sabre-Temp']);
159
160         $this->assertFalse(file_exists(SABRE_TEMPDIR . '/._testlock.txt'),'._testlock.txt should not exist in the regular file structure.');
161
162     }
163
164     function testPutDelete() {
165
166         // mimicking an OS/X resource fork
167         $serverVars = array(
168             'REQUEST_URI'    => '/._testput.txt',
169             'REQUEST_METHOD' => 'PUT',
170         );
171
172         $request = new Sabre_HTTP_Request($serverVars);
173         $request->setBody('Testing new file');
174         $this->server->httpRequest = ($request);
175         $this->server->exec();
176
177         $this->assertEquals('', $this->response->body);
178         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
179         $this->assertEquals(array(
180             'X-Sabre-Temp' => 'true',
181         ),$this->response->headers);
182
183         $serverVars = array(
184             'REQUEST_URI'    => '/._testput.txt',
185             'REQUEST_METHOD' => 'DELETE',
186         );
187
188         $request = new Sabre_HTTP_Request($serverVars);
189         $this->server->httpRequest = ($request);
190         $this->server->exec();
191
192         $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status, "Incorrect status code received. Full body:\n". $this->response->body);
193         $this->assertEquals(array(
194             'X-Sabre-Temp' => 'true',
195         ),$this->response->headers);
196
197         $this->assertEquals('',$this->response->body);
198
199     }
200
201     function testPutPropfind() {
202
203         // mimicking an OS/X resource fork
204         $serverVars = array(
205             'REQUEST_URI'    => '/._testput.txt',
206             'REQUEST_METHOD' => 'PUT',
207         );
208
209         $request = new Sabre_HTTP_Request($serverVars);
210         $request->setBody('Testing new file');
211         $this->server->httpRequest = ($request);
212         $this->server->exec();
213
214         $this->assertEquals('', $this->response->body);
215         $this->assertEquals('HTTP/1.1 201 Created',$this->response->status);
216         $this->assertEquals(array(
217             'X-Sabre-Temp' => 'true',
218         ),$this->response->headers);
219
220         $serverVars = array(
221             'REQUEST_URI'    => '/._testput.txt',
222             'REQUEST_METHOD' => 'PROPFIND',
223         );
224
225         $request = new Sabre_HTTP_Request($serverVars);
226         $request->setBody('');
227         $this->server->httpRequest = ($request);
228         $this->server->exec();
229
230         $this->assertEquals('HTTP/1.1 207 Multi-Status',$this->response->status,'Incorrect status code returned. Body: ' . $this->response->body);
231         $this->assertEquals(array(
232             'X-Sabre-Temp' => 'true',
233             'Content-Type' => 'application/xml; charset=utf-8',
234         ),$this->response->headers);
235
236         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"DAV:\"",$this->response->body);
237         $xml = simplexml_load_string($body);
238         $xml->registerXPathNamespace('d','DAV:');
239
240         list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
241         $this->assertEquals('/._testput.txt',(string)$data,'href element should have been /._testput.txt');
242
243         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
244         $this->assertEquals(1,count($data));
245
246     }
247
248 }