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