2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Full functional accesslib test.
22 * @copyright 2011 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
30 * Functional test for accesslib.php
32 * Note: execution may take many minutes especially on slower servers.
34 class core_accesslib_testcase extends advanced_testcase {
36 * Verify comparison of context instances in phpunit asserts.
38 public function test_context_comparisons() {
39 $frontpagecontext1 = context_course::instance(SITEID);
40 context_helper::reset_caches();
41 $frontpagecontext2 = context_course::instance(SITEID);
42 $this->assertEquals($frontpagecontext1, $frontpagecontext2);
44 $user1 = context_user::instance(1);
45 $user2 = context_user::instance(2);
46 $this->assertNotEquals($user1, $user2);
50 * Test resetting works.
52 public function test_accesslib_clear_all_caches() {
53 global $ACCESSLIB_PRIVATE;
55 $this->resetAfterTest();
57 $this->setAdminUser();
58 load_all_capabilities();
60 $this->assertNotEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
61 accesslib_clear_all_caches_for_unit_testing();
62 $this->assertEmpty($ACCESSLIB_PRIVATE->dirtycontexts);
63 $this->assertEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
67 * Check modifying capability record is not exposed to other code.
69 public function test_capabilities_mutation() {
70 $oldcap = get_capability_info('moodle/site:config');
71 $cap = get_capability_info('moodle/site:config');
73 $newcap = get_capability_info('moodle/site:config');
75 $this->assertFalse(isset($cap->name));
76 $this->assertTrue(isset($newcap->name));
77 $this->assertTrue(isset($oldcap->name));
81 * Test getting of role access
83 public function test_get_role_access() {
86 $roles = $DB->get_records('role');
87 foreach ($roles as $role) {
88 $access = get_role_access($role->id);
90 $this->assertTrue(is_array($access));
91 $this->assertTrue(is_array($access['ra']));
92 $this->assertFalse(isset($access['rdef']));
93 $this->assertFalse(isset($access['rdef_count']));
94 $this->assertFalse(isset($access['loaded']));
95 $this->assertTrue(isset($access['time']));
96 $this->assertTrue(is_array($access['rsw']));
99 // Note: the data is validated in the functional permission evaluation test at the end of this testcase.
103 * Test getting of guest role.
105 public function test_get_guest_role() {
108 $guest = get_guest_role();
109 $this->assertEquals('guest', $guest->archetype);
110 $this->assertEquals('guest', $guest->shortname);
112 $this->assertEquals($CFG->guestroleid, $guest->id);
116 * Test if user is admin.
118 public function test_is_siteadmin() {
121 $this->resetAfterTest();
123 $users = $DB->get_records('user');
125 foreach ($users as $user) {
127 if ($user->username === 'admin') {
128 $this->assertTrue(is_siteadmin($user));
129 $this->assertTrue(is_siteadmin($user->id));
130 $this->setUser($user);
131 $this->assertTrue(is_siteadmin());
132 $this->assertTrue(is_siteadmin(null));
134 $this->assertFalse(is_siteadmin($user));
135 $this->assertFalse(is_siteadmin($user->id));
136 $this->setUser($user);
137 $this->assertFalse(is_siteadmin());
138 $this->assertFalse(is_siteadmin(null));
142 // Change the site admin list and check that it still works with
143 // multiple admins. We do this with userids only (not real user
144 // accounts) because it makes the test simpler.
145 $before = $CFG->siteadmins;
146 set_config('siteadmins', '666,667,668');
147 $this->assertTrue(is_siteadmin(666));
148 $this->assertTrue(is_siteadmin(667));
149 $this->assertTrue(is_siteadmin(668));
150 $this->assertFalse(is_siteadmin(669));
151 set_config('siteadmins', '13');
152 $this->assertTrue(is_siteadmin(13));
153 $this->assertFalse(is_siteadmin(666));
154 set_config('siteadmins', $before);
158 * Test if user is enrolled in a course
160 public function test_is_enrolled() {
163 $this->resetAfterTest();
166 $user = $this->getDataGenerator()->create_user();
167 $course = $this->getDataGenerator()->create_course();
168 $coursecontext = context_course::instance($course->id);
169 $role = $DB->get_record('role', array('shortname'=>'student'));
171 // There should be a manual enrolment as part of the default install.
172 $plugin = enrol_get_plugin('manual');
173 $instance = $DB->get_record('enrol', array(
174 'courseid' => $course->id,
177 $this->assertNotSame(false, $instance);
179 // Enrol the user in the course.
180 $plugin->enrol_user($instance, $user->id, $role->id);
182 // We'll test with the mod/assign:submit capability.
183 $capability= 'mod/assign:submit';
184 $this->assertTrue($DB->record_exists('capabilities', array('name' => $capability)));
186 // Switch to our user.
187 $this->setUser($user);
189 // Ensure that the user has the capability first.
190 $this->assertTrue(has_capability($capability, $coursecontext, $user->id));
192 // We first test whether the user is enrolled on the course as this
193 // seeds the cache, then we test for the capability.
194 $this->assertTrue(is_enrolled($coursecontext, $user, '', true));
195 $this->assertTrue(is_enrolled($coursecontext, $user, $capability));
197 // Prevent the capability for this user role.
198 assign_capability($capability, CAP_PROHIBIT, $role->id, $coursecontext);
199 $this->assertFalse(has_capability($capability, $coursecontext, $user->id));
201 // Again, we seed the cache first by checking initial enrolment,
202 // and then we test the actual capability.
203 $this->assertTrue(is_enrolled($coursecontext, $user, '', true));
204 $this->assertFalse(is_enrolled($coursecontext, $user, $capability));
208 * Test logged in test.
210 public function test_isloggedin() {
213 $this->resetAfterTest();
216 $this->assertFalse(isloggedin());
218 $this->assertTrue(isloggedin());
222 * Test guest user test.
224 public function test_isguestuser() {
227 $this->resetAfterTest();
229 $guest = $DB->get_record('user', array('username'=>'guest'));
231 $this->assertFalse(isguestuser());
232 $this->setAdminUser();
233 $this->assertFalse(isguestuser());
234 $this->assertTrue(isguestuser($guest));
235 $this->assertTrue(isguestuser($guest->id));
236 $this->setUser($guest);
237 $this->assertTrue(isguestuser());
239 $users = $DB->get_records('user');
240 foreach ($users as $user) {
241 if ($user->username === 'guest') {
244 $this->assertFalse(isguestuser($user));
249 * Test capability riskiness.
251 public function test_is_safe_capability() {
253 // Note: there is not much to test, just make sure no notices are throw for the most dangerous cap.
254 $capability = $DB->get_record('capabilities', array('name'=>'moodle/site:config'), '*', MUST_EXIST);
255 $this->assertFalse(is_safe_capability($capability));
259 * Test context fetching.
261 public function test_get_context_info_array() {
262 $this->resetAfterTest();
264 $syscontext = context_system::instance();
265 $user = $this->getDataGenerator()->create_user();
266 $usercontext = context_user::instance($user->id);
267 $course = $this->getDataGenerator()->create_course();
268 $catcontext = context_coursecat::instance($course->category);
269 $coursecontext = context_course::instance($course->id);
270 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
271 $modcontext = context_module::instance($page->cmid);
272 $cm = get_coursemodule_from_instance('page', $page->id);
273 $block1 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid'=>$coursecontext->id));
274 $block1context = context_block::instance($block1->id);
275 $block2 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid'=>$modcontext->id));
276 $block2context = context_block::instance($block2->id);
278 $result = get_context_info_array($syscontext->id);
279 $this->assertCount(3, $result);
280 $this->assertEquals($syscontext, $result[0]);
281 $this->assertNull($result[1]);
282 $this->assertNull($result[2]);
284 $result = get_context_info_array($usercontext->id);
285 $this->assertCount(3, $result);
286 $this->assertEquals($usercontext, $result[0]);
287 $this->assertNull($result[1]);
288 $this->assertNull($result[2]);
290 $result = get_context_info_array($catcontext->id);
291 $this->assertCount(3, $result);
292 $this->assertEquals($catcontext, $result[0]);
293 $this->assertNull($result[1]);
294 $this->assertNull($result[2]);
296 $result = get_context_info_array($coursecontext->id);
297 $this->assertCount(3, $result);
298 $this->assertEquals($coursecontext, $result[0]);
299 $this->assertEquals($course->id, $result[1]->id);
300 $this->assertSame($course->shortname, $result[1]->shortname);
301 $this->assertNull($result[2]);
303 $result = get_context_info_array($block1context->id);
304 $this->assertCount(3, $result);
305 $this->assertEquals($block1context, $result[0]);
306 $this->assertEquals($course->id, $result[1]->id);
307 $this->assertEquals($course->shortname, $result[1]->shortname);
308 $this->assertNull($result[2]);
310 $result = get_context_info_array($modcontext->id);
311 $this->assertCount(3, $result);
312 $this->assertEquals($modcontext, $result[0]);
313 $this->assertEquals($course->id, $result[1]->id);
314 $this->assertSame($course->shortname, $result[1]->shortname);
315 $this->assertEquals($cm->id, $result[2]->id);
317 $result = get_context_info_array($block2context->id);
318 $this->assertCount(3, $result);
319 $this->assertEquals($block2context, $result[0]);
320 $this->assertEquals($course->id, $result[1]->id);
321 $this->assertSame($course->shortname, $result[1]->shortname);
322 $this->assertEquals($cm->id, $result[2]->id);
326 * Test looking for course contacts.
328 public function test_has_coursecontact_role() {
331 $this->resetAfterTest();
333 $users = $DB->get_records('user');
335 // Nobody is expected to have any course level roles.
336 $this->assertNotEmpty($CFG->coursecontact);
337 foreach ($users as $user) {
338 $this->assertFalse(has_coursecontact_role($user->id));
341 $user = $this->getDataGenerator()->create_user();
342 $course = $this->getDataGenerator()->create_course();
343 role_assign($CFG->coursecontact, $user->id, context_course::instance($course->id));
344 $this->assertTrue(has_coursecontact_role($user->id));
348 * Test creation of roles.
350 public function test_create_role() {
353 $this->resetAfterTest();
355 $id = create_role('New student role', 'student2', 'New student description', 'student');
356 $role = $DB->get_record('role', array('id'=>$id));
358 $this->assertNotEmpty($role);
359 $this->assertSame('New student role', $role->name);
360 $this->assertSame('student2', $role->shortname);
361 $this->assertSame('New student description', $role->description);
362 $this->assertSame('student', $role->archetype);
366 * Test adding of capabilities to roles.
368 public function test_assign_capability() {
371 $this->resetAfterTest();
373 $user = $this->getDataGenerator()->create_user();
374 $syscontext = context_system::instance();
375 $frontcontext = context_course::instance(SITEID);
376 $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
377 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability assigned to student by default.
378 $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')));
379 $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')));
381 $this->setUser($user);
382 $result = assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $student->id, $frontcontext->id);
383 $this->assertTrue($result);
384 $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
385 $this->assertNotEmpty($permission);
386 $this->assertEquals(CAP_ALLOW, $permission->permission);
387 $this->assertEquals($user->id, $permission->modifierid);
390 $result = assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $student->id, $frontcontext->id, false);
391 $this->assertTrue($result);
392 $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
393 $this->assertNotEmpty($permission);
394 $this->assertEquals(CAP_ALLOW, $permission->permission);
395 $this->assertEquals($user->id, $permission->modifierid);
397 $result = assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $student->id, $frontcontext->id, true);
398 $this->assertTrue($result);
399 $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
400 $this->assertNotEmpty($permission);
401 $this->assertEquals(CAP_PROHIBIT, $permission->permission);
402 $this->assertEquals(0, $permission->modifierid);
404 $result = assign_capability('moodle/backup:backupcourse', CAP_INHERIT, $student->id, $frontcontext->id);
405 $this->assertTrue($result);
406 $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
407 $this->assertEmpty($permission);
409 // Test event trigger.
410 $rolecapabilityevent = \core\event\role_capabilities_updated::create(array('context' => $syscontext,
411 'objectid' => $student->id,
412 'other' => array('name' => $student->shortname)
414 $expectedlegacylog = array(SITEID, 'role', 'view', 'admin/roles/define.php?action=view&roleid=' . $student->id,
415 $student->shortname, '', $user->id);
416 $rolecapabilityevent->set_legacy_logdata($expectedlegacylog);
417 $rolecapabilityevent->add_record_snapshot('role', $student);
419 $sink = $this->redirectEvents();
420 $rolecapabilityevent->trigger();
421 $events = $sink->get_events();
423 $event = array_pop($events);
425 $this->assertInstanceOf('\core\event\role_capabilities_updated', $event);
426 $expectedurl = new moodle_url('/admin/roles/define.php', array('action' => 'view', 'roleid' => $student->id));
427 $this->assertEquals($expectedurl, $event->get_url());
428 $this->assertEventLegacyLogData($expectedlegacylog, $event);
429 $this->assertEventContextNotUsed($event);
433 * Test removing of capabilities from roles.
435 public function test_unassign_capability() {
438 $this->resetAfterTest();
440 $syscontext = context_system::instance();
441 $frontcontext = context_course::instance(SITEID);
442 $manager = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
443 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability assigned to manager by default.
444 assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $frontcontext->id);
446 $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
447 $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
449 $result = unassign_capability('moodle/backup:backupcourse', $manager->id, $syscontext->id);
450 $this->assertTrue($result);
451 $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
452 $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
453 unassign_capability('moodle/backup:backupcourse', $manager->id, $frontcontext);
454 $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
456 assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $syscontext->id);
457 assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $frontcontext->id);
458 $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
460 $result = unassign_capability('moodle/backup:backupcourse', $manager->id);
461 $this->assertTrue($result);
462 $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
463 $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
467 * Test role assigning.
469 public function test_role_assign() {
472 $this->resetAfterTest();
474 $user = $this->getDataGenerator()->create_user();
475 $course = $this->getDataGenerator()->create_course();
476 $role = $DB->get_record('role', array('shortname'=>'student'));
479 $context = context_system::instance();
480 $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
481 role_assign($role->id, $user->id, $context->id);
482 $ras = $DB->get_record('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id));
483 $this->assertNotEmpty($ras);
484 $this->assertSame('', $ras->component);
485 $this->assertSame('0', $ras->itemid);
486 $this->assertEquals($USER->id, $ras->modifierid);
488 $this->setAdminUser();
489 $context = context_course::instance($course->id);
490 $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
491 role_assign($role->id, $user->id, $context->id, 'enrol_self', 1, 666);
492 $ras = $DB->get_record('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id));
493 $this->assertNotEmpty($ras);
494 $this->assertSame('enrol_self', $ras->component);
495 $this->assertSame('1', $ras->itemid);
496 $this->assertEquals($USER->id, $ras->modifierid);
497 $this->assertEquals(666, $ras->timemodified);
499 // Test event triggered.
501 $user2 = $this->getDataGenerator()->create_user();
502 $sink = $this->redirectEvents();
503 $raid = role_assign($role->id, $user2->id, $context->id);
504 $events = $sink->get_events();
506 $this->assertCount(1, $events);
508 $this->assertInstanceOf('\core\event\role_assigned', $event);
509 $this->assertSame('role', $event->target);
510 $this->assertSame('role', $event->objecttable);
511 $this->assertEquals($role->id, $event->objectid);
512 $this->assertEquals($context->id, $event->contextid);
513 $this->assertEquals($user2->id, $event->relateduserid);
514 $this->assertCount(3, $event->other);
515 $this->assertEquals($raid, $event->other['id']);
516 $this->assertSame('', $event->other['component']);
517 $this->assertEquals(0, $event->other['itemid']);
518 $this->assertInstanceOf('moodle_url', $event->get_url());
519 $this->assertSame('role_assigned', $event::get_legacy_eventname());
520 $roles = get_all_roles();
521 $rolenames = role_fix_names($roles, $context, ROLENAME_ORIGINAL, true);
522 $expectedlegacylog = array($course->id, 'role', 'assign',
523 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$role->id, $rolenames[$role->id], '', $USER->id);
524 $this->assertEventLegacyLogData($expectedlegacylog, $event);
528 * Test role unassigning.
530 public function test_role_unassign() {
533 $this->resetAfterTest();
535 $user = $this->getDataGenerator()->create_user();
536 $course = $this->getDataGenerator()->create_course();
537 $role = $DB->get_record('role', array('shortname'=>'student'));
539 $context = context_course::instance($course->id);
540 role_assign($role->id, $user->id, $context->id);
541 $this->assertTrue($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
542 role_unassign($role->id, $user->id, $context->id);
543 $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
545 role_assign($role->id, $user->id, $context->id, 'enrol_self', 1);
546 $this->assertTrue($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
547 role_unassign($role->id, $user->id, $context->id, 'enrol_self', 1);
548 $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
550 // Test event triggered.
552 role_assign($role->id, $user->id, $context->id);
553 $sink = $this->redirectEvents();
554 role_unassign($role->id, $user->id, $context->id);
555 $events = $sink->get_events();
557 $this->assertCount(1, $events);
559 $this->assertInstanceOf('\core\event\role_unassigned', $event);
560 $this->assertSame('role', $event->target);
561 $this->assertSame('role', $event->objecttable);
562 $this->assertEquals($role->id, $event->objectid);
563 $this->assertEquals($context->id, $event->contextid);
564 $this->assertEquals($user->id, $event->relateduserid);
565 $this->assertCount(3, $event->other);
566 $this->assertSame('', $event->other['component']);
567 $this->assertEquals(0, $event->other['itemid']);
568 $this->assertInstanceOf('moodle_url', $event->get_url());
569 $roles = get_all_roles();
570 $rolenames = role_fix_names($roles, $context, ROLENAME_ORIGINAL, true);
571 $expectedlegacylog = array($course->id, 'role', 'unassign',
572 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$role->id, $rolenames[$role->id], '', $USER->id);
573 $this->assertEventLegacyLogData($expectedlegacylog, $event);
577 * Test role unassigning.
579 public function test_role_unassign_all() {
582 $this->resetAfterTest();
584 $user = $this->getDataGenerator()->create_user();
585 $course = $this->getDataGenerator()->create_course();
586 $role = $DB->get_record('role', array('shortname'=>'student'));
587 $role2 = $DB->get_record('role', array('shortname'=>'teacher'));
588 $syscontext = context_system::instance();
589 $coursecontext = context_course::instance($course->id);
590 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
591 $modcontext = context_module::instance($page->cmid);
593 role_assign($role->id, $user->id, $syscontext->id);
594 role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
595 $this->assertEquals(2, $DB->count_records('role_assignments', array('userid'=>$user->id)));
596 role_unassign_all(array('userid'=>$user->id, 'roleid'=>$role->id));
597 $this->assertEquals(0, $DB->count_records('role_assignments', array('userid'=>$user->id)));
599 role_assign($role->id, $user->id, $syscontext->id);
600 role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
601 role_assign($role->id, $user->id, $modcontext->id);
602 $this->assertEquals(3, $DB->count_records('role_assignments', array('userid'=>$user->id)));
603 role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id), false);
604 $this->assertEquals(2, $DB->count_records('role_assignments', array('userid'=>$user->id)));
605 role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id), true);
606 $this->assertEquals(1, $DB->count_records('role_assignments', array('userid'=>$user->id)));
607 role_unassign_all(array('userid'=>$user->id));
608 $this->assertEquals(0, $DB->count_records('role_assignments', array('userid'=>$user->id)));
610 role_assign($role->id, $user->id, $syscontext->id);
611 role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
612 role_assign($role->id, $user->id, $coursecontext->id);
613 role_assign($role->id, $user->id, $modcontext->id);
614 $this->assertEquals(4, $DB->count_records('role_assignments', array('userid'=>$user->id)));
615 role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id, 'component'=>'enrol_self'), true, true);
616 $this->assertEquals(1, $DB->count_records('role_assignments', array('userid'=>$user->id)));
618 // Test events triggered.
620 role_assign($role2->id, $user->id, $coursecontext->id);
621 role_assign($role2->id, $user->id, $modcontext->id);
622 $sink = $this->redirectEvents();
623 role_unassign_all(array('userid'=>$user->id, 'roleid'=>$role2->id));
624 $events = $sink->get_events();
626 $this->assertCount(2, $events);
627 $this->assertInstanceOf('\core\event\role_unassigned', $events[0]);
628 $this->assertInstanceOf('\core\event\role_unassigned', $events[1]);
634 public function test_get_roles_with_capability() {
637 $this->resetAfterTest();
639 $syscontext = context_system::instance();
640 $frontcontext = context_course::instance(SITEID);
641 $manager = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
642 $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
644 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability is ok.
645 $DB->delete_records('role_capabilities', array('capability'=>'moodle/backup:backupcourse'));
647 $roles = get_roles_with_capability('moodle/backup:backupcourse');
648 $this->assertEquals(array(), $roles);
650 assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $syscontext->id);
651 assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $manager->id, $frontcontext->id);
652 assign_capability('moodle/backup:backupcourse', CAP_PREVENT, $teacher->id, $frontcontext->id);
654 $roles = get_roles_with_capability('moodle/backup:backupcourse');
655 $this->assertEquals(array($teacher->id, $manager->id), array_keys($roles), '', 0, 10, true);
657 $roles = get_roles_with_capability('moodle/backup:backupcourse', CAP_ALLOW);
658 $this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
660 $roles = get_roles_with_capability('moodle/backup:backupcourse', null, $syscontext);
661 $this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
665 * Test deleting of roles.
667 public function test_delete_role() {
670 $this->resetAfterTest();
672 $role = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
673 $user = $this->getDataGenerator()->create_user();
674 role_assign($role->id, $user->id, context_system::instance());
675 $course = $this->getDataGenerator()->create_course();
676 $rolename = (object)array('roleid'=>$role->id, 'name'=>'Man', 'contextid'=>context_course::instance($course->id)->id);
677 $DB->insert_record('role_names', $rolename);
679 $this->assertTrue($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
680 $this->assertTrue($DB->record_exists('role_capabilities', array('roleid'=>$role->id)));
681 $this->assertTrue($DB->record_exists('role_names', array('roleid'=>$role->id)));
682 $this->assertTrue($DB->record_exists('role_context_levels', array('roleid'=>$role->id)));
683 $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$role->id)));
684 $this->assertTrue($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
685 $this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
686 $this->assertTrue($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
688 // Delete role and get event.
689 $sink = $this->redirectEvents();
690 $result = delete_role($role->id);
691 $events = $sink->get_events();
693 $event = array_pop($events);
695 $this->assertTrue($result);
696 $this->assertFalse($DB->record_exists('role', array('id'=>$role->id)));
697 $this->assertFalse($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
698 $this->assertFalse($DB->record_exists('role_capabilities', array('roleid'=>$role->id)));
699 $this->assertFalse($DB->record_exists('role_names', array('roleid'=>$role->id)));
700 $this->assertFalse($DB->record_exists('role_context_levels', array('roleid'=>$role->id)));
701 $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid'=>$role->id)));
702 $this->assertFalse($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
703 $this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
704 $this->assertFalse($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
706 // Test triggered event.
707 $this->assertInstanceOf('\core\event\role_deleted', $event);
708 $this->assertSame('role', $event->target);
709 $this->assertSame('role', $event->objecttable);
710 $this->assertSame($role->id, $event->objectid);
711 $this->assertEquals(context_system::instance(), $event->get_context());
712 $this->assertSame($role->shortname, $event->other['shortname']);
713 $this->assertSame($role->description, $event->other['description']);
714 $this->assertSame($role->archetype, $event->other['archetype']);
716 $expectedlegacylog = array(SITEID, 'role', 'delete', 'admin/roles/manage.php?action=delete&roleid='.$role->id,
717 $role->shortname, '');
718 $this->assertEventLegacyLogData($expectedlegacylog, $event);
722 * Test fetching of all roles.
724 public function test_get_all_roles() {
727 $this->resetAfterTest();
729 $allroles = get_all_roles();
730 $this->assertInternalType('array', $allroles);
731 $this->assertCount(8, $allroles); // There are 8 roles is standard install.
733 $role = reset($allroles);
734 $role = (array)$role;
736 $this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype'), array_keys($role), '', 0, 10, true);
738 foreach ($allroles as $roleid => $role) {
739 $this->assertEquals($role->id, $roleid);
742 $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
743 $course = $this->getDataGenerator()->create_course();
744 $coursecontext = context_course::instance($course->id);
745 $otherid = create_role('Other role', 'other', 'Some other role', '');
746 $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
747 $DB->insert_record('role_names', $teacherename);
748 $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
749 $DB->insert_record('role_names', $otherrename);
750 $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
752 $allroles = get_all_roles($coursecontext);
753 $this->assertInternalType('array', $allroles);
754 $this->assertCount(9, $allroles);
755 $role = reset($allroles);
756 $role = (array)$role;
758 $this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype', 'coursealias'), array_keys($role), '', 0, 10, true);
760 foreach ($allroles as $roleid => $role) {
761 $this->assertEquals($role->id, $roleid);
762 if (isset($renames[$roleid])) {
763 $this->assertSame($renames[$roleid], $role->coursealias);
765 $this->assertNull($role->coursealias);
771 * Test getting of all archetypes.
773 public function test_get_role_archetypes() {
774 $archetypes = get_role_archetypes();
775 $this->assertCount(8, $archetypes); // There are 8 archetypes in standard install.
776 foreach ($archetypes as $k => $v) {
777 $this->assertSame($k, $v);
782 * Test getting of roles with given archetype.
784 public function test_get_archetype_roles() {
785 $this->resetAfterTest();
787 // New install should have 1 role for each archetype.
788 $archetypes = get_role_archetypes();
789 foreach ($archetypes as $archetype) {
790 $roles = get_archetype_roles($archetype);
791 $this->assertCount(1, $roles);
792 $role = reset($roles);
793 $this->assertSame($archetype, $role->archetype);
796 create_role('New student role', 'student2', 'New student description', 'student');
797 $roles = get_archetype_roles('student');
798 $this->assertCount(2, $roles);
802 * Test aliased role names.
804 public function test_role_get_name() {
807 $this->resetAfterTest();
809 $allroles = $DB->get_records('role');
810 $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
811 $course = $this->getDataGenerator()->create_course();
812 $coursecontext = context_course::instance($course->id);
813 $otherid = create_role('Other role', 'other', 'Some other role', '');
814 $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
815 $DB->insert_record('role_names', $teacherename);
816 $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
817 $DB->insert_record('role_names', $otherrename);
818 $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
820 foreach ($allroles as $role) {
821 // Get localised name from lang pack.
822 $this->assertSame('', $role->name);
823 $name = role_get_name($role, null, ROLENAME_ORIGINAL);
824 $this->assertNotEmpty($name);
825 $this->assertNotEquals($role->shortname, $name);
827 if (isset($renames[$role->id])) {
828 $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext));
829 $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext, ROLENAME_ALIAS));
830 $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext, ROLENAME_ALIAS_RAW));
831 $this->assertSame("{$renames[$role->id]} ($name)", role_get_name($role, $coursecontext, ROLENAME_BOTH));
833 $this->assertSame($name, role_get_name($role, $coursecontext));
834 $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_ALIAS));
835 $this->assertNull(role_get_name($role, $coursecontext, ROLENAME_ALIAS_RAW));
836 $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_BOTH));
838 $this->assertSame($name, role_get_name($role));
839 $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_ORIGINAL));
840 $this->assertSame($name, role_get_name($role, null, ROLENAME_ORIGINAL));
841 $this->assertSame($role->shortname, role_get_name($role, $coursecontext, ROLENAME_SHORT));
842 $this->assertSame($role->shortname, role_get_name($role, null, ROLENAME_SHORT));
843 $this->assertSame("$name ($role->shortname)", role_get_name($role, $coursecontext, ROLENAME_ORIGINALANDSHORT));
844 $this->assertSame("$name ($role->shortname)", role_get_name($role, null, ROLENAME_ORIGINALANDSHORT));
845 $this->assertNull(role_get_name($role, null, ROLENAME_ALIAS_RAW));
850 * Test tweaking of role name arrays.
852 public function test_role_fix_names() {
855 $this->resetAfterTest();
857 $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
858 $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
859 $otherid = create_role('Other role', 'other', 'Some other role', '');
860 $anotherid = create_role('Another role', 'another', 'Yet another other role', '');
861 $allroles = $DB->get_records('role');
863 $syscontext = context_system::instance();
864 $frontcontext = context_course::instance(SITEID);
865 $course = $this->getDataGenerator()->create_course();
866 $coursecontext = context_course::instance($course->id);
867 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
868 $categorycontext = context_coursecat::instance($category->id);
870 $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
871 $DB->insert_record('role_names', $teacherename);
872 $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
873 $DB->insert_record('role_names', $otherrename);
874 $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
876 // Make sure all localname contain proper values for each ROLENAME_ constant,
877 // note role_get_name() on frontpage is used to get the original name for future compatibility.
879 unset($roles[$student->id]); // Remove one role to make sure no role is added or removed.
880 $rolenames = array();
881 foreach ($roles as $role) {
882 $rolenames[$role->id] = $role->name;
885 $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT);
886 foreach ($alltypes as $type) {
887 $fixed = role_fix_names($roles, $coursecontext, $type);
888 $this->assertCount(count($roles), $fixed);
889 foreach ($fixed as $roleid => $rolename) {
890 $this->assertInstanceOf('stdClass', $rolename);
891 $role = $allroles[$roleid];
892 $name = role_get_name($role, $coursecontext, $type);
893 $this->assertSame($name, $rolename->localname);
895 $fixed = role_fix_names($rolenames, $coursecontext, $type);
896 $this->assertCount(count($rolenames), $fixed);
897 foreach ($fixed as $roleid => $rolename) {
898 $role = $allroles[$roleid];
899 $name = role_get_name($role, $coursecontext, $type);
900 $this->assertSame($name, $rolename);
906 * Test role default allows.
908 public function test_get_default_role_archetype_allows() {
909 $archetypes = get_role_archetypes();
910 foreach ($archetypes as $archetype) {
912 $result = get_default_role_archetype_allows('assign', $archetype);
913 $this->assertInternalType('array', $result);
915 $result = get_default_role_archetype_allows('override', $archetype);
916 $this->assertInternalType('array', $result);
918 $result = get_default_role_archetype_allows('switch', $archetype);
919 $this->assertInternalType('array', $result);
921 $result = get_default_role_archetype_allows('view', $archetype);
922 $this->assertInternalType('array', $result);
925 $result = get_default_role_archetype_allows('assign', '');
926 $this->assertSame(array(), $result);
928 $result = get_default_role_archetype_allows('override', '');
929 $this->assertSame(array(), $result);
931 $result = get_default_role_archetype_allows('switch', '');
932 $this->assertSame(array(), $result);
934 $result = get_default_role_archetype_allows('view', '');
935 $this->assertSame(array(), $result);
937 $result = get_default_role_archetype_allows('assign', 'wrongarchetype');
938 $this->assertSame(array(), $result);
939 $this->assertDebuggingCalled();
941 $result = get_default_role_archetype_allows('override', 'wrongarchetype');
942 $this->assertSame(array(), $result);
943 $this->assertDebuggingCalled();
945 $result = get_default_role_archetype_allows('switch', 'wrongarchetype');
946 $this->assertSame(array(), $result);
947 $this->assertDebuggingCalled();
949 $result = get_default_role_archetype_allows('view', 'wrongarchetype');
950 $this->assertSame(array(), $result);
951 $this->assertDebuggingCalled();
955 * Test allowing of role assignments.
957 public function test_core_role_set_assign_allowed() {
960 $this->resetAfterTest();
962 $otherid = create_role('Other role', 'other', 'Some other role', '');
963 $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
965 $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
966 core_role_set_assign_allowed($otherid, $student->id);
967 $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
969 // Test event trigger.
970 $allowroleassignevent = \core\event\role_allow_assign_updated::create(array('context' => context_system::instance()));
971 $sink = $this->redirectEvents();
972 $allowroleassignevent->trigger();
973 $events = $sink->get_events();
975 $event = array_pop($events);
976 $this->assertInstanceOf('\core\event\role_allow_assign_updated', $event);
978 $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
979 $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
980 $this->assertEventLegacyLogData($expectedlegacylog, $event);
984 * Test allowing of role overrides.
986 public function test_core_role_set_override_allowed() {
989 $this->resetAfterTest();
991 $otherid = create_role('Other role', 'other', 'Some other role', '');
992 $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
994 $this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
995 core_role_set_override_allowed($otherid, $student->id);
996 $this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
998 // Test event trigger.
999 $allowroleassignevent = \core\event\role_allow_override_updated::create(array('context' => context_system::instance()));
1000 $sink = $this->redirectEvents();
1001 $allowroleassignevent->trigger();
1002 $events = $sink->get_events();
1004 $event = array_pop($events);
1005 $this->assertInstanceOf('\core\event\role_allow_override_updated', $event);
1007 $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
1008 $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
1009 $this->assertEventLegacyLogData($expectedlegacylog, $event);
1013 * Test allowing of role switching.
1015 public function test_core_role_set_switch_allowed() {
1018 $this->resetAfterTest();
1020 $otherid = create_role('Other role', 'other', 'Some other role', '');
1021 $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
1023 $this->assertFalse($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
1024 core_role_set_switch_allowed($otherid, $student->id);
1025 $this->assertTrue($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
1027 // Test event trigger.
1028 $allowroleassignevent = \core\event\role_allow_switch_updated::create(array('context' => context_system::instance()));
1029 $sink = $this->redirectEvents();
1030 $allowroleassignevent->trigger();
1031 $events = $sink->get_events();
1033 $event = array_pop($events);
1034 $this->assertInstanceOf('\core\event\role_allow_switch_updated', $event);
1036 $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
1037 $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
1038 $this->assertEventLegacyLogData($expectedlegacylog, $event);
1042 * Test allowing of role switching.
1044 public function test_core_role_set_view_allowed() {
1047 $this->resetAfterTest();
1049 $otherid = create_role('Other role', 'other', 'Some other role', '');
1050 $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
1052 $this->assertFalse($DB->record_exists('role_allow_view', array('roleid' => $otherid, 'allowview' => $student->id)));
1053 core_role_set_view_allowed($otherid, $student->id);
1054 $this->assertTrue($DB->record_exists('role_allow_view', array('roleid' => $otherid, 'allowview' => $student->id)));
1056 // Test event trigger.
1057 $allowroleassignevent = \core\event\role_allow_view_updated::create(array('context' => context_system::instance()));
1058 $sink = $this->redirectEvents();
1059 $allowroleassignevent->trigger();
1060 $events = $sink->get_events();
1062 $event = array_pop($events);
1063 $this->assertInstanceOf('\core\event\role_allow_view_updated', $event);
1065 $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
1066 $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
1067 $this->assertEventLegacyLogData($expectedlegacylog, $event);
1071 * Test returning of assignable roles in context.
1073 public function test_get_assignable_roles() {
1076 $this->resetAfterTest();
1078 $course = $this->getDataGenerator()->create_course();
1079 $coursecontext = context_course::instance($course->id);
1081 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1082 $teacher = $this->getDataGenerator()->create_user();
1083 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1084 $teacherename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1085 $DB->insert_record('role_names', $teacherename);
1087 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
1088 $student = $this->getDataGenerator()->create_user();
1089 role_assign($studentrole->id, $student->id, $coursecontext);
1091 $contexts = $DB->get_records('context');
1092 $users = $DB->get_records('user');
1093 $allroles = $DB->get_records('role');
1095 // Evaluate all results for all users in all contexts.
1096 foreach ($users as $user) {
1097 $this->setUser($user);
1098 foreach ($contexts as $contextid => $unused) {
1099 $context = context_helper::instance_by_id($contextid);
1100 $roles = get_assignable_roles($context, ROLENAME_SHORT);
1101 foreach ($allroles as $roleid => $role) {
1102 if (isset($roles[$roleid])) {
1103 if (is_siteadmin()) {
1104 $this->assertTrue($DB->record_exists('role_context_levels', array('contextlevel'=>$context->contextlevel, 'roleid'=>$roleid)));
1106 $this->assertTrue(user_can_assign($context, $roleid), "u:$user->id r:$roleid");
1108 $this->assertEquals($role->shortname, $roles[$roleid]);
1110 $allowed = $DB->record_exists('role_context_levels', array('contextlevel'=>$context->contextlevel, 'roleid'=>$roleid));
1111 if (is_siteadmin()) {
1112 $this->assertFalse($allowed);
1114 $this->assertFalse($allowed and user_can_assign($context, $roleid), "u:$user->id, r:{$allroles[$roleid]->name}, c:$context->contextlevel");
1121 // Not-logged-in user.
1123 foreach ($contexts as $contextid => $unused) {
1124 $context = context_helper::instance_by_id($contextid);
1125 $roles = get_assignable_roles($context, ROLENAME_SHORT);
1126 $this->assertSame(array(), $roles);
1129 // Test current user.
1131 $admin = $DB->get_record('user', array('username'=>'admin'), '*', MUST_EXIST);
1132 $roles1 = get_assignable_roles($coursecontext, ROLENAME_SHORT, false, $admin);
1133 $roles2 = get_assignable_roles($coursecontext, ROLENAME_SHORT, false, $admin->id);
1134 $this->setAdminUser();
1135 $roles3 = get_assignable_roles($coursecontext, ROLENAME_SHORT);
1136 $this->assertSame($roles1, $roles3);
1137 $this->assertSame($roles2, $roles3);
1139 // Test parameter defaults.
1140 $this->setAdminUser();
1141 $roles1 = get_assignable_roles($coursecontext);
1142 $roles2 = get_assignable_roles($coursecontext, ROLENAME_ALIAS, false, $admin);
1143 $this->assertEquals($roles2, $roles1);
1145 // Verify returned names - let's allow all roles everywhere to simplify this a bit.
1146 $alllevels = context_helper::get_all_levels();
1147 $alllevels = array_keys($alllevels);
1148 foreach ($allroles as $roleid => $role) {
1149 set_role_contextlevels($roleid, $alllevels);
1151 $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT);
1152 foreach ($alltypes as $type) {
1153 $rolenames = role_fix_names($allroles, $coursecontext, $type);
1154 $roles = get_assignable_roles($coursecontext, $type, false, $admin);
1155 foreach ($roles as $roleid => $rolename) {
1156 $this->assertSame($rolenames[$roleid]->localname, $rolename);
1161 $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT);
1162 foreach ($alltypes as $type) {
1163 $roles = get_assignable_roles($coursecontext, $type, false, $admin);
1164 list($rolenames, $rolecounts, $nameswithcounts) = get_assignable_roles($coursecontext, $type, true, $admin);
1165 $this->assertEquals($roles, $rolenames);
1166 foreach ($rolenames as $roleid => $name) {
1167 if ($roleid == $teacherrole->id or $roleid == $studentrole->id) {
1168 $this->assertEquals(1, $rolecounts[$roleid]);
1170 $this->assertEquals(0, $rolecounts[$roleid]);
1172 $this->assertSame("$name ($rolecounts[$roleid])", $nameswithcounts[$roleid]);
1178 * Test user count of assignable roles in context where users are assigned the role via different components.
1180 public function test_get_assignable_roles_distinct_usercount() {
1183 $this->resetAfterTest(true);
1185 $this->setAdminUser();
1187 $course = $this->getDataGenerator()->create_course();
1188 $context = \context_course::instance($course->id);
1190 $user1 = $this->getDataGenerator()->create_user();
1191 $user2 = $this->getDataGenerator()->create_user();
1193 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
1195 // Assign each user the student role in course.
1196 role_assign($studentrole->id, $user1->id, $context->id);
1197 role_assign($studentrole->id, $user2->id, $context->id);
1199 list($rolenames, $rolecounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_SHORT, true);
1200 $this->assertEquals(2, $rolecounts[$studentrole->id]);
1202 // Assign first user the student role in course again (this time via 'enrol_self' component).
1203 role_assign($studentrole->id, $user1->id, $context->id, 'enrol_self', 1);
1205 // There are still only two distinct users.
1206 list($rolenames, $rolecounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_SHORT, true);
1207 $this->assertEquals(2, $rolecounts[$studentrole->id]);
1211 * Test getting of all switchable roles.
1213 public function test_get_switchable_roles() {
1216 $this->resetAfterTest();
1218 $course = $this->getDataGenerator()->create_course();
1219 $coursecontext = context_course::instance($course->id);
1221 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1222 $teacher = $this->getDataGenerator()->create_user();
1223 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1224 $teacherename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1225 $DB->insert_record('role_names', $teacherename);
1227 $contexts = $DB->get_records('context');
1228 $users = $DB->get_records('user');
1229 $allroles = $DB->get_records('role');
1231 // Evaluate all results for all users in all contexts.
1232 foreach ($users as $user) {
1233 $this->setUser($user);
1234 foreach ($contexts as $contextid => $unused) {
1235 $context = context_helper::instance_by_id($contextid);
1236 $roles = get_switchable_roles($context);
1237 foreach ($allroles as $roleid => $role) {
1238 if (is_siteadmin()) {
1239 $this->assertTrue(isset($roles[$roleid]));
1241 $parents = $context->get_parent_context_ids(true);
1242 $pcontexts = implode(',' , $parents);
1243 $allowed = $DB->record_exists_sql(
1246 JOIN {role_allow_switch} ras ON ras.allowswitch = r.id
1247 JOIN {role_assignments} ra ON ra.roleid = ras.roleid
1248 WHERE ra.userid = :userid AND ra.contextid IN ($pcontexts) AND r.id = :roleid
1250 array('userid'=>$user->id, 'roleid'=>$roleid)
1252 if (isset($roles[$roleid])) {
1253 $this->assertTrue($allowed);
1255 $this->assertFalse($allowed);
1259 if (isset($roles[$roleid])) {
1260 $coursecontext = $context->get_course_context(false);
1261 $this->assertSame(role_get_name($role, $coursecontext), $roles[$roleid]);
1269 * Test getting of all overridable roles.
1271 public function test_get_overridable_roles() {
1274 $this->resetAfterTest();
1276 $course = $this->getDataGenerator()->create_course();
1277 $coursecontext = context_course::instance($course->id);
1279 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1280 $teacher = $this->getDataGenerator()->create_user();
1281 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1282 $teacherename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1283 $DB->insert_record('role_names', $teacherename);
1284 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability is ok.
1285 assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id);
1287 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
1288 $student = $this->getDataGenerator()->create_user();
1289 role_assign($studentrole->id, $student->id, $coursecontext);
1291 $contexts = $DB->get_records('context');
1292 $users = $DB->get_records('user');
1293 $allroles = $DB->get_records('role');
1295 // Evaluate all results for all users in all contexts.
1296 foreach ($users as $user) {
1297 $this->setUser($user);
1298 foreach ($contexts as $contextid => $unused) {
1299 $context = context_helper::instance_by_id($contextid);
1300 $roles = get_overridable_roles($context, ROLENAME_SHORT);
1301 foreach ($allroles as $roleid => $role) {
1302 $hascap = has_any_capability(array('moodle/role:safeoverride', 'moodle/role:override'), $context);
1303 if (is_siteadmin()) {
1304 $this->assertTrue(isset($roles[$roleid]));
1306 $parents = $context->get_parent_context_ids(true);
1307 $pcontexts = implode(',' , $parents);
1308 $allowed = $DB->record_exists_sql(
1311 JOIN {role_allow_override} rao ON r.id = rao.allowoverride
1312 JOIN {role_assignments} ra ON rao.roleid = ra.roleid
1313 WHERE ra.userid = :userid AND ra.contextid IN ($pcontexts) AND r.id = :roleid
1315 array('userid'=>$user->id, 'roleid'=>$roleid)
1317 if (isset($roles[$roleid])) {
1318 $this->assertTrue($hascap);
1319 $this->assertTrue($allowed);
1321 $this->assertFalse($hascap and $allowed);
1325 if (isset($roles[$roleid])) {
1326 $this->assertEquals($role->shortname, $roles[$roleid]);
1332 // Test parameter defaults.
1333 $this->setAdminUser();
1334 $roles1 = get_overridable_roles($coursecontext);
1335 $roles2 = get_overridable_roles($coursecontext, ROLENAME_ALIAS, false);
1336 $this->assertEquals($roles2, $roles1);
1338 $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT);
1339 foreach ($alltypes as $type) {
1340 $rolenames = role_fix_names($allroles, $coursecontext, $type);
1341 $roles = get_overridable_roles($coursecontext, $type, false);
1342 foreach ($roles as $roleid => $rolename) {
1343 $this->assertSame($rolenames[$roleid]->localname, $rolename);
1348 $roles = get_overridable_roles($coursecontext, ROLENAME_ALIAS, false);
1349 list($rolenames, $rolecounts, $nameswithcounts) = get_overridable_roles($coursecontext, ROLENAME_ALIAS, true);
1350 $this->assertEquals($roles, $rolenames);
1351 foreach ($rolenames as $roleid => $name) {
1352 if ($roleid == $teacherrole->id) {
1353 $this->assertEquals(1, $rolecounts[$roleid]);
1355 $this->assertEquals(0, $rolecounts[$roleid]);
1357 $this->assertSame("$name ($rolecounts[$roleid])", $nameswithcounts[$roleid]);
1362 * Test getting of all overridable roles.
1364 public function test_get_viewable_roles_course() {
1367 $this->resetAfterTest();
1369 $course = $this->getDataGenerator()->create_course();
1370 $coursecontext = context_course::instance($course->id);
1372 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
1373 $teacher = $this->getDataGenerator()->create_user();
1374 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1376 $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
1377 $studentrolerename = (object) array('roleid' => $studentrole->id, 'name' => 'Učitel', 'contextid' => $coursecontext->id);
1378 $DB->insert_record('role_names', $studentrolerename);
1380 // By default teacher can see student.
1381 $this->setUser($teacher);
1382 $viewableroles = get_viewable_roles($coursecontext);
1383 $this->assertContains($studentrolerename->name, array_values($viewableroles));
1384 // Remove view permission.
1385 $DB->delete_records('role_allow_view', array('roleid' => $teacherrole->id, 'allowview' => $studentrole->id));
1386 $viewableroles = get_viewable_roles($coursecontext);
1387 // Teacher can no longer see student role.
1388 $this->assertNotContains($studentrolerename->name, array_values($viewableroles));
1389 // Allow again teacher to view student.
1390 core_role_set_view_allowed($teacherrole->id, $studentrole->id);
1391 // Teacher can now see student role.
1392 $viewableroles = get_viewable_roles($coursecontext);
1393 $this->assertContains($studentrolerename->name, array_values($viewableroles));
1397 * Test getting of all overridable roles.
1399 public function test_get_viewable_roles_system() {
1402 $this->resetAfterTest();
1404 $context = context_system::instance();
1406 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
1407 $teacher = $this->getDataGenerator()->create_user();
1408 role_assign($teacherrole->id, $teacher->id, $context);
1410 $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
1411 $studentrolename = role_get_name($studentrole, $context);
1413 // By default teacher can see student.
1414 $this->setUser($teacher);
1415 $viewableroles = get_viewable_roles($context);
1416 $this->assertContains($studentrolename, array_values($viewableroles));
1417 // Remove view permission.
1418 $DB->delete_records('role_allow_view', array('roleid' => $teacherrole->id, 'allowview' => $studentrole->id));
1419 $viewableroles = get_viewable_roles($context);
1420 // Teacher can no longer see student role.
1421 $this->assertNotContains($studentrolename, array_values($viewableroles));
1422 // Allow again teacher to view student.
1423 core_role_set_view_allowed($teacherrole->id, $studentrole->id);
1424 // Teacher can now see student role.
1425 $viewableroles = get_viewable_roles($context);
1426 $this->assertContains($studentrolename, array_values($viewableroles));
1430 * Test we have context level defaults.
1432 public function test_get_default_contextlevels() {
1433 $archetypes = get_role_archetypes();
1434 $alllevels = context_helper::get_all_levels();
1435 foreach ($archetypes as $archetype) {
1436 $defaults = get_default_contextlevels($archetype);
1437 $this->assertInternalType('array', $defaults);
1438 foreach ($defaults as $level) {
1439 $this->assertTrue(isset($alllevels[$level]));
1445 * Test role context level setup.
1447 public function test_set_role_contextlevels() {
1450 $this->resetAfterTest();
1452 $roleid = create_role('New student role', 'student2', 'New student description', 'student');
1454 $this->assertFalse($DB->record_exists('role_context_levels', array('roleid' => $roleid)));
1456 set_role_contextlevels($roleid, array(CONTEXT_COURSE, CONTEXT_MODULE));
1457 $levels = $DB->get_records('role_context_levels', array('roleid' => $roleid), '', 'contextlevel, contextlevel');
1458 $this->assertCount(2, $levels);
1459 $this->assertTrue(isset($levels[CONTEXT_COURSE]));
1460 $this->assertTrue(isset($levels[CONTEXT_MODULE]));
1462 set_role_contextlevels($roleid, array(CONTEXT_COURSE));
1463 $levels = $DB->get_records('role_context_levels', array('roleid' => $roleid), '', 'contextlevel, contextlevel');
1464 $this->assertCount(1, $levels);
1465 $this->assertTrue(isset($levels[CONTEXT_COURSE]));
1469 * Test getting of role context levels
1471 public function test_get_roles_for_contextlevels() {
1474 $allroles = get_all_roles();
1475 foreach (context_helper::get_all_levels() as $level => $unused) {
1476 $roles = get_roles_for_contextlevels($level);
1477 foreach ($allroles as $roleid => $unused) {
1478 $exists = $DB->record_exists('role_context_levels', array('contextlevel'=>$level, 'roleid'=>$roleid));
1479 if (in_array($roleid, $roles)) {
1480 $this->assertTrue($exists);
1482 $this->assertFalse($exists);
1489 * Test default enrol roles.
1491 public function test_get_default_enrol_roles() {
1492 $this->resetAfterTest();
1494 $course = $this->getDataGenerator()->create_course();
1495 $coursecontext = context_course::instance($course->id);
1497 $id2 = create_role('New student role', 'student2', 'New student description', 'student');
1498 set_role_contextlevels($id2, array(CONTEXT_COURSE));
1500 $allroles = get_all_roles();
1501 $expected = array($id2=>$allroles[$id2]);
1503 foreach (get_role_archetypes() as $archetype) {
1504 $defaults = get_default_contextlevels($archetype);
1505 if (in_array(CONTEXT_COURSE, $defaults)) {
1506 $roles = get_archetype_roles($archetype);
1507 foreach ($roles as $role) {
1508 $expected[$role->id] = $role;
1513 $roles = get_default_enrol_roles($coursecontext);
1514 foreach ($allroles as $role) {
1515 $this->assertEquals(isset($expected[$role->id]), isset($roles[$role->id]));
1516 if (isset($roles[$role->id])) {
1517 $this->assertSame(role_get_name($role, $coursecontext), $roles[$role->id]);
1523 * Test getting of role users.
1525 public function test_get_role_users() {
1528 $this->resetAfterTest();
1530 $systemcontext = context_system::instance();
1531 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
1532 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1533 $noeditteacherrole = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
1534 $course = $this->getDataGenerator()->create_course();
1535 $coursecontext = context_course::instance($course->id);
1536 $otherid = create_role('Other role', 'other', 'Some other role', '');
1537 $teacherrename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1538 $DB->insert_record('role_names', $teacherrename);
1539 $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
1540 $DB->insert_record('role_names', $otherrename);
1542 $user1 = $this->getDataGenerator()->create_user(array('firstname'=>'John', 'lastname'=>'Smith'));
1543 role_assign($teacherrole->id, $user1->id, $coursecontext->id);
1544 $user2 = $this->getDataGenerator()->create_user(array('firstname'=>'Jan', 'lastname'=>'Kovar'));
1545 role_assign($teacherrole->id, $user2->id, $systemcontext->id);
1546 $user3 = $this->getDataGenerator()->create_user();
1547 $this->getDataGenerator()->enrol_user($user3->id, $course->id, $teacherrole->id);
1548 $user4 = $this->getDataGenerator()->create_user();
1549 $this->getDataGenerator()->enrol_user($user4->id, $course->id, $studentrole->id);
1550 $this->getDataGenerator()->enrol_user($user4->id, $course->id, $noeditteacherrole->id);
1552 $group = $this->getDataGenerator()->create_group(array('courseid'=>$course->id));
1553 groups_add_member($group, $user3);
1555 $users = get_role_users($teacherrole->id, $coursecontext);
1556 $this->assertCount(2, $users);
1557 $this->assertArrayHasKey($user1->id, $users);
1558 $this->assertEquals($users[$user1->id]->id, $user1->id);
1559 $this->assertEquals($users[$user1->id]->roleid, $teacherrole->id);
1560 $this->assertEquals($users[$user1->id]->rolename, $teacherrole->name);
1561 $this->assertEquals($users[$user1->id]->roleshortname, $teacherrole->shortname);
1562 $this->assertEquals($users[$user1->id]->rolecoursealias, $teacherrename->name);
1563 $this->assertArrayHasKey($user3->id, $users);
1564 $this->assertEquals($users[$user3->id]->id, $user3->id);
1565 $this->assertEquals($users[$user3->id]->roleid, $teacherrole->id);
1566 $this->assertEquals($users[$user3->id]->rolename, $teacherrole->name);
1567 $this->assertEquals($users[$user3->id]->roleshortname, $teacherrole->shortname);
1568 $this->assertEquals($users[$user3->id]->rolecoursealias, $teacherrename->name);
1570 $users = get_role_users($teacherrole->id, $coursecontext, true);
1571 $this->assertCount(3, $users);
1573 $users = get_role_users($teacherrole->id, $coursecontext, true, '', null, null, '', 2, 1);
1574 $this->assertCount(1, $users);
1576 $users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email, u.idnumber', 'u.idnumber');
1577 $this->assertCount(2, $users);
1578 $this->assertArrayHasKey($user1->id, $users);
1579 $this->assertArrayHasKey($user3->id, $users);
1581 $users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email');
1582 $this->assertDebuggingCalled('get_role_users() adding u.lastname, u.firstname to the query result because they were required by $sort but missing in $fields');
1583 $this->assertCount(2, $users);
1584 $this->assertArrayHasKey($user1->id, $users);
1585 $this->assertObjectHasAttribute('lastname', $users[$user1->id]);
1586 $this->assertObjectHasAttribute('firstname', $users[$user1->id]);
1587 $this->assertArrayHasKey($user3->id, $users);
1588 $this->assertObjectHasAttribute('lastname', $users[$user3->id]);
1589 $this->assertObjectHasAttribute('firstname', $users[$user3->id]);
1591 $users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id AS id_alias');
1592 $this->assertDebuggingCalled('get_role_users() adding u.lastname, u.firstname to the query result because they were required by $sort but missing in $fields');
1593 $this->assertCount(2, $users);
1594 $this->assertArrayHasKey($user1->id, $users);
1595 $this->assertObjectHasAttribute('id_alias', $users[$user1->id]);
1596 $this->assertObjectHasAttribute('lastname', $users[$user1->id]);
1597 $this->assertObjectHasAttribute('firstname', $users[$user1->id]);
1598 $this->assertArrayHasKey($user3->id, $users);
1599 $this->assertObjectHasAttribute('id_alias', $users[$user3->id]);
1600 $this->assertObjectHasAttribute('lastname', $users[$user3->id]);
1601 $this->assertObjectHasAttribute('firstname', $users[$user3->id]);
1603 $users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email, u.idnumber', 'u.idnumber', null, $group->id);
1604 $this->assertCount(1, $users);
1605 $this->assertArrayHasKey($user3->id, $users);
1607 $users = get_role_users($teacherrole->id, $coursecontext, true, 'u.id, u.email, u.idnumber, u.firstname', 'u.idnumber', null, '', '', '', 'u.firstname = :xfirstname', array('xfirstname'=>'John'));
1608 $this->assertCount(1, $users);
1609 $this->assertArrayHasKey($user1->id, $users);
1611 $users = get_role_users(array($noeditteacherrole->id, $studentrole->id), $coursecontext, false, 'ra.id', 'ra.id');
1612 $this->assertDebuggingNotCalled();
1613 $users = get_role_users(array($noeditteacherrole->id, $studentrole->id), $coursecontext, false, 'ra.userid', 'ra.userid');
1614 $this->assertDebuggingCalled('get_role_users() without specifying one single roleid needs to be called prefixing ' .
1615 'role assignments id (ra.id) as unique field, you can use $fields param for it.');
1616 $users = get_role_users(array($noeditteacherrole->id, $studentrole->id), $coursecontext, false);
1617 $this->assertDebuggingCalled('get_role_users() without specifying one single roleid needs to be called prefixing ' .
1618 'role assignments id (ra.id) as unique field, you can use $fields param for it.');
1619 $users = get_role_users(array($noeditteacherrole->id, $studentrole->id), $coursecontext,
1620 false, 'u.id, u.firstname', 'u.id, u.firstname');
1621 $this->assertDebuggingCalled('get_role_users() without specifying one single roleid needs to be called prefixing ' .
1622 'role assignments id (ra.id) as unique field, you can use $fields param for it.');
1626 * Test used role query.
1628 public function test_get_roles_used_in_context() {
1631 $this->resetAfterTest();
1633 $systemcontext = context_system::instance();
1634 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1635 $course = $this->getDataGenerator()->create_course();
1636 $coursecontext = context_course::instance($course->id);
1637 $otherid = create_role('Other role', 'other', 'Some other role', '');
1638 $teacherrename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1639 $DB->insert_record('role_names', $teacherrename);
1640 $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
1641 $DB->insert_record('role_names', $otherrename);
1643 $user1 = $this->getDataGenerator()->create_user();
1644 role_assign($teacherrole->id, $user1->id, $coursecontext->id);
1646 $roles = get_roles_used_in_context($coursecontext);
1647 $this->assertCount(1, $roles);
1648 $role = reset($roles);
1649 $roleid = key($roles);
1650 $this->assertEquals($roleid, $role->id);
1651 $this->assertEquals($teacherrole->id, $role->id);
1652 $this->assertSame($teacherrole->name, $role->name);
1653 $this->assertSame($teacherrole->shortname, $role->shortname);
1654 $this->assertEquals($teacherrole->sortorder, $role->sortorder);
1655 $this->assertSame($teacherrename->name, $role->coursealias);
1657 $user2 = $this->getDataGenerator()->create_user();
1658 role_assign($teacherrole->id, $user2->id, $systemcontext->id);
1659 role_assign($otherid, $user2->id, $systemcontext->id);
1661 $roles = get_roles_used_in_context($systemcontext);
1662 $this->assertCount(2, $roles);
1666 * Test roles used in course.
1668 public function test_get_user_roles_in_course() {
1671 $this->resetAfterTest();
1673 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1674 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
1675 $managerrole = $DB->get_record('role', array('shortname' => 'manager'), '*', MUST_EXIST);
1676 $course = $this->getDataGenerator()->create_course();
1677 $coursecontext = context_course::instance($course->id);
1678 $teacherrename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1679 $DB->insert_record('role_names', $teacherrename);
1681 $roleids = explode(',', $CFG->profileroles); // Should include teacher and student in new installs.
1682 $this->assertTrue(in_array($teacherrole->id, $roleids));
1683 $this->assertTrue(in_array($studentrole->id, $roleids));
1684 $this->assertFalse(in_array($managerrole->id, $roleids));
1686 $user1 = $this->getDataGenerator()->create_user();
1687 role_assign($teacherrole->id, $user1->id, $coursecontext->id);
1688 role_assign($studentrole->id, $user1->id, $coursecontext->id);
1689 $user2 = $this->getDataGenerator()->create_user();
1690 role_assign($studentrole->id, $user2->id, $coursecontext->id);
1691 $user3 = $this->getDataGenerator()->create_user();
1692 $user4 = $this->getDataGenerator()->create_user();
1693 role_assign($managerrole->id, $user4->id, $coursecontext->id);
1695 $this->setAdminUser();
1697 $roles = get_user_roles_in_course($user1->id, $course->id);
1698 $this->assertEquals(1, preg_match_all('/,/', $roles, $matches));
1699 $this->assertTrue(strpos($roles, role_get_name($teacherrole, $coursecontext)) !== false);
1701 $roles = get_user_roles_in_course($user2->id, $course->id);
1702 $this->assertEquals(0, preg_match_all('/,/', $roles, $matches));
1703 $this->assertTrue(strpos($roles, role_get_name($studentrole, $coursecontext)) !== false);
1705 $roles = get_user_roles_in_course($user3->id, $course->id);
1706 $this->assertSame('', $roles);
1708 // Managers should be able to see a link to their own role type, given they can assign it in the context.
1709 $this->setUser($user4);
1710 $roles = get_user_roles_in_course($user4->id, $course->id);
1711 $this->assertNotEmpty($roles);
1712 $this->assertEquals(1, count(explode(',', $roles)));
1713 $this->assertTrue(strpos($roles, role_get_name($managerrole, $coursecontext)) !== false);
1715 // Managers should see 2 roles if viewing a user who has been enrolled as a student and a teacher in the course.
1716 $roles = get_user_roles_in_course($user1->id, $course->id);
1717 $this->assertEquals(2, count(explode(',', $roles)));
1718 $this->assertTrue(strpos($roles, role_get_name($studentrole, $coursecontext)) !== false);
1719 $this->assertTrue(strpos($roles, role_get_name($teacherrole, $coursecontext)) !== false);
1721 // Students should not see the manager role if viewing a manager's profile.
1722 $this->setUser($user2);
1723 $roles = get_user_roles_in_course($user4->id, $course->id);
1724 $this->assertEmpty($roles); // Should see 0 roles on the manager's profile.
1725 $this->assertFalse(strpos($roles, role_get_name($managerrole, $coursecontext)) !== false);
1729 * Test get_user_roles and get_users_roles
1731 public function test_get_user_roles() {
1734 $this->resetAfterTest();
1736 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1737 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
1738 $course = $this->getDataGenerator()->create_course();
1739 $coursecontext = context_course::instance($course->id);
1740 $teacherrename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
1741 $DB->insert_record('role_names', $teacherrename);
1743 $roleids = explode(',', $CFG->profileroles); // Should include teacher and student in new installs.
1745 $user1 = $this->getDataGenerator()->create_user();
1746 role_assign($teacherrole->id, $user1->id, $coursecontext->id);
1747 role_assign($studentrole->id, $user1->id, $coursecontext->id);
1748 $user2 = $this->getDataGenerator()->create_user();
1749 role_assign($studentrole->id, $user2->id, $coursecontext->id);
1750 $user3 = $this->getDataGenerator()->create_user();
1752 $u1roles = get_user_roles($coursecontext, $user1->id);
1754 $u2roles = get_user_roles($coursecontext, $user2->id);
1756 $allroles = get_users_roles($coursecontext, [], false);
1757 $specificuserroles = get_users_roles($coursecontext, [$user1->id, $user2->id]);
1758 $this->assertEquals($u1roles, $allroles[$user1->id]);
1759 $this->assertEquals($u1roles, $specificuserroles[$user1->id]);
1760 $this->assertEquals($u2roles, $allroles[$user2->id]);
1761 $this->assertEquals($u2roles, $specificuserroles[$user2->id]);
1765 * Test has_capability(), has_any_capability() and has_all_capabilities().
1767 public function test_has_capability_and_friends() {
1770 $this->resetAfterTest();
1772 $course = $this->getDataGenerator()->create_course();
1773 $coursecontext = context_course::instance($course->id);
1774 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
1775 $teacher = $this->getDataGenerator()->create_user();
1776 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1777 $admin = $DB->get_record('user', array('username'=>'admin'));
1779 // Note: Here are used default capabilities, the full test is in permission evaluation bellow,
1780 // use two capabilities that teacher has and one does not, none of them should be allowed for not-logged-in user.
1782 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupsection')));
1783 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse')));
1784 $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/site:approvecourse')));
1786 $sca = array('moodle/backup:backupsection', 'moodle/backup:backupcourse', 'moodle/site:approvecourse');
1787 $sc = array('moodle/backup:backupsection', 'moodle/backup:backupcourse');
1790 $this->assertFalse(has_capability('moodle/backup:backupsection', $coursecontext));
1791 $this->assertFalse(has_capability('moodle/backup:backupcourse', $coursecontext));
1792 $this->assertFalse(has_capability('moodle/site:approvecourse', $coursecontext));
1793 $this->assertFalse(has_any_capability($sca, $coursecontext));
1794 $this->assertFalse(has_all_capabilities($sca, $coursecontext));
1796 $this->assertTrue(has_capability('moodle/backup:backupsection', $coursecontext, $teacher));
1797 $this->assertTrue(has_capability('moodle/backup:backupcourse', $coursecontext, $teacher));
1798 $this->assertFalse(has_capability('moodle/site:approvecourse', $coursecontext, $teacher));
1799 $this->assertTrue(has_any_capability($sca, $coursecontext, $teacher));
1800 $this->assertTrue(has_all_capabilities($sc, $coursecontext, $teacher));
1801 $this->assertFalse(has_all_capabilities($sca, $coursecontext, $teacher));
1803 $this->assertTrue(has_capability('moodle/backup:backupsection', $coursecontext, $admin));
1804 $this->assertTrue(has_capability('moodle/backup:backupcourse', $coursecontext, $admin));
1805 $this->assertTrue(has_capability('moodle/site:approvecourse', $coursecontext, $admin));
1806 $this->assertTrue(has_any_capability($sca, $coursecontext, $admin));
1807 $this->assertTrue(has_all_capabilities($sc, $coursecontext, $admin));
1808 $this->assertTrue(has_all_capabilities($sca, $coursecontext, $admin));
1810 $this->assertFalse(has_capability('moodle/backup:backupsection', $coursecontext, $admin, false));
1811 $this->assertFalse(has_capability('moodle/backup:backupcourse', $coursecontext, $admin, false));
1812 $this->assertFalse(has_capability('moodle/site:approvecourse', $coursecontext, $admin, false));
1813 $this->assertFalse(has_any_capability($sca, $coursecontext, $admin, false));
1814 $this->assertFalse(has_all_capabilities($sc, $coursecontext, $admin, false));
1815 $this->assertFalse(has_all_capabilities($sca, $coursecontext, $admin, false));
1817 $this->setUser($teacher);
1818 $this->assertTrue(has_capability('moodle/backup:backupsection', $coursecontext));
1819 $this->assertTrue(has_capability('moodle/backup:backupcourse', $coursecontext));
1820 $this->assertFalse(has_capability('moodle/site:approvecourse', $coursecontext));
1821 $this->assertTrue(has_any_capability($sca, $coursecontext));
1822 $this->assertTrue(has_all_capabilities($sc, $coursecontext));
1823 $this->assertFalse(has_all_capabilities($sca, $coursecontext));
1825 $this->setAdminUser();
1826 $this->assertTrue(has_capability('moodle/backup:backupsection', $coursecontext));
1827 $this->assertTrue(has_capability('moodle/backup:backupcourse', $coursecontext));
1828 $this->assertTrue(has_capability('moodle/site:approvecourse', $coursecontext));
1829 $this->assertTrue(has_any_capability($sca, $coursecontext));
1830 $this->assertTrue(has_all_capabilities($sc, $coursecontext));
1831 $this->assertTrue(has_all_capabilities($sca, $coursecontext));
1833 $this->assertFalse(has_capability('moodle/backup:backupsection', $coursecontext, 0));
1834 $this->assertFalse(has_capability('moodle/backup:backupcourse', $coursecontext, 0));
1835 $this->assertFalse(has_capability('moodle/site:approvecourse', $coursecontext, 0));
1836 $this->assertFalse(has_any_capability($sca, $coursecontext, 0));
1837 $this->assertFalse(has_all_capabilities($sca, $coursecontext, 0));
1841 * Test that assigning a fake cap does not return.
1843 public function test_fake_capability() {
1846 $this->resetAfterTest();
1848 $course = $this->getDataGenerator()->create_course();
1849 $coursecontext = context_course::instance($course->id);
1850 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
1851 $teacher = $this->getDataGenerator()->create_user();
1853 $fakecapname = 'moodle/fake:capability';
1855 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1856 $admin = $DB->get_record('user', array('username' => 'admin'));
1858 // Test a capability which does not exist.
1859 // Note: Do not use assign_capability because it will not allow fake caps.
1860 $DB->insert_record('role_capabilities', (object) [
1861 'contextid' => $coursecontext->id,
1862 'roleid' => $teacherrole->id,
1863 'capability' => $fakecapname,
1864 'permission' => CAP_ALLOW,
1865 'timemodified' => time(),
1869 // Check `has_capability`.
1870 $this->assertFalse(has_capability($fakecapname, $coursecontext, $teacher));
1871 $this->assertDebuggingCalled("Capability \"{$fakecapname}\" was not found! This has to be fixed in code.");
1872 $this->assertFalse(has_capability($fakecapname, $coursecontext, $admin));
1873 $this->assertDebuggingCalled("Capability \"{$fakecapname}\" was not found! This has to be fixed in code.");
1875 // Check `get_with_capability_sql` (with uses `get_with_capability_join`).
1876 list($sql, $params) = get_with_capability_sql($coursecontext, $fakecapname);
1877 $users = $DB->get_records_sql($sql, $params);
1879 $this->assertFalse(array_key_exists($teacher->id, $users));
1880 $this->assertFalse(array_key_exists($admin->id, $users));
1882 // Check `get_users_by_capability`.
1883 $users = get_users_by_capability($coursecontext, $fakecapname);
1885 $this->assertFalse(array_key_exists($teacher->id, $users));
1886 $this->assertFalse(array_key_exists($admin->id, $users));
1890 * Test that assigning a fake cap does not return.
1892 public function test_fake_capability_assign() {
1895 $this->resetAfterTest();
1897 $course = $this->getDataGenerator()->create_course();
1898 $coursecontext = context_course::instance($course->id);
1899 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
1900 $teacher = $this->getDataGenerator()->create_user();
1902 $capability = 'moodle/fake:capability';
1904 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1905 $admin = $DB->get_record('user', array('username' => 'admin'));
1907 $this->expectException('coding_exception');
1908 $this->expectExceptionMessage("Capability '{$capability}' was not found! This has to be fixed in code.");
1909 assign_capability($capability, CAP_ALLOW, $teacherrole->id, $coursecontext);
1913 * Test that assigning a fake cap does not return.
1915 public function test_fake_capability_unassign() {
1918 $this->resetAfterTest();
1920 $course = $this->getDataGenerator()->create_course();
1921 $coursecontext = context_course::instance($course->id);
1922 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
1923 $teacher = $this->getDataGenerator()->create_user();
1925 $capability = 'moodle/fake:capability';
1927 role_assign($teacherrole->id, $teacher->id, $coursecontext);
1928 $admin = $DB->get_record('user', array('username' => 'admin'));
1930 $this->expectException('coding_exception');
1931 $this->expectExceptionMessage("Capability '{$capability}' was not found! This has to be fixed in code.");
1932 unassign_capability($capability, CAP_ALLOW, $teacherrole->id, $coursecontext);
1936 * Test that the caching in get_role_definitions() and get_role_definitions_uncached()
1937 * works as intended.
1939 public function test_role_definition_caching() {
1942 $this->resetAfterTest();
1944 // Get some role ids.
1945 $authenticatedrole = $DB->get_record('role', array('shortname' => 'user'), '*', MUST_EXIST);
1946 $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
1947 $emptyroleid = create_role('No capabilities', 'empty', 'A role with no capabilties');
1948 $course = $this->getDataGenerator()->create_course();
1949 $coursecontext = context_course::instance($course->id);
1951 // Instantiate the cache instance, since that does DB queries (get_config)
1952 // and we don't care about those.
1953 cache::make('core', 'roledefs');
1955 // One database query is not necessarily one database read, it seems. Find out how many.
1956 $startdbreads = $DB->perf_get_reads();
1957 $rs = $DB->get_recordset('user');
1959 $readsperquery = $DB->perf_get_reads() - $startdbreads;
1961 // Now load some role definitions, and check when it queries the database.
1963 // Load the capabilities for two roles. Should be one query.
1964 $startdbreads = $DB->perf_get_reads();
1965 get_role_definitions([$authenticatedrole->id, $studentrole->id]);
1966 $this->assertEquals(1 * $readsperquery, $DB->perf_get_reads() - $startdbreads);
1968 // Load the capabilities for same two roles. Should not query the DB.
1969 $startdbreads = $DB->perf_get_reads();
1970 get_role_definitions([$authenticatedrole->id, $studentrole->id]);
1971 $this->assertEquals(0 * $readsperquery, $DB->perf_get_reads() - $startdbreads);
1973 // Include a third role. Should do one DB query.
1974 $startdbreads = $DB->perf_get_reads();
1975 get_role_definitions([$authenticatedrole->id, $studentrole->id, $emptyroleid]);
1976 $this->assertEquals(1 * $readsperquery, $DB->perf_get_reads() - $startdbreads);
1978 // Repeat call. No DB queries.
1979 $startdbreads = $DB->perf_get_reads();
1980 get_role_definitions([$authenticatedrole->id, $studentrole->id, $emptyroleid]);
1981 $this->assertEquals(0 * $readsperquery, $DB->perf_get_reads() - $startdbreads);
1984 role_change_permission($studentrole->id, $coursecontext, 'moodle/course:tag', CAP_ALLOW);
1986 // Should now know to do one query.
1987 $startdbreads = $DB->perf_get_reads();
1988 get_role_definitions([$authenticatedrole->id, $studentrole->id]);
1989 $this->assertEquals(1 * $readsperquery, $DB->perf_get_reads() - $startdbreads);
1991 // Now clear the in-memory cache, and verify that it does not query the DB.
1992 // Cannot use accesslib_clear_all_caches_for_unit_testing since that also
1993 // clears the MUC cache.
1994 global $ACCESSLIB_PRIVATE;
1995 $ACCESSLIB_PRIVATE->cacheroledefs = array();
1997 // Get all roles. Should not need the DB.
1998 $startdbreads = $DB->perf_get_reads();
1999 get_role_definitions([$authenticatedrole->id, $studentrole->id, $emptyroleid]);
2000 $this->assertEquals(0 * $readsperquery, $DB->perf_get_reads() - $startdbreads);
2004 * Tests get_user_capability_course() which checks a capability across all courses.
2006 public function test_get_user_capability_course() {
2009 $this->resetAfterTest();
2011 $generator = $this->getDataGenerator();
2012 $cap = 'moodle/course:view';
2014 // The structure being created here is this:
2016 // All tests work with the single capability 'moodle/course:view'.
2018 // ROLE DEF/OVERRIDE ROLE ASSIGNS
2019 // Role: Allow Prohib Empty Def user u1 u2 u3 u4 u5 u6 u7 u8
2020 // System ALLOW PROHIBIT A E A+E
2031 // Front-page and guest role stuff from the end of this test not included in the diagram.
2033 // Create a role which allows course:view and one that prohibits it, and one neither.
2034 $allowroleid = $generator->create_role();
2035 $prohibitroleid = $generator->create_role();
2036 $emptyroleid = $generator->create_role();
2037 $systemcontext = context_system::instance();
2038 assign_capability($cap, CAP_ALLOW, $allowroleid, $systemcontext->id);
2039 assign_capability($cap, CAP_PROHIBIT, $prohibitroleid, $systemcontext->id);
2041 // Create two categories (nested).
2042 $cat1 = $generator->create_category();
2043 $cat2 = $generator->create_category(['parent' => $cat1->id]);
2045 // Create six courses - two in cat1, two in cat2, and two in default category.
2046 // Shortnames are used for a sorting test. Otherwise they are not significant.
2047 $c1 = $generator->create_course(['category' => $cat1->id, 'shortname' => 'Z']);
2048 $c2 = $generator->create_course(['category' => $cat1->id, 'shortname' => 'Y']);
2049 $c3 = $generator->create_course(['category' => $cat2->id, 'shortname' => 'X']);
2050 $c4 = $generator->create_course(['category' => $cat2->id]);
2051 $c5 = $generator->create_course();
2052 $c6 = $generator->create_course();
2054 // Category overrides: in cat 1, empty role is allowed; in cat 2, empty role is prevented.
2055 assign_capability($cap, CAP_ALLOW, $emptyroleid,
2056 context_coursecat::instance($cat1->id)->id);
2057 assign_capability($cap, CAP_PREVENT, $emptyroleid,
2058 context_coursecat::instance($cat2->id)->id);
2060 // Course overrides: in C5, allow role is prevented; in C6, empty role is prohibited; in
2061 // C3, empty role is allowed.
2062 assign_capability($cap, CAP_PREVENT, $allowroleid,
2063 context_course::instance($c5->id)->id);
2064 assign_capability($cap, CAP_PROHIBIT, $emptyroleid,
2065 context_course::instance($c6->id)->id);
2066 assign_capability($cap, CAP_ALLOW, $emptyroleid,
2067 context_course::instance($c3->id)->id);
2068 assign_capability($cap, CAP_ALLOW, $prohibitroleid,
2069 context_course::instance($c2->id)->id);
2071 // User 1 has no roles except default user role.
2072 $u1 = $generator->create_user();
2074 // It returns false (annoyingly) if there are no courses.
2075 $this->assertFalse(get_user_capability_course($cap, $u1->id, true, '', 'id'));
2077 // Final override: in C1, default user role is allowed.
2078 assign_capability($cap, CAP_ALLOW, $CFG->defaultuserroleid,
2079 context_course::instance($c1->id)->id);
2081 // Should now get C1 only.
2082 $courses = get_user_capability_course($cap, $u1->id, true, '', 'id');
2083 $this->assert_course_ids([$c1->id], $courses);
2085 // User 2 has allow role (system wide).
2086 $u2 = $generator->create_user();
2087 role_assign($allowroleid, $u2->id, $systemcontext->id);
2089 // Should get everything except C5.
2090 $courses = get_user_capability_course($cap, $u2->id, true, '', 'id');
2091 $this->assert_course_ids([SITEID, $c1->id, $c2->id, $c3->id, $c4->id, $c6->id], $courses);
2093 // User 3 has empty role (system wide).
2094 $u3 = $generator->create_user();
2095 role_assign($emptyroleid, $u3->id, $systemcontext->id);
2097 // Should get cat 1 courses but not cat2, except C3.
2098 $courses = get_user_capability_course($cap, $u3->id, true, '', 'id');
2099 $this->assert_course_ids([$c1->id, $c2->id, $c3->id], $courses);
2101 // User 4 has allow and empty role (system wide).
2102 $u4 = $generator->create_user();
2103 role_assign($allowroleid, $u4->id, $systemcontext->id);
2104 role_assign($emptyroleid, $u4->id, $systemcontext->id);
2106 // Should get everything except C5 and C6.
2107 $courses = get_user_capability_course($cap, $u4->id, true, '', 'id');
2108 $this->assert_course_ids([SITEID, $c1->id, $c2->id, $c3->id, $c4->id], $courses);
2110 // User 5 has allow role in default category only.
2111 $u5 = $generator->create_user();
2112 role_assign($allowroleid, $u5->id, context_coursecat::instance($c5->category)->id);
2114 // Should get C1 and the default category courses but not C5.
2115 $courses = get_user_capability_course($cap, $u5->id, true, '', 'id');
2116 $this->assert_course_ids([$c1->id, $c6->id], $courses);
2118 // User 6 has a bunch of course roles: prohibit role in C1, empty role in C3, allow role in
2120 $u6 = $generator->create_user();
2121 role_assign($prohibitroleid, $u6->id, context_course::instance($c1->id)->id);
2122 role_assign($emptyroleid, $u6->id, context_course::instance($c3->id)->id);
2123 role_assign($allowroleid, $u6->id, context_course::instance($c5->id)->id);
2125 // Should get C3 only because the allow role is prevented in C5.
2126 $courses = get_user_capability_course($cap, $u6->id, true, '', 'id');
2127 $this->assert_course_ids([$c3->id], $courses);
2129 // User 7 has empty role in C2.
2130 $u7 = $generator->create_user();
2131 role_assign($emptyroleid, $u7->id, context_course::instance($c2->id)->id);
2133 // Should get C1 by the default user role override, and C2 by the cat1 level override.
2134 $courses = get_user_capability_course($cap, $u7->id, true, '', 'id');
2135 $this->assert_course_ids([$c1->id, $c2->id], $courses);
2137 // User 8 has prohibit role as system context, to verify that prohibits can't be overridden.
2138 $u8 = $generator->create_user();
2139 role_assign($prohibitroleid, $u8->id, context_course::instance($c2->id)->id);
2141 // Should get C1 by the default user role override, no other courses because the prohibit cannot be overridden.
2142 $courses = get_user_capability_course($cap, $u8->id, true, '', 'id');
2143 $this->assert_course_ids([$c1->id], $courses);
2145 // Admin user gets everything....
2146 $courses = get_user_capability_course($cap, get_admin()->id, true, '', 'id');
2147 $this->assert_course_ids([SITEID, $c1->id, $c2->id, $c3->id, $c4->id, $c5->id, $c6->id],
2150 // Unless you turn off doanything, when it only has the things a user with no role does.
2151 $courses = get_user_capability_course($cap, get_admin()->id, false, '', 'id');
2152 $this->assert_course_ids([$c1->id], $courses);
2154 // Using u3 as an example, test the limit feature.
2155 $courses = get_user_capability_course($cap, $u3->id, true, '', 'id', 2);
2156 $this->assert_course_ids([$c1->id, $c2->id], $courses);
2159 $courses = get_user_capability_course($cap, $u3->id, true, '', 'shortname');
2160 $this->assert_course_ids([$c3->id, $c2->id, $c1->id], $courses);
2162 // Check returned fields - default.
2163 $courses = get_user_capability_course($cap, $u3->id, true, '', 'id');
2164 $this->assertEquals((object)['id' => $c1->id], $courses[0]);
2166 // Add a selection of fields, including the context ones with special handling.
2167 $courses = get_user_capability_course($cap, $u3->id, true, 'shortname, ctxlevel, ctxdepth, ctxinstance', 'id');
2168 $this->assertEquals((object)['id' => $c1->id, 'shortname' => 'Z', 'ctxlevel' => 50,
2169 'ctxdepth' => 3, 'ctxinstance' => $c1->id], $courses[0]);
2171 // Test front page role - user 1 has no roles, but if we change the front page role
2172 // definition so that it has our capability, then they should see the front page course.
2174 assign_capability($cap, CAP_ALLOW, $CFG->defaultfrontpageroleid, $systemcontext->id);
2175 $courses = get_user_capability_course($cap, $u1->id, true, '', 'id');
2176 $this->assert_course_ids([SITEID, $c1->id], $courses);
2178 // Check that temporary guest access (in this case, given on course 2 for user 1)
2179 // also is included, if it has this capability.
2180 assign_capability($cap, CAP_ALLOW, $CFG->guestroleid, $systemcontext->id);
2181 $this->setUser($u1);
2182 load_temp_course_role(context_course::instance($c2->id), $CFG->guestroleid);
2183 $courses = get_user_capability_course($cap, $USER->id, true, '', 'id');
2184 $this->assert_course_ids([SITEID, $c1->id, $c2->id], $courses);
2188 * Extracts an array of course ids to make the above test script shorter.
2190 * @param int[] $expected Array of expected course ids
2191 * @param stdClass[] $courses Array of course objects
2193 protected function assert_course_ids(array $expected, array $courses) {
2194 $courseids = array_map(function($c) {
2197 $this->assertEquals($expected, $courseids);
2201 * Test if course creator future capability lookup works.
2203 public function test_guess_if_creator_will_have_course_capability() {
2204 global $DB, $CFG, $USER;
2206 $this->resetAfterTest();
2208 $category = $this->getDataGenerator()->create_category();
2209 $course = $this->getDataGenerator()->create_course(array('category'=>$category->id));
2211 $syscontext = context_system::instance();
2212 $categorycontext = context_coursecat::instance($category->id);
2213 $coursecontext = context_course::instance($course->id);
2214 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
2215 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
2216 $creatorrole = $DB->get_record('role', array('shortname'=>'coursecreator'), '*', MUST_EXIST);
2217 $managerrole = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
2219 $this->assertEquals($teacherrole->id, $CFG->creatornewroleid);
2221 $creator = $this->getDataGenerator()->create_user();
2222 $manager = $this->getDataGenerator()->create_user();
2223 role_assign($managerrole->id, $manager->id, $categorycontext);
2225 $this->assertFalse(has_capability('moodle/course:view', $categorycontext, $creator));
2226 $this->assertFalse(has_capability('moodle/role:assign', $categorycontext, $creator));
2227 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, $creator));
2228 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, $creator));
2229 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $creator));
2230 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $creator));
2232 $this->assertTrue(has_capability('moodle/role:assign', $categorycontext, $manager));
2233 $this->assertTrue(has_capability('moodle/course:visibility', $categorycontext, $manager));
2234 $this->assertTrue(has_capability('moodle/course:visibility', $coursecontext, $manager));
2235 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $manager->id));
2236 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $manager->id));
2238 $this->assertEquals(0, $USER->id);
2239 $this->assertFalse(has_capability('moodle/course:view', $categorycontext));
2240 $this->assertFalse(has_capability('moodle/role:assign', $categorycontext));
2241 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext));
2242 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext));
2243 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext));
2244 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext));
2246 $this->setUser($manager);
2247 $this->assertTrue(has_capability('moodle/role:assign', $categorycontext));
2248 $this->assertTrue(has_capability('moodle/course:visibility', $categorycontext));
2249 $this->assertTrue(has_capability('moodle/course:visibility', $coursecontext));
2250 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext));
2251 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext));
2253 $this->setAdminUser();
2254 $this->assertTrue(has_capability('moodle/role:assign', $categorycontext));
2255 $this->assertTrue(has_capability('moodle/course:visibility', $categorycontext));
2256 $this->assertTrue(has_capability('moodle/course:visibility', $coursecontext));
2257 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext));
2258 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext));
2261 role_assign($creatorrole->id, $creator->id, $categorycontext);
2263 $this->assertFalse(has_capability('moodle/role:assign', $categorycontext, $creator));
2264 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, $creator));
2265 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, $creator));
2266 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $creator));
2267 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $creator));
2269 $this->setUser($creator);
2270 $this->assertFalse(has_capability('moodle/role:assign', $categorycontext, null));
2271 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, null));
2272 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, null));
2273 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, null));
2274 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, null));
2277 set_config('creatornewroleid', $studentrole->id);
2279 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, $creator));
2280 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, $creator));
2281 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $creator));
2282 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $creator));
2284 set_config('creatornewroleid', $teacherrole->id);
2286 role_change_permission($managerrole->id, $categorycontext, 'moodle/course:visibility', CAP_PREVENT);
2287 role_assign($creatorrole->id, $manager->id, $categorycontext);
2289 $this->assertTrue(has_capability('moodle/course:view', $categorycontext, $manager));
2290 $this->assertTrue(has_capability('moodle/course:view', $coursecontext, $manager));
2291 $this->assertTrue(has_capability('moodle/role:assign', $categorycontext, $manager));
2292 $this->assertTrue(has_capability('moodle/role:assign', $coursecontext, $manager));
2293 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, $manager));
2294 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, $manager));
2295 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $manager));
2296 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $manager));
2298 role_change_permission($managerrole->id, $categorycontext, 'moodle/course:view', CAP_PREVENT);
2299 $this->assertTrue(has_capability('moodle/role:assign', $categorycontext, $manager));
2300 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, $manager));
2301 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, $manager));
2302 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $manager));
2303 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $manager));
2305 $this->getDataGenerator()->enrol_user($manager->id, $course->id, 0);
2307 $this->assertTrue(has_capability('moodle/role:assign', $categorycontext, $manager));
2308 $this->assertTrue(has_capability('moodle/role:assign', $coursecontext, $manager));
2309 $this->assertTrue(is_enrolled($coursecontext, $manager));
2310 $this->assertFalse(has_capability('moodle/course:visibility', $categorycontext, $manager));
2311 $this->assertFalse(has_capability('moodle/course:visibility', $coursecontext, $manager));
2312 $this->assertTrue(guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext, $manager));
2313 $this->assertFalse(guess_if_creator_will_have_course_capability('moodle/course:visibility', $coursecontext, $manager));
2318 guess_if_creator_will_have_course_capability('moodle/course:visibility', $syscontext, $creator);
2319 $this->fail('Exception expected when non course/category context passed to guess_if_creator_will_have_course_capability()');
2320 } catch (moodle_exception $e) {
2321 $this->assertInstanceOf('coding_exception', $e);
2326 * Test require_capability() exceptions.
2328 public function test_require_capability() {
2329 $this->resetAfterTest();
2331 $syscontext = context_system::instance();
2334 $this->assertFalse(has_capability('moodle/site:config', $syscontext));
2336 require_capability('moodle/site:config', $syscontext);
2337 $this->fail('Exception expected from require_capability()');
2338 } catch (moodle_exception $e) {
2339 $this->assertInstanceOf('required_capability_exception', $e);
2341 $this->setAdminUser();
2342 $this->assertFalse(has_capability('moodle/site:config', $syscontext, 0));
2344 require_capability('moodle/site:config', $syscontext, 0);
2345 $this->fail('Exception expected from require_capability()');
2346 } catch (moodle_exception $e) {
2347 $this->assertInstanceOf('required_capability_exception', $e);
2349 $this->assertFalse(has_capability('moodle/site:config', $syscontext, null, false));
2351 require_capability('moodle/site:config', $syscontext, null, false);
2352 $this->fail('Exception expected from require_capability()');
2353 } catch (moodle_exception $e) {
2354 $this->assertInstanceOf('required_capability_exception', $e);
2359 * Test that enrolled users SQL does not return any values for users in
2362 public function test_get_enrolled_sql_different_course() {
2365 $this->resetAfterTest();
2367 $course = $this->getDataGenerator()->create_course();
2368 $context = context_course::instance($course->id);
2369 $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
2370 $user = $this->getDataGenerator()->create_user();
2372 // This user should not appear anywhere, we're not interested in that context.
2373 $course2 = $this->getDataGenerator()->create_course();
2374 $this->getDataGenerator()->enrol_user($user->id, $course2->id, $student->id);
2376 $enrolled = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, false);
2377 $active = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, true);
2378 $suspended = get_suspended_userids($context);
2380 $this->assertFalse(isset($enrolled[$user->id]));
2381 $this->assertFalse(isset($active[$user->id]));
2382 $this->assertFalse(isset($suspended[$user->id]));
2383 $this->assertCount(0, $enrolled);
2384 $this->assertCount(0, $active);
2385 $this->assertCount(0, $suspended);
2389 * Test that enrolled users SQL does not return any values for role
2390 * assignments without an enrolment.
2392 public function test_get_enrolled_sql_role_only() {
2395 $this->resetAfterTest();
2397 $course = $this->getDataGenerator()->create_course();
2398 $context = context_course::instance($course->id);
2399 $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
2400 $user = $this->getDataGenerator()->create_user();
2402 // Role assignment is not the same as course enrollment.
2403 role_assign($student->id, $user->id, $context->id);
2405 $enrolled = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, false);
2406 $active = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, true);
2407 $suspended = get_suspended_userids($context);
2409 $this->assertFalse(isset($enrolled[$user->id]));
2410 $this->assertFalse(isset($active[$user->id]));
2411 $this->assertFalse(isset($suspended[$user->id]));
2412 $this->assertCount(0, $enrolled);
2413 $this->assertCount(0, $active);
2414 $this->assertCount(0, $suspended);
2418 * Test that multiple enrolments for the same user are counted correctly.
2420 public function test_get_enrolled_sql_multiple_enrolments() {
2423 $this->resetAfterTest();
2425 $course = $this->getDataGenerator()->create_course();
2426 $context = context_course::instance($course->id);
2427 $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
2428 $user = $this->getDataGenerator()->create_user();
2430 // Add a suspended enrol.
2431 $selfinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'self'));
2432 $selfplugin = enrol_get_plugin('self');
2433 $selfplugin->update_status($selfinstance, ENROL_INSTANCE_ENABLED);
2434 $this->getDataGenerator()->enrol_user($user->id, $course->id, $student->id, 'self', 0, 0, ENROL_USER_SUSPENDED);
2436 // Should be enrolled, but not active - user is suspended.
2437 $enrolled = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, false);
2438 $active = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, true);
2439 $suspended = get_suspended_userids($context);
2441 $this->assertTrue(isset($enrolled[$user->id]));
2442 $this->assertFalse(isset($active[$user->id]));
2443 $this->assertTrue(isset($suspended[$user->id]));
2444 $this->assertCount(1, $enrolled);
2445 $this->assertCount(0, $active);
2446 $this->assertCount(1, $suspended);
2448 // Add an active enrol for the user. Any active enrol makes them enrolled.
2449 $this->getDataGenerator()->enrol_user($user->id, $course->id, $student->id);
2451 // User should be active now.
2452 $enrolled = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, false);
2453 $active = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, true);
2454 $suspended = get_suspended_userids($context);
2456 $this->assertTrue(isset($enrolled[$user->id]));
2457 $this->assertTrue(isset($active[$user->id]));
2458 $this->assertFalse(isset($suspended[$user->id]));
2459 $this->assertCount(1, $enrolled);
2460 $this->assertCount(1, $active);
2461 $this->assertCount(0, $suspended);
2466 * Test that enrolled users SQL does not return any values for users
2467 * without a group when $context is not a valid course context.
2469 public function test_get_enrolled_sql_userswithoutgroup() {
2472 $this->resetAfterTest();
2474 $systemcontext = context_system::instance();
2475 $course = $this->getDataGenerator()->create_course();
2476 $coursecontext = context_course::instance($course->id);
2477 $user1 = $this->getDataGenerator()->create_user();
2478 $user2 = $this->getDataGenerator()->create_user();
2480 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
2481 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
2483 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
2484 groups_add_member($group, $user1);
2486 $enrolled = get_enrolled_users($coursecontext);
2487 $this->assertCount(2, $enrolled);
2489 // Get users without any group on the course context.
2490 $enrolledwithoutgroup = get_enrolled_users($coursecontext, '', USERSWITHOUTGROUP);
2491 $this->assertCount(1, $enrolledwithoutgroup);
2492 $this->assertFalse(isset($enrolledwithoutgroup[$user1->id]));
2494 // Get users without any group on the system context (it should throw an exception).
2495 $this->expectException('coding_exception');
2496 get_enrolled_users($systemcontext, '', USERSWITHOUTGROUP);
2499 public function get_enrolled_sql_provider() {
2502 // Two users who are enrolled.
2520 // A user who is suspended.
2523 'status' => ENROL_USER_SUSPENDED,
2525 'suspended' => true,
2542 'status' => ENROL_USER_SUSPENDED,
2544 'suspended' => true,
2554 // One user who is not yet enrolled.
2557 'timestart' => DAYSECS,
2560 'suspended' => true,
2570 // One user who is no longer enrolled
2573 'timeend' => -DAYSECS,
2576 'suspended' => true,
2586 // One user who is not yet enrolled, and one who is no longer enrolled.
2589 'timeend' => -DAYSECS,
2592 'suspended' => true,
2595 'timestart' => DAYSECS,
2598 'suspended' => true,
2611 * @dataProvider get_enrolled_sql_provider
2613 public function test_get_enrolled_sql_course($users, $counts) {
2616 $this->resetAfterTest();
2618 $course = $this->getDataGenerator()->create_course();
2619 $context = context_course::instance($course->id);
2620 $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
2621 $createdusers = array();
2623 foreach ($users as &$userdata) {
2624 $user = $this->getDataGenerator()->create_user();
2625 $userdata['id'] = $user->id;
2630 if (isset($userdata['timestart'])) {
2631 $timestart = time() + $userdata['timestart'];
2633 if (isset($userdata['timeend'])) {
2634 $timeend = time() + $userdata['timeend'];
2636 if (isset($userdata['status'])) {
2637 $status = $userdata['status'];
2640 // Enrol the user in the course.
2641 $this->getDataGenerator()->enrol_user($user->id, $course->id, $student->id, 'manual', $timestart, $timeend, $status);
2644 // After all users have been enroled, check expectations.
2645 $enrolled = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, false);
2646 $active = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, true);
2647 $suspended = get_suspended_userids($context);
2649 foreach ($users as $userdata) {
2650 if (isset($userdata['enrolled']) && $userdata['enrolled']) {
2651 $this->assertTrue(isset($enrolled[$userdata['id']]));
2653 $this->assertFalse(isset($enrolled[$userdata['id']]));
2656 if (isset($userdata['active']) && $userdata['active']) {
2657 $this->assertTrue(isset($active[$userdata['id']]));
2659 $this->assertFalse(isset($active[$userdata['id']]));
2662 if (isset($userdata['suspended']) && $userdata['suspended']) {
2663 $this->assertTrue(isset($suspended[$userdata['id']]));
2665 $this->assertFalse(isset($suspended[$userdata['id']]));
2669 $this->assertCount($counts['enrolled'], $enrolled);
2670 $this->assertCount($counts['active'], $active);
2671 $this->assertCount($counts['suspended'], $suspended);
2675 * A small functional test of permission evaluations.
2677 public function test_permission_evaluation() {
2678 global $USER, $SITE, $CFG, $DB, $ACCESSLIB_PRIVATE;
2680 $this->resetAfterTest();
2682 $generator = $this->getDataGenerator();
2684 // Fill the site with some real data.
2685 $testcategories = array();
2686 $testcourses = array();
2687 $testpages = array();
2688 $testblocks = array();
2689 $allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id');
2691 $systemcontext = context_system::instance();
2692 $frontpagecontext = context_course::instance(SITEID);
2694 // Add block to system context.
2695 $bi = $generator->create_block('online_users');
2696 context_block::instance($bi->id);
2697 $testblocks[] = $bi->id;
2700 $testusers = array();
2701 for ($i=0; $i<20; $i++) {
2702 $user = $generator->create_user();
2703 $testusers[$i] = $user->id;
2704 $usercontext = context_user::instance($user->id);
2706 // Add block to user profile.
2707 $bi = $generator->create_block('online_users', array('parentcontextid'=>$usercontext->id));
2708 $testblocks[] = $bi->id;
2710 // Deleted user - should be ignored everywhere, can not have context.
2711 $generator->create_user(array('deleted'=>1));
2713 // Add block to frontpage.
2714 $bi = $generator->create_block('online_users', array('parentcontextid'=>$frontpagecontext->id));
2715 $frontpageblockcontext = context_block::instance($bi->id);
2716 $testblocks[] = $bi->id;
2718 // Add a resource to frontpage.
2719 $page = $generator->create_module('page', array('course'=>$SITE->id));
2720 $testpages[] = $page->cmid;
2721 $frontpagepagecontext = context_module::instance($page->cmid);
2723 // Add block to frontpage resource.
2724 $bi = $generator->create_block('online_users', array('parentcontextid'=>$frontpagepagecontext->id));
2725 $frontpagepageblockcontext = context_block::instance($bi->id);
2726 $testblocks[] = $bi->id;
2728 // Some nested course categories with courses.
2729 $manualenrol = enrol_get_plugin('manual');
2731 for ($i=0; $i<5; $i++) {
2732 $cat = $generator->create_category(array('parent'=>$parentcat));
2733 $testcategories[] = $cat->id;
2734 $catcontext = context_coursecat::instance($cat->id);
2735 $parentcat = $cat->id;
2741 // Add resource to each category.
2742 $bi = $generator->create_block('online_users', array('parentcontextid'=>$catcontext->id));
2743 context_block::instance($bi->id);
2745 // Add a few courses to each category.
2746 for ($j=0; $j<6; $j++) {
2747 $course = $generator->create_course(array('category'=>$cat->id));
2748 $testcourses[] = $course->id;
2749 $coursecontext = context_course::instance($course->id);
2754 // Add manual enrol instance.
2755 $manualenrol->add_default_instance($DB->get_record('course', array('id'=>$course->id)));
2757 // Add block to each course.
2758 $bi = $generator->create_block('online_users', array('parentcontextid'=>$coursecontext->id));
2759 $testblocks[] = $bi->id;
2761 // Add a resource to each course.
2762 $page = $generator->create_module('page', array('course'=>$course->id));
2763 $testpages[] = $page->cmid;
2764 $modcontext = context_module::instance($page->cmid);
2766 // Add block to each module.
2767 $bi = $generator->create_block('online_users', array('parentcontextid'=>$modcontext->id));
2768 $testblocks[] = $bi->id;
2772 // Make sure all contexts were created properly.
2773 $count = 1; // System.
2774 $count += $DB->count_records('user', array('deleted'=>0));
2775 $count += $DB->count_records('course_categories');
2776 $count += $DB->count_records('course');
2777 $count += $DB->count_records('course_modules');
2778 $count += $DB->count_records('block_instances');
2779 $this->assertEquals($count, $DB->count_records('context'));
2780 $this->assertEquals(0, $DB->count_records('context', array('depth'=>0)));
2781 $this->assertEquals(0, $DB->count_records('context', array('path'=>null)));
2784 // Test context_helper::get_level_name() method.
2786 $levels = context_helper::get_all_levels();
2787 foreach ($levels as $level => $classname) {
2788 $name = context_helper::get_level_name($level);
2789 $this->assertNotEmpty($name);
2793 // Test context::instance_by_id(), context_xxx::instance() methods.
2795 $context = context::instance_by_id($frontpagecontext->id);
2796 $this->assertSame(CONTEXT_COURSE, $context->contextlevel);
2797 $this->assertFalse(context::instance_by_id(-1, IGNORE_MISSING));
2799 context::instance_by_id(-1);
2800 $this->fail('exception expected');
2801 } catch (moodle_exception $e) {
2802 $this->assertTrue(true);
2804 $this->assertInstanceOf('context_system', context_system::instance());
2805 $this->assertInstanceOf('context_coursecat', context_coursecat::instance($testcategories[0]));
2806 $this->assertInstanceOf('context_course', context_course::instance($testcourses[0]));
2807 $this->assertInstanceOf('context_module', context_module::instance($testpages[0]));
2808 $this->assertInstanceOf('context_block', context_block::instance($testblocks[0]));
2810 $this->assertFalse(context_coursecat::instance(-1, IGNORE_MISSING));
2811 $this->assertFalse(context_course::instance(-1, IGNORE_MISSING));
2812 $this->assertFalse(context_module::instance(-1, IGNORE_MISSING));
2813 $this->assertFalse(context_block::instance(-1, IGNORE_MISSING));
2815 context_coursecat::instance(-1);
2816 $this->fail('exception expected');
2817 } catch (moodle_exception $e) {
2818 $this->assertTrue(true);
2821 context_course::instance(-1);
2822 $this->fail('exception expected');
2823 } catch (moodle_exception $e) {
2824 $this->assertTrue(true);
2827 context_module::instance(-1);
2828 $this->fail('exception expected');
2829 } catch (moodle_exception $e) {
2830 $this->assertTrue(true);
2833 context_block::instance(-1);
2834 $this->fail('exception expected');
2835 } catch (moodle_exception $e) {
2836 $this->assertTrue(true);
2840 // Test $context->get_url(), $context->get_context_name(), $context->get_capabilities() methods.
2842 $testcontexts = array();
2843 $testcontexts[CONTEXT_SYSTEM] = context_system::instance();
2844 $testcontexts[CONTEXT_COURSECAT] = context_coursecat::instance($testcategories[0]);
2845 $testcontexts[CONTEXT_COURSE] = context_course::instance($testcourses[0]);
2846 $testcontexts[CONTEXT_MODULE] = context_module::instance($testpages[0]);
2847 $testcontexts[CONTEXT_BLOCK] = context_block::instance($testblocks[0]);
2849 foreach ($testcontexts as $context) {
2850 $name = $context->get_context_name(true, true);
2851 $this->assertNotEmpty($name);
2853 $this->assertInstanceOf('moodle_url', $context->get_url());
2855 $caps = $context->get_capabilities();
2856 $this->assertTrue(is_array($caps));
2857 foreach ($caps as $cap) {
2859 $this->assertSame(array_keys($cap), array('id', 'name', 'captype', 'contextlevel', 'component', 'riskbitmask'));
2862 unset($testcontexts);
2864 // Test $context->get_course_context() method.
2866 $this->assertFalse($systemcontext->get_course_context(false));
2868 $systemcontext->get_course_context();
2869 $this->fail('exception expected');
2870 } catch (moodle_exception $e) {
2871 $this->assertInstanceOf('coding_exception', $e);
2873 $context = context_coursecat::instance($testcategories[0]);
2874 $this->assertFalse($context->get_course_context(false));
2876 $context->get_course_context();
2877 $this->fail('exception expected');
2878 } catch (moodle_exception $e) {
2879 $this->assertInstanceOf('coding_exception', $e);
2881 $this->assertEquals($frontpagecontext, $frontpagecontext->get_course_context(true));
2882 $this->assertEquals($frontpagecontext, $frontpagepagecontext->get_course_context(true));
2883 $this->assertEquals($frontpagecontext, $frontpagepageblockcontext->get_course_context(true));
2886 // Test $context->get_parent_context(), $context->get_parent_contexts(), $context->get_parent_context_ids() methods.
2888 $userid = reset($testusers);
2889 $usercontext = context_user::instance($userid);
2890 $this->assertEquals($systemcontext, $usercontext->get_parent_context());
2891 $this->assertEquals(array($systemcontext->id=>$systemcontext), $usercontext->get_parent_contexts());
2892 $this->assertEquals(array($usercontext->id=>$usercontext, $systemcontext->id=>$systemcontext), $usercontext->get_parent_contexts(true));
2894 $this->assertEquals(array(), $systemcontext->get_parent_contexts());
2895 $this->assertEquals(array($systemcontext->id=>$systemcontext), $systemcontext->get_parent_contexts(true));
2896 $this->assertEquals(array(), $systemcontext->get_parent_context_ids());
2897 $this->assertEquals(array($systemcontext->id), $systemcontext->get_parent_context_ids(true));
2898 $this->assertEquals(array(), $systemcontext->get_parent_context_paths());
2899 $this->assertEquals(array($systemcontext->id => $systemcontext->path), $systemcontext->get_parent_context_paths(true));
2901 $this->assertEquals($systemcontext, $frontpagecontext->get_parent_context());
2902 $this->assertEquals(array($systemcontext->id=>$systemcontext), $frontpagecontext->get_parent_contexts());
2903 $this->assertEquals(array($frontpagecontext->id=>$frontpagecontext, $systemcontext->id=>$systemcontext), $frontpagecontext->get_parent_contexts(true));
2904 $this->assertEquals(array($systemcontext->id), $frontpagecontext->get_parent_context_ids());
2905 $this->assertEquals(array($frontpagecontext->id, $systemcontext->id), $frontpagecontext->get_parent_context_ids(true));
2906 $this->assertEquals(array($systemcontext->id => $systemcontext->path), $frontpagecontext->get_parent_context_paths());
2907 $expected = array($systemcontext->id => $systemcontext->path, $frontpagecontext->id => $frontpagecontext->path);
2908 $this->assertEquals($expected, $frontpagecontext->get_parent_context_paths(true));
2910 $this->assertFalse($systemcontext->get_parent_context());
2911 $frontpagecontext = context_course::instance($SITE->id);
2912 $parent = $systemcontext;
2913 foreach ($testcategories as $catid) {
2914 $catcontext = context_coursecat::instance($catid);
2915 $this->assertEquals($parent, $catcontext->get_parent_context());
2916 $parent = $catcontext;
2918 $this->assertEquals($frontpagecontext, $frontpagepagecontext->get_parent_context());
2919 $this->assertEquals($frontpagecontext, $frontpageblockcontext->get_parent_context());
2920 $this->assertEquals($frontpagepagecontext, $frontpagepageblockcontext->get_parent_context());
2923 // Test $context->get_child_contexts() method.
2925 $children = $systemcontext->get_child_contexts();
2926 $this->resetDebugging();
2927 $this->assertEquals(count($children)+1, $DB->count_records('context'));
2929 $context = context_coursecat::instance($testcategories[3]);
2930 $children = $context->get_child_contexts();
2934 foreach ($children as $child) {
2935 if ($child->contextlevel == CONTEXT_COURSECAT) {
2938 if ($child->contextlevel == CONTEXT_COURSE) {
2941 if ($child->contextlevel == CONTEXT_BLOCK) {
2945 $this->assertCount(8, $children);
2946 $this->assertEquals(1, $countcats);
2947 $this->assertEquals(6, $countcourses);
2948 $this->assertEquals(1, $countblocks);
2950 $context = context_course::instance($testcourses[2]);
2951 $children = $context->get_child_contexts();
2953 $context = context_module::instance($testpages[3]);
2954 $children = $context->get_child_contexts();
2955 $this->assertCount(1, $children);
2957 $context = context_block::instance($testblocks[1]);
2958 $children = $context->get_child_contexts();
2959 $this->assertCount(0, $children);
2963 unset($countcourses);
2964 unset($countblocks);
2967 // Test context_helper::reset_caches() method.
2969 context_helper::reset_caches();
2970 $this->assertEquals(0, context_inspection::test_context_cache_size());
2971 context_course::instance($SITE->id);
2972 $this->assertEquals(1, context_inspection::test_context_cache_size());
2975 // Test context preloading.
2977 context_helper::reset_caches();
2978 $sql = "SELECT ".context_helper::get_preload_record_columns_sql('c')."
2980 WHERE c.contextlevel <> ".CONTEXT_SYSTEM;
2981 $records = $DB->get_records_sql($sql);
2982 $firstrecord = reset($records);
2983 $columns = context_helper::get_preload_record_columns('c');
2984 $firstrecord = (array)$firstrecord;
2985 $this->assertSame(array_keys($firstrecord), array_values($columns));
2986 context_helper::reset_caches();
2987 foreach ($records as $record) {
2988 context_helper::preload_from_record($record);
2989 $this->assertEquals(new stdClass(), $record);
2991 $this->assertEquals(count($records), context_inspection::test_context_cache_size());
2995 context_helper::reset_caches();
2996 context_helper::preload_course($SITE->id);
2997 $numfrontpagemodules = $DB->count_records('course_modules', array('course' => $SITE->id));
2998 $this->assertEquals(3 + $numfrontpagemodules, context_inspection::test_context_cache_size()); // Depends on number of default blocks.
3000 // Test assign_capability(), unassign_capability() functions.
3002 $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
3003 $this->assertFalse($rc);
3004 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext->id);
3005 $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
3006 $this->assertEquals(CAP_ALLOW, $rc->permission);
3007 assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext->id);
3008 $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
3009 $this->assertEquals(CAP_ALLOW, $rc->permission);
3010 assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext, true);
3011 $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
3012 $this->assertEquals(CAP_PREVENT, $rc->permission);
3014 assign_capability('moodle/site:accessallgroups', CAP_INHERIT, $allroles['teacher'], $frontpagecontext);
3015 $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
3016 $this->assertFalse($rc);
3017 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext);
3018 unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext, true);
3019 $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
3020 $this->assertFalse($rc);
3021 unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext->id, true);
3024 accesslib_clear_all_caches_for_unit_testing(); // Must be done after assign_capability().
3027 // Test role_assign(), role_unassign(), role_unassign_all() functions.
3029 $context = context_course::instance($testcourses[1]);
3030 $this->assertEquals(0, $DB->count_records('role_assignments', array('contextid'=>$context->id)));
3031 role_assign($allroles['teacher'], $testusers[1], $context->id);
3032 role_assign($allroles['teacher'], $testusers[2], $context->id);
3033 role_assign($allroles['manager'], $testusers[1], $context->id);
3034 $this->assertEquals(3, $DB->count_records('role_assignments', array('contextid'=>$context->id)));
3035 role_unassign($allroles['teacher'], $testusers[1], $context->id);
3036 $this->assertEquals(2, $DB->count_records('role_assignments', array('contextid'=>$context->id)));
3037 role_unassign_all(array('contextid'=>$context->id));
3038 $this->assertEquals(0, $DB->count_records('role_assignments', array('contextid'=>$context->id)));
3041 accesslib_clear_all_caches_for_unit_testing(); // Just in case.
3044 // Test has_capability(), get_users_by_capability(), role_switch(), reload_all_capabilities() and friends functions.
3046 $adminid = get_admin()->id;
3047 $guestid = $CFG->siteguest;
3049 // Enrol some users into some courses.
3050 $course1 = $DB->get_record('course', array('id'=>$testcourses[22]), '*', MUST_EXIST);
3051 $course2 = $DB->get_record('course', array('id'=>$testcourses[7]), '*', MUST_EXIST);
3052 $cms = $DB->get_records('course_modules', array('course'=>$course1->id), 'id');
3054 $blocks = $DB->get_records('block_instances', array('parentcontextid'=>context_module::instance($cm1->id)->id), 'id');
3055 $block1 = reset($blocks);
3056 $instance1 = $DB->get_record('enrol', array('enrol'=>'manual', 'courseid'=>$course1->id));
3057 $instance2 = $DB->get_record('enrol', array('enrol'=>'manual', 'courseid'=>$course2->id));
3058 for ($i=0; $i<9; $i++) {
3059 $manualenrol->enrol_user($instance1, $testusers[$i], $allroles['student']);
3061 $manualenrol->enrol_user($instance1, $testusers[8], $allroles['teacher']);
3062 $manualenrol->enrol_user($instance1, $testusers[9], $allroles['editingteacher']);
3064 for ($i=10; $i<15; $i++) {
3065 $manualenrol->enrol_user($instance2, $testusers[$i], $allroles['student']);
3067 $manualenrol->enrol_user($instance2, $testusers[15], $allroles['editingteacher']);
3069 // Add tons of role assignments - the more the better.
3070 role_assign($allroles['coursecreator'], $testusers[11], context_coursecat::instance($testcategories[2]));
3071 role_assign($allroles['manager'], $testusers[12], context_coursecat::instance($testcategories[1]));
3072 role_assign($allroles['student'], $testusers[9], context_module::instance($cm1->id));
3073 role_assign($allroles['teacher'], $testusers[8], context_module::instance($cm1->id));
3074 role_assign($allroles['guest'], $testusers[13], context_course::instance($course1->id));
3075 role_assign($allroles['teacher'], $testusers[7], context_block::instance($block1->id));
3076 role_assign($allroles['manager'], $testusers[9], context_block::instance($block1->id));
3077 role_assign($allroles['editingteacher'], $testusers[9], context_course::instance($course1->id));
3079 role_assign($allroles['teacher'], $adminid, context_course::instance($course1->id));
3080 role_assign($allroles['editingteacher'], $adminid, context_block::instance($block1->id));
3082 // Add tons of overrides - the more the better.
3083 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpageblockcontext, true);
3084 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpageblockcontext, true);
3085 assign_capability('moodle/block:view', CAP_PROHIBIT, $allroles['guest'], $frontpageblockcontext, true);
3086 assign_capability('block/online_users:viewlist', CAP_PREVENT, $allroles['user'], $frontpageblockcontext, true);
3087 assign_capability('block/online_users:viewlist', CAP_PREVENT, $allroles['student'], $frontpageblockcontext, true);
3089 assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $CFG->defaultuserroleid, $frontpagepagecontext, true);
3090 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagepagecontext, true);
3091 assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $frontpagepagecontext, true);
3092 assign_capability('mod/page:view', CAP_ALLOW, $allroles['user'], $frontpagepagecontext, true);
3093 assign_capability('mod/page:view', CAP_ALLOW, $allroles['student'], $frontpagepagecontext, true);
3095 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpagecontext, true);
3096 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagecontext, true);
3097 assign_capability('mod/page:view', CAP_ALLOW, $allroles['guest'], $frontpagecontext, true);
3098 assign_capability('mod/page:view', CAP_PROHIBIT, $allroles['user'], $frontpagecontext, true);
3100 assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $systemcontext, true);
3102 // Prepare for prohibit test.
3103 role_assign($allroles['editingteacher'], $testusers[19], context_system::instance());
3104 role_assign($allroles['teacher'], $testusers[19], context_course::instance($testcourses[17]));
3105 role_assign($allroles['editingteacher'], $testusers[19], context_course::instance($testcourses[17]));
3106 assign_capability('moodle/course:update', CAP_PROHIBIT, $allroles['teacher'], context_course::instance($testcourses[17]), true);
3108 accesslib_clear_all_caches_for_unit_testing(); /// Must be done after assign_capability().
3110 // Extra tests for guests and not-logged-in users because they can not be verified by cross checking
3111 // with get_users_by_capability() where they are ignored.
3112 $this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, $guestid));
3113 $this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, $guestid));
3114 $this->assertTrue(has_capability('mod/page:view', $frontpagecontext, $guestid));
3115 $this->assertFalse(has_capability('mod/page:view', $systemcontext, $guestid));
3117 $this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, 0));
3118 $this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, 0));
3119 $this->assertTrue(has_capability('mod/page:view', $frontpagecontext, 0));
3120 $this->assertFalse(has_capability('mod/page:view', $systemcontext, 0));
3122 $this->assertFalse(has_capability('moodle/course:create', $systemcontext, $testusers[11]));
3123 $this->assertTrue(has_capability('moodle/course:create', context_coursecat::instance($testcategories[2]), $testusers[11]));
3124 $this->assertFalse(has_capability('moodle/course:create', context_course::instance($testcourses[1]), $testusers[11]));
3125 $this->assertTrue(has_capability('moodle/course:create', context_course::instance($testcourses[19]), $testusers[11]));
3127 $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[1]), $testusers[9]));
3128 $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[19]), $testusers[9]));
3129 $this->assertFalse(has_capability('moodle/course:update', $systemcontext, $testusers[9]));
3132 $this->assertTrue(has_capability('moodle/course:update', context_system::instance(), $testusers[19]));
3133 $ids = get_users_by_capability(context_system::instance(), 'moodle/course:update', 'u.id');
3134 $this->assertArrayHasKey($testusers[19], $ids);
3135 $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[17]), $testusers[19]));
3136 $ids = get_users_by_capability(context_course::instance($testcourses[17]), 'moodle/course:update', 'u.id');
3137 $this->assertArrayNotHasKey($testusers[19], $ids);
3139 // Test the list of enrolled users.
3140 $coursecontext = context_course::instance($course1->id);
3141 $enrolled = get_enrolled_users($coursecontext);
3142 $this->assertCount(10, $enrolled);
3143 for ($i=0; $i<10; $i++) {
3144 $this->assertTrue(isset($enrolled[$testusers[$i]]));
3146 $enrolled = get_enrolled_users($coursecontext, 'moodle/course:update');
3147 $this->assertCount(1, $enrolled);
3148 $this->assertTrue(isset($enrolled[$testusers[9]]));
3152 $userid = $testusers[9];
3153 $USER = $DB->get_record('user', array('id'=>$userid));
3154 load_all_capabilities();
3155 $coursecontext = context_course::instance($course1->id);
3156 $this->assertTrue(has_capability('moodle/course:update', $coursecontext));
3157 $this->assertFalse(is_role_switched($course1->id));
3158 role_switch($allroles['student'], $coursecontext);
3159 $this->assertTrue(is_role_switched($course1->id));
3160 $this->assertEquals($allroles['student'], $USER->access['rsw'][$coursecontext->path]);
3161 $this->assertFalse(has_capability('moodle/course:update', $coursecontext));
3162 reload_all_capabilities();
3163 $this->assertFalse(has_capability('moodle/course:update', $coursecontext));
3164 role_switch(0, $coursecontext);
3165 $this->assertTrue(has_capability('moodle/course:update', $coursecontext));
3167 $USER = $DB->get_record('user', array('id'=>$userid));
3168 load_all_capabilities();
3169 $coursecontext = context_course::instance($course1->id);
3170 $blockcontext = context_block::instance($block1->id);
3171 $this->assertTrue(has_capability('moodle/course:update', $blockcontext));
3172 role_switch($allroles['student'], $coursecontext);
3173 $this->assertEquals($allroles['student'], $USER->access['rsw'][$coursecontext->path]);
3174 $this->assertFalse(has_capability('moodle/course:update', $blockcontext));
3175 reload_all_capabilities();
3176 $this->assertFalse(has_capability('moodle/course:update', $blockcontext));
3177 load_all_capabilities();
3178 $this->assertTrue(has_capability('moodle/course:update', $blockcontext));
3180 // Temp course role for enrol.
3181 $DB->delete_records('cache_flags', array()); // This prevents problem with dirty contexts immediately resetting the temp role - this is a known problem...
3182 $userid = $testusers[5];
3183 $roleid = $allroles['editingteacher'];
3184 $USER = $DB->get_record('user', array('id'=>$userid));
3185 load_all_capabilities();
3186 $coursecontext = context_course::instance($course1->id);
3187 $this->assertFalse(has_capability('moodle/course:update', $coursecontext));
3188 $this->assertFalse(isset($USER->access['ra'][$coursecontext->path][$roleid]));
3189 load_temp_course_role($coursecontext, $roleid);
3190 $this->assertEquals($USER->access['ra'][$coursecontext->path][$roleid], $roleid);
3191 $this->assertTrue(has_capability('moodle/course:update', $coursecontext));
3192 remove_temp_course_roles($coursecontext);
3193 $this->assertFalse(has_capability('moodle/course:update', $coursecontext, $userid));
3194 load_temp_course_role($coursecontext, $roleid);
3195 reload_all_capabilities();
3196 $this->assertFalse(has_capability('moodle/course:update', $coursecontext, $userid));
3197 $USER = new stdClass();
3200 // Now cross check has_capability() with get_users_by_capability(), each using different code paths,
3201 // they have to be kept in sync, usually only one of them breaks, so we know when something is wrong,
3202 // at the same time validate extra restrictions (guest read only no risks, admin exception, non existent and deleted users).
3203 $contexts = $DB->get_records('context', array(), 'id');
3204 $contexts = array_values($contexts);
3205 $capabilities = $DB->get_records('capabilities', array(), 'id');
3206 $capabilities = array_values($capabilities);
3207 $roles = array($allroles['guest'], $allroles['user'], $allroles['teacher'], $allroles['editingteacher'], $allroles['coursecreator'], $allroles['manager']);
3208 $userids = array_values($testusers);
3209 $userids[] = get_admin()->id;
3211 if (!PHPUNIT_LONGTEST) {
3212 $contexts = array_slice($contexts, 0, 10);
3213 $capabilities = array_slice($capabilities, 0, 5);
3214 $userids = array_slice($userids, 0, 5);
3217 foreach ($userids as $userid) { // No guest or deleted.
3218 // Each user gets 0-10 random roles.
3219 $rcount = rand(0, 10);
3220 for ($j=0; $j<$rcount; $j++) {
3221 $roleid = $roles[rand(0, count($roles)-1)];
3222 $contextid = $contexts[rand(0, count($contexts)-1)]->id;
3223 role_assign($roleid, $userid, $contextid);
3227 $permissions = array(CAP_ALLOW, CAP_PREVENT, CAP_INHERIT, CAP_PREVENT);
3228 $maxoverrides = count($contexts)*10;
3229 for ($j=0; $j<$maxoverrides; $j++) {
3230 $roleid = $roles[rand(0, count($roles)-1)];
3231 $contextid = $contexts[rand(0, count($contexts)-1)]->id;
3232 $permission = $permissions[rand(0, count($permissions)-1)];
3233 $capname = $capabilities[rand(0, count($capabilities)-1)]->name;
3234 assign_capability($capname, $permission, $roleid, $contextid, true);
3236 unset($permissions);
3239 accesslib_clear_all_caches_for_unit_testing(); // must be done after assign_capability().
3241 // Test time - let's set up some real user, just in case the logic for USER affects the others...
3242 $USER = $DB->get_record('user', array('id'=>$testusers[3]));
3243 load_all_capabilities();
3245 $userids[] = $CFG->siteguest;
3246 $userids[] = 0; // Not-logged-in user.
3247 $userids[] = -1; // Non-existent user.
3249 foreach ($contexts as $crecord) {