comparison plugins/libcalendaring/tests/libcalendaring.php @ 4:888e774ee983

libcalendar plugin as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:57:56 -0500
parents
children
comparison
equal deleted inserted replaced
3:f6fe4b6ae66a 4:888e774ee983
1 <?php
2
3 /**
4 * libcalendaring plugin's utility functions tests
5 *
6 * @author Thomas Bruederli <bruederli@kolabsys.com>
7 *
8 * Copyright (C) 2015, Kolab Systems AG <contact@kolabsys.com>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 class libcalendaring_test extends PHPUnit_Framework_TestCase
25 {
26 function setUp()
27 {
28 require_once __DIR__ . '/../libcalendaring.php';
29 }
30
31 /**
32 * libcalendaring::parse_alarm_value()
33 */
34 function test_parse_alarm_value()
35 {
36 $alarm = libcalendaring::parse_alarm_value('-15M');
37 $this->assertEquals('15', $alarm[0]);
38 $this->assertEquals('-M', $alarm[1]);
39 $this->assertEquals('-PT15M', $alarm[3]);
40
41 $alarm = libcalendaring::parse_alarm_value('-PT5H');
42 $this->assertEquals('5', $alarm[0]);
43 $this->assertEquals('-H', $alarm[1]);
44
45 $alarm = libcalendaring::parse_alarm_value('P0DT1H0M0S');
46 $this->assertEquals('1', $alarm[0]);
47 $this->assertEquals('+H', $alarm[1]);
48
49 // FIXME: this should return something like (1140 + 120 + 30)M
50 $alarm = libcalendaring::parse_alarm_value('-P1DT2H30M');
51 // $this->assertEquals('1590', $alarm[0]);
52 // $this->assertEquals('-M', $alarm[1]);
53
54 $alarm = libcalendaring::parse_alarm_value('@1420722000');
55 $this->assertInstanceOf('DateTime', $alarm[0]);
56 }
57
58 /**
59 * libcalendaring::get_next_alarm()
60 */
61 function test_get_next_alarm()
62 {
63 // alarm 10 minutes before event
64 $date = date('Ymd', strtotime('today + 2 days'));
65 $event = array(
66 'start' => new DateTime($date . 'T160000Z'),
67 'end' => new DateTime($date . 'T200000Z'),
68 'valarms' => array(
69 array(
70 'trigger' => '-PT10M',
71 'action' => 'DISPLAY',
72 ),
73 ),
74 );
75 $alarm = libcalendaring::get_next_alarm($event);
76 $this->assertEquals($event['valarms'][0]['action'], $alarm['action']);
77 $this->assertEquals(strtotime($date . 'T155000Z'), $alarm['time']);
78
79 // alarm 1 hour after event start
80 $event['valarms'] = array(
81 array(
82 'trigger' => '+PT1H',
83 ),
84 );
85 $alarm = libcalendaring::get_next_alarm($event);
86 $this->assertEquals('DISPLAY', $alarm['action']);
87 $this->assertEquals(strtotime($date . 'T170000Z'), $alarm['time']);
88
89 // alarm 1 hour before event end
90 $event['valarms'] = array(
91 array(
92 'trigger' => '-PT1H',
93 'related' => 'END',
94 ),
95 );
96 $alarm = libcalendaring::get_next_alarm($event);
97 $this->assertEquals('DISPLAY', $alarm['action']);
98 $this->assertEquals(strtotime($date . 'T190000Z'), $alarm['time']);
99
100 // alarm 1 hour after event end
101 $event['valarms'] = array(
102 array(
103 'trigger' => 'PT1H',
104 'related' => 'END',
105 ),
106 );
107 $alarm = libcalendaring::get_next_alarm($event);
108 $this->assertEquals('DISPLAY', $alarm['action']);
109 $this->assertEquals(strtotime($date . 'T210000Z'), $alarm['time']);
110
111 // ignore past alarms
112 $event['start'] = new DateTime('today 22:00:00');
113 $event['end'] = new DateTime('today 23:00:00');
114 $event['valarms'] = array(
115 array(
116 'trigger' => '-P2D',
117 'action' => 'EMAIL',
118 ),
119 array(
120 'trigger' => '-PT30M',
121 'action' => 'DISPLAY',
122 ),
123 );
124 $alarm = libcalendaring::get_next_alarm($event);
125 $this->assertEquals('DISPLAY', $alarm['action']);
126 $this->assertEquals(strtotime('today 21:30:00'), $alarm['time']);
127
128 // absolute alarm date/time
129 $event['valarms'] = array(
130 array('trigger' => new DateTime('today 20:00:00'))
131 );
132 $alarm = libcalendaring::get_next_alarm($event);
133 $this->assertEquals($event['valarms'][0]['trigger']->format('U'), $alarm['time']);
134
135 // no alarms for cancelled events
136 $event['status'] = 'CANCELLED';
137 $alarm = libcalendaring::get_next_alarm($event);
138 $this->assertEquals(null, $alarm);
139 }
140
141 /**
142 * libcalendaring::part_is_vcalendar()
143 */
144 function test_part_is_vcalendar()
145 {
146 $part = new StdClass;
147 $part->mimetype = 'text/plain';
148 $part->filename = 'event.ics';
149
150 $this->assertFalse(libcalendaring::part_is_vcalendar($part));
151
152 $part->mimetype = 'text/calendar';
153 $this->assertTrue(libcalendaring::part_is_vcalendar($part));
154
155 $part->mimetype = 'text/x-vcalendar';
156 $this->assertTrue(libcalendaring::part_is_vcalendar($part));
157
158 $part->mimetype = 'application/ics';
159 $this->assertTrue(libcalendaring::part_is_vcalendar($part));
160
161 $part->mimetype = 'application/x-any';
162 $this->assertTrue(libcalendaring::part_is_vcalendar($part));
163 }
164
165 /**
166 * libcalendaring::to_rrule()
167 */
168 function test_to_rrule()
169 {
170 $rrule = array(
171 'FREQ' => 'MONTHLY',
172 'BYDAY' => '2WE',
173 'INTERVAL' => 2,
174 'UNTIL' => new DateTime('2025-05-01 18:00:00 CEST'),
175 );
176
177 $s = libcalendaring::to_rrule($rrule);
178
179 $this->assertRegExp('/FREQ='.$rrule['FREQ'].'/', $s, "Recurrence Frequence");
180 $this->assertRegExp('/INTERVAL='.$rrule['INTERVAL'].'/', $s, "Recurrence Interval");
181 $this->assertRegExp('/BYDAY='.$rrule['BYDAY'].'/', $s, "Recurrence BYDAY");
182 $this->assertRegExp('/UNTIL=20250501T160000Z/', $s, "Recurrence End date (in UTC)");
183 }
184 }