MDL-58145 block_myoverview: format timestamps using userdate helper
[moodle.git] / calendar / tests / event_factory_test.php
CommitLineData
5ca71c2d
CB
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Event factory test.
19 *
20 * @package core_calendar
21 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use core_calendar\local\event\factories\event_factory;
28use core_calendar\local\interfaces\event_interface;
29
30/**
31 * Event factory testcase.
32 *
33 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_calendar_event_factory_testcase extends advanced_testcase {
37 /**
38 * Test event class getters.
39 *
40 * @dataProvider create_instance_testcases()
2a5cce61
CB
41 * @param \stdClass $dbrow Row from the event table.
42 * @param callable $actioncallbackapplier Action callback applier.
43 * @param callable $visibilitycallbackapplier Visibility callback applier.
7aedfe32 44 * @param callable $bailoutcheck Early bail out check function.
2a5cce61
CB
45 * @param string $expectedclass Class the factory is expected to produce.
46 * @param mixed $expectedattributevalue Expected value of the modified attribute.
5ca71c2d
CB
47 */
48 public function test_create_instance(
49 $dbrow,
2a5cce61
CB
50 callable $actioncallbackapplier,
51 callable $visibilitycallbackapplier,
7aedfe32 52 callable $bailoutcheck,
2a5cce61 53 $expectedclass,
5ca71c2d
CB
54 $expectedattributevalue
55 ) {
56 $this->resetAfterTest(true);
57 $this->setAdminUser();
58 $event = $this->create_event();
1d364a89 59 $coursecache = [];
7aedfe32
CB
60 $factory = new event_factory(
61 $actioncallbackapplier,
62 $visibilitycallbackapplier,
63 $bailoutcheck,
64 $coursecache
65 );
5ca71c2d
CB
66 $dbrow->id = $event->id;
67 $instance = $factory->create_instance($dbrow);
68
2a5cce61
CB
69 if ($expectedclass) {
70 $this->assertInstanceOf($expectedclass, $instance);
71 }
72
73 if (is_null($expectedclass)) {
74 $this->assertNull($instance);
75 }
76
77 if ($expectedattributevalue) {
78 $this->assertEquals($instance->testattribute, $expectedattributevalue);
79 }
80 }
81
82 /**
83 * Test invalid callback exception.
84 *
85 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
86 */
87 public function test_invalid_action_callback() {
88 $this->resetAfterTest(true);
89 $this->setAdminUser();
90 $event = $this->create_event();
1d364a89 91 $coursecache = [];
2a5cce61
CB
92 $factory = new event_factory(
93 function () {
94 return 'hello';
95 },
96 function () {
97 return true;
1d364a89 98 },
7aedfe32
CB
99 function () {
100 return false;
101 },
1d364a89 102 $coursecache
2a5cce61
CB
103 );
104
105 $factory->create_instance(
106 (object)[
107 'id' => $event->id,
108 'name' => 'test',
109 'description' => 'Test description',
110 'format' => 2,
111 'courseid' => 1,
112 'groupid' => 1,
113 'userid' => 1,
114 'repeatid' => 1,
115 'modulename' => 'assign',
116 'instance' => 1,
117 'eventtype' => 'due',
118 'timestart' => 123456789,
119 'timeduration' => 12,
120 'timemodified' => 123456789,
121 'timesort' => 123456789,
122 'visible' => 1,
123 'subscriptionid' => 1
124 ]
125 );
5ca71c2d
CB
126 }
127
128 /**
129 * Test invalid callback exception.
130 *
131 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
132 */
2a5cce61 133 public function test_invalid_visibility_callback() {
5ca71c2d
CB
134 $this->resetAfterTest(true);
135 $this->setAdminUser();
136 $event = $this->create_event();
1d364a89 137 $coursecache = [];
2a5cce61
CB
138 $factory = new event_factory(
139 function ($event) {
140 return $event;
141 },
142 function () {
143 return 'asdf';
1d364a89 144 },
7aedfe32
CB
145 function () {
146 return false;
147 },
148 $coursecache
149 );
150
151 $factory->create_instance(
152 (object)[
153 'id' => $event->id,
154 'name' => 'test',
155 'description' => 'Test description',
156 'format' => 2,
157 'courseid' => 1,
158 'groupid' => 1,
159 'userid' => 1,
160 'repeatid' => 1,
161 'modulename' => 'assign',
162 'instance' => 1,
163 'eventtype' => 'due',
164 'timestart' => 123456789,
165 'timeduration' => 12,
166 'timemodified' => 123456789,
167 'timesort' => 123456789,
168 'visible' => 1,
169 'subscriptionid' => 1
170 ]
171 );
172 }
173
174 /**
175 * Test invalid callback exception.
176 *
177 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
178 */
179 public function test_invalid_bail_callback() {
180 $this->resetAfterTest(true);
181 $this->setAdminUser();
182 $event = $this->create_event();
183 $coursecache = [];
184 $factory = new event_factory(
185 function ($event) {
186 return $event;
187 },
188 function () {
189 return true;
190 },
191 function () {
192 return 'asdf';
193 },
1d364a89 194 $coursecache
2a5cce61 195 );
5ca71c2d
CB
196
197 $factory->create_instance(
198 (object)[
199 'id' => $event->id,
200 'name' => 'test',
201 'description' => 'Test description',
202 'format' => 2,
203 'courseid' => 1,
204 'groupid' => 1,
205 'userid' => 1,
206 'repeatid' => 1,
207 'modulename' => 'assign',
208 'instance' => 1,
209 'eventtype' => 'due',
210 'timestart' => 123456789,
211 'timeduration' => 12,
212 'timemodified' => 123456789,
213 'timesort' => 123456789,
214 'visible' => 1,
215 'subscriptionid' => 1
216 ]
217 );
218 }
219
1d364a89
CB
220 /**
221 * Test the factory's course cache.
222 */
223 public function test_course_cache() {
224 $this->resetAfterTest(true);
225 $this->setAdminUser();
226 $course = self::getDataGenerator()->create_course();
227 $event = $this->create_event(['courseid' => $course->id]);
228 $coursecache = [];
229 $factory = new event_factory(
230 function ($event) {
231 return $event;
232 },
233 function () {
234 return true;
235 },
7aedfe32
CB
236 function () {
237 return false;
238 },
1d364a89
CB
239 $coursecache
240 );
241
242 $instance = $factory->create_instance(
243 (object)[
244 'id' => $event->id,
245 'name' => 'test',
246 'description' => 'Test description',
247 'format' => 2,
248 'courseid' => $course->id,
249 'groupid' => 1,
250 'userid' => 1,
251 'repeatid' => 1,
252 'modulename' => 'assign',
253 'instance' => 1,
254 'eventtype' => 'due',
255 'timestart' => 123456789,
256 'timeduration' => 12,
257 'timemodified' => 123456789,
258 'timesort' => 123456789,
259 'visible' => 1,
260 'subscriptionid' => 1
261 ]
262 );
263
264 $instance->get_course()->get('fullname');
265 $this->assertArrayHasKey($course->id, $coursecache);
266 }
267
268 /**
269 * Testcases for the create instance test.
270 *
271 * @return array Array of testcases.
272 */
5ca71c2d
CB
273 public function create_instance_testcases() {
274 return [
2a5cce61 275 'Sample event record with event exposed' => [
5ca71c2d
CB
276 'dbrow' => (object)[
277 'name' => 'Test event',
278 'description' => 'Hello',
279 'format' => 1,
280 'courseid' => 1,
281 'groupid' => 1,
282 'userid' => 1,
283 'repeatid' => null,
284 'modulename' => 'Test module',
285 'instance' => 1,
286 'eventtype' => 'Due',
287 'timestart' => 123456789,
288 'timeduration' => 123456789,
289 'timemodified' => 123456789,
290 'timesort' => 123456789,
291 'visible' => true,
292 'subscriptionid' => 1
293 ],
2a5cce61 294 'actioncallbackapplier' => function(event_interface $event) {
5ca71c2d
CB
295 $event->testattribute = 'Hello';
296 return $event;
297 },
2a5cce61
CB
298 'visibilitycallbackapplier' => function(event_interface $event) {
299 return true;
300 },
7aedfe32
CB
301 'bailoutcheck' => function() {
302 return false;
303 },
2a5cce61 304 event_interface::class,
5ca71c2d 305 'Hello'
2a5cce61
CB
306 ],
307 'Sample event record with event hidden' => [
308 'dbrow' => (object)[
309 'name' => 'Test event',
310 'description' => 'Hello',
311 'format' => 1,
312 'courseid' => 1,
313 'groupid' => 1,
314 'userid' => 1,
315 'repeatid' => null,
316 'modulename' => 'Test module',
317 'instance' => 1,
318 'eventtype' => 'Due',
319 'timestart' => 123456789,
320 'timeduration' => 123456789,
321 'timemodified' => 123456789,
322 'timesort' => 123456789,
323 'visible' => true,
324 'subscriptionid' => 1
325 ],
326 'actioncallbackapplier' => function(event_interface $event) {
327 $event->testattribute = 'Hello';
328 return $event;
329 },
330 'visibilitycallbackapplier' => function(event_interface $event) {
331 return false;
332 },
7aedfe32
CB
333 'bailoutcheck' => function() {
334 return false;
335 },
336 null,
337 null
338 ],
339 'Sample event record with early bail' => [
340 'dbrow' => (object)[
341 'name' => 'Test event',
342 'description' => 'Hello',
343 'format' => 1,
344 'courseid' => 1,
345 'groupid' => 1,
346 'userid' => 1,
347 'repeatid' => null,
348 'modulename' => 'Test module',
349 'instance' => 1,
350 'eventtype' => 'Due',
351 'timestart' => 123456789,
352 'timeduration' => 123456789,
353 'timemodified' => 123456789,
354 'timesort' => 123456789,
355 'visible' => true,
356 'subscriptionid' => 1
357 ],
358 'actioncallbackapplier' => function(event_interface $event) {
359 $event->testattribute = 'Hello';
360 return $event;
361 },
362 'visibilitycallbackapplier' => function(event_interface $event) {
363 return true;
364 },
365 'bailoutcheck' => function() {
366 return true;
367 },
2a5cce61
CB
368 null,
369 null
5ca71c2d
CB
370 ]
371 ];
372 }
373
374 /**
375 * Helper function to create calendar events using the old code.
376 *
377 * @param array $properties A list of calendar event properties to set
378 * @return event
379 */
380 protected function create_event($properties = []) {
381 $record = new \stdClass();
382 $record->name = 'event name';
383 $record->eventtype = 'global';
384 $record->timestart = time();
385 $record->timeduration = 0;
386 $record->timesort = 0;
387 $record->type = 1;
388 $record->courseid = 0;
389
390 foreach ($properties as $name => $value) {
391 $record->$name = $value;
392 }
393
394 $event = new \core_calendar\event($record);
395 return $event->create($record, false);
396 }
397}