From 43d66a9c8f6dfb37e643aec254a121a0995b1ec2 Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Thu, 18 Sep 2025 16:12:14 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20could=20delete=20unexisting?= =?UTF-8?q?=20resource?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/Requests/DestroyRequest.php | 6 +++++ .../Controllers/DeleteOperationsTest.php | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Http/Requests/DestroyRequest.php b/src/Http/Requests/DestroyRequest.php index a180064..c087d66 100644 --- a/src/Http/Requests/DestroyRequest.php +++ b/src/Http/Requests/DestroyRequest.php @@ -2,6 +2,7 @@ namespace Lomkit\Rest\Http\Requests; +use Illuminate\Validation\Rule; use Lomkit\Rest\Http\Resource; class DestroyRequest extends RestRequest @@ -32,10 +33,15 @@ public function rules() */ public function destroyRules(Resource $resource) { + $model = $resource::newModel(); + return [ 'resources' => [ 'required', 'array', ], + 'resources.*' => [ + Rule::exists($model->getTable(), $model->getKeyName()) + ] ]; } } diff --git a/tests/Feature/Controllers/DeleteOperationsTest.php b/tests/Feature/Controllers/DeleteOperationsTest.php index cb577d1..e4bf24f 100644 --- a/tests/Feature/Controllers/DeleteOperationsTest.php +++ b/tests/Feature/Controllers/DeleteOperationsTest.php @@ -56,6 +56,28 @@ public function test_deleting_a_non_authorized_model_with_an_authorized_one(): v $this->assertDatabaseHas('models', $modelDeletable->only('id')); } + public function test_deleting_a_not_existing_model(): void + { + $model = ModelFactory::new()->count(1)->createOne(); + + Gate::policy(Model::class, GreenPolicy::class); + + $response = $this->delete( + '/api/models', + [ + 'resources' => [ + 'undefined-id', + $model->getKey() + ], + ], + ['Accept' => 'application/json'] + ); + + $response->assertStatus(422); + $response->assertExactJsonStructure(['message', 'errors' => ['resources.0']]); + $this->assertDatabaseHas('models', $model->only('id')); + } + public function test_deleting_a_model(): void { $model = ModelFactory::new()->count(1)->createOne(); From 61774f6cb91d77f8d11120e3f6f33107f4965448 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 18 Sep 2025 14:12:26 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- src/Http/Requests/DestroyRequest.php | 4 ++-- tests/Feature/Controllers/DeleteOperationsTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Requests/DestroyRequest.php b/src/Http/Requests/DestroyRequest.php index c087d66..c76f40f 100644 --- a/src/Http/Requests/DestroyRequest.php +++ b/src/Http/Requests/DestroyRequest.php @@ -40,8 +40,8 @@ public function destroyRules(Resource $resource) 'required', 'array', ], 'resources.*' => [ - Rule::exists($model->getTable(), $model->getKeyName()) - ] + Rule::exists($model->getTable(), $model->getKeyName()), + ], ]; } } diff --git a/tests/Feature/Controllers/DeleteOperationsTest.php b/tests/Feature/Controllers/DeleteOperationsTest.php index e4bf24f..0be7ac6 100644 --- a/tests/Feature/Controllers/DeleteOperationsTest.php +++ b/tests/Feature/Controllers/DeleteOperationsTest.php @@ -67,7 +67,7 @@ public function test_deleting_a_not_existing_model(): void [ 'resources' => [ 'undefined-id', - $model->getKey() + $model->getKey(), ], ], ['Accept' => 'application/json'] From e2b21ee43b087f48a4562e773e95731a4a97cffd Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Thu, 18 Sep 2025 16:16:50 +0200 Subject: [PATCH 3/5] Update DeleteOperationsTest.php --- tests/Feature/Controllers/DeleteOperationsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/DeleteOperationsTest.php b/tests/Feature/Controllers/DeleteOperationsTest.php index e4bf24f..4b44275 100644 --- a/tests/Feature/Controllers/DeleteOperationsTest.php +++ b/tests/Feature/Controllers/DeleteOperationsTest.php @@ -66,7 +66,7 @@ public function test_deleting_a_not_existing_model(): void '/api/models', [ 'resources' => [ - 'undefined-id', + 999999, $model->getKey() ], ], From fbf555d942171f939ca48371f55c875502e5f872 Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Thu, 18 Sep 2025 17:23:47 +0200 Subject: [PATCH 4/5] code rabbit --- src/Http/Requests/DestroyRequest.php | 3 ++- tests/Feature/Controllers/DeleteOperationsTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Http/Requests/DestroyRequest.php b/src/Http/Requests/DestroyRequest.php index c76f40f..772d666 100644 --- a/src/Http/Requests/DestroyRequest.php +++ b/src/Http/Requests/DestroyRequest.php @@ -23,7 +23,7 @@ public function rules() /** * Define the validation rules for destroying resources. * - * @param resource $resource + * @param Resource $resource * * @return array * @@ -40,6 +40,7 @@ public function destroyRules(Resource $resource) 'required', 'array', ], 'resources.*' => [ + 'distinct', Rule::exists($model->getTable(), $model->getKeyName()), ], ]; diff --git a/tests/Feature/Controllers/DeleteOperationsTest.php b/tests/Feature/Controllers/DeleteOperationsTest.php index f32072d..b60b4c6 100644 --- a/tests/Feature/Controllers/DeleteOperationsTest.php +++ b/tests/Feature/Controllers/DeleteOperationsTest.php @@ -56,7 +56,7 @@ public function test_deleting_a_non_authorized_model_with_an_authorized_one(): v $this->assertDatabaseHas('models', $modelDeletable->only('id')); } - public function test_deleting_a_not_existing_model(): void + public function test_deleting_a_non_existing_model(): void { $model = ModelFactory::new()->count(1)->createOne(); From 3c47b5c31f42db1e2cc174607a452240270c58c4 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 18 Sep 2025 15:23:57 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- src/Http/Requests/DestroyRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Requests/DestroyRequest.php b/src/Http/Requests/DestroyRequest.php index 772d666..4bfcc7e 100644 --- a/src/Http/Requests/DestroyRequest.php +++ b/src/Http/Requests/DestroyRequest.php @@ -23,7 +23,7 @@ public function rules() /** * Define the validation rules for destroying resources. * - * @param Resource $resource + * @param resource $resource * * @return array *