From 5becd6adb218dabbf262e9a6ac2ba663b1e3e942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Hojtsy?= Date: Wed, 23 Sep 2020 16:15:25 +0200 Subject: [PATCH] Issue #3156878 by alexpott, andypost: \Drupal\Component\Datetime\DateTimePlus should pass correct parameter types to checkdate() (cherry picked from commit dc21aa7c12e3dd3a70c8da991335fe1c37859b25) --- .../Component/Datetime/DateTimePlus.php | 9 ++-- .../Component/Datetime/DateTimePlusTest.php | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index ef200d19f1..01c4647013 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -624,11 +624,10 @@ public static function checkArray($array) { $valid_date = FALSE; $valid_time = TRUE; // Check for a valid date using checkdate(). Only values that - // meet that test are valid. - if (array_key_exists('year', $array) && array_key_exists('month', $array) && array_key_exists('day', $array)) { - if (@checkdate($array['month'], $array['day'], $array['year'])) { - $valid_date = TRUE; - } + // meet that test are valid. An empty value, either a string or a 0, is not + // a valid value. + if (!empty($array['year']) && !empty($array['month']) && !empty($array['day'])) { + $valid_date = checkdate($array['month'], $array['day'], $array['year']); } // Testing for valid time is reversed. Missing time is OK, // but incorrect values are not. diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index ca280d582b..cde8dacad8 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -112,6 +112,23 @@ public function testInvalidDateArrays($input, $timezone, $class) { ); } + /** + * Tests DateTimePlus::checkArray(). + * + * @param mixed $array + * Input argument for DateTimePlus::checkArray(). + * @param bool $expected + * The expected result of DateTimePlus::checkArray(). + * + * @dataProvider providerTestCheckArray + */ + public function testCheckArray(array $array, $expected) { + $this->assertSame( + $expected, + DateTimePlus::checkArray($array) + ); + } + /** * Test creating dates from timestamps, and manipulating timezones. * @@ -442,6 +459,30 @@ public function providerTestInvalidDateArrays() { ]; } + /** + * Data provider for testCheckArray. + * + * @return array + * An array of arrays, each containing: + * - 'array' - Input for DateTimePlus::checkArray(). + * - 'expected' - Expected output for DateTimePlus::checkArray(). + * + * @see testCheckArray + */ + public function providerTestCheckArray() { + return [ + 'Date array, date only' => [['year' => 2010, 'month' => 2, 'day' => 28], TRUE], + 'Date array with hour' => [['year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10], TRUE], + 'One year larger than the documented upper limit of checkdate()' => [['year' => 32768, 'month' => 1, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0], FALSE], + 'One year smaller than the documented lower limit of checkdate()' => [['year' => 0, 'month' => 1, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0], FALSE], + 'Invalid month from date array' => [['year' => 2010, 'month' => 27, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0], FALSE], + 'Invalid hour from date array' => [['year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 80, 'minute' => 0, 'second' => 0], FALSE], + 'Invalid minute from date array.' => [['year' => 2010, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 88, 'second' => 0], FALSE], + 'Missing day' => [['year' => 2059, 'month' => 1, 'second' => 1], FALSE], + 'Zero day' => [['year' => 2059, 'month' => 1, 'day' => 0], FALSE], + ]; + } + /** * Provides data for testDateTimezone. * -- GitLab