2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core_calendar
;
19 use core_calendar\local\event\factories\event_factory
;
20 use core_calendar\local\event\entities\event_interface
;
22 defined('MOODLE_INTERNAL') ||
die();
25 require_once($CFG->dirroot
. '/calendar/lib.php');
30 * @package core_calendar
31 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class event_factory_test
extends \advanced_testcase
{
36 * Test event class getters.
38 * @dataProvider create_instance_testcases()
39 * @param \stdClass $dbrow Row from the event table.
40 * @param callable $actioncallbackapplier Action callback applier.
41 * @param callable $visibilitycallbackapplier Visibility callback applier.
42 * @param callable $bailoutcheck Early bail out check function.
43 * @param string $expectedclass Class the factory is expected to produce.
44 * @param mixed $expectedattributevalue Expected value of the modified attribute.
46 public function test_create_instance(
48 callable
$actioncallbackapplier,
49 callable
$visibilitycallbackapplier,
50 callable
$bailoutcheck,
52 $expectedattributevalue
54 $this->resetAfterTest(true);
55 $this->setAdminUser();
56 $event = $this->create_event();
59 $factory = new event_factory(
60 $actioncallbackapplier,
61 $visibilitycallbackapplier,
66 $dbrow->id
= $event->id
;
67 $instance = $factory->create_instance($dbrow);
70 $this->assertInstanceOf($expectedclass, $instance);
73 if (is_null($expectedclass)) {
74 $this->assertNull($instance);
77 if ($expectedattributevalue) {
78 $this->assertEquals($instance->testattribute
, $expectedattributevalue);
83 * Test invalid callback exception.
85 public function test_invalid_action_callback() {
86 $this->resetAfterTest(true);
87 $this->setAdminUser();
88 $event = $this->create_event();
91 $factory = new event_factory(
105 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
106 $factory->create_instance(
110 'description' => 'Test description',
117 'modulename' => 'assign',
119 'eventtype' => 'due',
120 'type' => CALENDAR_EVENT_TYPE_ACTION
,
121 'timestart' => 123456789,
122 'timeduration' => 12,
123 'timemodified' => 123456789,
124 'timesort' => 123456789,
126 'subscriptionid' => 1,
127 'location' => 'Test location',
133 * Test invalid callback exception.
135 public function test_invalid_visibility_callback() {
136 $this->resetAfterTest(true);
137 $this->setAdminUser();
138 $event = $this->create_event();
141 $factory = new event_factory(
155 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
156 $factory->create_instance(
160 'description' => 'Test description',
167 'modulename' => 'assign',
169 'eventtype' => 'due',
170 'type' => CALENDAR_EVENT_TYPE_ACTION
,
171 'timestart' => 123456789,
172 'timeduration' => 12,
173 'timemodified' => 123456789,
174 'timesort' => 123456789,
176 'subscriptionid' => 1,
177 'location' => 'Test location',
183 * Test invalid callback exception.
185 public function test_invalid_bail_callback() {
186 $this->resetAfterTest(true);
187 $this->setAdminUser();
188 $event = $this->create_event();
191 $factory = new event_factory(
205 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
206 $factory->create_instance(
210 'description' => 'Test description',
217 'modulename' => 'assign',
219 'eventtype' => 'due',
220 'type' => CALENDAR_EVENT_TYPE_ACTION
,
221 'timestart' => 123456789,
222 'timeduration' => 12,
223 'timemodified' => 123456789,
224 'timesort' => 123456789,
226 'subscriptionid' => 1,
227 'location' => 'Test location',
233 * Test the factory's course cache.
235 public function test_course_cache() {
236 $this->resetAfterTest(true);
237 $this->setAdminUser();
238 $course = self
::getDataGenerator()->create_course();
239 $event = $this->create_event(['courseid' => $course->id
]);
242 $factory = new event_factory(
256 $instance = $factory->create_instance(
260 'description' => 'Test description',
263 'courseid' => $course->id
,
267 'modulename' => 'assign',
269 'eventtype' => 'due',
270 'type' => CALENDAR_EVENT_TYPE_ACTION
,
271 'timestart' => 123456789,
272 'timeduration' => 12,
273 'timemodified' => 123456789,
274 'timesort' => 123456789,
276 'subscriptionid' => 1,
277 'location' => 'Test location',
281 $instance->get_course()->get('fullname');
282 $this->assertArrayHasKey($course->id
, $coursecache);
286 * Test the factory's module cache.
288 public function test_module_cache() {
289 $this->resetAfterTest(true);
290 $this->setAdminUser();
291 $course = self
::getDataGenerator()->create_course();
292 $event = $this->create_event(['courseid' => $course->id
]);
293 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
294 $assigninstance = $plugingenerator->create_instance(['course' => $course->id
]);
298 $factory = new event_factory(
312 $instance = $factory->create_instance(
316 'description' => 'Test description',
323 'modulename' => 'assign',
324 'instance' => $assigninstance->id
,
325 'eventtype' => 'due',
326 'type' => CALENDAR_EVENT_TYPE_ACTION
,
327 'timestart' => 123456789,
328 'timeduration' => 12,
329 'timemodified' => 123456789,
330 'timesort' => 123456789,
332 'subscriptionid' => 1,
333 'location' => 'Test location',
337 $instance->get_course_module()->get('course');
338 $this->assertArrayHasKey('assign' . '_' . $assigninstance->id
, $modulecache);
342 * Testcases for the create instance test.
344 * @return array Array of testcases.
346 public function create_instance_testcases() {
348 'Sample event record with event exposed' => [
350 'name' => 'Test event',
351 'description' => 'Hello',
358 'modulename' => 'Test module',
360 'eventtype' => 'Due',
361 'type' => CALENDAR_EVENT_TYPE_ACTION
,
362 'timestart' => 123456789,
363 'timeduration' => 123456789,
364 'timemodified' => 123456789,
365 'timesort' => 123456789,
367 'subscriptionid' => 1,
368 'location' => 'Test location',
370 'actioncallbackapplier' => function(event_interface
$event) {
371 $event->testattribute
= 'Hello';
374 'visibilitycallbackapplier' => function(event_interface
$event) {
377 'bailoutcheck' => function() {
380 event_interface
::class,
383 'Sample event record with event hidden' => [
385 'name' => 'Test event',
386 'description' => 'Hello',
393 'modulename' => 'Test module',
395 'eventtype' => 'Due',
396 'type' => CALENDAR_EVENT_TYPE_ACTION
,
397 'timestart' => 123456789,
398 'timeduration' => 123456789,
399 'timemodified' => 123456789,
400 'timesort' => 123456789,
402 'subscriptionid' => 1,
403 'location' => 'Test location',
405 'actioncallbackapplier' => function(event_interface
$event) {
406 $event->testattribute
= 'Hello';
409 'visibilitycallbackapplier' => function(event_interface
$event) {
412 'bailoutcheck' => function() {
418 'Sample event record with early bail' => [
420 'name' => 'Test event',
421 'description' => 'Hello',
428 'modulename' => 'Test module',
430 'eventtype' => 'Due',
431 'type' => CALENDAR_EVENT_TYPE_ACTION
,
432 'timestart' => 123456789,
433 'timeduration' => 123456789,
434 'timemodified' => 123456789,
435 'timesort' => 123456789,
437 'subscriptionid' => 1,
438 'location' => 'Test location',
440 'actioncallbackapplier' => function(event_interface
$event) {
441 $event->testattribute
= 'Hello';
444 'visibilitycallbackapplier' => function(event_interface
$event) {
447 'bailoutcheck' => function() {
457 * Helper function to create calendar events using the old code.
459 * @param array $properties A list of calendar event properties to set
460 * @return calendar_event
462 protected function create_event($properties = []) {
463 $record = new \
stdClass();
464 $record->name
= 'event name';
465 $record->eventtype
= 'site';
466 $record->timestart
= time();
467 $record->timeduration
= 0;
468 $record->timesort
= 0;
470 $record->courseid
= 0;
471 $record->categoryid
= 0;
473 foreach ($properties as $name => $value) {
474 $record->$name = $value;
477 $event = new \
calendar_event($record);
478 return $event->create($record, false);