MDL-37624 calendar: Added location support
[moodle.git] / calendar / tests / lib_test.php
CommitLineData
23a29de7
MN
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/>.
16
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 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once(__DIR__ . '/helpers.php');
28
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 */
36class core_calendar_lib_testcase extends advanced_testcase {
37
38 /**
39 * Tests set up
40 */
41 protected function setUp() {
42 $this->resetAfterTest();
43 }
44
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' => '',
ef4e04ee 64 'location' => 'Test',
23a29de7
MN
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' => '',
ef4e04ee 78 'location' => 'Test',
23a29de7
MN
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 }
109
23a29de7
MN
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();
115
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);
121
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);
128
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);
134
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 }
141
142 /**
143 * Test the update_subscription() function.
144 */
145 public function test_update_subscription() {
146 $this->resetAfterTest(true);
147
148 $subscription = new stdClass();
149 $subscription->eventtype = 'site';
150 $subscription->name = 'test';
151 $id = calendar_add_subscription($subscription);
152
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);
158
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);
166
167 $subscription = new stdClass();
168 $subscription->name = 'awesome4';
169 $this->expectException('coding_exception');
170 calendar_update_subscription($subscription);
171 }
172
173 public function test_add_subscription() {
174 global $DB, $CFG;
175
176 require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
177
178 $this->resetAfterTest(true);
179
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);
186
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());
191
192 $sub = calendar_get_subscription($id);
b9fd5164 193 calendar_import_icalendar_events($ical, null, $sub->id);
23a29de7
MN
194 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
195 $this->assertEquals($count, 1);
196
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);
203
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());
208
209 $sub = calendar_get_subscription($id);
b9fd5164 210 calendar_import_icalendar_events($ical, null, $sub->id);
23a29de7
MN
211 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
212 $this->assertEquals($count, 1);
213
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);
220
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());
225
226 $sub = calendar_get_subscription($id);
b9fd5164 227 calendar_import_icalendar_events($ical, null, $sub->id);
23a29de7
MN
228 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
229 $this->assertEquals($count, 1);
230 }
22753c8c
JP
231
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();
237
238 $course = $generator->create_course();
239
240 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
241 if (!isset($params['course'])) {
242 $params['course'] = $course->id;
243 }
244
245 $instance = $plugingenerator->create_instance($params);
246
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();
253
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');
260
261 // Create groups.
262 $group1 = $generator->create_group(['courseid' => $course->id]);
263 $group2 = $generator->create_group(['courseid' => $course->id]);
264
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();
271
272 // Events with the same module name, instance and event type.
273 $events = [
274 [
275 'name' => 'Assignment 1 due date',
276 'description' => '',
ef4e04ee 277 'location' => 'Test',
22753c8c
JP
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' => '',
ef4e04ee 291 'location' => 'Test',
22753c8c
JP
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' => '',
ef4e04ee 306 'location' => 'Test',
22753c8c
JP
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' => '',
ef4e04ee 321 'location' => 'Test',
22753c8c
JP
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 ];
335
336 foreach ($events as $event) {
337 calendar_event::create($event, false);
338 }
339
340 $timestart = $now - 100;
341 $timeend = $now + (3 * 86400);
342 $groups = [$group1->id, $group2->id];
343
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);
350
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);
354
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);
361
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);
a776415d 367 $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
22753c8c
JP
368
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);
375
376 // Add repeating events.
377 $repeatingevents = [
378 [
379 'name' => 'Repeating site event',
380 'description' => '',
ef4e04ee 381 'location' => 'Test',
22753c8c
JP
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' => '',
ef4e04ee 397 'location' => 'Test',
22753c8c
JP
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 ];
411
412 foreach ($repeatingevents as $event) {
413 calendar_event::create($event, false);
414 }
415
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 }
aa091225
RW
420
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();
427
428 $generator->role_assign($roleid, $user->id, $systemcontext->id);
429 $generator->role_assign($roleid, $user->id, $sitecontext->id);
430 $this->setUser($user);
431
432 assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $sitecontext, true);
433 assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $systemcontext, true);
434
435 $types = calendar_get_all_allowed_types();
436 $this->assertEmpty($types);
437 }
438
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();
444
445 $generator->role_assign($roleid, $user->id, $context->id);
446 $this->setUser($user);
447
448 assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true);
449
450 $types = calendar_get_all_allowed_types();
451 $this->assertTrue($types['user']);
452
453 assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
454
455 $types = calendar_get_all_allowed_types();
456 $this->assertArrayNotHasKey('user', $types);
457 }
458
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();
464
465 $generator->role_assign($roleid, $user->id, $context->id);
466 $this->setUser($user);
467
468 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
469
470 $types = calendar_get_all_allowed_types();
471 $this->assertTrue($types['site']);
472
473 assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
474
475 $types = calendar_get_all_allowed_types();
476 $this->assertArrayNotHasKey('site', $types);
477 }
478
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];
491
492 foreach ($enrolledcourses as $course) {
493 $generator->enrol_user($user->id, $course->id, 'student');
494 }
495
496 foreach ($contexts as $context) {
497 $generator->role_assign($roleid, $user->id, $context->id);
498 }
499
500 $this->setUser($user);
501
502 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
503 assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true);
504
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);
580a168a 510 $this->assertEquals($course1->id, $typecourses[$course1->id]->id);
aa091225
RW
511
512 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
513
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;
523
524 if ($aid == $bid) {
525 return 0;
526 }
527 return ($aid < $bid) ? -1 : 1;
528 });
529
530 $this->assertCount(2, $typecourses);
531 $this->assertEquals($course1->id, $typecourses[0]->id);
532 $this->assertEquals($course2->id, $typecourses[1]->id);
533 }
534
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();
541
542 $generator->enrol_user($user->id, $course->id, 'student');
543 $generator->role_assign($roleid, $user->id, $context->id);
544
545 $this->setUser($user);
546
547 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
548
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);
580a168a 554 $this->assertEquals($course->id, $typecourses[$course->id]->id);
aa091225
RW
555 $this->assertArrayNotHasKey('group', $types);
556 $this->assertArrayNotHasKey('groupcourses', $types);
557 }
558
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();
567
568 $generator->enrol_user($user->id, $course->id, 'student');
569 $generator->role_assign($roleid, $user->id, $context->id);
570
571 $this->setUser($user);
572
573 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
574 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
575
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);
580a168a 581 $this->assertEquals($course->id, $typecourses[$course->id]->id);
aa091225
RW
582 $this->assertArrayNotHasKey('group', $types);
583 $this->assertArrayNotHasKey('groupcourses', $types);
584 }
585
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();
596
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);
601
602 $this->setUser($user);
603
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);
608
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;
618
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);
628
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);
580a168a 633 $this->assertEquals($course1->id, $typegroupcourses[$course1->id]->id);
aa091225
RW
634 $this->assertCount(2, $typegroups);
635 $this->assertEquals($group1->id, $typegroups[0]->id);
636 $this->assertEquals($group2->id, $typegroups[1]->id);
637 }
638
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();
648
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));
653
654 $this->setUser($user);
655
656 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
657 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
658
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;
667
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);
676
677 $this->assertCount(1, $typegroupcourses);
580a168a 678 $this->assertEquals($course->id, $typegroupcourses[$course->id]->id);
aa091225
RW
679 $this->assertCount(2, $typegroups);
680 $this->assertEquals($group1->id, $typegroups[0]->id);
681 $this->assertEquals($group2->id, $typegroups[1]->id);
682 }
4dc645d6
DW
683
684 public function test_calendar_get_default_courses() {
685 global $USER, $CFG;
686
687 $this->resetAfterTest(true);
688
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);
695
696 $this->setAdminUser();
697 $admin = clone $USER;
698
699 $teacher = $generator->create_user();
700 $generator->enrol_user($teacher->id, $course1->id, 'teacher');
701 $generator->enrol_user($admin->id, $course1->id, 'teacher');
702
703 $CFG->calendar_adminseesall = false;
704
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);
718
719 $this->setUser($teacher);
720
721 $CFG->calendar_adminseesall = false;
722
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);
737
738 }
7884ecf2
RW
739
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();
747
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 ]);
756
757 $this->setUser($user);
758 $calendar = \calendar_information::create(time() - 10, SITEID, null);
759
760 list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
761 $this->assertEmpty($data->events);
762
763 $skipevents = false;
764 list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
765
766 $this->assertEquals($event->id, $data->events[0]->id);
767 }
aa091225 768}