1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/HTTP/ResponseMock.php';
9
10class PrincipalSearchPropertySetTest extends \PHPUnit_Framework_TestCase {
11
12    function getServer() {
13
14        $backend = new PrincipalBackend\Mock();
15
16        $dir = new DAV\SimpleCollection('root');
17        $principals = new PrincipalCollection($backend);
18        $dir->addChild($principals);
19
20        $fakeServer = new DAV\Server($dir);
21        $fakeServer->sapi = new HTTP\SapiMock();
22        $fakeServer->httpResponse = new HTTP\ResponseMock();
23        $plugin = new Plugin($backend,'realm');
24        $this->assertTrue($plugin instanceof Plugin);
25        $fakeServer->addPlugin($plugin);
26        $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
27
28        return $fakeServer;
29
30    }
31
32    function testDepth1() {
33
34        $xml = '<?xml version="1.0"?>
35<d:principal-search-property-set xmlns:d="DAV:" />';
36
37        $serverVars = array(
38            'REQUEST_METHOD' => 'REPORT',
39            'HTTP_DEPTH'     => '1',
40            'REQUEST_URI'    => '/principals',
41        );
42
43        $request = HTTP\Sapi::createFromServerArray($serverVars);
44        $request->setBody($xml);
45
46        $server = $this->getServer();
47        $server->httpRequest = $request;
48
49        $server->exec();
50
51        $this->assertEquals(400, $server->httpResponse->status);
52        $this->assertEquals(array(
53            'X-Sabre-Version' => [DAV\Version::VERSION],
54            'Content-Type' => ['application/xml; charset=utf-8'],
55        ), $server->httpResponse->getHeaders());
56
57    }
58
59    function testDepthIncorrectXML() {
60
61        $xml = '<?xml version="1.0"?>
62<d:principal-search-property-set xmlns:d="DAV:"><d:ohell /></d:principal-search-property-set>';
63
64        $serverVars = array(
65            'REQUEST_METHOD' => 'REPORT',
66            'HTTP_DEPTH'     => '0',
67            'REQUEST_URI'    => '/principals',
68        );
69
70        $request = HTTP\Sapi::createFromServerArray($serverVars);
71        $request->setBody($xml);
72
73        $server = $this->getServer();
74        $server->httpRequest = $request;
75
76        $server->exec();
77
78        $this->assertEquals(400, $server->httpResponse->status, $server->httpResponse->body);
79        $this->assertEquals(array(
80            'X-Sabre-Version' => [DAV\Version::VERSION],
81            'Content-Type' => ['application/xml; charset=utf-8'],
82        ), $server->httpResponse->getHeaders());
83
84    }
85
86    function testCorrect() {
87
88        $xml = '<?xml version="1.0"?>
89<d:principal-search-property-set xmlns:d="DAV:"/>';
90
91        $serverVars = array(
92            'REQUEST_METHOD' => 'REPORT',
93            'HTTP_DEPTH'     => '0',
94            'REQUEST_URI'    => '/principals',
95        );
96
97        $request = HTTP\Sapi::createFromServerArray($serverVars);
98        $request->setBody($xml);
99
100        $server = $this->getServer();
101        $server->httpRequest = $request;
102
103        $server->exec();
104
105        $this->assertEquals(200, $server->httpResponse->status, $server->httpResponse->body);
106        $this->assertEquals(array(
107            'X-Sabre-Version' => [DAV\Version::VERSION],
108            'Content-Type' => ['application/xml; charset=utf-8'],
109        ), $server->httpResponse->getHeaders());
110
111
112        $check = array(
113            '/d:principal-search-property-set',
114            '/d:principal-search-property-set/d:principal-search-property' => 2,
115            '/d:principal-search-property-set/d:principal-search-property/d:prop' => 2,
116            '/d:principal-search-property-set/d:principal-search-property/d:prop/d:displayname' => 1,
117            '/d:principal-search-property-set/d:principal-search-property/d:prop/s:email-address' => 1,
118            '/d:principal-search-property-set/d:principal-search-property/d:description' => 2,
119        );
120
121        $xml = simplexml_load_string($server->httpResponse->body);
122        $xml->registerXPathNamespace('d','DAV:');
123        $xml->registerXPathNamespace('s','http://sabredav.org/ns');
124        foreach($check as $v1=>$v2) {
125
126            $xpath = is_int($v1)?$v2:$v1;
127
128            $result = $xml->xpath($xpath);
129
130            $count = 1;
131            if (!is_int($v1)) $count = $v2;
132
133            $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body);
134
135        }
136
137    }
138
139}
140