From 6b17b82355f2cf7730b8e3f3abc6343f4745a6ff Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 9 Sep 2024 11:07:07 +0200 Subject: [PATCH] Make time zone for slugs configurable Slugs of dates are generated during import. This might lead to confusion if the time zone differs from frontend. Therefore the time zone is now configurable to allow adjustments for the actual website. A new interface `TimeZoneProviderInterface` is provided which can be re configured to a different implementation. The default implementation will use TYPO3s `SYS.phpTimeZone` setting, with fall back to `date_default_timezone_get()` call. That way it should be useful for most systems out of the box. Resolves: #11345 --- .../TimeZone/TimeZoneProviderInterface.php | 31 +++++++++++++ .../Typo3ConfiguredTimeZoneProvider.php | 37 +++++++++++++++ .../Slugger/Date.php | 8 +++- Documentation/Changelog/4.2.1.rst | 39 ++++++++++++++++ ...mportsWithSystemConfiguredTimeZoneTest.php | 46 +++++++++++++++++++ ext_emconf.php | 2 +- 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 Classes/Domain/TimeZone/TimeZoneProviderInterface.php create mode 100644 Classes/Domain/TimeZone/Typo3ConfiguredTimeZoneProvider.php create mode 100644 Documentation/Changelog/4.2.1.rst create mode 100644 Tests/Functional/Import/DestinationDataTest/ImportsWithSystemConfiguredTimeZoneTest.php diff --git a/Classes/Domain/TimeZone/TimeZoneProviderInterface.php b/Classes/Domain/TimeZone/TimeZoneProviderInterface.php new file mode 100644 index 0000000..259ddb9 --- /dev/null +++ b/Classes/Domain/TimeZone/TimeZoneProviderInterface.php @@ -0,0 +1,31 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +namespace WerkraumMedia\Events\Domain\TimeZone; + +use DateTimeZone; + +interface TimeZoneProviderInterface +{ + public function getTimeZone(): DateTimeZone; +} diff --git a/Classes/Domain/TimeZone/Typo3ConfiguredTimeZoneProvider.php b/Classes/Domain/TimeZone/Typo3ConfiguredTimeZoneProvider.php new file mode 100644 index 0000000..dea1938 --- /dev/null +++ b/Classes/Domain/TimeZone/Typo3ConfiguredTimeZoneProvider.php @@ -0,0 +1,37 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +namespace WerkraumMedia\Events\Domain\TimeZone; + +use DateTimeZone; + +final class Typo3ConfiguredTimeZoneProvider implements TimeZoneProviderInterface +{ + public function getTimeZone(): DateTimeZone + { + $timeZone = $GLOBALS['TYPO3_CONF_VARS']['SYS']['phpTimeZone'] ?? null; + $timeZone = $timeZone ?: date_default_timezone_get(); + + return new DateTimeZone($timeZone); + } +} diff --git a/Classes/Service/DestinationDataImportService/Slugger/Date.php b/Classes/Service/DestinationDataImportService/Slugger/Date.php index 4556c1d..d0c244b 100644 --- a/Classes/Service/DestinationDataImportService/Slugger/Date.php +++ b/Classes/Service/DestinationDataImportService/Slugger/Date.php @@ -24,18 +24,22 @@ namespace WerkraumMedia\Events\Service\DestinationDataImportService\Slugger; use DateTimeImmutable; +use DateTimeZone; use TYPO3\CMS\Core\Database\ConnectionPool; +use WerkraumMedia\Events\Domain\TimeZone\TimeZoneProviderInterface; final class Date implements SluggerType { public function __construct( - private readonly ConnectionPool $connectionPool + private readonly ConnectionPool $connectionPool, + private readonly TimeZoneProviderInterface $timeZoneProvider, ) { } public function prepareRecordForSlugGeneration(array $record): array { - $start = new DateTimeImmutable('@' . $record['start']); + $start = new DateTimeImmutable('@' . $record['start'], new DateTimeZone('UTC')); + $start = $start->setTimezone($this->timeZoneProvider->getTimeZone()); $record['event-title'] = $this->getEventTitle((int)$record['event']); $record['start'] = $start->format('Y-m-d'); diff --git a/Documentation/Changelog/4.2.1.rst b/Documentation/Changelog/4.2.1.rst new file mode 100644 index 0000000..5f9b3ba --- /dev/null +++ b/Documentation/Changelog/4.2.1.rst @@ -0,0 +1,39 @@ +4.3.0 +===== + +Breaking +-------- + +Nothing + +Features +-------- + +Nothing + +Fixes +----- + +* Make time zone for slugs configurable. + + Slugs of dates are generated during import. + This might lead to confusion if the time zone differs from frontend. + Therefore the time zone is now configurable to allow adjustments for the actual + website. + + A new interface `TimeZoneProviderInterface` is provided which can be re configured to a + different implementation. + + The default implementation will use TYPO3s `SYS.phpTimeZone` setting, with fall + back to `date_default_timezone_get()` call. + That way it should be useful for most systems out of the box. + +Tasks +----- + +Nothing + +Deprecation +----------- + +Nothing diff --git a/Tests/Functional/Import/DestinationDataTest/ImportsWithSystemConfiguredTimeZoneTest.php b/Tests/Functional/Import/DestinationDataTest/ImportsWithSystemConfiguredTimeZoneTest.php new file mode 100644 index 0000000..7d16c74 --- /dev/null +++ b/Tests/Functional/Import/DestinationDataTest/ImportsWithSystemConfiguredTimeZoneTest.php @@ -0,0 +1,46 @@ +configurationToUseInTestInstance, [ + 'SYS' => [ + 'phpTimeZone' => 'Europe/Berlin', + ], + ]); + + parent::setUp(); + + $this->setUpConfiguration([ + 'restUrl = https://example.com/some-path/', + ]); + $this->importPHPDataSet(__DIR__ . '/Fixtures/Database/DefaultImportConfiguration.php'); + $this->setDateAspect(new DateTimeImmutable('2022-07-13', new DateTimeZone('UTC'))); + } + + #[Test] + public function withTYPO3TimeZone(): void + { + $this->setUpResponses([new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/ResponseWithSingleDate.json') ?: '')]); + + $this->executeCommand(); + + $dates = $this->getAllRecords('tx_events_domain_model_date'); + self::assertSame('kurzfuehrung-historische-altstadt-2022-07-13t15-00-00', $dates[0]['slug']); + + $this->assertEmptyLog(); + } +} diff --git a/ext_emconf.php b/ext_emconf.php index 6ad5214..a79d2f2 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -9,7 +9,7 @@ 'author' => 'Dirk Koritnik, Daniel Siepmann', 'author_email' => 'koritnik@werkraum-media.de, coding@daniel-siepmann.de', 'state' => 'stable', - 'version' => '4.2.0', + 'version' => '4.2.1', 'constraints' => [ 'depends' => [ 'typo3' => '',