1<?php
2
3namespace Sabre\DAV\Xml\Element;
4
5use Sabre\DAV;
6
7class ResponseTest extends DAV\Xml\XmlTest {
8
9    function testSimple() {
10
11        $innerProps = [
12            200 => [
13                '{DAV:}displayname' => 'my file',
14            ],
15            404 => [
16                '{DAV:}owner' => null,
17            ]
18        ];
19
20        $property = new Response('uri', $innerProps);
21
22        $this->assertEquals('uri', $property->getHref());
23        $this->assertEquals($innerProps, $property->getResponseProperties());
24
25
26    }
27
28    /**
29     * @depends testSimple
30     */
31    function testSerialize() {
32
33        $innerProps = [
34            200 => [
35                '{DAV:}displayname' => 'my file',
36            ],
37            404 => [
38                '{DAV:}owner' => null,
39            ]
40        ];
41
42        $property = new Response('uri', $innerProps);
43
44        $xml = $this->write(['{DAV:}root' => ['{DAV:}response' => $property]]);
45
46        $this->assertXmlStringEqualsXmlString(
47'<?xml version="1.0"?>
48<d:root xmlns:d="DAV:">
49  <d:response>
50    <d:href>/uri</d:href>
51    <d:propstat>
52      <d:prop>
53        <d:displayname>my file</d:displayname>
54      </d:prop>
55      <d:status>HTTP/1.1 200 OK</d:status>
56    </d:propstat>
57    <d:propstat>
58      <d:prop>
59        <d:owner/>
60      </d:prop>
61      <d:status>HTTP/1.1 404 Not Found</d:status>
62    </d:propstat>
63  </d:response>
64</d:root>
65', $xml);
66
67    }
68
69    /**
70     * This one is specifically for testing properties with no namespaces, which is legal xml
71     *
72     * @depends testSerialize
73     */
74    function testSerializeEmptyNamespace() {
75
76        $innerProps = [
77            200 => [
78                '{}propertyname' => 'value',
79            ],
80        ];
81
82        $property = new Response('uri', $innerProps);
83
84        $xml = $this->write(['{DAV:}root' => ['{DAV:}response' => $property]]);
85
86        $this->assertEquals(
87'<d:root xmlns:d="DAV:">
88 <d:response>
89  <d:href>/uri</d:href>
90  <d:propstat>
91   <d:prop>
92    <propertyname xmlns="">value</propertyname>
93   </d:prop>
94   <d:status>HTTP/1.1 200 OK</d:status>
95  </d:propstat>
96 </d:response>
97</d:root>
98', $xml);
99
100    }
101
102    /**
103     * This one is specifically for testing properties with no namespaces, which is legal xml
104     *
105     * @depends testSerialize
106     */
107    function testSerializeCustomNamespace() {
108
109        $innerProps = [
110            200 => [
111                '{http://sabredav.org/NS/example}propertyname' => 'value',
112            ],
113        ];
114
115        $property = new Response('uri', $innerProps);
116        $xml = $this->write(['{DAV:}root' => ['{DAV:}response' => $property]]);
117
118        $this->assertXmlStringEqualsXmlString(
119'<?xml version="1.0"?>
120<d:root xmlns:d="DAV:">
121  <d:response>
122      <d:href>/uri</d:href>
123      <d:propstat>
124        <d:prop>
125          <x1:propertyname xmlns:x1="http://sabredav.org/NS/example">value</x1:propertyname>
126        </d:prop>
127      <d:status>HTTP/1.1 200 OK</d:status>
128      </d:propstat>
129  </d:response>
130</d:root>', $xml);
131
132    }
133
134    /**
135     * @depends testSerialize
136     */
137    function testSerializeComplexProperty() {
138
139        $innerProps = [
140            200 => [
141                '{DAV:}link' => new DAV\Xml\Property\Href('http://sabredav.org/', false)
142            ],
143        ];
144
145        $property = new Response('uri', $innerProps);
146        $xml = $this->write(['{DAV:}root' => ['{DAV:}response' => $property]]);
147
148        $this->assertXmlStringEqualsXmlString(
149'<?xml version="1.0"?>
150<d:root xmlns:d="DAV:">
151  <d:response>
152      <d:href>/uri</d:href>
153      <d:propstat>
154        <d:prop>
155          <d:link><d:href>http://sabredav.org/</d:href></d:link>
156        </d:prop>
157        <d:status>HTTP/1.1 200 OK</d:status>
158      </d:propstat>
159  </d:response>
160</d:root>
161', $xml);
162
163    }
164
165    /**
166     * @depends testSerialize
167     * @expectedException \InvalidArgumentException
168     */
169    function testSerializeBreak() {
170
171        $innerProps = [
172            200 => [
173                '{DAV:}link' => new \STDClass()
174            ],
175        ];
176
177        $property = new Response('uri', $innerProps);
178        $this->write(['{DAV:}root' => ['{DAV:}response' => $property]]);
179
180    }
181
182    function testDeserializeComplexProperty() {
183
184        $xml = '<?xml version="1.0"?>
185<d:response xmlns:d="DAV:">
186  <d:href>/uri</d:href>
187  <d:propstat>
188    <d:prop>
189      <d:foo>hello</d:foo>
190    </d:prop>
191    <d:status>HTTP/1.1 200 OK</d:status>
192  </d:propstat>
193</d:response>
194';
195
196        $result = $this->parse($xml, [
197            '{DAV:}response' => 'Sabre\DAV\Xml\Element\Response',
198            '{DAV:}foo' => function($reader) {
199
200                $reader->next();
201                return 'world';
202            },
203        ]);
204        $this->assertEquals(
205            new Response('/uri', [
206                '200' => [
207                    '{DAV:}foo' => 'world',
208                ]
209            ]),
210            $result['value']
211        );
212
213    }
214
215    /**
216     * In the case of {DAV:}prop, a deserializer should never get called, if
217     * the property element is empty.
218     */
219    function testDeserializeComplexPropertyEmpty() {
220
221        $xml = '<?xml version="1.0"?>
222<d:response xmlns:d="DAV:">
223  <d:href>/uri</d:href>
224  <d:propstat>
225    <d:prop>
226      <d:foo />
227    </d:prop>
228    <d:status>HTTP/1.1 404 Not Found</d:status>
229  </d:propstat>
230</d:response>
231';
232
233        $result = $this->parse($xml, [
234            '{DAV:}response' => 'Sabre\DAV\Xml\Element\Response',
235            '{DAV:}foo' => function($reader) {
236                throw new \LogicException('This should never happen');
237            },
238        ]);
239        $this->assertEquals(
240            new Response('/uri', [
241                '404' => [
242                    '{DAV:}foo' => null
243                ]
244            ]),
245            $result['value']
246        );
247
248    }
249}
250