Skip to content

Commit

Permalink
Merge branch 'MDL-70614-311' of git://github.com/paulholden/moodle in…
Browse files Browse the repository at this point in the history
…to MOODLE_311_STABLE
  • Loading branch information
andrewnicols committed Feb 8, 2021
2 parents 9254610 + 58b31a5 commit c012235
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/accesslib.php
Expand Up @@ -2778,7 +2778,7 @@ function get_user_roles_in_course($userid, $courseid) {
$rolenames[] = '<a href="' . $url . '">' . $viewableroles[$roleid] . '</a>';
}
}
$rolestring = implode(',', $rolenames);
$rolestring = implode(', ', $rolenames);
}

return $rolestring;
Expand Down
27 changes: 15 additions & 12 deletions lib/tests/accesslib_test.php
Expand Up @@ -1738,34 +1738,37 @@ public function test_get_user_roles_in_course() {
$this->setAdminUser();

$roles = get_user_roles_in_course($user1->id, $course->id);
$this->assertEquals(1, preg_match_all('/,/', $roles, $matches));
$this->assertTrue(strpos($roles, role_get_name($teacherrole, $coursecontext)) !== false);
$this->assertEquals([
role_get_name($teacherrole, $coursecontext),
role_get_name($studentrole, $coursecontext),
], array_map('strip_tags', explode(', ', $roles)));

$roles = get_user_roles_in_course($user2->id, $course->id);
$this->assertEquals(0, preg_match_all('/,/', $roles, $matches));
$this->assertTrue(strpos($roles, role_get_name($studentrole, $coursecontext)) !== false);
$this->assertEquals([
role_get_name($studentrole, $coursecontext),
], array_map('strip_tags', explode(', ', $roles)));

$roles = get_user_roles_in_course($user3->id, $course->id);
$this->assertSame('', $roles);
$this->assertEmpty($roles);

// Managers should be able to see a link to their own role type, given they can assign it in the context.
$this->setUser($user4);
$roles = get_user_roles_in_course($user4->id, $course->id);
$this->assertNotEmpty($roles);
$this->assertEquals(1, count(explode(',', $roles)));
$this->assertTrue(strpos($roles, role_get_name($managerrole, $coursecontext)) !== false);
$this->assertEquals([
role_get_name($managerrole, $coursecontext),
], array_map('strip_tags', explode(', ', $roles)));

// Managers should see 2 roles if viewing a user who has been enrolled as a student and a teacher in the course.
$roles = get_user_roles_in_course($user1->id, $course->id);
$this->assertEquals(2, count(explode(',', $roles)));
$this->assertTrue(strpos($roles, role_get_name($studentrole, $coursecontext)) !== false);
$this->assertTrue(strpos($roles, role_get_name($teacherrole, $coursecontext)) !== false);
$this->assertEquals([
role_get_name($teacherrole, $coursecontext),
role_get_name($studentrole, $coursecontext),
], array_map('strip_tags', explode(', ', $roles)));

// Students should not see the manager role if viewing a manager's profile.
$this->setUser($user2);
$roles = get_user_roles_in_course($user4->id, $course->id);
$this->assertEmpty($roles); // Should see 0 roles on the manager's profile.
$this->assertFalse(strpos($roles, role_get_name($managerrole, $coursecontext)) !== false);
}

/**
Expand Down

0 comments on commit c012235

Please sign in to comment.