A free open source IT asset/license management system
Add option to skip nulling null fields
snipe1613981fe9b6
diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.phpindex 56c8624518..40dcd03261 100644--- a/resources/lang/en-US/general.php+++ b/resources/lang/en-US/general.php@@ -306,6 +306,9 @@ return ['pagination_info' => 'Showing _START_ to _END_ of _TOTAL_ items','pending' => 'Pending','people' => 'People',+ 'preserve_blank_cells_on_update' => 'Skip updating fields with blank cells',+ 'preserve_blank_cells_on_update_help' => 'By default, a blank cell in a mapped column overwrites the existing value on update. Turn this on to skip blank cells so the existing value stays put. Columns you leave out of your CSV entirely are always preserved regardless of this setting.',+ 'update_mode_help' => 'When enabled, existing records matched by identity (serial, asset tag, username, etc.) are updated instead of skipped. Any column in your CSV with an empty value will clear the corresponding field on the existing record. Columns you leave out of your CSV entirely are not touched, so existing values are preserved. Required fields (like name and seats on a license) cannot be cleared. Leaving them empty will produce a validation error for that row.','per_page' => 'Results Per Page','previous' => 'Previous','previous_page' => 'Previous Page',diff --git a/resources/views/livewire/importer.blade.php b/resources/views/livewire/importer.blade.phpindex 24a8d10e0f..17483082af 100644--- a/resources/views/livewire/importer.blade.php+++ b/resources/views/livewire/importer.blade.php@@ -416,10 +416,24 @@<x-form.checkbox-rowname="update":label="trans('general.update_existing_values')"- :help_text="trans('admin/hardware/message.import.update_mode_help')"+ :help_text="trans('general.update_mode_help')":checked="(bool) $update"wire:model.live="update"/>++ {{-- Only useful when Update Existing Values+ is on. Default to clear-blanks behavior+ unless the importing user explicitly opts in to+ preserving DB values on blank CSV cells. --}}+ @if ($update)+ <x-form.checkbox-row+ name="preserve_blanks"+ :label="trans('general.preserve_blank_cells_on_update')"+ :help_text="trans('general.preserve_blank_cells_on_update_help')"+ :checked="(bool) $preserve_blanks"+ wire:model.live="preserve_blanks"+ />+ @endif@endif@if ($typeOfImport === 'asset' && $snipeSettings->auto_increment_assets == 1 && $update)@@ -1096,6 +1110,7 @@var isLastSlice = (sliceIndex === totalSlices - 1);var payload = {'import-update': !!$wire.$get('update'),+ 'import-preserve-blanks': !!$wire.$get('preserve_blanks'),'send-welcome': !!$wire.$get('send_welcome'),'import-type': $wire.$get('typeOfImport'),// run-backup only makes sense before the firstdiff --git a/tests/Feature/Importing/AssetImportPreserveBlanksTest.php b/tests/Feature/Importing/AssetImportPreserveBlanksTest.phpnew file mode 100644index 0000000000..3060f68792--- /dev/null+++ b/tests/Feature/Importing/AssetImportPreserveBlanksTest.php@@ -0,0 +1,157 @@+<?php++namespace Tests\Feature\Importing;++use App\Models\Asset;+use App\Models\AssetModel;+use App\Models\Category;+use App\Models\Company;+use App\Models\Import;+use App\Models\Location;+use App\Models\Statuslabel;+use App\Models\User;+use Illuminate\Http\UploadedFile;+use PHPUnit\Framework\Attributes\Test;+use Tests\TestCase;++/**+ * Coverage for the "Preserve blank CSV cells on update" wizard opt-in.+ * Default behavior on an update-mode import is that a present-but-empty+ * CSV cell clears the corresponding DB column. When the importer passes+ * `import-preserve-blanks=1`, that clear-on-blank step is skipped so+ * blank cells keep whatever value is already in the DB.+ */+class AssetImportPreserveBlanksTest extends TestCase+{+ #[Test]+ public function default_update_clears_db_value_when_csv_cell_is_blank()+ {+ [$asset, $category, $statusLabel, $importer] = $this->seedAssetForUpdate();++ $csv = "asset tag,item name,category,status,asset_notes\n";+ $csv .= "TEST-001,Test Asset,{$category->name},{$statusLabel->name},\n";++ $this->uploadAndProcess($importer, $csv, [+ 'import-type' => 'asset',+ 'import-update' => true,+ 'column-mappings' => [+ 'asset tag' => 'asset_tag',+ 'item name' => 'item_name',+ 'category' => 'category',+ 'status' => 'status',+ 'asset_notes' => 'asset_notes',+ ],+ ]);++ $asset->refresh();+ $this->assertNull(+ $asset->notes,+ 'A blank asset_notes cell should clear the DB value under default behavior.'+ );+ }++ #[Test]+ public function preserve_blanks_keeps_db_value_when_csv_cell_is_blank()+ {+ [$asset, $category, $statusLabel, $importer] = $this->seedAssetForUpdate();++ $csv = "asset tag,item name,category,status,asset_notes\n";+ $csv .= "TEST-001,Test Asset,{$category->name},{$statusLabel->name},\n";++ $this->uploadAndProcess($importer, $csv, [+ 'import-type' => 'asset',+ 'import-update' => true,+ 'import-preserve-blanks' => true,+ 'column-mappings' => [+ 'asset tag' => 'asset_tag',+ 'item name' => 'item_name',+ 'category' => 'category',+ 'status' => 'status',+ 'asset_notes' => 'asset_notes',+ ],+ ]);++ $asset->refresh();+ $this->assertSame(+ 'Original notes',+ $asset->notes,+ 'A blank asset_notes cell should keep the existing DB value when preserve-blanks is on.'+ );+ }++ #[Test]+ public function preserve_blanks_still_updates_populated_cells()+ {+ [$asset, $category, $statusLabel, $importer] = $this->seedAssetForUpdate();++ $csv = "asset tag,item name,category,status,asset_notes\n";+ $csv .= "TEST-001,Renamed Asset,{$category->name},{$statusLabel->name},\n";++ $this->uploadAndProcess($importer, $csv, [+ 'import-type' => 'asset',+ 'import-update' => true,+ 'import-preserve-blanks' => true,+ 'column-mappings' => [+ 'asset tag' => 'asset_tag',+ 'item name' => 'item_name',+ 'category' => 'category',+ 'status' => 'status',+ 'asset_notes' => 'asset_notes',+ ],+ ]);++ $asset->refresh();+ $this->assertSame('Renamed Asset', $asset->name, 'Populated cells must still update under preserve-blanks.');+ $this->assertSame('Original notes', $asset->notes, 'Blank asset_notes cell must be preserved alongside the name update.');+ }++ /**+ * @return array{0: Asset, 1: Category, 2: Statuslabel, 3: User}+ */+ private function seedAssetForUpdate(): array+ {+ $category = Category::factory()->create(['category_type' => 'asset']);+ $location = Location::factory()->create();+ $statusLabel = Statuslabel::factory()->create();+ $company = Company::factory()->create();+ $assetModel = AssetModel::factory()->for($category, 'category')->create();++ $asset = Asset::factory()+ ->for($assetModel, 'model')+ ->for($statusLabel, 'status')+ ->for($location, 'defaultLoc')+ ->for($company, 'company')+ ->create([+ 'asset_tag' => 'TEST-001',+ 'name' => 'Test Asset',+ 'notes' => 'Original notes',+ ]);++ $importer = User::factory()->canImport()->create();++ return [$asset, $category, $statusLabel, $importer];+ }++ private function uploadAndProcess(User $actor, string $csv, array $processPayload): void+ {+ $this->actingAsForApi($actor)+ ->postJson(route('api.imports.store'), [+ 'files' => [$this->fakeUploadedFile('test.csv', $csv)],+ ])+ ->assertSuccessful();++ $import = Import::latest()->first();++ $this->actingAsForApi($actor)+ ->postJson(route('api.imports.importFile', $import->id), $processPayload)+ ->assertSuccessful();+ }++ private function fakeUploadedFile(string $filename, string $content): UploadedFile+ {+ $path = tempnam(sys_get_temp_dir(), 'csv');+ file_put_contents($path, $content);++ return new UploadedFile($path, $filename, 'text/csv', null, true);+ }+}