mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
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.
This commit is contained in:
parent
233355ab48
commit
233656df01
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
134
tests/Feature/Assets/Ui/StoreAssetTest.php
Normal file
134
tests/Feature/Assets/Ui/StoreAssetTest.php
Normal file
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Assets\Ui;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Models\AssetModel;
|
||||
use App\Models\Location;
|
||||
use App\Models\Statuslabel;
|
||||
use App\Models\Supplier;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
class StoreAssetTest extends TestCase
|
||||
{
|
||||
public function test_all_fields_are_saved()
|
||||
{
|
||||
Storage::fake('public');
|
||||
|
||||
$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);
|
||||
|
||||
|
||||
$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]));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue