Skip to content

Commit 2d0eded

Browse files
committed
Clean up DateRangeFilter - broken pills, and missing test
1 parent 7542020 commit 2d0eded

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/Traits/Filters/HandlesPillsData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function getPillDataForFilter(): array
1111
$filters = [];
1212

1313
foreach ($this->getAppliedFiltersWithValuesForPills() as $filterKey => $value) {
14-
if (! is_null($filter = $this->getFilterByKey($filterKey))) {
14+
if (! is_null($filter = $this->getFilterByKey($filterKey)) && !$filter->isEmpty($filter->validate($value))) {
1515
$filters[$filter->getKey()] = FilterPillData::make(
1616
filterKey: $filter->getKey(),
1717
customPillBlade: $filter->getCustomPillBlade() ?? null,

src/Traits/Filters/Helpers/FilterPillsHelpers.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public function getAppliedFiltersWithValuesForPills(): array
4949
}
5050

5151
$validatedValue = $filter->validate($item);
52+
5253
if ($filter instanceof BooleanFilter) {
5354
return ! ($filter->isEmpty($validatedValue));
54-
} elseif ($validatedValue === null || $validatedValue === 'null') {
55+
} elseif ($validatedValue === null || $validatedValue === 'null' || $filter->isEmpty($validatedValue)) {
5556
return false;
5657
} elseif (is_array($validatedValue)) {
58+
$filter->isEmpty($validatedValue);
5759
if (array_key_exists(0, $validatedValue) && (is_null($validatedValue[0]) || $validatedValue[0] == 'null')) {
5860
return false;
5961
}

src/Views/Filters/DateRangeFilter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ public function getFilterPillValue($value): array|string|bool|null
198198

199199
public function isEmpty(array|string|null $value): bool
200200
{
201+
if(is_null($value) || empty($value))
202+
{
203+
return true;
204+
}
201205
$values = [];
202206
if (is_array($value)) {
203207
if (! isset($value['minDate']) || ! isset($value['maxDate'])) {

tests/Unit/Views/Filters/DateRangeFilterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,25 @@ public function test_check_if_can_get_locale(): void
435435
$this->assertSame('de', self::$filterInstance->getPillsLocale());
436436
$this->assertTrue(self::$filterInstance->hasPillsLocale());
437437
}
438+
439+
public function test_can_check_validation_rejects_invalid_values_array(): void
440+
{
441+
$missingStartDate = self::$filterInstance->validate([null,'2020-01-01']);
442+
$missingEndDate = self::$filterInstance->validate(['2020-01-01', null]);
443+
$missingBoth = self::$filterInstance->validate([null,null]);
444+
445+
$this->assertFalse($missingStartDate);
446+
$this->assertFalse($missingEndDate);
447+
$this->assertFalse($missingBoth);
448+
$this->assertTrue(self::$filterInstance->isEmpty($missingStartDate));
449+
$this->assertTrue(self::$filterInstance->isEmpty($missingEndDate));
450+
$this->assertTrue(self::$filterInstance->isEmpty($missingBoth));
451+
}
452+
453+
public function test_can_check_validation_rejects_broken_values_array(): void
454+
{
455+
$this->assertFalse(self::$filterInstance->validate(['minDate' => 'asdf', 'maxDate' => '2020-02-02']));
456+
}
457+
458+
438459
}

0 commit comments

Comments
 (0)