]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/CardDAV/AddressBookQueryParserTest.php
54fa53449b60049d63f4ccc100779bf4e460ab64
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / CardDAV / AddressBookQueryParserTest.php
1 <?php
2
3 class Sabre_CardDAV_AddressBookQueryParserTest extends PHPUnit_Framework_TestCase {
4
5     function parse($xml) {
6
7         $xml = implode("\n", $xml);
8         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
9
10         $q = new Sabre_CardDAV_AddressBookQueryParser($dom);
11         $q->parse();
12         return $q;
13
14     }
15
16     function testFilterBasic() {
17
18         $xml = array(
19             '<?xml version="1.0"?>',
20             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
21             '   <d:prop>',
22             '      <d:foo />',
23             '   </d:prop>',
24             '   <c:filter>',
25             '     <c:prop-filter name="NICKNAME" />',
26             '   </c:filter>',
27             '</c:addressbook-query>'
28         );
29
30         $q = $this->parse($xml);
31
32         $this->assertEquals(
33             array('{DAV:}foo'),
34             $q->requestedProperties
35         );
36
37         $this->assertEquals(
38             array(
39                 array(
40                     'name' => 'NICKNAME',
41                     'test' => 'anyof',
42                     'is-not-defined' => false,
43                     'param-filters' => array(),
44                     'text-matches' => array(),
45                 ),
46             ),
47             $q->filters
48         );
49
50         $this->assertNull($q->limit);
51         $this->assertEquals('anyof', $q->test);
52
53     }
54
55     function testNoFilter() {
56
57         // This is non-standard, but helps working around a KDE bug
58         $xml = array(
59             '<?xml version="1.0"?>',
60             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
61             '   <d:prop>',
62             '      <d:foo />',
63             '   </d:prop>',
64             '</c:addressbook-query>'
65         );
66
67         $q = $this->parse($xml);
68
69         $this->assertEquals(
70             array('{DAV:}foo'),
71             $q->requestedProperties
72         );
73
74         $this->assertEquals(
75             array(),
76             $q->filters
77         );
78
79         $this->assertNull($q->limit);
80         $this->assertEquals('anyof', $q->test);
81
82     }
83
84     /**
85      * @expectedException Sabre_DAV_Exception_BadRequest
86      */
87     function testFilterDoubleFilter() {
88
89         $xml = array(
90             '<?xml version="1.0"?>',
91             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
92             '   <d:prop>',
93             '      <d:foo />',
94             '   </d:prop>',
95             '   <c:filter>',
96             '     <c:prop-filter name="NICKNAME" />',
97             '   </c:filter>',
98             '   <c:filter>',
99             '     <c:prop-filter name="NICKNAME" />',
100             '   </c:filter>',
101             '</c:addressbook-query>'
102         );
103
104         $q = $this->parse($xml);
105
106     }
107     /**
108      * @expectedException Sabre_DAV_Exception_BadRequest
109      */
110     function testFilterCorruptTest() {
111
112         $xml = array(
113             '<?xml version="1.0"?>',
114             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
115             '   <d:prop>',
116             '      <d:foo />',
117             '   </d:prop>',
118             '   <c:filter test="foo">',
119             '     <c:prop-filter name="NICKNAME" />',
120             '   </c:filter>',
121             '</c:addressbook-query>'
122         );
123
124         $q = $this->parse($xml);
125
126     }
127
128     function testPropFilter() {
129
130         $xml = array(
131             '<?xml version="1.0"?>',
132             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
133             '   <d:prop>',
134             '      <d:foo />',
135             '   </d:prop>',
136             '   <c:filter test="allof">',
137             '     <c:prop-filter name="NICKNAME" />',
138             '     <c:prop-filter name="EMAIL" test="allof" />',
139             '     <c:prop-filter name="FN">',
140             '        <c:is-not-defined />',
141             '     </c:prop-filter>',
142             '   </c:filter>',
143             '   <c:limit><c:nresults>4</c:nresults></c:limit>',
144             '</c:addressbook-query>'
145         );
146
147         $q = $this->parse($xml);
148
149         $this->assertEquals(
150             array(
151                 array(
152                     'name' => 'NICKNAME',
153                     'test' => 'anyof',
154                     'is-not-defined' => false,
155                     'param-filters' => array(),
156                     'text-matches' => array(),
157                 ),
158                 array(
159                     'name' => 'EMAIL',
160                     'test' => 'allof',
161                     'is-not-defined' => false,
162                     'param-filters' => array(),
163                     'text-matches' => array(),
164                 ),
165                 array(
166                     'name' => 'FN',
167                     'test' => 'anyof',
168                     'is-not-defined' => true,
169                     'param-filters' => array(),
170                     'text-matches' => array(),
171                 ),
172             ),
173             $q->filters
174         );
175
176         $this->assertEquals(4,$q->limit);
177         $this->assertEquals('allof', $q->test);
178
179     }
180
181     function testParamFilter() {
182
183         $xml = array(
184             '<?xml version="1.0"?>',
185             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
186             '   <d:prop>',
187             '      <d:foo />',
188             '   </d:prop>',
189             '   <c:filter>',
190             '     <c:prop-filter name="NICKNAME">',
191             '        <c:param-filter name="BLA" />',
192             '        <c:param-filter name="BLA2">',
193             '          <c:is-not-defined />',
194             '        </c:param-filter>',
195             '     </c:prop-filter>',
196             '   </c:filter>',
197             '</c:addressbook-query>'
198         );
199
200         $q = $this->parse($xml);
201
202         $this->assertEquals(
203             array(
204                 array(
205                     'name' => 'NICKNAME',
206                     'test' => 'anyof',
207                     'is-not-defined' => false,
208                     'param-filters' => array(
209                         array(
210                             'name' => 'BLA',
211                             'is-not-defined' => false,
212                             'text-match' => null
213                         ),
214                         array(
215                             'name' => 'BLA2',
216                             'is-not-defined' => true,
217                             'text-match' => null
218                         ),
219                     ),
220                     'text-matches' => array(),
221                 ),
222             ),
223             $q->filters
224         );
225
226     }
227
228     function testTextMatch() {
229
230         $xml = array(
231             '<?xml version="1.0"?>',
232             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
233             '   <d:prop>',
234             '      <d:foo />',
235             '   </d:prop>',
236             '   <c:filter>',
237             '     <c:prop-filter name="NICKNAME">',
238             '        <c:text-match>evert</c:text-match>',
239             '        <c:text-match collation="i;octet">evert</c:text-match>',
240             '        <c:text-match negate-condition="yes">rene</c:text-match>',
241             '        <c:text-match match-type="starts-with">e</c:text-match>',
242             '        <c:param-filter name="BLA">',
243             '            <c:text-match>foo</c:text-match>',
244             '        </c:param-filter>',
245             '     </c:prop-filter>',
246             '   </c:filter>',
247             '</c:addressbook-query>'
248         );
249
250         $q = $this->parse($xml);
251
252         $this->assertEquals(
253             array(
254                 array(
255                     'name' => 'NICKNAME',
256                     'test' => 'anyof',
257                     'is-not-defined' => false,
258                     'param-filters' => array(
259                         array(
260                             'name' => 'BLA',
261                             'is-not-defined' => false,
262                             'text-match' => array(
263                                 'negate-condition' => false,
264                                 'collation' => 'i;unicode-casemap',
265                                 'match-type' => 'contains',
266                                 'value'     => 'foo',
267                             ),
268                         ),
269                     ),
270                     'text-matches' => array(
271                         array(
272                             'negate-condition' => false,
273                             'collation' => 'i;unicode-casemap',
274                             'match-type' => 'contains',
275                             'value'     => 'evert',
276                         ),
277                         array(
278                             'negate-condition' => false,
279                             'collation' => 'i;octet',
280                             'match-type' => 'contains',
281                             'value'     => 'evert',
282                         ),
283                         array(
284                             'negate-condition' => true,
285                             'collation' => 'i;unicode-casemap',
286                             'match-type' => 'contains',
287                             'value'     => 'rene',
288                         ),
289                         array(
290                             'negate-condition' => false,
291                             'collation' => 'i;unicode-casemap',
292                             'match-type' => 'starts-with',
293                             'value'     => 'e',
294                         ),
295                     ),
296                 ),
297             ),
298             $q->filters
299         );
300
301     }
302
303     /**
304      * @expectedException Sabre_DAV_Exception_BadRequest
305      */
306     function testBadTextMatch() {
307
308         $xml = array(
309             '<?xml version="1.0"?>',
310             '<c:addressbook-query xmlns:c="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">',
311             '   <d:prop>',
312             '      <d:foo />',
313             '   </d:prop>',
314             '   <c:filter>',
315             '     <c:prop-filter name="NICKNAME">',
316             '        <c:text-match match-type="foo">evert</c:text-match>',
317             '     </c:prop-filter>',
318             '   </c:filter>',
319             '</c:addressbook-query>'
320         );
321
322         $q = $this->parse($xml);
323
324     }
325 }