/lib/tests/accesslib_test.php
PHP | 2846 lines | 2118 code | 452 blank | 276 comment | 75 complexity | 2d364cea25b1d0ac0a2f662b2a5305a7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- // This file is part of Moodle - http://moodle.org/
- //
- // Moodle is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Moodle is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
- /**
- * Full functional accesslib test.
- *
- * @package core
- * @category phpunit
- * @copyright 2011 Petr Skoda {@link http://skodak.org}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- defined('MOODLE_INTERNAL') || die();
- /**
- * Functional test for accesslib.php
- *
- * Note: execution may take many minutes especially on slower servers.
- */
- class core_accesslib_testcase extends advanced_testcase {
- /**
- * Verify comparison of context instances in phpunit asserts.
- */
- public function test_context_comparisons() {
- $frontpagecontext1 = context_course::instance(SITEID);
- context_helper::reset_caches();
- $frontpagecontext2 = context_course::instance(SITEID);
- $this->assertEquals($frontpagecontext1, $frontpagecontext2);
- $user1 = context_user::instance(1);
- $user2 = context_user::instance(2);
- $this->assertNotEquals($user1, $user2);
- }
- /**
- * Test resetting works.
- */
- public function test_accesslib_clear_all_caches() {
- global $ACCESSLIB_PRIVATE;
- $this->resetAfterTest();
- $this->setAdminUser();
- load_all_capabilities();
- $this->assertNotEmpty($ACCESSLIB_PRIVATE->rolepermissions);
- $this->assertNotEmpty($ACCESSLIB_PRIVATE->rolepermissions);
- $this->assertNotEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
- accesslib_clear_all_caches_for_unit_testing();
- $this->assertEmpty($ACCESSLIB_PRIVATE->rolepermissions);
- $this->assertEmpty($ACCESSLIB_PRIVATE->rolepermissions);
- $this->assertEmpty($ACCESSLIB_PRIVATE->dirtycontexts);
- $this->assertEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
- }
- /**
- * Test getting of role access
- */
- public function test_get_role_access() {
- global $DB;
- $roles = $DB->get_records('role');
- foreach ($roles as $role) {
- $access = get_role_access($role->id);
- $this->assertTrue(is_array($access));
- $this->assertTrue(is_array($access['ra']));
- $this->assertTrue(is_array($access['rdef']));
- $this->assertTrue(isset($access['rdef_count']));
- $this->assertTrue(is_array($access['loaded']));
- $this->assertTrue(isset($access['time']));
- $this->assertTrue(is_array($access['rsw']));
- }
- // Note: the data is validated in the functional permission evaluation test at the end of this testcase.
- }
- /**
- * Test getting of guest role.
- */
- public function test_get_guest_role() {
- global $CFG;
- $guest = get_guest_role();
- $this->assertEquals('guest', $guest->archetype);
- $this->assertEquals('guest', $guest->shortname);
- $this->assertEquals($CFG->guestroleid, $guest->id);
- }
- /**
- * Test if user is admin.
- */
- public function test_is_siteadmin() {
- global $DB, $CFG;
- $this->resetAfterTest();
- $users = $DB->get_records('user');
- foreach ($users as $user) {
- $this->setUser(0);
- if ($user->username === 'admin') {
- $this->assertTrue(is_siteadmin($user));
- $this->assertTrue(is_siteadmin($user->id));
- $this->setUser($user);
- $this->assertTrue(is_siteadmin());
- $this->assertTrue(is_siteadmin(null));
- } else {
- $this->assertFalse(is_siteadmin($user));
- $this->assertFalse(is_siteadmin($user->id));
- $this->setUser($user);
- $this->assertFalse(is_siteadmin());
- $this->assertFalse(is_siteadmin(null));
- }
- }
- // Change the site admin list and check that it still works with
- // multiple admins. We do this with userids only (not real user
- // accounts) because it makes the test simpler.
- $before = $CFG->siteadmins;
- set_config('siteadmins', '666,667,668');
- $this->assertTrue(is_siteadmin(666));
- $this->assertTrue(is_siteadmin(667));
- $this->assertTrue(is_siteadmin(668));
- $this->assertFalse(is_siteadmin(669));
- set_config('siteadmins', '13');
- $this->assertTrue(is_siteadmin(13));
- $this->assertFalse(is_siteadmin(666));
- set_config('siteadmins', $before);
- }
- /**
- * Test if user is enrolled in a course
- */
- public function test_is_enrolled() {
- global $DB;
- $this->resetAfterTest();
- // Generate data.
- $user = $this->getDataGenerator()->create_user();
- $course = $this->getDataGenerator()->create_course();
- $coursecontext = context_course::instance($course->id);
- $role = $DB->get_record('role', array('shortname'=>'student'));
- // There should be a manual enrolment as part of the default install.
- $plugin = enrol_get_plugin('manual');
- $instance = $DB->get_record('enrol', array(
- 'courseid' => $course->id,
- 'enrol' => 'manual',
- ));
- $this->assertNotSame(false, $instance);
- // Enrol the user in the course.
- $plugin->enrol_user($instance, $user->id, $role->id);
- // We'll test with the mod/assign:submit capability.
- $capability= 'mod/assign:submit';
- $this->assertTrue($DB->record_exists('capabilities', array('name' => $capability)));
- // Switch to our user.
- $this->setUser($user);
- // Ensure that the user has the capability first.
- $this->assertTrue(has_capability($capability, $coursecontext, $user->id));
- // We first test whether the user is enrolled on the course as this
- // seeds the cache, then we test for the capability.
- $this->assertTrue(is_enrolled($coursecontext, $user, '', true));
- $this->assertTrue(is_enrolled($coursecontext, $user, $capability));
- // Prevent the capability for this user role.
- assign_capability($capability, CAP_PROHIBIT, $role->id, $coursecontext);
- $coursecontext->mark_dirty();
- $this->assertFalse(has_capability($capability, $coursecontext, $user->id));
- // Again, we seed the cache first by checking initial enrolment,
- // and then we test the actual capability.
- $this->assertTrue(is_enrolled($coursecontext, $user, '', true));
- $this->assertFalse(is_enrolled($coursecontext, $user, $capability));
- }
- /**
- * Test logged in test.
- */
- public function test_isloggedin() {
- global $USER;
- $this->resetAfterTest();
- $USER->id = 0;
- $this->assertFalse(isloggedin());
- $USER->id = 1;
- $this->assertTrue(isloggedin());
- }
- /**
- * Test guest user test.
- */
- public function test_isguestuser() {
- global $DB;
- $this->resetAfterTest();
- $guest = $DB->get_record('user', array('username'=>'guest'));
- $this->setUser(0);
- $this->assertFalse(isguestuser());
- $this->setAdminUser();
- $this->assertFalse(isguestuser());
- $this->assertTrue(isguestuser($guest));
- $this->assertTrue(isguestuser($guest->id));
- $this->setUser($guest);
- $this->assertTrue(isguestuser());
- $users = $DB->get_records('user');
- foreach ($users as $user) {
- if ($user->username === 'guest') {
- continue;
- }
- $this->assertFalse(isguestuser($user));
- }
- }
- /**
- * Test capability riskiness.
- */
- public function test_is_safe_capability() {
- global $DB;
- // Note: there is not much to test, just make sure no notices are throw for the most dangerous cap.
- $capability = $DB->get_record('capabilities', array('name'=>'moodle/site:config'), '*', MUST_EXIST);
- $this->assertFalse(is_safe_capability($capability));
- }
- /**
- * Test context fetching.
- */
- public function test_get_context_info_array() {
- $this->resetAfterTest();
- $syscontext = context_system::instance();
- $user = $this->getDataGenerator()->create_user();
- $usercontext = context_user::instance($user->id);
- $course = $this->getDataGenerator()->create_course();
- $catcontext = context_coursecat::instance($course->category);
- $coursecontext = context_course::instance($course->id);
- $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
- $modcontext = context_module::instance($page->cmid);
- $cm = get_coursemodule_from_instance('page', $page->id);
- $block1 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid'=>$coursecontext->id));
- $block1context = context_block::instance($block1->id);
- $block2 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid'=>$modcontext->id));
- $block2context = context_block::instance($block2->id);
- $result = get_context_info_array($syscontext->id);
- $this->assertCount(3, $result);
- $this->assertEquals($syscontext, $result[0]);
- $this->assertNull($result[1]);
- $this->assertNull($result[2]);
- $result = get_context_info_array($usercontext->id);
- $this->assertCount(3, $result);
- $this->assertEquals($usercontext, $result[0]);
- $this->assertNull($result[1]);
- $this->assertNull($result[2]);
- $result = get_context_info_array($catcontext->id);
- $this->assertCount(3, $result);
- $this->assertEquals($catcontext, $result[0]);
- $this->assertNull($result[1]);
- $this->assertNull($result[2]);
- $result = get_context_info_array($coursecontext->id);
- $this->assertCount(3, $result);
- $this->assertEquals($coursecontext, $result[0]);
- $this->assertEquals($course->id, $result[1]->id);
- $this->assertSame($course->shortname, $result[1]->shortname);
- $this->assertNull($result[2]);
- $result = get_context_info_array($block1context->id);
- $this->assertCount(3, $result);
- $this->assertEquals($block1context, $result[0]);
- $this->assertEquals($course->id, $result[1]->id);
- $this->assertEquals($course->shortname, $result[1]->shortname);
- $this->assertNull($result[2]);
- $result = get_context_info_array($modcontext->id);
- $this->assertCount(3, $result);
- $this->assertEquals($modcontext, $result[0]);
- $this->assertEquals($course->id, $result[1]->id);
- $this->assertSame($course->shortname, $result[1]->shortname);
- $this->assertEquals($cm->id, $result[2]->id);
- $this->assertEquals($cm->groupmembersonly, $result[2]->groupmembersonly);
- $result = get_context_info_array($block2context->id);
- $this->assertCount(3, $result);
- $this->assertEquals($block2context, $result[0]);
- $this->assertEquals($course->id, $result[1]->id);
- $this->assertSame($course->shortname, $result[1]->shortname);
- $this->assertEquals($cm->id, $result[2]->id);
- $this->assertEquals($cm->groupmembersonly, $result[2]->groupmembersonly);
- }
- /**
- * Test looking for course contacts.
- */
- public function test_has_coursecontact_role() {
- global $DB, $CFG;
- $this->resetAfterTest();
- $users = $DB->get_records('user');
- // Nobody is expected to have any course level roles.
- $this->assertNotEmpty($CFG->coursecontact);
- foreach ($users as $user) {
- $this->assertFalse(has_coursecontact_role($user->id));
- }
- $user = $this->getDataGenerator()->create_user();
- $course = $this->getDataGenerator()->create_course();
- role_assign($CFG->coursecontact, $user->id, context_course::instance($course->id));
- $this->assertTrue(has_coursecontact_role($user->id));
- }
- /**
- * Test creation of roles.
- */
- public function test_create_role() {
- global $DB;
- $this->resetAfterTest();
- $id = create_role('New student role', 'student2', 'New student description', 'student');
- $role = $DB->get_record('role', array('id'=>$id));
- $this->assertNotEmpty($role);
- $this->assertSame('New student role', $role->name);
- $this->assertSame('student2', $role->shortname);
- $this->assertSame('New student description', $role->description);
- $this->assertSame('student', $role->archetype);
- }
- /**
- * Test adding of capabilities to roles.
- */
- public function test_assign_capability() {
- global $DB;
- $this->resetAfterTest();
- $user = $this->getDataGenerator()->create_user();
- $syscontext = context_system::instance();
- $frontcontext = context_course::instance(SITEID);
- $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
- $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability assigned to student by default.
- $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')));
- $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')));
- $this->setUser($user);
- $result = assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $student->id, $frontcontext->id);
- $this->assertTrue($result);
- $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
- $this->assertNotEmpty($permission);
- $this->assertEquals(CAP_ALLOW, $permission->permission);
- $this->assertEquals($user->id, $permission->modifierid);
- $this->setUser(0);
- $result = assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $student->id, $frontcontext->id, false);
- $this->assertTrue($result);
- $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
- $this->assertNotEmpty($permission);
- $this->assertEquals(CAP_ALLOW, $permission->permission);
- $this->assertEquals(3, $permission->modifierid);
- $result = assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $student->id, $frontcontext->id, true);
- $this->assertTrue($result);
- $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
- $this->assertNotEmpty($permission);
- $this->assertEquals(CAP_PROHIBIT, $permission->permission);
- $this->assertEquals(0, $permission->modifierid);
- $result = assign_capability('moodle/backup:backupcourse', CAP_INHERIT, $student->id, $frontcontext->id);
- $this->assertTrue($result);
- $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
- $this->assertEmpty($permission);
- // Test event trigger.
- $rolecapabilityevent = \core\event\role_capabilities_updated::create(array('context' => $syscontext,
- 'objectid' => $student->id,
- 'other' => array('name' => $student->shortname)
- ));
- $expectedlegacylog = array(SITEID, 'role', 'view', 'admin/roles/define.php?action=view&roleid=' . $student->id,
- $student->shortname, '', $user->id);
- $rolecapabilityevent->set_legacy_logdata($expectedlegacylog);
- $rolecapabilityevent->add_record_snapshot('role', $student);
- $sink = $this->redirectEvents();
- $rolecapabilityevent->trigger();
- $events = $sink->get_events();
- $sink->close();
- $event = array_pop($events);
- $this->assertInstanceOf('\core\event\role_capabilities_updated', $event);
- $expectedurl = new moodle_url('admin/roles/define.php', array('action' => 'view', 'roleid' => $student->id));
- $this->assertEquals($expectedurl, $event->get_url());
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test removing of capabilities from roles.
- */
- public function test_unassign_capability() {
- global $DB;
- $this->resetAfterTest();
- $syscontext = context_system::instance();
- $frontcontext = context_course::instance(SITEID);
- $manager = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
- $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability assigned to manager by default.
- assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $frontcontext->id);
- $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- $result = unassign_capability('moodle/backup:backupcourse', $manager->id, $syscontext->id);
- $this->assertTrue($result);
- $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- unassign_capability('moodle/backup:backupcourse', $manager->id, $frontcontext);
- $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $syscontext->id);
- assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $frontcontext->id);
- $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- $result = unassign_capability('moodle/backup:backupcourse', $manager->id);
- $this->assertTrue($result);
- $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
- }
- /**
- * Test role assigning.
- */
- public function test_role_assign() {
- global $DB, $USER;
- $this->resetAfterTest();
- $user = $this->getDataGenerator()->create_user();
- $course = $this->getDataGenerator()->create_course();
- $role = $DB->get_record('role', array('shortname'=>'student'));
- $this->setUser(0);
- $context = context_system::instance();
- $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
- role_assign($role->id, $user->id, $context->id);
- $ras = $DB->get_record('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id));
- $this->assertNotEmpty($ras);
- $this->assertSame('', $ras->component);
- $this->assertSame('0', $ras->itemid);
- $this->assertEquals($USER->id, $ras->modifierid);
- $this->setAdminUser();
- $context = context_course::instance($course->id);
- $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
- role_assign($role->id, $user->id, $context->id, 'enrol_self', 1, 666);
- $ras = $DB->get_record('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id));
- $this->assertNotEmpty($ras);
- $this->assertSame('enrol_self', $ras->component);
- $this->assertSame('1', $ras->itemid);
- $this->assertEquals($USER->id, $ras->modifierid);
- $this->assertEquals(666, $ras->timemodified);
- // Test event triggered.
- $user2 = $this->getDataGenerator()->create_user();
- $sink = $this->redirectEvents();
- $raid = role_assign($role->id, $user2->id, $context->id);
- $events = $sink->get_events();
- $sink->close();
- $this->assertCount(1, $events);
- $event = $events[0];
- $this->assertInstanceOf('\core\event\role_assigned', $event);
- $this->assertSame('role', $event->target);
- $this->assertSame('role', $event->objecttable);
- $this->assertEquals($role->id, $event->objectid);
- $this->assertEquals($context->id, $event->contextid);
- $this->assertEquals($user2->id, $event->relateduserid);
- $this->assertCount(3, $event->other);
- $this->assertEquals($raid, $event->other['id']);
- $this->assertSame('', $event->other['component']);
- $this->assertEquals(0, $event->other['itemid']);
- $this->assertInstanceOf('moodle_url', $event->get_url());
- $this->assertSame('role_assigned', $event::get_legacy_eventname());
- $roles = get_all_roles();
- $rolenames = role_fix_names($roles, $context, ROLENAME_ORIGINAL, true);
- $expectedlegacylog = array($course->id, 'role', 'assign',
- 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$role->id, $rolenames[$role->id], '', $USER->id);
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test role unassigning.
- */
- public function test_role_unassign() {
- global $DB, $USER;
- $this->resetAfterTest();
- $user = $this->getDataGenerator()->create_user();
- $course = $this->getDataGenerator()->create_course();
- $role = $DB->get_record('role', array('shortname'=>'student'));
- $context = context_course::instance($course->id);
- role_assign($role->id, $user->id, $context->id);
- $this->assertTrue($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
- role_unassign($role->id, $user->id, $context->id);
- $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
- role_assign($role->id, $user->id, $context->id, 'enrol_self', 1);
- $this->assertTrue($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
- role_unassign($role->id, $user->id, $context->id, 'enrol_self', 1);
- $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
- // Test event triggered.
- role_assign($role->id, $user->id, $context->id);
- $sink = $this->redirectEvents();
- role_unassign($role->id, $user->id, $context->id);
- $events = $sink->get_events();
- $sink->close();
- $this->assertCount(1, $events);
- $event = $events[0];
- $this->assertInstanceOf('\core\event\role_unassigned', $event);
- $this->assertSame('role', $event->target);
- $this->assertSame('role', $event->objecttable);
- $this->assertEquals($role->id, $event->objectid);
- $this->assertEquals($context->id, $event->contextid);
- $this->assertEquals($user->id, $event->relateduserid);
- $this->assertCount(3, $event->other);
- $this->assertSame('', $event->other['component']);
- $this->assertEquals(0, $event->other['itemid']);
- $this->assertInstanceOf('moodle_url', $event->get_url());
- $roles = get_all_roles();
- $rolenames = role_fix_names($roles, $context, ROLENAME_ORIGINAL, true);
- $expectedlegacylog = array($course->id, 'role', 'unassign',
- 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$role->id, $rolenames[$role->id], '', $USER->id);
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test role unassigning.
- */
- public function test_role_unassign_all() {
- global $DB;
- $this->resetAfterTest();
- $user = $this->getDataGenerator()->create_user();
- $course = $this->getDataGenerator()->create_course();
- $role = $DB->get_record('role', array('shortname'=>'student'));
- $role2 = $DB->get_record('role', array('shortname'=>'teacher'));
- $syscontext = context_system::instance();
- $coursecontext = context_course::instance($course->id);
- $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
- $modcontext = context_module::instance($page->cmid);
- role_assign($role->id, $user->id, $syscontext->id);
- role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
- $this->assertEquals(2, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_unassign_all(array('userid'=>$user->id, 'roleid'=>$role->id));
- $this->assertEquals(0, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_assign($role->id, $user->id, $syscontext->id);
- role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
- role_assign($role->id, $user->id, $modcontext->id);
- $this->assertEquals(3, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id), false);
- $this->assertEquals(2, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id), true);
- $this->assertEquals(1, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_unassign_all(array('userid'=>$user->id));
- $this->assertEquals(0, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_assign($role->id, $user->id, $syscontext->id);
- role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
- role_assign($role->id, $user->id, $coursecontext->id);
- role_assign($role->id, $user->id, $modcontext->id);
- $this->assertEquals(4, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id, 'component'=>'enrol_self'), true, true);
- $this->assertEquals(1, $DB->count_records('role_assignments', array('userid'=>$user->id)));
- // Test events triggered.
- role_assign($role2->id, $user->id, $coursecontext->id);
- role_assign($role2->id, $user->id, $modcontext->id);
- $sink = $this->redirectEvents();
- role_unassign_all(array('userid'=>$user->id, 'roleid'=>$role2->id));
- $events = $sink->get_events();
- $sink->close();
- $this->assertCount(2, $events);
- $this->assertInstanceOf('\core\event\role_unassigned', $events[0]);
- $this->assertInstanceOf('\core\event\role_unassigned', $events[1]);
- }
- /**
- * Test role queries.
- */
- public function test_get_roles_with_capability() {
- global $DB;
- $this->resetAfterTest();
- $syscontext = context_system::instance();
- $frontcontext = context_course::instance(SITEID);
- $manager = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
- $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
- $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability is ok.
- $DB->delete_records('role_capabilities', array('capability'=>'moodle/backup:backupcourse'));
- $roles = get_roles_with_capability('moodle/backup:backupcourse');
- $this->assertEquals(array(), $roles);
- assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $syscontext->id);
- assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $manager->id, $frontcontext->id);
- assign_capability('moodle/backup:backupcourse', CAP_PREVENT, $teacher->id, $frontcontext->id);
- $roles = get_roles_with_capability('moodle/backup:backupcourse');
- $this->assertEquals(array($teacher->id, $manager->id), array_keys($roles), '', 0, 10, true);
- $roles = get_roles_with_capability('moodle/backup:backupcourse', CAP_ALLOW);
- $this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
- $roles = get_roles_with_capability('moodle/backup:backupcourse', null, $syscontext);
- $this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
- }
- /**
- * Test deleting of roles.
- */
- public function test_delete_role() {
- global $DB;
- $this->resetAfterTest();
- $role = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
- $user = $this->getDataGenerator()->create_user();
- role_assign($role->id, $user->id, context_system::instance());
- $course = $this->getDataGenerator()->create_course();
- $rolename = (object)array('roleid'=>$role->id, 'name'=>'Man', 'contextid'=>context_course::instance($course->id)->id);
- $DB->insert_record('role_names', $rolename);
- $this->assertTrue($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_capabilities', array('roleid'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_names', array('roleid'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_context_levels', array('roleid'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
- $this->assertTrue($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
- // Delete role and get event.
- $sink = $this->redirectEvents();
- $result = delete_role($role->id);
- $events = $sink->get_events();
- $sink->close();
- $event = array_pop($events);
- $this->assertTrue($result);
- $this->assertFalse($DB->record_exists('role', array('id'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_capabilities', array('roleid'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_names', array('roleid'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_context_levels', array('roleid'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
- $this->assertFalse($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
- // Test triggered event.
- $this->assertInstanceOf('\core\event\role_deleted', $event);
- $this->assertSame('role', $event->target);
- $this->assertSame('role', $event->objecttable);
- $this->assertSame($role->id, $event->objectid);
- $this->assertEquals(context_system::instance(), $event->get_context());
- $this->assertSame($role->shortname, $event->other['shortname']);
- $this->assertSame($role->description, $event->other['description']);
- $this->assertSame($role->archetype, $event->other['archetype']);
- $expectedlegacylog = array(SITEID, 'role', 'delete', 'admin/roles/manage.php?action=delete&roleid='.$role->id,
- $role->shortname, '');
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test fetching of all roles.
- */
- public function test_get_all_roles() {
- global $DB;
- $this->resetAfterTest();
- $allroles = get_all_roles();
- $this->assertInternalType('array', $allroles);
- $this->assertCount(8, $allroles); // There are 8 roles is standard install.
- $role = reset($allroles);
- $role = (array)$role;
- $this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype'), array_keys($role), '', 0, 10, true);
- foreach ($allroles as $roleid => $role) {
- $this->assertEquals($role->id, $roleid);
- }
- $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
- $course = $this->getDataGenerator()->create_course();
- $coursecontext = context_course::instance($course->id);
- $otherid = create_role('Other role', 'other', 'Some other role', '');
- $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $teacherename);
- $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $otherrename);
- $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
- $allroles = get_all_roles($coursecontext);
- $this->assertInternalType('array', $allroles);
- $this->assertCount(9, $allroles);
- $role = reset($allroles);
- $role = (array)$role;
- $this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype', 'coursealias'), array_keys($role), '', 0, 10, true);
- foreach ($allroles as $roleid => $role) {
- $this->assertEquals($role->id, $roleid);
- if (isset($renames[$roleid])) {
- $this->assertSame($renames[$roleid], $role->coursealias);
- } else {
- $this->assertNull($role->coursealias);
- }
- }
- }
- /**
- * Test getting of all archetypes.
- */
- public function test_get_role_archetypes() {
- $archetypes = get_role_archetypes();
- $this->assertCount(8, $archetypes); // There are 8 archetypes in standard install.
- foreach ($archetypes as $k => $v) {
- $this->assertSame($k, $v);
- }
- }
- /**
- * Test getting of roles with given archetype.
- */
- public function test_get_archetype_roles() {
- $this->resetAfterTest();
- // New install should have 1 role for each archetype.
- $archetypes = get_role_archetypes();
- foreach ($archetypes as $archetype) {
- $roles = get_archetype_roles($archetype);
- $this->assertCount(1, $roles);
- $role = reset($roles);
- $this->assertSame($archetype, $role->archetype);
- }
- create_role('New student role', 'student2', 'New student description', 'student');
- $roles = get_archetype_roles('student');
- $this->assertCount(2, $roles);
- }
- /**
- * Test aliased role names.
- */
- public function test_role_get_name() {
- global $DB;
- $this->resetAfterTest();
- $allroles = $DB->get_records('role');
- $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
- $course = $this->getDataGenerator()->create_course();
- $coursecontext = context_course::instance($course->id);
- $otherid = create_role('Other role', 'other', 'Some other role', '');
- $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $teacherename);
- $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $otherrename);
- $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
- foreach ($allroles as $role) {
- // Get localised name from lang pack.
- $this->assertSame('', $role->name);
- $name = role_get_name($role, null, ROLENAME_ORIGINAL);
- $this->assertNotEmpty($name);
- $this->assertNotEquals($role->shortname, $name);
- if (isset($renames[$role->id])) {
- $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext));
- $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext, ROLENAME_ALIAS));
- $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext, ROLENAME_ALIAS_RAW));
- $this->assertSame("{$renames[$role->id]} ($name)", role_get_name($role, $coursecontext, ROLENAME_BOTH));
- } else {
- $this->assertSame($name, role_get_name($role, $coursecontext));
- $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_ALIAS));
- $this->assertNull(role_get_name($role, $coursecontext, ROLENAME_ALIAS_RAW));
- $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_BOTH));
- }
- $this->assertSame($name, role_get_name($role));
- $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_ORIGINAL));
- $this->assertSame($name, role_get_name($role, null, ROLENAME_ORIGINAL));
- $this->assertSame($role->shortname, role_get_name($role, $coursecontext, ROLENAME_SHORT));
- $this->assertSame($role->shortname, role_get_name($role, null, ROLENAME_SHORT));
- $this->assertSame("$name ($role->shortname)", role_get_name($role, $coursecontext, ROLENAME_ORIGINALANDSHORT));
- $this->assertSame("$name ($role->shortname)", role_get_name($role, null, ROLENAME_ORIGINALANDSHORT));
- $this->assertNull(role_get_name($role, null, ROLENAME_ALIAS_RAW));
- }
- }
- /**
- * Test tweaking of role name arrays.
- */
- public function test_role_fix_names() {
- global $DB;
- $this->resetAfterTest();
- $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
- $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
- $otherid = create_role('Other role', 'other', 'Some other role', '');
- $anotherid = create_role('Another role', 'another', 'Yet another other role', '');
- $allroles = $DB->get_records('role');
- $syscontext = context_system::instance();
- $frontcontext = context_course::instance(SITEID);
- $course = $this->getDataGenerator()->create_course();
- $coursecontext = context_course::instance($course->id);
- $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
- $categorycontext = context_coursecat::instance($category->id);
- $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $teacherename);
- $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $otherrename);
- $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
- // Make sure all localname contain proper values for each ROLENAME_ constant,
- // note role_get_name() on frontpage is used to get the original name for future compatibility.
- $roles = $allroles;
- unset($roles[$student->id]); // Remove one role to make sure no role is added or removed.
- $rolenames = array();
- foreach ($roles as $role) {
- $rolenames[$role->id] = $role->name;
- }
- $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT);
- foreach ($alltypes as $type) {
- $fixed = role_fix_names($roles, $coursecontext, $type);
- $this->assertCount(count($roles), $fixed);
- foreach ($fixed as $roleid => $rolename) {
- $this->assertInstanceOf('stdClass', $rolename);
- $role = $allroles[$roleid];
- $name = role_get_name($role, $coursecontext, $type);
- $this->assertSame($name, $rolename->localname);
- }
- $fixed = role_fix_names($rolenames, $coursecontext, $type);
- $this->assertCount(count($rolenames), $fixed);
- foreach ($fixed as $roleid => $rolename) {
- $role = $allroles[$roleid];
- $name = role_get_name($role, $coursecontext, $type);
- $this->assertSame($name, $rolename);
- }
- }
- }
- /**
- * Test role default allows.
- */
- public function test_get_default_role_archetype_allows() {
- $archetypes = get_role_archetypes();
- foreach ($archetypes as $archetype) {
- $result = get_default_role_archetype_allows('assign', $archetype);
- $this->assertInternalType('array', $result);
- $result = get_default_role_archetype_allows('override', $archetype);
- $this->assertInternalType('array', $result);
- $result = get_default_role_archetype_allows('switch', $archetype);
- $this->assertInternalType('array', $result);
- }
- $result = get_default_role_archetype_allows('assign', '');
- $this->assertSame(array(), $result);
- $result = get_default_role_archetype_allows('override', '');
- $this->assertSame(array(), $result);
- $result = get_default_role_archetype_allows('switch', '');
- $this->assertSame(array(), $result);
- $result = get_default_role_archetype_allows('assign', 'wrongarchetype');
- $this->assertSame(array(), $result);
- $this->assertDebuggingCalled();
- $result = get_default_role_archetype_allows('override', 'wrongarchetype');
- $this->assertSame(array(), $result);
- $this->assertDebuggingCalled();
- $result = get_default_role_archetype_allows('switch', 'wrongarchetype');
- $this->assertSame(array(), $result);
- $this->assertDebuggingCalled();
- }
- /**
- * Test allowing of role assignments.
- */
- public function test_allow_assign() {
- global $DB, $CFG;
- $this->resetAfterTest();
- $otherid = create_role('Other role', 'other', 'Some other role', '');
- $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
- $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
- allow_assign($otherid, $student->id);
- $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
- // Test event trigger.
- $allowroleassignevent = \core\event\role_allow_assign_updated::create(array('context' => context_system::instance()));
- $sink = $this->redirectEvents();
- $allowroleassignevent->trigger();
- $events = $sink->get_events();
- $sink->close();
- $event = array_pop($events);
- $this->assertInstanceOf('\core\event\role_allow_assign_updated', $event);
- $mode = 'assign';
- $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
- $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test allowing of role overrides.
- */
- public function test_allow_override() {
- global $DB, $CFG;
- $this->resetAfterTest();
- $otherid = create_role('Other role', 'other', 'Some other role', '');
- $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
- $this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
- allow_override($otherid, $student->id);
- $this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
- // Test event trigger.
- $allowroleassignevent = \core\event\role_allow_override_updated::create(array('context' => context_system::instance()));
- $sink = $this->redirectEvents();
- $allowroleassignevent->trigger();
- $events = $sink->get_events();
- $sink->close();
- $event = array_pop($events);
- $this->assertInstanceOf('\core\event\role_allow_override_updated', $event);
- $mode = 'override';
- $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
- $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test allowing of role switching.
- */
- public function test_allow_switch() {
- global $DB, $CFG;
- $this->resetAfterTest();
- $otherid = create_role('Other role', 'other', 'Some other role', '');
- $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
- $this->assertFalse($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
- allow_switch($otherid, $student->id);
- $this->assertTrue($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
- // Test event trigger.
- $allowroleassignevent = \core\event\role_allow_switch_updated::create(array('context' => context_system::instance()));
- $sink = $this->redirectEvents();
- $allowroleassignevent->trigger();
- $events = $sink->get_events();
- $sink->close();
- $event = array_pop($events);
- $this->assertInstanceOf('\core\event\role_allow_switch_updated', $event);
- $mode = 'switch';
- $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
- $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
- $this->assertEventLegacyLogData($expectedlegacylog, $event);
- }
- /**
- * Test returning of assignable roles in context.
- */
- public function test_get_assignable_roles() {
- global $DB;
- $this->resetAfterTest();
- $course = $this->getDataGenerator()->create_course();
- $coursecontext = context_course::instance($course->id);
- $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
- $teacher = $this->getDataGenerator()->create_user();
- role_assign($teacherrole->id, $teacher->id, $coursecontext);
- $teacherename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
- $DB->insert_record('role_names', $teacherename);
- $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
- $student = $this->getDataGenerator()->create_user();
- role_assign($studentrole->id, $student->id, $coursecontext);
- $contexts = $DB->get_records('context');
- $users = $DB->get_records('user');
- $allroles = $DB->get_records('role');
- // Evaluate all results for all users in all contexts.
- foreach ($users as $user) {
- $this->setUser($user);
- foreach ($contexts as $contextid => $unused) {
- $context = context_helper::instance_by_id($contextid);
- $roles = get_assignable_roles($context, ROLENAME_SHORT);
- foreach ($allroles as $roleid => $role) {
- if (isset($roles[$roleid])) {
- if (is_siteadmin()) {
- $this->assertTrue($DB->record_exists('role_context_levels', array('contextlevel'=>$context->contextlevel, 'roleid'=>$roleid)));
- } else {
- $this->assertTrue(user_can_assign($context, $roleid), "u:$user->id r:$roleid");
- }
- $this->assertEquals($role->shortname, $roles[$roleid]);
- } else {
- …
Large files files are truncated, but you can click here to view the full file