1<?php
2
3namespace Sabre\CardDAV;
4
5use Sabre\HTTP;
6use Sabre\DAV;
7
8require_once 'Sabre/CardDAV/AbstractPluginTest.php';
9require_once 'Sabre/HTTP/ResponseMock.php';
10
11class AddressBookQueryTest extends AbstractPluginTest {
12
13    function testQuery() {
14
15        $request = HTTP\Sapi::createFromServerArray(array(
16            'REQUEST_METHOD' => 'REPORT',
17            'REQUEST_URI' => '/addressbooks/user1/book1',
18            'HTTP_DEPTH' => '1',
19        ));
20
21        $request->setBody(
22'<?xml version="1.0"?>
23<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
24    <d:prop>
25      <d:getetag />
26    </d:prop>
27    <c:filter>
28        <c:prop-filter name="uid" />
29    </c:filter>
30</c:addressbook-query>'
31            );
32
33        $response = new HTTP\ResponseMock();
34
35        $this->server->httpRequest = $request;
36        $this->server->httpResponse = $response;
37
38        $this->server->exec();
39
40        $this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);
41
42        // using the client for parsing
43        $client = new DAV\Client(array('baseUri'=>'/'));
44
45        $result = $client->parseMultiStatus($response->body);
46
47        $this->assertEquals(array(
48            '/addressbooks/user1/book1/card1' => array(
49                200 => array(
50                    '{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
51                ),
52             ),
53            '/addressbooks/user1/book1/card2' => array(
54                404 => array(
55                    '{DAV:}getetag' => null,
56                ),
57            )
58        ), $result);
59
60
61    }
62
63    function testQueryDepth0() {
64
65        $request = HTTP\Sapi::createFromServerArray(array(
66            'REQUEST_METHOD' => 'REPORT',
67            'REQUEST_URI' => '/addressbooks/user1/book1/card1',
68            'HTTP_DEPTH' => '0',
69        ));
70
71        $request->setBody(
72'<?xml version="1.0"?>
73<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
74    <d:prop>
75      <d:getetag />
76    </d:prop>
77    <c:filter>
78        <c:prop-filter name="uid" />
79    </c:filter>
80</c:addressbook-query>'
81            );
82
83        $response = new HTTP\ResponseMock();
84
85        $this->server->httpRequest = $request;
86        $this->server->httpResponse = $response;
87
88        $this->server->exec();
89
90        $this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);
91
92        // using the client for parsing
93        $client = new DAV\Client(array('baseUri'=>'/'));
94
95        $result = $client->parseMultiStatus($response->body);
96
97        $this->assertEquals(array(
98            '/addressbooks/user1/book1/card1' => array(
99                200 => array(
100                    '{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
101                ),
102             ),
103        ), $result);
104
105
106    }
107
108    function testQueryNoMatch() {
109
110        $request = HTTP\Sapi::createFromServerArray(array(
111            'REQUEST_METHOD' => 'REPORT',
112            'REQUEST_URI' => '/addressbooks/user1/book1',
113            'HTTP_DEPTH' => '1',
114        ));
115
116        $request->setBody(
117'<?xml version="1.0"?>
118<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
119    <d:prop>
120      <d:getetag />
121    </d:prop>
122    <c:filter>
123        <c:prop-filter name="email" />
124    </c:filter>
125</c:addressbook-query>'
126            );
127
128        $response = new HTTP\ResponseMock();
129
130        $this->server->httpRequest = $request;
131        $this->server->httpResponse = $response;
132
133        $this->server->exec();
134
135        $this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);
136
137        // using the client for parsing
138        $client = new DAV\Client(array('baseUri'=>'/'));
139
140        $result = $client->parseMultiStatus($response->body);
141
142        $this->assertEquals(array(), $result);
143
144    }
145
146    function testQueryLimit() {
147
148        $request = HTTP\Sapi::createFromServerArray(array(
149            'REQUEST_METHOD' => 'REPORT',
150            'REQUEST_URI' => '/addressbooks/user1/book1',
151            'HTTP_DEPTH' => '1',
152        ));
153
154        $request->setBody(
155'<?xml version="1.0"?>
156<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
157    <d:prop>
158      <d:getetag />
159    </d:prop>
160    <c:filter>
161        <c:prop-filter name="uid" />
162    </c:filter>
163    <c:limit><c:nresults>1</c:nresults></c:limit>
164</c:addressbook-query>'
165            );
166
167        $response = new HTTP\ResponseMock();
168
169        $this->server->httpRequest = $request;
170        $this->server->httpResponse = $response;
171
172        $this->server->exec();
173
174        $this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);
175
176        // using the client for parsing
177        $client = new DAV\Client(array('baseUri'=>'/'));
178
179        $result = $client->parseMultiStatus($response->body);
180
181        $this->assertEquals(array(
182            '/addressbooks/user1/book1/card1' => array(
183                200 => array(
184                    '{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD"). '"',
185                ),
186             ),
187        ), $result);
188
189
190    }
191
192    function testJson() {
193
194        $request = new HTTP\Request(
195            'REPORT',
196            '/addressbooks/user1/book1/card1',
197            ['Depth' => '0']
198        );
199
200        $request->setBody(
201'<?xml version="1.0"?>
202<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
203    <d:prop>
204      <c:address-data content-type="application/vcard+json" />
205      <d:getetag />
206    </d:prop>
207</c:addressbook-query>'
208            );
209
210        $response = new HTTP\ResponseMock();
211
212        $this->server->httpRequest = $request;
213        $this->server->httpResponse = $response;
214
215        $this->server->exec();
216
217        $this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);
218
219        // using the client for parsing
220        $client = new DAV\Client(array('baseUri'=>'/'));
221
222        $result = $client->parseMultiStatus($response->body);
223
224        $vobjVersion = \Sabre\VObject\Version::VERSION;
225
226        $this->assertEquals(array(
227            '/addressbooks/user1/book1/card1' => array(
228                200 => array(
229                    '{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD"). '"',
230                    '{urn:ietf:params:xml:ns:carddav}address-data' => '["vcard",[["version",{},"text","4.0"],["prodid",{},"text","-\/\/Sabre\/\/Sabre VObject ' . $vobjVersion . '\/\/EN"],["uid",{},"text","12345"]]]',
231                ),
232             ),
233        ), $result);
234
235    }
236
237    function testVCard4() {
238
239        $request = new HTTP\Request(
240            'REPORT',
241            '/addressbooks/user1/book1/card1',
242            ['Depth' => '0']
243        );
244
245        $request->setBody(
246'<?xml version="1.0"?>
247<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
248    <d:prop>
249      <c:address-data content-type="text/vcard" version="4.0" />
250      <d:getetag />
251    </d:prop>
252</c:addressbook-query>'
253            );
254
255        $response = new HTTP\ResponseMock();
256
257        $this->server->httpRequest = $request;
258        $this->server->httpResponse = $response;
259
260        $this->server->exec();
261
262        $this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);
263
264        // using the client for parsing
265        $client = new DAV\Client(array('baseUri'=>'/'));
266
267        $result = $client->parseMultiStatus($response->body);
268
269        $vobjVersion = \Sabre\VObject\Version::VERSION;
270
271        $this->assertEquals(array(
272            '/addressbooks/user1/book1/card1' => array(
273                200 => array(
274                    '{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD"). '"',
275                    '{urn:ietf:params:xml:ns:carddav}address-data' => "BEGIN:VCARD\r\nVERSION:4.0\r\nPRODID:-//Sabre//Sabre VObject $vobjVersion//EN\r\nUID:12345\r\nEND:VCARD\r\n",
276                ),
277             ),
278        ), $result);
279
280    }
281
282    function testAddressBookDepth0() {
283
284        $request = new HTTP\Request(
285            'REPORT',
286            '/addressbooks/user1/book1',
287            ['Depth' => '0']
288        );
289
290        $request->setBody(
291'<?xml version="1.0"?>
292<c:addressbook-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
293    <d:prop>
294      <c:address-data content-type="application/vcard+json" />
295      <d:getetag />
296    </d:prop>
297</c:addressbook-query>'
298            );
299
300        $response = new HTTP\ResponseMock();
301
302        $this->server->httpRequest = $request;
303        $this->server->httpResponse = $response;
304
305        $this->server->exec();
306
307        $this->assertEquals(415, $response->status, 'Incorrect status code. Full response body:' . $response->body);
308
309    }
310}
311