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) {
79 $instance->get_description()->get_value(),
80 $expectedattributevalue
86 * Test invalid callback exception.
88 public function test_invalid_action_callback() {
89 $this->resetAfterTest(true);
90 $this->setAdminUser();
91 $event = $this->create_event();
94 $factory = new event_factory(
108 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
109 $factory->create_instance(
113 'description' => 'Test description',
120 'modulename' => 'assign',
122 'eventtype' => 'due',
123 'type' => CALENDAR_EVENT_TYPE_ACTION
,
124 'timestart' => 123456789,
125 'timeduration' => 12,
126 'timemodified' => 123456789,
127 'timesort' => 123456789,
129 'subscriptionid' => 1,
130 'location' => 'Test location',
136 * Test invalid callback exception.
138 public function test_invalid_visibility_callback() {
139 $this->resetAfterTest(true);
140 $this->setAdminUser();
141 $event = $this->create_event();
144 $factory = new event_factory(
158 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
159 $factory->create_instance(
163 'description' => 'Test description',
170 'modulename' => 'assign',
172 'eventtype' => 'due',
173 'type' => CALENDAR_EVENT_TYPE_ACTION
,
174 'timestart' => 123456789,
175 'timeduration' => 12,
176 'timemodified' => 123456789,
177 'timesort' => 123456789,
179 'subscriptionid' => 1,
180 'location' => 'Test location',
186 * Test invalid callback exception.
188 public function test_invalid_bail_callback() {
189 $this->resetAfterTest(true);
190 $this->setAdminUser();
191 $event = $this->create_event();
194 $factory = new event_factory(
208 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
209 $factory->create_instance(
213 'description' => 'Test description',
220 'modulename' => 'assign',
222 'eventtype' => 'due',
223 'type' => CALENDAR_EVENT_TYPE_ACTION
,
224 'timestart' => 123456789,
225 'timeduration' => 12,
226 'timemodified' => 123456789,
227 'timesort' => 123456789,
229 'subscriptionid' => 1,
230 'location' => 'Test location',
236 * Test the factory's course cache.
238 public function test_course_cache() {
239 $this->resetAfterTest(true);
240 $this->setAdminUser();
241 $course = self
::getDataGenerator()->create_course();
242 $event = $this->create_event(['courseid' => $course->id
]);
245 $factory = new event_factory(
259 $instance = $factory->create_instance(
263 'description' => 'Test description',
266 'courseid' => $course->id
,
270 'modulename' => 'assign',
272 'eventtype' => 'due',
273 'type' => CALENDAR_EVENT_TYPE_ACTION
,
274 'timestart' => 123456789,
275 'timeduration' => 12,
276 'timemodified' => 123456789,
277 'timesort' => 123456789,
279 'subscriptionid' => 1,
280 'location' => 'Test location',
284 $instance->get_course()->get('fullname');
285 $this->assertArrayHasKey($course->id
, $coursecache);
289 * Test the factory's module cache.
291 public function test_module_cache() {
292 $this->resetAfterTest(true);
293 $this->setAdminUser();
294 $course = self
::getDataGenerator()->create_course();
295 $event = $this->create_event(['courseid' => $course->id
]);
296 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
297 $assigninstance = $plugingenerator->create_instance(['course' => $course->id
]);
301 $factory = new event_factory(
315 $instance = $factory->create_instance(
319 'description' => 'Test description',
326 'modulename' => 'assign',
327 'instance' => $assigninstance->id
,
328 'eventtype' => 'due',
329 'type' => CALENDAR_EVENT_TYPE_ACTION
,
330 'timestart' => 123456789,
331 'timeduration' => 12,
332 'timemodified' => 123456789,
333 'timesort' => 123456789,
335 'subscriptionid' => 1,
336 'location' => 'Test location',
340 $instance->get_course_module()->get('course');
341 $this->assertArrayHasKey('assign' . '_' . $assigninstance->id
, $modulecache);
345 * Testcases for the create instance test.
347 * @return array Array of testcases.
349 public function create_instance_testcases() {
351 'Sample event record with event exposed' => [
353 'name' => 'Test event',
354 'description' => 'Hello',
361 'modulename' => 'Test module',
363 'eventtype' => 'Due',
364 'type' => CALENDAR_EVENT_TYPE_ACTION
,
365 'timestart' => 123456789,
366 'timeduration' => 123456789,
367 'timemodified' => 123456789,
368 'timesort' => 123456789,
370 'subscriptionid' => 1,
371 'location' => 'Test location',
373 'actioncallbackapplier' => function(event_interface
$event) {
376 'visibilitycallbackapplier' => function(event_interface
$event) {
379 'bailoutcheck' => function() {
382 event_interface
::class,
385 'Sample event record with event hidden' => [
387 'name' => 'Test event',
388 'description' => 'Hello',
395 'modulename' => 'Test module',
397 'eventtype' => 'Due',
398 'type' => CALENDAR_EVENT_TYPE_ACTION
,
399 'timestart' => 123456789,
400 'timeduration' => 123456789,
401 'timemodified' => 123456789,
402 'timesort' => 123456789,
404 'subscriptionid' => 1,
405 'location' => 'Test location',
407 'actioncallbackapplier' => function(event_interface
$event) {
410 'visibilitycallbackapplier' => function(event_interface
$event) {
413 'bailoutcheck' => function() {
419 'Sample event record with early bail' => [
421 'name' => 'Test event',
422 'description' => 'Hello',
429 'modulename' => 'Test module',
431 'eventtype' => 'Due',
432 'type' => CALENDAR_EVENT_TYPE_ACTION
,
433 'timestart' => 123456789,
434 'timeduration' => 123456789,
435 'timemodified' => 123456789,
436 'timesort' => 123456789,
438 'subscriptionid' => 1,
439 'location' => 'Test location',
441 'actioncallbackapplier' => function(event_interface
$event) {
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);