diff --git a/Classes/Domain/Import/Entity/InvalidDataException.php b/Classes/Domain/Import/Entity/InvalidDataException.php new file mode 100644 index 00000000..bb7ff8a3 --- /dev/null +++ b/Classes/Domain/Import/Entity/InvalidDataException.php @@ -0,0 +1,30 @@ + + * + * 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\ThueCat\Domain\Import\Entity; + +use Exception; + +final class InvalidDataException extends Exception +{ +} diff --git a/Classes/Domain/Import/Entity/Properties/OpeningHour.php b/Classes/Domain/Import/Entity/Properties/OpeningHour.php index 357da021..ccdb8708 100644 --- a/Classes/Domain/Import/Entity/Properties/OpeningHour.php +++ b/Classes/Domain/Import/Entity/Properties/OpeningHour.php @@ -23,6 +23,8 @@ namespace WerkraumMedia\ThueCat\Domain\Import\Entity\Properties; +use WerkraumMedia\ThueCat\Domain\Import\Entity\InvalidDataException; + class OpeningHour { /** @@ -36,14 +38,14 @@ class OpeningHour protected $validThrough = null; /** - * @var \DateTimeImmutable + * @var \DateTimeImmutable|null */ - protected $opens; + protected $opens = null; /** - * @var \DateTimeImmutable + * @var \DateTimeImmutable|null */ - protected $closes; + protected $closes = null; /** * @var string[] @@ -62,11 +64,19 @@ public function getValidThrough(): ?\DateTimeImmutable public function getOpens(): \DateTimeImmutable { + if ($this->opens === null) { + throw new InvalidDataException('Opens was empty for opening hour.'); + } + return $this->opens; } public function getCloses(): \DateTimeImmutable { + if ($this->closes === null) { + throw new InvalidDataException('Closes was empty for opening hour.'); + } + return $this->closes; } diff --git a/Classes/Domain/Import/Typo3Converter/GeneralConverter.php b/Classes/Domain/Import/Typo3Converter/GeneralConverter.php index 3b7aebb0..97d646d8 100644 --- a/Classes/Domain/Import/Typo3Converter/GeneralConverter.php +++ b/Classes/Domain/Import/Typo3Converter/GeneralConverter.php @@ -28,6 +28,7 @@ use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; use WerkraumMedia\ThueCat\Domain\Import\Entity\AccessibilitySpecification; use WerkraumMedia\ThueCat\Domain\Import\Entity\Base; +use WerkraumMedia\ThueCat\Domain\Import\Entity\InvalidDataException; use WerkraumMedia\ThueCat\Domain\Import\Entity\MapsToType; use WerkraumMedia\ThueCat\Domain\Import\Entity\MediaObject; use WerkraumMedia\ThueCat\Domain\Import\Entity\Minimum; @@ -424,13 +425,21 @@ private function getOpeningHours(array $openingHours): string $data = []; foreach ($openingHours as $openingHour) { - $data[] = array_filter([ - 'opens' => $openingHour->getOpens()->format('H:i:s'), - 'closes' => $openingHour->getCloses()->format('H:i:s'), - 'from' => $openingHour->getValidFrom() ?? '', - 'through' => $openingHour->getValidThrough() ?? '', - 'daysOfWeek' => $openingHour->getDaysOfWeek(), - ]); + try { + $data[] = array_filter([ + 'opens' => $openingHour->getOpens()->format('H:i:s'), + 'closes' => $openingHour->getCloses()->format('H:i:s'), + 'from' => $openingHour->getValidFrom() ?? '', + 'through' => $openingHour->getValidThrough() ?? '', + 'daysOfWeek' => $openingHour->getDaysOfWeek(), + ]); + } catch (InvalidDataException $e) { + $this->logger->error('Could not import opening hour due to type error: ' . $e->getMessage(), [ + 'errorMessage' => $e->getMessage(), + 'openingHour' => var_export($openingHour, true), + ]); + continue; + } } return json_encode($data) ?: ''; diff --git a/Documentation/Changelog/2.2.0.rst b/Documentation/Changelog/2.2.0.rst index 52cc07d6..af704603 100644 --- a/Documentation/Changelog/2.2.0.rst +++ b/Documentation/Changelog/2.2.0.rst @@ -16,7 +16,9 @@ Features Fixes ----- -Nothing +* Handle broken opening hours. + Those are skipped during import and written as error to TYPO3 logs. + That way entries with broken opening hours can still be imported. Tasks ----- diff --git a/Tests/Functional/AbstractImportTest.php b/Tests/Functional/AbstractImportTest.php index f477f0c4..52742855 100644 --- a/Tests/Functional/AbstractImportTest.php +++ b/Tests/Functional/AbstractImportTest.php @@ -117,7 +117,12 @@ private function getLogFiles(): array { return [ self::getInstancePath() . '/typo3temp/var/log/typo3_0493d91d8e.log', - self::getInstancePath() . '/typo3temp/var/log/typo3_error_0493d91d8e.log', + $this->getErrorLogFile(), ]; } + + protected function getErrorLogFile(): string + { + return self::getInstancePath() . '/typo3temp/var/log/typo3_error_0493d91d8e.log'; + } } diff --git a/Tests/Functional/Fixtures/Import/Guzzle/thuecat.org/resources/attraction-with-broken-opening-hour.json b/Tests/Functional/Fixtures/Import/Guzzle/thuecat.org/resources/attraction-with-broken-opening-hour.json new file mode 100644 index 00000000..a7036aab --- /dev/null +++ b/Tests/Functional/Fixtures/Import/Guzzle/thuecat.org/resources/attraction-with-broken-opening-hour.json @@ -0,0 +1,97 @@ +{ + "@context": { + "cdb": "https://thuecat.org/ontology/cdb/1.0/", + "dachkg": "https://thuecat.org/ontology/dachkg/1.0/", + "dbo": "http://dbpedia.org/ontology/", + "dsv": "http://ontologies.sti-innsbruck.at/dsv/", + "foaf": "http://xmlns.com/foaf/0.1/", + "owl": "http://www.w3.org/2002/07/owl#", + "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "schema": "http://schema.org/", + "sh": "http://www.w3.org/ns/shacl#", + "thuecat": "https://thuecat.org/ontology/thuecat/1.0/", + "ttgds": "https://thuecat.org/ontology/ttgds/1.0/", + "xsd": "http://www.w3.org/2001/XMLSchema#" + }, + "@graph": [ + { + "@id": "https://thuecat.org/resources/attraction-with-broken-opening-hour", + "@type": [ + "schema:Place", + "schema:Thing", + "schema:TouristAttraction", + "ttgds:PointOfInterest" + ], + "schema:name": { + "@language": "de", + "@value": "One specialOpeningHoursSpecification misses opens and closes, leading to type error" + }, + "schema:availableLanguage": [ + { + "@type": "thuecat:Language", + "@value": "thuecat:German" + }, + { + "@type": "thuecat:Language", + "@value": "thuecat:English" + } + ], + "schema:specialOpeningHoursSpecification": [ + { + "@id": "genid-1dccaefc0e06401c93068c8081b7ea8c294892-b11", + "@type": [ + "schema:Thing", + "schema:Intangible", + "schema:StructuredValue", + "schema:OpeningHoursSpecification" + ], + "schema:dayOfWeek": { + "@type": "schema:DayOfWeek", + "@value": "schema:Thursday" + }, + "schema:validThrough": { + "@type": "schema:Date", + "@value": "2024-09-19" + }, + "schema:validFrom": { + "@type": "schema:Date", + "@value": "2024-09-19" + } + }, + { + "@id": "genid-1dccaefc0e06401c93068c8081b7ea8c294892-b12", + "@type": [ + "schema:Thing", + "schema:Intangible", + "schema:StructuredValue", + "schema:OpeningHoursSpecification" + ], + "schema:opens": { + "@type": "schema:Time", + "@value": "09:30:00" + }, + "schema:closes": { + "@type": "schema:Time", + "@value": "18:00:00" + }, + "schema:dayOfWeek": { + "@type": "schema:DayOfWeek", + "@value": "schema:Thursday" + }, + "schema:validThrough": { + "@type": "schema:Date", + "@value": "2024-09-19" + }, + "schema:validFrom": { + "@type": "schema:Date", + "@value": "2024-09-19" + } + } + ], + "thuecat:contentResponsible": { + "@id": "https://thuecat.org/resources/018132452787-ngbe" + } + } + ] +} diff --git a/Tests/Functional/Fixtures/Import/ImportsWithBrokenOpeningHour.xml b/Tests/Functional/Fixtures/Import/ImportsWithBrokenOpeningHour.xml new file mode 100644 index 00000000..b589e556 --- /dev/null +++ b/Tests/Functional/Fixtures/Import/ImportsWithBrokenOpeningHour.xml @@ -0,0 +1,68 @@ + + + + 1 + 0 + 1613400587 + 1613400558 + 1 + 4 + Rootpage + 1 + + + 10 + 1 + 1613400587 + 1613400558 + 1 + 254 + Storage folder + + + + 1 + 0 + English + en-us-gb + en + + + + 1 + 0 + 1613400587 + 1613400558 + 1 + 0 + Tourist Attraction + static + + + + + + + 10 + + + + + + + + https://thuecat.org/resources/attraction-with-broken-opening-hour + + + + 0 + + + + + + + + ]]> + + diff --git a/Tests/Functional/ImportTest.php b/Tests/Functional/ImportTest.php index d3964d9b..d0f27113 100644 --- a/Tests/Functional/ImportTest.php +++ b/Tests/Functional/ImportTest.php @@ -422,6 +422,34 @@ public function importsTouristAttractionWithSingleSlogan(): void $this->assertPHPDataSet(__DIR__ . '/Assertions/Import/ImportsTouristAttractionWithSingleSlogan.php'); } + /** + * @test + */ + public function importsWithBrokenOpeningHour(): void + { + $this->importDataSet(__DIR__ . '/Fixtures/Import/ImportsWithBrokenOpeningHour.xml'); + + GuzzleClientFaker::appendResponseFromFile(__DIR__ . '/Fixtures/Import/Guzzle/thuecat.org/resources/attraction-with-broken-opening-hour.json'); + GuzzleClientFaker::appendResponseFromFile(__DIR__ . '/Fixtures/Import/Guzzle/thuecat.org/resources/018132452787-ngbe.json'); + + $this->importConfiguration(); + + $records = $this->getAllRecords('tx_thuecat_tourist_attraction'); + self::assertCount(1, $this->getAllRecords('tx_thuecat_tourist_attraction')); + $specialOpeningHours = json_decode($records[0]['special_opening_hours'], true, 512, JSON_THROW_ON_ERROR); + self::assertIsArray($specialOpeningHours); + self::assertCount(1, $specialOpeningHours); + + $this->expectErrors = true; + $loggedErrors = file_get_contents($this->getErrorLogFile()); + self::assertIsString($loggedErrors); + self::assertStringContainsString( + 'Could not import opening hour due to type error: Opens was empty for opening hour.', + $loggedErrors + ); + self::assertStringContainsString('\'closes\' => NULL,', $loggedErrors); + } + private function importConfiguration(): void { $configuration = $this->get(ImportConfigurationRepository::class)->findByUid(1);