forked from sabre-io/dav
/
ServerPropsInfiniteDepthTest.php
217 lines (160 loc) · 8.11 KB
/
ServerPropsInfiniteDepthTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace Sabre\DAV;
use Sabre\HTTP;
require_once 'Sabre/DAV/AbstractServer.php';
class ServerPropsInfiniteDepthTest extends AbstractServer {
protected function getRootNode() {
return new FSExt\Directory(SABRE_TEMPDIR);
}
function setUp() {
if (file_exists(SABRE_TEMPDIR . '../.sabredav')) unlink(SABRE_TEMPDIR . '../.sabredav');
parent::setUp();
file_put_contents(SABRE_TEMPDIR . '/test2.txt', 'Test contents2');
mkdir(SABRE_TEMPDIR . '/col');
mkdir(SABRE_TEMPDIR . '/col/col');
file_put_contents(SABRE_TEMPDIR . 'col/col/test.txt', 'Test contents');
$this->server->addPlugin(new Locks\Plugin(new Locks\Backend\File(SABRE_TEMPDIR . '/.locksdb')));
$this->server->enablePropfindDepthInfinity = true;
}
function tearDown() {
parent::tearDown();
if (file_exists(SABRE_TEMPDIR . '../.locksdb')) unlink(SABRE_TEMPDIR . '../.locksdb');
}
private function sendRequest($body) {
$request = new HTTP\Request('PROPFIND', '/', ['Depth' => 'infinity']);
$request->setBody($body);
$this->server->httpRequest = $request;
$this->server->exec();
}
function testPropFindEmptyBody() {
$this->sendRequest("");
$this->assertEquals(207, $this->response->status, 'Incorrect status received. Full response body: ' . $this->response->getBodyAsString());
$this->assertEquals([
'X-Sabre-Version' => [Version::VERSION],
'Content-Type' => ['application/xml; charset=utf-8'],
'DAV' => ['1, 3, extended-mkcol, 2'],
'Vary' => ['Brief,Prefer'],
],
$this->response->getHeaders()
);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
$this->assertEquals('/', (string)$data, 'href element should have been /');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
// 8 resources are to be returned: /, col, col/col, col/col/test.txt, dir, dir/child.txt, test.txt and test2.txt
$this->assertEquals(8, count($data));
}
function testSupportedLocks() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:supportedlock />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = $this->response->getBodyAsString();
$this->assertEquals(207, $this->response->getStatus(), $body);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
$this->assertEquals(16, count($data), 'We expected sixteen \'d:lockentry\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope');
$this->assertEquals(16, count($data), 'We expected sixteen \'d:lockscope\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype');
$this->assertEquals(16, count($data), 'We expected sixteen \'d:locktype\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:shared');
$this->assertEquals(8, count($data), 'We expected eight \'d:shared\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:exclusive');
$this->assertEquals(8, count($data), 'We expected eight \'d:exclusive\' tags');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype/d:write');
$this->assertEquals(16, count($data), 'We expected sixteen \'d:write\' tags');
}
function testLockDiscovery() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:lockdiscovery />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
$this->assertEquals(8, count($data), 'We expected eight \'d:lockdiscovery\' tags');
}
function testUnknownProperty() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:macaroni />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$pathTests = [
'/d:multistatus',
'/d:multistatus/d:response',
'/d:multistatus/d:response/d:propstat',
'/d:multistatus/d:response/d:propstat/d:status',
'/d:multistatus/d:response/d:propstat/d:prop',
'/d:multistatus/d:response/d:propstat/d:prop/d:macaroni',
];
foreach ($pathTests as $test) {
$this->assertTrue(count($xml->xpath($test)) == true, 'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
}
$val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
$this->assertEquals(8, count($val), $body);
$this->assertEquals('HTTP/1.1 404 Not Found', (string)$val[0]);
}
function testFilesThatAreSiblingsOfDirectoriesShouldBeReportedAsFiles() {
$xml = '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:">
<d:prop>
<d:resourcetype />
</d:prop>
</d:propfind>';
$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$pathTests = [
'/d:multistatus',
'/d:multistatus/d:response',
'/d:multistatus/d:response/d:href',
'/d:multistatus/d:response/d:propstat',
'/d:multistatus/d:response/d:propstat/d:status',
'/d:multistatus/d:response/d:propstat/d:prop',
'/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype',
];
$hrefPaths = [];
foreach ($pathTests as $test) {
$this->assertTrue(count($xml->xpath($test)) == true, 'We expected the ' . $test . ' element to appear in the response, we got: ' . $body);
if ($test === '/d:multistatus/d:response/d:href') {
foreach ($xml->xpath($test) as $thing) {
/** @var \SimpleXMLElement $thing */
$hrefPaths[] = strip_tags($thing->asXML());
}
} elseif ($test === '/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype') {
$count = 0;
foreach ($xml->xpath($test) as $thing) {
/** @var \SimpleXMLElement $thing */
if (substr($hrefPaths[$count], -4) !== '.txt') {
$this->assertEquals('<d:resourcetype><d:collection/></d:resourcetype>', $thing->asXML(), 'Path ' . $hrefPaths[$count] . ' is not reported as a directory');
} else {
$this->assertEquals('<d:resourcetype/>', $thing->asXML(), 'Path ' . $hrefPaths[$count] . ' is not reported as a file');
}
$count++;
}
}
}
$val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
$this->assertEquals(8, count($val), $body);
$this->assertEquals('HTTP/1.1 200 OK', (string)$val[0]);
}
}