From 9f815996c741326300b8825dcfc560a3d71dd7c0 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 19 Nov 2024 14:45:54 -0600 Subject: [PATCH] tests 'n stuff --- app/Actions/Assets/StoreAssetAction.php | 2 -- app/Actions/Assets/UpdateAssetAction.php | 14 ++++++++++++-- app/Http/Controllers/Api/AssetsController.php | 4 ++-- app/Http/Controllers/Assets/AssetsController.php | 6 ++---- .../Controllers/Assets/BulkAssetsController.php | 1 + tests/Feature/Assets/Api/UpdateAssetTest.php | 1 + tests/Unit/AssetTest.php | 6 ++++-- tests/Unit/DepreciationTest.php | 4 ++-- 8 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/Actions/Assets/StoreAssetAction.php b/app/Actions/Assets/StoreAssetAction.php index de04dbd6c9..9305b031a3 100644 --- a/app/Actions/Assets/StoreAssetAction.php +++ b/app/Actions/Assets/StoreAssetAction.php @@ -46,8 +46,6 @@ class StoreAssetAction $assigned_user = null, $assigned_asset = null, $assigned_location = null, - $custom_fields = null, - $last_audit_date = null, ) { diff --git a/app/Actions/Assets/UpdateAssetAction.php b/app/Actions/Assets/UpdateAssetAction.php index 9c314abc58..b083b78422 100644 --- a/app/Actions/Assets/UpdateAssetAction.php +++ b/app/Actions/Assets/UpdateAssetAction.php @@ -14,9 +14,13 @@ use Carbon\Carbon; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; +use Watson\Validating\ValidationException; class UpdateAssetAction { + /** + * @throws ValidationException + */ public static function run( Asset $asset, ImageUploadRequest $request, //very much would like this to go away @@ -111,7 +115,11 @@ class UpdateAssetAction $asset->serial = $serial; - $asset->name = $name; + if ($request->filled('null_name')) { + $asset->name = null; + } else { + $asset->name = $name ?? $asset->name; + } $asset->company_id = Company::getIdForCurrentUser($company_id); $asset->model_id = $model_id ?? $asset->model_id; $asset->order_number = $order_number ?? $asset->order_number; @@ -127,10 +135,12 @@ class UpdateAssetAction // FIXME: No idea why this is returning a Builder error on db_column_name. // Need to investigate and fix. Using static method for now. - $model = AssetModel::find($request->get('model_id')); + $model = $asset->model; if (($model) && ($model->fieldset)) { + dump($model->fieldset->fields); foreach ($model->fieldset->fields as $field) { + if ($field->field_encrypted == '1') { if (Gate::allows('assets.view.encrypted_custom_fields')) { if (is_array($request->input($field->db_column))) { diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index c82a6e46df..4b586260bb 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -652,7 +652,7 @@ class AssetsController extends Controller { try { $updatedAsset = UpdateAssetAction::run($asset, $request, ...$request->validated()); - return response()->json(Helper::formatStandardApiResponse('success', trans('admin/hardware/message.update.success'))); + return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.success'))); } catch (CheckoutNotAllowed $e) { return response()->json(Helper::formatStandardApiResponse('error', null, $e->getMessage()), 200); } catch (ValidationException $e) { @@ -680,7 +680,7 @@ class AssetsController extends Controller return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.delete.success'))); } catch (\Exception $e) { report($e); - return response()->json(Helper::formatStandardApiResponse('error', null, 'something went wrong: ')); + return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.something_went_wrong'))); } } diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index e6394bbda5..555174d9eb 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -119,6 +119,7 @@ class AssetsController extends Controller $asset = StoreAssetAction::run( model_id: $request->validated('model_id'), status_id: $request->validated('status_id'), + request: $request, name: $request->validated('name'), serial: $request->has('serials') ? $serials[$key] : null, company_id: $request->validated('company_id'), @@ -140,8 +141,6 @@ class AssetsController extends Controller assigned_user: $request->validated('assigned_user'), assigned_asset: $request->validated('assigned_asset'), assigned_location: $request->validated('assigned_location'), - custom_fields: $custom_fields, - request: $request, //this is just for the handleImages method... would love to figure out a different way of doing this last_audit_date: $request->validated('last_audit_date'), ); } @@ -153,7 +152,6 @@ class AssetsController extends Controller return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.create.error')); } catch (\Exception $e) { report($e); - dd($e); return redirect()->back()->with('error', 'something bad'); } } @@ -283,7 +281,7 @@ class AssetsController extends Controller * @param int $assetId * @since [v1.0] */ - public function destroy(Asset $asset): RedirectResponse + public function destroy(Asset $asset, $request): RedirectResponse { $this->authorize('delete', $asset); try { diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index e7a8184ddc..d9849cbf7a 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Assets; +use App\Actions\Assets\StoreAssetAction; use App\Actions\Assets\UpdateAssetAction; use App\Helpers\Helper; use App\Http\Controllers\CheckInOutRequest; diff --git a/tests/Feature/Assets/Api/UpdateAssetTest.php b/tests/Feature/Assets/Api/UpdateAssetTest.php index df4448a2db..55983d6c15 100644 --- a/tests/Feature/Assets/Api/UpdateAssetTest.php +++ b/tests/Feature/Assets/Api/UpdateAssetTest.php @@ -80,6 +80,7 @@ class UpdateAssetTest extends TestCase ->assertStatusMessageIs('success') ->json(); + dd($response); $updatedAsset = Asset::find($response['payload']['id']); $this->assertEquals('2024-06-02', $updatedAsset->asset_eol_date); diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.php index d0f3af6233..bf9d4f6fdc 100644 --- a/tests/Unit/AssetTest.php +++ b/tests/Unit/AssetTest.php @@ -7,6 +7,7 @@ use App\Models\Category; use Carbon\Carbon; use Tests\TestCase; use App\Models\Setting; +use Watson\Validating\ValidationException; class AssetTest extends TestCase { @@ -31,7 +32,8 @@ class AssetTest extends TestCase $b = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset() ]); $this->assertTrue($a->save()); - $this->assertFalse($b->save()); + $this->expectException(ValidationException::class); + $b->save(); } public function testAutoIncrementDouble() @@ -181,7 +183,7 @@ class AssetTest extends TestCase ] )->id, 'warranty_months' => 24, - 'purchase_date' => Carbon::createFromDate(2017, 1, 1)->hour(0)->minute(0)->second(0) + 'purchase_date' => Carbon::createFromDate(2017, 1, 1)->hour(0)->minute(0)->second(0)->format("Y-m-d") ]); diff --git a/tests/Unit/DepreciationTest.php b/tests/Unit/DepreciationTest.php index c170ac0e1d..f3d1097e8d 100644 --- a/tests/Unit/DepreciationTest.php +++ b/tests/Unit/DepreciationTest.php @@ -39,7 +39,7 @@ class DepreciationTest extends TestCase ->create( [ 'category_id' => Category::factory()->assetLaptopCategory()->create(), - 'purchase_date' => now()->subDecade(), + 'purchase_date' => now()->subDecade()->format("Y-m-d"), 'purchase_cost' => 4000, ]); $asset->model->update([ @@ -63,7 +63,7 @@ class DepreciationTest extends TestCase ->create( [ 'category_id' => Category::factory()->assetLaptopCategory()->create(), - 'purchase_date' => now()->subDecade(), + 'purchase_date' => now()->subDecade()->format("Y-m-d"), 'purchase_cost' => 4000, ]); $asset->model->update([