1<?php
2
3namespace Sabre\CardDAV;
4
5use Sabre\DAV\PropPatch;
6
7require_once 'Sabre/CardDAV/Backend/Mock.php';
8
9class AddressBookTest extends \PHPUnit_Framework_TestCase {
10
11    /**
12     * @var Sabre\CardDAV\AddressBook
13     */
14    protected $ab;
15    protected $backend;
16
17    function setUp() {
18
19        $this->backend = new Backend\Mock();
20        $this->ab = new AddressBook(
21            $this->backend,
22            array(
23                'uri' => 'book1',
24                'id' => 'foo',
25                '{DAV:}displayname' => 'd-name',
26                'principaluri' => 'principals/user1',
27            )
28        );
29
30    }
31
32    function testGetName() {
33
34        $this->assertEquals('book1', $this->ab->getName());
35
36    }
37
38    function testGetChild() {
39
40        $card = $this->ab->getChild('card1');
41        $this->assertInstanceOf('Sabre\\CardDAV\\Card', $card);
42        $this->assertEquals('card1', $card->getName());
43
44    }
45
46    /**
47     * @expectedException Sabre\DAV\Exception\NotFound
48     */
49    function testGetChildNotFound() {
50
51        $card = $this->ab->getChild('card3');
52
53    }
54
55    function testGetChildren() {
56
57        $cards = $this->ab->getChildren();
58        $this->assertEquals(2, count($cards));
59
60        $this->assertEquals('card1', $cards[0]->getName());
61        $this->assertEquals('card2', $cards[1]->getName());
62
63    }
64
65    /**
66     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
67     */
68    function testCreateDirectory() {
69
70        $this->ab->createDirectory('name');
71
72    }
73
74    function testCreateFile() {
75
76        $file = fopen('php://memory','r+');
77        fwrite($file,'foo');
78        rewind($file);
79        $this->ab->createFile('card2',$file);
80
81        $this->assertEquals('foo', $this->backend->cards['foo']['card2']);
82
83    }
84
85    function testDelete() {
86
87        $this->ab->delete();
88        $this->assertEquals(array(), $this->backend->addressBooks);
89
90    }
91
92    /**
93     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
94     */
95    function testSetName() {
96
97        $this->ab->setName('foo');
98
99    }
100
101    function testGetLastModified() {
102
103        $this->assertNull($this->ab->getLastModified());
104
105    }
106
107    function testUpdateProperties() {
108
109        $propPatch = new PropPatch([
110            '{DAV:}displayname' => 'barrr',
111        ]);
112        $this->ab->propPatch($propPatch);
113        $this->assertTrue($propPatch->commit());
114
115        $this->assertEquals('barrr', $this->backend->addressBooks[0]['{DAV:}displayname']);
116
117    }
118
119    function testGetProperties() {
120
121        $props = $this->ab->getProperties(array('{DAV:}displayname'));
122        $this->assertEquals(array(
123            '{DAV:}displayname' => 'd-name',
124        ), $props);
125
126    }
127
128    function testACLMethods() {
129
130        $this->assertEquals('principals/user1', $this->ab->getOwner());
131        $this->assertNull($this->ab->getGroup());
132        $this->assertEquals(array(
133            array(
134                'privilege' => '{DAV:}read',
135                'principal' => 'principals/user1',
136                'protected' => true,
137            ),
138            array(
139                'privilege' => '{DAV:}write',
140                'principal' => 'principals/user1',
141                'protected' => true,
142            ),
143        ), $this->ab->getACL());
144
145    }
146
147    /**
148     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
149     */
150    function testSetACL() {
151
152       $this->ab->setACL(array());
153
154    }
155
156    function testGetSupportedPrivilegeSet() {
157
158        $this->assertNull(
159            $this->ab->getSupportedPrivilegeSet()
160        );
161
162    }
163
164    function testGetSyncTokenNoSyncSupport() {
165
166        $this->assertNull(null, $this->ab->getSyncToken());
167
168    }
169    function testGetChangesNoSyncSupport() {
170
171        $this->assertNull($this->ab->getChanges(1,null));
172
173    }
174
175    function testGetSyncToken() {
176
177        if (!SABRE_HASSQLITE) {
178            $this->markTestSkipped('Sqlite is required for this test to run');
179        }
180        $ab = new AddressBook(TestUtil::getBackend(), [ 'id' => 1, '{DAV:}sync-token' => 2]);
181        $this->assertEquals(2, $ab->getSyncToken());
182    }
183
184    function testGetSyncToken2() {
185
186        if (!SABRE_HASSQLITE) {
187            $this->markTestSkipped('Sqlite is required for this test to run');
188        }
189        $ab = new AddressBook(TestUtil::getBackend(), [ 'id' => 1, '{http://sabredav.org/ns}sync-token' => 2]);
190        $this->assertEquals(2, $ab->getSyncToken());
191    }
192
193    function testGetChanges() {
194
195        if (!SABRE_HASSQLITE) {
196            $this->markTestSkipped('Sqlite is required for this test to run');
197        }
198        $ab = new AddressBook(TestUtil::getBackend(), [ 'id' => 1, '{DAV:}sync-token' => 2]);
199        $this->assertEquals([
200            'syncToken' => 2,
201            'modified' => [],
202            'deleted' => [],
203            'added' => ['UUID-2345'],
204        ], $ab->getChanges(1, 1));
205
206    }
207
208
209}
210