diff --git a/application/utils.inc.php b/application/utils.inc.php index 45f484a13..a923a4914 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -2803,4 +2803,31 @@ HTML; { return str_replace(' ', '', ucwords(strtr($sInput, '_-', ' '))); } + + /** + * @param string $sInput + * + * @return string First letter of first word + first letter of any other word if capitalized + * @since 3.0.0 + */ + public static function ToAcronym(string $sInput): string + { + $sAcronym = ''; + // - Capitalize the first letter no matter what + $sReworkedInput = ucfirst($sInput); + // - Replace dashes with spaces to interpret all parts of the input + $sReworkedInput = str_replace('-', ' ', $sReworkedInput); + // - Explode input to check parts individually + $aInputParts = explode(' ', $sReworkedInput); + foreach ($aInputParts as $sInputPart) { + // 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', $sInputPart) > 0) { + $sAcronym .= mb_substr($sInputPart, 0, 1); + } + } + + return $sAcronym; + } } diff --git a/core/userrights.class.inc.php b/core/userrights.class.inc.php index f29bfda60..7d1f56f92 100644 --- a/core/userrights.class.inc.php +++ b/core/userrights.class.inc.php @@ -292,19 +292,16 @@ abstract class User extends cmdbAbstractObject * @throws \CoreException * @since 3.0.0 */ - public function GetInitials() + public function GetInitials(): string { $sInitials = ''; - if (MetaModel::IsValidAttCode(get_class($this), 'contactid') && ($this->Get('contactid') != 0)) - { - $sInitials .= mb_substr($this->Get('first_name'), 0, 1); - $sInitials .= mb_substr($this->Get('last_name'), 0, 1); + if (MetaModel::IsValidAttCode(get_class($this), 'contactid') && ($this->Get('contactid') != 0)) { + $sInitials = utils::ToAcronym($this->Get('contactid_friendlyname')); } - if (empty($sInitials)) - { - $sInitials = mb_substr($this->Get('login'), 0, 1); + if (empty($sInitials)) { + $sInitials = utils::ToAcronym($this->Get('login')); } return $sInitials; @@ -1268,34 +1265,15 @@ class UserRights */ public static function GetUserInitials($sLogin = '') { - if (empty($sLogin)) - { + if (empty($sLogin)) { $oUser = self::$m_oUser; - } - else - { + } else { $oUser = self::FindUser($sLogin); } - if (is_null($oUser)) - { - $sInitials = ''; - // - 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) { - $sInitials .= mb_substr($sLoginPart, 0, 1); - } - } - - return $sInitials; + if (is_null($oUser)) { + return utils::ToAcronym($sLogin); } + return $oUser->GetInitials(); } diff --git a/test/application/UtilsTest.php b/test/application/UtilsTest.php index 9eba027e3..61bbdce74 100644 --- a/test/application/UtilsTest.php +++ b/test/application/UtilsTest.php @@ -426,4 +426,59 @@ class UtilsTest extends \Combodo\iTop\Test\UnitTest\ItopTestCase ], ]; } + + /** + * @dataProvider ToAcronymProvider + * @covers utils::ToAcronym + * + * @param string $sInput + * @param string $sExceptedAcronym + */ + public function testToAcronym(string $sInput, string $sExceptedAcronym) + { + $sTestedAcronym = utils::ToAcronym($sInput); + $this->assertEquals($sTestedAcronym, $sExceptedAcronym, "Acronym for '$sInput' doesn't match. Got '$sTestedAcronym', expected '$sExceptedAcronym'."); + } + + public function ToAcronymProvider() + { + 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', + ], + ]; + } } diff --git a/test/core/UserRightsTest.php b/test/core/UserRightsTest.php index 18d3c5632..20ca4d880 100644 --- a/test/core/UserRightsTest.php +++ b/test/core/UserRightsTest.php @@ -249,60 +249,4 @@ class UserRightsTest extends ItopDataTestCase 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', - ], - ]; - } }