Skip to content

A free open source IT asset/license management system

CSV Import: User importer rejects entire row when start/end dates are not exactly Y-m-d

TowyTowy0249be3b7cfa
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index a5b530a299..9851d17295 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -90,7 +90,9 @@ class UserImporter extends ItemImporter
$this->item['state'] = trim($this->findCsvMatch($row, 'state'));
$this->item['country'] = trim($this->findCsvMatch($row, 'country'));
$this->item['start_date'] = trim($this->findCsvMatch($row, 'start_date'));
+ $this->item['start_date'] = $this->parseOrNullDate('start_date');
$this->item['end_date'] = trim($this->findCsvMatch($row, 'end_date'));
+ $this->item['end_date'] = $this->parseOrNullDate('end_date');
$this->item['zip'] = trim($this->findCsvMatch($row, 'zip'));
$this->item['activated'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'activated'))) == 1) ? '1' : 0;
$this->item['employee_num'] = trim($this->findCsvMatch($row, 'employee_num'));
@@ -100,8 +102,6 @@ class UserImporter extends ItemImporter
$this->item['vip'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'vip'))) == 1) ? '1' : 0;
$this->item['autoassign_licenses'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'autoassign_licenses'))) == 1) ? '1' : 0;
- $this->handleEmptyStringsForDates();
-
$user_department = trim($this->findCsvMatch($row, 'department'));
if ($this->shouldUpdateField($user_department)) {
$this->item['department_id'] = $this->createOrFetchDepartment($user_department);
@@ -334,15 +334,4 @@ class UserImporter extends ItemImporter
return in_array($location->company_id, $companyIds);
}
-
- private function handleEmptyStringsForDates(): void
- {
- if ($this->item['start_date'] === '') {
- $this->item['start_date'] = null;
- }
-
- if ($this->item['end_date'] === '') {
- $this->item['end_date'] = null;
- }
- }
}
diff --git a/tests/Feature/Importing/Api/ImportUsersTest.php b/tests/Feature/Importing/Api/ImportUsersTest.php
index f6a0fc6bf3..ed5538fae8 100644
--- a/tests/Feature/Importing/Api/ImportUsersTest.php
+++ b/tests/Feature/Importing/Api/ImportUsersTest.php
@@ -118,6 +118,44 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
$this->assertEquals(0, $newUser->activated);
}
+ #[Test]
+ public function parses_non_iso_start_and_end_dates(): void
+ {
+ $row = ImportFileBuilder::new()->definition();
+ $row['start_date'] = '07/28/2025';
+ $row['end_date'] = '12/31/2025';
+
+ $importFileBuilder = new ImportFileBuilder([$row]);
+ $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('2025-07-28', $newUser->start_date);
+ $this->assertEquals('2025-12-31', $newUser->end_date);
+ }
+
+ #[Test]
+ public function stores_null_when_start_or_end_date_is_unparseable(): void
+ {
+ $row = ImportFileBuilder::new()->definition();
+ $row['start_date'] = 'not-a-date';
+ $row['end_date'] = 'also-not-a-date';
+
+ $importFileBuilder = new ImportFileBuilder([$row]);
+ $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->assertNull($newUser->start_date);
+ $this->assertNull($newUser->end_date);
+ }
+
#[Test]
public function will_ignore_unknown_columns_when_file_contains_unknown_columns(): void
{