weekly release 3.6dev
[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
71d1430c
CB
27global $CFG;
28require_once($CFG->dirroot . '/calendar/lib.php');
29
5ca71c2d 30use core_calendar\local\event\factories\event_factory;
42e76c3f 31use core_calendar\local\event\entities\event_interface;
5ca71c2d
CB
32
33/**
34 * Event factory testcase.
35 *
36 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class core_calendar_event_factory_testcase extends advanced_testcase {
40 /**
41 * Test event class getters.
42 *
43 * @dataProvider create_instance_testcases()
2a5cce61
CB
44 * @param \stdClass $dbrow Row from the event table.
45 * @param callable $actioncallbackapplier Action callback applier.
46 * @param callable $visibilitycallbackapplier Visibility callback applier.
7aedfe32 47 * @param callable $bailoutcheck Early bail out check function.
2a5cce61
CB
48 * @param string $expectedclass Class the factory is expected to produce.
49 * @param mixed $expectedattributevalue Expected value of the modified attribute.
5ca71c2d
CB
50 */
51 public function test_create_instance(
52 $dbrow,
2a5cce61
CB
53 callable $actioncallbackapplier,
54 callable $visibilitycallbackapplier,
7aedfe32 55 callable $bailoutcheck,
2a5cce61 56 $expectedclass,
5ca71c2d
CB
57 $expectedattributevalue
58 ) {
59 $this->resetAfterTest(true);
60 $this->setAdminUser();
61 $event = $this->create_event();
1d364a89 62 $coursecache = [];
e798fa76 63 $modulecache = [];
7aedfe32
CB
64 $factory = new event_factory(
65 $actioncallbackapplier,
66 $visibilitycallbackapplier,
67 $bailoutcheck,
e798fa76
CB
68 $coursecache,
69 $modulecache
7aedfe32 70 );
5ca71c2d
CB
71 $dbrow->id = $event->id;
72 $instance = $factory->create_instance($dbrow);
73
2a5cce61
CB
74 if ($expectedclass) {
75 $this->assertInstanceOf($expectedclass, $instance);
76 }
77
78 if (is_null($expectedclass)) {
79 $this->assertNull($instance);
80 }
81
82 if ($expectedattributevalue) {
83 $this->assertEquals($instance->testattribute, $expectedattributevalue);
84 }
85 }
86
87 /**
88 * Test invalid callback exception.
2a5cce61
CB
89 */
90 public function test_invalid_action_callback() {
91 $this->resetAfterTest(true);
92 $this->setAdminUser();
93 $event = $this->create_event();
1d364a89 94 $coursecache = [];
e798fa76 95 $modulecache = [];
2a5cce61
CB
96 $factory = new event_factory(
97 function () {
98 return 'hello';
99 },
100 function () {
101 return true;
1d364a89 102 },
7aedfe32
CB
103 function () {
104 return false;
105 },
e798fa76
CB
106 $coursecache,
107 $modulecache
2a5cce61
CB
108 );
109
20592f5f 110 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
2a5cce61
CB
111 $factory->create_instance(
112 (object)[
113 'id' => $event->id,
114 'name' => 'test',
115 'description' => 'Test description',
116 'format' => 2,
0085b0ea 117 'categoryid' => 0,
2a5cce61
CB
118 'courseid' => 1,
119 'groupid' => 1,
120 'userid' => 1,
bd01fd15 121 'repeatid' => 0,
2a5cce61
CB
122 'modulename' => 'assign',
123 'instance' => 1,
124 'eventtype' => 'due',
bd01fd15 125 'type' => CALENDAR_EVENT_TYPE_ACTION,
2a5cce61
CB
126 'timestart' => 123456789,
127 'timeduration' => 12,
128 'timemodified' => 123456789,
129 'timesort' => 123456789,
130 'visible' => 1,
131 'subscriptionid' => 1
132 ]
133 );
5ca71c2d
CB
134 }
135
136 /**
137 * Test invalid callback exception.
5ca71c2d 138 */
2a5cce61 139 public function test_invalid_visibility_callback() {
5ca71c2d
CB
140 $this->resetAfterTest(true);
141 $this->setAdminUser();
142 $event = $this->create_event();
1d364a89 143 $coursecache = [];
e798fa76 144 $modulecache = [];
2a5cce61
CB
145 $factory = new event_factory(
146 function ($event) {
147 return $event;
148 },
149 function () {
150 return 'asdf';
1d364a89 151 },
7aedfe32
CB
152 function () {
153 return false;
154 },
e798fa76
CB
155 $coursecache,
156 $modulecache
7aedfe32
CB
157 );
158
20592f5f 159 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
7aedfe32
CB
160 $factory->create_instance(
161 (object)[
162 'id' => $event->id,
163 'name' => 'test',
164 'description' => 'Test description',
165 'format' => 2,
0085b0ea 166 'categoryid' => 0,
7aedfe32
CB
167 'courseid' => 1,
168 'groupid' => 1,
169 'userid' => 1,
bd01fd15 170 'repeatid' => 0,
7aedfe32
CB
171 'modulename' => 'assign',
172 'instance' => 1,
173 'eventtype' => 'due',
bd01fd15 174 'type' => CALENDAR_EVENT_TYPE_ACTION,
7aedfe32
CB
175 'timestart' => 123456789,
176 'timeduration' => 12,
177 'timemodified' => 123456789,
178 'timesort' => 123456789,
179 'visible' => 1,
180 'subscriptionid' => 1
181 ]
182 );
183 }
184
185 /**
186 * Test invalid callback exception.
7aedfe32
CB
187 */
188 public function test_invalid_bail_callback() {
189 $this->resetAfterTest(true);
190 $this->setAdminUser();
191 $event = $this->create_event();
192 $coursecache = [];
e798fa76 193 $modulecache = [];
7aedfe32
CB
194 $factory = new event_factory(
195 function ($event) {
196 return $event;
197 },
198 function () {
199 return true;
200 },
201 function () {
202 return 'asdf';
203 },
e798fa76
CB
204 $coursecache,
205 $modulecache
2a5cce61 206 );
5ca71c2d 207
20592f5f 208 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
5ca71c2d
CB
209 $factory->create_instance(
210 (object)[
211 'id' => $event->id,
212 'name' => 'test',
213 'description' => 'Test description',
214 'format' => 2,
0085b0ea 215 'categoryid' => 0,
5ca71c2d
CB
216 'courseid' => 1,
217 'groupid' => 1,
218 'userid' => 1,
bd01fd15 219 'repeatid' => 0,
5ca71c2d
CB
220 'modulename' => 'assign',
221 'instance' => 1,
222 'eventtype' => 'due',
bd01fd15 223 'type' => CALENDAR_EVENT_TYPE_ACTION,
5ca71c2d
CB
224 'timestart' => 123456789,
225 'timeduration' => 12,
226 'timemodified' => 123456789,
227 'timesort' => 123456789,
228 'visible' => 1,
229 'subscriptionid' => 1
230 ]
231 );
232 }
233
1d364a89
CB
234 /**
235 * Test the factory's course cache.
236 */
237 public function test_course_cache() {
238 $this->resetAfterTest(true);
239 $this->setAdminUser();
240 $course = self::getDataGenerator()->create_course();
241 $event = $this->create_event(['courseid' => $course->id]);
242 $coursecache = [];
e798fa76 243 $modulecache = [];
1d364a89
CB
244 $factory = new event_factory(
245 function ($event) {
246 return $event;
247 },
248 function () {
249 return true;
250 },
7aedfe32
CB
251 function () {
252 return false;
253 },
e798fa76
CB
254 $coursecache,
255 $modulecache
1d364a89
CB
256 );
257
258 $instance = $factory->create_instance(
259 (object)[
260 'id' => $event->id,
261 'name' => 'test',
262 'description' => 'Test description',
263 'format' => 2,
0085b0ea 264 'categoryid' => 0,
1d364a89
CB
265 'courseid' => $course->id,
266 'groupid' => 1,
267 'userid' => 1,
bd01fd15 268 'repeatid' => 0,
1d364a89
CB
269 'modulename' => 'assign',
270 'instance' => 1,
271 'eventtype' => 'due',
bd01fd15 272 'type' => CALENDAR_EVENT_TYPE_ACTION,
1d364a89
CB
273 'timestart' => 123456789,
274 'timeduration' => 12,
275 'timemodified' => 123456789,
276 'timesort' => 123456789,
277 'visible' => 1,
278 'subscriptionid' => 1
279 ]
280 );
281
282 $instance->get_course()->get('fullname');
283 $this->assertArrayHasKey($course->id, $coursecache);
284 }
285
e798fa76
CB
286 /**
287 * Test the factory's module cache.
288 */
289 public function test_module_cache() {
290 $this->resetAfterTest(true);
291 $this->setAdminUser();
292 $course = self::getDataGenerator()->create_course();
293 $event = $this->create_event(['courseid' => $course->id]);
294 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
295 $assigninstance = $plugingenerator->create_instance(['course' => $course->id]);
296
297 $coursecache = [];
298 $modulecache = [];
299 $factory = new event_factory(
300 function ($event) {
301 return $event;
302 },
303 function () {
304 return true;
305 },
306 function () {
307 return false;
308 },
309 $coursecache,
310 $modulecache
311 );
312
313 $instance = $factory->create_instance(
314 (object)[
315 'id' => $event->id,
316 'name' => 'test',
317 'description' => 'Test description',
318 'format' => 2,
0085b0ea 319 'categoryid' => 0,
47a71017 320 'courseid' => 0,
e798fa76
CB
321 'groupid' => 1,
322 'userid' => 1,
bd01fd15 323 'repeatid' => 0,
e798fa76
CB
324 'modulename' => 'assign',
325 'instance' => $assigninstance->id,
326 'eventtype' => 'due',
bd01fd15 327 'type' => CALENDAR_EVENT_TYPE_ACTION,
e798fa76
CB
328 'timestart' => 123456789,
329 'timeduration' => 12,
330 'timemodified' => 123456789,
331 'timesort' => 123456789,
332 'visible' => 1,
333 'subscriptionid' => 1
334 ]
335 );
336
337 $instance->get_course_module()->get('course');
338 $this->assertArrayHasKey('assign' . '_' . $assigninstance->id, $modulecache);
339 }
340
1d364a89
CB
341 /**
342 * Testcases for the create instance test.
343 *
344 * @return array Array of testcases.
345 */
5ca71c2d
CB
346 public function create_instance_testcases() {
347 return [
2a5cce61 348 'Sample event record with event exposed' => [
5ca71c2d
CB
349 'dbrow' => (object)[
350 'name' => 'Test event',
351 'description' => 'Hello',
352 'format' => 1,
0085b0ea 353 'categoryid' => 0,
5ca71c2d
CB
354 'courseid' => 1,
355 'groupid' => 1,
356 'userid' => 1,
bd01fd15 357 'repeatid' => 0,
5ca71c2d
CB
358 'modulename' => 'Test module',
359 'instance' => 1,
360 'eventtype' => 'Due',
bd01fd15 361 'type' => CALENDAR_EVENT_TYPE_ACTION,
5ca71c2d
CB
362 'timestart' => 123456789,
363 'timeduration' => 123456789,
364 'timemodified' => 123456789,
365 'timesort' => 123456789,
366 'visible' => true,
367 'subscriptionid' => 1
368 ],
2a5cce61 369 'actioncallbackapplier' => function(event_interface $event) {
5ca71c2d
CB
370 $event->testattribute = 'Hello';
371 return $event;
372 },
2a5cce61
CB
373 'visibilitycallbackapplier' => function(event_interface $event) {
374 return true;
375 },
7aedfe32
CB
376 'bailoutcheck' => function() {
377 return false;
378 },
2a5cce61 379 event_interface::class,
5ca71c2d 380 'Hello'
2a5cce61
CB
381 ],
382 'Sample event record with event hidden' => [
383 'dbrow' => (object)[
384 'name' => 'Test event',
385 'description' => 'Hello',
386 'format' => 1,
0085b0ea 387 'categoryid' => 0,
2a5cce61
CB
388 'courseid' => 1,
389 'groupid' => 1,
390 'userid' => 1,
bd01fd15 391 'repeatid' => 0,
2a5cce61
CB
392 'modulename' => 'Test module',
393 'instance' => 1,
394 'eventtype' => 'Due',
bd01fd15 395 'type' => CALENDAR_EVENT_TYPE_ACTION,
2a5cce61
CB
396 'timestart' => 123456789,
397 'timeduration' => 123456789,
398 'timemodified' => 123456789,
399 'timesort' => 123456789,
400 'visible' => true,
401 'subscriptionid' => 1
402 ],
403 'actioncallbackapplier' => function(event_interface $event) {
404 $event->testattribute = 'Hello';
405 return $event;
406 },
407 'visibilitycallbackapplier' => function(event_interface $event) {
408 return false;
409 },
7aedfe32
CB
410 'bailoutcheck' => function() {
411 return false;
412 },
413 null,
414 null
415 ],
416 'Sample event record with early bail' => [
417 'dbrow' => (object)[
418 'name' => 'Test event',
419 'description' => 'Hello',
420 'format' => 1,
0085b0ea 421 'categoryid' => 0,
7aedfe32
CB
422 'courseid' => 1,
423 'groupid' => 1,
424 'userid' => 1,
bd01fd15 425 'repeatid' => 0,
7aedfe32
CB
426 'modulename' => 'Test module',
427 'instance' => 1,
428 'eventtype' => 'Due',
bd01fd15 429 'type' => CALENDAR_EVENT_TYPE_ACTION,
7aedfe32
CB
430 'timestart' => 123456789,
431 'timeduration' => 123456789,
432 'timemodified' => 123456789,
433 'timesort' => 123456789,
434 'visible' => true,
435 'subscriptionid' => 1
436 ],
437 'actioncallbackapplier' => function(event_interface $event) {
438 $event->testattribute = 'Hello';
439 return $event;
440 },
441 'visibilitycallbackapplier' => function(event_interface $event) {
442 return true;
443 },
444 'bailoutcheck' => function() {
445 return true;
446 },
2a5cce61
CB
447 null,
448 null
5ca71c2d
CB
449 ]
450 ];
451 }
452
453 /**
454 * Helper function to create calendar events using the old code.
455 *
456 * @param array $properties A list of calendar event properties to set
e1cd93ce 457 * @return calendar_event
5ca71c2d
CB
458 */
459 protected function create_event($properties = []) {
460 $record = new \stdClass();
461 $record->name = 'event name';
462 $record->eventtype = 'global';
463 $record->timestart = time();
464 $record->timeduration = 0;
465 $record->timesort = 0;
466 $record->type = 1;
467 $record->courseid = 0;
44ae0838 468 $record->categoryid = 0;
5ca71c2d
CB
469
470 foreach ($properties as $name => $value) {
471 $record->$name = $value;
472 }
473
e1cd93ce 474 $event = new calendar_event($record);
5ca71c2d
CB
475 return $event->create($record, false);
476 }
477}