1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/share/awl/inc' . PATH_SEPARATOR . 'inc');
require_once('RRule.php');
require_once('vCalendar.php');
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
$c = (object) array();
$c->rrule_loop_limit = 100;
// 1PM-2PM Monday-Thursday (only for one week), NZ time
$base_cal = new vCalendar("BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VEVENT
CREATED:20190117T001216Z
LAST-MODIFIED:20190117T001233Z
DTSTAMP:20190117T001233Z
UID:dae6404d-1ce0-42d0-af3b-0d303034197b
SUMMARY:New Event
RRULE:FREQ=DAILY;UNTIL=20190124T000000Z
DTSTART;TZID=Pacific/Auckland:20190121T130000
DTEND;TZID=Pacific/Auckland:20190121T140000
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR");
$tuesday_renamed_cal = new vCalendar("
BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VEVENT
CREATED:20190117T001216Z
LAST-MODIFIED:20190117T001805Z
DTSTAMP:20190117T001805Z
UID:d0d2df67-df7c-4b07-b729-221af3681c09
SUMMARY:New Event
RRULE:FREQ=DAILY;UNTIL=20190124T000000Z
DTSTART;TZID=Pacific/Auckland:20190121T130000
DTEND;TZID=Pacific/Auckland:20190121T140000
TRANSP:OPAQUE
X-MOZ-GENERATION:1
END:VEVENT
BEGIN:VEVENT
CREATED:20190117T001741Z
LAST-MODIFIED:20190117T001805Z
DTSTAMP:20190117T001805Z
UID:d0d2df67-df7c-4b07-b729-221af3681c09
SUMMARY:Tuesday has been renamed
RECURRENCE-ID;TZID=Pacific/Auckland:20190122T130000
DTSTART;TZID=Pacific/Auckland:20190122T130000
DTEND;TZID=Pacific/Auckland:20190122T140000
TRANSP:OPAQUE
X-MOZ-GENERATION:1
SEQUENCE:1
END:VEVENT
END:VCALENDAR
");
$tuesday_renamed_cal_order_swapped = new vCalendar("
BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VEVENT
CREATED:20190117T001741Z
LAST-MODIFIED:20190117T001805Z
DTSTAMP:20190117T001805Z
UID:d0d2df67-df7c-4b07-b729-221af3681c09
SUMMARY:Tuesday has been renamed
RECURRENCE-ID;TZID=Pacific/Auckland:20190122T130000
DTSTART;TZID=Pacific/Auckland:20190122T130000
DTEND;TZID=Pacific/Auckland:20190122T140000
TRANSP:OPAQUE
X-MOZ-GENERATION:1
SEQUENCE:1
END:VEVENT
BEGIN:VEVENT
CREATED:20190117T001216Z
LAST-MODIFIED:20190117T001805Z
DTSTAMP:20190117T001805Z
UID:d0d2df67-df7c-4b07-b729-221af3681c09
SUMMARY:New Event
RRULE:FREQ=DAILY;UNTIL=20190124T000000Z
DTSTART;TZID=Pacific/Auckland:20190121T130000
DTEND;TZID=Pacific/Auckland:20190121T140000
TRANSP:OPAQUE
X-MOZ-GENERATION:1
END:VEVENT
END:VCALENDAR
");
$count_cal = new vCalendar("BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VEVENT
CREATED:20190117T001216Z
LAST-MODIFIED:20190117T001233Z
DTSTAMP:20190117T001233Z
UID:dae6404d-1ce0-42d0-af3b-0d303034197b
SUMMARY:New Event
RRULE:FREQ=WEEKLY;COUNT=4;BYDAY=MO,TU,WE,TH
DTSTART;TZID=Pacific/Auckland:20190121T130000
DTEND;TZID=Pacific/Auckland:20190121T140000
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR");
/**
* A simplified model of get_freebusy, which works off of a passed-in vCalendar
* rather than making SQL queries
*/
function get_freebusyish(vCalendar $cal) {
$expansion = expand_event_instances($cal, new RepeatRuleDateTime('2019-01-01 00:00:00'))->GetComponents(['VEVENT' => true]);
$result = array();
foreach ($expansion as $k => $instance) {
// The same logic used in freebusy-functions (apart from default timezone
// handling, which isn't really under test here)
$start_date = new RepeatRuleDateTime($instance->GetProperty('DTSTART'));
$duration = $instance->GetProperty('DURATION');
$duration = (!isset($duration) ? 'P1D' : $duration->Value());
$end_date = clone($start_date);
$end_date->modify($duration);
array_push($result, $start_date->UTC() .'/'. $end_date->UTC());
}
sort($result);
return $result;
}
final class ExpansionTest extends TestCase
{
const expected_freebusyish_for_base = [
'20190121T000000Z/20190121T010000Z',
'20190122T000000Z/20190122T010000Z',
'20190123T000000Z/20190123T010000Z',
'20190124T000000Z/20190124T010000Z',
];
#[PreserveGlobalState('enabled')]
#[RunInSeparateProcess]
public function testUnmodifiedCal() {
global $base_cal;
self::assertEquals(
self::expected_freebusyish_for_base,
get_freebusyish($base_cal)
);
}
#[PreserveGlobalState('enabled')]
#[RunInSeparateProcess]
public function testTueRenamed() {
global $tuesday_renamed_cal;
self::assertEquals(
self::expected_freebusyish_for_base,
get_freebusyish($tuesday_renamed_cal)
);
}
#[PreserveGlobalState('enabled')]
#[RunInSeparateProcess]
public function testTueRenamedSwapped() {
global $tuesday_renamed_cal_order_swapped;
self::assertEquals(
self::expected_freebusyish_for_base,
get_freebusyish($tuesday_renamed_cal_order_swapped)
);
}
#[PreserveGlobalState('enabled')]
#[RunInSeparateProcess]
public function testCalWithCount() {
global $count_cal;
self::assertEquals(
self::expected_freebusyish_for_base,
get_freebusyish($count_cal)
);
}
}
|