From c4ebcd9d59532b7a58f6d993e4c23fc2c6a94714 Mon Sep 17 00:00:00 2001 From: agchan12 Date: Mon, 3 Jun 2024 11:03:47 +1000 Subject: [PATCH] Increase CanValidateDateTimeTest invalid data test coverage This change is to ensure `Respect\Validation` is intentional about the DateTime formats. For context the [ISO 8601][] standard says that we shouldn't accept `-00:00` (or anything similar) offsets. I noticed that there was an a [change][] in how `CanValidateDateTime.php` behaved from v2.2 to v2.3. Prior to v2.3 date time formats of `2018-01-30T19:04:35-00:00` (note the -00:00) would pass validation. After updating to v2.3 the format is not accepted. This is because the `DateTime::createFromFormat` accepts the `$value` of `2018-01-30T19:04:35-00:00` but internally converts the `-00:00` to `+00:00` ``` $formattedDate = DateTime::createFromFormat( $format, $value, new DateTimeZone(date_default_timezone_get()) ); ``` This in turn causes the validation around `$value !== $formattedDate->format($format))` to fail ``` if ($formattedDate === false || $value !== $formattedDate->format($format)) { return false; } ``` [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601#Other_time_offset_specifications [change]: 5fe4b96ebf651407d6b8ba2272e4bdab980b4438 --- tests/unit/Helpers/CanValidateDateTimeTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/Helpers/CanValidateDateTimeTest.php b/tests/unit/Helpers/CanValidateDateTimeTest.php index 9857e0123..689a3fa94 100644 --- a/tests/unit/Helpers/CanValidateDateTimeTest.php +++ b/tests/unit/Helpers/CanValidateDateTimeTest.php @@ -64,6 +64,10 @@ public static function providerForInvalidDateTime(): array ['Y-m-d', '0000-00-31'], ['Y-m-d', '0000-12-00'], ['Y-m-d H:i:s', '1987-12-31'], + ['c', '2018-01-30T19:04:35-00:00'], + ['Y-m-d\TH:i:sP', '2018-01-30T19:04:35-00:00'], + ['r', 'Tue, 30 Jan 2018 19:06:01 -0000'], + ['D, d M Y H:i:s O', 'Tue, 30 Jan 2018 19:06:01 -0000'], ]; } }