diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index d3cb15ec7c2d9b45030998c4a41ead05e27b9e0e..0158eac4b580ff4c2a1f5e563909559da145d6cb 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -268,10 +268,11 @@ public function __construct($time = 'now', $timezone = NULL, $settings = []) { $prepared_timezone = $this->prepareTimezone($timezone); try { + $this->errors = []; if (!empty($prepared_time)) { $test = date_parse($prepared_time); if (!empty($test['errors'])) { - $this->errors[] = $test['errors']; + $this->errors = $test['errors']; } } @@ -285,7 +286,6 @@ public function __construct($time = 'now', $timezone = NULL, $settings = []) { // Clean up the error messages. $this->checkErrors(); - $this->errors = array_unique($this->errors); } /** @@ -442,7 +442,7 @@ protected function prepareFormat($format) { public function checkErrors() { $errors = \DateTime::getLastErrors(); if (!empty($errors['errors'])) { - $this->errors += $errors['errors']; + $this->errors = array_merge($this->errors, $errors['errors']); } // Most warnings are messages that the date could not be parsed // which causes it to be altered. For validation purposes, a warning @@ -451,6 +451,8 @@ public function checkErrors() { if (!empty($errors['warnings'])) { $this->errors[] = 'The date is invalid.'; } + + $this->errors = array_values(array_unique($this->errors)); } /** diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index 95fdd64d182af60eb4647529488554a56a1a50d3..314899cbb2f1a090f2cab87d6b2fd4b63c9c59c1 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -670,4 +670,93 @@ public function providerTestInvalidDateDiff() { ]; } + /** + * Tests invalid values passed to constructor. + * + * @param string $time + * A date/time string. + * @param string[] $errors + * An array of error messages. + * + * @covers ::__construct + * + * @dataProvider providerTestInvalidConstructor + */ + public function testInvalidConstructor($time, array $errors) { + $date = new DateTimePlus($time); + + $this->assertEquals(TRUE, $date->hasErrors()); + $this->assertEquals($errors, $date->getErrors()); + } + + /** + * Provider for testInvalidConstructor(). + * + * @return array + * An array of invalid date/time strings, and corresponding error messages. + */ + public function providerTestInvalidConstructor() { + return [ + [ + 'YYYY-MM-DD', + [ + 'The timezone could not be found in the database', + 'Unexpected character', + 'Double timezone specification', + ], + ], + [ + '2017-MM-DD', + [ + 'Unexpected character', + 'The timezone could not be found in the database', + ], + ], + [ + 'YYYY-03-DD', + [ + 'The timezone could not be found in the database', + 'Unexpected character', + 'Double timezone specification', + ], + ], + [ + 'YYYY-MM-07', + [ + 'The timezone could not be found in the database', + 'Unexpected character', + 'Double timezone specification', + ], + ], + [ + '2017-13-55', + [ + 'Unexpected character', + ], + ], + [ + 'YYYY-MM-DD hh:mm:ss', + [ + 'The timezone could not be found in the database', + 'Unexpected character', + 'Double timezone specification', + ], + ], + [ + '2017-03-07 25:70:80', + [ + 'Unexpected character', + 'Double time specification', + ], + ], + [ + 'lorem ipsum dolor sit amet', + [ + 'The timezone could not be found in the database', + 'Double timezone specification', + ], + ], + ]; + } + }