diff --git a/phpunit/functional/UserTest.php b/phpunit/functional/UserTest.php index c06f90c073c..b0bdf7a7b1f 100644 --- a/phpunit/functional/UserTest.php +++ b/phpunit/functional/UserTest.php @@ -2120,4 +2120,102 @@ public function testReapplyRightRules() $user->reapplyRightRules(); $this->assertNotContains($profiles_id, Profile_User::getUserProfiles($user->getID())); } + + public static function testGetFriendlyNameFieldsProvider() + { + return [ + [ + 'input' => [ + 'name' => 'login_only', + ], + 'names_format' => User::REALNAME_BEFORE, + 'expected' => 'login_only', + ], + [ + 'input' => [ + 'name' => 'firstname_only', + 'firstname' => 'firstname', + ], + 'names_format' => User::REALNAME_BEFORE, + 'expected' => 'firstname_only', + ], + [ + 'input' => [ + 'name' => 'lastname_only', + 'realname' => 'lastname', + ], + 'names_format' => User::REALNAME_BEFORE, + 'expected' => 'lastname_only', + ], + [ + 'input' => [ + 'name' => 'firstname_lastname', + 'firstname' => 'firstname', + 'realname' => 'lastname', + ], + 'names_format' => User::REALNAME_BEFORE, + 'expected' => 'lastname firstname', + ], + [ + 'input' => [ + 'name' => 'login_only', + ], + 'names_format' => User::FIRSTNAME_BEFORE, + 'expected' => 'login_only', + ], + [ + 'input' => [ + 'name' => 'firstname_only', + 'firstname' => 'firstname', + ], + 'names_format' => User::FIRSTNAME_BEFORE, + 'expected' => 'firstname_only', + ], + [ + 'input' => [ + 'name' => 'lastname_only', + 'realname' => 'lastname', + ], + 'names_format' => User::FIRSTNAME_BEFORE, + 'expected' => 'lastname_only', + ], + [ + 'input' => [ + 'name' => 'firstname_lastname', + 'firstname' => 'firstname', + 'realname' => 'lastname', + ], + 'names_format' => User::FIRSTNAME_BEFORE, + 'expected' => 'firstname lastname', + ], + ]; + } + + #[DataProvider('testGetFriendlyNameFieldsProvider')] + public function testGetFriendlyNameFields( + array $input, + int $names_format, + string $expected + ) { + /** @var \DBmysql $DB */ + global $DB; + + \Config::setConfigurationValues('core', ['names_format' => $names_format]); + + $user = $this->createItem('User', $input); + + $query = [ + 'SELECT' => [ + User::getFriendlyNameFields(), + ], + 'FROM' => [ + User::getTable(), + ], + 'WHERE' => [ + 'id' => $user->fields['id'], + ] + ]; + $result = $DB->request($query)->current(); + $this->assertSame($expected, $result['name']); + } } diff --git a/src/Item_SoftwareLicense.php b/src/Item_SoftwareLicense.php index 14897ca866f..4a76ccf2c1b 100644 --- a/src/Item_SoftwareLicense.php +++ b/src/Item_SoftwareLicense.php @@ -783,7 +783,7 @@ function updateItemDropdown(itemtype_el) { 'glpi_softwarelicenses.name AS license', 'glpi_softwarelicenses.id AS vID', 'glpi_softwarelicenses.softwares_id AS softid', - "$users_table.name AS itemname", + User::getFriendlyNameFields('itemname'), "$users_table.id AS iID", new QueryExpression($DB::quoteValue(User::class), 'item_type'), new QueryExpression($DB::quoteValue(''), "serial"), diff --git a/src/User.php b/src/User.php index cb566d0d508..54534ffc2c9 100644 --- a/src/User.php +++ b/src/User.php @@ -6735,8 +6735,8 @@ public static function getFriendlyNameFields(string $alias = "name") $table = self::getTable(); return QueryFunction::if( condition: [ - "$table.$first" => ['<>' => ''], - "$table.$second" => ['<>' => ''] + "$table.$first" => ['<>', ''], + "$table.$second" => ['<>', ''] ], true_expression: QueryFunction::concat(["$table.$first", new QueryExpression($DB::quoteValue(' ')), "$table.$second"]), false_expression: $table . '.' . self::getNameField(),