From 25480293dc3f31435c7b5a792cf05175075a8063 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 30 Apr 2024 18:03:26 -0500 Subject: [PATCH 1/7] change keys to values, add test --- .../Assets/BulkAssetsController.php | 2 +- resources/views/hardware/bulk.blade.php | 4 +-- tests/Feature/Assets/AssetsBulkEditTest.php | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/Assets/AssetsBulkEditTest.php diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 561e13b200..d60507527f 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -214,7 +214,7 @@ class BulkAssetsController extends Controller } - $assets = Asset::whereIn('id', array_keys($request->input('ids')))->get(); + $assets = Asset::whereIn('id', $request->input('ids'))->get(); diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php index fa4680e2e8..d4f0544851 100755 --- a/resources/views/hardware/bulk.blade.php +++ b/resources/views/hardware/bulk.blade.php @@ -196,8 +196,8 @@ @include("models/custom_fields_form_bulk_edit",["models" => $models]) - @foreach ($assets as $key => $value) - + @foreach($assets as $asset) + @endforeach diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php new file mode 100644 index 0000000000..7054b36da6 --- /dev/null +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -0,0 +1,33 @@ +count(10)->create(['purchase_date' => '2023-01-01']); + + $id_array = $assets->pluck('id')->toArray(); + + $response = $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ + 'ids' => array_values($id_array), + 'purchase_date' => '2024-01-01' + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors(); + + Asset::findMany($id_array)->each(function (Asset $asset) { + $this->assertEquals('2024-01-01', $asset->purchase_date->format('Y-m-d')); + }); + + $this->assertDatabaseHas('assets', [ + 'purchase_date' => '2024-01-01' + ]); + } +} From 39cc99c89b52628beb0fb41addf5ceab00706294 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 30 Apr 2024 22:22:59 -0500 Subject: [PATCH 2/7] all initial attributes set in test --- tests/Feature/Assets/AssetsBulkEditTest.php | 61 +++++++++++++++++++-- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php index 7054b36da6..6cb879d333 100644 --- a/tests/Feature/Assets/AssetsBulkEditTest.php +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -3,27 +3,78 @@ namespace Tests\Feature\Assets; use App\Models\Asset; +use App\Models\AssetModel; +use App\Models\Company; +use App\Models\Statuslabel; +use App\Models\Supplier; use App\Models\User; use Carbon\Carbon; use Tests\TestCase; class AssetsBulkEditTest extends TestCase { - public function testBulkEditAsset() + public function testBulkEditAssetsAcceptsAllPossibleAttributes() { - $assets = Asset::factory()->count(10)->create(['purchase_date' => '2023-01-01']); + // 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([ + '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 $response = $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ - 'ids' => array_values($id_array), - 'purchase_date' => '2024-01-01' + 'ids' => $id_array, + 'purchase_date' => '2024-01-01', + 'expected_checkin' => '2024-01-01', + 'status_id' => $status2->id, + 'model_id' => $model2->id, + 'purchase_cost' => 5678.92, + 'supplier_id' => $supplier2->id, + 'company_id' => $company2->id, + 'order_number' => '7890', + 'warranty_months' => 36, + 'next_audit_date' => '2025-01-01', + 'requestable' => true ]) ->assertStatus(302) ->assertSessionHasNoErrors(); - Asset::findMany($id_array)->each(function (Asset $asset) { + // asserts that each asset has the updated values + Asset::findMany($id_array)->each(function (Asset $asset) use ($status2, $model2, $supplier2, $company2) { $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($model2->id, $asset->model_id); + $this->assertEquals(5678.92, $asset->purchase_cost); + $this->assertEquals($supplier2->id, $asset->supplier_id); + $this->assertEquals($company2->id, $asset->company_id); + $this->assertEquals(7890, $asset->order_number); + $this->assertEquals(36, $asset->warranty_months); + $this->assertEquals('2025-01-01', $asset->next_audit_date->format('Y-m-d')); + // shouldn't requestable be cast as a boolean??? + $this->assertEquals(1, $asset->requestable); }); $this->assertDatabaseHas('assets', [ From 6a7f3ecc2edf80d8ba2905173c1677ec0e2df142 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 1 May 2024 16:12:56 -0500 Subject: [PATCH 3/7] new test not quite working, almost there --- database/factories/AssetFactory.php | 9 +++++ database/factories/AssetModelFactory.php | 9 +++++ database/factories/CustomFieldsetFactory.php | 21 +++++++++++ tests/Feature/Assets/AssetsBulkEditTest.php | 37 +++++++++++++++++--- 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php index 6c5e883636..43845c3075 100644 --- a/database/factories/AssetFactory.php +++ b/database/factories/AssetFactory.php @@ -363,6 +363,15 @@ class AssetFactory extends Factory }); } + public function hasMultipleCustomFields(array $fields = null): self + { + return $this->state(function () use ($fields) { + return [ + 'model_id' => AssetModel::factory()->hasMultipleCustomFields($fields), + ]; + }); + } + /** * This allows bypassing model level validation if you want to purposefully diff --git a/database/factories/AssetModelFactory.php b/database/factories/AssetModelFactory.php index ed3d478261..6790897567 100644 --- a/database/factories/AssetModelFactory.php +++ b/database/factories/AssetModelFactory.php @@ -439,4 +439,13 @@ class AssetModelFactory extends Factory ]; }); } + + public function hasMultipleCustomFields(array $fields = null) + { + return $this->state(function () use ($fields) { + return [ + 'fieldset_id' => CustomFieldset::factory()->hasMultipleCustomFields($fields), + ]; + }); + } } diff --git a/database/factories/CustomFieldsetFactory.php b/database/factories/CustomFieldsetFactory.php index 9a410ba25f..9f02c8e457 100644 --- a/database/factories/CustomFieldsetFactory.php +++ b/database/factories/CustomFieldsetFactory.php @@ -53,4 +53,25 @@ class CustomFieldsetFactory extends Factory $fieldset->fields()->attach($field, ['order' => '1', 'required' => false]); }); } + + public function hasMultipleCustomFields(array $fields = null): self + { + return $this->afterCreating(function (CustomFieldset $fieldset) { + if (empty($fields)) { + return $this->afterCreating(function (CustomFieldset $fieldset) { + $mac_address = CustomField::factory()->macAddress()->create(); + $ram = CustomField::factory()->ram()->create(); + $cpu = CustomField::factory()->cpu()->create(); + + $fieldset->fields()->attach($mac_address, ['order' => '1', 'required' => false]); + $fieldset->fields()->attach($ram, ['order' => '2', 'required' => false]); + $fieldset->fields()->attach($cpu, ['order' => '3', 'required' => false]); + }); + } else { + foreach ($fields as $field) { + $fieldset->fields()->attach($field, ['order' => '1', 'required' => false]); + } + } + }); + } } diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php index 6cb879d333..34bd58bfb8 100644 --- a/tests/Feature/Assets/AssetsBulkEditTest.php +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -5,10 +5,10 @@ namespace Tests\Feature\Assets; use App\Models\Asset; use App\Models\AssetModel; use App\Models\Company; +use App\Models\CustomField; use App\Models\Statuslabel; use App\Models\Supplier; use App\Models\User; -use Carbon\Carbon; use Tests\TestCase; class AssetsBulkEditTest extends TestCase @@ -44,7 +44,7 @@ class AssetsBulkEditTest extends TestCase $id_array = $assets->pluck('id')->toArray(); // submits the ids and new values for each attribute - $response = $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ + $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ 'ids' => $id_array, 'purchase_date' => '2024-01-01', 'expected_checkin' => '2024-01-01', @@ -73,12 +73,39 @@ class AssetsBulkEditTest extends TestCase $this->assertEquals(7890, $asset->order_number); $this->assertEquals(36, $asset->warranty_months); $this->assertEquals('2025-01-01', $asset->next_audit_date->format('Y-m-d')); - // shouldn't requestable be cast as a boolean??? + // shouldn't requestable be cast as a boolean??? it's not. $this->assertEquals(1, $asset->requestable); }); + } - $this->assertDatabaseHas('assets', [ - 'purchase_date' => '2024-01-01' + public function testBulkEditAssetsAcceptsAndUpdatesUnencryptedCustomFields() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $mac_address = CustomField::factory()->macAddress()->create(); + $ram = CustomField::factory()->ram()->create(); + $cpu = CustomField::factory()->cpu()->create(); + + $assets = Asset::factory()->laptopMbp()->count(10)->hasMultipleCustomFields([$mac_address, $ram, $cpu])->create([ + $ram->db_column => 8, + $cpu->db_column => '2.1', ]); + + $id_array = $assets->pluck('id')->toArray(); + + // submits the ids and new values for each attribute + $asset = Asset::find(1); + + $this->assertEquals(8, $asset->{$ram->db_column}); + $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ + 'ids' => $id_array, + $ram->db_column => 16, + $cpu->db_column => '4.1', + ]); + + Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu, $mac_address) { + $this->assertEquals(16, $asset->{$ram->db_column}); + $this->assertEquals('4.1', $asset->{$ram->db_column}); + }); } } From e177993bcce298dd6cff730324bea7d5084e39c0 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 1 May 2024 16:57:11 -0500 Subject: [PATCH 4/7] notes and some playing around, push for eod --- app/Http/Controllers/Assets/BulkAssetsController.php | 1 + database/factories/CustomFieldsetFactory.php | 1 + tests/Feature/Assets/AssetsBulkEditTest.php | 10 +++++----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index d60507527f..66e0f75715 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -195,6 +195,7 @@ class BulkAssetsController extends Controller */ public function update(Request $request) { + //dd($request->all()); $this->authorize('update', Asset::class); $has_errors = 0; $error_array = array(); diff --git a/database/factories/CustomFieldsetFactory.php b/database/factories/CustomFieldsetFactory.php index 9f02c8e457..66d7591aa5 100644 --- a/database/factories/CustomFieldsetFactory.php +++ b/database/factories/CustomFieldsetFactory.php @@ -58,6 +58,7 @@ class CustomFieldsetFactory extends Factory { return $this->afterCreating(function (CustomFieldset $fieldset) { if (empty($fields)) { + //why are there two after creating and why does it break if i remove one return $this->afterCreating(function (CustomFieldset $fieldset) { $mac_address = CustomField::factory()->macAddress()->create(); $ram = CustomField::factory()->ram()->create(); diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php index 34bd58bfb8..7a72c44636 100644 --- a/tests/Feature/Assets/AssetsBulkEditTest.php +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -86,17 +86,17 @@ class AssetsBulkEditTest extends TestCase $ram = CustomField::factory()->ram()->create(); $cpu = CustomField::factory()->cpu()->create(); - $assets = Asset::factory()->laptopMbp()->count(10)->hasMultipleCustomFields([$mac_address, $ram, $cpu])->create([ + $assets = Asset::factory()->count(10)->hasMultipleCustomFields([$mac_address, $ram, $cpu])->create([ $ram->db_column => 8, $cpu->db_column => '2.1', ]); + // seems like the fieldset is random, so bulkedit isn't working because assets don't have the "correct" fieldset + // look into more tomorrow + dd(Asset::find(1)->model->fieldset); + $id_array = $assets->pluck('id')->toArray(); - // submits the ids and new values for each attribute - $asset = Asset::find(1); - - $this->assertEquals(8, $asset->{$ram->db_column}); $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ 'ids' => $id_array, $ram->db_column => 16, From 17ef20ea92f53888b964e570d6971e550af0a8fe Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 7 May 2024 14:08:47 -0500 Subject: [PATCH 5/7] alright, in a working place --- .../Controllers/Assets/BulkAssetsController.php | 5 ++--- database/factories/CustomFieldsetFactory.php | 5 +---- tests/Feature/Assets/AssetsBulkEditTest.php | 16 ++++++++-------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 66e0f75715..a21fd0a8d3 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -13,6 +13,7 @@ use App\Models\Setting; use App\View\Label; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; @@ -189,13 +190,11 @@ class BulkAssetsController extends Controller * Save bulk edits * * @author [A. Gianotto] [] - * @return Redirect * @internal param array $assets * @since [v2.0] */ public function update(Request $request) { - //dd($request->all()); $this->authorize('update', Asset::class); $has_errors = 0; $error_array = array(); @@ -390,7 +389,7 @@ class BulkAssetsController extends Controller * but it wasn't. */ if ($decrypted_old != $this->update_array[$field->db_column]) { - $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); + $asset->{$field->db_column} = Crypt::encrypt($this->update_array[$field->db_column]); } else { /* * Remove the encrypted custom field from the update_array, since nothing changed diff --git a/database/factories/CustomFieldsetFactory.php b/database/factories/CustomFieldsetFactory.php index 66d7591aa5..a9e8b9ae12 100644 --- a/database/factories/CustomFieldsetFactory.php +++ b/database/factories/CustomFieldsetFactory.php @@ -56,10 +56,8 @@ class CustomFieldsetFactory extends Factory public function hasMultipleCustomFields(array $fields = null): self { - return $this->afterCreating(function (CustomFieldset $fieldset) { + return $this->afterCreating(function (CustomFieldset $fieldset) use ($fields) { if (empty($fields)) { - //why are there two after creating and why does it break if i remove one - return $this->afterCreating(function (CustomFieldset $fieldset) { $mac_address = CustomField::factory()->macAddress()->create(); $ram = CustomField::factory()->ram()->create(); $cpu = CustomField::factory()->cpu()->create(); @@ -67,7 +65,6 @@ class CustomFieldsetFactory extends Factory $fieldset->fields()->attach($mac_address, ['order' => '1', 'required' => false]); $fieldset->fields()->attach($ram, ['order' => '2', 'required' => false]); $fieldset->fields()->attach($cpu, ['order' => '3', 'required' => false]); - }); } else { foreach ($fields as $field) { $fieldset->fields()->attach($field, ['order' => '1', 'required' => false]); diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php index 7a72c44636..de86d33533 100644 --- a/tests/Feature/Assets/AssetsBulkEditTest.php +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -82,19 +82,19 @@ class AssetsBulkEditTest extends TestCase { $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); - $mac_address = CustomField::factory()->macAddress()->create(); - $ram = CustomField::factory()->ram()->create(); - $cpu = CustomField::factory()->cpu()->create(); + CustomField::factory()->macAddress()->create(); + CustomField::factory()->ram()->create(); + CustomField::factory()->cpu()->create(); + + $mac_address = CustomField::where('name', 'MAC Address')->first(); + $ram = CustomField::where('name', 'RAM')->first(); + $cpu = CustomField::where('name', 'CPU')->first(); $assets = Asset::factory()->count(10)->hasMultipleCustomFields([$mac_address, $ram, $cpu])->create([ $ram->db_column => 8, $cpu->db_column => '2.1', ]); - // seems like the fieldset is random, so bulkedit isn't working because assets don't have the "correct" fieldset - // look into more tomorrow - dd(Asset::find(1)->model->fieldset); - $id_array = $assets->pluck('id')->toArray(); $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ @@ -105,7 +105,7 @@ class AssetsBulkEditTest extends TestCase Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu, $mac_address) { $this->assertEquals(16, $asset->{$ram->db_column}); - $this->assertEquals('4.1', $asset->{$ram->db_column}); + $this->assertEquals('4.1', $asset->{$cpu->db_column}); }); } } From ad2ba252eef9a7088004ae5db1e1f375764c11e7 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 7 May 2024 15:11:33 -0500 Subject: [PATCH 6/7] two new test --- .../Assets/BulkAssetsController.php | 39 +++++----- tests/Feature/Assets/AssetsBulkEditTest.php | 76 ++++++++++++++++++- 2 files changed, 93 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index a21fd0a8d3..ffbf81944b 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -15,6 +15,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; use App\Http\Requests\AssetCheckoutRequest; @@ -379,28 +380,30 @@ class BulkAssetsController extends Controller foreach ($asset->model->fieldset->fields as $field) { if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted == '1')) { - $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); + if (Gate::allows('admin')) { + $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); - /* - * Check if the decrypted existing value is different from one we just submitted - * and if not, pull it out of the object since it shouldn't really be updating at all. - * If we don't do this, it will try to re-encrypt it, and the same value encrypted two - * different times will have different values, so it will *look* like it was updated - * but it wasn't. - */ - if ($decrypted_old != $this->update_array[$field->db_column]) { - $asset->{$field->db_column} = Crypt::encrypt($this->update_array[$field->db_column]); - } else { /* - * Remove the encrypted custom field from the update_array, since nothing changed + * Check if the decrypted existing value is different from one we just submitted + * and if not, pull it out of the object since it shouldn't really be updating at all. + * If we don't do this, it will try to re-encrypt it, and the same value encrypted two + * different times will have different values, so it will *look* like it was updated + * but it wasn't. */ - unset($this->update_array[$field->db_column]); - unset($asset->{$field->db_column}); - } + if ($decrypted_old != $this->update_array[$field->db_column]) { + $asset->{$field->db_column} = Crypt::encrypt($this->update_array[$field->db_column]); + } else { + /* + * Remove the encrypted custom field from the update_array, since nothing changed + */ + unset($this->update_array[$field->db_column]); + unset($asset->{$field->db_column}); + } - /* - * These custom fields aren't encrypted, just carry on as usual - */ + /* + * These custom fields aren't encrypted, just carry on as usual + */ + } } else { if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php index de86d33533..4da43da753 100644 --- a/tests/Feature/Assets/AssetsBulkEditTest.php +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -9,6 +9,7 @@ use App\Models\CustomField; use App\Models\Statuslabel; use App\Models\Supplier; use App\Models\User; +use Illuminate\Support\Facades\Crypt; use Tests\TestCase; class AssetsBulkEditTest extends TestCase @@ -82,15 +83,16 @@ class AssetsBulkEditTest extends TestCase { $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); - CustomField::factory()->macAddress()->create(); CustomField::factory()->ram()->create(); CustomField::factory()->cpu()->create(); - $mac_address = CustomField::where('name', 'MAC Address')->first(); + // when getting the custom field directly from the factory the field has not been fully created yet + // so we have to do a query afterwards to get the actual model :shrug: + $ram = CustomField::where('name', 'RAM')->first(); $cpu = CustomField::where('name', 'CPU')->first(); - $assets = Asset::factory()->count(10)->hasMultipleCustomFields([$mac_address, $ram, $cpu])->create([ + $assets = Asset::factory()->count(10)->hasMultipleCustomFields([$ram, $cpu])->create([ $ram->db_column => 8, $cpu->db_column => '2.1', ]); @@ -103,9 +105,75 @@ class AssetsBulkEditTest extends TestCase $cpu->db_column => '4.1', ]); - Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu, $mac_address) { + Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu) { $this->assertEquals(16, $asset->{$ram->db_column}); $this->assertEquals('4.1', $asset->{$cpu->db_column}); }); } + + public function testBulkEditAssetsAcceptsAndUpdatesEncryptedCustomFields() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + CustomField::factory()->testEncrypted()->create(); + + $encrypted = CustomField::where('name', 'Test Encrypted')->first(); + + $assets = Asset::factory()->count(10)->hasEncryptedCustomField($encrypted)->create([ + $encrypted->db_column => Crypt::encrypt('Original Encrypted Text'), + ]); + + $id_array = $assets->pluck('id')->toArray(); + + $this->actingAs(User::factory()->admin()->create())->post(route('hardware/bulksave'), [ + 'ids' => $id_array, + $encrypted->db_column => 'New Encrypted Text', + ]); + + Asset::findMany($id_array)->each(function (Asset $asset) use ($encrypted) { + $this->assertEquals('New Encrypted Text', Crypt::decrypt($asset->{$encrypted->db_column})); + }); + } + + public function testBulkEditAssetsRequiresAdminUserToUpdateEncryptedCustomFields() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on mysql'); + $edit_user = User::factory()->editAssets()->create(); + $admin_user = User::factory()->admin()->create(); + + CustomField::factory()->testEncrypted()->create(); + + $encrypted = CustomField::where('name', 'Test Encrypted')->first(); + + $admin_assets = Asset::factory()->count(5)->hasEncryptedCustomField($encrypted)->create([ + $encrypted->db_column => Crypt::encrypt('Original Encrypted Text'), + ]); + + $standard_assets = Asset::factory()->count(5)->hasEncryptedCustomField($encrypted)->create([ + $encrypted->db_column => Crypt::encrypt('Original Encrypted Text'), + ]); + + $admin_id_array = $admin_assets->pluck('id')->toArray(); + $standard_id_array = $standard_assets->pluck('id')->toArray(); + + $this->actingAs($admin_user)->post(route('hardware/bulksave'), [ + 'ids' => $admin_id_array, + $encrypted->db_column => 'New Encrypted Text', + ])->assertStatus(302); + + // do we want to return an error when this happens??? + $this->actingAs($edit_user)->post(route('hardware/bulksave'), [ + 'ids' => $standard_id_array, + $encrypted->db_column => 'New Encrypted Text', + ])->assertStatus(302); + + Asset::findMany($admin_id_array)->each(function (Asset $asset) use ($encrypted) { + $this->assertEquals('New Encrypted Text', Crypt::decrypt($asset->{$encrypted->db_column})); + }); + + Asset::findMany($standard_id_array)->each(function (Asset $asset) use ($encrypted) { + $this->assertEquals('Original Encrypted Text', Crypt::decrypt($asset->{$encrypted->db_column})); + }); + + } } From 5b86ee729157c5e74eb302be8f572e1b42756d13 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 7 May 2024 17:19:00 -0500 Subject: [PATCH 7/7] a couple more tests --- tests/Feature/Assets/AssetsBulkEditTest.php | 35 +++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Assets/AssetsBulkEditTest.php b/tests/Feature/Assets/AssetsBulkEditTest.php index 4da43da753..ee684d7676 100644 --- a/tests/Feature/Assets/AssetsBulkEditTest.php +++ b/tests/Feature/Assets/AssetsBulkEditTest.php @@ -14,6 +14,36 @@ use Tests\TestCase; class AssetsBulkEditTest extends TestCase { + public function testUserWithPermissionsCanAccessPage() + { + $user = User::factory()->viewAssets()->editAssets()->create(); + $assets = Asset::factory()->count(2)->create(); + + $id_array = $assets->pluck('id')->toArray(); + + $this->actingAs($user)->post('/hardware/bulkedit', [ + 'ids' => $id_array, + 'order' => 'asc', + 'bulk_actions' => 'edit', + 'sort' => 'id' + ])->assertStatus(200); + } + + public function testStandardUserCannotAccessPage() + { + $user = User::factory()->create(); + $assets = Asset::factory()->count(2)->create(); + + $id_array = $assets->pluck('id')->toArray(); + + $this->actingAs($user)->post('/hardware/bulkedit', [ + 'ids' => $id_array, + 'order' => 'asc', + 'bulk_actions' => 'edit', + 'sort' => 'id' + ])->assertStatus(403); + } + public function testBulkEditAssetsAcceptsAllPossibleAttributes() { // sets up all needed models and attributes on the assets @@ -103,7 +133,7 @@ class AssetsBulkEditTest extends TestCase 'ids' => $id_array, $ram->db_column => 16, $cpu->db_column => '4.1', - ]); + ])->assertStatus(302); Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu) { $this->assertEquals(16, $asset->{$ram->db_column}); @@ -128,7 +158,7 @@ class AssetsBulkEditTest extends TestCase $this->actingAs(User::factory()->admin()->create())->post(route('hardware/bulksave'), [ 'ids' => $id_array, $encrypted->db_column => 'New Encrypted Text', - ]); + ])->assertStatus(302); Asset::findMany($id_array)->each(function (Asset $asset) use ($encrypted) { $this->assertEquals('New Encrypted Text', Crypt::decrypt($asset->{$encrypted->db_column})); @@ -174,6 +204,5 @@ class AssetsBulkEditTest extends TestCase Asset::findMany($standard_id_array)->each(function (Asset $asset) use ($encrypted) { $this->assertEquals('Original Encrypted Text', Crypt::decrypt($asset->{$encrypted->db_column})); }); - } }