MDL-37810 roles: unit tests for get_profile_roles()
This commit is contained in:
@@ -3166,6 +3166,136 @@ class core_accesslib_testcase extends advanced_testcase {
|
||||
$this->assertTrue(array_key_exists($student->id, $users));
|
||||
$this->assertFalse(array_key_exists($guest->id, $users));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get_profile_roles() function.
|
||||
*/
|
||||
public function test_get_profile_roles() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$coursecontext = context_course::instance($course->id);
|
||||
|
||||
// Assign a student role.
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
role_assign($studentrole->id, $user1->id, $coursecontext);
|
||||
|
||||
// Assign an editing teacher role.
|
||||
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
role_assign($teacherrole->id, $user2->id, $coursecontext);
|
||||
|
||||
// Create a custom role that can be assigned at course level, but don't assign it yet.
|
||||
create_role('Custom role', 'customrole', 'Custom course role');
|
||||
$customrole = $DB->get_record('role', array('shortname' => 'customrole'), '*', MUST_EXIST);
|
||||
set_role_contextlevels($customrole->id, [CONTEXT_COURSE]);
|
||||
allow_assign($teacherrole->id, $customrole->id); // Allow teacher to assign the role in the course.
|
||||
|
||||
// Set the site policy 'profileroles' to show student, teacher and non-editing teacher roles (i.e. not the custom role).
|
||||
$neteacherrole = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
|
||||
set_config('profileroles', "{$studentrole->id}, {$teacherrole->id}, {$neteacherrole->id}");
|
||||
|
||||
// A student in the course (given they can't assign roles) should see those roles which are:
|
||||
// - listed in the 'profileroles' site policy AND
|
||||
// - are assigned in the course context (or parent contexts).
|
||||
// In this case, the non-editing teacher role is not assigned and should not be returned.
|
||||
$expected = [
|
||||
$teacherrole->id => (object) [
|
||||
'id' => $teacherrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $teacherrole->shortname,
|
||||
'sortorder' => $teacherrole->sortorder,
|
||||
'coursealias' => null
|
||||
],
|
||||
$studentrole->id => (object) [
|
||||
'id' => $studentrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $studentrole->shortname,
|
||||
'sortorder' => $studentrole->sortorder,
|
||||
'coursealias' => null
|
||||
]
|
||||
];
|
||||
$this->setUser($user1);
|
||||
$this->assertEquals($expected, get_profile_roles($coursecontext));
|
||||
|
||||
// An editing teacher should also see only 2 roles at this stage as only 2 roles are assigned: 'teacher' and 'student'.
|
||||
$this->setUser($user2);
|
||||
$this->assertEquals($expected, get_profile_roles($coursecontext));
|
||||
|
||||
// Assign a custom role in the course.
|
||||
$user3 = $this->getDataGenerator()->create_user();
|
||||
role_assign($customrole->id, $user3->id, $coursecontext);
|
||||
|
||||
// Confirm that the teacher can see the custom role now that it's assigned.
|
||||
$expectedteacher = [
|
||||
$teacherrole->id => (object) [
|
||||
'id' => $teacherrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $teacherrole->shortname,
|
||||
'sortorder' => $teacherrole->sortorder,
|
||||
'coursealias' => null
|
||||
],
|
||||
$studentrole->id => (object) [
|
||||
'id' => $studentrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $studentrole->shortname,
|
||||
'sortorder' => $studentrole->sortorder,
|
||||
'coursealias' => null
|
||||
],
|
||||
$customrole->id => (object) [
|
||||
'id' => $customrole->id,
|
||||
'name' => 'Custom role',
|
||||
'shortname' => $customrole->shortname,
|
||||
'sortorder' => $customrole->sortorder,
|
||||
'coursealias' => null
|
||||
]
|
||||
];
|
||||
$this->setUser($user2);
|
||||
$this->assertEquals($expectedteacher, get_profile_roles($coursecontext));
|
||||
|
||||
// And that the student can't, because the role isn't included in the 'profileroles' site policy.
|
||||
$expectedstudent = [
|
||||
$teacherrole->id => (object) [
|
||||
'id' => $teacherrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $teacherrole->shortname,
|
||||
'sortorder' => $teacherrole->sortorder,
|
||||
'coursealias' => null
|
||||
],
|
||||
$studentrole->id => (object) [
|
||||
'id' => $studentrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $studentrole->shortname,
|
||||
'sortorder' => $studentrole->sortorder,
|
||||
'coursealias' => null
|
||||
]
|
||||
];
|
||||
$this->setUser($user1);
|
||||
$this->assertEquals($expectedstudent, get_profile_roles($coursecontext));
|
||||
|
||||
// If we have no roles listed in the site policy, the teacher should only see the student and custom roles.
|
||||
$expectedteacher = [
|
||||
$studentrole->id => (object) [
|
||||
'id' => $studentrole->id,
|
||||
'name' => '',
|
||||
'shortname' => $studentrole->shortname,
|
||||
'sortorder' => $studentrole->sortorder,
|
||||
'coursealias' => null
|
||||
],
|
||||
$customrole->id => (object) [
|
||||
'id' => $customrole->id,
|
||||
'name' => 'Custom role',
|
||||
'shortname' => $customrole->shortname,
|
||||
'sortorder' => $customrole->sortorder,
|
||||
'coursealias' => null
|
||||
]
|
||||
];
|
||||
set_config('profileroles', "");
|
||||
$this->setUser($user2);
|
||||
$this->assertEquals($expectedteacher, get_profile_roles($coursecontext));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user