@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhutilCalendarRecurrenceTestCase extends PhutilTestCase {
4
5 public function testCalendarRecurrenceLists() {
6 $set = new PhutilCalendarRecurrenceSet();
7 $result = $set->getEventsBetween(null, null, 0xFFFF);
8 $this->assertEqual(
9 array(),
10 $result,
11 pht('Set with no sources.'));
12
13
14 $set = id(new PhutilCalendarRecurrenceSet())
15 ->addSource(new PhutilCalendarRecurrenceList());
16 $result = $set->getEventsBetween(null, null, 0xFFFF);
17 $this->assertEqual(
18 array(),
19 $result,
20 pht('Set with empty list source.'));
21
22
23 $list = array(
24 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
25 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
26 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
27 );
28
29 $source = id(new PhutilCalendarRecurrenceList())
30 ->setDates($list);
31
32 $set = id(new PhutilCalendarRecurrenceSet())
33 ->addSource($source);
34
35 $expect = array(
36 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
37 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
38 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
39 );
40
41 $result = $set->getEventsBetween(null, null, 0xFFFF);
42 $this->assertEqual(
43 mpull($expect, 'getISO8601'),
44 mpull($result, 'getISO8601'),
45 pht('Simple date list.'));
46
47 $list_a = array(
48 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
49 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
50 );
51
52 $list_b = array(
53 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
54 );
55
56 $source_a = id(new PhutilCalendarRecurrenceList())
57 ->setDates($list_a);
58
59 $source_b = id(new PhutilCalendarRecurrenceList())
60 ->setDates($list_b);
61
62 $set = id(new PhutilCalendarRecurrenceSet())
63 ->addSource($source_a)
64 ->addSource($source_b);
65
66 $expect = array(
67 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
68 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
69 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
70 );
71
72 $result = $set->getEventsBetween(null, null, 0xFFFF);
73 $this->assertEqual(
74 mpull($expect, 'getISO8601'),
75 mpull($result, 'getISO8601'),
76 pht('Multiple date lists.'));
77
78 $list_a = array(
79 // This is Jan 1, 3, 5, 7, 8 and 10, but listed out-of-order.
80 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160105T120000Z'),
81 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160108T120000Z'),
82 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
83 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
84 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160110T120000Z'),
85 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160107T120000Z'),
86 );
87
88 $list_b = array(
89 // This is Jan 2, 4, 5, 8, but listed out of order.
90 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
91 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160104T120000Z'),
92 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160105T120000Z'),
93 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160108T120000Z'),
94 );
95
96 $list_c = array(
97 // We're going to use this as an exception list.
98
99 // This is Jan 7 (listed in one other source), 8 (listed in two)
100 // and 9 (listed in none).
101 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160107T120000Z'),
102 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160108T120000Z'),
103 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160109T120000Z'),
104 );
105
106 $expect = array(
107 // From source A.
108 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
109 // From source B.
110 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
111 // From source A.
112 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
113 // From source B.
114 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160104T120000Z'),
115 // From source A and B. Should appear only once.
116 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160105T120000Z'),
117 // The 6th appears in no source.
118 // The 7th, 8th and 9th are excluded.
119 // The 10th is from source A.
120 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160110T120000Z'),
121 );
122
123 $list_a = id(new PhutilCalendarRecurrenceList())
124 ->setDates($list_a);
125
126 $list_b = id(new PhutilCalendarRecurrenceList())
127 ->setDates($list_b);
128
129 $list_c = id(new PhutilCalendarRecurrenceList())
130 ->setDates($list_c)
131 ->setIsExceptionSource(true);
132
133 $date_set = id(new PhutilCalendarRecurrenceSet())
134 ->addSource($list_b)
135 ->addSource($list_c)
136 ->addSource($list_a);
137
138 $date_set->setViewerTimezone('UTC');
139
140 $result = $date_set->getEventsBetween(null, null, 0xFFFF);
141 $this->assertEqual(
142 mpull($expect, 'getISO8601'),
143 mpull($result, 'getISO8601'),
144 pht('Set of all results in multiple lists with exclusions.'));
145
146
147 $expect = array(
148 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
149 );
150 $result = $date_set->getEventsBetween(null, null, 1);
151 $this->assertEqual(
152 mpull($expect, 'getISO8601'),
153 mpull($result, 'getISO8601'),
154 pht('Multiple lists, one result.'));
155
156 $expect = array(
157 2 => PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
158 3 => PhutilCalendarAbsoluteDateTime::newFromISO8601('20160104T120000Z'),
159 );
160 $result = $date_set->getEventsBetween(
161 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
162 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160104T120000Z'));
163 $this->assertEqual(
164 mpull($expect, 'getISO8601'),
165 mpull($result, 'getISO8601'),
166 pht('Multiple lists, time window.'));
167 }
168
169 public function testCalendarRecurrenceOffsets() {
170 $list = array(
171 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160101T120000Z'),
172 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z'),
173 PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120000Z'),
174 );
175
176 $source = id(new PhutilCalendarRecurrenceList())
177 ->setDates($list);
178
179 $set = id(new PhutilCalendarRecurrenceSet())
180 ->addSource($source);
181
182 $t1 = PhutilCalendarAbsoluteDateTime::newFromISO8601('20160102T120001Z');
183 $t2 = PhutilCalendarAbsoluteDateTime::newFromISO8601('20160103T120000Z');
184
185 $expect = array(
186 2 => $t2,
187 );
188
189 $result = $set->getEventsBetween($t1, null, 0xFFFF);
190 $this->assertEqual(
191 mpull($expect, 'getISO8601'),
192 mpull($result, 'getISO8601'),
193 pht('Correct event indexes with start date.'));
194 }
195
196}