6c6e7536ceb258e281abc6199c5460effaffd2da
[moodle.git] / calendar / tests / lib_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  * Contains the class containing unit tests for the calendar lib.
19  *
20  * @package    core_calendar
21  * @copyright  2017 Mark Nelson <markn@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/helpers.php');
29 /**
30  * Class contaning unit tests for the calendar lib.
31  *
32  * @package    core_calendar
33  * @copyright  2017 Mark Nelson <markn@moodle.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class core_calendar_lib_testcase extends advanced_testcase {
38     /**
39      * Tests set up
40      */
41     protected function setUp() {
42         $this->resetAfterTest();
43     }
45     /**
46      * Test that the get_events() function only returns activity events that are enabled.
47      */
48     public function test_get_events_with_disabled_module() {
49         global $DB;
50         $this->setAdminUser();
51         $generator = $this->getDataGenerator();
52         $course = $generator->create_course();
53         $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
54         $assigninstance = $assigngenerator->create_instance(['course' => $course->id]);
55         $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
56         $lessoninstance = $lessongenerator->create_instance(['course' => $course->id]);
57         $student = $generator->create_user();
58         $generator->enrol_user($student->id, $course->id, 'student');
59         $this->setUser($student);
60         $events = [
61             [
62                 'name' => 'Start of assignment',
63                 'description' => '',
64                 'format' => 1,
65                 'courseid' => $course->id,
66                 'groupid' => 0,
67                 'userid' => 2,
68                 'modulename' => 'assign',
69                 'instance' => $assigninstance->id,
70                 'eventtype' => 'due',
71                 'timestart' => time(),
72                 'timeduration' => 86400,
73                 'visible' => 1
74             ], [
75                 'name' => 'Start of lesson',
76                 'description' => '',
77                 'format' => 1,
78                 'courseid' => $course->id,
79                 'groupid' => 0,
80                 'userid' => 2,
81                 'modulename' => 'lesson',
82                 'instance' => $lessoninstance->id,
83                 'eventtype' => 'end',
84                 'timestart' => time(),
85                 'timeduration' => 86400,
86                 'visible' => 1
87             ]
88         ];
89         foreach ($events as $event) {
90             calendar_event::create($event, false);
91         }
92         $timestart = time() - 60;
93         $timeend = time() + 60;
94         // Get all events.
95         $events = calendar_get_events($timestart, $timeend, true, 0, true);
96         $this->assertCount(2, $events);
97         // Disable the lesson module.
98         $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
99         $modulerecord->visible = 0;
100         $DB->update_record('modules', $modulerecord);
101         // Check that we only return the assign event.
102         $events = calendar_get_events($timestart, $timeend, true, 0, true);
103         $this->assertCount(1, $events);
104         $event = reset($events);
105         $this->assertEquals('assign', $event->modulename);
106     }
108     public function test_get_course_cached() {
109         // Setup some test courses.
110         $course1 = $this->getDataGenerator()->create_course();
111         $course2 = $this->getDataGenerator()->create_course();
112         $course3 = $this->getDataGenerator()->create_course();
114         // Load courses into cache.
115         $coursecache = null;
116         calendar_get_course_cached($coursecache, $course1->id);
117         calendar_get_course_cached($coursecache, $course2->id);
118         calendar_get_course_cached($coursecache, $course3->id);
120         // Verify the cache.
121         $this->assertArrayHasKey($course1->id, $coursecache);
122         $cachedcourse1 = $coursecache[$course1->id];
123         $this->assertEquals($course1->id, $cachedcourse1->id);
124         $this->assertEquals($course1->shortname, $cachedcourse1->shortname);
125         $this->assertEquals($course1->fullname, $cachedcourse1->fullname);
127         $this->assertArrayHasKey($course2->id, $coursecache);
128         $cachedcourse2 = $coursecache[$course2->id];
129         $this->assertEquals($course2->id, $cachedcourse2->id);
130         $this->assertEquals($course2->shortname, $cachedcourse2->shortname);
131         $this->assertEquals($course2->fullname, $cachedcourse2->fullname);
133         $this->assertArrayHasKey($course3->id, $coursecache);
134         $cachedcourse3 = $coursecache[$course3->id];
135         $this->assertEquals($course3->id, $cachedcourse3->id);
136         $this->assertEquals($course3->shortname, $cachedcourse3->shortname);
137         $this->assertEquals($course3->fullname, $cachedcourse3->fullname);
138     }
140     /**
141      * Test the update_subscription() function.
142      */
143     public function test_update_subscription() {
144         $this->resetAfterTest(true);
146         $subscription = new stdClass();
147         $subscription->eventtype = 'site';
148         $subscription->name = 'test';
149         $id = calendar_add_subscription($subscription);
151         $subscription = calendar_get_subscription($id);
152         $subscription->name = 'awesome';
153         calendar_update_subscription($subscription);
154         $sub = calendar_get_subscription($id);
155         $this->assertEquals($subscription->name, $sub->name);
157         $subscription = calendar_get_subscription($id);
158         $subscription->name = 'awesome2';
159         $subscription->pollinterval = 604800;
160         calendar_update_subscription($subscription);
161         $sub = calendar_get_subscription($id);
162         $this->assertEquals($subscription->name, $sub->name);
163         $this->assertEquals($subscription->pollinterval, $sub->pollinterval);
165         $subscription = new stdClass();
166         $subscription->name = 'awesome4';
167         $this->expectException('coding_exception');
168         calendar_update_subscription($subscription);
169     }
171     public function test_add_subscription() {
172         global $DB, $CFG;
174         require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
176         $this->resetAfterTest(true);
178         // Test for Microsoft Outlook 2010.
179         $subscription = new stdClass();
180         $subscription->name = 'Microsoft Outlook 2010';
181         $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
182         $subscription->eventtype = 'site';
183         $id = calendar_add_subscription($subscription);
185         $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
186         $ical = new iCalendar();
187         $ical->unserialize($calendar);
188         $this->assertEquals($ical->parser_errors, array());
190         $sub = calendar_get_subscription($id);
191         calendar_import_icalendar_events($ical, null, $sub->id);
192         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
193         $this->assertEquals($count, 1);
195         // Test for OSX Yosemite.
196         $subscription = new stdClass();
197         $subscription->name = 'OSX Yosemite';
198         $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
199         $subscription->eventtype = 'site';
200         $id = calendar_add_subscription($subscription);
202         $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
203         $ical = new iCalendar();
204         $ical->unserialize($calendar);
205         $this->assertEquals($ical->parser_errors, array());
207         $sub = calendar_get_subscription($id);
208         calendar_import_icalendar_events($ical, null, $sub->id);
209         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
210         $this->assertEquals($count, 1);
212         // Test for Google Gmail.
213         $subscription = new stdClass();
214         $subscription->name = 'Google Gmail';
215         $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
216         $subscription->eventtype = 'site';
217         $id = calendar_add_subscription($subscription);
219         $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
220         $ical = new iCalendar();
221         $ical->unserialize($calendar);
222         $this->assertEquals($ical->parser_errors, array());
224         $sub = calendar_get_subscription($id);
225         calendar_import_icalendar_events($ical, null, $sub->id);
226         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
227         $this->assertEquals($count, 1);
228     }
230     /**
231      * Test for calendar_get_legacy_events() when there are user and group overrides.
232      */
233     public function test_get_legacy_events_with_overrides() {
234         $generator = $this->getDataGenerator();
236         $course = $generator->create_course();
238         $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
239         if (!isset($params['course'])) {
240             $params['course'] = $course->id;
241         }
243         $instance = $plugingenerator->create_instance($params);
245         // Create users.
246         $useroverridestudent = $generator->create_user();
247         $group1student = $generator->create_user();
248         $group2student = $generator->create_user();
249         $group12student = $generator->create_user();
250         $nogroupstudent = $generator->create_user();
252         // Enrol users.
253         $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
254         $generator->enrol_user($group1student->id, $course->id, 'student');
255         $generator->enrol_user($group2student->id, $course->id, 'student');
256         $generator->enrol_user($group12student->id, $course->id, 'student');
257         $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
259         // Create groups.
260         $group1 = $generator->create_group(['courseid' => $course->id]);
261         $group2 = $generator->create_group(['courseid' => $course->id]);
263         // Add members to groups.
264         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
265         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
266         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
267         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
268         $now = time();
270         // Events with the same module name, instance and event type.
271         $events = [
272             [
273                 'name' => 'Assignment 1 due date',
274                 'description' => '',
275                 'format' => 0,
276                 'courseid' => $course->id,
277                 'groupid' => 0,
278                 'userid' => 2,
279                 'modulename' => 'assign',
280                 'instance' => $instance->id,
281                 'eventtype' => 'due',
282                 'timestart' => $now,
283                 'timeduration' => 0,
284                 'visible' => 1
285             ], [
286                 'name' => 'Assignment 1 due date - User override',
287                 'description' => '',
288                 'format' => 1,
289                 'courseid' => 0,
290                 'groupid' => 0,
291                 'userid' => $useroverridestudent->id,
292                 'modulename' => 'assign',
293                 'instance' => $instance->id,
294                 'eventtype' => 'due',
295                 'timestart' => $now + 86400,
296                 'timeduration' => 0,
297                 'visible' => 1,
298                 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
299             ], [
300                 'name' => 'Assignment 1 due date - Group A override',
301                 'description' => '',
302                 'format' => 1,
303                 'courseid' => $course->id,
304                 'groupid' => $group1->id,
305                 'userid' => 2,
306                 'modulename' => 'assign',
307                 'instance' => $instance->id,
308                 'eventtype' => 'due',
309                 'timestart' => $now + (2 * 86400),
310                 'timeduration' => 0,
311                 'visible' => 1,
312                 'priority' => 1,
313             ], [
314                 'name' => 'Assignment 1 due date - Group B override',
315                 'description' => '',
316                 'format' => 1,
317                 'courseid' => $course->id,
318                 'groupid' => $group2->id,
319                 'userid' => 2,
320                 'modulename' => 'assign',
321                 'instance' => $instance->id,
322                 'eventtype' => 'due',
323                 'timestart' => $now + (3 * 86400),
324                 'timeduration' => 0,
325                 'visible' => 1,
326                 'priority' => 2,
327             ],
328         ];
330         foreach ($events as $event) {
331             calendar_event::create($event, false);
332         }
334         $timestart = $now - 100;
335         $timeend = $now + (3 * 86400);
336         $groups = [$group1->id, $group2->id];
338         // Get user override events.
339         $this->setUser($useroverridestudent);
340         $events = calendar_get_legacy_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
341         $this->assertCount(1, $events);
342         $event = reset($events);
343         $this->assertEquals('Assignment 1 due date - User override', $event->name);
345         // Get event for user with override but with the timestart and timeend parameters only covering the original event.
346         $events = calendar_get_legacy_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
347         $this->assertCount(0, $events);
349         // Get events for user that does not belong to any group and has no user override events.
350         $this->setUser($nogroupstudent);
351         $events = calendar_get_legacy_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
352         $this->assertCount(1, $events);
353         $event = reset($events);
354         $this->assertEquals('Assignment 1 due date', $event->name);
356         // Get events for user that belongs to groups A and B and has no user override events.
357         $this->setUser($group12student);
358         $events = calendar_get_legacy_events($timestart, $timeend, $group12student->id, $groups, $course->id);
359         $this->assertCount(1, $events);
360         $event = reset($events);
361         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
363         // Get events for user that belongs to group A and has no user override events.
364         $this->setUser($group1student);
365         $events = calendar_get_legacy_events($timestart, $timeend, $group1student->id, $groups, $course->id);
366         $this->assertCount(1, $events);
367         $event = reset($events);
368         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
370         // Add repeating events.
371         $repeatingevents = [
372             [
373                 'name' => 'Repeating site event',
374                 'description' => '',
375                 'format' => 1,
376                 'courseid' => SITEID,
377                 'groupid' => 0,
378                 'userid' => 2,
379                 'repeatid' => $event->id,
380                 'modulename' => '0',
381                 'instance' => 0,
382                 'eventtype' => 'site',
383                 'timestart' => $now + 86400,
384                 'timeduration' => 0,
385                 'visible' => 1,
386             ],
387             [
388                 'name' => 'Repeating site event',
389                 'description' => '',
390                 'format' => 1,
391                 'courseid' => SITEID,
392                 'groupid' => 0,
393                 'userid' => 2,
394                 'repeatid' => $event->id,
395                 'modulename' => '0',
396                 'instance' => 0,
397                 'eventtype' => 'site',
398                 'timestart' => $now + (2 * 86400),
399                 'timeduration' => 0,
400                 'visible' => 1,
401             ],
402         ];
404         foreach ($repeatingevents as $event) {
405             calendar_event::create($event, false);
406         }
408         // Make sure repeating events are not filtered out.
409         $events = calendar_get_legacy_events($timestart, $timeend, true, true, true);
410         $this->assertCount(3, $events);
411     }
413     public function test_calendar_get_all_allowed_types_no_types() {
414         $generator = $this->getDataGenerator();
415         $user = $generator->create_user();
416         $systemcontext = context_system::instance();
417         $sitecontext = context_course::instance(SITEID);
418         $roleid = $generator->create_role();
420         $generator->role_assign($roleid, $user->id, $systemcontext->id);
421         $generator->role_assign($roleid, $user->id, $sitecontext->id);
422         $this->setUser($user);
424         assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $sitecontext, true);
425         assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $systemcontext, true);
427         $types = calendar_get_all_allowed_types();
428         $this->assertEmpty($types);
429     }
431     public function test_calendar_get_all_allowed_types_user() {
432         $generator = $this->getDataGenerator();
433         $user = $generator->create_user();
434         $context = context_system::instance();
435         $roleid = $generator->create_role();
437         $generator->role_assign($roleid, $user->id, $context->id);
438         $this->setUser($user);
440         assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true);
442         $types = calendar_get_all_allowed_types();
443         $this->assertTrue($types['user']);
445         assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
447         $types = calendar_get_all_allowed_types();
448         $this->assertArrayNotHasKey('user', $types);
449     }
451     public function test_calendar_get_all_allowed_types_site() {
452         $generator = $this->getDataGenerator();
453         $user = $generator->create_user();
454         $context = context_course::instance(SITEID);
455         $roleid = $generator->create_role();
457         $generator->role_assign($roleid, $user->id, $context->id);
458         $this->setUser($user);
460         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
462         $types = calendar_get_all_allowed_types();
463         $this->assertTrue($types['site']);
465         assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
467         $types = calendar_get_all_allowed_types();
468         $this->assertArrayNotHasKey('site', $types);
469     }
471     public function test_calendar_get_all_allowed_types_course() {
472         $generator = $this->getDataGenerator();
473         $user = $generator->create_user();
474         $course1 = $generator->create_course(); // Has capability.
475         $course2 = $generator->create_course(); // Doesn't have capability.
476         $course3 = $generator->create_course(); // Not enrolled.
477         $context1 = context_course::instance($course1->id);
478         $context2 = context_course::instance($course2->id);
479         $context3 = context_course::instance($course3->id);
480         $roleid = $generator->create_role();
481         $contexts = [$context1, $context2, $context3];
482         $enrolledcourses = [$course1, $course2];
484         foreach ($enrolledcourses as $course) {
485             $generator->enrol_user($user->id, $course->id, 'student');
486         }
488         foreach ($contexts as $context) {
489             $generator->role_assign($roleid, $user->id, $context->id);
490         }
492         $this->setUser($user);
494         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
495         assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true);
497         // The user only has the correct capability in course 1 so that is the only
498         // one that should be in the results.
499         $types = calendar_get_all_allowed_types();
500         $typecourses = $types['course'];
501         $this->assertCount(1, $typecourses);
502         $this->assertEquals($course1->id, $typecourses[$course1->id]->id);
504         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
506         // The user only now has the correct capability in both course 1 and 2 so we
507         // expect both to be in the results.
508         $types = calendar_get_all_allowed_types();
509         $typecourses = $types['course'];
510         // Sort the results by id ascending to ensure the test is consistent
511         // and repeatable.
512         usort($typecourses, function($a, $b) {
513             $aid = $a->id;
514             $bid = $b->id;
516             if ($aid == $bid) {
517                 return 0;
518             }
519             return ($aid < $bid) ? -1 : 1;
520         });
522         $this->assertCount(2, $typecourses);
523         $this->assertEquals($course1->id, $typecourses[0]->id);
524         $this->assertEquals($course2->id, $typecourses[1]->id);
525     }
527     public function test_calendar_get_all_allowed_types_group_no_groups() {
528         $generator = $this->getDataGenerator();
529         $user = $generator->create_user();
530         $course = $generator->create_course();
531         $context = context_course::instance($course->id);
532         $roleid = $generator->create_role();
534         $generator->enrol_user($user->id, $course->id, 'student');
535         $generator->role_assign($roleid, $user->id, $context->id);
537         $this->setUser($user);
539         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
541         // The user has the correct capability in the course but there are
542         // no groups so we shouldn't see a group type.
543         $types = calendar_get_all_allowed_types();
544         $typecourses = $types['course'];
545         $this->assertCount(1, $typecourses);
546         $this->assertEquals($course->id, $typecourses[$course->id]->id);
547         $this->assertArrayNotHasKey('group', $types);
548         $this->assertArrayNotHasKey('groupcourses', $types);
549     }
551     public function test_calendar_get_all_allowed_types_group_no_acces_to_diff_groups() {
552         $generator = $this->getDataGenerator();
553         $user = $generator->create_user();
554         $course = $generator->create_course();
555         $context = context_course::instance($course->id);
556         $group1 = $generator->create_group(array('courseid' => $course->id));
557         $group2 = $generator->create_group(array('courseid' => $course->id));
558         $roleid = $generator->create_role();
560         $generator->enrol_user($user->id, $course->id, 'student');
561         $generator->role_assign($roleid, $user->id, $context->id);
563         $this->setUser($user);
565         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
566         assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
568         // The user has the correct capability in the course but they aren't a member
569         // of any of the groups and don't have the accessallgroups capability.
570         $types = calendar_get_all_allowed_types();
571         $typecourses = $types['course'];
572         $this->assertCount(1, $typecourses);
573         $this->assertEquals($course->id, $typecourses[$course->id]->id);
574         $this->assertArrayNotHasKey('group', $types);
575         $this->assertArrayNotHasKey('groupcourses', $types);
576     }
578     public function test_calendar_get_all_allowed_types_group_access_all_groups() {
579         $generator = $this->getDataGenerator();
580         $user = $generator->create_user();
581         $course1 = $generator->create_course();
582         $course2 = $generator->create_course();
583         $context1 = context_course::instance($course1->id);
584         $context2 = context_course::instance($course2->id);
585         $group1 = $generator->create_group(array('courseid' => $course1->id));
586         $group2 = $generator->create_group(array('courseid' => $course1->id));
587         $roleid = $generator->create_role();
589         $generator->enrol_user($user->id, $course1->id, 'student');
590         $generator->enrol_user($user->id, $course2->id, 'student');
591         $generator->role_assign($roleid, $user->id, $context1->id);
592         $generator->role_assign($roleid, $user->id, $context2->id);
594         $this->setUser($user);
596         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
597         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
598         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context1, true);
599         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context2, true);
601         // The user has the correct capability in the course and has
602         // the accessallgroups capability.
603         $types = calendar_get_all_allowed_types();
604         $typecourses = $types['course'];
605         $typegroups = $types['group'];
606         $typegroupcourses = $types['groupcourses'];
607         $idascfunc = function($a, $b) {
608             $aid = $a->id;
609             $bid = $b->id;
611             if ($aid == $bid) {
612                 return 0;
613             }
614             return ($aid < $bid) ? -1 : 1;
615         };
616         // Sort the results by id ascending to ensure the test is consistent
617         // and repeatable.
618         usort($typecourses, $idascfunc);
619         usort($typegroups, $idascfunc);
621         $this->assertCount(2, $typecourses);
622         $this->assertEquals($course1->id, $typecourses[0]->id);
623         $this->assertEquals($course2->id, $typecourses[1]->id);
624         $this->assertCount(1, $typegroupcourses);
625         $this->assertEquals($course1->id, $typegroupcourses[$course1->id]->id);
626         $this->assertCount(2, $typegroups);
627         $this->assertEquals($group1->id, $typegroups[0]->id);
628         $this->assertEquals($group2->id, $typegroups[1]->id);
629     }
631     public function test_calendar_get_all_allowed_types_group_no_access_all_groups() {
632         $generator = $this->getDataGenerator();
633         $user = $generator->create_user();
634         $course = $generator->create_course();
635         $context = context_course::instance($course->id);
636         $group1 = $generator->create_group(array('courseid' => $course->id));
637         $group2 = $generator->create_group(array('courseid' => $course->id));
638         $group3 = $generator->create_group(array('courseid' => $course->id));
639         $roleid = $generator->create_role();
641         $generator->enrol_user($user->id, $course->id, 'student');
642         $generator->role_assign($roleid, $user->id, $context->id);
643         $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user->id));
644         $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user->id));
646         $this->setUser($user);
648         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
649         assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
651         // The user has the correct capability in the course but can't access
652         // groups that they are not a member of.
653         $types = calendar_get_all_allowed_types();
654         $typegroups = $types['group'];
655         $typegroupcourses = $types['groupcourses'];
656         $idascfunc = function($a, $b) {
657             $aid = $a->id;
658             $bid = $b->id;
660             if ($aid == $bid) {
661                 return 0;
662             }
663             return ($aid < $bid) ? -1 : 1;
664         };
665         // Sort the results by id ascending to ensure the test is consistent
666         // and repeatable.
667         usort($typegroups, $idascfunc);
669         $this->assertCount(1, $typegroupcourses);
670         $this->assertEquals($course->id, $typegroupcourses[$course->id]->id);
671         $this->assertCount(2, $typegroups);
672         $this->assertEquals($group1->id, $typegroups[0]->id);
673         $this->assertEquals($group2->id, $typegroups[1]->id);
674     }
676     public function test_calendar_get_default_courses() {
677         global $USER, $CFG;
679         $this->resetAfterTest(true);
681         $generator = $this->getDataGenerator();
682         $user = $generator->create_user();
683         $course1 = $generator->create_course();
684         $course2 = $generator->create_course();
685         $course3 = $generator->create_course();
686         $context = context_course::instance($course1->id);
688         $this->setAdminUser();
689         $admin = clone $USER;
691         $teacher = $generator->create_user();
692         $generator->enrol_user($teacher->id, $course1->id, 'teacher');
693         $generator->enrol_user($admin->id, $course1->id, 'teacher');
695         $CFG->calendar_adminseesall = false;
697         $courses = calendar_get_default_courses();
698         // Only enrolled in one course.
699         $this->assertCount(1, $courses);
700         $courses = calendar_get_default_courses($course2->id);
701         // Enrolled course + current course.
702         $this->assertCount(2, $courses);
703         $CFG->calendar_adminseesall = true;
704         $courses = calendar_get_default_courses();
705         // All courses + SITE.
706         $this->assertCount(4, $courses);
707         $courses = calendar_get_default_courses($course2->id);
708         // All courses + SITE.
709         $this->assertCount(4, $courses);
711         $this->setUser($teacher);
713         $CFG->calendar_adminseesall = false;
715         $courses = calendar_get_default_courses();
716         // Only enrolled in one course.
717         $this->assertCount(1, $courses);
718         $courses = calendar_get_default_courses($course2->id);
719         // Enrolled course only (ignore current).
720         $this->assertCount(1, $courses);
721         // This setting should not affect teachers.
722         $CFG->calendar_adminseesall = true;
723         $courses = calendar_get_default_courses();
724         // Only enrolled in one course.
725         $this->assertCount(1, $courses);
726         $courses = calendar_get_default_courses($course2->id);
727         // Enrolled course only (ignore current).
728         $this->assertCount(1, $courses);
730     }
732     /**
733      * Confirm that the skip events flag causes the calendar_get_view function
734      * to avoid querying for the calendar events.
735      */
736     public function test_calendar_get_view_skip_events() {
737         $this->resetAfterTest(true);
738         $this->setAdminUser();
740         $generator = $this->getDataGenerator();
741         $user = $generator->create_user();
742         $skipnavigation = true;
743         $skipevents = true;
744         $event = create_event([
745             'eventtype' => 'user',
746             'userid' => $user->id
747         ]);
749         $this->setUser($user);
750         $calendar = \calendar_information::create(time() - 10, SITEID, null);
752         list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
753         $this->assertEmpty($data->events);
755         $skipevents = false;
756         list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
758         $this->assertEquals($event->id, $data->events[0]->id);
759     }