From c2ad4bcddc46fb10cb0f51464f4a4551ef4d8eaf Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 3 Apr 2017 15:16:42 +0100 Subject: [PATCH] Issue #2865829 by mpdonadio: DateTimePlus test of validate_format = FALSE --- .../Component/Datetime/DateTimePlusTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index 314899cbb2f1..f120007d4820 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -759,4 +759,23 @@ public function providerTestInvalidConstructor() { ]; } + /** + * Tests the $settings['validate_format'] parameter in ::createFromFormat(). + */ + public function testValidateFormat() { + // Check that an input that does not strictly follow the input format will + // produce the desired date. In this case the year string '11' doesn't + // precisely match the 'Y' formater parameter, but PHP will parse it + // regardless. However, when formatted with the same string, the year will + // be output with four digits. With the ['validate_format' => FALSE] + // $settings, this will not thrown an exception. + $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => FALSE]); + $this->assertEquals('0011-03-31 17:44:00', $date->format('Y-m-d H:i:s')); + + // Parse the same date with ['validate_format' => TRUE] and make sure we + // get the expected exception. + $this->setExpectedException(\UnexpectedValueException::class); + $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => TRUE]); + } + } -- GitLab