From 233656df01c7487c61ccc22985d78eb1a3910a93 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 26 Nov 2024 14:28:22 -0600 Subject: [PATCH] Add tests for storing assets with all fields and multiple entries Introduced StoreAssetTest to verify the handling of full asset records and multiple asset entries in the database. Updated StoreAssetAction and AssetsController to support next audit date validation and improved error handling. --- app/Actions/Assets/StoreAssetAction.php | 4 +- .../Controllers/Assets/AssetsController.php | 7 +- tests/Feature/Assets/Ui/StoreAssetTest.php | 134 ++++++++++++++++++ 3 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 tests/Feature/Assets/Ui/StoreAssetTest.php diff --git a/app/Actions/Assets/StoreAssetAction.php b/app/Actions/Assets/StoreAssetAction.php index b80893d2f3..4fbf73cbae 100644 --- a/app/Actions/Assets/StoreAssetAction.php +++ b/app/Actions/Assets/StoreAssetAction.php @@ -47,6 +47,7 @@ class StoreAssetAction $assigned_asset = null, $assigned_location = null, $last_audit_date = null, + $next_audit_date = null, ): Asset|bool { $settings = Setting::getSettings(); @@ -73,10 +74,11 @@ class StoreAssetAction $asset->rtd_location_id = $rtd_location_id; $asset->byod = $byod; $asset->last_audit_date = $last_audit_date; + $asset->next_audit_date = $next_audit_date; $asset->location_id = $location_id; // set up next audit date - if (!empty($settings->audit_interval)) { + if (!empty($settings->audit_interval) && is_null($next_audit_date)) { $asset->next_audit_date = Carbon::now()->addMonths($settings->audit_interval)->toDateString(); } diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index 2ff9ce66cc..47ae6892aa 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -111,10 +111,6 @@ class AssetsController extends Controller try { $asset_tags = $request->input('asset_tags'); $serials = $request->input('serials'); - $custom_fields = $request->collect()->filter(function ($value, $key) { - return starts_with($key, '_snipeit_'); - }); - //DB::transaction(function () use ($request, $asset_tags, $serials, $custom_fields) { foreach ($asset_tags as $key => $asset_tag) { $asset = StoreAssetAction::run( @@ -143,6 +139,7 @@ class AssetsController extends Controller assigned_asset: $request->validated('assigned_asset'), assigned_location: $request->validated('assigned_location'), last_audit_date: $request->validated('last_audit_date'), + next_audit_date: $request->validated('next_audit_date'), ); } //}); @@ -153,7 +150,7 @@ class AssetsController extends Controller return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.create.error')); } catch (Exception $e) { report($e); - return redirect()->back()->with('error', 'something bad'); + return redirect()->back()->with('error', trans('general.something_went_wrong')); } } diff --git a/tests/Feature/Assets/Ui/StoreAssetTest.php b/tests/Feature/Assets/Ui/StoreAssetTest.php new file mode 100644 index 0000000000..51dfd3806a --- /dev/null +++ b/tests/Feature/Assets/Ui/StoreAssetTest.php @@ -0,0 +1,134 @@ +createAssets()->create(); + $model = AssetModel::factory()->create(); + $status = Statuslabel::factory()->readyToDeploy()->create(); + $defaultLocation = Location::factory()->create(); + $supplier = Supplier::factory()->create(); + $file = UploadedFile::fake()->image("test.jpg", 2000); + + + $response = $this->actingAs($user) + ->post(route('hardware.store'), [ + 'redirect_option' => 'item', + 'name' => 'Test Asset', + 'model_id' => $model->id, + 'status_id' => $status->id, + // ugh, this is because for some reason asset tags and serials are expected to start at an index of [1], so throwing an empty in for [0] + 'asset_tags' => ['', 'TEST-ASSET'], + 'serials' => ['', 'TEST-SERIAL'], + 'notes' => 'Test Notes', + 'rtd_location_id' => $defaultLocation->id, + 'requestable' => true, + 'image' => $file, + 'warranty_months' => 12, + 'next_audit_date' => Carbon::now()->addMonths(12)->format('Y-m-d'), + 'byod' => true, + 'order_number' => 'TEST-ORDER', + 'purchase_date' => Carbon::now()->format('Y-m-d'), + 'asset_eol_date' => Carbon::now()->addMonths(36)->format('Y-m-d'), + 'supplier_id' => $supplier->id, + 'purchase_cost' => 1234.56, + ])->assertSessionHasNoErrors(); + + $storedAsset = Asset::where('asset_tag', 'TEST-ASSET')->sole(); + + $response->assertRedirect(route('hardware.show', ['hardware' => $storedAsset->id])); + + $this->assertDatabaseHas('assets', [ + 'id' => $storedAsset->id, + 'name' => 'Test Asset', + 'model_id' => $model->id, + 'status_id' => $status->id, + 'asset_tag' => 'TEST-ASSET', + 'serial' => 'TEST-SERIAL', + 'notes' => 'Test Notes', + 'rtd_location_id' => $defaultLocation->id, + 'requestable' => 1, + 'image' => $storedAsset->image, + 'warranty_months' => 12, + 'next_audit_date' => Carbon::now()->addMonths(12)->format('Y-m-d'), + 'byod' => 1, + 'order_number' => 'TEST-ORDER', + 'purchase_date' => Carbon::now()->format('Y-m-d'), + 'asset_eol_date' => Carbon::now()->addMonths(36)->format('Y-m-d'), + 'supplier_id' => $supplier->id, + 'purchase_cost' => 1234.56, + ]); + } + + public function test_multiple_assets_are_stored() + { + $user = User::factory()->createAssets()->create(); + $model = AssetModel::factory()->create(); + $status = Statuslabel::factory()->readyToDeploy()->create(); + $defaultLocation = Location::factory()->create(); + $supplier = Supplier::factory()->create(); + $file = UploadedFile::fake()->image("test.jpg", 2000); + + $this->actingAs($user)->post(route('hardware.store'), [ + 'redirect_option' => 'index', + 'name' => 'Test Assets', + 'model_id' => $model->id, + 'status_id' => $status->id, + 'asset_tags' => ['', 'TEST-ASSET-1', 'TEST-ASSET-2'], + 'serials' => ['', 'TEST-SERIAL-1', 'TEST-SERIAL-2'], + 'notes' => 'Test Notes', + 'rtd_location_id' => $defaultLocation->id, + 'requestable' => true, + 'image' => $file, + 'warranty_months' => 12, + 'next_audit_date' => Carbon::now()->addMonths(12)->format('Y-m-d'), + 'byod' => true, + 'order_number' => 'TEST-ORDER', + 'purchase_date' => Carbon::now()->format('Y-m-d'), + 'asset_eol_date' => Carbon::now()->addMonths(36)->format('Y-m-d'), + 'supplier_id' => $supplier->id, + 'purchase_cost' => 1234.56, + ])->assertRedirect(route('hardware.index'))->assertSessionHasNoErrors(); + + $storedAsset = Asset::where('asset_tag', 'TEST-ASSET-1')->sole(); + $storedAsset2 = Asset::where('asset_tag', 'TEST-ASSET-2')->sole(); + + $commonData = [ + 'name' => 'Test Assets', + 'model_id' => $model->id, + 'status_id' => $status->id, + 'notes' => 'Test Notes', + 'rtd_location_id' => $defaultLocation->id, + 'requestable' => 1, + 'warranty_months' => 12, + 'next_audit_date' => Carbon::now()->addMonths(12)->format('Y-m-d'), + 'byod' => 1, + 'order_number' => 'TEST-ORDER', + 'purchase_date' => Carbon::now()->format('Y-m-d'), + 'asset_eol_date' => Carbon::now()->addMonths(36)->format('Y-m-d'), + 'supplier_id' => $supplier->id, + 'purchase_cost' => 1234.56, + ]; + + $this->assertDatabaseHas('assets', array_merge($commonData, ['asset_tag' => 'TEST-ASSET-1', 'serial' => 'TEST-SERIAL-1', 'image' => $storedAsset->image])); + $this->assertDatabaseHas('assets', array_merge($commonData, ['asset_tag' => 'TEST-ASSET-2', 'serial' => 'TEST-SERIAL-2', 'image' => $storedAsset2->image])); + } + +} \ No newline at end of file