Fixed #19106 - tighten up user accessor to treat null and empty string the same

This commit is contained in:
snipe
2026-05-29 10:55:53 +01:00
parent c758fb4c83
commit 7bf8fd5eeb
3 changed files with 32 additions and 2 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ class UserImporter extends ItemImporter
// Pull the records from the CSV to determine their values
$this->item['id'] = trim($this->findCsvMatch($row, 'id'));
$this->item['username'] = trim($this->findCsvMatch($row, 'username'));
$this->item['display_name'] = trim($this->findCsvMatch($row, 'display_name'));
$this->item['display_name'] = trim($this->findCsvMatch($row, 'display_name')) ?: null;
$this->item['first_name'] = trim($this->findCsvMatch($row, 'first_name'));
$this->item['last_name'] = trim($this->findCsvMatch($row, 'last_name'));
$this->item['email'] = trim($this->findCsvMatch($row, 'email'));
+1 -1
View File
@@ -323,7 +323,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
protected function displayName(): Attribute
{
return Attribute::make(
get: fn (mixed $value) => $value ?? $this->getFullNameAttribute(),
get: fn (mixed $value) => ($value !== null && $value !== '') ? $value : $this->getFullNameAttribute(),
);
}
@@ -434,4 +434,34 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
$this->assertEquals('updated@example.com', $target->refresh()->email);
}
#[Test]
public function display_name_falls_back_to_full_name_when_column_is_missing_from_csv(): void
{
$importFileBuilder = ImportFileBuilder::new()->forget('displayName');
$row = $importFileBuilder->firstRow();
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newUser = User::query()->where('username', $row['username'])->sole();
$this->assertEquals("{$row['firstName']} {$row['lastName']}", $newUser->display_name);
}
#[Test]
public function display_name_falls_back_to_full_name_when_column_is_empty_in_csv(): void
{
$importFileBuilder = ImportFileBuilder::new(['displayName' => '']);
$row = $importFileBuilder->firstRow();
$import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk();
$newUser = User::query()->where('username', $row['username'])->sole();
$this->assertEquals("{$row['firstName']} {$row['lastName']}", $newUser->display_name);
}
}