ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilCalendarRecurrenceCalculationTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  protected $backupGlobals = false;
27  protected Container $dic;
28 
29  protected function setUp(): void
30  {
31  $this->initDependencies();
32  parent::setUp();
33  }
34 
35  public function testCalculatorConstruct(): void
36  {
37  $entry = new ilCalendarEntry(0);
38  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
39  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
40  $entry->setFullday(true);
41  $entry->setTitle('First');
42  $rec = new ilCalendarRecurrence(0);
43 
45  $entry,
46  $rec
47  );
48  $this->assertInstanceOf(ilCalendarRecurrenceCalculator::class, $calc);
49  }
50 
51  public function testYearly()
52  {
53  $entry = new ilCalendarEntry(0);
54  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
55  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
56  $entry->setFullday(true);
57 
58  $rec = new ilCalendarRecurrence(0);
59  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_YEARLY);
60  $rec->setInterval(1);
61  $rec->setFrequenceUntilCount(1);
62 
64  $entry,
65  $rec
66  );
67  $dl = $calc->calculateDateList(
68  new ilDateTime('2021-12-31', IL_CAL_DATE),
69  new ilDate('2023-12-31', IL_CAL_DATE),
70  -1
71  );
72  $this->assertCount(1, $dl);
73  foreach ($dl as $date) {
74  $this->assertTrue(strcmp($date->get(IL_CAL_DATE), '2022-01-01') === 0);
75  }
76  }
77 
78  public function testYearlyByDay(): void
79  {
80  $entry = new ilCalendarEntry(0);
81  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
82  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
83  $entry->setFullday(true);
84 
85  // first saturday and second monday in january and december
86  $rec = new ilCalendarRecurrence(0);
87  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_YEARLY);
88  $rec->setBYDAY('1SA,2MO');
89  $rec->setBYMONTH('1,12');
90  $rec->setInterval(1);
91  $rec->setFrequenceUntilCount(6);
92 
94  $entry,
95  $rec
96  );
97  $dl = $calc->calculateDateList(
98  new ilDateTime('2021-12-31', IL_CAL_DATE),
99  new ilDate('2023-12-31', IL_CAL_DATE),
100  -1
101  );
102  $result = new ilDateList(ilDateList::TYPE_DATE);
103  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
104  $result->add(new ilDate('2022-01-10', IL_CAL_DATE));
105  $result->add(new ilDate('2022-12-03', IL_CAL_DATE));
106  $result->add(new ilDate('2022-12-12', IL_CAL_DATE));
107  $result->add(new ilDate('2023-01-07', IL_CAL_DATE));
108  $result->add(new ilDate('2023-01-09', IL_CAL_DATE));
109 
110  $this->assertTrue($result == $dl);
111  }
112 
113  public function testMonthly()
114  {
115  $entry = new ilCalendarEntry(0);
116  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
117  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
118  $entry->setFullday(true);
119 
120  $rec = new ilCalendarRecurrence(0);
121  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_MONTHLY);
122  $rec->setInterval(1);
123  $rec->setFrequenceUntilCount(2);
124 
125  $calc = new ilCalendarRecurrenceCalculator(
126  $entry,
127  $rec
128  );
129  $dl = $calc->calculateDateList(
130  new ilDateTime('2021-12-31', IL_CAL_DATE),
131  new ilDate('2023-12-31', IL_CAL_DATE),
132  -1
133  );
134  $result = new ilDateList(ilDateList::TYPE_DATE);
135  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
136  $result->add(new ilDate('2022-02-01', IL_CAL_DATE));
137 
138  $this->assertTrue($result == $dl);
139  }
140 
141  public function testMonthlyByDay()
142  {
143  $entry = new ilCalendarEntry(0);
144  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
145  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
146  $entry->setFullday(true);
147 
148  // next two first days of the month which are on saturday => (2022-01-01, 2022-10-01)
149  $rec = new ilCalendarRecurrence(0);
150  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_MONTHLY);
151  $rec->setBYDAY('SA');
152  $rec->setBYMONTHDAY('1');
153  $rec->setInterval(1);
154  $rec->setFrequenceUntilCount(2);
155 
156  $calc = new ilCalendarRecurrenceCalculator(
157  $entry,
158  $rec
159  );
160  $dl = $calc->calculateDateList(
161  new ilDateTime('2021-12-31', IL_CAL_DATE),
162  new ilDate('2023-12-31', IL_CAL_DATE),
163  -1
164  );
165  $result = new ilDateList(ilDateList::TYPE_DATE);
166  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
167  $result->add(new ilDate('2022-10-01', IL_CAL_DATE));
168  $this->assertTrue($result == $dl);
169  }
170 
171  public function testWeekly(): void
172  {
173  $entry = new ilCalendarEntry(0);
174  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
175  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
176  $entry->setFullday(true);
177 
178  $rec = new ilCalendarRecurrence(0);
179  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_WEEKLY);
180  $rec->setInterval(1);
181  $rec->setFrequenceUntilCount(2);
182 
183  $calc = new ilCalendarRecurrenceCalculator(
184  $entry,
185  $rec
186  );
187  $dl = $calc->calculateDateList(
188  new ilDateTime('2021-12-31', IL_CAL_DATE),
189  new ilDate('2023-12-31', IL_CAL_DATE),
190  -1
191  );
192  $result = new ilDateList(ilDateList::TYPE_DATE);
193  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
194  $result->add(new ilDate('2022-01-08', IL_CAL_DATE));
195 
196  $this->assertTrue($result == $dl);
197  }
198 
199  public function testWeeklyByDay(): void
200  {
201  $entry = new ilCalendarEntry(0);
202  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
203  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
204  $entry->setFullday(true);
205 
206  $rec = new ilCalendarRecurrence(0);
207  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_WEEKLY);
208  $rec->setBYDAY('MO,SA');
209  $rec->setInterval(1);
210  $rec->setFrequenceUntilCount(3);
211 
212  $calc = new ilCalendarRecurrenceCalculator(
213  $entry,
214  $rec
215  );
216  $dl = $calc->calculateDateList(
217  new ilDateTime('2021-12-31', IL_CAL_DATE),
218  new ilDate('2023-12-31', IL_CAL_DATE),
219  -1
220  );
221  $result = new ilDateList(ilDateList::TYPE_DATE);
222  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
223  $result->add(new ilDate('2022-01-03', IL_CAL_DATE));
224  $result->add(new ilDate('2022-01-08', IL_CAL_DATE));
225 
226  $this->assertTrue($result == $dl);
227  }
228 
229  public function testDaily(): void
230  {
231  $entry = new ilCalendarEntry(0);
232  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
233  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
234  $entry->setFullday(true);
235 
236  $rec = new ilCalendarRecurrence(0);
237  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_DAILY);
238  $rec->setInterval(1);
239  $rec->setFrequenceUntilCount(2);
240 
241  $calc = new ilCalendarRecurrenceCalculator(
242  $entry,
243  $rec
244  );
245  $dl = $calc->calculateDateList(
246  new ilDateTime('2021-12-31', IL_CAL_DATE),
247  new ilDate('2023-12-31', IL_CAL_DATE),
248  -1
249  );
250  $result = new ilDateList(ilDateList::TYPE_DATE);
251  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
252  $result->add(new ilDate('2022-01-02', IL_CAL_DATE));
253 
254  $this->assertTrue($result == $dl);
255  }
256 
257  public function testUntilEndDateInclusive(): void
258  {
259  $entry = new ilCalendarEntry(0);
260  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
261  $entry->setEnd(new ilDate('2022-01-01', IL_CAL_DATE));
262  $entry->setFullday(true);
263 
264  $rec = new ilCalendarRecurrence(0);
265  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_DAILY);
266  $rec->setInterval(1);
267  $rec->setFrequenceUntilDate(new ilDate('2022-01-02', IL_CAL_DATE));
268 
269  $calc = new ilCalendarRecurrenceCalculator(
270  $entry,
271  $rec
272  );
273  $dl = $calc->calculateDateList(
274  new ilDateTime('2021-12-31', IL_CAL_DATE),
275  new ilDate('2023-12-31', IL_CAL_DATE),
276  -1
277  );
278  $result = new ilDateList(ilDateList::TYPE_DATE);
279  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
280  $result->add(new ilDate('2022-01-02', IL_CAL_DATE));
281 
282  $this->assertTrue($result == $dl);
283  }
284 
285  protected function setGlobalVariable(string $name, $value): void
286  {
287  global $DIC;
288 
289  $GLOBALS[$name] = $value;
290  unset($DIC[$name]);
291  $DIC[$name] = static function (\ILIAS\DI\Container $c) use ($value) {
292  return $value;
293  };
294  }
295 
296  protected function initDependencies(): void
297  {
298  $this->dic = new Container();
299  $GLOBALS['DIC'] = $this->dic;
300 
301  $this->setGlobalVariable('ilDB', $this->createMock(ilDBInterface::class));
302  $this->setGlobalVariable('lng', $this->createMock(ilLanguage::class));
303  $this->setGlobalVariable('ilErr', $this->createMock(ilErrorHandling::class));
304 
305  $logger = $this->getMockBuilder(ilLogger::class)
306  ->disableOriginalConstructor()
307  ->getMock();
308 
309  $logger_factory = $this->getMockBuilder(ilLoggerFactory::class)
310  ->disableOriginalConstructor()
311  ->onlyMethods(['getComponentLogger'])
312  ->getMock();
313  $logger_factory->method('getComponentLogger')->willReturn($logger);
314  $this->setGlobalVariable('ilLoggerFactory', $logger_factory);
315  }
316 }
$c
Definition: deliver.php:25
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
$GLOBALS["DIC"]
Definition: wac.php:53
Calculates an ilDateList for a given calendar entry and recurrence rule.
List of dates.
global $DIC
Definition: shib_login.php:26
const IL_CAL_DATE