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/>.
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
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->dirroot
. '/calendar/lib.php');
30 use core_calendar\local\event\factories\event_factory
;
31 use core_calendar\local\event\entities\event_interface
;
34 * Event factory testcase.
36 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_calendar_event_factory_testcase
extends advanced_testcase
{
41 * Test event class getters.
43 * @dataProvider create_instance_testcases()
44 * @param \stdClass $dbrow Row from the event table.
45 * @param callable $actioncallbackapplier Action callback applier.
46 * @param callable $visibilitycallbackapplier Visibility callback applier.
47 * @param callable $bailoutcheck Early bail out check function.
48 * @param string $expectedclass Class the factory is expected to produce.
49 * @param mixed $expectedattributevalue Expected value of the modified attribute.
51 public function test_create_instance(
53 callable
$actioncallbackapplier,
54 callable
$visibilitycallbackapplier,
55 callable
$bailoutcheck,
57 $expectedattributevalue
59 $this->resetAfterTest(true);
60 $this->setAdminUser();
61 $event = $this->create_event();
64 $factory = new event_factory(
65 $actioncallbackapplier,
66 $visibilitycallbackapplier,
71 $dbrow->id
= $event->id
;
72 $instance = $factory->create_instance($dbrow);
75 $this->assertInstanceOf($expectedclass, $instance);
78 if (is_null($expectedclass)) {
79 $this->assertNull($instance);
82 if ($expectedattributevalue) {
83 $this->assertEquals($instance->testattribute
, $expectedattributevalue);
88 * Test invalid callback exception.
90 public function test_invalid_action_callback() {
91 $this->resetAfterTest(true);
92 $this->setAdminUser();
93 $event = $this->create_event();
96 $factory = new event_factory(
110 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
111 $factory->create_instance(
115 'description' => 'Test description',
122 'modulename' => 'assign',
124 'eventtype' => 'due',
125 'type' => CALENDAR_EVENT_TYPE_ACTION
,
126 'timestart' => 123456789,
127 'timeduration' => 12,
128 'timemodified' => 123456789,
129 'timesort' => 123456789,
131 'subscriptionid' => 1
137 * Test invalid callback exception.
139 public function test_invalid_visibility_callback() {
140 $this->resetAfterTest(true);
141 $this->setAdminUser();
142 $event = $this->create_event();
145 $factory = new event_factory(
159 $this->expectException('\core_calendar\local\event\exceptions\invalid_callback_exception');
160 $factory->create_instance(
164 'description' => 'Test description',
171 'modulename' => 'assign',
173 'eventtype' => 'due',
174 'type' => CALENDAR_EVENT_TYPE_ACTION
,
175 'timestart' => 123456789,
176 'timeduration' => 12,
177 'timemodified' => 123456789,
178 'timesort' => 123456789,
180 'subscriptionid' => 1
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
235 * Test the factory's course cache.
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
]);
244 $factory = new event_factory(
258 $instance = $factory->create_instance(
262 'description' => 'Test description',
265 'courseid' => $course->id
,
269 'modulename' => 'assign',
271 'eventtype' => 'due',
272 'type' => CALENDAR_EVENT_TYPE_ACTION
,
273 'timestart' => 123456789,
274 'timeduration' => 12,
275 'timemodified' => 123456789,
276 'timesort' => 123456789,
278 'subscriptionid' => 1
282 $instance->get_course()->get('fullname');
283 $this->assertArrayHasKey($course->id
, $coursecache);
287 * Test the factory's module cache.
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
]);
299 $factory = new event_factory(
313 $instance = $factory->create_instance(
317 'description' => 'Test description',
324 'modulename' => 'assign',
325 'instance' => $assigninstance->id
,
326 'eventtype' => 'due',
327 'type' => CALENDAR_EVENT_TYPE_ACTION
,
328 'timestart' => 123456789,
329 'timeduration' => 12,
330 'timemodified' => 123456789,
331 'timesort' => 123456789,
333 'subscriptionid' => 1
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
369 'actioncallbackapplier' => function(event_interface
$event) {
370 $event->testattribute
= 'Hello';
373 'visibilitycallbackapplier' => function(event_interface
$event) {
376 'bailoutcheck' => function() {
379 event_interface
::class,
382 'Sample event record with event hidden' => [
384 'name' => 'Test event',
385 'description' => 'Hello',
392 'modulename' => 'Test module',
394 'eventtype' => 'Due',
395 'type' => CALENDAR_EVENT_TYPE_ACTION
,
396 'timestart' => 123456789,
397 'timeduration' => 123456789,
398 'timemodified' => 123456789,
399 'timesort' => 123456789,
401 'subscriptionid' => 1
403 'actioncallbackapplier' => function(event_interface
$event) {
404 $event->testattribute
= 'Hello';
407 'visibilitycallbackapplier' => function(event_interface
$event) {
410 'bailoutcheck' => function() {
416 'Sample event record with early bail' => [
418 'name' => 'Test event',
419 'description' => 'Hello',
426 'modulename' => 'Test module',
428 'eventtype' => 'Due',
429 'type' => CALENDAR_EVENT_TYPE_ACTION
,
430 'timestart' => 123456789,
431 'timeduration' => 123456789,
432 'timemodified' => 123456789,
433 'timesort' => 123456789,
435 'subscriptionid' => 1
437 'actioncallbackapplier' => function(event_interface
$event) {
438 $event->testattribute
= 'Hello';
441 'visibilitycallbackapplier' => function(event_interface
$event) {
444 'bailoutcheck' => function() {
454 * Helper function to create calendar events using the old code.
456 * @param array $properties A list of calendar event properties to set
457 * @return calendar_event
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;
467 $record->courseid
= 0;
468 $record->categoryid
= 0;
470 foreach ($properties as $name => $value) {
471 $record->$name = $value;
474 $event = new calendar_event($record);
475 return $event->create($record, false);