]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/DAV/XMLUtilTest.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / DAV / XMLUtilTest.php
1 <?php
2
3 class Sabre_DAV_XMLUtilTest extends PHPUnit_Framework_TestCase {
4
5     function testToClarkNotation() {
6
7         $dom = new DOMDocument();
8         $dom->loadXML('<?xml version="1.0"?><test1 xmlns="http://www.example.org/">Testdoc</test1>');
9
10         $this->assertEquals(
11             '{http://www.example.org/}test1',
12             Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild)
13         );
14
15     }
16
17     function testToClarkNotation2() {
18
19         $dom = new DOMDocument();
20         $dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="http://www.example.org/">Testdoc</s:test1>');
21
22         $this->assertEquals(
23             '{http://www.example.org/}test1',
24             Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild)
25         );
26
27     }
28
29     function testToClarkNotationDAVNamespace() {
30
31         $dom = new DOMDocument();
32         $dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="urn:DAV">Testdoc</s:test1>');
33
34         $this->assertEquals(
35             '{DAV:}test1',
36             Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild)
37         );
38
39     }
40
41     function testToClarkNotationNoElem() {
42
43         $dom = new DOMDocument();
44         $dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="urn:DAV">Testdoc</s:test1>');
45
46         $this->assertNull(
47             Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild->firstChild)
48         );
49
50     }
51
52     function testConvertDAVNamespace() {
53
54         $xml='<?xml version="1.0"?><document xmlns="DAV:">blablabla</document>';
55         $this->assertEquals(
56             '<?xml version="1.0"?><document xmlns="urn:DAV">blablabla</document>',
57             Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
58         );
59
60     }
61
62     function testConvertDAVNamespace2() {
63
64         $xml='<?xml version="1.0"?><s:document xmlns:s="DAV:">blablabla</s:document>';
65         $this->assertEquals(
66             '<?xml version="1.0"?><s:document xmlns:s="urn:DAV">blablabla</s:document>',
67             Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
68         );
69
70     }
71
72     function testConvertDAVNamespace3() {
73
74         $xml='<?xml version="1.0"?><s:document xmlns="http://bla" xmlns:s="DAV:" xmlns:z="http://othernamespace">blablabla</s:document>';
75         $this->assertEquals(
76             '<?xml version="1.0"?><s:document xmlns="http://bla" xmlns:s="urn:DAV" xmlns:z="http://othernamespace">blablabla</s:document>',
77             Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
78         );
79
80     }
81
82     function testConvertDAVNamespace4() {
83
84         $xml='<?xml version="1.0"?><document xmlns=\'DAV:\'>blablabla</document>';
85         $this->assertEquals(
86             '<?xml version="1.0"?><document xmlns=\'urn:DAV\'>blablabla</document>',
87             Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
88         );
89
90     }
91
92     function testConvertDAVNamespaceMixedQuotes() {
93
94         $xml='<?xml version="1.0"?><document xmlns=\'DAV:" xmlns="Another attribute\'>blablabla</document>';
95         $this->assertEquals(
96             $xml,
97             Sabre_DAV_XMLUtil::convertDAVNamespace($xml)
98         );
99
100     }
101
102     /**
103      * @depends testConvertDAVNamespace
104      */
105     function testLoadDOMDocument() {
106
107         $xml='<?xml version="1.0"?><document></document>';
108         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
109         $this->assertTrue($dom instanceof DOMDocument);
110
111     }
112
113     /**
114      * @depends testLoadDOMDocument
115      * @expectedException Sabre_DAV_Exception_BadRequest
116      */
117     function testLoadDOMDocumentEmpty() {
118
119         Sabre_DAV_XMLUtil::loadDOMDocument('');
120
121     }
122
123     /**
124      * @depends testConvertDAVNamespace
125      * @expectedException Sabre_DAV_Exception_BadRequest
126      */
127     function testLoadDOMDocumentInvalid() {
128
129         $xml='<?xml version="1.0"?><document></docu';
130         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
131
132     }
133
134     /**
135      * @depends testLoadDOMDocument
136      */
137     function testLoadDOMDocumentUTF16() {
138
139         $xml='<?xml version="1.0" encoding="UTF-16"?><root xmlns="DAV:">blabla</root>';
140         $xml = iconv('UTF-8','UTF-16LE',$xml);
141         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
142         $this->assertEquals('blabla',$dom->firstChild->nodeValue);
143
144     }
145
146
147     function testParseProperties() {
148
149         $xml='<?xml version="1.0"?>
150 <root xmlns="DAV:">
151   <prop>
152     <displayname>Calendars</displayname>
153   </prop>
154 </root>';
155
156         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
157         $properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
158
159         $this->assertEquals(array(
160             '{DAV:}displayname' => 'Calendars',
161         ), $properties);
162
163
164
165     }
166
167     /**
168      * @depends testParseProperties
169      */
170     function testParsePropertiesEmpty() {
171
172         $xml='<?xml version="1.0"?>
173 <root xmlns="DAV:" xmlns:s="http://www.rooftopsolutions.nl/example">
174   <prop>
175     <displayname>Calendars</displayname>
176   </prop>
177   <prop>
178     <s:example />
179   </prop>
180 </root>';
181
182         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
183         $properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
184
185         $this->assertEquals(array(
186             '{DAV:}displayname' => 'Calendars',
187             '{http://www.rooftopsolutions.nl/example}example' => null
188         ), $properties);
189
190     }
191
192
193     /**
194      * @depends testParseProperties
195      */
196     function testParsePropertiesComplex() {
197
198         $xml='<?xml version="1.0"?>
199 <root xmlns="DAV:">
200   <prop>
201     <displayname>Calendars</displayname>
202   </prop>
203   <prop>
204     <someprop>Complex value <b>right here</b></someprop>
205   </prop>
206 </root>';
207
208         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
209         $properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
210
211         $this->assertEquals(array(
212             '{DAV:}displayname' => 'Calendars',
213             '{DAV:}someprop'    => 'Complex value right here',
214         ), $properties);
215
216     }
217
218
219     /**
220      * @depends testParseProperties
221      */
222     function testParsePropertiesNoProperties() {
223
224         $xml='<?xml version="1.0"?>
225 <root xmlns="DAV:">
226   <prop>
227   </prop>
228 </root>';
229
230         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
231         $properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild);
232
233         $this->assertEquals(array(), $properties);
234
235     }
236
237     function testParsePropertiesMapHref() {
238
239         $xml='<?xml version="1.0"?>
240 <root xmlns="DAV:">
241   <prop>
242     <displayname>Calendars</displayname>
243   </prop>
244   <prop>
245     <someprop><href>http://sabredav.org/</href></someprop>
246   </prop>
247 </root>';
248
249         $dom = Sabre_DAV_XMLUtil::loadDOMDocument($xml);
250         $properties = Sabre_DAV_XMLUtil::parseProperties($dom->firstChild,array('{DAV:}someprop'=>'Sabre_DAV_Property_Href'));
251
252         $this->assertEquals(array(
253             '{DAV:}displayname' => 'Calendars',
254             '{DAV:}someprop'    => new Sabre_DAV_Property_Href('http://sabredav.org/',false),
255         ), $properties);
256
257     }
258
259     function testParseClarkNotation() {
260
261         $this->assertEquals(array(
262             'DAV:',
263             'foo',
264         ), Sabre_DAV_XMLUtil::parseClarkNotation('{DAV:}foo'));
265
266         $this->assertEquals(array(
267             'http://example.org/ns/bla',
268             'bar-soap',
269         ), Sabre_DAV_XMLUtil::parseClarkNotation('{http://example.org/ns/bla}bar-soap'));
270     }
271
272     /**
273      * @expectedException InvalidArgumentException
274      */
275     function testParseClarkNotationFail() {
276
277         Sabre_DAV_XMLUtil::parseClarkNotation('}foo');
278
279     }
280
281 }
282