9578: Cli Ex / Import Calendar doesn't work
[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 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         parent::setUp();
39     }
40     
41     public function testExport()
42     {
43         $exporter = new Calendar_Export_Ical();
44         $ics = $exporter->eventToIcal($this->_testEvent);
45 //        echo $ics;
46
47         // assert basics
48         $this->assertEquals(1, preg_match("/SUMMARY:{$this->_testEvent->summary}\r\n/", $ics), 'SUMMARY not correct');
49         
50         // assert dtstart/dtend tz
51         $this->assertEquals(1, preg_match("/DTSTART;TZID=Europe\/Berlin:20101230T130000\r\n/", $ics), 'DTSTART not correct');
52         $this->assertEquals(1, preg_match("/DTEND;TZID=Europe\/Berlin:20101230T140000\r\n/", $ics), 'DTEND not correct');
53         
54         // assert vtimezone
55         $this->assertEquals(1, preg_match("/BEGIN:VTIMEZONE\r\n/", $ics), 'VTIMEZONE missing');
56         $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');
57
58         // assert rrule
59         $this->assertEquals(1, preg_match("/RRULE:FREQ=DAILY;INTERVAL=1;UNTIL=20151230T140000Z\r\n/", $ics), 'RRULE broken');
60     }
61     
62     public function testExportAllDayEvent()
63     {
64         $this->_testEvent->is_all_day_event = TRUE;
65         $this->_testEvent->dtend = $this->_testEvent->dtend->addDay(1);
66         
67         $exporter = new Calendar_Export_Ical();
68         $ics = $exporter->eventToIcal($this->_testEvent);
69 //        echo $ics;
70         
71         // assert dtstart/dtend tz
72         $this->assertEquals(1, preg_match("/DTSTART;VALUE=DATE:20101230\r\n/", $ics), 'DTSTART not correct');
73         $this->assertEquals(1, preg_match("/DTEND;VALUE=DATE:20101231\r\n/", $ics), 'DTEND not correct');
74         
75     }
76     
77     public function testExportRecurId()
78     {
79         $exceptions = new Tinebase_Record_RecordSet('Calendar_Model_Event');
80         $nextOccurance = Calendar_Model_Rrule::computeNextOccurrence($this->_testEvent, $exceptions, $this->_testEvent->dtend);
81         
82         $exporter = new Calendar_Export_Ical();
83         $ics = $exporter->eventToIcal($nextOccurance);
84 //        echo $ics;
85
86         // assert recurid
87         $this->assertEquals(1, preg_match("/RECURRENCE-ID;TZID=Europe\/Berlin:20101231T130000\r\n/", $ics), 'RECURRENCE-ID broken');
88     }
89     
90     public function testExportExdate()
91     {
92         $exceptions = new Tinebase_Record_RecordSet('Calendar_Model_Event');
93         $from = clone $this->_testEvent->dtstart;
94         $until = clone $this->_testEvent->dtend;
95         $until->addDay(2);
96         
97         $recurSet = Calendar_Model_Rrule::computeRecurrenceSet($this->_testEvent, $exceptions, $from, $until);
98         $this->_testEvent->exdate = $recurSet->dtstart;
99
100         $exporter = new Calendar_Export_Ical();
101         $ics = $exporter->eventToIcal($this->_testEvent);
102 //        echo $ics;
103
104         // assert exdate
105 //        $this->assertEquals(1, preg_match("/EXDATE;TZID=Europe\/Berlin:20101231T130000,20110101T130000\r\n/", $ics), 'RECURRENCE-ID broken');
106         $this->assertEquals(1, preg_match("/EXDATE;TZID=Europe\/Berlin:20101231T130000\r\n/", $ics), 'RECURRENCE-ID broken');
107         $this->assertEquals(1, preg_match("/EXDATE;TZID=Europe\/Berlin:20110101T130000\r\n/", $ics), 'RECURRENCE-ID broken');
108     }
109     
110     public function testExportRecurSet()
111     {
112         $exceptions = new Tinebase_Record_RecordSet('Calendar_Model_Event');
113         $from = clone $this->_testEvent->dtstart;
114         $until = clone $this->_testEvent->dtend;
115         $until->addDay(2);
116         
117         $recurSet = Calendar_Model_Rrule::computeRecurrenceSet($this->_testEvent, $exceptions, $from, $until);
118         $this->_testEvent->exdate = array($recurSet->dtstart[0]);
119         
120         $eventSet = new Tinebase_Record_RecordSet('Calendar_Model_Event', array(
121             $this->_testEvent,
122             $recurSet[1]
123         ));
124
125         $exporter = new Calendar_Export_Ical();
126         $ics = $exporter->eventToIcal($eventSet);
127 //        echo $ics;
128
129         $this->assertEquals(2, preg_match_all('/BEGIN:VEVENT\r\n/', $ics, $matches), 'There should be exactly 2 VEVENT compontents');
130     }
131     
132     public function testExportOrganizer()
133     {
134         $this->_testEvent->organizer = array_value('pwulf', Zend_Registry::get('personas'))->contact_id;
135         
136         $exporter = new Calendar_Export_Ical();
137         $ics = $exporter->eventToIcal($this->_testEvent);
138 //        echo $ics;
139
140         // assert organizer
141         $this->assertEquals(1, preg_match("/ORGANIZER;CN=\"Wulf, Paul\":mailto:pwulf@tine20.org\r\n/", $ics), 'ORGANIZER missing/broken');
142     }
143     
144     public function testExportAttendee()
145     {
146         $this->_testEvent->attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
147             array(
148                 'role'          => Calendar_Model_Attender::ROLE_REQUIRED,
149                 'status'        => Calendar_Model_Attender::STATUS_ACCEPTED,
150                 'user_type'     => Calendar_Model_Attender::USERTYPE_USER,
151                 'user_id'       => array_value('pwulf', Zend_Registry::get('personas'))->contact_id,
152             )
153         ));
154         
155         $exporter = new Calendar_Export_Ical();
156         $ics = $exporter->eventToIcal($this->_testEvent);
157 //        echo $ics;
158
159         // assert organizer
160         $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');
161     }
162     
163     public function testExportAlarm()
164     {
165         // alarm handling is ugly...
166         $alarmTime = clone $this->_testEvent->dtstart;
167         $alarmTime->sub(15, Tinebase_DateTime::MODIFIER_MINUTE);
168             
169             
170         $this->_testEvent->alarms = new Tinebase_Record_RecordSet('Tinebase_Model_Alarm', array(
171             new Tinebase_Model_Alarm(array(
172                 'minutes_before' => 15,
173                 'alarm_time'     => $alarmTime
174             ), TRUE)
175         ));
176         
177         $exporter = new Calendar_Export_Ical();
178         $ics = $exporter->eventToIcal($this->_testEvent);
179 //        echo $ics;
180
181         // assert organizer
182         $this->assertEquals(1, preg_match("/TRIGGER:-PT15M\r\n/", $ics), 'TRIGGER missing/broken');
183     }
184     
185     /**
186      * test ical cli export
187      * 
188      */
189     public function testCliExport()
190     {
191         $eventData = $this->_getEvent(TRUE)->toArray();
192         $this->_uit = new Calendar_Frontend_Json();
193         $persistentEventData = $this->_uit->saveEvent($eventData);
194         
195         $this->_testNeedsTransaction();
196         $cmd = realpath(__DIR__ . "/../../../../tine20/tine20.php") . ' --method Calendar.exportICS ' .
197             $this->_testCalendar->getId() ;
198         
199         $cmd = TestServer::assembleCliCommand($cmd, TRUE);
200         exec($cmd, $output);
201         $result = implode(',', $output);
202         
203         $failMessage = print_r($output, TRUE);
204         $this->assertEquals(1, preg_match("/SUMMARY:{$eventData['summary']}/", $result), 'DESCRIPTION not correct');
205     }
206 }