MDL-58768 Calendar: Added user param to calendar_set_filters
[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 420
4dc645d6
DW
421 public function test_calendar_get_default_courses() {
422 global $USER, $CFG;
423
424 $this->resetAfterTest(true);
425
426 $generator = $this->getDataGenerator();
427 $user = $generator->create_user();
428 $course1 = $generator->create_course();
429 $course2 = $generator->create_course();
430 $course3 = $generator->create_course();
431 $context = context_course::instance($course1->id);
432
433 $this->setAdminUser();
434 $admin = clone $USER;
435
436 $teacher = $generator->create_user();
437 $generator->enrol_user($teacher->id, $course1->id, 'teacher');
438 $generator->enrol_user($admin->id, $course1->id, 'teacher');
439
440 $CFG->calendar_adminseesall = false;
441
442 $courses = calendar_get_default_courses();
443 // Only enrolled in one course.
444 $this->assertCount(1, $courses);
445 $courses = calendar_get_default_courses($course2->id);
446 // Enrolled course + current course.
447 $this->assertCount(2, $courses);
448 $CFG->calendar_adminseesall = true;
449 $courses = calendar_get_default_courses();
450 // All courses + SITE.
451 $this->assertCount(4, $courses);
452 $courses = calendar_get_default_courses($course2->id);
453 // All courses + SITE.
454 $this->assertCount(4, $courses);
455
456 $this->setUser($teacher);
457
458 $CFG->calendar_adminseesall = false;
459
460 $courses = calendar_get_default_courses();
461 // Only enrolled in one course.
462 $this->assertCount(1, $courses);
463 $courses = calendar_get_default_courses($course2->id);
464 // Enrolled course only (ignore current).
465 $this->assertCount(1, $courses);
466 // This setting should not affect teachers.
467 $CFG->calendar_adminseesall = true;
468 $courses = calendar_get_default_courses();
469 // Only enrolled in one course.
470 $this->assertCount(1, $courses);
471 $courses = calendar_get_default_courses($course2->id);
472 // Enrolled course only (ignore current).
473 $this->assertCount(1, $courses);
474
bd870573
SR
475 // Now, log out and test again.
476 $this->setUser();
477
478 $CFG->calendar_adminseesall = false;
479
480 $courses = calendar_get_default_courses(null, '*', false, $teacher->id);
481 // Only enrolled in one course.
482 $this->assertCount(1, $courses);
483 $courses = calendar_get_default_courses($course2->id, '*', false, $teacher->id);
484 // Enrolled course only (ignore current).
485 $this->assertCount(1, $courses);
486 // This setting should not affect teachers.
487 $CFG->calendar_adminseesall = true;
488 $courses = calendar_get_default_courses(null, '*', false, $teacher->id);
489 // Only enrolled in one course.
490 $this->assertCount(1, $courses);
491 $courses = calendar_get_default_courses($course2->id, '*', false, $teacher->id);
492 // Enrolled course only (ignore current).
493 $this->assertCount(1, $courses);
494
4dc645d6 495 }
7884ecf2
RW
496
497 /**
498 * Confirm that the skip events flag causes the calendar_get_view function
499 * to avoid querying for the calendar events.
500 */
501 public function test_calendar_get_view_skip_events() {
502 $this->resetAfterTest(true);
503 $this->setAdminUser();
504
505 $generator = $this->getDataGenerator();
506 $user = $generator->create_user();
507 $skipnavigation = true;
508 $skipevents = true;
509 $event = create_event([
510 'eventtype' => 'user',
511 'userid' => $user->id
512 ]);
513
514 $this->setUser($user);
515 $calendar = \calendar_information::create(time() - 10, SITEID, null);
516
517 list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
518 $this->assertEmpty($data->events);
519
520 $skipevents = false;
521 list($data, $template) = calendar_get_view($calendar, 'day', $skipnavigation, $skipevents);
522
523 $this->assertEquals($event->id, $data->events[0]->id);
524 }
633c5ffb 525
633c5ffb
SL
526 public function test_calendar_get_allowed_event_types_course() {
527 $generator = $this->getDataGenerator();
528 $user = $generator->create_user();
529 $course1 = $generator->create_course(); // Has capability.
530 $course2 = $generator->create_course(); // Doesn't have capability.
531 $course3 = $generator->create_course(); // Not enrolled.
532 $context1 = context_course::instance($course1->id);
533 $context2 = context_course::instance($course2->id);
534 $context3 = context_course::instance($course3->id);
535 $roleid = $generator->create_role();
536 $contexts = [$context1, $context2, $context3];
537 $enrolledcourses = [$course1, $course2];
375b15a3 538
633c5ffb
SL
539 foreach ($enrolledcourses as $course) {
540 $generator->enrol_user($user->id, $course->id, 'student');
541 }
375b15a3 542
633c5ffb
SL
543 foreach ($contexts as $context) {
544 $generator->role_assign($roleid, $user->id, $context->id);
545 }
375b15a3 546
633c5ffb 547 $this->setUser($user);
375b15a3 548
633c5ffb
SL
549 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
550 assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context2, true);
375b15a3 551
633c5ffb
SL
552 // The user only has the correct capability in course 1 so that is the only
553 // one that should be in the results.
554 $types = calendar_get_allowed_event_types($course1->id);
555 $this->assertTrue($types['course']);
375b15a3 556
633c5ffb 557 assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context1, true);
375b15a3 558
633c5ffb
SL
559 // The user only now has the correct capability in both course 1 and 2 so we
560 // expect both to be in the results.
375b15a3 561 $types = calendar_get_allowed_event_types($course3->id);
633c5ffb
SL
562 $this->assertFalse($types['course']);
563 }
375b15a3
SL
564
565 public function test_calendar_get_allowed_event_types_group_no_acces_to_diff_groups() {
633c5ffb
SL
566 $generator = $this->getDataGenerator();
567 $user = $generator->create_user();
568 $course = $generator->create_course();
569 $context = context_course::instance($course->id);
570 $roleid = $generator->create_role();
375b15a3 571
633c5ffb
SL
572 $generator->enrol_user($user->id, $course->id, 'student');
573 $generator->role_assign($roleid, $user->id, $context->id);
375b15a3 574
633c5ffb 575 $this->setUser($user);
375b15a3 576
633c5ffb 577 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
375b15a3
SL
578 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
579
580 // The user has the correct capability in the course but they aren't a member
581 // of any of the groups and don't have the accessallgroups capability.
633c5ffb
SL
582 $types = calendar_get_allowed_event_types($course->id);
583 $this->assertTrue($types['course']);
375b15a3 584 $this->assertFalse($types['group']);
633c5ffb 585 }
375b15a3
SL
586
587 public function test_calendar_get_allowed_event_types_group_no_groups() {
633c5ffb
SL
588 $generator = $this->getDataGenerator();
589 $user = $generator->create_user();
590 $course = $generator->create_course();
591 $context = context_course::instance($course->id);
592 $roleid = $generator->create_role();
593 $generator->enrol_user($user->id, $course->id, 'student');
594 $generator->role_assign($roleid, $user->id, $context->id);
595 $this->setUser($user);
596 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
375b15a3
SL
597 // The user has the correct capability in the course but there are
598 // no groups so we shouldn't see a group type.
633c5ffb
SL
599 $types = calendar_get_allowed_event_types($course->id);
600 $this->assertTrue($types['course']);
633c5ffb 601 }
375b15a3 602
633c5ffb
SL
603 public function test_calendar_get_allowed_event_types_group_access_all_groups() {
604 $generator = $this->getDataGenerator();
605 $user = $generator->create_user();
606 $course1 = $generator->create_course();
607 $course2 = $generator->create_course();
608 $generator->create_group(array('courseid' => $course1->id));
609 $generator->create_group(array('courseid' => $course2->id));
610 $context1 = context_course::instance($course1->id);
611 $context2 = context_course::instance($course2->id);
612 $roleid = $generator->create_role();
613 $generator->enrol_user($user->id, $course1->id, 'student');
614 $generator->enrol_user($user->id, $course2->id, 'student');
615 $generator->role_assign($roleid, $user->id, $context1->id);
616 $generator->role_assign($roleid, $user->id, $context2->id);
617 $this->setUser($user);
618 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context1, true);
619 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context2, true);
620 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context1, true);
621 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context2, true);
622 // The user has the correct capability in the course and has
623 // the accessallgroups capability.
624 $types = calendar_get_allowed_event_types($course1->id);
625 $this->assertTrue($types['group']);
626 }
627 public function test_calendar_get_allowed_event_types_group_no_access_all_groups() {
628 $generator = $this->getDataGenerator();
629 $user = $generator->create_user();
630 $course = $generator->create_course();
631 $context = context_course::instance($course->id);
632 $group1 = $generator->create_group(array('courseid' => $course->id));
633 $group2 = $generator->create_group(array('courseid' => $course->id));
634 $roleid = $generator->create_role();
635 $generator->enrol_user($user->id, $course->id, 'student');
636 $generator->role_assign($roleid, $user->id, $context->id);
637 $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user->id));
638 $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user->id));
639 $this->setUser($user);
640 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $roleid, $context, true);
641 // The user has the correct capability in the course but can't access
642 // groups that they are not a member of.
643 $types = calendar_get_allowed_event_types($course->id);
644 $this->assertFalse($types['group']);
645 assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true);
646 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, $context, true);
647 $types = calendar_get_allowed_event_types($course->id);
648 $this->assertTrue($types['group']);
649 }
d8c6c21c
SR
650
651 /**
652 * This is a setup helper function that create some users, courses, groups and group memberships.
653 * This is useful to prepare the environment for testing the calendar_set_filters function.
654 *
655 * @return array An array of ($users, $courses, $coursegroups)
656 */
657 protected function setup_test_calendar_set_filters() {
658 $generator = $this->getDataGenerator();
659
660 // Create some users.
661 $users = [];
662 $users[] = $generator->create_user();
663 $users[] = $generator->create_user();
664 $users[] = $generator->create_user();
665
666 // Create some courses.
667 $courses = [];
668 $courses[] = $generator->create_course();
669 $courses[] = $generator->create_course();
670 $courses[] = $generator->create_course();
671 $courses[] = $generator->create_course();
672
673 // Create some groups.
674 $coursegroups = [];
675 $coursegroups[$courses[0]->id] = [];
676 $coursegroups[$courses[0]->id][] = $generator->create_group(['courseid' => $courses[0]->id]);
677 $coursegroups[$courses[0]->id][] = $generator->create_group(['courseid' => $courses[0]->id]);
678 $coursegroups[$courses[2]->id] = [];
679 $coursegroups[$courses[2]->id][] = $generator->create_group(['courseid' => $courses[2]->id]);
680 $coursegroups[$courses[2]->id][] = $generator->create_group(['courseid' => $courses[2]->id]);
681 $coursegroups[$courses[3]->id] = [];
682 $coursegroups[$courses[3]->id][] = $generator->create_group(['courseid' => $courses[3]->id]);
683 $coursegroups[$courses[3]->id][] = $generator->create_group(['courseid' => $courses[3]->id]);
684
685 // Create some enrolments and group memberships.
686 $generator->enrol_user($users[0]->id, $courses[0]->id, 'student');
687 $generator->create_group_member(['groupid' => $coursegroups[$courses[0]->id][0]->id, 'userid' => $users[0]->id]);
688 $generator->enrol_user($users[1]->id, $courses[0]->id, 'student');
689 $generator->create_group_member(['groupid' => $coursegroups[$courses[0]->id][1]->id, 'userid' => $users[1]->id]);
690 $generator->enrol_user($users[0]->id, $courses[1]->id, 'student');
691 $generator->enrol_user($users[0]->id, $courses[2]->id, 'student');
692
693 return array($users, $courses, $coursegroups);
694 }
695
696 /**
697 * This function tests calendar_set_filters for the case when user is not logged in.
698 */
699 public function test_calendar_set_filters_not_logged_in() {
700 $this->resetAfterTest();
701
702 list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
703
704 $defaultcourses = calendar_get_default_courses(null, '*', false, $users[0]->id);
705 list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses);
706
707 $this->assertEquals(
708 [$courses[0]->id, $courses[1]->id, $courses[2]->id, SITEID],
709 array_values($courseids),
710 '', 0.0, 10, true);
711 $this->assertFalse($groupids);
712 $this->assertFalse($userid);
713 }
714
715 /**
716 * This function tests calendar_set_filters for the case when no one is logged in, but a user id is provided.
717 */
718 public function test_calendar_set_filters_not_logged_in_with_user() {
719 $this->resetAfterTest();
720
721 list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
722
723 $defaultcourses = calendar_get_default_courses(null, '*', false, $users[1]->id);
724 list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false, $users[1]);
725
726 $this->assertEquals(array($courses[0]->id, SITEID), array_values($courseids));
727 $this->assertEquals(array($coursegroups[$courses[0]->id][1]->id), $groupids);
728 $this->assertEquals($users[1]->id, $userid);
729
730 $defaultcourses = calendar_get_default_courses(null, '*', false, $users[0]->id);
731 list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false, $users[0]);
732
733 $this->assertEquals(
734 [$courses[0]->id, $courses[1]->id, $courses[2]->id, SITEID],
735 array_values($courseids),
736 '', 0.0, 10, true);
737 $this->assertEquals(array($coursegroups[$courses[0]->id][0]->id), $groupids);
738 $this->assertEquals($users[0]->id, $userid);
739
740 }
741
742 /**
743 * This function tests calendar_set_filters for the case when user is logged in, but no user id is provided.
744 */
745 public function test_calendar_set_filters_logged_in_no_user() {
746 $this->resetAfterTest();
747
748 list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
749
750 $this->setUser($users[0]);
751 $defaultcourses = calendar_get_default_courses(null, '*', false, $users[0]->id);
752 list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false);
753 $this->assertEquals([$courses[0]->id, $courses[1]->id, $courses[2]->id, SITEID], array_values($courseids), '', 0.0, 10,
754 true);
755 $this->assertEquals(array($coursegroups[$courses[0]->id][0]->id), $groupids);
756 $this->assertEquals($users[0]->id, $userid);
757 }
758
759 /**
760 * This function tests calendar_set_filters for the case when a user is logged in, but another user id is provided.
761 */
762 public function test_calendar_set_filters_logged_in_another_user() {
763 $this->resetAfterTest();
764
765 list($users, $courses, $coursegroups) = $this->setup_test_calendar_set_filters();
766
767 $this->setUser($users[0]);
768 $defaultcourses = calendar_get_default_courses(null, '*', false, $users[1]->id);
769 list($courseids, $groupids, $userid) = calendar_set_filters($defaultcourses, false, $users[1]);
770
771 $this->assertEquals(array($courses[0]->id, SITEID), array_values($courseids));
772 $this->assertEquals(array($coursegroups[$courses[0]->id][1]->id), $groupids);
773 $this->assertEquals($users[1]->id, $userid);
774 }
aa091225 775}