adds test for deleting assigned asset

This commit is contained in:
Godfrey M 2025-02-19 15:59:57 -08:00
parent eaacf29d57
commit a275391557
2 changed files with 26 additions and 3 deletions

View file

@ -529,18 +529,18 @@ class BulkAssetsController extends Controller
if ($request->session()->has('bulk_back_url')) {
$bulk_back_url = $request->session()->pull('bulk_back_url');
}
$assetIds = $request->get('ids') ?? [];
$assetIds = $request->get('ids');
if(empty($assetIds)) {
return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated'));
}
$assignedAssets = Asset::whereIn('id', $assetIds)->whereNotNull('assigned_to')->get();
if($assignedAssets->isNotEmpty()) {
//if assets are checked out, return a list of asset tags that would need to be checked in first.
$assetTags = $assignedAssets->pluck('asset_tag')->implode(', ');
return redirect()->route($bulk_back_url)->with('error', trans_choice('admin/hardware/message.delete.assigned_to_error', $assignedAssets->count(), ['asset_tag' => $assetTags] ));
return redirect($bulk_back_url)->with('error', trans_choice('admin/hardware/message.delete.assigned_to_error', $assignedAssets->count(), ['asset_tag' => $assetTags] ));
}
foreach (Asset::wherein('id', $assetIds)->get() as $asset) {

View file

@ -162,5 +162,28 @@ class BulkDeleteAssetsTest extends TestCase
);
}
public function testBulkDeleteAssignedAssetTriggersError(){
$user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create();
$asset = Asset::factory()->create([
'id' => 5,
'assigned_to' => $user->id,
'asset_tag' => '12345',
]);
$response = $this->actingAs($user)
->from(route('hardware/bulkedit'))
->post('/hardware/bulkdelete', [
'ids' => [$asset->id],
'bulk_actions' => 'delete',
]);
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals(route('hardware.index'), $response->headers->get('Location'));
$errorMessage = session('error');
$expectedMessage = trans_choice('admin/hardware/message.delete.assigned_to_error',1, ['asset_tag' => $asset->asset_tag]);
$this->assertEquals($expectedMessage, $errorMessage);
}
}