8439caaea80f415796f3aeb5b83f7e4756910a7b
[moodle.git] / calendar / tests / container_test.php
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/>.
17 /**
18  * Event container tests.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/calendar/lib.php');
31 use core_calendar\local\event\entities\action_event;
32 use core_calendar\local\event\entities\event;
33 use core_calendar\local\event\entities\event_interface;
34 use core_calendar\local\event\factories\event_factory;
35 use core_calendar\local\event\factories\event_factory_interface;
36 use core_calendar\local\event\mappers\event_mapper;
37 use core_calendar\local\event\mappers\event_mapper_interface;
39 /**
40  * Core container testcase.
41  *
42  * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
43  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class core_calendar_container_testcase extends advanced_testcase {
47     /**
48      * Test setup.
49      */
50     public function setUp() {
51         $this->resetAfterTest();
52         $this->setAdminUser();
53     }
55     /**
56      * Test getting the event factory.
57      */
58     public function test_get_event_factory() {
59         $factory = \core_calendar\local\event\container::get_event_factory();
61         // Test that the container is returning the right type.
62         $this->assertInstanceOf(event_factory_interface::class, $factory);
63         // Test that the container is returning the right implementation.
64         $this->assertInstanceOf(event_factory::class, $factory);
66         // Test that getting the factory a second time returns the same instance.
67         $factory2 = \core_calendar\local\event\container::get_event_factory();
68         $this->assertTrue($factory === $factory2);
69     }
71     /**
72      * Test that the event factory correctly creates instances of events.
73      *
74      * @dataProvider get_event_factory_testcases()
75      * @param \stdClass $dbrow Row from the "database".
76      */
77     public function test_event_factory_create_instance($dbrow) {
78         $legacyevent = $this->create_event($dbrow);
79         $factory = \core_calendar\local\event\container::get_event_factory();
80         $course = $this->getDataGenerator()->create_course();
81         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
82         $moduleinstance = $generator->create_instance(['course' => $course->id]);
84         // Set some of the fake dbrow properties to match real data in the DB
85         // this is necessary as the factory hides things that modinfo doesn't
86         // know about.
87         $dbrow->id = $legacyevent->id;
88         $dbrow->courseid = $course->id;
89         $dbrow->instance = $moduleinstance->id;
90         $dbrow->modulename = 'assign';
91         $event = $factory->create_instance($dbrow);
93         // Test that the factory is returning the right type.
94         $this->assertInstanceOf(event_interface::class, $event);
95         // Test that the factory is returning the right implementation.
96         $this->assertTrue($event instanceof event || $event instanceof action_event);
98         // Test that the event created has the correct properties.
99         $this->assertEquals($legacyevent->id, $event->get_id());
100         $this->assertEquals($dbrow->description, $event->get_description()->get_value());
101         $this->assertEquals($dbrow->format, $event->get_description()->get_format());
102         $this->assertEquals($dbrow->courseid, $event->get_course()->get('id'));
104         if ($dbrow->groupid == 0) {
105             $this->assertNull($event->get_group());
106         } else {
107             $this->assertEquals($dbrow->groupid, $event->get_group()->get('id'));
108         }
110         $this->assertEquals($dbrow->userid, $event->get_user()->get('id'));
111         $this->assertEquals(null, $event->get_repeats());
112         $this->assertEquals($dbrow->modulename, $event->get_course_module()->get('modname'));
113         $this->assertEquals($dbrow->instance, $event->get_course_module()->get('instance'));
114         $this->assertEquals($dbrow->timestart, $event->get_times()->get_start_time()->getTimestamp());
115         $this->assertEquals($dbrow->timemodified, $event->get_times()->get_modified_time()->getTimestamp());
116         $this->assertEquals($dbrow->timesort, $event->get_times()->get_sort_time()->getTimestamp());
118         if ($dbrow->visible == 1) {
119             $this->assertTrue($event->is_visible());
120         } else {
121             $this->assertFalse($event->is_visible());
122         }
124         if (!$dbrow->subscriptionid) {
125             $this->assertNull($event->get_subscription());
126         } else {
127             $this->assertEquals($event->get_subscription()->get('id'));
128         }
129     }
131     /**
132      * Test that the event factory deals with invisible modules properly as admin.
133      *
134      * @dataProvider get_event_factory_testcases()
135      * @param \stdClass $dbrow Row from the "database".
136      */
137     public function test_event_factory_when_module_visibility_is_toggled_as_admin($dbrow) {
138         $legacyevent = $this->create_event($dbrow);
139         $factory = \core_calendar\local\event\container::get_event_factory();
140         $course = $this->getDataGenerator()->create_course();
141         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
142         $moduleinstance = $generator->create_instance(['course' => $course->id]);
144         $dbrow->id = $legacyevent->id;
145         $dbrow->courseid = $course->id;
146         $dbrow->instance = $moduleinstance->id;
147         $dbrow->modulename = 'assign';
149         set_coursemodule_visible($moduleinstance->cmid, 0);
151         $event = $factory->create_instance($dbrow);
153         // Test that the factory is returning an event as the admin can see hidden course modules.
154         $this->assertInstanceOf(event_interface::class, $event);
155     }
157     /**
158      * Test that the event factory deals with invisible modules properly as a guest.
159      *
160      * @dataProvider get_event_factory_testcases()
161      * @param \stdClass $dbrow Row from the "database".
162      */
163     public function test_event_factory_when_module_visibility_is_toggled_as_guest($dbrow) {
164         $legacyevent = $this->create_event($dbrow);
165         $factory = \core_calendar\local\event\container::get_event_factory();
166         $course = $this->getDataGenerator()->create_course();
167         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
168         $moduleinstance = $generator->create_instance(['course' => $course->id]);
170         $dbrow->id = $legacyevent->id;
171         $dbrow->courseid = $course->id;
172         $dbrow->instance = $moduleinstance->id;
173         $dbrow->modulename = 'assign';
175         set_coursemodule_visible($moduleinstance->cmid, 0);
177         // Set to a user who can not view hidden course modules.
178         $this->setGuestUser();
180         $event = $factory->create_instance($dbrow);
182         // Module is invisible to guest users so this should return null.
183         $this->assertNull($event);
184     }
186     /**
187      * Test that the event factory deals with invisible courses as an admin.
188      *
189      * @dataProvider get_event_factory_testcases()
190      * @param \stdClass $dbrow Row from the "database".
191      */
192     public function test_event_factory_when_course_visibility_is_toggled_as_admin($dbrow) {
193         $legacyevent = $this->create_event($dbrow);
194         $factory = \core_calendar\local\event\container::get_event_factory();
196         // Create a hidden course with an assignment.
197         $course = $this->getDataGenerator()->create_course(['visible' => 0]);
198         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
199         $moduleinstance = $generator->create_instance(['course' => $course->id]);
201         $dbrow->id = $legacyevent->id;
202         $dbrow->courseid = $course->id;
203         $dbrow->instance = $moduleinstance->id;
204         $dbrow->modulename = 'assign';
205         $event = $factory->create_instance($dbrow);
207         // Module is still visible to admins even if the course is invisible.
208         $this->assertInstanceOf(event_interface::class, $event);
209     }
211     /**
212      * Test that the event factory deals with invisible courses as a student.
213      *
214      * @dataProvider get_event_factory_testcases()
215      * @param \stdClass $dbrow Row from the "database".
216      */
217     public function test_event_factory_when_course_visibility_is_toggled_as_student($dbrow) {
218         $legacyevent = $this->create_event($dbrow);
219         $factory = \core_calendar\local\event\container::get_event_factory();
221         // Create a hidden course with an assignment.
222         $course = $this->getDataGenerator()->create_course(['visible' => 0]);
223         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
224         $moduleinstance = $generator->create_instance(['course' => $course->id]);
226         // Enrol a student into this course.
227         $student = $this->getDataGenerator()->create_user();
228         $this->getDataGenerator()->enrol_user($student->id, $course->id);
230         // Set the user to the student.
231         $this->setUser($student);
233         $dbrow->id = $legacyevent->id;
234         $dbrow->courseid = $course->id;
235         $dbrow->instance = $moduleinstance->id;
236         $dbrow->modulename = 'assign';
237         $event = $factory->create_instance($dbrow);
239         // Module is invisible to students if the course is invisible.
240         $this->assertNull($event);
241     }
243     /**
244      * Test that the event factory deals with invisible categorys as an admin.
245      */
246     public function test_event_factory_when_category_visibility_is_toggled_as_admin() {
247         // Create a hidden category.
248         $category = $this->getDataGenerator()->create_category(['visible' => 0]);
250         $eventdata = [
251                 'categoryid' => $category->id,
252                 'eventtype' => 'category',
253             ];
254         $legacyevent = $this->create_event($eventdata);
256         $dbrow = $this->get_dbrow_from_skeleton((object) $eventdata);
257         $dbrow->id = $legacyevent->id;
259         $factory = \core_calendar\local\event\container::get_event_factory();
260         $event = $factory->create_instance($dbrow);
262         // Module is still visible to admins even if the category is invisible.
263         $this->assertInstanceOf(event_interface::class, $event);
264     }
266     /**
267      * Test that the event factory deals with invisible categorys as an user.
268      */
269     public function test_event_factory_when_category_visibility_is_toggled_as_user() {
270         // Create a hidden category.
271         $category = $this->getDataGenerator()->create_category(['visible' => 0]);
273         $eventdata = [
274                 'categoryid' => $category->id,
275                 'eventtype' => 'category',
276             ];
277         $legacyevent = $this->create_event($eventdata);
279         $dbrow = $this->get_dbrow_from_skeleton((object) $eventdata);
280         $dbrow->id = $legacyevent->id;
282         // Use a standard user.
283         $user = $this->getDataGenerator()->create_user();
285         // Set the user to the student.
286         $this->setUser($user);
288         $factory = \core_calendar\local\event\container::get_event_factory();
289         $event = $factory->create_instance($dbrow);
291         // Module is invisible to non-privileged users.
292         $this->assertNull($event);
293     }
295     /**
296      * Test that the event factory deals with invisible categorys as an guest.
297      */
298     public function test_event_factory_when_category_visibility_is_toggled_as_guest() {
299         // Create a hidden category.
300         $category = $this->getDataGenerator()->create_category(['visible' => 0]);
302         $eventdata = [
303                 'categoryid' => $category->id,
304                 'eventtype' => 'category',
305             ];
306         $legacyevent = $this->create_event($eventdata);
308         $dbrow = $this->get_dbrow_from_skeleton((object) $eventdata);
309         $dbrow->id = $legacyevent->id;
311         // Set the user to the student.
312         $this->setGuestUser();
314         $factory = \core_calendar\local\event\container::get_event_factory();
315         $event = $factory->create_instance($dbrow);
317         // Module is invisible to guests.
318         $this->assertNull($event);
319     }
321     /**
322      * Test that the event factory deals with completion related events properly.
323      */
324     public function test_event_factory_with_completion_related_event() {
325         global $CFG;
327         $CFG->enablecompletion = true;
329         // Create the course we will be using.
330         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
332         // Add the assignment.
333         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
334         $assign = $generator->create_instance(array('course' => $course->id), array('completion' => 1));
336         // Create a completion event.
337         $event = new \stdClass();
338         $event->name = 'An event';
339         $event->description = 'Event description';
340         $event->format = FORMAT_HTML;
341         $event->eventtype = \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED;
342         $event->userid = 1;
343         $event->modulename = 'assign';
344         $event->instance = $assign->id;
345         $event->categoryid = 0;
346         $event->courseid = $course->id;
347         $event->groupid = 0;
348         $event->timestart = time();
349         $event->timesort = time();
350         $event->timemodified = time();
351         $event->timeduration = 0;
352         $event->subscriptionid = null;
353         $event->repeatid = 0;
354         $legacyevent = $this->create_event($event);
356         // Update the id of the event that was created.
357         $event->id = $legacyevent->id;
359         // Create the factory we are going to be testing the behaviour of.
360         $factory = \core_calendar\local\event\container::get_event_factory();
362         // Check that we get the correct instance.
363         $this->assertInstanceOf(event_interface::class, $factory->create_instance($event));
365         // Now, disable completion.
366         $CFG->enablecompletion = false;
368         // The result should now be null since we have disabled completion.
369         $this->assertNull($factory->create_instance($event));
370     }
372     /**
373      * Test that the event factory only returns an event if the logged in user
374      * is enrolled in the course.
375      */
376     public function test_event_factory_unenrolled_user() {
377         $user = $this->getDataGenerator()->create_user();
378         // Create the course we will be using.
379         $course = $this->getDataGenerator()->create_course();
381         // Add the assignment.
382         $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
383         $lesson = $generator->create_instance(array('course' => $course->id));
385         // Create a user override event for the lesson.
386         $event = new \stdClass();
387         $event->name = 'An event';
388         $event->description = 'Event description';
389         $event->format = FORMAT_HTML;
390         $event->eventtype = 'close';
391         $event->userid = $user->id;
392         $event->modulename = 'lesson';
393         $event->instance = $lesson->id;
394         $event->categoryid = 0;
395         $event->courseid = $course->id;
396         $event->groupid = 0;
397         $event->timestart = time();
398         $event->timesort = time();
399         $event->timemodified = time();
400         $event->timeduration = 0;
401         $event->subscriptionid = null;
402         $event->repeatid = 0;
403         $legacyevent = $this->create_event($event);
405         // Update the id of the event that was created.
406         $event->id = $legacyevent->id;
408         // Set the logged in user to the one we created.
409         $this->setUser($user);
411         // Create the factory we are going to be testing the behaviour of.
412         $factory = \core_calendar\local\event\container::get_event_factory();
414         // The result should be null since the user is not enrolled in the
415         // course the event is for.
416         $this->assertNull($factory->create_instance($event));
418         // Now enrol the user in the course.
419         $this->getDataGenerator()->enrol_user($user->id, $course->id);
421         // Check that we get the correct instance.
422         $this->assertInstanceOf(event_interface::class, $factory->create_instance($event));
423     }
425     /**
426      * Test that when course module is deleted all events are also deleted.
427      */
428     public function test_delete_module_delete_events() {
429         global $DB;
430         $user = $this->getDataGenerator()->create_user();
431         // Create the course we will be using.
432         $course = $this->getDataGenerator()->create_course();
433         $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
435         foreach (core_component::get_plugin_list('mod') as $modname => $unused) {
436             try {
437                 $generator = $this->getDataGenerator()->get_plugin_generator('mod_'.$modname);
438             } catch (coding_exception $e) {
439                 // Module generator is not implemented.
440                 continue;
441             }
442             $module = $generator->create_instance(['course' => $course->id]);
444             // Create bunch of events of different type (user override, group override, module event).
445             $this->create_event(['userid' => $user->id, 'modulename' => $modname, 'instance' => $module->id]);
446             $this->create_event(['groupid' => $group->id, 'modulename' => $modname, 'instance' => $module->id]);
447             $this->create_event(['modulename' => $modname, 'instance' => $module->id]);
448             $this->create_event(['modulename' => $modname, 'instance' => $module->id, 'courseid' => $course->id]);
450             // Delete module and make sure all events are deleted.
451             course_delete_module($module->cmid);
452             $this->assertEmpty($DB->get_record('event', ['modulename' => $modname, 'instance' => $module->id]));
453         }
454     }
456     /**
457      * Test getting the event mapper.
458      */
459     public function test_get_event_mapper() {
460         $mapper = \core_calendar\local\event\container::get_event_mapper();
462         $this->assertInstanceOf(event_mapper_interface::class, $mapper);
463         $this->assertInstanceOf(event_mapper::class, $mapper);
465         $mapper2 = \core_calendar\local\event\container::get_event_mapper();
467         $this->assertTrue($mapper === $mapper2);
468     }
470     /**
471      * Test cases for the get event factory test.
472      */
473     public function get_event_factory_testcases() {
474         return [
475             'Data set 1' => [
476                 'dbrow' => (object)[
477                     'name' => 'Test event',
478                     'description' => 'Hello',
479                     'format' => 1,
480                     'categoryid' => 0,
481                     'courseid' => 1,
482                     'groupid' => 0,
483                     'userid' => 1,
484                     'repeatid' => 0,
485                     'modulename' => 'assign',
486                     'instance' => 2,
487                     'eventtype' => 'due',
488                     'timestart' => 1486396800,
489                     'timeduration' => 0,
490                     'timesort' => 1486396800,
491                     'visible' => 1,
492                     'timemodified' => 1485793098,
493                     'subscriptionid' => null
494                 ]
495             ],
497             'Data set 2' => [
498                 'dbrow' => (object)[
499                     'name' => 'Test event',
500                     'description' => 'Hello',
501                     'format' => 1,
502                     'categoryid' => 0,
503                     'courseid' => 1,
504                     'groupid' => 1,
505                     'userid' => 1,
506                     'repeatid' => 0,
507                     'modulename' => 'assign',
508                     'instance' => 2,
509                     'eventtype' => 'due',
510                     'timestart' => 1486396800,
511                     'timeduration' => 0,
512                     'timesort' => 1486396800,
513                     'visible' => 1,
514                     'timemodified' => 1485793098,
515                     'subscriptionid' => null
516                 ]
517             ]
518         ];
519     }
521     /**
522      * Helper function to create calendar events using the old code.
523      *
524      * @param array $properties A list of calendar event properties to set
525      * @return calendar_event|bool
526      */
527     protected function create_event($properties = []) {
528         $record = new \stdClass();
529         $record->name = 'event name';
530         $record->eventtype = 'global';
531         $record->timestart = time();
532         $record->timeduration = 0;
533         $record->timesort = 0;
534         $record->type = 1;
535         $record->courseid = 0;
536         $record->categoryid = 0;
538         foreach ($properties as $name => $value) {
539             $record->$name = $value;
540         }
542         $event = new calendar_event($record);
543         return $event->create($record, false);
544     }
546     /**
547      * Pad out a basic DB row with basic information.
548      *
549      * @param   \stdClass   $skeleton the current skeleton
550      * @return  \stdClass
551      */
552     protected function get_dbrow_from_skeleton($skeleton) {
553         $dbrow = (object) [
554             'name' => 'Name',
555             'description' => 'Description',
556             'format' => 1,
557             'categoryid' => 0,
558             'courseid' => 0,
559             'groupid' => 0,
560             'userid' => 0,
561             'repeatid' => 0,
562             'modulename' => '',
563             'instance' => 0,
564             'eventtype' => 'user',
565             'timestart' => 1486396800,
566             'timeduration' => 0,
567             'timesort' => 1486396800,
568             'visible' => 1,
569             'timemodified' => 1485793098,
570             'subscriptionid' => null
571         ];
573         foreach ((array) $skeleton as $key => $value) {
574             $dbrow->$key = $value;
575         }
577         return $dbrow;
578     }