Skip to content

Commit

Permalink
IBX-6631: Enriched TrashItem with removedLocationContentIdMap
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-6631 and #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
  • Loading branch information
barw4 authored Oct 19, 2023
1 parent 83cc49f commit 336f0a6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
30 changes: 25 additions & 5 deletions eZ/Publish/API/Repository/Tests/TrashServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,6 @@ public function testLoadTrashItem()
$trashItemReloaded->pathString
);

$this->assertEquals(
$trashItem,
$trashItemReloaded
);

$this->assertInstanceOf(
DateTime::class,
$trashItemReloaded->trashed
Expand Down Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions eZ/Publish/API/Repository/Values/Content/TrashItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ abstract class TrashItem extends Location
* @var \DateTime
*/
protected $trashed;

/** @var array<int, int> */
protected $removedLocationContentIdMap = [];

/**
* @return array<int, int>
*/
public function getRemovedLocationContentIdMap(): array
{
return $this->removedLocationContentIdMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion eZ/Publish/Core/Repository/TrashService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
);
Expand Down
3 changes: 3 additions & 0 deletions eZ/Publish/SPI/Persistence/Content/Location/Trashed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ class Trashed extends Location
* @var mixed Trashed timestamp.
*/
public $trashed;

/** @var array<int, int> Location ID to a Content ID map of removed items */
public $removedLocationContentIdMap = [];
}

0 comments on commit 336f0a6

Please sign in to comment.