From ab26e7560016592c37e75a322607c4d8691945eb Mon Sep 17 00:00:00 2001 From: Marc Willmann Date: Fri, 20 Nov 2026 10:12:45 +0100 Subject: [PATCH] [BUGFIX] Handle deleted pages in DatamapHook BackendUtility::getRecord() returns null for a deleted page. If such a page is updated (e.g. an admin changes its permissions in the Access module from an outdated view), the hook accessed an array offset on null, which TYPO3 turns into an exception. A page without record is now treated like a page which must not be cached, so its static cache is flushed. Co-Authored-By: Claude Opus 5.5 --- Classes/Hook/DatamapHook.php | 13 ++++++- Tests/Unit/Hook/DatamapHookTest.php | 60 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/Classes/Hook/DatamapHook.php b/Classes/Hook/DatamapHook.php index 0b4d63a7b5f..4f2fb51e19b 100644 --- a/Classes/Hook/DatamapHook.php +++ b/Classes/Hook/DatamapHook.php @@ -28,8 +28,9 @@ public function processDatamap_afterDatabaseOperations(string $status, string $t return; } - $row = BackendUtility::getRecord($table, (int) $id); - $allowSfc = (bool) $row['tx_staticfilecache_cache']; + // A deleted page has no record anymore and must not stay in the cache either + $row = $this->getPageRecord((int) $id); + $allowSfc = (bool) ($row['tx_staticfilecache_cache'] ?? false); if (!$allowSfc) { try { // Delete right now!! do not wait until queue is deleting this @@ -44,4 +45,12 @@ public function processDatamap_afterDatabaseOperations(string $status, string $t } } } + + /** + * @return array|null + */ + protected function getPageRecord(int $id): ?array + { + return BackendUtility::getRecord('pages', $id); + } } diff --git a/Tests/Unit/Hook/DatamapHookTest.php b/Tests/Unit/Hook/DatamapHookTest.php index cc81bb1408f..89b44173714 100644 --- a/Tests/Unit/Hook/DatamapHookTest.php +++ b/Tests/Unit/Hook/DatamapHookTest.php @@ -8,6 +8,7 @@ use SFC\Staticfilecache\Service\CacheService; use SFC\Staticfilecache\Service\ConfigurationService; use SFC\Staticfilecache\Tests\Unit\AbstractTest; +use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -49,4 +50,63 @@ public function testIgnoredRecordsDoNotFlushCache(string $table, int|string $id) // Remove the unused prototype instance again GeneralUtility::purgeInstances(); } + + /** + * @return array|null, bool}> + */ + public static function pageRecordsDataProvider(): array + { + return [ + 'cache allowed' => [['tx_staticfilecache_cache' => 1], false], + 'cache disallowed' => [['tx_staticfilecache_cache' => 0], true], + // e.g. a page which is updated after it was deleted + 'deleted page' => [null, true], + ]; + } + + /** + * @param array|null $pageRecord + */ + #[\PHPUnit\Framework\Attributes\DataProvider('pageRecordsDataProvider')] + public function testCacheIsFlushedIfPageIsNotCacheable(?array $pageRecord, bool $expectFlush): void + { + $configurationService = $this->createMock(ConfigurationService::class); + $configurationService->expects($expectFlush ? self::once() : self::never())->method('override')->with('boostMode', '0'); + GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationService); + + $cache = $this->createMock(FrontendInterface::class); + $cache->expects($expectFlush ? self::once() : self::never())->method('flushByTag')->with('pageId_42'); + $cacheService = $this->createStub(CacheService::class); + $cacheService->method('get')->willReturn($cache); + GeneralUtility::addInstance(CacheService::class, $cacheService); + + $dataHandler = (new \ReflectionClass(DataHandler::class))->newInstanceWithoutConstructor(); + + // TYPO3 turns warnings into exceptions, so a warning has to fail the test + set_error_handler( + static function (int $severity, string $message, string $file, int $line): never { + throw new \ErrorException($message, 0, $severity, $file, $line); + }, + ); + + try { + (new TestableDatamapHook($pageRecord))->processDatamap_afterDatabaseOperations('update', 'pages', 42, [], $dataHandler); + } finally { + restore_error_handler(); + GeneralUtility::purgeInstances(); + } + } +} + +final class TestableDatamapHook extends DatamapHook +{ + /** + * @param array|null $pageRecord + */ + public function __construct(private readonly ?array $pageRecord) {} + + protected function getPageRecord(int $id): ?array + { + return $this->pageRecord; + } }