Skip to content

Commit

Permalink
fix(licence): user name display (#18776)
Browse files Browse the repository at this point in the history
* fix(licence): user name display

* add tests
  • Loading branch information
Rom1-B authored Jan 22, 2025
1 parent 5f8a4a1 commit 8f97759
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
98 changes: 98 additions & 0 deletions phpunit/functional/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
2 changes: 1 addition & 1 deletion src/Item_SoftwareLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
4 changes: 2 additions & 2 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 8f97759

Please sign in to comment.