MDL-37624 calendar: Added location support
[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                 'location' => 'Test',
65                 'format' => 1,
66                 'courseid' => $course->id,
67                 'groupid' => 0,
68                 'userid' => 2,
69                 'modulename' => 'assign',
70                 'instance' => $assigninstance->id,
71                 'eventtype' => 'due',
72                 'timestart' => time(),
73                 'timeduration' => 86400,
74                 'visible' => 1
75             ], [
76                 'name' => 'Start of lesson',
77                 'description' => '',
78                 'location' => 'Test',
79                 'format' => 1,
80                 'courseid' => $course->id,
81                 'groupid' => 0,
82                 'userid' => 2,
83                 'modulename' => 'lesson',
84                 'instance' => $lessoninstance->id,
85                 'eventtype' => 'end',
86                 'timestart' => time(),
87                 'timeduration' => 86400,
88                 'visible' => 1
89             ]
90         ];
91         foreach ($events as $event) {
92             calendar_event::create($event, false);
93         }
94         $timestart = time() - 60;
95         $timeend = time() + 60;
96         // Get all events.
97         $events = calendar_get_events($timestart, $timeend, true, 0, true);
98         $this->assertCount(2, $events);
99         // Disable the lesson module.
100         $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
101         $modulerecord->visible = 0;
102         $DB->update_record('modules', $modulerecord);
103         // Check that we only return the assign event.
104         $events = calendar_get_events($timestart, $timeend, true, 0, true);
105         $this->assertCount(1, $events);
106         $event = reset($events);
107         $this->assertEquals('assign', $event->modulename);
108     }
110     public function test_get_course_cached() {
111         // Setup some test courses.
112         $course1 = $this->getDataGenerator()->create_course();
113         $course2 = $this->getDataGenerator()->create_course();
114         $course3 = $this->getDataGenerator()->create_course();
116         // Load courses into cache.
117         $coursecache = null;
118         calendar_get_course_cached($coursecache, $course1->id);
119         calendar_get_course_cached($coursecache, $course2->id);
120         calendar_get_course_cached($coursecache, $course3->id);
122         // Verify the cache.
123         $this->assertArrayHasKey($course1->id, $coursecache);
124         $cachedcourse1 = $coursecache[$course1->id];
125         $this->assertEquals($course1->id, $cachedcourse1->id);
126         $this->assertEquals($course1->shortname, $cachedcourse1->shortname);
127         $this->assertEquals($course1->fullname, $cachedcourse1->fullname);
129         $this->assertArrayHasKey($course2->id, $coursecache);
130         $cachedcourse2 = $coursecache[$course2->id];
131         $this->assertEquals($course2->id, $cachedcourse2->id);
132         $this->assertEquals($course2->shortname, $cachedcourse2->shortname);
133         $this->assertEquals($course2->fullname, $cachedcourse2->fullname);
135         $this->assertArrayHasKey($course3->id, $coursecache);
136         $cachedcourse3 = $coursecache[$course3->id];
137         $this->assertEquals($course3->id, $cachedcourse3->id);
138         $this->assertEquals($course3->shortname, $cachedcourse3->shortname);
139         $this->assertEquals($course3->fullname, $cachedcourse3->fullname);
140     }
142     /**
143      * Test the update_subscription() function.
144      */
145     public function test_update_subscription() {
146         $this->resetAfterTest(true);
148         $subscription = new stdClass();
149         $subscription->eventtype = 'site';
150         $subscription->name = 'test';
151         $id = calendar_add_subscription($subscription);
153         $subscription = calendar_get_subscription($id);
154         $subscription->name = 'awesome';
155         calendar_update_subscription($subscription);
156         $sub = calendar_get_subscription($id);
157         $this->assertEquals($subscription->name, $sub->name);
159         $subscription = calendar_get_subscription($id);
160         $subscription->name = 'awesome2';
161         $subscription->pollinterval = 604800;
162         calendar_update_subscription($subscription);
163         $sub = calendar_get_subscription($id);
164         $this->assertEquals($subscription->name, $sub->name);
165         $this->assertEquals($subscription->pollinterval, $sub->pollinterval);
167         $subscription = new stdClass();
168         $subscription->name = 'awesome4';
169         $this->expectException('coding_exception');
170         calendar_update_subscription($subscription);
171     }
173     public function test_add_subscription() {
174         global $DB, $CFG;
176         require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
178         $this->resetAfterTest(true);
180         // Test for Microsoft Outlook 2010.
181         $subscription = new stdClass();
182         $subscription->name = 'Microsoft Outlook 2010';
183         $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
184         $subscription->eventtype = 'site';
185         $id = calendar_add_subscription($subscription);
187         $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
188         $ical = new iCalendar();
189         $ical->unserialize($calendar);
190         $this->assertEquals($ical->parser_errors, array());
192         $sub = calendar_get_subscription($id);
193         calendar_import_icalendar_events($ical, null, $sub->id);
194         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
195         $this->assertEquals($count, 1);
197         // Test for OSX Yosemite.
198         $subscription = new stdClass();
199         $subscription->name = 'OSX Yosemite';
200         $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
201         $subscription->eventtype = 'site';
202         $id = calendar_add_subscription($subscription);
204         $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
205         $ical = new iCalendar();
206         $ical->unserialize($calendar);
207         $this->assertEquals($ical->parser_errors, array());
209         $sub = calendar_get_subscription($id);
210         calendar_import_icalendar_events($ical, null, $sub->id);
211         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
212         $this->assertEquals($count, 1);
214         // Test for Google Gmail.
215         $subscription = new stdClass();
216         $subscription->name = 'Google Gmail';
217         $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
218         $subscription->eventtype = 'site';
219         $id = calendar_add_subscription($subscription);
221         $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
222         $ical = new iCalendar();
223         $ical->unserialize($calendar);
224         $this->assertEquals($ical->parser_errors, array());
226         $sub = calendar_get_subscription($id);
227         calendar_import_icalendar_events($ical, null, $sub->id);
228         $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
229         $this->assertEquals($count, 1);
230     }
232     /**
233      * Test for calendar_get_legacy_events() when there are user and group overrides.
234      */
235     public function test_get_legacy_events_with_overrides() {
236         $generator = $this->getDataGenerator();
238         $course = $generator->create_course();
240         $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
241         if (!isset($params['course'])) {
242             $params['course'] = $course->id;
243         }
245         $instance = $plugingenerator->create_instance($params);
247         // Create users.
248         $useroverridestudent = $generator->create_user();
249         $group1student = $generator->create_user();
250         $group2student = $generator->create_user();
251         $group12student = $generator->create_user();
252         $nogroupstudent = $generator->create_user();
254         // Enrol users.
255         $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
256         $generator->enrol_user($group1student->id, $course->id, 'student');
257         $generator->enrol_user($group2student->id, $course->id, 'student');
258         $generator->enrol_user($group12student->id, $course->id, 'student');
259         $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
261         // Create groups.
262         $group1 = $generator->create_group(['courseid' => $course->id]);
263         $group2 = $generator->create_group(['courseid' => $course->id]);
265         // Add members to groups.
266         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
267         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
268         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
269         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
270         $now = time();
272         // Events with the same module name, instance and event type.
273         $events = [
274             [
275                 'name' => 'Assignment 1 due date',
276                 'description' => '',
277                 'location' => 'Test',
278                 'format' => 0,
279                 'courseid' => $course->id,
280                 'groupid' => 0,
281                 'userid' => 2,
282                 'modulename' => 'assign',
283                 'instance' => $instance->id,
284                 'eventtype' => 'due',
285                 'timestart' => $now,
286                 'timeduration' => 0,
287                 'visible' => 1
288             ], [
289                 'name' => 'Assignment 1 due date - User override',
290                 'description' => '',
291                 'location' => 'Test',
292                 'format' => 1,
293                 'courseid' => 0,
294                 'groupid' => 0,
295                 'userid' => $useroverridestudent->id,
296                 'modulename' => 'assign',
297                 'instance' => $instance->id,
298                 'eventtype' => 'due',
299                 'timestart' => $now + 86400,
300                 'timeduration' => 0,
301                 'visible' => 1,
302                 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
303             ], [
304                 'name' => 'Assignment 1 due date - Group A override',
305                 'description' => '',
306                 'location' => 'Test',
307                 'format' => 1,
308                 'courseid' => $course->id,
309                 'groupid' => $group1->id,
310                 'userid' => 2,
311                 'modulename' => 'assign',
312                 'instance' => $instance->id,
313                 'eventtype' => 'due',
314                 'timestart' => $now + (2 * 86400),
315                 'timeduration' => 0,
316                 'visible' => 1,
317                 'priority' => 1,
318             ], [
319                 'name' => 'Assignment 1 due date - Group B override',
320                 'description' => '',
321                 'location' => 'Test',
322                 'format' => 1,
323                 'courseid' => $course->id,
324                 'groupid' => $group2->id,
325                 'userid' => 2,
326                 'modulename' => 'assign',
327                 'instance' => $instance->id,
328                 'eventtype' => 'due',
329                 'timestart' => $now + (3 * 86400),
330                 'timeduration' => 0,
331                 'visible' => 1,
332                 'priority' => 2,
333             ],
334         ];
336         foreach ($events as $event) {
337             calendar_event::create($event, false);
338         }
340         $timestart = $now - 100;
341         $timeend = $now + (3 * 86400);
342         $groups = [$group1->id, $group2->id];
344         // Get user override events.
345         $this->setUser($useroverridestudent);
346         $events = calendar_get_legacy_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
347         $this->assertCount(1, $events);
348         $event = reset($events);
349         $this->assertEquals('Assignment 1 due date - User override', $event->name);
351         // Get event for user with override but with the timestart and timeend parameters only covering the original event.
352         $events = calendar_get_legacy_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
353         $this->assertCount(0, $events);
355         // Get events for user that does not belong to any group and has no user override events.
356         $this->setUser($nogroupstudent);
357         $events = calendar_get_legacy_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
358         $this->assertCount(1, $events);
359         $event = reset($events);
360         $this->assertEquals('Assignment 1 due date', $event->name);
362         // Get events for user that belongs to groups A and B and has no user override events.
363         $this->setUser($group12student);
364         $events = calendar_get_legacy_events($timestart, $timeend, $group12student->id, $groups, $course->id);
365         $this->assertCount(1, $events);
366         $event = reset($events);
367         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
369         // Get events for user that belongs to group A and has no user override events.
370         $this->setUser($group1student);
371         $events = calendar_get_legacy_events($timestart, $timeend, $group1student->id, $groups, $course->id);
372         $this->assertCount(1, $events);
373         $event = reset($events);
374         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
376         // Add repeating events.
377         $repeatingevents = [
378             [
379                 'name' => 'Repeating site event',
380                 'description' => '',
381                 'location' => 'Test',
382                 'format' => 1,
383                 'courseid' => SITEID,
384                 'groupid' => 0,
385                 'userid' => 2,
386                 'repeatid' => $event->id,
387                 'modulename' => '0',
388                 'instance' => 0,
389                 'eventtype' => 'site',
390                 'timestart' => $now + 86400,
391                 'timeduration' => 0,
392                 'visible' => 1,
393             ],
394             [
395                 'name' => 'Repeating site event',
396                 'description' => '',
397                 'location' => 'Test',
398                 'format' => 1,
399                 'courseid' => SITEID,
400                 'groupid' => 0,
401                 'userid' => 2,
402                 'repeatid' => $event->id,
403                 'modulename' => '0',
404                 'instance' => 0,
405                 'eventtype' => 'site',
406                 'timestart' => $now + (2 * 86400),
407                 'timeduration' => 0,
408                 'visible' => 1,
409             ],
410         ];
412         foreach ($repeatingevents as $event) {
413             calendar_event::create($event, false);
414         }
416         // Make sure repeating events are not filtered out.
417         $events = calendar_get_legacy_events($timestart, $timeend, true, true, true);
418         $this->assertCount(3, $events);
419     }
421     public function test_calendar_get_all_allowed_types_no_types() {
422         $generator = $this->getDataGenerator();
423         $user = $generator->create_user();
424         $systemcontext = context_system::instance();
425         $sitecontext = context_course::instance(SITEID);
426         $roleid = $generator->create_role();
428         $generator->role_assign($roleid, $user->id, $systemcontext->id);
429         $generator->role_assign($roleid, $user->id, $sitecontext->id);
430         $this->setUser($user);
432         assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $sitecontext, true);
433         assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $systemcontext, true);
435         $types = calendar_get_all_allowed_types();
436         $this->assertEmpty($types);
437     }
439     public function test_calendar_get_all_allowed_types_user() {
440         $generator = $this->getDataGenerator();
441         $user = $generator->create_user();
442         $context = context_system::instance();
443         $roleid = $generator->create_role();
445         $generator->role_assign($roleid, $user->id, $context->id);
446         $this->setUser($user);
448         assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true);
450         $types = calendar_get_all_allowed_types();
451         $this->assertTrue($types['user']);
453         assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
455         $types = calendar_get_all_allowed_types();
456         $this->assertArrayNotHasKey('user', $types);
457     }
459     public function test_calendar_get_all_allowed_types_site() {
460         $generator = $this->getDataGenerator();
461         $user = $generator->create_user();
462         $context = context_course::instance(SITEID);
463         $roleid = $generator->create_role();
465         $generator->role_assign($roleid, $user->id, $context->id);
466         $this->setUser($user);
468         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
470         $types = calendar_get_all_allowed_types();
471         $this->assertTrue($types['site']);
473         assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
475         $types = calendar_get_all_allowed_types();
476         $this->assertArrayNotHasKey('site', $types);
477     }
479     public function test_calendar_get_all_allowed_types_course() {
480         $generator = $this->getDataGenerator();
481         $user = $generator->create_user();
482         $course1 = $generator->create_course(); // Has capability.
483         $course2 = $generator->create_course(); // Doesn't have capability.
484         $course3 = $generator->create_course(); // Not enrolled.
485         $context1 = context_course::instance($course1->id);
486         $context2 = context_course::instance($course2->id);
487         $context3 = context_course::instance($course3->id);
488         $roleid = $generator->create_role();
489         $contexts = [$context1, $context2, $context3];
490         $enrolledcourses = [$course1, $course2];
492         foreach ($enrolledcourses as $course) {
493             $generator->enrol_user($user->id, $course->id, 'student');
494         }
496         foreach ($contexts as $context) {
497             $generator->role_assign($roleid, $user->id, $context->id);
498         }
500         $this->setUser($user);
502         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
503         assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true);
505         // The user only has the correct capability in course 1 so that is the only
506         // one that should be in the results.
507         $types = calendar_get_all_allowed_types();
508         $typecourses = $types['course'];
509         $this->assertCount(1, $typecourses);
510         $this->assertEquals($course1->id, $typecourses[$course1->id]->id);
512         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
514         // The user only now has the correct capability in both course 1 and 2 so we
515         // expect both to be in the results.
516         $types = calendar_get_all_allowed_types();
517         $typecourses = $types['course'];
518         // Sort the results by id ascending to ensure the test is consistent
519         // and repeatable.
520         usort($typecourses, function($a, $b) {
521             $aid = $a->id;
522             $bid = $b->id;
524             if ($aid == $bid) {
525                 return 0;
526             }
527             return ($aid < $bid) ? -1 : 1;
528         });
530         $this->assertCount(2, $typecourses);
531         $this->assertEquals($course1->id, $typecourses[0]->id);
532         $this->assertEquals($course2->id, $typecourses[1]->id);
533     }
535     public function test_calendar_get_all_allowed_types_group_no_groups() {
536         $generator = $this->getDataGenerator();
537         $user = $generator->create_user();
538         $course = $generator->create_course();
539         $context = context_course::instance($course->id);
540         $roleid = $generator->create_role();
542         $generator->enrol_user($user->id, $course->id, 'student');
543         $generator->role_assign($roleid, $user->id, $context->id);
545         $this->setUser($user);
547         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
549         // The user has the correct capability in the course but there are
550         // no groups so we shouldn't see a group type.
551         $types = calendar_get_all_allowed_types();
552         $typecourses = $types['course'];
553         $this->assertCount(1, $typecourses);
554         $this->assertEquals($course->id, $typecourses[$course->id]->id);
555         $this->assertArrayNotHasKey('group', $types);
556         $this->assertArrayNotHasKey('groupcourses', $types);
557     }
559     public function test_calendar_get_all_allowed_types_group_no_acces_to_diff_groups() {
560         $generator = $this->getDataGenerator();
561         $user = $generator->create_user();
562         $course = $generator->create_course();
563         $context = context_course::instance($course->id);
564         $group1 = $generator->create_group(array('courseid' => $course->id));
565         $group2 = $generator->create_group(array('courseid' => $course->id));
566         $roleid = $generator->create_role();
568         $generator->enrol_user($user->id, $course->id, 'student');
569         $generator->role_assign($roleid, $user->id, $context->id);
571         $this->setUser($user);
573         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
574         assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
576         // The user has the correct capability in the course but they aren't a member
577         // of any of the groups and don't have the accessallgroups capability.
578         $types = calendar_get_all_allowed_types();
579         $typecourses = $types['course'];
580         $this->assertCount(1, $typecourses);
581         $this->assertEquals($course->id, $typecourses[$course->id]->id);
582         $this->assertArrayNotHasKey('group', $types);
583         $this->assertArrayNotHasKey('groupcourses', $types);
584     }
586     public function test_calendar_get_all_allowed_types_group_access_all_groups() {
587         $generator = $this->getDataGenerator();
588         $user = $generator->create_user();
589         $course1 = $generator->create_course();
590         $course2 = $generator->create_course();
591         $context1 = context_course::instance($course1->id);
592         $context2 = context_course::instance($course2->id);
593         $group1 = $generator->create_group(array('courseid' => $course1->id));
594         $group2 = $generator->create_group(array('courseid' => $course1->id));
595         $roleid = $generator->create_role();
597         $generator->enrol_user($user->id, $course1->id, 'student');
598         $generator->enrol_user($user->id, $course2->id, 'student');
599         $generator->role_assign($roleid, $user->id, $context1->id);
600         $generator->role_assign($roleid, $user->id, $context2->id);
602         $this->setUser($user);
604         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
605         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
606         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context1, true);
607         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context2, true);
609         // The user has the correct capability in the course and has
610         // the accessallgroups capability.
611         $types = calendar_get_all_allowed_types();
612         $typecourses = $types['course'];
613         $typegroups = $types['group'];
614         $typegroupcourses = $types['groupcourses'];
615         $idascfunc = function($a, $b) {
616             $aid = $a->id;
617             $bid = $b->id;
619             if ($aid == $bid) {
620                 return 0;
621             }
622             return ($aid < $bid) ? -1 : 1;
623         };
624         // Sort the results by id ascending to ensure the test is consistent
625         // and repeatable.
626         usort($typecourses, $idascfunc);
627         usort($typegroups, $idascfunc);
629         $this->assertCount(2, $typecourses);
630         $this->assertEquals($course1->id, $typecourses[0]->id);
631         $this->assertEquals($course2->id, $typecourses[1]->id);
632         $this->assertCount(1, $typegroupcourses);
633         $this->assertEquals($course1->id, $typegroupcourses[$course1->id]->id);
634         $this->assertCount(2, $typegroups);
635         $this->assertEquals($group1->id, $typegroups[0]->id);
636         $this->assertEquals($group2->id, $typegroups[1]->id);
637     }
639     public function test_calendar_get_all_allowed_types_group_no_access_all_groups() {
640         $generator = $this->getDataGenerator();
641         $user = $generator->create_user();
642         $course = $generator->create_course();
643         $context = context_course::instance($course->id);
644         $group1 = $generator->create_group(array('courseid' => $course->id));
645         $group2 = $generator->create_group(array('courseid' => $course->id));
646         $group3 = $generator->create_group(array('courseid' => $course->id));
647         $roleid = $generator->create_role();
649         $generator->enrol_user($user->id, $course->id, 'student');
650         $generator->role_assign($roleid, $user->id, $context->id);
651         $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user->id));
652         $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user->id));
654         $this->setUser($user);
656         assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
657         assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
659         // The user has the correct capability in the course but can't access
660         // groups that they are not a member of.
661         $types = calendar_get_all_allowed_types();
662         $typegroups = $types['group'];
663         $typegroupcourses = $types['groupcourses'];
664         $idascfunc = function($a, $b) {
665             $aid = $a->id;
666             $bid = $b->id;
668             if ($aid == $bid) {
669                 return 0;
670             }
671             return ($aid < $bid) ? -1 : 1;
672         };
673         // Sort the results by id ascending to ensure the test is consistent
674         // and repeatable.
675         usort($typegroups, $idascfunc);
677         $this->assertCount(1, $typegroupcourses);
678         $this->assertEquals($course->id, $typegroupcourses[$course->id]->id);
679         $this->assertCount(2, $typegroups);
680         $this->assertEquals($group1->id, $typegroups[0]->id);
681         $this->assertEquals($group2->id, $typegroups[1]->id);
682     }
684     public function test_calendar_get_default_courses() {
685         global $USER, $CFG;
687         $this->resetAfterTest(true);
689         $generator = $this->getDataGenerator();
690         $user = $generator->create_user();
691         $course1 = $generator->create_course();
692         $course2 = $generator->create_course();
693         $course3 = $generator->create_course();
694         $context = context_course::instance($course1->id);
696         $this->setAdminUser();
697         $admin = clone $USER;
699         $teacher = $generator->create_user();
700         $generator->enrol_user($teacher->id, $course1->id, 'teacher');
701         $generator->enrol_user($admin->id, $course1->id, 'teacher');
703         $CFG->calendar_adminseesall = false;
705         $courses = calendar_get_default_courses();
706         // Only enrolled in one course.
707         $this->assertCount(1, $courses);
708         $courses = calendar_get_default_courses($course2->id);
709         // Enrolled course + current course.
710         $this->assertCount(2, $courses);
711         $CFG->calendar_adminseesall = true;
712         $courses = calendar_get_default_courses();
713         // All courses + SITE.
714         $this->assertCount(4, $courses);
715         $courses = calendar_get_default_courses($course2->id);
716         // All courses + SITE.
717         $this->assertCount(4, $courses);
719         $this->setUser($teacher);
721         $CFG->calendar_adminseesall = false;
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);
729         // This setting should not affect teachers.
730         $CFG->calendar_adminseesall = true;
731         $courses = calendar_get_default_courses();
732         // Only enrolled in one course.
733         $this->assertCount(1, $courses);
734         $courses = calendar_get_default_courses($course2->id);
735         // Enrolled course only (ignore current).
736         $this->assertCount(1, $courses);
738     }
740     /**
741      * Confirm that the skip events flag causes the calendar_get_view function
742      * to avoid querying for the calendar events.
743      */
744     public function test_calendar_get_view_skip_events() {
745         $this->resetAfterTest(true);
746         $this->setAdminUser();
748         $generator = $this->getDataGenerator();
749         $user = $generator->create_user();
750         $skipnavigation = true;
751         $skipevents = true;
752         $event = create_event([
753             'eventtype' => 'user',
754             'userid' => $user->id
755         ]);
757         $this->setUser($user);
758         $calendar = \calendar_information::create(time() - 10, SITEID, null);
760         list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
761         $this->assertEmpty($data->events);
763         $skipevents = false;
764         list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
766         $this->assertEquals($event->id, $data->events[0]->id);
767     }