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; + } }