Commit 357b503e authored by alexpott's avatar alexpott

Issue #2084455 by Mile23: Drupal\Component\Datetime\DateTimePlus::checkArray() compares wrong value

parent 68af24db
......@@ -112,9 +112,15 @@ public static function createFromDateTime(\DateTime $datetime, $settings = array
* @param array $date_parts
* An array of date parts, like ('year' => 2014, 'month => 4).
* @param mixed $timezone
* @see __construct()
* (optional) \DateTimeZone object, time zone string or NULL. NULL uses the
* default system time zone. Defaults to NULL.
* @param array $settings
* @see __construct()
* (optional) A keyed array for settings, suitable for passing on to
* __construct().
*
* @return static
* A new \Drupal\Component\DateTimePlus object based on the parameters
* passed in.
*/
public static function createFromArray(array $date_parts, $timezone = NULL, $settings = array()) {
$date_parts = static::prepareArray($date_parts, TRUE);
......@@ -216,11 +222,12 @@ public static function createFromFormat($format, $time, $timezone = NULL, $setti
* Constructs a date object set to a requested date and timezone.
*
* @param string $time
* A date/time string. Defaults to 'now'.
* (optional) A date/time string. Defaults to 'now'.
* @param mixed $timezone
* PHP DateTimeZone object, string or NULL allowed.
* Defaults to NULL.
* (optional) \DateTimeZone object, time zone string or NULL. NULL uses the
* default system time zone. Defaults to NULL.
* @param array $settings
* (optional) Keyed array of settings. Defaults to empty array.
* - langcode: (optional) String two letter language code used to control
* the result of the format(). Defaults to NULL.
* - debug: (optional) Boolean choice to leave debug values in the
......@@ -475,7 +482,7 @@ public static function checkArray($array) {
foreach (array('hour', 'minute', 'second') as $key) {
if (array_key_exists($key, $array)) {
$value = $array[$key];
switch ($value) {
switch ($key) {
case 'hour':
if (!preg_match('/^([1-2][0-3]|[01]?[0-9])$/', $value)) {
$valid_time = FALSE;
......
......@@ -60,6 +60,24 @@ public function testDateArrays($input, $timezone, $expected) {
$this->assertEquals($expected, $value, sprintf("Test new DateTimePlus(%s, %s): should be %s, found %s.", $input, $timezone, $expected, $value));
}
/**
* Test creating dates from invalid array input.
*
* @param mixed $input
* Input argument for DateTimePlus.
* @param string $timezone
* Timezone argument for DateTimePlus.
*
* @dataProvider providerTestInvalidDateArrays
* @expectedException \Exception
*/
public function testInvalidDateArrays($input, $timezone) {
$this->assertInstanceOf(
'\Drupal\Component\DateTimePlus',
DateTimePlus::createFromArray($input, $timezone)
);
}
/**
* Test creating dates from timestamps, and manipulating timezones.
*
......@@ -336,28 +354,29 @@ public function providerTestInvalidDates() {
}
/**
* Provides data for testInvalidDates.
* Data provider for testInvalidDateArrays.
*
* @return array
* An array of arrays, each containing:
* - 'input' - Input for DateTimePlus.
* - 'timezone' - Timezone for DateTimePlus.
* - 'format' - Format for DateTimePlus.
* - 'message' - Message to display on failure.
*
* @see testInvalidDateArrays
*/
public function providerTestInvalidDateArrays() {
return array(
// Test for invalid year from date array. 10000 as a year will
// create an exception error in the PHP DateTime object.
array(array('year' => 10000, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0), 'America/Chicago', NULL, "array('year' => 10000, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0) contains an invalid year and did not produce errors."),
// One year larger than the documented upper limit of checkdate().
array(array('year' => 32768, 'month' => 1, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0), 'America/Chicago'),
// One year smaller than the documented lower limit of checkdate().
array(array('year' => 0, 'month' => 1, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0), 'America/Chicago'),
// Test for invalid month from date array.
array(array('year' => 2010, 'month' => 27, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0), 'America/Chicago', NULL, "array('year' => 2010, 'month' => 27, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0) contains an invalid month and did not produce errors."),
array(array('year' => 2010, 'month' => 27, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0), 'America/Chicago'),
// Test for invalid hour from date array.
array(array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 80, 'minute' => 0, 'second' => 0), 'America/Chicago', NULL, "array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 80, 'minute' => 0, 'second' => 0) contains an invalid hour and produces errors."),
array(array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 80, 'minute' => 0, 'second' => 0), 'America/Chicago'),
// Test for invalid minute from date array.
array(array('year' => 2010, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 88, 'second' => 0), 'America/Chicago', NULL, "array('year' => 2010, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 88, 'second' => 0) contains an invalid minute and produces errors."),
array(array('year' => 2010, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 88, 'second' => 0), 'America/Chicago'),
// Regression test for https://drupal.org/node/2084455
array(array('hour' => 59, 'minute' => 1,'second' => 1), 'America/Chicago'),
);
}
......
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