From bbb818a6e17b6015cd18636257f5170aa374e705 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 14 Sep 2017 15:23:04 +0800 Subject: [PATCH] MDL-37810 roles: added manager case to test_get_user_roles_in_course() Managers can assign managers, and should be able to see that role on the course profile page, as a link to the participants page. --- lib/tests/accesslib_test.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index 6384b20bcd7ce..83b41fc518245 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -1537,6 +1537,7 @@ public function test_get_user_roles_in_course() { $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST); $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST); + $managerrole = $DB->get_record('role', array('shortname' => 'manager'), '*', MUST_EXIST); $course = $this->getDataGenerator()->create_course(); $coursecontext = context_course::instance($course->id); $teacherrename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id); @@ -1545,6 +1546,7 @@ public function test_get_user_roles_in_course() { $roleids = explode(',', $CFG->profileroles); // Should include teacher and student in new installs. $this->assertTrue(in_array($teacherrole->id, $roleids)); $this->assertTrue(in_array($studentrole->id, $roleids)); + $this->assertFalse(in_array($managerrole->id, $roleids)); $user1 = $this->getDataGenerator()->create_user(); role_assign($teacherrole->id, $user1->id, $coursecontext->id); @@ -1552,6 +1554,8 @@ public function test_get_user_roles_in_course() { $user2 = $this->getDataGenerator()->create_user(); role_assign($studentrole->id, $user2->id, $coursecontext->id); $user3 = $this->getDataGenerator()->create_user(); + $user4 = $this->getDataGenerator()->create_user(); + role_assign($managerrole->id, $user4->id, $coursecontext->id); $roles = get_user_roles_in_course($user1->id, $course->id); $this->assertEquals(1, preg_match_all('/,/', $roles, $matches)); @@ -1563,6 +1567,25 @@ public function test_get_user_roles_in_course() { $roles = get_user_roles_in_course($user3->id, $course->id); $this->assertSame('', $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); + + // 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); + + // 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); } /**