Skip to content

Commit

Permalink
Merge branch '1.3' of ezsystems/ezplatform-kernel into 4.5 (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Dec 27, 2023
2 parents 843d709 + 8c21882 commit 67811a0
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 62 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -52715,21 +52715,11 @@ parameters:
count: 1
path: tests/lib/Persistence/Legacy/Content/Location/TrashHandlerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\Location\\\\TrashHandlerTest\\:\\:testDeleteTrashItemNoMoreLocations\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Persistence/Legacy/Content/Location/TrashHandlerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\Location\\\\TrashHandlerTest\\:\\:testDeleteTrashItemStillHaveLocations\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Persistence/Legacy/Content/Location/TrashHandlerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\Location\\\\TrashHandlerTest\\:\\:testEmptyTrash\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Persistence/Legacy/Content/Location/TrashHandlerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\Location\\\\TrashHandlerTest\\:\\:testFindTrashItemsWhenEmpty\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class TrashItemDeleteResult extends ValueObject
* @var bool
*/
public $contentRemoved = false;

/** @var array<int> */
public $reverseRelationContentIds = [];
}

class_alias(TrashItemDeleteResult::class, 'eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult');
3 changes: 3 additions & 0 deletions src/lib/Persistence/Legacy/Content/Location/Trash/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,14 @@ protected function delete(Trashed $trashItem)
$result->trashItemId = $trashItem->id;
$result->contentId = $trashItem->contentId;

$reverseRelations = $this->contentHandler->loadReverseRelations($trashItem->contentId);

$this->locationGateway->removeElementFromTrash($trashItem->id);

if ($this->locationGateway->countLocationsByContentId($trashItem->contentId) < 1) {
$this->contentHandler->deleteContent($trashItem->contentId);
$result->contentRemoved = true;
$result->reverseRelationContentIds = array_column($reverseRelations, 'sourceContentId');
}

return $result;
Expand Down
33 changes: 33 additions & 0 deletions src/lib/Search/Common/EventSubscriber/TrashEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace Ibexa\Core\Search\Common\EventSubscriber;

use Ibexa\Contracts\Core\Repository\Events\Trash\DeleteTrashItemEvent;
use Ibexa\Contracts\Core\Repository\Events\Trash\EmptyTrashEvent;
use Ibexa\Contracts\Core\Repository\Events\Trash\RecoverEvent;
use Ibexa\Contracts\Core\Repository\Events\Trash\TrashEvent;
use Ibexa\Contracts\Core\Repository\Values\Content\TrashItem;
Expand All @@ -18,6 +20,8 @@ public static function getSubscribedEvents(): array
return [
RecoverEvent::class => 'onRecover',
TrashEvent::class => 'onTrash',
DeleteTrashItemEvent::class => 'onDeleteTrashItem',
EmptyTrashEvent::class => 'onEmptyTrashEvent',
];
}

Expand All @@ -39,6 +43,35 @@ public function onTrash(TrashEvent $event)
$event->getLocation()->contentId
);
}

public function onDeleteTrashItem(DeleteTrashItemEvent $event): void
{
$contentHandler = $this->persistenceHandler->contentHandler();

$reverseRelationContentIds = $event->getResult()->reverseRelationContentIds;
foreach ($reverseRelationContentIds as $contentId) {
$persistenceContent = $contentHandler->load($contentId);

$this->searchHandler->indexContent($persistenceContent);
}
}

public function onEmptyTrashEvent(EmptyTrashEvent $event): void
{
$contentHandler = $this->persistenceHandler->contentHandler();

$results = $event->getResultList()->getIterator();

/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Trash\TrashItemDeleteResult $result */
foreach ($results as $result) {
$reverseRelationContentIds = $result->reverseRelationContentIds;
foreach ($reverseRelationContentIds as $contentId) {
$persistenceContent = $contentHandler->load($contentId);

$this->searchHandler->indexContent($persistenceContent);
}
}
}
}

