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
22 changes: 12 additions & 10 deletions Classes/Cache/CloudFrontCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ProcessedFile;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Connection;
Expand Down Expand Up @@ -48,10 +50,10 @@ public function __construct()
* This function handles the cache clearing for files and folders.
* It enqueues the resource identifier and distribution IDs for invalidation.
*
* @param Folder|File $resource The resource that has been modified.
* @param Folder|File|ProcessedFile $resource The resource that has been modified.
* @return void
*/
public function fileMod(Folder|File $resource): void
public function fileMod(Folder|File|ProcessedFile $resource): void
{
$storage = $resource->getStorage();
$storageConfig = $storage->getConfiguration();
Expand Down Expand Up @@ -229,7 +231,7 @@ public function resolveDistributionIds(): array
*/
public function queueClearCache(int $pageId, bool $recursive = false, string|null $distributionIds = null)
{
$errorMessage = 'queueClearCache $pageId: ' . $pageId . ' recursive: ' . $recursive . ' distributionIds: '.$distributionIds;
$errorMessage = 'queueClearCache $pageId: ' . $pageId . ' recursive: ' . $recursive . ' distributionIds: ' . $distributionIds;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, "tm_cloudfront");

$wildcard = '';
Expand All @@ -250,26 +252,25 @@ public function queueClearCache(int $pageId, bool $recursive = false, string|nul
$languages = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageId)->getAllLanguages();

if (count($languages) > 0) {
if($this->isMultiLanguageDomains($entry)){
if ($this->isMultiLanguageDomains($entry)) {
$this->enqueue($this->buildLink($entry, array('_language' => 0)) . $wildcard, $this->distributionsMapping[$languages[0]->getBase()->getHost()]);
foreach ($languages as $k => $lang) {
if ($lang->getLanguageId() != 0) {
$this->enqueue($this->buildLink($entry, array('_language' => $lang->getLanguageId())) . $wildcard, $this->distributionsMapping[$lang->getBase()->getHost()]);
}
}
} else{
} else {
$this->enqueue($this->buildLink($entry, array('_language' => 0)) . $wildcard, $distributionIds);
$errorMessage = 'queueClearCache enque lang: 0 distributionIds: '.$distributionIds;
$errorMessage = 'queueClearCache enque lang: 0 distributionIds: ' . $distributionIds;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, "tm_cloudfront");
foreach ($languages as $k => $lang) {
if ($lang->getLanguageId() != 0) {
$this->enqueue($this->buildLink($entry, array('_language' => $lang->getLanguageId())) . $wildcard, $distributionIds);
$errorMessage = 'queueClearCache enque lang: ' . $lang->getLanguageId() . ' distributionIds: '.$distributionIds;
$errorMessage = 'queueClearCache enque lang: ' . $lang->getLanguageId() . ' distributionIds: ' . $distributionIds;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, "tm_cloudfront");
}
}
}

} else {
$this->enqueue($this->buildLink($entry) . $wildcard, $distributionIds);
}
Expand Down Expand Up @@ -329,7 +330,7 @@ public function isMultiLanguageDomains(int $uid_page): bool
{
$multi = true;
$domains = $this->getLanguagesDomains($uid_page);
foreach ($domains as $lang => $domain){
foreach ($domains as $lang => $domain) {
if (strpos($domain, '.') === false) {
$multi = false;
}
Expand All @@ -353,7 +354,8 @@ protected function generateRandomString(int $length = 10): string
return $randomString;
}

public function resetQueue(){
public function resetQueue()
{
$this->queue = [];
}
}
2 changes: 1 addition & 1 deletion Classes/Hooks/ClearCachePostProc.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function clearCachePostProc(&$params, &$pObj): void
}
} */

