Skip to content

Commit

Permalink
Flush corresponding page caches on import
Browse files Browse the repository at this point in the history
The pages now receive proper cache tags.
The import now properly clears those cache tags.
That way all corresponding pages will show updated content after import
finished.

We need one test that executes frontend requests and import command.
The separation is therefore removed and tests are streamlined to have a
single parent providing all necessary information and setup.
  • Loading branch information
DanielSiepmann committed Jun 15, 2023
1 parent d9fef53 commit edc1e1a
Show file tree
Hide file tree
Showing 18 changed files with 365 additions and 232 deletions.
64 changes: 64 additions & 0 deletions Classes/Caching/CacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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 Wrm\Events\Caching;

use TYPO3\CMS\Core\Cache\CacheManager as Typo3CacheManager;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

class CacheManager
{
/**
* @var Typo3CacheManager
*/
private $cacheManager;

/**
* @var array
*/
private $tags = [
'tx_events_domain_model_date',
'tx_events_domain_model_event',
'tx_events_domain_model_organizer',
'tx_events_domain_model_partner',
'tx_events_domain_model_region',
];

public function __construct(
Typo3CacheManager $cacheManager
) {
$this->cacheManager = $cacheManager;
}

public function addAllCacheTagsToPage(ContentObjectRenderer $cObject): void
{
$cObject->stdWrap_addPageCacheTags('', [
'addPageCacheTags' => implode(',', $this->tags),
]);
}

public function clearAllCacheTags(): void
{
$this->cacheManager->flushCachesByTags($this->tags);
}
}
25 changes: 25 additions & 0 deletions Classes/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,34 @@

use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use Wrm\Events\Caching\CacheManager;

class AbstractController extends ActionController
{
/**
* @var CacheManager
*/
protected $cacheManager;

public function injectCacheManager(CacheManager $cacheManager): void
{
$this->cacheManager = $cacheManager;
}

/**
* Extend to add cache tag to page.
* This allows to clear pages on modifications.
*/
protected function initializeAction(): void
{
parent::initializeAction();

$cObject = $this->configurationManager->getContentObject();
if ($cObject instanceof ContentObjectRenderer) {
$this->cacheManager->addAllCacheTagsToPage($cObject);
}
}

/**
* Extend original to also add data from current cobject if available.
*/
Expand Down
2 changes: 2 additions & 0 deletions Classes/Controller/DateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function __construct(

protected function initializeAction(): void
{
parent::initializeAction();

$contentObject = $this->configurationManager->getContentObject();
if ($contentObject !== null) {
$this->demandFactory->setContentObjectRenderer($contentObject);
Expand Down
2 changes: 2 additions & 0 deletions Classes/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function __construct(

protected function initializeAction(): void
{
parent::initializeAction();

$this->dataProcessing->setConfigurationManager($this->configurationManager);
}

Expand Down
14 changes: 13 additions & 1 deletion Classes/Service/DestinationDataImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use Wrm\Events\Caching\CacheManager;
use Wrm\Events\Domain\Model\Event;
use Wrm\Events\Domain\Model\Import;
use Wrm\Events\Domain\Model\Organizer;
Expand Down Expand Up @@ -102,6 +103,11 @@ class DestinationDataImportService
*/
private $slugger;

/**
* @var CacheManager
*/
private $cacheManager;

/**
* ImportService constructor.
* @param EventRepository $eventRepository
Expand All @@ -115,6 +121,7 @@ class DestinationDataImportService
* @param CategoriesAssignment $categoriesAssignment
* @param LocationAssignment $locationAssignment
* @param Slugger $slugger
* @param CacheManager $cacheManager
*/
public function __construct(
EventRepository $eventRepository,
Expand All @@ -128,7 +135,8 @@ public function __construct(
FilesAssignment $filesAssignment,
CategoriesAssignment $categoriesAssignment,
LocationAssignment $locationAssignment,
Slugger $slugger
Slugger $slugger,
CacheManager $cacheManager
) {
$this->eventRepository = $eventRepository;
$this->organizerRepository = $organizerRepository;
Expand All @@ -142,6 +150,7 @@ public function __construct(
$this->categoriesAssignment = $categoriesAssignment;
$this->locationAssignment = $locationAssignment;
$this->slugger = $slugger;
$this->cacheManager = $cacheManager;
}

public function import(
Expand Down Expand Up @@ -265,6 +274,9 @@ public function processData(array $data): int
$this->slugger->update('tx_events_domain_model_date');
}

$this->logger->info('Flushing cache');
$this->cacheManager->clearAllCacheTags();

$this->logger->info('Finished import');
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,34 @@
* 02110-1301, USA.
*/

namespace Wrm\Events\Tests\Functional\Frontend;
namespace Wrm\Events\Testing;

use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Hook\TypoScriptInstructionModifier;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
use Wrm\Events\Tests\Functional\AbstractFunctionalTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Hook\TypoScriptInstructionModifier as Typo3TypoScriptInstructionModifier;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\RequestBootstrap;

abstract class AbstractTestCase extends AbstractFunctionalTestCase
/**
* Wrap original code to check whether internal request exists.
* It does not exist during import.
*
* No PR upstream as this is probably an unusual use case.
* But we call frontend, import, frontend so have a mix.
*/
class TypoScriptInstructionModifier implements SingletonInterface
{
protected function setUp(): void
public function apply(array $parameters, TemplateService $service): void
{
ArrayUtility::mergeRecursiveWithOverrule($this->configurationToUseInTestInstance, [
'SC_OPTIONS' => [
'Core/TypoScript/TemplateService' => [
'runThroughTemplatesPostProcessing' => [
'FunctionalTest' => TypoScriptInstructionModifier::class . '->apply',
],
],
],
]);
$request = RequestBootstrap::getInternalRequest();
if ($request === null) {
return;
}

parent::setUp();
}

protected function getTypoScriptInstruction(): TypoScriptInstruction
{
return new TypoScriptInstruction(TemplateService::class);
GeneralUtility::callUserFunction(
Typo3TypoScriptInstructionModifier::class . '->apply',
$parameters,
$service
);
}
}
5 changes: 5 additions & 0 deletions Documentation/Changelog/3.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Features
The import of destination data one only added new images but kept existing images untouched.
This was now improved. The import now will update, remove and re-sort images as well.

* Flushes page caches during import and edits.
Proper cache tags are now added to the pages whenever the controller is used.
That ensures that modifications during import or while editing records are flushing
corresponding pages.

Fixes
-----

Expand Down
11 changes: 11 additions & 0 deletions Documentation/Maintenance/TYPO3/V10.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TYPO3 V10
=========

Changes that should happen once we drop TYPO3 v10.


Remove fetching cached page stage from body from tests
------------------------------------------------------

We have different assertions based on TYPO3 version, due to how TYPO3 exposes the info.
We can remove the condition with its content once we drop v10.

0 comments on commit edc1e1a

Please sign in to comment.