1<?php
2
3namespace Sabre\DAVACL\PrincipalBackend;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8
9abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
10
11    abstract function getPDO();
12
13    function testConstruct() {
14
15        $pdo = $this->getPDO();
16        $backend = new PDO($pdo);
17        $this->assertTrue($backend instanceof PDO);
18
19    }
20
21    /**
22     * @depends testConstruct
23     */
24    function testGetPrincipalsByPrefix() {
25
26        $pdo = $this->getPDO();
27        $backend = new PDO($pdo);
28
29        $expected = array(
30            array(
31                'uri' => 'principals/user',
32                '{http://sabredav.org/ns}email-address' => 'user@example.org',
33                '{DAV:}displayname' => 'User',
34            ),
35            array(
36                'uri' => 'principals/group',
37                '{http://sabredav.org/ns}email-address' => 'group@example.org',
38                '{DAV:}displayname' => 'Group',
39            ),
40        );
41
42        $this->assertEquals($expected, $backend->getPrincipalsByPrefix('principals'));
43        $this->assertEquals(array(), $backend->getPrincipalsByPrefix('foo'));
44
45    }
46
47    /**
48     * @depends testConstruct
49     */
50    function testGetPrincipalByPath() {
51
52        $pdo = $this->getPDO();
53        $backend = new PDO($pdo);
54
55        $expected = array(
56            'id' => 1,
57            'uri' => 'principals/user',
58            '{http://sabredav.org/ns}email-address' => 'user@example.org',
59            '{DAV:}displayname' => 'User',
60        );
61
62        $this->assertEquals($expected, $backend->getPrincipalByPath('principals/user'));
63        $this->assertEquals(null, $backend->getPrincipalByPath('foo'));
64
65    }
66
67    function testGetGroupMemberSet() {
68
69        $pdo = $this->getPDO();
70        $backend = new PDO($pdo);
71        $expected = array('principals/user');
72
73        $this->assertEquals($expected,$backend->getGroupMemberSet('principals/group'));
74
75    }
76
77    function testGetGroupMembership() {
78
79        $pdo = $this->getPDO();
80        $backend = new PDO($pdo);
81        $expected = array('principals/group');
82
83        $this->assertEquals($expected,$backend->getGroupMembership('principals/user'));
84
85    }
86
87    function testSetGroupMemberSet() {
88
89        $pdo = $this->getPDO();
90
91        // Start situation
92        $backend = new PDO($pdo);
93        $this->assertEquals(array('principals/user'), $backend->getGroupMemberSet('principals/group'));
94
95        // Removing all principals
96        $backend->setGroupMemberSet('principals/group', array());
97        $this->assertEquals(array(), $backend->getGroupMemberSet('principals/group'));
98
99        // Adding principals again
100        $backend->setGroupMemberSet('principals/group', array('principals/user'));
101        $this->assertEquals(array('principals/user'), $backend->getGroupMemberSet('principals/group'));
102
103
104    }
105
106    function testSearchPrincipals() {
107
108        $pdo = $this->getPDO();
109
110        $backend = new PDO($pdo);
111
112        $result = $backend->searchPrincipals('principals', array('{DAV:}blabla' => 'foo'));
113        $this->assertEquals(array(), $result);
114
115        $result = $backend->searchPrincipals('principals', array('{DAV:}displayname' => 'ou'));
116        $this->assertEquals(array('principals/group'), $result);
117
118        $result = $backend->searchPrincipals('principals', array('{DAV:}displayname' => 'UsEr', '{http://sabredav.org/ns}email-address' => 'USER@EXAMPLE'));
119        $this->assertEquals(array('principals/user'), $result);
120
121        $result = $backend->searchPrincipals('mom', array('{DAV:}displayname' => 'UsEr', '{http://sabredav.org/ns}email-address' => 'USER@EXAMPLE'));
122        $this->assertEquals(array(), $result);
123
124    }
125
126    function testUpdatePrincipal() {
127
128        $pdo = $this->getPDO();
129        $backend = new PDO($pdo);
130
131        $propPatch = new DAV\PropPatch([
132            '{DAV:}displayname' => 'pietje',
133        ]);
134
135        $backend->updatePrincipal('principals/user', $propPatch);
136        $result = $propPatch->commit();
137
138        $this->assertTrue($result);
139
140        $this->assertEquals(array(
141            'id' => 1,
142            'uri' => 'principals/user',
143            '{DAV:}displayname' => 'pietje',
144            '{http://sabredav.org/ns}email-address' => 'user@example.org',
145        ), $backend->getPrincipalByPath('principals/user'));
146
147    }
148
149    function testUpdatePrincipalUnknownField() {
150
151        $pdo = $this->getPDO();
152        $backend = new PDO($pdo);
153
154        $propPatch = new DAV\PropPatch([
155            '{DAV:}displayname' => 'pietje',
156            '{DAV:}unknown' => 'foo',
157        ]);
158
159        $backend->updatePrincipal('principals/user', $propPatch);
160        $result = $propPatch->commit();
161
162        $this->assertFalse($result);
163
164        $this->assertEquals(array(
165            '{DAV:}displayname' => 424,
166            '{DAV:}unknown' => 403
167        ), $propPatch->getResult());
168
169        $this->assertEquals(array(
170            'id' => '1',
171            'uri' => 'principals/user',
172            '{DAV:}displayname' => 'User',
173            '{http://sabredav.org/ns}email-address' => 'user@example.org',
174        ), $backend->getPrincipalByPath('principals/user'));
175
176    }
177
178}
179