Fix UserRights::GetUserInitials() for login starting with a lower case letter

This commit is contained in:
Molkobain
2021-03-11 16:04:45 +01:00
parent c1564fdcc6
commit 3380b8896a
2 changed files with 68 additions and 10 deletions

View File

@@ -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);
}
}

View File

@@ -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',
],
];
}
}