From 43adc7a333e9602c1da7c08832129dc1912f68f5 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 12 Jan 2021 08:05:28 +0000 Subject: [PATCH] MDL-70614 user: consistent roles spacing in course profile. --- lib/accesslib.php | 2 +- lib/tests/accesslib_test.php | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 63e43412c02..39f110e2553 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2778,7 +2778,7 @@ function get_user_roles_in_course($userid, $courseid) { $rolenames[] = '' . $viewableroles[$roleid] . ''; } } - $rolestring = implode(',', $rolenames); + $rolestring = implode(', ', $rolenames); } return $rolestring; diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index a36a6e4117b..257eea0cecd 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -1738,34 +1738,37 @@ class core_accesslib_testcase extends advanced_testcase { $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); } /**