Skip to content

Commit 805a8e2

Browse files
authored
Merge pull request #92 from tailflow/hotfix/boolean-query-params
fix: parsing boolean query params
2 parents 81f4113 + a2ab618 commit 805a8e2

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

src/Concerns/InteractsWithSoftDeletes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function softDeletes(string $resourceModelClass): bool
2828
*/
2929
protected function forceDeletes(Request $request, bool $softDeletes): bool
3030
{
31-
return $softDeletes && $request->get('force');
31+
return $softDeletes && filter_var($request->query('force', false), FILTER_VALIDATE_BOOLEAN);
3232
}
3333

3434
/**

src/Drivers/Standard/QueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ public function applySoftDeletesToQuery($query, Request $request): bool
233233
return false;
234234
}
235235

236-
if ($request->has('with_trashed')) {
236+
if (filter_var($request->query('with_trashed', false), FILTER_VALIDATE_BOOLEAN)) {
237237
$query->withTrashed();
238-
} elseif ($request->has('only_trashed')) {
238+
} elseif (filter_var($request->query('only_trashed', false), FILTER_VALIDATE_BOOLEAN)) {
239239
$query->onlyTrashed();
240240
}
241241

tests/Feature/StandardDeleteOperationsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ public function trashing_a_single_soft_deletable_resource_when_authorized(): voi
6363
$this->assertResourceTrashed($response, $post);
6464
}
6565

66+
/** @test */
67+
public function trashing_a_single_soft_deletable_resource_when_authorized_with_force_set_to_false(): void
68+
{
69+
$post = factory(Post::class)->create(['user_id' => factory(User::class)->create()->id]);
70+
71+
Gate::policy(Post::class, GreenPolicy::class);
72+
73+
$response = $this->delete("/api/posts/{$post->id}?force=false");
74+
75+
$this->assertResourceTrashed($response, $post);
76+
}
77+
6678
/** @test */
6779
public function trashing_a_single_soft_deletable_resource_with_custom_key(): void
6880
{

tests/Feature/StandardIndexOperationsTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function getting_a_paginated_list_of_resources(): void
7878
}
7979

8080
/** @test */
81-
public function getting_a_list_of_soft_deletable_resources_when_with_trashed_query_parameter_is_present(): void
81+
public function getting_a_list_of_soft_deletable_resources_when_with_trashed_query_parameter_is_set_to_true(): void
8282
{
8383
$trashedPosts = factory(Post::class)->state('trashed')->times(5)->create();
8484
$posts = factory(Post::class)->times(5)->create();
@@ -94,7 +94,23 @@ public function getting_a_list_of_soft_deletable_resources_when_with_trashed_que
9494
}
9595

9696
/** @test */
97-
public function getting_a_list_of_soft_deletable_resources_when_only_trashed_query_parameter_is_present(): void
97+
public function getting_a_list_of_soft_deletable_resources_when_with_trashed_query_parameter_is_set_to_false(): void
98+
{
99+
factory(Post::class)->state('trashed')->times(5)->create();
100+
$posts = factory(Post::class)->times(5)->create();
101+
102+
Gate::policy(Post::class, GreenPolicy::class);
103+
104+
$response = $this->get('/api/posts?with_trashed=false');
105+
106+
$this->assertResourcesPaginated(
107+
$response,
108+
$this->makePaginator($posts, 'posts')
109+
);
110+
}
111+
112+
/** @test */
113+
public function getting_a_list_of_soft_deletable_resources_when_only_trashed_query_parameter_is_set_to_true(): void
98114
{
99115
$trashedPosts = factory(Post::class)->state('trashed')->times(5)->create();
100116
factory(Post::class)->times(5)->create();
@@ -109,6 +125,22 @@ public function getting_a_list_of_soft_deletable_resources_when_only_trashed_que
109125
);
110126
}
111127

128+
/** @test */
129+
public function getting_a_list_of_soft_deletable_resources_when_only_trashed_query_parameter_is_set_to_false(): void
130+
{
131+
factory(Post::class)->state('trashed')->times(5)->create();
132+
$posts = factory(Post::class)->times(5)->create();
133+
134+
Gate::policy(Post::class, GreenPolicy::class);
135+
136+
$response = $this->get('/api/posts?only_trashed=false');
137+
138+
$this->assertResourcesPaginated(
139+
$response,
140+
$this->makePaginator($posts, 'posts')
141+
);
142+
}
143+
112144
/** @test */
113145
public function getting_a_list_of_soft_deletable_resources_with_trashed_resources_filtered_out(): void
114146
{

0 commit comments

Comments
 (0)