ea55ffaccef197270887cae489703e148489c9b2
[tine20] / tests / tine20 / Calendar / Export / ICalTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Calendar
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2010 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  * 
10  */
11
12 /**
13  * Test helper
14  */
15 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
16
17 /**
18  * Test class for Calendar_ICalTests
19  */
20 class Calendar_Export_ICalTest extends PHPUnit_Framework_TestCase //extends Calendar_TestCase
21 {
22     public function setUp()
23     {
24         $this->_testEvent = new Calendar_Model_Event(array(
25             'dtstart'       => '2010-12-30 12:00:00',
26             'dtend'         => '2010-12-30 13:00:00',
27             'originator_tz' => 'Europe/Berlin',
28             'summary'       => 'take a nap',
29             'description'   => 'hard working man needs some silence',
30             'uid'           => Tinebase_Record_Abstract::generateUID(),
31             'seq'           => 3,
32             'transp'        => Calendar_Model_Event::TRANSP_OPAQUE,
33             'class'         => Calendar_Model_Event::CLASS_PUBLIC,
34             'location'      => 'couch',
35             'priority'      => 1,
36             'rrule'         => 'FREQ=DAILY;INTERVAL=1;UNTIL=2015-12-30 13:00:00'
37         ));
38     }
39     
40     public function testExport()
41     {
42         $exporter = new Calendar_Export_Ical();
43         $ics = $exporter->eventToIcal($this->_testEvent);
44 //        echo $ics;
45
46         // assert basics
47         $this->assertEquals(1, preg_match("/SUMMARY:{$this->_testEvent->summary}\r\n/", $ics), 'SUMMARY not correct');
48         
49         // assert dtstart/dtend tz
50         $this->assertEquals(1, preg_match("/DTSTART;TZID=Europe\/Berlin:20101230T130000\r\n/", $ics), 'DTSTART not correct');
51         $this->assertEquals(1, preg_match("/DTEND;TZID=Europe\/Berlin:20101230T140000\r\n/", $ics), 'DTEND not correct');
52         
53         // assert vtimezone
54         $this->assertEquals(1, preg_match("/BEGIN:VTIMEZONE\r\n/", $ics), 'VTIMEZONE missing');
55         $this->assertEquals(1, preg_match("/BEGIN:DAYLIGHT\r\nTZOFFSETFROM:\+0100\r\nTZOFFSETTO:\+0200\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\n/", $ics), 'DAYLIGHT not correct');
56
57         // assert rrule
58         $this->assertEquals(1, preg_match("/RRULE:FREQ=DAILY;INTERVAL=1;UNTIL=20151230T140000Z\r\n/", $ics), 'RRULE broken');
59     }
60     
61     public function testExportAllDayEvent()
62     {
63         $this->_testEvent->is_all_day_event = TRUE;
64         $this->_testEvent->dtend = $this->_testEvent->dtend->addDay(1);
65         
66         $exporter = new Calendar_Export_Ical();
67         $ics = $exporter->eventToIcal($this->_testEvent);
68 //        echo $ics;
69         
70         // assert dtstart/dtend tz
71         $this->assertEquals(1, preg_match("/DTSTART;VALUE=DATE:20101230\r\n/", $ics), 'DTSTART not correct');
72         $this->assertEquals(1, preg_match("/DTEND;VALUE=DATE:20101231\r\n/", $ics), 'DTEND not correct');
73         
74     }
75     
76     public function testExportRecurId()
77     {
78         $exceptions = new Tinebase_Record_RecordSet('Calendar_Model_Event');
79         $nextOccurance = Calendar_Model_Rrule::computeNextOccurrence($this->_testEvent, $exceptions, $this->_testEvent->dtend);
80         
81         $exporter = new Calendar_Export_Ical();
82         $ics = $exporter->eventToIcal($nextOccurance);
83 //        echo $ics;
84
85         // assert recurid
86         $this->assertEquals(1, preg_match("/RECURRENCE-ID;TZID=Europe\/Berlin:20101231T130000\r\n/", $ics), 'RECURRENCE-ID broken');
87     }
88     
89     public function testExportExdate()
90     {
91         $exceptions = new Tinebase_Record_RecordSet('Calendar_Model_Event');
92         $from = clone $this->_testEvent->dtstart;
93         $until = clone $this->_testEvent->dtend;
94         $until->addDay(2);
95         
96         $recurSet = Calendar_Model_Rrule::computeRecurrenceSet($this->_testEvent, $exceptions, $from, $until);
97         $this->_testEvent->exdate = $recurSet->dtstart;
98
99         $exporter = new Calendar_Export_Ical();
100         $ics = $exporter->eventToIcal($this->_testEvent);
101 //        echo $ics;
102
103         // assert exdate
104 //        $this->assertEquals(1, preg_match("/EXDATE;TZID=Europe\/Berlin:20101231T130000,20110101T130000\r\n/", $ics), 'RECURRENCE-ID broken');
105         $this->assertEquals(1, preg_match("/EXDATE;TZID=Europe\/Berlin:20101231T130000\r\n/", $ics), 'RECURRENCE-ID broken');
106         $this->assertEquals(1, preg_match("/EXDATE;TZID=Europe\/Berlin:20110101T130000\r\n/", $ics), 'RECURRENCE-ID broken');
107     }
108     
109     public function testExportRecurSet()
110     {
111         $exceptions = new Tinebase_Record_RecordSet('Calendar_Model_Event');
112         $from = clone $this->_testEvent->dtstart;
113         $until = clone $this->_testEvent->dtend;
114         $until->addDay(2);
115         
116         $recurSet = Calendar_Model_Rrule::computeRecurrenceSet($this->_testEvent, $exceptions, $from, $until);
117         $this->_testEvent->exdate = array($recurSet->dtstart[0]);
118         
119         $eventSet = new Tinebase_Record_RecordSet('Calendar_Model_Event', array(
120             $this->_testEvent,
121             $recurSet[1]
122         ));
123
124         $exporter = new Calendar_Export_Ical();
125         $ics = $exporter->eventToIcal($eventSet);
126 //        echo $ics;
127
128         $this->assertEquals(2, preg_match_all('/BEGIN:VEVENT\r\n/', $ics, $matches), 'There should be exactly 2 VEVENT compontents');
129     }
130     
131     public function testExportOrganizer()
132     {
133         $this->_testEvent->organizer = array_value('pwulf', Zend_Registry::get('personas'))->contact_id;
134         
135         $exporter = new Calendar_Export_Ical();
136         $ics = $exporter->eventToIcal($this->_testEvent);
137 //        echo $ics;
138
139         // assert organizer
140         $this->assertEquals(1, preg_match("/ORGANIZER;CN=\"Wulf, Paul\":mailto:pwulf@tine20.org\r\n/", $ics), 'ORGANIZER missing/broken');
141     }
142     
143     public function testExportAttendee()
144     {
145         $this->_testEvent->attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
146             array(
147                 'role'          => Calendar_Model_Attender::ROLE_REQUIRED,
148                 'status'        => Calendar_Model_Attender::STATUS_ACCEPTED,
149                 'user_type'     => Calendar_Model_Attender::USERTYPE_USER,
150                 'user_id'       => array_value('pwulf', Zend_Registry::get('personas'))->contact_id,
151             )
152         ));
153         
154         $exporter = new Calendar_Export_Ical();
155         $ics = $exporter->eventToIcal($this->_testEvent);
156 //        echo $ics;
157
158         // assert organizer
159         $this->assertEquals(1, preg_match("/ATTENDEE;CN=\"Wulf, Paul\";CUTYPE=INDIVIDUAL;EMAIL=pwulf@tine20.org;PARTSTAT=\r\n ACCEPTED;ROLE=REQ-PARTICIPANT;RSVP=FALSE:mailto:pwulf@tine20.org\r\n/", $ics), 'ATTENDEE missing/broken');
160     }
161     
162     public function testExportAlarm()
163     {
164         // alarm handling is ugly...
165         $alarmTime = clone $this->_testEvent->dtstart;
166         $alarmTime->sub(15, Tinebase_DateTime::MODIFIER_MINUTE);
167             
168             
169         $this->_testEvent->alarms = new Tinebase_Record_RecordSet('Tinebase_Model_Alarm', array(
170             new Tinebase_Model_Alarm(array(
171                 'minutes_before' => 15,
172                 'alarm_time'     => $alarmTime
173             ), TRUE)
174         ));
175         
176         $exporter = new Calendar_Export_Ical();
177         $ics = $exporter->eventToIcal($this->_testEvent);
178 //        echo $ics;
179
180         // assert organizer
181         $this->assertEquals(1, preg_match("/TRIGGER:-PT15M\r\n/", $ics), 'TRIGGER missing/broken');
182     }
183 }