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