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