1<?php
2
3namespace Sabre\CalDAV\Schedule;
4use Sabre\DAVACL;
5use Sabre\CalDAV\Backend;
6
7class SchedulingObjectTest extends \PHPUnit_Framework_TestCase {
8
9    /**
10     * @var Sabre\CalDAV\Backend_PDO
11     */
12    protected $backend;
13    /**
14     * @var Sabre\CalDAV\Calendar
15     */
16    protected $calendar;
17    protected $principalBackend;
18
19    protected $data;
20    protected $data2;
21
22    function setup() {
23
24        if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
25        $this->backend = new Backend\MockScheduling();
26
27        $this->data = <<<ICS
28BEGIN:VCALENDAR
29METHOD:REQUEST
30BEGIN:VEVENT
31SEQUENCE:1
32END:VEVENT
33END:VCALENDAR
34ICS;
35        $this->data = <<<ICS
36BEGIN:VCALENDAR
37METHOD:REQUEST
38BEGIN:VEVENT
39SEQUENCE:2
40END:VEVENT
41END:VCALENDAR
42ICS;
43
44        $this->inbox = new Inbox($this->backend, 'principals/user1');
45        $this->inbox->createFile('item1.ics', $this->data);
46
47    }
48
49    function teardown() {
50
51        unset($this->inbox);
52        unset($this->backend);
53
54    }
55
56    function testSetup() {
57
58        $children = $this->inbox->getChildren();
59        $this->assertTrue($children[0] instanceof SchedulingObject);
60
61        $this->assertInternalType('string',$children[0]->getName());
62        $this->assertInternalType('string',$children[0]->get());
63        $this->assertInternalType('string',$children[0]->getETag());
64        $this->assertEquals('text/calendar; charset=utf-8', $children[0]->getContentType());
65
66    }
67
68    /**
69     * @expectedException InvalidArgumentException
70     */
71    function testInvalidArg1() {
72
73        $obj = new SchedulingObject(
74            new Backend\MockScheduling(array(),array()),
75            array(),
76            array()
77        );
78
79    }
80
81    /**
82     * @expectedException InvalidArgumentException
83     */
84    function testInvalidArg2() {
85
86        $obj = new SchedulingObject(
87            new Backend\MockScheduling(array(),array()),
88            array(),
89            array('calendarid' => '1')
90        );
91
92    }
93
94    /**
95     * @depends testSetup
96     * @expectedException \Sabre\DAV\Exception\MethodNotAllowed
97     */
98    function testPut() {
99
100        $children = $this->inbox->getChildren();
101        $this->assertTrue($children[0] instanceof SchedulingObject);
102
103        $children[0]->put('');
104
105    }
106
107    /**
108     * @depends testSetup
109     */
110    function testDelete() {
111
112        $children = $this->inbox->getChildren();
113        $this->assertTrue($children[0] instanceof SchedulingObject);
114
115        $obj = $children[0];
116        $obj->delete();
117
118        $children2 =  $this->inbox->getChildren();
119        $this->assertEquals(count($children)-1, count($children2));
120
121    }
122
123    /**
124     * @depends testSetup
125     */
126    function testGetLastModified() {
127
128        $children = $this->inbox->getChildren();
129        $this->assertTrue($children[0] instanceof SchedulingObject);
130
131        $obj = $children[0];
132
133        $lastMod = $obj->getLastModified();
134        $this->assertTrue(is_int($lastMod) || ctype_digit($lastMod) || is_null($lastMod));
135
136    }
137
138    /**
139     * @depends testSetup
140     */
141    function testGetSize() {
142
143        $children = $this->inbox->getChildren();
144        $this->assertTrue($children[0] instanceof SchedulingObject);
145
146        $obj = $children[0];
147
148        $size = $obj->getSize();
149        $this->assertInternalType('int', $size);
150
151    }
152
153    function testGetOwner() {
154
155        $children = $this->inbox->getChildren();
156        $this->assertTrue($children[0] instanceof SchedulingObject);
157
158        $obj = $children[0];
159        $this->assertEquals('principals/user1', $obj->getOwner());
160
161    }
162
163    function testGetGroup() {
164
165        $children = $this->inbox->getChildren();
166        $this->assertTrue($children[0] instanceof SchedulingObject);
167
168        $obj = $children[0];
169        $this->assertNull($obj->getGroup());
170
171    }
172
173    function testGetACL() {
174
175        $expected = array(
176            array(
177                'privilege' => '{DAV:}read',
178                'principal' => 'principals/user1',
179                'protected' => true,
180            ),
181            array(
182                'privilege' => '{DAV:}write',
183                'principal' => 'principals/user1',
184                'protected' => true,
185            ),
186            array(
187                'privilege' => '{DAV:}read',
188                'principal' => 'principals/user1/calendar-proxy-write',
189                'protected' => true,
190            ),
191            array(
192                'privilege' => '{DAV:}write',
193                'principal' => 'principals/user1/calendar-proxy-write',
194                'protected' => true,
195            ),
196            array(
197                'privilege' => '{DAV:}read',
198                'principal' => 'principals/user1/calendar-proxy-read',
199                'protected' => true,
200            ),
201        );
202
203        $children = $this->inbox->getChildren();
204        $this->assertTrue($children[0] instanceof SchedulingObject);
205
206        $obj = $children[0];
207        $this->assertEquals($expected, $obj->getACL());
208
209    }
210
211    function testDefaultACL() {
212
213        $backend = new Backend\MockScheduling([], []);
214        $calendarObject = new SchedulingObject($backend, ['calendarid' => 1, 'uri' => 'foo', 'principaluri' => 'principals/user1' ]);
215        $expected = array(
216            array(
217                'privilege' => '{DAV:}read',
218                'principal' => 'principals/user1',
219                'protected' => true,
220            ),
221            array(
222                'privilege' => '{DAV:}write',
223                'principal' => 'principals/user1',
224                'protected' => true,
225            ),
226            array(
227                'privilege' => '{DAV:}read',
228                'principal' => 'principals/user1/calendar-proxy-write',
229                'protected' => true,
230            ),
231            array(
232                'privilege' => '{DAV:}write',
233                'principal' => 'principals/user1/calendar-proxy-write',
234                'protected' => true,
235            ),
236            array(
237                'privilege' => '{DAV:}read',
238                'principal' => 'principals/user1/calendar-proxy-read',
239                'protected' => true,
240            ),
241        );
242        $this->assertEquals($expected, $calendarObject->getACL());
243
244
245    }
246
247    /**
248     * @expectedException Sabre\DAV\Exception\MethodNotAllowed
249     */
250    function testSetACL() {
251
252        $children = $this->inbox->getChildren();
253        $this->assertTrue($children[0] instanceof SchedulingObject);
254
255        $obj = $children[0];
256        $obj->setACL(array());
257
258    }
259
260    function testGet() {
261
262        $children = $this->inbox->getChildren();
263        $this->assertTrue($children[0] instanceof SchedulingObject);
264
265        $obj = $children[0];
266
267        $this->assertEquals($this->data, $obj->get());
268
269    }
270
271    function testGetRefetch() {
272
273        $backend = new Backend\MockScheduling();
274        $backend->createSchedulingObject('principals/user1', 'foo', 'foo');
275
276        $obj = new SchedulingObject($backend, array(
277            'calendarid' => 1,
278            'uri' => 'foo',
279            'principaluri' => 'principals/user1',
280        ));
281
282        $this->assertEquals('foo', $obj->get());
283
284    }
285
286    function testGetEtag1() {
287
288        $objectInfo = array(
289            'calendardata' => 'foo',
290            'uri' => 'foo',
291            'etag' => 'bar',
292            'calendarid' => 1
293        );
294
295        $backend = new Backend\MockScheduling(array(), array());
296        $obj = new SchedulingObject($backend, $objectInfo);
297
298        $this->assertEquals('bar', $obj->getETag());
299
300    }
301
302    function testGetEtag2() {
303
304        $objectInfo = array(
305            'calendardata' => 'foo',
306            'uri' => 'foo',
307            'calendarid' => 1
308        );
309
310        $backend = new Backend\MockScheduling(array(), array());
311        $obj = new SchedulingObject($backend, $objectInfo);
312
313        $this->assertEquals('"' . md5('foo') . '"', $obj->getETag());
314
315    }
316
317    function testGetSupportedPrivilegesSet() {
318
319        $objectInfo = array(
320            'calendardata' => 'foo',
321            'uri' => 'foo',
322            'calendarid' => 1
323        );
324
325        $backend = new Backend\MockScheduling(array(), array());
326        $obj = new SchedulingObject($backend, $objectInfo);
327        $this->assertNull($obj->getSupportedPrivilegeSet());
328
329    }
330
331    function testGetSize1() {
332
333        $objectInfo = array(
334            'calendardata' => 'foo',
335            'uri' => 'foo',
336            'calendarid' => 1
337        );
338
339        $backend = new Backend\MockScheduling(array(), array());
340        $obj = new SchedulingObject($backend, $objectInfo);
341        $this->assertEquals(3, $obj->getSize());
342
343    }
344
345    function testGetSize2() {
346
347        $objectInfo = array(
348            'uri' => 'foo',
349            'calendarid' => 1,
350            'size' => 4,
351        );
352
353        $backend = new Backend\MockScheduling(array(), array());
354        $obj = new SchedulingObject($backend, $objectInfo);
355        $this->assertEquals(4, $obj->getSize());
356
357    }
358
359    function testGetContentType() {
360
361        $objectInfo = array(
362            'uri' => 'foo',
363            'calendarid' => 1,
364        );
365
366        $backend = new Backend\MockScheduling(array(), array());
367        $obj = new SchedulingObject($backend, $objectInfo);
368        $this->assertEquals('text/calendar; charset=utf-8', $obj->getContentType());
369
370    }
371
372    function testGetContentType2() {
373
374        $objectInfo = array(
375            'uri' => 'foo',
376            'calendarid' => 1,
377            'component' => 'VEVENT',
378        );
379
380        $backend = new Backend\MockScheduling(array(), array());
381        $obj = new SchedulingObject($backend, $objectInfo);
382        $this->assertEquals('text/calendar; charset=utf-8; component=VEVENT', $obj->getContentType());
383
384    }
385    function testGetACL2() {
386
387        $objectInfo = array(
388            'uri' => 'foo',
389            'calendarid' => 1,
390            'acl' => [],
391        );
392
393        $backend = new Backend\MockScheduling(array(), array());
394        $obj = new SchedulingObject($backend, $objectInfo);
395        $this->assertEquals([], $obj->getACL());
396
397    }
398}
399