Skip to content

Commit

Permalink
Merge pull request #37 from ans-group/support-false-in-urls
Browse files Browse the repository at this point in the history
Stop false string in URLs being detected as a truthy value
  • Loading branch information
Gman98ish authored Mar 17, 2023
2 parents 0072f42 + 778a3dc commit 3adfefa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Filters/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public function modifyQuery($query, SearchTerm $search)
if ($search->operator() == 'neq') {
$op = '!=';
}
$query->where($search->column(), $op, $search->term() == true ? $this->trueVal : $this->falseVal);

$searchTerm = true;
if ($search->term() == 'false') {
$searchTerm = false;
}

$query->where($search->column(), $op, $searchTerm ? $this->trueVal : $this->falseVal);
}

public function operators()
Expand Down
23 changes: 19 additions & 4 deletions tests/Filters/BooleanFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function can_filter_eq()
{
$filter = new BooleanFilter;
$builder = Pet::query()->getQuery();
$filter->modifyQuery($builder, $this->searchTerm('eq', true));
$filter->modifyQuery($builder, $this->searchTerm('eq', 'true'));

$this->assertEquals(
"select * from `pets` where `is_active` = ?",
Expand All @@ -32,7 +32,7 @@ public function can_filter_neq()
{
$filter = new BooleanFilter;
$builder = Pet::query()->getQuery();
$filter->modifyQuery($builder, $this->searchTerm('neq', true));
$filter->modifyQuery($builder, $this->searchTerm('neq', 'true'));

$this->assertEquals(
"select * from `pets` where `is_active` != ?",
Expand All @@ -46,10 +46,9 @@ public function can_filter_neq()
*/
public function can_override_true_and_false_val()
{

$filter = new BooleanFilter('yes', 'no');
$builder = Pet::query()->getQuery();
$filter->modifyQuery($builder, $this->searchTerm('eq', true));
$filter->modifyQuery($builder, $this->searchTerm('eq', 'true'));

$this->assertEquals(
"select * from `pets` where `is_active` = ?",
Expand All @@ -58,6 +57,22 @@ public function can_override_true_and_false_val()
$this->assertEquals(['yes'], $builder->getBindings());
}

/**
* @test
*/
public function can_handle_false_in_url_string_as_boolean()
{
$filter = new BooleanFilter;
$builder = Pet::query()->getQuery();
$filter->modifyQuery($builder, $this->searchTerm('eq', 'false'));

$this->assertEquals(
"select * from `pets` where `is_active` = ?",
$builder->toSql()
);
$this->assertEquals([0], $builder->getBindings());
}

private function searchTerm($operator, $term)
{
return new SearchTerm('is_active', $operator, 'is_active', $term);
Expand Down

0 comments on commit 3adfefa

Please sign in to comment.