From c0110e7f29bd6f3b68c7d07bdcacd1319ae8e2fa Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 19 Mar 2024 19:27:35 -0500 Subject: [PATCH] some more tests and refinement --- app/Http/Requests/UpdateAssetRequest.php | 12 +-- tests/Feature/Api/Assets/AssetUpdateTest.php | 93 +++++++++++++++++++- 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/app/Http/Requests/UpdateAssetRequest.php b/app/Http/Requests/UpdateAssetRequest.php index 71792b85fc..97282036fa 100644 --- a/app/Http/Requests/UpdateAssetRequest.php +++ b/app/Http/Requests/UpdateAssetRequest.php @@ -23,9 +23,9 @@ class UpdateAssetRequest extends ImageUploadRequest // the following are 'required' attributes that may or may not be present on an patch request // so supplying them here instead of doing funky array modification to the rules return $this->merge([ - 'asset_tag' => $this->asset_tag ?? $this->asset->asset_tag, - 'model_id' => $this->model_id ?? $this->asset->model_id, - 'status_id' => $this->status_id ?? $this->asset->status_id, + //'asset_tag' => $this->asset_tag ?? $this->asset->asset_tag, + //'model_id' => $this->model_id ?? $this->asset->model_id, + //'status_id' => $this->status_id ?? $this->asset->status_id, ]); } @@ -37,9 +37,9 @@ class UpdateAssetRequest extends ImageUploadRequest public function rules() { $rules = [ - 'model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array', - 'status_id' => 'required|integer|exists:status_labels,id', - 'asset_tag' => ['required', 'min:1', 'max:255', 'not_array', Rule::unique('assets', 'asset_tag')->ignore($this->asset)->withoutTrashed()], + 'model_id' => 'integer|exists:models,id,deleted_at,NULL|not_array', + 'status_id' => 'integer|exists:status_labels,id', + 'asset_tag' => ['min:1', 'max:255', 'not_array', Rule::unique('assets', 'asset_tag')->ignore($this->asset)->withoutTrashed()], 'name' => 'nullable|max:255', 'company_id' => 'nullable|integer|exists:companies,id', 'warranty_months' => 'nullable|numeric|digits_between:0,240', diff --git a/tests/Feature/Api/Assets/AssetUpdateTest.php b/tests/Feature/Api/Assets/AssetUpdateTest.php index 17bb72fa30..79a9eb4e0b 100644 --- a/tests/Feature/Api/Assets/AssetUpdateTest.php +++ b/tests/Feature/Api/Assets/AssetUpdateTest.php @@ -11,6 +11,8 @@ use App\Models\Supplier; use App\Models\User; use Carbon\Carbon; use Illuminate\Testing\Fluent\AssertableJson; + +// TODO: DELETE INTERACTSWITHSETTINGS BEFORE FINAL PR use Tests\Support\InteractsWithSettings; use Tests\TestCase; @@ -125,7 +127,7 @@ class AssetUpdateTest extends TestCase $this->settings->enableAutoIncrement(); - $response = $this->actingAsForApi(User::factory()->editAssets()->create()) + $this->actingAsForApi(User::factory()->editAssets()->create()) ->patchJson(route('api.assets.update', $asset->id), [ 'name' => 'test asset', 'asset_eol_date' => '2022-01-01' @@ -139,4 +141,93 @@ class AssetUpdateTest extends TestCase $this->assertEquals('2022-01-01', $asset->asset_eol_date); } + public function testAssetEolExplicitIsSetIfAssetEolDateIsExplicitlySet() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'asset_eol_date' => '2025-01-01', + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->json(); + + $asset->refresh(); + + $this->assertEquals('2025-01-01', $asset->asset_eol_date); + $this->assertTrue($asset->eol_explicit); + } + + public function testAssetTagCannotUpdateToNullValue() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'asset_tag' => null, + ]) + ->assertOk() + ->assertStatusMessageIs('error'); + } + + public function testAssetTagCannotUpdateToEmptyStringValue() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'asset_tag' => "", + ]) + ->assertOk() + ->assertStatusMessageIs('error'); + } + + public function testModelIdCannotUpdateToNullValue() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'model_id' => null + ]) + ->assertOk() + ->assertStatusMessageIs('error'); + } + + public function testModelIdCannotUpdateToEmptyStringValue() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'model_id' => "" + ]) + ->assertOk() + ->assertStatusMessageIs('error'); + } + + public function testStatusIdCannotUpdateToNullValue() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'status_id' => null + ]) + ->assertOk() + ->assertStatusMessageIs('error'); + } + + public function testStatusIdCannotUpdateToEmptyStringValue() + { + $asset = Asset::factory()->laptopMbp()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->patchJson(route('api.assets.update', $asset->id), [ + 'status_id' => "" + ]) + ->assertOk() + ->assertStatusMessageIs('error'); + } } \ No newline at end of file