class_alias(TrashEventSubscriber::class, 'eZ\Publish\Core\Search\Common\EventSubscriber\TrashEventSubscriber');
109 changes: 57 additions & 52 deletions tests/lib/Persistence/Legacy/Content/Location/TrashHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function testLoadTrashItem()
$handler->loadTrashItem(69);
}

public function testEmptyTrash()
public function testEmptyTrash(): void
{
$handler = $this->getTrashHandler();

Expand All @@ -336,48 +336,51 @@ public function testEmptyTrash()
$iLocation = 0;

$this->locationGateway
->expects($this->at($i++))
->expects(self::at($i++))
->method('countTrashed')
->willReturn(2);

$this->locationGateway
->expects($this->at($i++))
->expects(self::at($i++))
->method('listTrashed')
->will(
$this->returnValue($expectedTrashed)
);
->willReturn($expectedTrashed);

$trashedItemIds = [];
$trashedContentIds = [];

foreach ($expectedTrashed as $trashedElement) {
$this->locationMapper
->expects($this->at($iLocation++))
->expects(self::at($iLocation++))
->method('createLocationFromRow')
->will(
$this->returnValue(
new Trashed(
[
'id' => $trashedElement['node_id'],
'contentId' => $trashedElement['contentobject_id'],
'pathString' => $trashedElement['path_string'],
]
)
->willReturn(
new Trashed(
[
'id' => $trashedElement['node_id'],
'contentId' => $trashedElement['contentobject_id'],
'pathString' => $trashedElement['path_string'],
]
)
);

$this->contentHandler
->expects(self::at($iContent++))
->method('loadReverseRelations')
->with($trashedElement['contentobject_id'])
->willReturn([]);

$this->locationGateway
->expects($this->at($i++))
->expects(self::at($i++))
->method('removeElementFromTrash')
->with($trashedElement['node_id']);

$this->locationGateway
->expects($this->at($i++))
->expects(self::at($i++))
->method('countLocationsByContentId')
->with($trashedElement['contentobject_id'])
->will($this->returnValue(0));
->willReturn(0);

$this->contentHandler
->expects($this->at($iContent++))
->expects(self::at($iContent++))
->method('deleteContent')
->with($trashedElement['contentobject_id']);

Expand All @@ -387,72 +390,74 @@ public function testEmptyTrash()

$returnValue = $handler->emptyTrash();

$this->assertInstanceOf(TrashItemDeleteResultList::class, $returnValue);
self::assertInstanceOf(TrashItemDeleteResultList::class, $returnValue);

foreach ($returnValue->items as $key => $trashItemDeleteResult) {
$this->assertEquals($trashItemDeleteResult->trashItemId, $trashedItemIds[$key]);
$this->assertEquals($trashItemDeleteResult->contentId, $trashedContentIds[$key]);
$this->assertTrue($trashItemDeleteResult->contentRemoved);
self::assertEquals($trashItemDeleteResult->trashItemId, $trashedItemIds[$key]);
self::assertEquals($trashItemDeleteResult->contentId, $trashedContentIds[$key]);
self::assertTrue($trashItemDeleteResult->contentRemoved);
}
}

public function testDeleteTrashItemNoMoreLocations()
public function testDeleteTrashItemNoMoreLocations(): void
{
$handler = $this->getTrashHandler();

$trashItemId = 69;
$contentId = 67;
$this->locationGateway
->expects($this->once())
->expects(self::once())
->method('loadTrashByLocation')
->with($trashItemId)
->will(
$this->returnValue(
[
'node_id' => $trashItemId,
'contentobject_id' => $contentId,
'path_string' => '/1/2/69',
]
)
->willReturn(
[
'node_id' => $trashItemId,
'contentobject_id' => $contentId,
'path_string' => '/1/2/69',
]
);

$this->locationMapper
->expects($this->once())
->expects(self::once())
->method('createLocationFromRow')
->will(
$this->returnValue(
new Trashed(
[
'id' => $trashItemId,
'contentId' => $contentId,
'pathString' => '/1/2/69',
]
)
->willReturn(
new Trashed(
[
'id' => $trashItemId,
'contentId' => $contentId,
'pathString' => '/1/2/69',
]
)
);

$this->contentHandler
->expects(self::once())
->method('loadReverseRelations')
->with($contentId)
->willReturn([]);

$this->locationGateway
->expects($this->once())
->expects(self::once())
->method('removeElementFromTrash')
->with($trashItemId);

$this->locationGateway
->expects($this->once())
->expects(self::once())
->method('countLocationsByContentId')
->with($contentId)
->will($this->returnValue(0));
->willReturn(0);

$this->contentHandler
->expects($this->once())
->expects(self::once())
->method('deleteContent')
->with($contentId);

$trashItemDeleteResult = $handler->deleteTrashItem($trashItemId);

$this->assertInstanceOf(TrashItemDeleteResult::class, $trashItemDeleteResult);
$this->assertEquals($trashItemId, $trashItemDeleteResult->trashItemId);
$this->assertEquals($contentId, $trashItemDeleteResult->contentId);
$this->assertTrue($trashItemDeleteResult->contentRemoved);
self::assertInstanceOf(TrashItemDeleteResult::class, $trashItemDeleteResult);
self::assertEquals($trashItemId, $trashItemDeleteResult->trashItemId);
self::assertEquals($contentId, $trashItemDeleteResult->contentId);
self::assertTrue($trashItemDeleteResult->contentRemoved);
}

public function testDeleteTrashItemStillHaveLocations()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Search\Common\EventSubscriber;

use Ibexa\Contracts\Core\Persistence\Content as PersistenceContent;
use Ibexa\Contracts\Core\Persistence\Content\Handler;
use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler;
use Ibexa\Contracts\Core\Repository\Events\Trash\DeleteTrashItemEvent;
use Ibexa\Contracts\Core\Repository\Values\Content\Trash\TrashItemDeleteResult;
use Ibexa\Contracts\Core\Search\Handler as SearchHandler;
use Ibexa\Core\Repository\Values\Content\TrashItem;
use Ibexa\Core\Search\Common\EventSubscriber\TrashEventSubscriber;
use PHPUnit\Framework\TestCase;

final class TrashEventSubscriberTest extends TestCase
{
/** @var \Ibexa\Contracts\Core\Search\Handler&\PHPUnit\Framework\MockObject\MockObject */
private $searchHandler;

/** @var \Ibexa\Contracts\Core\Persistence\Handler&\PHPUnit\Framework\MockObject\MockObject */
private $persistenceHandler;

/** @var \Ibexa\Core\Search\Common\EventSubscriber\TrashEventSubscriber */
private $subscriber;

protected function setUp(): void
{
$this->searchHandler = $this->createMock(SearchHandler::class);
$this->persistenceHandler = $this->createMock(PersistenceHandler::class);

$this->subscriber = new TrashEventSubscriber(
$this->searchHandler,
$this->persistenceHandler
);
}

public function testOnDeleteTrashItem(): void
{
$trashItem = new TrashItem(['id' => 12345]);
$reverseRelationContentId = 12;
$trashItemDeleteResult = new TrashItemDeleteResult(
[
'trashItemId' => $trashItem->id,
'reverseRelationContentIds' => [$reverseRelationContentId],
]
);

$this->persistenceHandler
->expects(self::once())
->method('contentHandler')
->willReturn($contentHandler = $this->createMock(Handler::class));

$contentHandler
->expects(self::once())
->method('load')
->with($reverseRelationContentId)
->willReturn($content = new PersistenceContent());

$this->searchHandler
->expects(self::once())
->method('indexContent')
->with($content);

$this->subscriber->onDeleteTrashItem(
new DeleteTrashItemEvent(
$trashItemDeleteResult,
$trashItem,
)
);
}
}

0 comments on commit 67811a0

Please sign in to comment.