From 371afde52dde371ba7468377475d95b3293bfcc0 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 14 Aug 2024 09:52:25 +0100 Subject: [PATCH 1/2] Bulk edit asset name Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 16 ++++++++++---- resources/views/hardware/bulk.blade.php | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 287bc0611b..d58edbacab 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -227,7 +227,8 @@ class BulkAssetsController extends Controller * its checkout status. */ - if (($request->filled('purchase_date')) + if (($request->filled('name')) + || ($request->filled('purchase_date')) || ($request->filled('expected_checkin')) || ($request->filled('purchase_cost')) || ($request->filled('supplier_id')) @@ -239,6 +240,7 @@ class BulkAssetsController extends Controller || ($request->filled('status_id')) || ($request->filled('model_id')) || ($request->filled('next_audit_date')) + || ($request->filled('null_name')) || ($request->filled('null_purchase_date')) || ($request->filled('null_expected_checkin_date')) || ($request->filled('null_next_audit_date')) @@ -251,13 +253,14 @@ class BulkAssetsController extends Controller $this->update_array = []; /** - * Leave out model_id and status here because we do math on that later. We have to do some extra - * validation and checks on those two. + * Leave out model_id and status here because we do math on that later. We have to do some + * extra validation and checks on those two. * * It's tempting to make these match the request check above, but some of these values require * extra work to make sure the data makes sense. */ - $this->conditionallyAddItem('purchase_date') + $this->conditionallyAddItem('name') + ->conditionallyAddItem('purchase_date') ->conditionallyAddItem('expected_checkin') ->conditionallyAddItem('order_number') ->conditionallyAddItem('requestable') @@ -271,6 +274,11 @@ class BulkAssetsController extends Controller /** * Blank out fields that were requested to be blanked out via checkbox */ + if ($request->input('null_name')=='1') { + + $this->update_array['name'] = null; + } + if ($request->input('null_purchase_date')=='1') { $this->update_array['purchase_date'] = null; } diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php index 371f0c2549..248b18e400 100755 --- a/resources/views/hardware/bulk.blade.php +++ b/resources/views/hardware/bulk.blade.php @@ -35,6 +35,27 @@ @endif + +
+ +
+
+ +
+ {!! $errors->first('name', '') !!} +
+
+ +
+
+ +
From 4b690e8e1f273516ef0fadd98af9f5911997eac9 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 14 Aug 2024 10:01:56 +0100 Subject: [PATCH 2/2] Updated test to check for nulling Signed-off-by: snipe --- .../Feature/Assets/Ui/BulkEditAssetsTest.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php b/tests/Feature/Assets/Ui/BulkEditAssetsTest.php index 76d0a9c345..44e9052482 100644 --- a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php +++ b/tests/Feature/Assets/Ui/BulkEditAssetsTest.php @@ -57,6 +57,7 @@ class BulkEditAssetsTest extends TestCase $company1 = Company::factory()->create(); $company2 = Company::factory()->create(); $assets = Asset::factory()->count(10)->create([ + 'name' => 'Old Asset Name', 'purchase_date' => '2023-01-01', 'expected_checkin' => '2023-01-01', 'status_id' => $status1->id, @@ -77,6 +78,7 @@ class BulkEditAssetsTest extends TestCase // submits the ids and new values for each attribute $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ 'ids' => $id_array, + 'name' => 'New Asset Name', 'purchase_date' => '2024-01-01', 'expected_checkin' => '2024-01-01', 'status_id' => $status2->id, @@ -97,6 +99,7 @@ class BulkEditAssetsTest extends TestCase $this->assertEquals('2024-01-01', $asset->purchase_date->format('Y-m-d')); $this->assertEquals('2024-01-01', $asset->expected_checkin->format('Y-m-d')); $this->assertEquals($status2->id, $asset->status_id); + $this->assertEquals('New Asset Name', $asset->name); $this->assertEquals($model2->id, $asset->model_id); $this->assertEquals(5678.92, $asset->purchase_cost); $this->assertEquals($supplier2->id, $asset->supplier_id); @@ -109,6 +112,59 @@ class BulkEditAssetsTest extends TestCase }); } + public function testBulkEditAssetsNullsOutFieldsIfSelected() + { + // sets up all needed models and attributes on the assets + // this test does not deal with custom fields - will be dealt with in separate cases + $status1 = Statuslabel::factory()->create(); + $status2 = Statuslabel::factory()->create(); + $model1 = AssetModel::factory()->create(); + $model2 = AssetModel::factory()->create(); + $supplier1 = Supplier::factory()->create(); + $supplier2 = Supplier::factory()->create(); + $company1 = Company::factory()->create(); + $company2 = Company::factory()->create(); + $assets = Asset::factory()->count(10)->create([ + 'name' => 'Old Asset Name', + 'purchase_date' => '2023-01-01', + 'expected_checkin' => '2023-01-01', + 'status_id' => $status1->id, + 'model_id' => $model1->id, + // skipping locations on this test, it deserves it's own test + 'purchase_cost' => 1234.90, + 'supplier_id' => $supplier1->id, + 'company_id' => $company1->id, + 'order_number' => '123456', + 'warranty_months' => 24, + 'next_audit_date' => '2024-06-01', + 'requestable' => false + ]); + + // gets the ids together to submit to the endpoint + $id_array = $assets->pluck('id')->toArray(); + + // submits the ids and new values for each attribute + $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ + 'ids' => $id_array, + 'null_name' => '1', + 'null_purchase_date' => '1', + 'null_expected_checkin_date' => '1', + 'null_next_audit_date' => '1', + 'status_id' => $status2->id, + 'model_id' => $model2->id, + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors(); + + // asserts that each asset has the updated values + Asset::findMany($id_array)->each(function (Asset $asset) use ($status2, $model2, $supplier2, $company2) { + $this->assertNull($asset->name); + $this->assertNull($asset->purchase_date); + $this->assertNull($asset->expected_checkin); + $this->assertNull($asset->next_audit_date); + }); + } + public function testBulkEditAssetsAcceptsAndUpdatesUnencryptedCustomFields() { $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');