d02bec90724fef7ec2b3faee9524f099d9fd8278
[tine20] / tests / tine20 / Calendar / Backend / SqlTest.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 AGPL Version 3
7  * @copyright   Copyright (c) 2009-2014 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  */
10
11 /**
12  * Test class for Calendar_Backend_Sql
13  * 
14  * @package     Calendar
15  */
16 class Calendar_Backend_SqlTest extends Calendar_TestCase
17 {
18     public function testCreateEvent()
19     {
20         $event = $this->_getEvent();
21         $event->creation_time = Tinebase_DateTime::now();
22         $persistentEvent = $this->_backend->create($event);
23         
24         $event->attendee->cal_event_id = $persistentEvent->getId();
25         foreach ($event->attendee as $attender) {
26             $this->_backend->createAttendee($attender);
27         }
28
29         $loadedPersitentEvent = $this->_backend->get($persistentEvent->getId());
30         $this->assertEquals($event->summary, $loadedPersitentEvent->summary);
31         $this->_assertAttendee($event->attendee, $loadedPersitentEvent->attendee);
32         
33         return $loadedPersitentEvent;
34     }
35     
36     public function testUpdateEvent()
37     {
38         $event = $this->_getEvent();
39         $persistentEvent = $this->_backend->create($event);
40         
41         $event->attendee->cal_event_id = $persistentEvent->getId();
42         foreach ($event->attendee as $attender) {
43             $this->_backend->createAttendee($attender);
44         }
45         
46         $persistentEvent->dtstart->addHour(3);
47         $persistentEvent->dtend->addHour(3);
48         $persistentEvent->summary = 'Robert Lembke:';
49         $persistentEvent->description = 'Wer spät zu Bett geht und früh heraus muß, weiß, woher das Wort Morgengrauen kommt';
50         
51         $updatedEvent = $this->_backend->update($persistentEvent);
52         $loadedPersitentEvent = $this->_backend->get($persistentEvent->getId());
53         
54         $this->assertEquals($loadedPersitentEvent->summary, $updatedEvent->summary);
55         $this->assertTrue($loadedPersitentEvent->dtstart->equals($updatedEvent->dtstart));
56         $this->_assertAttendee($loadedPersitentEvent->attendee, $updatedEvent->attendee);
57     }
58     
59     public function testGetEvent()
60     {
61         $event = $this->_getEvent();
62         $persistentEvent = $this->_backend->create($event);
63         
64         $event->attendee->cal_event_id = $persistentEvent->getId();
65         foreach ($event->attendee as $attender) {
66             $this->_backend->createAttendee($attender);
67         }
68         
69         $loadedEvent = $this->_backend->get($persistentEvent->getId());
70         
71         $this->assertEquals($event->summary, $loadedEvent->summary);
72         $this->_assertAttendee($event->attendee, $loadedEvent->attendee);
73     }
74     
75     public function testDeleteEvent()
76     {
77         $event = $this->_getEvent();
78         $persistentEvent = $this->_backend->create($event);
79         
80         $this->_backend->delete($persistentEvent->getId());
81
82         $attendeeBackend = new Calendar_Backend_Sql_Attendee($this->_backend->getAdapter());
83         $this->assertEquals(0, count($attendeeBackend->getMultipleByProperty($persistentEvent->getId(), 'cal_event_id')));
84         $this->setExpectedException('Tinebase_Exception_NotFound');
85         $loadedEvent = $this->_backend->get($persistentEvent->getId());
86         
87     }
88     
89     /**
90      * test search events
91      */
92     public function testSearchEvents()
93     {
94         $from = '2009-04-03 00:00:00';
95         $until = '2009-04-10 23:59:59';
96         
97         $events = new Tinebase_Record_RecordSet('Calendar_Model_Event', array(
98             array(
99                 'dtstart'      => '2009-04-02 22:00:00',
100                 'dtend'        => '2009-04-02 23:59:59',
101                 'summary'      => 'non recur event ending before search period => should _not_ be found',
102                 'attendee'     => $this->_getAttendee(),
103                 'container_id' => $this->_getTestCalendar()->getId(),
104                 'organizer'    => Tinebase_Core::getUser()->getId(),
105                 'uid'          => Calendar_Model_Event::generateUID(),
106                 Tinebase_Model_Grants::GRANT_READ    => true,
107             ),
108             array(
109                 'dtstart'      => '2009-04-02 23:30:00',
110                 'dtend'        => '2009-04-03 00:30:00',
111                 'summary'      => 'non recur event ending within search period => should be found',
112                 'attendee'     => $this->_getAttendee(),
113                 'container_id' => $this->_getTestCalendar()->getId(),
114                 'organizer'    => Tinebase_Core::getUser()->getId(),
115                 'uid'          => Calendar_Model_Event::generateUID(),
116                 Tinebase_Model_Grants::GRANT_READ    => true
117             ),
118             array(
119                 'dtstart'      => '2009-04-06 12:00:00',
120                 'dtend'        => '2009-04-07 12:00:00',
121                 'summary'      => 'non recur event completly within search period => should be found',
122                 'attendee'     => $this->_getAttendee(),
123                 'container_id' => $this->_getTestCalendar()->getId(),
124                 'organizer'    => Tinebase_Core::getUser()->getId(),
125                 'uid'          => Calendar_Model_Event::generateUID(),
126                 Tinebase_Model_Grants::GRANT_READ    => true
127             ),
128             array(
129                 'dtstart'      => '2009-04-10 23:30:00',
130                 'dtend'        => '2009-04-11 00:30:00',
131                 'summary'      => 'non recur event starting within search period => should be found',
132                 'attendee'     => $this->_getAttendee(),
133                 'container_id' => $this->_getTestCalendar()->getId(),
134                 'organizer'    => Tinebase_Core::getUser()->getId(),
135                 'uid'          => Calendar_Model_Event::generateUID(),
136                 Tinebase_Model_Grants::GRANT_READ    => true
137             ),
138             array(
139                 'dtstart'      => '2009-04-11 00:00:00',
140                 'dtend'        => '2009-04-11 02:00:00',
141                 'summary'      => 'non recur event starting after search period => should _not_ be found',
142                 'attendee'     => $this->_getAttendee(),
143                 'container_id' => $this->_getTestCalendar()->getId(),
144                 'organizer'    => Tinebase_Core::getUser()->getId(),
145                 'uid'          => Calendar_Model_Event::generateUID(),
146                 Tinebase_Model_Grants::GRANT_READ    => true
147             ),
148             array(
149                 'dtstart'      => '2009-03-27 22:00:00',
150                 'dtend'        => '2009-03-27 23:59:59',
151                 'rrule'        => 'FREQ=DAILY;INTERVAL=1;UNTIL=2009-04-02 23:59:59',
152                 'summary'      => 'recur event ending before search period => should _not_ be found',
153                 'attendee'     => $this->_getAttendee(),
154                 'container_id' => $this->_getTestCalendar()->getId(),
155                 'organizer'    => Tinebase_Core::getUser()->getId(),
156                 'uid'          => Calendar_Model_Event::generateUID(),
157                 'rrule_until'  => '2009-04-02 23:59:59',
158                 Tinebase_Model_Grants::GRANT_READ    => true,
159                 'originator_tz' => Tinebase_Core::getUserTimezone()
160             ),
161             array(
162                 'dtstart'      => '2009-03-27 22:00:00',
163                 'dtend'        => '2009-03-27 23:59:59',
164                 'rrule'        => 'FREQ=DAILY;INTERVAL=1;UNTIL=2009-04-05 23:59:59',
165                 'summary'      => 'recur event ending within search period => should be found',
166                 'attendee'     => $this->_getAttendee(),
167                 'container_id' => $this->_getTestCalendar()->getId(),
168                 'organizer'    => Tinebase_Core::getUser()->getId(),
169                 'uid'          => Calendar_Model_Event::generateUID(),
170                 'rrule_until'  => '2009-04-05 23:59:59',
171                 Tinebase_Model_Grants::GRANT_READ    => true,
172                 'originator_tz' => Tinebase_Core::getUserTimezone()
173             ),
174             array(
175                 'dtstart'      => '2009-04-03 22:00:00',
176                 'dtend'        => '2009-04-03 23:59:59',
177                 'rrule'        => 'FREQ=DAILY;INTERVAL=1;UNTIL=2009-04-06 23:59:59',
178                 'summary'      => 'recur event completly within search period => should be found',
179                 'attendee'     => $this->_getAttendee(),
180                 'container_id' => $this->_getTestCalendar()->getId(),
181                 'organizer'    => Tinebase_Core::getUser()->getId(),
182                 'uid'          => Calendar_Model_Event::generateUID(),
183                 'rrule_until'  => '2009-04-06 23:59:59',
184                 Tinebase_Model_Grants::GRANT_READ    => true,
185                 'originator_tz' => Tinebase_Core::getUserTimezone()
186             ),
187             array(
188                 'dtstart'      => '2009-04-03 22:00:00',
189                 'dtend'        => '2009-04-03 23:59:59',
190                 'rrule'        => 'FREQ=DAILY;INTERVAL=1;UNTIL=2009-04-12 23:59:59',
191                 'summary'      => 'recur event starting within search period => should be found',
192                 'attendee'     => $this->_getAttendee(),
193                 'container_id' => $this->_getTestCalendar()->getId(),
194                 'organizer'    => Tinebase_Core::getUser()->getId(),
195                 'uid'          => Calendar_Model_Event::generateUID(),
196                 'rrule_until'  => '2009-04-12 23:59:59',
197                 Tinebase_Model_Grants::GRANT_READ    => true,
198                 'originator_tz' => Tinebase_Core::getUserTimezone()
199             ),
200             array(
201                 'dtstart'      => '2009-04-11 00:00:00',
202                 'dtend'        => '2009-04-11 02:00:00',
203                 'rrule'        => 'FREQ=DAILY;INTERVAL=1;UNTIL=2009-04-15 02:00:00',
204                 'summary'      => 'recur event starting after search period => should _not_ be found',
205                 'attendee'     => $this->_getAttendee(),
206                 'container_id' => $this->_getTestCalendar()->getId(),
207                 'organizer'    => Tinebase_Core::getUser()->getId(),
208                 'uid'          => Calendar_Model_Event::generateUID(),
209                 'rrule_until'  => '2009-04-15 02:00:00',
210                 Tinebase_Model_Grants::GRANT_READ    => true,
211                 'originator_tz' => Tinebase_Core::getUserTimezone()
212             )
213         ));
214         
215         foreach ($events as $event) {
216             $persistentEvent = $this->_backend->create($event);
217             $event->attendee->cal_event_id = $persistentEvent->getId();
218             foreach ($event->attendee as $attender) {
219                 $this->_backend->createAttendee($attender);
220             }
221         }
222         
223         $filter = new Calendar_Model_EventFilter(array(
224             array('field' => 'container_id', 'operator' => 'equals', 'value' => $this->_getTestCalendar()->getId()),
225             array('field' => 'period'      , 'operator' => 'within', 'value' => array(
226                 'from'  => $from,
227                 'until' => $until
228             )),
229         ));
230         
231         $eventsFound = $this->_backend->search($filter, new Tinebase_Model_Pagination());
232         
233         $eventsFoundIds = $eventsFound->getArrayOfIds();
234         
235         foreach ($events as $event) {
236             $eventId = $event->getId();
237             if (strpos($event->summary, '_not_') === false) {
238                 $this->assertTrue(in_array($eventId, $eventsFoundIds), 'The following event is missing in the search result :' . print_r($event->toArray(), true));
239             } else {
240                 $this->assertFalse(in_array($eventId, $eventsFoundIds), 'The following event is in the search result, but should not be :' . print_r($event->toArray(), true));
241             }
242         }
243         
244         $expectedAttendee = $this->_getAttendee();
245         foreach ($eventsFound as $fetchedEvent) {
246             $this->_assertAttendee($expectedAttendee, $fetchedEvent->attendee);
247         }
248     }
249     
250     public function testExDate()
251     {
252         $event = $this->_getEvent();
253         $event->rrule = 'FREQ=WEEKLY;INTERVAL=1;UNTIL=2009-05-20 23:59:59';
254         $event->exdate = array(
255             new Tinebase_DateTime('2009-04-29 06:00:00'),
256             new Tinebase_DateTime('2009-05-06 06:00:00'),
257         );
258         
259         $persistentEvent = $this->_backend->create($event);
260         
261         $this->assertEquals(2, count($persistentEvent->exdate), 'We put in two exdates, we should get out two exdates!');
262         foreach ($persistentEvent->exdate as $exdate) {
263             $this->assertTrue($exdate->equals($event->exdate[0]) || $exdate->equals($event->exdate[1]), 'exdates mismatch');
264         }
265     }
266     
267     /**
268      * asserts attendee
269      *
270      * @param Tinebase_Record_RecordSet $_expected
271      * @param Tinebase_Record_RecordSet $_actual
272      */
273     protected function _assertAttendee($_expected, $_actual)
274     {
275         $this->assertEquals(count($_expected), count($_actual));
276     }
277     
278     /**
279      * testDeleteDuplicateEvents
280      * 
281      * @see 0008182: event with lots of exceptions breaks calendar sync
282      */
283     public function testDeleteDuplicateEvents()
284     {
285         $this->_backend->deleteByProperty('Wakeup', 'summary');
286         
287         $event1 = $this->testCreateEvent();
288         sleep(1);
289         $event2 = $this->testCreateEvent();
290         sleep(1);
291         $event3 = $this->testCreateEvent();
292         
293         $filter = new Calendar_Model_EventFilter(array(array(
294             'field'    => 'summary',
295             'operator' => 'equals',
296             'value'    => 'Wakeup',
297         )));
298         $deletedDuplicates = $this->_backend->deleteDuplicateEvents($filter, FALSE);
299         
300         $this->assertGreaterThan(1, $deletedDuplicates);
301         $this->_backend->get($event1->getId());
302         $this->assertEquals(0, $event1->is_deleted);
303     }
304 }