Skip to content

Commit

Permalink
Merge branch 'MDL-69504-master' of https://github.com/snake/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Sep 7, 2021
2 parents 3a4ad12 + 38ff4d5 commit b97c021
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mod/lti/locallib.php
Expand Up @@ -2165,7 +2165,7 @@ function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
}
}

if (is_siteadmin($user) || has_capability('mod/lti:admin', $context)) {
if (!is_role_switched($courseid) && (is_siteadmin($user)) || has_capability('mod/lti:admin', $context)) {
// Make sure admins do not have the Learner role, then set admin role.
$roles = array_diff($roles, array('Learner'));
if (!$islti2) {
Expand Down
97 changes: 97 additions & 0 deletions mod/lti/tests/locallib_test.php
Expand Up @@ -1740,6 +1740,103 @@ public function test_get_course_history() {
$this->assertEquals(get_course_history($course), [38903]);
}

/**
* Test the lti_get_ims_role helper function.
*
* @dataProvider lti_get_ims_role_provider
* @covers ::lti_get_ims_role()
*
* @param bool $islti2 whether the method is called with LTI 2.0 role names or not.
* @param string $rolename the name of the role (student, teacher, admin)
* @param null|string $switchedto the role to switch to, or false if not using the 'switch to' functionality.
* @param string $expected the expected role name.
*/
public function test_lti_get_ims_role(bool $islti2, string $rolename, ?string $switchedto, string $expected) {
global $DB;
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();
$user = $rolename == 'admin' ? get_admin() : $this->getDataGenerator()->create_and_enrol($course, $rolename);

if ($switchedto) {
$this->setUser($user);
$role = $DB->get_record('role', array('shortname' => $switchedto));
role_switch($role->id, context_course::instance($course->id));
}

$this->assertEquals($expected, lti_get_ims_role($user, 0, $course->id, $islti2));
}

/**
* Data provider for testing lti_get_ims_role.
*
* @return array[] the test case data.
*/
public function lti_get_ims_role_provider() {
return [
'Student, LTI 1.1, no role switch' => [
'islti2' => false,
'rolename' => 'student',
'switchedto' => null,
'expected' => 'Learner'
],
'Student, LTI 2.0, no role switch' => [
'islti2' => true,
'rolename' => 'student',
'switchedto' => null,
'expected' => 'Learner'
],
'Teacher, LTI 1.1, no role switch' => [
'islti2' => false,
'rolename' => 'editingteacher',
'switchedto' => null,
'expected' => 'Instructor'
],
'Teacher, LTI 2.0, no role switch' => [
'islti2' => true,
'rolename' => 'editingteacher',
'switchedto' => null,
'expected' => 'Instructor'
],
'Admin, LTI 1.1, no role switch' => [
'islti2' => false,
'rolename' => 'admin',
'switchedto' => null,
'expected' => 'Instructor,urn:lti:sysrole:ims/lis/Administrator,urn:lti:instrole:ims/lis/Administrator'
],
'Admin, LTI 2.0, no role switch' => [
'islti2' => true,
'rolename' => 'admin',
'switchedto' => null,
'expected' => 'Instructor,http://purl.imsglobal.org/vocab/lis/v2/person#Administrator'
],
'Admin, LTI 1.1, role switch student' => [
'islti2' => false,
'rolename' => 'admin',
'switchedto' => 'student',
'expected' => 'Learner'
],
'Admin, LTI 2.0, role switch student' => [
'islti2' => true,
'rolename' => 'admin',
'switchedto' => 'student',
'expected' => 'Learner'
],
'Admin, LTI 1.1, role switch teacher' => [
'islti2' => false,
'rolename' => 'admin',
'switchedto' => 'editingteacher',
'expected' => 'Instructor'
],
'Admin, LTI 2.0, role switch teacher' => [
'islti2' => true,
'rolename' => 'admin',
'switchedto' => 'editingteacher',
'expected' => 'Instructor'
],
];
}

/**
* Create an LTI Tool.
*
Expand Down

0 comments on commit b97c021

Please sign in to comment.