Skip to content

Commit 8214eea

Browse files
[FIX] Refinery: Handle deprecated DateTimeInterface::RFC7231 format
1 parent 5eb61dd commit 8214eea

2 files changed

Lines changed: 68 additions & 15 deletions

File tree

components/ILIAS/Refinery/src/KindlyTo/Transformation/DateTimeTransformation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class DateTimeTransformation implements Transformation
3939
use DeriveApplyToFromTransform;
4040
use DeriveInvokeFromTransform;
4141

42+
private const string RFC7231 = 'D, d M Y H:i:s \G\M\T';
43+
4244
/**
4345
* @inheritDoc
4446
*/
@@ -53,7 +55,7 @@ public function transform($from): DateTimeImmutable
5355
DateTimeInterface::COOKIE,
5456
DateTimeInterface::ISO8601,
5557
DateTimeInterface::RFC822,
56-
DateTimeInterface::RFC7231,
58+
self::RFC7231, // DateTimeInterface::RFC7231 format (deprecated constant in PHP 8.5)
5759
DateTimeInterface::RFC3339_EXTENDED
5860
];
5961

components/ILIAS/Refinery/tests/KindlyTo/Transformation/DateTimeTransformationTest.php

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use ILIAS\Refinery\KindlyTo\Transformation\DateTimeTransformation;
2727
use PHPUnit\Framework\TestCase;
2828
use PHPUnit\Framework\Attributes\DataProvider;
29+
use DateTimeZone;
2930

3031
class DateTimeTransformationTest extends TestCase
3132
{
@@ -45,6 +46,29 @@ public function testDateTimeISOTransformation(mixed $originVal, DateTimeImmutabl
4546
$this->assertEquals($expectedVal, $transformedValue);
4647
}
4748

49+
/**
50+
* @see https://github.com/php/php-src/pull/2450
51+
* @see https://github.com/php/php-src/pull/12989
52+
*/
53+
public function testRFC7231ResultsInMisleadingFormattedDateString(): void
54+
{
55+
$gmt_format = 'D, d M Y H:i:s \G\M\T'; // former DateTimeInterface::RFC7231
56+
$test_gmt_date_time = 'Mon, 06 Jul 2020 12:23:05 GMT';
57+
58+
$exptected = DateTimeImmutable::createFromFormat(
59+
$gmt_format,
60+
$test_gmt_date_time
61+
);
62+
63+
$actual = $this->transformation->transform($test_gmt_date_time);
64+
$this->assertEquals($exptected, $actual);
65+
66+
$actual = $actual->setTimezone(new DateTimeZone('Europe/Berlin'));
67+
// GMT in the provided format is just a string, it does not effect the presented timezone
68+
$this->assertSame('Mon, 06 Jul 2020 14:23:05 GMT', $actual->format($gmt_format));
69+
$this->assertEquals($exptected, $actual);
70+
}
71+
4872
#[DataProvider('TransformationFailureDataProvider')]
4973
public function testTransformIsInvalid(string $failingValue): void
5074
{
@@ -57,20 +81,47 @@ public static function DateTimeTransformationDataProvider(): array
5781
$now = new DateTimeImmutable();
5882
return [
5983
'datetime' => [$now, $now],
60-
'iso8601' => ['2020-07-06T12:23:05+0000',
61-
DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2020-07-06T12:23:05+0000')],
62-
'atom' => ['2020-07-06T12:23:05+00:00',
63-
DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, '2020-07-06T12:23:05+00:00')],
64-
'rfc3339_ext' => ['2020-07-06T12:23:05.000+00:00',
65-
DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, '2020-07-06T12:23:05.000+00:00')],
66-
'cookie' => ['Monday, 06-Jul-2020 12:23:05 GMT+0000',
67-
DateTimeImmutable::createFromFormat(DateTimeInterface::COOKIE, 'Monday, 06-Jul-2020 12:23:05 GMT+0000')],
68-
'rfc822' => ['Mon, 06 Jul 20 12:23:05 +0000',
69-
DateTimeImmutable::createFromFormat(DateTimeInterface::RFC822, 'Mon, 06 Jul 20 12:23:05 +0000')],
70-
'rfc7231' => ['Mon, 06 Jul 2020 12:23:05 GMT',
71-
DateTimeImmutable::createFromFormat(DateTimeInterface::RFC7231, 'Mon, 06 Jul 2020 12:23:05 GMT')],
72-
'unix_timestamp' => [481556262, DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '1985-04-05T13:37:42+0000')],
73-
'unix_timestamp_float' => [481556262.4, DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '1985-04-05T13:37:42+0000')]
84+
'iso8601' => [
85+
'2020-07-06T12:23:05+0000',
86+
DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2020-07-06T12:23:05+0000')
87+
],
88+
'atom' => [
89+
'2020-07-06T12:23:05+00:00',
90+
DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, '2020-07-06T12:23:05+00:00')
91+
],
92+
'rfc3339_ext' => [
93+
'2020-07-06T12:23:05.000+00:00',
94+
DateTimeImmutable::createFromFormat(
95+
DateTimeInterface::RFC3339_EXTENDED,
96+
'2020-07-06T12:23:05.000+00:00'
97+
)
98+
],
99+
'cookie' => [
100+
'Monday, 06-Jul-2020 12:23:05 GMT+0000',
101+
DateTimeImmutable::createFromFormat(DateTimeInterface::COOKIE, 'Monday, 06-Jul-2020 12:23:05 GMT+0000')
102+
],
103+
'rfc822' => [
104+
'Mon, 06 Jul 20 12:23:05 +0000',
105+
DateTimeImmutable::createFromFormat(DateTimeInterface::RFC822, 'Mon, 06 Jul 20 12:23:05 +0000')
106+
],
107+
'rfc7231' => [
108+
'Mon, 06 Jul 2020 12:23:05 GMT',
109+
DateTimeImmutable::createFromFormat('D, d M Y H:i:s \G\M\T', 'Mon, 06 Jul 2020 12:23:05 GMT')
110+
],
111+
'unix_timestamp' => [
112+
481556262,
113+
DateTimeImmutable::createFromFormat(
114+
DateTimeInterface::ISO8601,
115+
'1985-04-05T13:37:42+0000'
116+
)
117+
],
118+
'unix_timestamp_float' => [
119+
481556262.4,
120+
DateTimeImmutable::createFromFormat(
121+
DateTimeInterface::ISO8601,
122+
'1985-04-05T13:37:42+0000'
123+
)
124+
]
74125
];
75126
}
76127

0 commit comments

Comments
 (0)