Commit ade05896 authored by alexpott's avatar alexpott

Issue #2858295 by mpdonadio, GoZ, Wim Leers, alexpott: DateTimePlus doesn't track errors properly

parent ac6cb0e7
......@@ -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));
}
/**
......
......@@ -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',
],
],
];
}
}
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