Skip to content

Commit

Permalink
[4.x] Handle empty values in collection tag filters (#8915)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Nov 2, 2023
1 parent 16fd55d commit e816cf3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Tags/Collection/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ protected function queryRedirects($query)

protected function queryableConditionParams()
{
return $this->traitQueryableConditionParams()->reject(function ($value, $key) {
return Str::startsWith($key, 'taxonomy:');
});
return $this->traitQueryableConditionParams()
->reject(fn ($value, $key) => Str::startsWith($key, 'taxonomy:'))
->reject(fn ($value, $key) => $value === '');
}
}
6 changes: 3 additions & 3 deletions src/Tags/Concerns/QueriesConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ protected function queryConditions($query)

protected function queryableConditionParams()
{
return $this->params->filter(function ($value, $param) {
return Str::contains($param, ':');
});
return $this->params
->filter(fn ($value, $param) => Str::contains($param, ':'))
->reject(fn ($value, $param) => $value === '');
}

protected function isQueryingCondition($field)
Expand Down
40 changes: 40 additions & 0 deletions tests/Tags/Concerns/QueriesConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function it_filters_by_is_condition()
$this->assertCount(4, $this->getEntries(['featured:is' => false]));
}

/** @test */
public function it_does_not_filter_by_is_condition_when_value_is_empty()
{
$this->makeEntry('a')->set('author', 'john-doe')->save();
$this->makeEntry('b')->set('author', 'david-hasselhoff')->save();
$this->makeEntry('c')->set('author', 'josiah-bartlet')->save();

$this->assertCount(3, $this->getEntries(['author:is' => '']));
}

/** @test */
public function it_filters_by_not_condition()
{
Expand All @@ -78,6 +88,16 @@ public function it_filters_by_not_condition()
$this->assertCount(4, $this->getEntries(['featured:not' => false]));
}

/** @test */
public function it_does_not_filter_by_not_condition_when_value_is_empty()
{
$this->makeEntry('a')->set('author', 'john-doe')->save();
$this->makeEntry('b')->set('author', 'david-hasselhoff')->save();
$this->makeEntry('c')->set('author', 'josiah-bartlet')->save();

$this->assertCount(3, $this->getEntries(['author:not' => '']));
}

/** @test */
public function it_filters_by_contains_condition()
{
Expand All @@ -89,6 +109,16 @@ public function it_filters_by_contains_condition()
$this->assertCount(1, $this->getEntries(['title:contains' => 'sto']));
}

/** @test */
public function it_does_not_filter_by_contains_condition_when_value_is_empty()
{
$this->makeEntry('dog')->set('title', 'Dog Stories')->save();
$this->makeEntry('cat')->set('title', 'Cat Fables')->save();
$this->makeEntry('tiger')->set('title', 'Tiger Tales')->save();

$this->assertCount(3, $this->getEntries(['title:contains' => '']));
}

/** @test */
public function it_filters_by_doesnt_contain_condition()
{
Expand All @@ -100,6 +130,16 @@ public function it_filters_by_doesnt_contain_condition()
$this->assertCount(2, $this->getEntries(['title:doesnt_contain' => 'sto']));
}

/** @test */
public function it_does_not_filter_by_doesnt_contains_condition_when_value_is_empty()
{
$this->makeEntry('dog')->set('title', 'Dog Stories')->save();
$this->makeEntry('cat')->set('title', 'Cat Fables')->save();
$this->makeEntry('tiger')->set('title', 'Tiger Tales')->save();

$this->assertCount(3, $this->getEntries(['title:doesnt_contain' => '']));
}

/** @test */
public function it_filters_by_in_condition()
{
Expand Down

0 comments on commit e816cf3

Please sign in to comment.