MDL-64410 access: Unit tests for context locking SQL
This commit is contained in:
committed by
Eloy Lafuente (stronk7)
parent
fe41810304
commit
54c2b17604
@@ -3724,6 +3724,57 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
$this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test fetching users by capability.
|
||||
*/
|
||||
public function test_get_users_by_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();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
||||
$student = $this->getDataGenerator()->create_user();
|
||||
$guest = $DB->get_record('user', array('username' => 'guest'));
|
||||
|
||||
role_assign($teacherrole->id, $teacher->id, $coursecontext);
|
||||
role_assign($studentrole->id, $student->id, $coursecontext);
|
||||
$admin = $DB->get_record('user', array('username' => 'admin'));
|
||||
|
||||
// Note: Here are used default capabilities, the full test is in permission evaluation below,
|
||||
// use two capabilities that teacher has and one does not, none of them should be allowed for not-logged-in user.
|
||||
$this->assertTrue($DB->record_exists('capabilities', array('name' => 'moodle/backup:backupcourse')));
|
||||
$this->assertTrue($DB->record_exists('capabilities', array('name' => 'moodle/site:approvecourse')));
|
||||
|
||||
$users = get_users_by_capability($coursecontext, 'moodle/backup:backupcourse');
|
||||
|
||||
$this->assertTrue(array_key_exists($teacher->id, $users));
|
||||
$this->assertFalse(array_key_exists($admin->id, $users));
|
||||
$this->assertFalse(array_key_exists($student->id, $users));
|
||||
$this->assertFalse(array_key_exists($guest->id, $users));
|
||||
|
||||
$users = get_users_by_capability($coursecontext, 'moodle/site:approvecourse');
|
||||
|
||||
$this->assertFalse(array_key_exists($teacher->id, $users));
|
||||
$this->assertFalse(array_key_exists($admin->id, $users));
|
||||
$this->assertFalse(array_key_exists($student->id, $users));
|
||||
$this->assertFalse(array_key_exists($guest->id, $users));
|
||||
|
||||
// Test role override.
|
||||
assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $teacherrole->id, $coursecontext, true);
|
||||
assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $studentrole->id, $coursecontext, true);
|
||||
|
||||
$users = get_users_by_capability($coursecontext, 'moodle/backup:backupcourse');
|
||||
|
||||
$this->assertFalse(array_key_exists($teacher->id, $users));
|
||||
$this->assertFalse(array_key_exists($admin->id, $users));
|
||||
$this->assertTrue(array_key_exists($student->id, $users));
|
||||
$this->assertFalse(array_key_exists($guest->id, $users));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating of role capabilities during upgrade
|
||||
* @return void
|
||||
@@ -3956,6 +4007,170 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
// These all show as reads, when there has actually only been a single query.
|
||||
$this->assertLessThanOrEqual(3, $DB->perf_get_reads() - $predbqueries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that get_with_capability_sql and get_with_capability_join respect context locking.
|
||||
*/
|
||||
public function test_get_with_capability_sql_locked() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
$cat1 = $generator->create_category();
|
||||
$cat2 = $generator->create_category();
|
||||
$cat1course1 = $generator->create_course(['category' => $cat1->id]);
|
||||
$cat1course1forum = $generator->create_module('forum', ['course' => $cat1course1]);
|
||||
|
||||
$contexts = (object) [
|
||||
'system' => \context_system::instance(),
|
||||
'cat1' => \context_coursecat::instance($cat1->id),
|
||||
'cat2' => \context_coursecat::instance($cat2->id),
|
||||
'cat1course1' => \context_course::instance($cat1course1->id),
|
||||
'cat1course1forum' => \context_module::instance($cat1course1forum->cmid),
|
||||
];
|
||||
|
||||
// Test with the 'mod/forum:startdiscussion' capability.
|
||||
$caput = 'mod/forum:startdiscussion';
|
||||
|
||||
// Create a test user.
|
||||
$uut = $generator->create_and_enrol($cat1course1, 'teacher');
|
||||
|
||||
// Initially the user will be returned by get_users_by_capability.
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
// Freezing the forum will remove the user.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat1course1forum->set_locked(true);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayNotHasKey($uut->id, $users);
|
||||
|
||||
// But not if context locking is disabled.
|
||||
set_config('contextlocking', 0);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
$contexts->cat1course1forum->set_locked(false);
|
||||
|
||||
// Freezing the course will have the same effect.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat1course1->set_locked(true);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayNotHasKey($uut->id, $users);
|
||||
|
||||
// But not if context locking is disabled.
|
||||
set_config('contextlocking', 0);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
$contexts->cat1course1->set_locked(false);
|
||||
|
||||
// Freezing the category will have the same effect.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat1->set_locked(true);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayNotHasKey($uut->id, $users);
|
||||
|
||||
// But not if context locking is disabled.
|
||||
set_config('contextlocking', 0);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
$contexts->cat1->set_locked(false);
|
||||
|
||||
// Freezing an unrelated category will have no effect.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat2->set_locked(true);
|
||||
list($sql, $params) = get_with_capability_sql($contexts->cat1course1forum, $caput);
|
||||
$users = $DB->get_records_sql($sql, $params);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that get_users_by_capability respects context freezing.
|
||||
*/
|
||||
public function test_get_users_by_capability_locked() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
$cat1 = $generator->create_category();
|
||||
$cat2 = $generator->create_category();
|
||||
$cat1course1 = $generator->create_course(['category' => $cat1->id]);
|
||||
$cat1course1forum = $generator->create_module('forum', ['course' => $cat1course1]);
|
||||
|
||||
$contexts = (object) [
|
||||
'system' => \context_system::instance(),
|
||||
'cat1' => \context_coursecat::instance($cat1->id),
|
||||
'cat2' => \context_coursecat::instance($cat2->id),
|
||||
'cat1course1' => \context_course::instance($cat1course1->id),
|
||||
'cat1course1forum' => \context_module::instance($cat1course1forum->cmid),
|
||||
];
|
||||
|
||||
// Test with the 'mod/forum:startdiscussion' capability.
|
||||
$caput = 'mod/forum:startdiscussion';
|
||||
|
||||
// Create a test user.
|
||||
$uut = $generator->create_and_enrol($cat1course1, 'teacher');
|
||||
|
||||
// Initially the user will be returned by get_users_by_capability.
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
// Freezing the forum will remove the user.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat1course1forum->set_locked(true);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayNotHasKey($uut->id, $users);
|
||||
|
||||
// But not if context locking is disabled.
|
||||
set_config('contextlocking', 0);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
$contexts->cat1course1forum->set_locked(false);
|
||||
|
||||
// Freezing the course will have the same effect.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat1course1->set_locked(true);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayNotHasKey($uut->id, $users);
|
||||
|
||||
// But not if context locking is disabled.
|
||||
set_config('contextlocking', 0);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
$contexts->cat1course1->set_locked(false);
|
||||
|
||||
// Freezing the category will have the same effect.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat1->set_locked(true);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayNotHasKey($uut->id, $users);
|
||||
|
||||
// But not if context locking is disabled.
|
||||
set_config('contextlocking', 0);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
|
||||
$contexts->cat1->set_locked(false);
|
||||
|
||||
// Freezing an unrelated category will have no effect.
|
||||
set_config('contextlocking', 1);
|
||||
$contexts->cat2->set_locked(true);
|
||||
$users = get_users_by_capability($contexts->cat1course1forum, $caput);
|
||||
$this->assertArrayHasKey($uut->id, $users);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user