Skip to content

Fix - getFriendlyNameFields() query to return only first name if only one is defined #19351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions phpunit/functional/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,84 @@ public function testGetFriendlyName($input, $rawname)
$this->assertSame($rawname, $user->getFriendlyName());
}

public function testGetFriendlyNameFields()
{
global $DB;

// Create test users with different combinations of name/firstname
$users_data = [
// User with both firstname and realname
[
'name' => 'user1',
'realname' => 'Doe',
'firstname' => 'John',
'expected' => 'Doe John' // Or 'John Doe' depending on names_format config
],
// User with only realname
[
'name' => 'user2',
'realname' => 'Smith',
'firstname' => '',
'expected' => 'Smith'
],
// User with only firstname
[
'name' => 'user3',
'realname' => '',
'firstname' => 'Alice',
'expected' => 'Alice'
],
// User with neither realname nor firstname
[
'name' => 'user4',
'realname' => '',
'firstname' => '',
'expected' => 'user4'
]
];

$this->login();

// Check names_format to adjust expected results if necessary
$config = \Config::getConfigurationValues('core');
if ($config['names_format'] == User::FIRSTNAME_BEFORE) {
$users_data[0]['expected'] = 'John Doe';
}

// Create all test users
$user = new \User();
$user_ids = [];

foreach ($users_data as &$user_data) {
$id = (int)$user->add([
'name' => $user_data['name'],
'realname' => $user_data['realname'],
'firstname' => $user_data['firstname'],
]);
$this->assertGreaterThan(0, $id);
$user_ids[] = $id;
}

// Test the SQL query properly using the GLPI query API
$alias = 'test_name';
$field_expr = User::getFriendlyNameFields($alias);

$iterator = $DB->request([
'SELECT' => ['id', new \QueryExpression($field_expr)],
'FROM' => 'glpi_users',
'WHERE' => ['id' => $user_ids],
'ORDER' => 'id'
]);

$index = 0;
foreach ($iterator as $row) {
// Verify we get the expected friendly name based on our test data
$this->assertEquals($users_data[$index]['expected'], $row[$alias],
"Failed for user with name={$users_data[$index]['name']}, realname={$users_data[$index]['realname']}, firstname={$users_data[$index]['firstname']}");
$index++;
}
}

public function testBlankPassword()
{
$input = [
Expand Down
11 changes: 6 additions & 5 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6565,11 +6565,12 @@ public static function getFriendlyNameFields(string $alias = "name")
$alias = DBmysql::quoteName($alias);
$name = DBmysql::quoteName($table . '.' . self::getNameField());

return new QueryExpression("IF(
$first <> '' && $second <> '',
CONCAT($first, ' ', $second),
$name
) AS $alias");
return new QueryExpression("CASE
WHEN $first <> '' AND $second <> '' THEN CONCAT($first, ' ', $second)
WHEN $first <> '' THEN $first
WHEN $second <> '' THEN $second
ELSE $name
END AS $alias");
}

public static function getIcon()
Expand Down
Loading