1<?php
2
3namespace Sabre\CardDAV;
4
5use Sabre\DAV\MkCol;
6
7class AddressBookHomeTest extends \PHPUnit_Framework_TestCase {
8
9    /**
10     * @var Sabre\CardDAV\AddressBookHome
11     */
12    protected $s;
13    protected $backend;
14
15    function setUp() {
16
17        $this->backend = new Backend\Mock();
18        $this->s = new AddressBookHome(
19            $this->backend,
20            'principals/user1'
21        );
22
23    }
24
25    function testGetName() {
26
27        $this->assertEquals('user1', $this->s->getName());
28
29    }
30
31    /**
32     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
33     */
34    function testSetName() {
35
36        $this->s->setName('user2');
37
38    }
39
40    /**
41     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
42     */
43    function testDelete() {
44
45        $this->s->delete();
46
47    }
48
49    function testGetLastModified() {
50
51        $this->assertNull($this->s->getLastModified());
52
53    }
54
55    /**
56     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
57     */
58    function testCreateFile() {
59
60        $this->s->createFile('bla');
61
62    }
63
64    /**
65     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
66     */
67    function testCreateDirectory() {
68
69        $this->s->createDirectory('bla');
70
71    }
72
73    function testGetChild() {
74
75        $child = $this->s->getChild('book1');
76        $this->assertInstanceOf('Sabre\\CardDAV\\AddressBook', $child);
77        $this->assertEquals('book1', $child->getName());
78
79    }
80
81    /**
82     * @expectedException Sabre\DAV\Exception\NotFound
83     */
84    function testGetChild404() {
85
86        $this->s->getChild('book2');
87
88    }
89
90    function testGetChildren() {
91
92        $children = $this->s->getChildren();
93        $this->assertEquals(1, count($children));
94        $this->assertInstanceOf('Sabre\\CardDAV\\AddressBook', $children[0]);
95        $this->assertEquals('book1', $children[0]->getName());
96
97    }
98
99    function testCreateExtendedCollection() {
100
101        $resourceType = [
102            '{' . Plugin::NS_CARDDAV . '}addressbook',
103            '{DAV:}collection',
104        ];
105        $this->s->createExtendedCollection('book2', new MkCol($resourceType, ['{DAV:}displayname' => 'a-book 2']));
106
107        $this->assertEquals(array(
108            'id' => 'book2',
109            'uri' => 'book2',
110            '{DAV:}displayname' => 'a-book 2',
111            'principaluri' => 'principals/user1',
112        ), $this->backend->addressBooks[1]);
113
114    }
115
116    /**
117     * @expectedException Sabre\DAV\Exception\InvalidResourceType
118     */
119    function testCreateExtendedCollectionInvalid() {
120
121        $resourceType = array(
122            '{DAV:}collection',
123        );
124        $this->s->createExtendedCollection('book2', new MkCol($resourceType, array('{DAV:}displayname' => 'a-book 2')));
125
126    }
127
128
129    function testACLMethods() {
130
131        $this->assertEquals('principals/user1', $this->s->getOwner());
132        $this->assertNull($this->s->getGroup());
133        $this->assertEquals(array(
134            array(
135                'privilege' => '{DAV:}read',
136                'principal' => 'principals/user1',
137                'protected' => true,
138            ),
139            array(
140                'privilege' => '{DAV:}write',
141                'principal' => 'principals/user1',
142                'protected' => true,
143            ),
144        ), $this->s->getACL());
145
146    }
147
148    /**
149     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
150     */
151    function testSetACL() {
152
153       $this->s->setACL(array());
154
155    }
156
157    function testGetSupportedPrivilegeSet() {
158
159        $this->assertNull(
160            $this->s->getSupportedPrivilegeSet()
161        );
162
163    }
164}
165