A free open source IT asset/license management system
Fixed #19900 - allow default location in asset importer
snipe0b86167c5cc3
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.phpindex 2349fffe5f..8e69304335 100644--- a/app/Importer/AssetImporter.php+++ b/app/Importer/AssetImporter.php@@ -61,6 +61,7 @@ class AssetImporter extends ItemImporter['category_id', 'category', fn ($v) => $this->createOrFetchCategory($v)],['company_id', 'company', fn ($v) => $this->createOrFetchCompany($v)],['location_id', 'location', fn ($v) => $this->createOrFetchLocation($v)],+ ['rtd_location_id', 'default_location', fn ($v) => $this->createOrFetchLocation($v)],['manufacturer_id', 'manufacturer', fn ($v) => $this->createOrFetchManufacturer($v)],['status_id', 'status', fn ($v) => $this->createOrFetchStatusLabel($v)],['supplier_id', 'supplier', fn ($v) => $this->createOrFetchSupplier($v)],@@ -274,10 +275,9 @@ class AssetImporter extends ItemImporter$item = $this->sanitizeItemForStoring($asset, $editingAsset);- // The location id fetched by the csv reader is actually the rtd_location_id.- // This will also set location_id, but then that will be overridden by the- // checkout method if necessary below.- if (isset($this->item['location_id'])) {+ if (isset($this->item['rtd_location_id'])) {+ $item['rtd_location_id'] = $this->item['rtd_location_id'];+ } elseif (isset($this->item['location_id'])) {$item['rtd_location_id'] = $this->item['location_id'];}diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.phpindex e95f68b40e..eda314bb5f 100644--- a/app/Livewire/Importer.php+++ b/app/Livewire/Importer.php@@ -416,6 +416,7 @@ class Importer extends Component'image' => trans('general.importer.image_filename'),'item_name' => trans('general.item_name_var', ['item' => trans('general.asset')]),'location' => trans('general.location'),+ 'default_location' => trans('admin/hardware/form.default_location'),'manufacturer' => trans('general.manufacturer'),'model_notes' => trans('general.item_notes', ['item' => trans('admin/hardware/form.model')]),'model_number' => trans('general.model_no'),diff --git a/tests/Feature/Importing/AssetImportDefaultLocationTest.php b/tests/Feature/Importing/AssetImportDefaultLocationTest.phpnew file mode 100644index 0000000000..340fc27368--- /dev/null+++ b/tests/Feature/Importing/AssetImportDefaultLocationTest.php@@ -0,0 +1,143 @@+<?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;++class AssetImportDefaultLocationTest extends TestCase+{+ /** @var array<int, string> */+ private array $uploadedTempPaths = [];++ protected function tearDown(): void+ {+ // tempnam() writes into /tmp and Laravel's test harness doesn't+ // clean them up by itself, so we track + unlink per-test to keep+ // CI runners' /tmp from accreting a CSV per import test. Also+ // clean out the imports storage directory for files the import+ // controller moved into place during the test.+ foreach ($this->uploadedTempPaths as $path) {+ @unlink($path);+ }+ foreach (Import::query()->get() as $import) {+ @unlink(config('app.private_uploads').'/imports/'.$import->file_path);+ }+ parent::tearDown();+ }++ private function createFakeUploadedFile(string $filename, string $content): UploadedFile+ {+ $path = tempnam(sys_get_temp_dir(), 'csv');+ file_put_contents($path, $content);+ $this->uploadedTempPaths[] = $path;++ return new UploadedFile($path, $filename, 'text/csv', null, true);+ }++ private function runAssetImport(string $csv, array $mappings): void+ {+ Category::factory()->create(['category_type' => 'asset', 'name' => 'Default Loc Test Category']);+ Statuslabel::factory()->create(['name' => 'Default Loc Test Status']);+ Company::factory()->create();+ AssetModel::factory()+ ->for(Category::where('name', 'Default Loc Test Category')->first(), 'category')+ ->create(['name' => 'Default Loc Test Model']);++ $importer = User::factory()->canImport()->create();++ $this->actingAsForApi($importer)+ ->postJson(route('api.imports.store'), [+ 'files' => [+ $this->createFakeUploadedFile('default-loc.csv', $csv),+ ],+ ])+ ->assertSuccessful();++ $import = Import::latest()->first();++ $this->actingAsForApi($importer)+ ->postJson(route('api.imports.importFile', $import->id), [+ 'import-type' => 'asset',+ 'column-mappings' => $mappings,+ ])+ ->assertSuccessful();+ }++ #[Test]+ public function default_location_only_writes_rtd_location_without_touching_current_location(): void+ {+ $newDefault = Location::factory()->create(['name' => 'New Default Loc']);++ $csv = "asset tag,item name,category,status,model name,default location\n";+ $csv .= "DL-001,Default-only Asset,Default Loc Test Category,Default Loc Test Status,Default Loc Test Model,{$newDefault->name}\n";++ $this->runAssetImport($csv, [+ 'asset tag' => 'asset_tag',+ 'item name' => 'item_name',+ 'category' => 'category',+ 'status' => 'status',+ 'model name' => 'asset_model',+ 'default location' => 'default_location',+ ]);++ $imported = Asset::where('asset_tag', 'DL-001')->firstOrFail();+ $this->assertSame($newDefault->id, $imported->rtd_location_id);+ $this->assertNull($imported->location_id);+ }++ #[Test]+ public function location_only_mirrors_to_both_current_and_default_location_for_back_compat(): void+ {+ $target = Location::factory()->create(['name' => 'Mirror Loc']);++ $csv = "asset tag,item name,category,status,model name,location\n";+ $csv .= "DL-002,Mirror Asset,Default Loc Test Category,Default Loc Test Status,Default Loc Test Model,{$target->name}\n";++ $this->runAssetImport($csv, [+ 'asset tag' => 'asset_tag',+ 'item name' => 'item_name',+ 'category' => 'category',+ 'status' => 'status',+ 'model name' => 'asset_model',+ 'location' => 'location',+ ]);++ $imported = Asset::where('asset_tag', 'DL-002')->firstOrFail();+ $this->assertSame($target->id, $imported->location_id);+ $this->assertSame($target->id, $imported->rtd_location_id);+ }++ #[Test]+ public function explicit_default_location_wins_over_mirror_from_location_when_both_present(): void+ {+ $currentLoc = Location::factory()->create(['name' => 'Current Office']);+ $defaultLoc = Location::factory()->create(['name' => 'Warehouse HQ']);++ $csv = "asset tag,item name,category,status,model name,location,default location\n";+ $csv .= "DL-003,Both-cols Asset,Default Loc Test Category,Default Loc Test Status,Default Loc Test Model,{$currentLoc->name},{$defaultLoc->name}\n";++ $this->runAssetImport($csv, [+ 'asset tag' => 'asset_tag',+ 'item name' => 'item_name',+ 'category' => 'category',+ 'status' => 'status',+ 'model name' => 'asset_model',+ 'location' => 'location',+ 'default location' => 'default_location',+ ]);++ $imported = Asset::where('asset_tag', 'DL-003')->firstOrFail();+ $this->assertSame($currentLoc->id, $imported->location_id);+ $this->assertSame($defaultLoc->id, $imported->rtd_location_id);+ }+}