From 336f0a658d56d913a9d44b67f88a88ae5f423539 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Thu, 19 Oct 2023 16:59:09 +0200 Subject: [PATCH] IBX-6631: Enriched `TrashItem` with `removedLocationContentIdMap` For more details see https://issues.ibexa.co/browse/IBX-6631 and https://github.com/ezsystems/ezplatform-kernel/pull/388 Key changes: * Enriched `TrashItem` with `removedLocationContentIdMap` API `TrashItem` object, when returned as a result of `TrashService::trash` operation, contains a scalar key->value map of trashed Location ID to its Content ID. It includes trashed subtree Locations. Available also via `TrashEvent` to delegate handling of trashed subtree, if needed. * [Tests] Added an integration test coverage for the change --- .../API/Repository/Tests/TrashServiceTest.php | 30 +++++++++++++++---- .../Repository/Values/Content/TrashItem.php | 11 +++++++ .../Legacy/Content/Location/Trash/Handler.php | 11 ++++++- eZ/Publish/Core/Repository/TrashService.php | 5 +++- .../Persistence/Content/Location/Trashed.php | 3 ++ 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/eZ/Publish/API/Repository/Tests/TrashServiceTest.php b/eZ/Publish/API/Repository/Tests/TrashServiceTest.php index c46d00ae25..c4bbe2950a 100644 --- a/eZ/Publish/API/Repository/Tests/TrashServiceTest.php +++ b/eZ/Publish/API/Repository/Tests/TrashServiceTest.php @@ -254,11 +254,6 @@ public function testLoadTrashItem() $trashItemReloaded->pathString ); - $this->assertEquals( - $trashItem, - $trashItemReloaded - ); - $this->assertInstanceOf( DateTime::class, $trashItemReloaded->trashed @@ -1041,6 +1036,31 @@ public function testDeleteThrowsNotFoundExceptionForNonExistingTrashItem() )); } + public function testTrashProperlyAssignsRemovedLocationContentMapToTrashItem(): void + { + $repository = $this->getRepository(); + $trashService = $repository->getTrashService(); + $locationService = $repository->getLocationService(); + + $folder1 = $this->createFolder(['eng-GB' => 'Folder1'], 2); + $folder2 = $this->createFolder(['eng-GB' => 'Folder2'], $folder1->contentInfo->getMainLocationId()); + $folder3 = $this->createFolder(['eng-GB' => 'Folder2'], $folder2->contentInfo->getMainLocationId()); + + $folderLocation = $locationService->loadLocation($folder1->contentInfo->getMainLocationId()); + + $trashItem = $trashService->trash($folderLocation); + $removedLocationContentMap = $trashItem->getRemovedLocationContentIdMap(); + + self::assertSame( + [ + $folderLocation->id => $folder1->id, + $folder2->contentInfo->getMainLocationId() => $folder2->id, + $folder3->contentInfo->getMainLocationId() => $folder3->id, + ], + $removedLocationContentMap, + ); + } + /** * @return array */ diff --git a/eZ/Publish/API/Repository/Values/Content/TrashItem.php b/eZ/Publish/API/Repository/Values/Content/TrashItem.php index 009ab9d81e..6f0545d23d 100644 --- a/eZ/Publish/API/Repository/Values/Content/TrashItem.php +++ b/eZ/Publish/API/Repository/Values/Content/TrashItem.php @@ -19,4 +19,15 @@ abstract class TrashItem extends Location * @var \DateTime */ protected $trashed; + + /** @var array */ + protected $removedLocationContentIdMap = []; + + /** + * @return array + */ + public function getRemovedLocationContentIdMap(): array + { + return $this->removedLocationContentIdMap; + } } diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Location/Trash/Handler.php b/eZ/Publish/Core/Persistence/Legacy/Content/Location/Trash/Handler.php index c395782956..d4affc5cdd 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/Location/Trash/Handler.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/Location/Trash/Handler.php @@ -109,6 +109,7 @@ public function trashSubtree($locationId) $locationRows = $this->locationGateway->getSubtreeContent($locationId); $isLocationRemoved = false; $parentLocationId = null; + $removedLocationsContentMap = []; foreach ($locationRows as $locationRow) { if ($locationRow['node_id'] == $locationId) { @@ -117,6 +118,7 @@ public function trashSubtree($locationId) if ($this->locationGateway->countLocationsByContentId($locationRow['contentobject_id']) == 1) { $this->locationGateway->trashLocation($locationRow['node_id']); + $removedLocationsContentMap[(int)$locationRow['node_id']] = (int)$locationRow['contentobject_id']; } else { if ($locationRow['node_id'] == $locationId) { $isLocationRemoved = true; @@ -143,7 +145,14 @@ public function trashSubtree($locationId) $this->locationHandler->markSubtreeModified($parentLocationId, time()); } - return $isLocationRemoved ? null : $this->loadTrashItem($locationId); + if ($isLocationRemoved === true) { + return null; + } + + $trashItem = $this->loadTrashItem($locationId); + $trashItem->removedLocationContentIdMap = $removedLocationsContentMap; + + return $trashItem; } /** diff --git a/eZ/Publish/Core/Repository/TrashService.php b/eZ/Publish/Core/Repository/TrashService.php index 55c628d3ff..3e3e31de7f 100644 --- a/eZ/Publish/Core/Repository/TrashService.php +++ b/eZ/Publish/Core/Repository/TrashService.php @@ -377,7 +377,10 @@ protected function buildDomainTrashItemObject(Trashed $spiTrashItem, Content $co 'depth' => $spiTrashItem->depth, 'sortField' => $spiTrashItem->sortField, 'sortOrder' => $spiTrashItem->sortOrder, - 'trashed' => isset($spiTrashItem->trashed) ? new DateTime('@' . $spiTrashItem->trashed) : new DateTime('@0'), + 'trashed' => isset($spiTrashItem->trashed) + ? new DateTime('@' . $spiTrashItem->trashed) + : new DateTime('@0'), + 'removedLocationContentIdMap' => $spiTrashItem->removedLocationContentIdMap, 'parentLocation' => $this->proxyDomainMapper->createLocationProxy($spiTrashItem->parentId), ] ); diff --git a/eZ/Publish/SPI/Persistence/Content/Location/Trashed.php b/eZ/Publish/SPI/Persistence/Content/Location/Trashed.php index a6ff294dcd..3cae5d9ac7 100644 --- a/eZ/Publish/SPI/Persistence/Content/Location/Trashed.php +++ b/eZ/Publish/SPI/Persistence/Content/Location/Trashed.php @@ -19,4 +19,7 @@ class Trashed extends Location * @var mixed Trashed timestamp. */ public $trashed; + + /** @var array Location ID to a Content ID map of removed items */ + public $removedLocationContentIdMap = []; }