diff --git a/core/userrights.class.inc.php b/core/userrights.class.inc.php index 7205f14a4d..0e6eaefd84 100644 --- a/core/userrights.class.inc.php +++ b/core/userrights.class.inc.php @@ -1274,14 +1274,17 @@ class UserRights if (is_null($oUser)) { $sInitials = ''; - $aLoginParts = explode(' ', $sLogin); - foreach($aLoginParts as $sLoginPart) - { + // - Capitalize the first letter no matter what + $sReworkedLogin = ucfirst($sLogin); + // - Replace dashes with spaces to interpret all parts of the login + $sReworkedLogin = str_replace('-', ' ', $sReworkedLogin); + // - Explode login to check parts individually + $aLoginParts = explode(' ', $sReworkedLogin); + foreach ($aLoginParts as $sLoginPart) { // Keep only upper case first letters // eg. "My first name My last name" => "MM" // eg. "Carrie Anne Moss" => "CAM" - if(preg_match('/^\p{Lu}/u', $sLoginPart) > 0) - { + if (preg_match('/^\p{Lu}/u', $sLoginPart) > 0) { $sInitials .= mb_substr($sLoginPart, 0, 1); } } diff --git a/test/core/UserRightsTest.php b/test/core/UserRightsTest.php index fd2102114f..391011a10b 100644 --- a/test/core/UserRightsTest.php +++ b/test/core/UserRightsTest.php @@ -240,14 +240,69 @@ class UserRightsTest extends ItopDataTestCase 'User Portal ModuleInstallation' => array(2 , array('class' => 'ModuleInstallation', 'action' => 2, 'res' => true)), /* Configuration manager (2 = UR_ACTION_MODIFY) */ - 'Configuration manager FunctionalCI' => array(3 , array('class' => 'FunctionalCI', 'action' => 2, 'res' => true)), - 'Configuration manager UserRequest' => array(3 , array('class' => 'UserRequest', 'action' => 2, 'res' => false)), - 'Configuration manager URP_UserProfile' => array(3 , array('class' => 'URP_UserProfile', 'action' => 2, 'res' => false)), - 'Configuration manager UserLocal' => array(3 , array('class' => 'UserLocal', 'action' => 2, 'res' => false)), - 'Configuration manager ModuleInstallation' => array(3 , array('class' => 'ModuleInstallation', 'action' => 2, 'res' => true)), + 'Configuration manager FunctionalCI' => array(3, array('class' => 'FunctionalCI', 'action' => 2, 'res' => true)), + 'Configuration manager UserRequest' => array(3, array('class' => 'UserRequest', 'action' => 2, 'res' => false)), + 'Configuration manager URP_UserProfile' => array(3, array('class' => 'URP_UserProfile', 'action' => 2, 'res' => false)), + 'Configuration manager UserLocal' => array(3, array('class' => 'UserLocal', 'action' => 2, 'res' => false)), + 'Configuration manager ModuleInstallation' => array(3, array('class' => 'ModuleInstallation', 'action' => 2, 'res' => true)), ); return $aClassActionResult; } + /** + * @dataProvider GetUserInitialsProvider + * + * @param string $sLogin + * @param string $sExceptedInitials + * + * @throws \OQLException + */ + public function testGetUserInitials(string $sLogin, string $sExceptedInitials) + { + $sTestedInitials = UserRights::GetUserInitials($sLogin); + $this->assertEquals($sTestedInitials, $sExceptedInitials, "Initials for '$sLogin' don't match. Got '$sTestedInitials', expected '$sExceptedInitials'."); + } + + public function GetUserInitialsProvider() + { + return [ + 'One word, upper case letter' => [ + 'Carrie', + 'C', + ], + 'One word, lower case letter' => [ + 'carrie', + 'C', + ], + 'Application name' => [ + 'iTop', + 'I', + ], + 'Several words, upper case letters' => [ + 'Carrie Ann Moss', + 'CAM', + ], + 'Several words, mixed case letters' => [ + 'My name My name', + 'MM', + ], + 'Several words, upper case letters, two first hyphened' => [ + 'Lily-Rose Depp', + 'LRD', + ], + 'Several words, mixed case letters, two first hyphened' => [ + 'Lily-rose Depp', + 'LD', + ], + 'Several words, upper case letetrs, two last hypened' => [ + 'Jada Pinkett-Smith', + 'JPS', + ], + 'Several words, mixed case letters, two last hyphened' => [ + 'Jada Pinkett-smith', + 'JP', + ], + ]; + } }