if (isset($params['cacheCmd']) && $params['cacheCmd'] == 'all') {
if (isset($params['cacheCmd']) && (in_array($params['cacheCmd'], ['all', 'pages']))) {
// when a clear cache button is clicked
$this->cacheManager->cacheCmd($params);
} elseif (isset($params['cacheCmd']) && MathUtility::canBeInterpretedAsInteger($params['cacheCmd'])) {
Expand Down
49 changes: 31 additions & 18 deletions Tests/Functional/Hooks/ClearCachePostProcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Toumoro\TmCloudfront\Tests\Unit\Hooks;


use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
Expand Down Expand Up @@ -74,9 +75,9 @@ protected function setUp(): void
$this->importCSVDataSet(__DIR__ . '/../DataSet/sys_file_storage.csv');
$this->importCSVDataSet(__DIR__ . '/../DataSet/tt_content.csv');

$this->fileFolderTests = Yaml::parseFile( __DIR__ . '/../Fixtures/fileFolderTests.yaml');
$this->contentPageTests = Yaml::parseFile( __DIR__ . '/../Fixtures/contentPageTests.yaml');
$this->languages = Yaml::parseFile( __DIR__ . '/../Fixtures/languages.yaml');
$this->fileFolderTests = Yaml::parseFile(__DIR__ . '/../Fixtures/fileFolderTests.yaml');
$this->contentPageTests = Yaml::parseFile(__DIR__ . '/../Fixtures/contentPageTests.yaml');
$this->languages = Yaml::parseFile(__DIR__ . '/../Fixtures/languages.yaml');

$this->setUpFrontendSite(1);

Expand All @@ -101,6 +102,21 @@ protected function callInaccessibleMethod($object, $name, ...$arguments)
return $reflectionMethod->invokeArgs($object, $arguments);
}

/**
* Simulate clear cache for pages button
* fixes UnexpectedValueException: ConnectionPool->getQueryBuilderForTable() requires a connection name to be provided.
*/
#[\PHPUnit\Framework\Attributes\Test]
public function testClearPagesCache(): void
{
$this->setUpBackendUser(1);
$tce = GeneralUtility::makeInstance(DataHandler::class);
$tce->start([], []);
$tce->clear_cacheCmd("pages");
$rows = $this->getAllRecords('tx_tmcloudfront_domain_model_invalidation');
$this->assertCount(4, $rows, 'Nombre d’invalidation incorrect');
}

/**
* Modify page or content and check if invalidation is created for simple domain
*/
Expand All @@ -118,7 +134,7 @@ public function modifyTest(): void
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_tmcloudfront_domain_model_invalidation')
->truncate('tx_tmcloudfront_domain_model_invalidation');

$this->actionService->modifyRecord(
$table,
$row['uid'],
Expand All @@ -130,9 +146,9 @@ public function modifyTest(): void
foreach ($allRecords as $record) {
var_dump($record['pathsegment'] . ' / ' . $record['distributionId']);
}
$this->assertCount(count($row['expectedArray']??[]), $allRecords, 'Nombre d’invalidation incorrect');
$this->assertCount(count($row['expectedArray'] ?? []), $allRecords, 'Nombre d’invalidation incorrect');

if(isset($row['expectedArray'])) {
if (isset($row['expectedArray'])) {
foreach ($row['expectedArray'] as $expectedRow) {
$this->checkInvalidation($expectedRow);
}
Expand All @@ -148,7 +164,7 @@ public function modifyTest(): void
public function modifyMultiTest(): void
{
$this->setUpBackendUser(1);
$this->setUpFrontendSite(1,'multiDomain');
$this->setUpFrontendSite(1, 'multiDomain');
$this->actionService = $this->getActionService();

foreach ($this->contentPageTests['multiDomain'] as $table => $rows) {
Expand All @@ -158,7 +174,7 @@ public function modifyMultiTest(): void
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_tmcloudfront_domain_model_invalidation')
->truncate('tx_tmcloudfront_domain_model_invalidation');

$this->actionService->modifyRecord(
$table,
$row['uid'],
Expand All @@ -170,15 +186,13 @@ public function modifyMultiTest(): void
foreach ($allRecords as $record) {
var_dump($record['pathsegment'] . ' / ' . $record['distributionId']);
}
$this->assertCount(count($row['expectedArray']??[]), $allRecords, 'Nombre d’invalidation incorrect');
$this->assertCount(count($row['expectedArray'] ?? []), $allRecords, 'Nombre d’invalidation incorrect');

if(isset($row['expectedArray'])) {
if (isset($row['expectedArray'])) {
foreach ($row['expectedArray'] as $expectedRow) {
$this->checkInvalidation($expectedRow);
}
}


}
}
}
Expand Down Expand Up @@ -214,10 +228,10 @@ public function afterFileReplaced($storage, array $expectedRows): void
__DIR__ . '/../Fixtures/sample.txt',
'contenu texte'
);

// Créer un sous-dossier 'test_fileReplaced_folder' pour les fichiers du test
$folder = $storage->hasFolder('test_fileReplaced_folder') ? $storage->getFolder('test_fileReplaced_folder') : $storage->createFolder('test_fileReplaced_folder');

// Ajouter un fichier
$sourceFile = __DIR__ . '/../Fixtures/sample.txt';
$storageFile = $storage->addFile($sourceFile, $folder, 'replacedfile.txt');
Expand All @@ -237,7 +251,6 @@ public function afterFileReplaced($storage, array $expectedRows): void
foreach ($expectedRows as $expectedRow) {
$this->checkInvalidation($expectedRow);
}

}

public function afterFileMoved($storage, array $expectedRows): void
Expand Down Expand Up @@ -417,10 +430,10 @@ protected function afterFolderDeleted(ResourceStorage $storage, array $expectedR
protected function checkInvalidation(array $expectedRow): void
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_tmcloudfront_domain_model_invalidation');
->getConnectionForTable('tx_tmcloudfront_domain_model_invalidation');
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder
->select('pathsegment','distributionId')
->select('pathsegment', 'distributionId')
->from('tx_tmcloudfront_domain_model_invalidation')
->where(
$queryBuilder->expr()->eq(
Expand Down Expand Up @@ -465,4 +478,4 @@ protected function getActionService()
{
return GeneralUtility::makeInstance(ActionService::class);
}
}
}
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'author' => 'Simon Ouellet',
'author_email' => '',
'state' => 'beta',
'version' => '13.0.1',
'version' => '13.0.2',
'constraints' => [
'depends' => [
'typo3' => '12.4.0-13.5.99',
Expand Down