A free open source IT asset/license management system
Added tests
snipe03053d4d52ee
diff --git a/tests/Feature/Importing/Api/ImportAccessoriesTest.php b/tests/Feature/Importing/Api/ImportAccessoriesTest.phpindex de668c742e..c2c95bd163 100644--- a/tests/Feature/Importing/Api/ImportAccessoriesTest.php+++ b/tests/Feature/Importing/Api/ImportAccessoriesTest.php@@ -457,4 +457,62 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio$this->assertEquals($row['category'], $newAccessory->manufacturer->name);$this->assertEquals($row['purchaseCost'], $newAccessory->location->name);}++ #[Test]+ public function accessory_import_checks_out_to_user_when_username_matches(): void+ {+ $actor = User::factory()->superuser()->create();+ $target = User::factory()->create(['username' => 'accessorytarget']);++ // Hand-crafted CSV: needs the checkout columns+ // (checkout_class + username) alongside the standard accessory+ // columns, which the AccessoriesImportFileBuilder doesn't include.+ $csv = "Item Name,Category,Quantity,Company,Checkout Type,Username\n"+ .'CSV-Checked-Accessory,Cables,5,CSVCo,user,'.$target->username."\n";+ $filename = 'accessory-checkout-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = Import::factory()->accessory()->create(['file_path' => $filename]);++ $this->actingAsForApi($actor);+ $this->importFileResponse(['import' => $import->id])->assertOk();++ $accessory = Accessory::query()->where('name', 'CSV-Checked-Accessory')->sole();+ $this->assertEquals(5, $accessory->qty);++ $this->assertDatabaseHas('accessories_checkout', [+ 'accessory_id' => $accessory->id,+ 'assigned_to' => $target->id,+ 'assigned_type' => User::class,+ ]);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }++ #[Test]+ public function accessory_import_without_checkout_columns_does_not_create_checkout(): void+ {+ $actor = User::factory()->superuser()->create();++ $csv = "Item Name,Category,Quantity,Company\n"+ ."CSV-Uncheckedout-Accessory,Cables,3,CSVCo\n";+ $filename = 'accessory-nocheckout-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = Import::factory()->accessory()->create(['file_path' => $filename]);++ $this->actingAsForApi($actor);+ $this->importFileResponse(['import' => $import->id])->assertOk();++ $accessory = Accessory::query()->where('name', 'CSV-Uncheckedout-Accessory')->sole();+ $this->assertDatabaseMissing('accessories_checkout', [+ 'accessory_id' => $accessory->id,+ ]);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }}diff --git a/tests/Feature/Importing/Api/ImportConsumablesTest.php b/tests/Feature/Importing/Api/ImportConsumablesTest.phpindex fd211913ea..5bf8e3a375 100644--- a/tests/Feature/Importing/Api/ImportConsumablesTest.php+++ b/tests/Feature/Importing/Api/ImportConsumablesTest.php@@ -346,4 +346,61 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio$this->assertNull($newConsumable->manufacturer_id);$this->assertNull($newConsumable->notes);}++ #[Test]+ public function consumable_import_checks_out_to_user_when_username_matches(): void+ {+ $actor = User::factory()->superuser()->create();+ $target = User::factory()->create(['username' => 'consumabletarget']);++ $csv = "Item Name,Category,Quantity,Company,Checkout Type,Username\n"+ .'CSV-Checked-Consumable,Toner,10,CSVCo,user,'.$target->username."\n";+ $filename = 'consumable-checkout-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = Import::factory()->consumable()->create(['file_path' => $filename]);++ $this->actingAsForApi($actor);+ $this->importFileResponse(['import' => $import->id])->assertOk();++ $consumable = Consumable::query()->where('name', 'CSV-Checked-Consumable')->sole();+ $this->assertEquals(10, $consumable->qty);++ $this->assertDatabaseHas('consumables_users', [+ 'consumable_id' => $consumable->id,+ 'assigned_to' => $target->id,+ ]);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }++ #[Test]+ public function consumable_import_ignores_location_checkout_target(): void+ {+ // consumables_users only supports user targets - a location+ // target in the CSV should be silently skipped rather than+ // producing an invalid pivot row.+ $actor = User::factory()->superuser()->create();++ $csv = "Item Name,Category,Quantity,Company,Checkout Type,Checkout Location\n"+ ."CSV-Loc-Consumable,Toner,4,CSVCo,location,HQ\n";+ $filename = 'consumable-loc-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = Import::factory()->consumable()->create(['file_path' => $filename]);++ $this->actingAsForApi($actor);+ $this->importFileResponse(['import' => $import->id])->assertOk();++ $consumable = Consumable::query()->where('name', 'CSV-Loc-Consumable')->sole();+ $this->assertDatabaseMissing('consumables_users', [+ 'consumable_id' => $consumable->id,+ ]);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }}diff --git a/tests/Feature/Livewire/ImporterTest.php b/tests/Feature/Livewire/ImporterTest.phpindex cdbedc913c..9aa6e94a72 100644--- a/tests/Feature/Livewire/ImporterTest.php+++ b/tests/Feature/Livewire/ImporterTest.php@@ -266,4 +266,441 @@ class ImporterTest extends TestCase$this->assertContains($import->id, $visibleIds);}}++ public function test_selecting_a_file_dispatches_open_import_modal_event(): void+ {+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'header_row' => ['asset tag'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->assertDispatched('open-import-modal');+ }++ public function test_selecting_a_file_populates_row_count_from_csv(): void+ {+ $user = User::factory()->canImport()->create();++ $csv = "asset tag,name,email,checkout date\n"+ ."AH-1,alice,,2025-01-01\n"+ ."AH-2,bob,,2025-01-02\n"+ ."AH-3,carol,,2025-01-03\n";++ $filename = 'row-count-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'file_path' => $filename,+ 'header_row' => ['asset tag', 'name', 'email', 'checkout date'],+ 'first_row' => ['AH-1', 'alice', '', '2025-01-01'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->assertSet('activeFileRowCount', 3);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }++ public function test_row_count_is_zero_when_csv_file_is_missing_from_disk(): void+ {+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'file_path' => 'never-existed-'.uniqid().'.csv',+ 'header_row' => ['asset tag'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->assertSet('activeFileRowCount', 0);+ }++ public function test_next_step_from_type_selection_advances_when_type_is_set(): void+ {+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'header_row' => ['asset tag'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->assertSet('wizardStep', 1)+ ->set('typeOfImport', 'user')+ ->call('nextStep')+ ->assertSet('wizardStep', 2);+ }++ public function test_next_step_blocks_when_no_type_is_selected(): void+ {+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'header_row' => ['asset tag'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->set('typeOfImport', '')+ ->call('nextStep')+ ->assertSet('wizardStep', 1)+ ->assertSet('statusType', 'error');+ }++ public function test_previous_step_walks_backwards(): void+ {+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'header_row' => ['asset tag'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->set('typeOfImport', 'user')+ ->call('nextStep') // step 2+ ->call('previousStep')+ ->assertSet('wizardStep', 1);+ }++ public function test_next_step_from_mapping_blocks_when_required_field_is_unmapped(): void+ {+ Storage::fake();+ $user = User::factory()->canImport()->create();+ // User import requires first_name (per User::$rules); leave field_map+ // pointing at nothing useful so the required check fires.+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'header_row' => ['Some Column', 'Another Column'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->set('typeOfImport', 'user')+ ->call('nextStep') // -> step 2+ ->set('field_map', [null, null]) // nothing mapped+ ->call('nextStep') // should block+ ->assertSet('wizardStep', 2)+ ->assertSet('statusType', 'error');+ }++ public function test_next_step_from_mapping_advances_when_required_fields_are_mapped(): void+ {+ $user = User::factory()->canImport()->create();++ $csv = "First Name,Username\nAlice,alice\nBob,bob\n";+ $filename = 'wiz-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'file_path' => $filename,+ 'header_row' => ['First Name', 'Username'],+ 'first_row' => ['Alice', 'alice'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->set('typeOfImport', 'user')+ ->call('nextStep') // -> step 2+ ->set('field_map', ['first_name', 'username'])+ ->call('nextStep') // -> step 3+ ->assertSet('wizardStep', 3)+ ->assertCount('previewRows', 2);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }++ public function test_asset_history_required_fields_are_hardcoded_not_from_model(): void+ {+ Livewire::actingAs(User::factory()->canImport()->create())+ ->test(Importer::class)+ ->tap(function ($c) {+ $this->assertEquals(+ ['asset_tag', 'full_name', 'checkout_date'],+ $c->instance()->requiredForType('assetHistory'),+ );+ });+ }++ public function test_user_required_fields_derived_from_user_model_rules(): void+ {+ // User::$rules requires first_name; requiredForType('user') should+ // include it even though we didn't hardcode it.+ Livewire::actingAs(User::factory()->canImport()->create())+ ->test(Importer::class)+ ->tap(function ($c) {+ $required = $c->instance()->requiredForType('user');+ $this->assertContains('first_name', $required);+ });+ }++ public function test_next_step_auto_maps_fields_even_when_type_was_preselected(): void+ {+ // Regression: updatingTypeOfImport only fires on wire-model changes.+ // When selectFile programmatically assigns import_type, the auto-map+ // never runs and users land on step 2 with an all-null field_map.+ // nextStep() 1->2 now re-runs the auto-map so headers still bind.+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'import_type' => 'user',+ 'header_row' => ['First Name', 'Username', 'Email'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->call('nextStep')+ ->tap(function ($c) {+ $map = $c->get('field_map');+ $this->assertContains('first_name', $map);+ $this->assertContains('username', $map);+ $this->assertContains('email', $map);+ });+ }++ public function test_auto_map_handles_underscore_style_headers(): void+ {+ // Regression: CSV column "asset_tag" (underscore) should still bind+ // to the asset_tag target field, even though the target's display+ // label is "Asset Tag" (with a space).+ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create([+ 'created_by' => $user->id,+ 'import_type' => 'asset',+ 'header_row' => ['asset_tag', 'serial_number', 'purchase_cost'],+ ]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('selectFile', $import->id)+ ->call('nextStep')+ ->tap(function ($c) {+ $map = $c->get('field_map');+ $this->assertContains('asset_tag', $map);+ $this->assertContains('serial', $map);+ $this->assertContains('purchase_cost', $map);+ });+ }++ public function test_demo_mode_blocks_start_processing(): void+ {+ // With lock_passwords set the Process button on the wizard is+ // disabled in the blade, but a hand-crafted Livewire call would+ // still fire the action - guard it server-side so the modal can't+ // flip into processing mode either.+ config(['app.lock_passwords' => true]);++ $user = User::factory()->canImport()->create();++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('startProcessing')+ ->assertSet('processing', false)+ ->assertSet('message_type', 'danger');+ }++ public function test_demo_mode_blocks_destroy(): void+ {+ config(['app.lock_passwords' => true]);++ Storage::fake();+ $user = User::factory()->canImport()->create();+ $import = Import::factory()->create(['created_by' => $user->id]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->call('destroy', $import->id)+ ->assertSet('message_type', 'danger');++ $this->assertDatabaseHas('imports', ['id' => $import->id]);+ }++ public function test_demo_mode_blocks_bulk_destroy(): void+ {+ config(['app.lock_passwords' => true]);++ Storage::fake();+ $user = User::factory()->canImport()->create();+ $imports = Import::factory()->count(2)->create(['created_by' => $user->id]);++ Livewire::actingAs($user)+ ->test(Importer::class)+ ->set('selectedIds', $imports->pluck('id')->map(fn ($id) => (string) $id)->all())+ ->call('bulkDestroy')+ ->assertSet('message_type', 'danger');++ foreach ($imports as $import) {+ $this->assertDatabaseHas('imports', ['id' => $import->id]);+ }+ }++ public function test_asset_model_required_field_uses_name_key_not_item_name(): void+ {+ // Regression: earlier iteration of required_field_model_map+ // pointed assetModel at 'item_name' but AssetModel's mapping+ // dropdown uses 'name' as the option value. Result was that the+ // dropdown never lit up as required for a mapped Name column.+ Livewire::actingAs(User::factory()->canImport()->create())+ ->test(Importer::class)+ ->tap(function ($c) {+ $required = $c->instance()->requiredForType('assetModel');+ $this->assertContains('name', $required);+ $this->assertNotContains('item_name', $required);+ });+ }++ public function test_sliced_import_processes_only_the_requested_slice(): void+ {+ $actor = User::factory()->canImport()->superuser()->create();++ // 5-row CSV, slice size 2 starting at offset 1: should touch rows+ // 2 and 3 only. Rows 0, 1 (offset 0), and 4 (past offset+limit)+ // should be untouched.+ $csv = "First Name,Username\n"+ ."alice,alice.slice\n"+ ."bob,bob.slice\n"+ ."carol,carol.slice\n"+ ."dave,dave.slice\n"+ ."eve,eve.slice\n";++ $filename = 'slice-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = \App\Models\Import::factory()->create([+ 'created_by' => $actor->id,+ 'file_path' => $filename,+ 'import_type' => 'user',+ 'header_row' => ['First Name', 'Username'],+ 'first_row' => ['alice', 'alice.slice'],+ ]);++ $this->actingAsForApi($actor);+ $this->postJson(+ route('api.imports.importFile', ['import' => $import->id]),+ [+ 'import-type' => 'user',+ 'import' => $import->id,+ 'offset' => 1,+ 'limit' => 2,+ ],+ )->assertOk();++ $this->assertDatabaseMissing('users', ['username' => 'alice.slice']);+ $this->assertDatabaseHas('users', ['username' => 'bob.slice']);+ $this->assertDatabaseHas('users', ['username' => 'carol.slice']);+ $this->assertDatabaseMissing('users', ['username' => 'dave.slice']);+ $this->assertDatabaseMissing('users', ['username' => 'eve.slice']);+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }++ public function test_all_slices_together_reach_every_row(): void+ {+ $actor = User::factory()->canImport()->superuser()->create();++ $csv = "First Name,Username\n"+ ."alice,alice.all\n"+ ."bob,bob.all\n"+ ."carol,carol.all\n"+ ."dave,dave.all\n";++ $filename = 'sliceall-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = \App\Models\Import::factory()->create([+ 'created_by' => $actor->id,+ 'file_path' => $filename,+ 'import_type' => 'user',+ 'header_row' => ['First Name', 'Username'],+ 'first_row' => ['alice', 'alice.all'],+ ]);++ $this->actingAsForApi($actor);++ // Two slices of 2 rows each.+ foreach ([0, 2] as $offset) {+ $this->postJson(+ route('api.imports.importFile', ['import' => $import->id]),+ [+ 'import-type' => 'user',+ 'import' => $import->id,+ 'offset' => $offset,+ 'limit' => 2,+ ],+ )->assertOk();+ }++ foreach (['alice.all', 'bob.all', 'carol.all', 'dave.all'] as $username) {+ $this->assertDatabaseHas('users', ['username' => $username]);+ }+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }++ public function test_import_without_offset_and_limit_processes_whole_file(): void+ {+ // Backwards-compat: any caller that doesn't send offset/limit+ // should see the pre-slicing behavior - the whole CSV in one shot.+ $actor = User::factory()->canImport()->superuser()->create();++ $csv = "First Name,Username\n"+ ."alice,alice.full\n"+ ."bob,bob.full\n"+ ."carol,carol.full\n";++ $filename = 'slicefull-'.uniqid().'.csv';+ file_put_contents(config('app.private_uploads').'/imports/'.$filename, $csv);++ try {+ $import = \App\Models\Import::factory()->create([+ 'created_by' => $actor->id,+ 'file_path' => $filename,+ 'import_type' => 'user',+ 'header_row' => ['First Name', 'Username'],+ 'first_row' => ['alice', 'alice.full'],+ ]);++ $this->actingAsForApi($actor);+ $this->postJson(+ route('api.imports.importFile', ['import' => $import->id]),+ ['import-type' => 'user', 'import' => $import->id],+ )->assertOk();++ foreach (['alice.full', 'bob.full', 'carol.full'] as $username) {+ $this->assertDatabaseHas('users', ['username' => $username]);+ }+ } finally {+ @unlink(config('app.private_uploads').'/imports/'.$filename);+ }+ }}