ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilCalendarRecurrenceCalculationTest.php
Go to the documentation of this file.
1 <?php
2 
19 require_once("vendor/composer/vendor/autoload.php");
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()
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 testMonthly()
79  {
80  $entry = new ilCalendarEntry(0);
81  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
82  $entry->setEnd(new ilDate('2023-01-01', IL_CAL_DATE));
83  $entry->setFullday(true);
84 
85  $rec = new ilCalendarRecurrence(0);
86  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_MONTHLY);
87  $rec->setInterval(1);
88  $rec->setFrequenceUntilCount(2);
89 
91  $entry,
92  $rec
93  );
94  $dl = $calc->calculateDateList(
95  new ilDateTime('2021-12-31', IL_CAL_DATE),
96  new ilDate('2023-12-31', IL_CAL_DATE),
97  -1
98  );
99  $result = new ilDateList(ilDateList::TYPE_DATE);
100  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
101  $result->add(new ilDate('2022-02-01', IL_CAL_DATE));
102 
103  $this->assertTrue($result == $dl);
104  }
105 
106  public function testMonthlyByDay()
107  {
108  $entry = new ilCalendarEntry(0);
109  $entry->setStart(new ilDate('2022-01-01', IL_CAL_DATE));
110  $entry->setEnd(new ilDate('2023-01-01', IL_CAL_DATE));
111  $entry->setFullday(true);
112 
113  // next two first days of the month which are on saturday => (2022-01-01, 2022-10-01)
114  $rec = new ilCalendarRecurrence(0);
115  $rec->setFrequenceType(ilCalendarRecurrence::FREQ_MONTHLY);
116  $rec->setBYDAY('SA');
117  $rec->setBYMONTHDAY('1');
118  $rec->setInterval(1);
119  $rec->setFrequenceUntilCount(2);
120 
121  $calc = new ilCalendarRecurrenceCalculator(
122  $entry,
123  $rec
124  );
125  $dl = $calc->calculateDateList(
126  new ilDateTime('2021-12-31', IL_CAL_DATE),
127  new ilDate('2023-12-31', IL_CAL_DATE),
128  -1
129  );
130  $result = new ilDateList(ilDateList::TYPE_DATE);
131  $result->add(new ilDate('2022-01-01', IL_CAL_DATE));
132  $result->add(new ilDate('2022-10-01', IL_CAL_DATE));
133  $this->assertTrue($result == $dl);
134  }
135 
136  protected function setGlobalVariable(string $name, $value): void
137  {
138  global $DIC;
139 
140  $GLOBALS[$name] = $value;
141  unset($DIC[$name]);
142  $DIC[$name] = static function (\ILIAS\DI\Container $c) use ($value) {
143  return $value;
144  };
145  }
146 
147  protected function initDependencies(): void
148  {
149  $this->dic = new Container();
150  $GLOBALS['DIC'] = $this->dic;
151 
152  $this->setGlobalVariable('ilDB', $this->createMock(ilDBInterface::class));
153  $this->setGlobalVariable('lng', $this->createMock(ilLanguage::class));
154  $this->setGlobalVariable('ilErr', $this->createMock(ilErrorHandling::class));
155 
156  $logger = $this->getMockBuilder(ilLogger::class)
157  ->disableOriginalConstructor()
158  ->getMock();
159 
160  $logger_factory = $this->getMockBuilder(ilLoggerFactory::class)
161  ->disableOriginalConstructor()
162  ->onlyMethods(['getComponentLogger'])
163  ->getMock();
164  $logger_factory->method('getComponentLogger')->willReturn($logger);
165  $this->setGlobalVariable('ilLoggerFactory', $logger_factory);
166  }
167 }
Model of calendar entry recurrcences based on iCalendar-RFC-5545.
$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:22
const IL_CAL_DATE