Commit 54dbe469 authored by Gábor Hojtsy's avatar Gábor Hojtsy

Issue #3156878 by alexpott, andypost: \Drupal\Component\Datetime\DateTimePlus...

Issue #3156878 by alexpott, andypost: \Drupal\Component\Datetime\DateTimePlus should pass correct parameter types to checkdate()

(cherry picked from commit dc21aa7c)
parent 3dfa5601
......@@ -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.
......
......@@ -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.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment