Skip to content

Commit

Permalink
Merge pull request #92 from tailflow/hotfix/boolean-query-params
Browse files Browse the repository at this point in the history
fix: parsing boolean query params
  • Loading branch information
alexzarbn authored May 24, 2021
2 parents 81f4113 + a2ab618 commit 805a8e2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/InteractsWithSoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function softDeletes(string $resourceModelClass): bool
*/
protected function forceDeletes(Request $request, bool $softDeletes): bool
{
return $softDeletes && $request->get('force');
return $softDeletes && filter_var($request->query('force', false), FILTER_VALIDATE_BOOLEAN);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ public function applySoftDeletesToQuery($query, Request $request): bool
return false;
}

if ($request->has('with_trashed')) {
if (filter_var($request->query('with_trashed', false), FILTER_VALIDATE_BOOLEAN)) {
$query->withTrashed();
} elseif ($request->has('only_trashed')) {
} elseif (filter_var($request->query('only_trashed', false), FILTER_VALIDATE_BOOLEAN)) {
$query->onlyTrashed();
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/StandardDeleteOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public function trashing_a_single_soft_deletable_resource_when_authorized(): voi
$this->assertResourceTrashed($response, $post);
}

/** @test */
public function trashing_a_single_soft_deletable_resource_when_authorized_with_force_set_to_false(): void
{
$post = factory(Post::class)->create(['user_id' => factory(User::class)->create()->id]);

Gate::policy(Post::class, GreenPolicy::class);

$response = $this->delete("/api/posts/{$post->id}?force=false");

$this->assertResourceTrashed($response, $post);
}

/** @test */
public function trashing_a_single_soft_deletable_resource_with_custom_key(): void
{
Expand Down
36 changes: 34 additions & 2 deletions tests/Feature/StandardIndexOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getting_a_paginated_list_of_resources(): void
}

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

/** @test */
public function getting_a_list_of_soft_deletable_resources_when_only_trashed_query_parameter_is_present(): void
public function getting_a_list_of_soft_deletable_resources_when_with_trashed_query_parameter_is_set_to_false(): void
{
factory(Post::class)->state('trashed')->times(5)->create();
$posts = factory(Post::class)->times(5)->create();

Gate::policy(Post::class, GreenPolicy::class);

$response = $this->get('/api/posts?with_trashed=false');

$this->assertResourcesPaginated(
$response,
$this->makePaginator($posts, 'posts')
);
}

/** @test */
public function getting_a_list_of_soft_deletable_resources_when_only_trashed_query_parameter_is_set_to_true(): void
{
$trashedPosts = factory(Post::class)->state('trashed')->times(5)->create();
factory(Post::class)->times(5)->create();
Expand All @@ -109,6 +125,22 @@ public function getting_a_list_of_soft_deletable_resources_when_only_trashed_que
);
}

/** @test */
public function getting_a_list_of_soft_deletable_resources_when_only_trashed_query_parameter_is_set_to_false(): void
{
factory(Post::class)->state('trashed')->times(5)->create();
$posts = factory(Post::class)->times(5)->create();

Gate::policy(Post::class, GreenPolicy::class);

$response = $this->get('/api/posts?only_trashed=false');

$this->assertResourcesPaginated(
$response,
$this->makePaginator($posts, 'posts')
);
}

/** @test */
public function getting_a_list_of_soft_deletable_resources_with_trashed_resources_filtered_out(): void
{
Expand Down

0 comments on commit 805a8e2

Please sign in to comment.