Skip to content

Commit

Permalink
IBX-2844: Handled archived URL aliases cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed May 16, 2022
1 parent eea306c commit 70b2130
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
26 changes: 26 additions & 0 deletions eZ/Publish/API/Repository/Tests/URLAliasServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,32 @@ static function (Connection $connection) use ($nestedFolderLocation) {
$urlAliasService->refreshSystemUrlAliasesForLocation($nestedFolderLocation);
}

public function testOverrideHistoryUrlAliasAtTheSameLocation(): void
{
$repository = $this->getRepository();
$urlAliasService = $repository->getURLAliasService();
$locationService = $repository->getLocationService();

$folderNames = ['eng-GB' => 'foo'];
$folder = $this->createFolder($folderNames, 2);
$destinationFolder = $this->createFolder($folderNames, 2);

$location = $locationService->loadLocation($folder->contentInfo->mainLocationId);
$destinationFolderLocation = $locationService->loadLocation($destinationFolder->contentInfo->mainLocationId);

$locationService->moveSubtree($location, $destinationFolderLocation);

$urlAliasService->lookup('foo');
$urlAliasService->lookup('foo2/foo');

$newFolder = ['eng-GB' => 'foo'];
$this->createFolder($newFolder, 2);

$newAlias = $urlAliasService->lookup('foo');

self::assertFalse($newAlias->isHistory);
}

/**
* Lookup given URL and check if it is archived and points to the given Location Id.
*
Expand Down
46 changes: 35 additions & 11 deletions eZ/Publish/Core/Persistence/Cache/Tests/UrlAliasHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,45 @@ public function getHandlerClassName(): string
return SPIUrlAliasHandler::class;
}

public function testPublishUrlAliasForLocation(): void
{
$this->loggerMock->expects(self::once())->method('logCall');

$innerHandlerMock = $this->createMock($this->getHandlerClassName());
$this->persistenceHandlerMock
->expects(self::once())
->method('urlAliasHandler')
->willReturn($innerHandlerMock);

$innerHandlerMock
->expects(self::once())
->method('publishUrlAliasForLocation')
->with(44, 2, 'name', 'eng-GB', true, false);

$innerHandlerMock
->expects(self::once())
->method('listURLAliasesForLocation')
->with(44)
->willReturn([]);

$this->cacheIdentifierGeneratorMock
->expects(self::exactly(3))
->method('generateTag')
->withConsecutive(
['url_alias_location', [44], false],
['url_alias_location_path', [44], false],
['url_alias_not_found', [], false]
)
->willReturnOnConsecutiveCalls('urlal-44', 'urlalp-44', 'urlanf');

$handler = $this->persistenceCacheHandler->urlAliasHandler();
$handler->publishUrlAliasForLocation(44, 2, 'name', 'eng-GB', true, false);
}

public function providerForUnCachedMethods(): array
{
// string $method, array $arguments, array? $tagGeneratingArguments, array? $keyGeneratingArguments, array? $tags, array? $key, ?mixed $returnValue
return [
[
'publishUrlAliasForLocation',
[44, 2, 'name', 'eng-GB', true, false],
[
['url_alias_location', [44], false],
['url_alias_location_path', [44], false],
['url_alias_not_found', [], false],
],
null,
['urlal-44', 'urlalp-44', 'urlanf'],
],
[
'createCustomUrlAlias',
[44, '1/2/44', true, null, false],
Expand Down
34 changes: 28 additions & 6 deletions eZ/Publish/Core/Persistence/Cache/UrlAliasHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace eZ\Publish\Core\Persistence\Cache;

use eZ\Publish\API\Repository\Exceptions\BadStateException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\SPI\Persistence\Content\UrlAlias;
Expand Down Expand Up @@ -53,7 +54,9 @@ public function publishUrlAliasForLocation(
]
);

$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
$urlAliasHandler = $this->persistenceHandler->urlAliasHandler();

$urlAliasHandler->publishUrlAliasForLocation(
$locationId,
$parentLocationId,
$name,
Expand All @@ -62,11 +65,30 @@ public function publishUrlAliasForLocation(
$updatePathIdentificationString
);

$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_PATH_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_NOT_FOUND_IDENTIFIER),
]);
try {
$existingLocationAliases = $urlAliasHandler->listURLAliasesForLocation($locationId);
} catch (BadStateException $e) {
$existingLocationAliases = [];
}

$existingLocationAliasesTags = [];
foreach ($existingLocationAliases as $existingAlias) {
$existingLocationAliasesTags[] = $this->cacheIdentifierGenerator->generateTag(
self::URL_ALIAS_IDENTIFIER,
[$existingAlias->id]
);
}

$this->cache->invalidateTags(
array_merge(
[
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_PATH_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_NOT_FOUND_IDENTIFIER),
],
$existingLocationAliasesTags
)
);
}

/**
Expand Down

0 comments on commit 70b2130

Please sign in to comment.