From 4058312eb9613d027640d25391fbdf6fed96ef85 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 14 May 2025 13:42:19 +0200 Subject: [PATCH] Fixed tests Signed-off-by: snipe --- app/Exceptions/Handler.php | 2 +- .../Controllers/Api/AssetFilesController.php | 2 +- app/Http/Requests/UploadFileRequest.php | 7 ++- .../AssetModels/Api/AssetModelFilesTest.php | 48 +++++++++---------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 1293efca8d..6c2dd55d75 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -126,7 +126,7 @@ class Handler extends ExceptionHandler return response()->json(Helper::formatStandardApiResponse('error', null, $e->errors()), 200); } - return response()->json(Helper::formatStandardApiResponse('error', null, 'Undefined exception'), 200); + // return response()->json(Helper::formatStandardApiResponse('error', null, 'Undefined exception'), 200); diff --git a/app/Http/Controllers/Api/AssetFilesController.php b/app/Http/Controllers/Api/AssetFilesController.php index 7dcc584d86..af4c99c947 100644 --- a/app/Http/Controllers/Api/AssetFilesController.php +++ b/app/Http/Controllers/Api/AssetFilesController.php @@ -58,7 +58,7 @@ class AssetFilesController extends Controller } $files = Actionlog::select('action_logs.*')->where('action_type', '=', 'uploaded')->where('item_type', '=', Asset::class)->where('item_id', '=', $asset->id)->whereIn('filename', $files)->get(); // All done - report success - return response()->json(Helper::formatStandardApiResponse('success', (new UploadedFilesTransformer())->transformFilesArray($files, count($files)), trans('admin/hardware/message.upload.success'))); + return response()->json(Helper::formatStandardApiResponse('success', (new UploadedFilesTransformer())->transformFiles($files, count($files)), trans('admin/hardware/message.upload.success'))); } // We only reach here if no files were included in the POST, so tell the user this diff --git a/app/Http/Requests/UploadFileRequest.php b/app/Http/Requests/UploadFileRequest.php index 940db35841..7788557841 100644 --- a/app/Http/Requests/UploadFileRequest.php +++ b/app/Http/Requests/UploadFileRequest.php @@ -79,9 +79,12 @@ class UploadFileRequest extends Request { $attributes = []; - for ($i = 0; $i < count($this->file); $i++) { - $attributes['file.'.$i] = $this->file[$i]->getClientOriginalName(); + if ($this->file) { + for ($i = 0; $i < count($this->file); $i++) { + $attributes['file.'.$i] = $this->file[$i]->getClientOriginalName(); + } } + return $attributes; } diff --git a/tests/Feature/AssetModels/Api/AssetModelFilesTest.php b/tests/Feature/AssetModels/Api/AssetModelFilesTest.php index d29ea4f002..566d085dcf 100644 --- a/tests/Feature/AssetModels/Api/AssetModelFilesTest.php +++ b/tests/Feature/AssetModels/Api/AssetModelFilesTest.php @@ -22,7 +22,7 @@ class AssetModelFilesTest extends TestCase //Upload a file $this->actingAsForApi($user) ->post( - route('api.models.files.store', ['model_id' => $model[0]["id"]]), [ + route('api.models.files.store', $model), [ 'file' => [UploadedFile::fake()->create("test.jpg", 100)] ]) ->assertOk(); @@ -41,7 +41,7 @@ class AssetModelFilesTest extends TestCase // List the files $this->actingAsForApi($user) ->getJson( - route('api.models.files.index', ['model_id' => $model[0]["id"]])) + route('api.models.files.index', $model)) ->assertOk() ->assertJsonStructure([ 'rows', @@ -62,7 +62,7 @@ class AssetModelFilesTest extends TestCase // Upload a file $this->actingAsForApi($user) ->post( - route('api.models.files.store', ['model_id' => $model[0]["id"]]), [ + route('api.models.files.store', $model), [ 'file' => [UploadedFile::fake()->create("test.jpg", 100)], ]) ->assertOk() @@ -74,7 +74,7 @@ class AssetModelFilesTest extends TestCase // Upload a file with notes $this->actingAsForApi($user) ->post( - route('api.models.files.store', ['model_id' => $model[0]["id"]]), [ + route('api.models.files.store', $model), [ 'file' => [UploadedFile::fake()->create("test.jpg", 100)], 'notes' => 'manual' ]) @@ -87,7 +87,7 @@ class AssetModelFilesTest extends TestCase // List the files to get the file ID $result = $this->actingAsForApi($user) ->getJson( - route('api.models.files.index', ['model_id' => $model[0]["id"]])) + route('api.models.files.index', $model)) ->assertOk() ->assertJsonStructure([ 'total', @@ -105,7 +105,7 @@ class AssetModelFilesTest extends TestCase ] ] ]) - ->assertJsonPath('rows.0.note','') + ->assertJsonPath('rows.0.note',null) ->assertJsonPath('rows.1.note','manual'); @@ -114,7 +114,7 @@ class AssetModelFilesTest extends TestCase $this->actingAsForApi($user) ->get( route('api.models.files.show', [ - 'model_id' => $model[0]["id"], + $model, 'file_id' => $result->decodeResponseJson()->json()["rows"][0]["id"], ])) ->assertOk(); @@ -127,28 +127,28 @@ class AssetModelFilesTest extends TestCase // Create a model to work with $model = AssetModel::factory()->count(1)->create(); - // Create a superuser to run this as - $user = User::factory()->superuser()->create(); + // Create a superuser to run this as + $user = User::factory()->superuser()->create(); - //Upload a file - $this->actingAsForApi($user) - ->post( - route('api.models.files.store', ['model_id' => $model[0]["id"]]), [ - 'file' => [UploadedFile::fake()->create("test.jpg", 100)] - ]) - ->assertOk(); + //Upload a file + $this->actingAsForApi($user) + ->post( + route('api.models.files.store', $model), [ + 'file' => [UploadedFile::fake()->create("test.jpg", 100)] + ]) + ->assertOk(); - // List the files to get the file ID - $result = $this->actingAsForApi($user) - ->getJson( - route('api.models.files.index', ['model_id' => $model[0]["id"]])) - ->assertOk(); + // List the files to get the file ID + $result = $this->actingAsForApi($user) + ->getJson( + route('api.models.files.index', $model)) + ->assertOk(); - // Delete the file - $this->actingAsForApi($user) + // Delete the file + $this->actingAsForApi($user) ->delete( route('api.models.files.destroy', [ - 'model_id' => $model[0]["id"], + $model, 'file_id' => $result->decodeResponseJson()->json()["rows"][0]["id"], ])) ->assertOk()