1<?php
2
3namespace Sabre\DAV\Property;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/HTTP/ResponseMock.php';
9require_once 'Sabre/DAV/AbstractServer.php';
10
11class SupportedReportSetTest extends DAV\AbstractServer {
12
13    function sendPROPFIND($body) {
14
15        $serverVars = [
16            'REQUEST_URI'         => '/',
17            'REQUEST_METHOD'      => 'PROPFIND',
18            'HTTP_DEPTH'          => '0',
19        ];
20
21        $request = HTTP\Sapi::createFromServerArray($serverVars);
22        $request->setBody($body);
23
24        $this->server->httpRequest = ($request);
25        $this->server->exec();
26
27    }
28
29    /**
30     */
31    function testNoReports() {
32
33        $xml = '<?xml version="1.0"?>
34<d:propfind xmlns:d="DAV:">
35  <d:prop>
36    <d:supported-report-set />
37  </d:prop>
38</d:propfind>';
39
40        $this->sendPROPFIND($xml);
41
42        $this->assertEquals(207, $this->response->status, 'We expected a multi-status response. Full response body: ' . $this->response->body);
43
44        $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
45        $xml = simplexml_load_string($body);
46        $xml->registerXPathNamespace('d', 'urn:DAV');
47
48        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
49        $this->assertEquals(1, count($data), 'We expected 1 \'d:prop\' element');
50
51        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set');
52        $this->assertEquals(1, count($data), 'We expected 1 \'d:supported-report-set\' element');
53
54        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
55        $this->assertEquals(1, count($data), 'We expected 1 \'d:status\' element');
56
57        $this->assertEquals('HTTP/1.1 200 OK', (string)$data[0], 'The status for this property should have been 200');
58
59    }
60
61    /**
62     * @depends testNoReports
63     */
64    function testCustomReport() {
65
66        // Intercepting the report property
67        $this->server->on('propFind', function(DAV\PropFind $propFind, DAV\INode $node) {
68            if ($prop = $propFind->get('{DAV:}supported-report-set')) {
69                $prop->addReport('{http://www.rooftopsolutions.nl/testnamespace}myreport');
70                $prop->addReport('{DAV:}anotherreport');
71            }
72        }, 200);
73
74        $xml = '<?xml version="1.0"?>
75<d:propfind xmlns:d="DAV:">
76  <d:prop>
77    <d:supported-report-set />
78  </d:prop>
79</d:propfind>';
80
81        $this->sendPROPFIND($xml);
82
83        $this->assertEquals(207, $this->response->status, 'We expected a multi-status response. Full response body: ' . $this->response->body);
84
85        $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $this->response->body);
86        $xml = simplexml_load_string($body);
87        $xml->registerXPathNamespace('d', 'urn:DAV');
88        $xml->registerXPathNamespace('x', 'http://www.rooftopsolutions.nl/testnamespace');
89
90        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
91        $this->assertEquals(1, count($data), 'We expected 1 \'d:prop\' element');
92
93        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set');
94        $this->assertEquals(1, count($data), 'We expected 1 \'d:supported-report-set\' element');
95
96        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report');
97        $this->assertEquals(2, count($data), 'We expected 2 \'d:supported-report\' elements');
98
99        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report/d:report');
100        $this->assertEquals(2, count($data), 'We expected 2 \'d:report\' elements');
101
102        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report/d:report/x:myreport');
103        $this->assertEquals(1, count($data), 'We expected 1 \'x:myreport\' element. Full body: ' . $this->response->body);
104
105        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supported-report-set/d:supported-report/d:report/d:anotherreport');
106        $this->assertEquals(1, count($data), 'We expected 1 \'d:anotherreport\' element. Full body: ' . $this->response->body);
107
108        $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
109        $this->assertEquals(1, count($data), 'We expected 1 \'d:status\' element');
110
111        $this->assertEquals('HTTP/1.1 200 OK', (string)$data[0], 'The status for this property should have been 200');
112
113    }
114
115}
116