From 8188cbd04a416129ec1521796b985eddc2c553f5 Mon Sep 17 00:00:00 2001 From: Thomas David Baker Date: Sun, 10 Nov 2024 12:17:18 -0800 Subject: [PATCH] Differentiate between 0 and null in NumDropMenu The season dropdown on eventreport.php has both "All" (empty string, converted to null by Request->optionalInt) and "0" (converted to 0 by Request->optionalInt) both of which are fuzzily equal to 0 but only one of which is strictly equal to 0. So take this opportunity to tighten up the types here and make that work how it should ("All" is preselected when no season specified, but 0 is preselected if season 0 is specified). --- gatherling/Views/Components/NumDropMenu.php | 8 ++------ gatherling/Views/Pages/EventForm.php | 20 ++++++++++---------- gatherling/eventreport.php | 2 +- tests/Views/Components/NumDropMenuTest.php | 7 ++++--- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/gatherling/Views/Components/NumDropMenu.php b/gatherling/Views/Components/NumDropMenu.php index c23d9ce42..df364b89d 100644 --- a/gatherling/Views/Components/NumDropMenu.php +++ b/gatherling/Views/Components/NumDropMenu.php @@ -8,12 +8,8 @@ class NumDropMenu extends DropMenu { - public function __construct(string $field, ?string $title, int $max, int|string|null $def, int $min = 0, ?string $special = null) + public function __construct(string $field, ?string $title, int $max, ?int $def, int $min = 0, ?string $special = null) { - if ($def === '') { - $def = -1; - } - $options = []; if ($special) { $options[] = [ @@ -26,7 +22,7 @@ public function __construct(string $field, ?string $title, int $max, int|string| $options[] = [ 'text' => (string) $n, 'value' => (string) $n, - 'isSelected' => $n == $def, + 'isSelected' => $n === $def, ]; } diff --git a/gatherling/Views/Pages/EventForm.php b/gatherling/Views/Pages/EventForm.php index 9872ea94b..b439ac919 100644 --- a/gatherling/Views/Pages/EventForm.php +++ b/gatherling/Views/Pages/EventForm.php @@ -70,17 +70,17 @@ public function __construct(Event $event, bool $edit) if ($event->start != null) { $date = $event->start; preg_match('/([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):.*/', $date, $datearr); - $year = $datearr[1]; - $month = $datearr[2]; - $day = $datearr[3]; - $hour = $datearr[4]; - $minutes = $datearr[5]; + $year = (int) $datearr[1]; + $month = (int) $datearr[2]; + $day = (int) $datearr[3]; + $hour = (int) $datearr[4]; + $minutes = (int) $datearr[5]; } else { - $year = date('Y', time()); - $month = date('n', time()); - $day = date('j', time()); - $hour = date('H', time()); - $minutes = date('i', time()); + $year = (int) date('Y', time()); + $month = (int) date('n', time()); + $day = (int) date('j', time()); + $hour = (int) date('H', time()); + $minutes = (int) date('i', time()); } $navLinks = []; diff --git a/gatherling/eventreport.php b/gatherling/eventreport.php index 0b8b67503..f61784bdc 100644 --- a/gatherling/eventreport.php +++ b/gatherling/eventreport.php @@ -60,7 +60,7 @@ function eventList(string $format, string $series, ?int $season): array $params['series'] = $series; } - if (!empty($season)) { + if ($season !== null) { $sql .= ' AND e.season = :season'; $params['season'] = $season; } diff --git a/tests/Views/Components/NumDropMenuTest.php b/tests/Views/Components/NumDropMenuTest.php index f6f9f2cfc..bb207a169 100644 --- a/tests/Views/Components/NumDropMenuTest.php +++ b/tests/Views/Components/NumDropMenuTest.php @@ -18,13 +18,14 @@ public function testNumDropMenu(): void $this->assertCount(12, $html->filter('select')->filter('option')); $this->assertEquals('5', $html->filter('select')->filter('option[selected]')->attr('value')); - $numDropMenu = new NumDropMenu('test', 'Test', 10, '5'); + $numDropMenu = new NumDropMenu('test', 'Test', 10, null); $html = new Crawler($numDropMenu->render()); $this->assertEquals('test', $html->filter('select')->attr('name')); $this->assertCount(12, $html->filter('select')->filter('option')); - $this->assertEquals('5', $html->filter('select')->filter('option[selected]')->attr('value')); + // Nothing is explicitly marked as selected, not even the default option + $this->assertEquals(0, $html->filter('select')->filter('option[selected]')->count()); - $numDropMenu = new NumDropMenu('test', 'Test', 10, null); + $numDropMenu = new NumDropMenu('test', 'Test', 10, 0); $html = new Crawler($numDropMenu->render()); $this->assertEquals('test', $html->filter('select')->attr('name')); $this->assertCount(12, $html->filter('select')->filter('option'));