Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Classes/Hook/DatamapHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,4 +45,12 @@ public function processDatamap_afterDatabaseOperations(string $status, string $t
}
}
}

/**
* @return array<string, mixed>|null
*/
protected function getPageRecord(int $id): ?array
{
return BackendUtility::getRecord('pages', $id);
}
}
60 changes: 60 additions & 0 deletions Tests/Unit/Hook/DatamapHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,4 +50,63 @@ public function testIgnoredRecordsDoNotFlushCache(string $table, int|string $id)
// Remove the unused prototype instance again
GeneralUtility::purgeInstances();
}

/**
* @return array<string, array{array<string, mixed>|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<string, mixed>|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<string, mixed>|null $pageRecord
*/
public function __construct(private readonly ?array $pageRecord) {}

protected function getPageRecord(int $id): ?array
{
return $this->pageRecord;
}
}