Skip to content

Commit a99b5b3

Browse files
authored
DateRangeFilter - fix broken pills, and add missing test (#2219)
* Clean up DateRangeFilter - broken pills, and missing test
1 parent 7542020 commit a99b5b3

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/Traits/Filters/HandlesPillsData.php

+1-1
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

+3-1
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public function validate(array|string|null $values): array|bool
4040
return false;
4141
}
4242

43-
$startDate = $this->createCarbonDate($returnedValues['minDate']);
44-
$endDate = $this->createCarbonDate($returnedValues['maxDate']);
45-
46-
if (! ($startDate instanceof Carbon) || ! ($endDate instanceof Carbon)) {
43+
if (! (($startDate = $this->createCarbonDate($returnedValues['minDate'])) instanceof Carbon) || ! (($endDate = $this->createCarbonDate($returnedValues['maxDate'])) instanceof Carbon)) {
4744
return false;
4845
}
46+
4947
if ($startDate->gt($endDate)) {
5048
return false;
5149
}
@@ -198,6 +196,9 @@ public function getFilterPillValue($value): array|string|bool|null
198196

199197
public function isEmpty(array|string|null $value): bool
200198
{
199+
if (is_null($value) || empty($value)) {
200+
return true;
201+
}
201202
$values = [];
202203
if (is_array($value)) {
203204
if (! isset($value['minDate']) || ! isset($value['maxDate'])) {

tests/Unit/Views/Filters/DateRangeFilterTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,27 @@ 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+
$this->assertFalse(self::$filterInstance->validate(['minDate' => '4121-31-31', 'maxDate' => '2020-02-02']));
457+
$this->assertFalse(self::$filterInstance->validate(['minDate' => '2020-02-02', 'maxDate' => 'asdf']));
458+
$this->assertFalse(self::$filterInstance->validate(['minDate' => '2020-02-02', 'maxDate' => '4121-31-31']));
459+
460+
}
438461
}

0 commit comments

Comments
 (0)