1<?php
2
3namespace Sabre\CalDAV;
4
5use Sabre\DAV\PropPatch;
6use Sabre\DAVACL;
7
8require_once 'Sabre/CalDAV/TestUtil.php';
9
10class CalendarTest extends \PHPUnit_Framework_TestCase {
11
12    /**
13     * @var Sabre\CalDAV\Backend\PDO
14     */
15    protected $backend;
16    protected $principalBackend;
17    /**
18     * @var Sabre\CalDAV\Calendar
19     */
20    protected $calendar;
21    /**
22     * @var array
23     */
24    protected $calendars;
25
26    function setup() {
27
28        if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
29
30        $this->backend = TestUtil::getBackend();
31
32        $this->calendars = $this->backend->getCalendarsForUser('principals/user1');
33        $this->assertEquals(2, count($this->calendars));
34        $this->calendar = new Calendar($this->backend, $this->calendars[0]);
35
36
37    }
38
39    function teardown() {
40
41        unset($this->backend);
42
43    }
44
45    function testSimple() {
46
47        $this->assertEquals($this->calendars[0]['uri'], $this->calendar->getName());
48
49    }
50
51    /**
52     * @depends testSimple
53     */
54    function testUpdateProperties() {
55
56        $propPatch = new PropPatch([
57            '{DAV:}displayname' => 'NewName',
58        ]);
59
60        $result = $this->calendar->propPatch($propPatch);
61        $result = $propPatch->commit();
62
63        $this->assertEquals(true, $result);
64
65        $calendars2 = $this->backend->getCalendarsForUser('principals/user1');
66        $this->assertEquals('NewName',$calendars2[0]['{DAV:}displayname']);
67
68    }
69
70    /**
71     * @depends testSimple
72     */
73    function testGetProperties() {
74
75        $question = array(
76            '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set',
77        );
78
79        $result = $this->calendar->getProperties($question);
80
81        foreach($question as $q) $this->assertArrayHasKey($q,$result);
82
83        $this->assertEquals(array('VEVENT','VTODO'), $result['{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set']->getValue());
84
85    }
86
87    /**
88     * @expectedException Sabre\DAV\Exception\NotFound
89     * @depends testSimple
90     */
91    function testGetChildNotFound() {
92
93        $this->calendar->getChild('randomname');
94
95    }
96
97    /**
98     * @depends testSimple
99     */
100    function testGetChildren() {
101
102        $children = $this->calendar->getChildren();
103        $this->assertEquals(1,count($children));
104
105        $this->assertTrue($children[0] instanceof CalendarObject);
106
107    }
108
109    /**
110     * @depends testGetChildren
111     */
112    function testChildExists() {
113
114        $this->assertFalse($this->calendar->childExists('foo'));
115
116        $children = $this->calendar->getChildren();
117        $this->assertTrue($this->calendar->childExists($children[0]->getName()));
118    }
119
120
121
122    /**
123     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
124     */
125    function testCreateDirectory() {
126
127        $this->calendar->createDirectory('hello');
128
129    }
130
131    /**
132     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
133     */
134    function testSetName() {
135
136        $this->calendar->setName('hello');
137
138    }
139
140    function testGetLastModified() {
141
142        $this->assertNull($this->calendar->getLastModified());
143
144    }
145
146    function testCreateFile() {
147
148        $file = fopen('php://memory','r+');
149        fwrite($file,TestUtil::getTestCalendarData());
150        rewind($file);
151
152        $this->calendar->createFile('hello',$file);
153
154        $file = $this->calendar->getChild('hello');
155        $this->assertTrue($file instanceof CalendarObject);
156
157    }
158
159    function testCreateFileNoSupportedComponents() {
160
161        $file = fopen('php://memory','r+');
162        fwrite($file,TestUtil::getTestCalendarData());
163        rewind($file);
164
165        $calendar = new Calendar($this->backend, $this->calendars[1]);
166        $calendar->createFile('hello',$file);
167
168        $file = $calendar->getChild('hello');
169        $this->assertTrue($file instanceof CalendarObject);
170
171    }
172
173    function testDelete() {
174
175        $this->calendar->delete();
176
177        $calendars = $this->backend->getCalendarsForUser('principals/user1');
178        $this->assertEquals(1, count($calendars));
179    }
180
181    function testGetOwner() {
182
183        $this->assertEquals('principals/user1',$this->calendar->getOwner());
184
185    }
186
187    function testGetGroup() {
188
189        $this->assertNull($this->calendar->getGroup());
190
191    }
192
193    function testGetACL() {
194
195        $expected = array(
196            array(
197                'privilege' => '{DAV:}read',
198                'principal' => 'principals/user1',
199                'protected' => true,
200            ),
201            array(
202                'privilege' => '{DAV:}read',
203                'principal' => 'principals/user1/calendar-proxy-write',
204                'protected' => true,
205            ),
206            array(
207                'privilege' => '{DAV:}read',
208                'principal' => 'principals/user1/calendar-proxy-read',
209                'protected' => true,
210            ),
211            array(
212                'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
213                'principal' => '{DAV:}authenticated',
214                'protected' => true,
215            ),
216            array(
217                'privilege' => '{DAV:}write',
218                'principal' => 'principals/user1',
219                'protected' => true,
220            ),
221            array(
222                'privilege' => '{DAV:}write',
223                'principal' => 'principals/user1/calendar-proxy-write',
224                'protected' => true,
225            ),
226        );
227        $this->assertEquals($expected, $this->calendar->getACL());
228
229    }
230
231    /**
232     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
233     */
234    function testSetACL() {
235
236        $this->calendar->setACL(array());
237
238    }
239
240    function testGetSupportedPrivilegesSet() {
241
242        $result = $this->calendar->getSupportedPrivilegeSet();
243
244        $this->assertEquals(
245            '{' . Plugin::NS_CALDAV . '}read-free-busy',
246            $result['aggregates'][0]['aggregates'][2]['privilege']
247        );
248
249    }
250
251    function testGetSyncToken() {
252
253        $this->assertEquals(2, $this->calendar->getSyncToken());
254
255    }
256    function testGetSyncToken2() {
257
258        $calendar = new Calendar(new Backend\Mock([],[]), [
259            '{DAV:}sync-token' => 2
260        ]);
261        $this->assertEquals(2, $this->calendar->getSyncToken());
262
263    }
264
265    function testGetSyncTokenNoSyncSupport() {
266
267        $calendar = new Calendar(new Backend\Mock([],[]), []);
268        $this->assertNull($calendar->getSyncToken());
269
270    }
271
272    function testGetChanges() {
273
274        $this->assertEquals([
275            'syncToken' => 2,
276            'modified'  => [],
277            'deleted'   => [],
278            'added'     => ['UUID-2345'],
279        ], $this->calendar->getChanges(1, 1));
280
281    }
282
283    function testGetChangesNoSyncSupport() {
284
285        $calendar = new Calendar(new Backend\Mock([],[]), []);
286        $this->assertNull($calendar->getChanges(1,null));
287
288    }
289}
290