MDL-64971 access: Ensure that the capability exists when fetching
This commit is contained in:
@@ -1804,6 +1804,101 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
$this->assertFalse(has_all_capabilities($sca, $coursecontext, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that assigning a fake cap does not return.
|
||||
*/
|
||||
public function test_fake_capability() {
|
||||
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();
|
||||
|
||||
$fakecapname = 'moodle/fake:capability';
|
||||
|
||||
role_assign($teacherrole->id, $teacher->id, $coursecontext);
|
||||
$admin = $DB->get_record('user', array('username' => 'admin'));
|
||||
|
||||
// Test a capability which does not exist.
|
||||
// Note: Do not use assign_capability because it will not allow fake caps.
|
||||
$DB->insert_record('role_capabilities', (object) [
|
||||
'contextid' => $coursecontext->id,
|
||||
'roleid' => $teacherrole->id,
|
||||
'capability' => $fakecapname,
|
||||
'permission' => CAP_ALLOW,
|
||||
'timemodified' => time(),
|
||||
'modifierid' => 0,
|
||||
]);
|
||||
|
||||
// Check `has_capability`.
|
||||
$this->assertFalse(has_capability($fakecapname, $coursecontext, $teacher));
|
||||
$this->assertDebuggingCalled("Capability \"{$fakecapname}\" was not found! This has to be fixed in code.");
|
||||
$this->assertFalse(has_capability($fakecapname, $coursecontext, $admin));
|
||||
$this->assertDebuggingCalled("Capability \"{$fakecapname}\" was not found! This has to be fixed in code.");
|
||||
|
||||
// Check `get_with_capability_sql` (with uses `get_with_capability_join`).
|
||||
list($sql, $params) = get_with_capability_sql($coursecontext, $fakecapname);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
|
||||
$this->assertFalse(array_key_exists($teacher->id, $users));
|
||||
$this->assertFalse(array_key_exists($admin->id, $users));
|
||||
|
||||
// Check `get_users_by_capability`.
|
||||
$users = get_users_by_capability($coursecontext, $fakecapname);
|
||||
|
||||
$this->assertFalse(array_key_exists($teacher->id, $users));
|
||||
$this->assertFalse(array_key_exists($admin->id, $users));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that assigning a fake cap does not return.
|
||||
*/
|
||||
public function test_fake_capability_assign() {
|
||||
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();
|
||||
|
||||
$capability = 'moodle/fake:capability';
|
||||
|
||||
role_assign($teacherrole->id, $teacher->id, $coursecontext);
|
||||
$admin = $DB->get_record('user', array('username' => 'admin'));
|
||||
|
||||
$this->expectException('coding_exception');
|
||||
$this->expectExceptionMessage("Capability '{$capability}' was not found! This has to be fixed in code.");
|
||||
assign_capability($capability, CAP_ALLOW, $teacherrole->id, $coursecontext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that assigning a fake cap does not return.
|
||||
*/
|
||||
public function test_fake_capability_unassign() {
|
||||
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();
|
||||
|
||||
$capability = 'moodle/fake:capability';
|
||||
|
||||
role_assign($teacherrole->id, $teacher->id, $coursecontext);
|
||||
$admin = $DB->get_record('user', array('username' => 'admin'));
|
||||
|
||||
$this->expectException('coding_exception');
|
||||
$this->expectExceptionMessage("Capability '{$capability}' was not found! This has to be fixed in code.");
|
||||
unassign_capability($capability, CAP_ALLOW, $teacherrole->id, $coursecontext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the caching in get_role_definitions() and get_role_definitions_uncached()
|
||||
* works as intended.
|
||||
@@ -2962,7 +3057,7 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagepagecontext, true);
|
||||
assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $frontpagepagecontext, true);
|
||||
assign_capability('mod/page:view', CAP_ALLOW, $allroles['user'], $frontpagepagecontext, true);
|
||||
assign_capability('moodle/page:view', CAP_ALLOW, $allroles['student'], $frontpagepagecontext, true);
|
||||
assign_capability('mod/page:view', CAP_ALLOW, $allroles['student'], $frontpagepagecontext, true);
|
||||
|
||||
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpagecontext, true);
|
||||
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagecontext, true);
|
||||
@@ -3672,10 +3767,10 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
$this->assertFalse(array_key_exists($guest->id, $users));
|
||||
|
||||
// Test role override.
|
||||
assign_capability('moodle/site:backupcourse', CAP_PROHIBIT, $teacherrole->id, $coursecontext, true);
|
||||
assign_capability('moodle/site:backupcourse', CAP_ALLOW, $studentrole->id, $coursecontext, true);
|
||||
assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $teacherrole->id, $coursecontext, true);
|
||||
assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $studentrole->id, $coursecontext, true);
|
||||
|
||||
list($sql, $params) = get_with_capability_sql($coursecontext, 'moodle/site:backupcourse');
|
||||
list($sql, $params) = get_with_capability_sql($coursecontext, 'moodle/backup:backupcourse');
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
|
||||
$this->assertFalse(array_key_exists($teacher->id, $users));
|
||||
|
||||
Reference in New Issue
Block a user