1<?php
2
3namespace Sabre\DAVACL;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8class PrincipalTest extends \PHPUnit_Framework_TestCase {
9
10    public function testConstruct() {
11
12        $principalBackend = new PrincipalBackend\Mock();
13        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
14        $this->assertTrue($principal instanceof Principal);
15
16    }
17
18    /**
19     * @expectedException Sabre\DAV\Exception
20     */
21    public function testConstructNoUri() {
22
23        $principalBackend = new PrincipalBackend\Mock();
24        $principal = new Principal($principalBackend, array());
25
26    }
27
28    public function testGetName() {
29
30        $principalBackend = new PrincipalBackend\Mock();
31        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
32        $this->assertEquals('admin',$principal->getName());
33
34    }
35
36    public function testGetDisplayName() {
37
38        $principalBackend = new PrincipalBackend\Mock();
39        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
40        $this->assertEquals('admin',$principal->getDisplayname());
41
42        $principal = new Principal($principalBackend, array(
43            'uri' => 'principals/admin',
44            '{DAV:}displayname' => 'Mr. Admin'
45        ));
46        $this->assertEquals('Mr. Admin',$principal->getDisplayname());
47
48    }
49
50    public function testGetProperties() {
51
52        $principalBackend = new PrincipalBackend\Mock();
53        $principal = new Principal($principalBackend, array(
54            'uri' => 'principals/admin',
55            '{DAV:}displayname' => 'Mr. Admin',
56            '{http://www.example.org/custom}custom' => 'Custom',
57            '{http://sabredav.org/ns}email-address' => 'admin@example.org',
58        ));
59
60        $keys = array(
61            '{DAV:}displayname',
62            '{http://www.example.org/custom}custom',
63            '{http://sabredav.org/ns}email-address',
64        );
65        $props = $principal->getProperties($keys);
66
67        foreach($keys as $key) $this->assertArrayHasKey($key,$props);
68
69        $this->assertEquals('Mr. Admin',$props['{DAV:}displayname']);
70
71        $this->assertEquals('admin@example.org', $props['{http://sabredav.org/ns}email-address']);
72    }
73
74    public function testUpdateProperties() {
75
76        $principalBackend = new PrincipalBackend\Mock();
77        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
78
79        $propPatch = new DAV\PropPatch(array('{DAV:}yourmom' => 'test'));
80
81        $result = $principal->propPatch($propPatch);
82        $result = $propPatch->commit();
83        $this->assertTrue($result);
84
85    }
86
87    public function testGetPrincipalUrl() {
88
89        $principalBackend = new PrincipalBackend\Mock();
90        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
91        $this->assertEquals('principals/admin',$principal->getPrincipalUrl());
92
93    }
94
95    public function testGetAlternateUriSet() {
96
97        $principalBackend = new PrincipalBackend\Mock();
98        $principal = new Principal($principalBackend, array(
99            'uri' => 'principals/admin',
100            '{DAV:}displayname' => 'Mr. Admin',
101            '{http://www.example.org/custom}custom' => 'Custom',
102            '{http://sabredav.org/ns}email-address' => 'admin@example.org',
103            '{DAV:}alternate-URI-set' => array(
104                'mailto:admin+1@example.org',
105                'mailto:admin+2@example.org',
106                'mailto:admin@example.org',
107            ),
108        ));
109
110        $expected = array(
111            'mailto:admin+1@example.org',
112            'mailto:admin+2@example.org',
113            'mailto:admin@example.org',
114        );
115
116        $this->assertEquals($expected,$principal->getAlternateUriSet());
117
118    }
119    public function testGetAlternateUriSetEmpty() {
120
121        $principalBackend = new PrincipalBackend\Mock();
122        $principal = new Principal($principalBackend, array(
123            'uri' => 'principals/admin',
124        ));
125
126        $expected = array();
127
128        $this->assertEquals($expected,$principal->getAlternateUriSet());
129
130    }
131
132    public function testGetGroupMemberSet() {
133
134        $principalBackend = new PrincipalBackend\Mock();
135        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
136        $this->assertEquals(array(),$principal->getGroupMemberSet());
137
138    }
139    public function testGetGroupMembership() {
140
141        $principalBackend = new PrincipalBackend\Mock();
142        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
143        $this->assertEquals(array(),$principal->getGroupMembership());
144
145    }
146
147    public function testSetGroupMemberSet() {
148
149        $principalBackend = new PrincipalBackend\Mock();
150        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
151        $principal->setGroupMemberSet(array('principals/foo'));
152
153        $this->assertEquals(array(
154            'principals/admin' => array('principals/foo'),
155        ), $principalBackend->groupMembers);
156
157    }
158
159    public function testGetOwner() {
160
161        $principalBackend = new PrincipalBackend\Mock();
162        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
163        $this->assertEquals('principals/admin',$principal->getOwner());
164
165    }
166
167    public function testGetGroup() {
168
169        $principalBackend = new PrincipalBackend\Mock();
170        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
171        $this->assertNull($principal->getGroup());
172
173    }
174
175    public function testGetACl() {
176
177        $principalBackend = new PrincipalBackend\Mock();
178        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
179        $this->assertEquals(array(
180            array(
181                'privilege' => '{DAV:}read',
182                'principal' => '{DAV:}authenticated',
183                'protected' => true,
184            )
185        ),$principal->getACL());
186
187    }
188
189    /**
190     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
191     */
192    public function testSetACl() {
193
194        $principalBackend = new PrincipalBackend\Mock();
195        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
196        $principal->setACL(array());
197
198    }
199
200    public function testGetSupportedPrivilegeSet() {
201
202        $principalBackend = new PrincipalBackend\Mock();
203        $principal = new Principal($principalBackend, array('uri' => 'principals/admin'));
204        $this->assertNull($principal->getSupportedPrivilegeSet());
205
206    }
207
208}
209