Skip to content

Commit

Permalink
Merge pull request #104 from adamwojs/update_related_co
Browse files Browse the repository at this point in the history
EZEE-1784: Update reverse relations when content object is published
  • Loading branch information
andrerom authored Jan 4, 2018
2 parents 8308172 + 49ea796 commit 5cbf22c
Show file tree
Hide file tree
Showing 19 changed files with 901 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Resources/config/slots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
- "@ezpublish.api.persistence_handler"

ez_recommendation.ez_slot.publish_version:
parent: ez_recommendation.ez_slot.base
parent: ez_recommendation.ez_slot.persistence_aware_base
class: "%ez_recommendation.ez_slot.publish_version.class%"
tags:
- {name: ezpublish.api.slot, signal: ContentService\PublishVersionSignal}
Expand Down Expand Up @@ -78,7 +78,7 @@ services:
- {name: ezpublish.api.slot, signal: LocationService\MoveSubtreeSignal}

ez_recommendation.ez_slot.trash:
parent: ez_recommendation.ez_slot.base
parent: ez_recommendation.ez_slot.persistence_aware_base
class: "%ez_recommendation.ez_slot.trash.class%"
tags:
- {name: ezpublish.api.slot, signal: TrashService\TrashSignal}
Expand Down
26 changes: 26 additions & 0 deletions Tests/eZ/Publish/Slot/AbstractPersistenceAwareBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RecommendationBundle\Tests\eZ\Publish\Slot;

abstract class AbstractPersistenceAwareBaseTest extends AbstractSlotTest
{
/** @var \eZ\Publish\SPI\Persistence\Handler|\PHPUnit_Framework_MockObject_MockObject */
protected $persistenceHandler;

public function setUp()
{
$this->persistenceHandler = $this->getMock('\eZ\Publish\SPI\Persistence\Handler');

parent::setUp();
}

protected function createSlot()
{
$class = $this->getSlotClass();

return new $class($this->client, $this->persistenceHandler);
}
}
228 changes: 228 additions & 0 deletions Tests/eZ/Publish/Slot/AbstractSlotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RecommendationBundle\Tests\eZ\Publish\Slot;

use PHPUnit\Framework\TestCase;

abstract class AbstractSlotTest extends TestCase
{
/** @var \EzSystems\RecommendationBundle\Client\RecommendationClient|\PHPUnit_Framework_MockObject_MockObject */
protected $client;

/** @var \EzSystems\RecommendationBundle\eZ\Publish\Slot\Base */
protected $slot;

public function setUp()
{
$this->client = $this->getMock('\EzSystems\RecommendationBundle\Client\RecommendationClient');
$this->slot = $this->createSlot();
}

public function testReceiveSignal()
{
$this->client->expects($this->never())->method('updateContent');
$this->client->expects($this->never())->method('deleteContent');
$this->client->expects($this->never())->method('hideLocation');
$this->client->expects($this->never())->method('unhideLocation');

$this->slot->receive($this->createSignal());
}

/**
* @dataProvider getUnreceivedSignals
*/
public function testDoesNotReceiveOtherSignals($signal)
{
$this->client->expects($this->never())->method('updateContent');
$this->client->expects($this->never())->method('deleteContent');
$this->client->expects($this->never())->method('hideLocation');
$this->client->expects($this->never())->method('unhideLocation');

$this->slot->receive($signal);
}

public function getReceivedSignals()
{
return [
$this->createSignal(),
];
}

public function getUnreceivedSignals()
{
$arguments = [];

$signals = $this->getAllSignals();
foreach ($signals as $signalClass) {
if (in_array($signalClass, $this->getReceivedSignalClasses())) {
continue;
}
$arguments[] = [new $signalClass()];
}

return $arguments;
}

/**
* Asserts that recommendation service is notified about update of specified content objects.
*
* @param array $notifications
*/
protected function assertRecommendationServiceIsNotified(array $notifications)
{
$notifications = array_merge([
'deleteContent' => [],
'updateContent' => [],
'hideLocation' => [],
'unhideLocation' => [],
], $notifications);

foreach ($notifications as $action => $contentIds) {
if (empty($contentIds)) {
$this->client->expects($this->never())->method($action);
} else {
$this->client
->expects($this->exactly(count($contentIds)))
->method($action)
->willReturnCallback(function ($id) use ($contentIds) {
$this->assertContains($id, $contentIds);
});
}
}
}

/**
* Asserts that recommendation service is notified about delete of specified content objects.
*
* @param array $contentIds
*/
protected function assertContentIsDeleted(array $contentIds)
{
$this->client
->expects($this->exactly(count($contentIds)))
->method('deleteContent')
->willReturnCallback(function ($id) use ($contentIds) {
$this->assertContains($id, $contentIds);
});
}

/**
* Asserts that recommendation service is notified about update of specified content objects.
*
* @param array $contentIds
*/
protected function assertContentIsUpdated(array $contentIds)
{
$this->client
->expects($this->exactly(count($contentIds)))
->method('updateContent')
->willReturnCallback(function ($id) use ($contentIds) {
$this->assertContains($id, $contentIds);
});
}

protected function createSlot()
{
$class = $this->getSlotClass();

return new $class($this->client);
}

abstract protected function createSignal();

abstract protected function getSlotClass();

abstract protected function getReceivedSignalClasses();

/**
* @return array
*/
private function getAllSignals()
{
return [
'eZ\Publish\Core\SignalSlot\Signal\URLAliasService\CreateUrlAliasSignal',
'eZ\Publish\Core\SignalSlot\Signal\URLAliasService\RemoveAliasesSignal',
'eZ\Publish\Core\SignalSlot\Signal\URLAliasService\CreateGlobalUrlAliasSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CreateContentTypeSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\AddFieldDefinitionSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CopyContentTypeSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\DeleteContentTypeSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UpdateContentTypeGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\DeleteContentTypeGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UnassignContentTypeGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\PublishContentTypeDraftSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\AssignContentTypeGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UpdateFieldDefinitionSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UpdateContentTypeDraftSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\RemoveFieldDefinitionSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CreateContentTypeDraftSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CreateContentTypeGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\LanguageService\EnableLanguageSignal',
'eZ\Publish\Core\SignalSlot\Signal\LanguageService\UpdateLanguageNameSignal',
'eZ\Publish\Core\SignalSlot\Signal\LanguageService\CreateLanguageSignal',
'eZ\Publish\Core\SignalSlot\Signal\LanguageService\DisableLanguageSignal',
'eZ\Publish\Core\SignalSlot\Signal\LanguageService\DeleteLanguageSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\MoveUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\UnAssignUserFromUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\AssignUserToUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserSignal',
'eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserSignal',
'eZ\Publish\Core\SignalSlot\Signal\SectionService\DeleteSectionSignal',
'eZ\Publish\Core\SignalSlot\Signal\SectionService\CreateSectionSignal',
'eZ\Publish\Core\SignalSlot\Signal\SectionService\UpdateSectionSignal',
'eZ\Publish\Core\SignalSlot\Signal\SectionService\AssignSectionSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\AssignRoleToUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\UpdatePolicySignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\CreateRoleSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\RemovePolicySignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\UnassignRoleFromUserSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\AddPolicySignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\UnassignRoleFromUserGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\UpdateRoleSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\AssignRoleToUserSignal',
'eZ\Publish\Core\SignalSlot\Signal\RoleService\DeleteRoleSignal',
'eZ\Publish\Core\SignalSlot\Signal\TrashService\TrashSignal',
'eZ\Publish\Core\SignalSlot\Signal\TrashService\EmptyTrashSignal',
'eZ\Publish\Core\SignalSlot\Signal\TrashService\RecoverSignal',
'eZ\Publish\Core\SignalSlot\Signal\TrashService\DeleteTrashItemSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\DeleteObjectStateSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\CreateObjectStateSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\DeleteObjectStateGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\CreateObjectStateGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\UpdateObjectStateSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\UpdateObjectStateGroupSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\SetContentStateSignal',
'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\SetPriorityOfObjectStateSignal',
'eZ\Publish\Core\SignalSlot\Signal\URLWildcardService\TranslateSignal',
'eZ\Publish\Core\SignalSlot\Signal\URLWildcardService\RemoveSignal',
'eZ\Publish\Core\SignalSlot\Signal\URLWildcardService\CreateSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\UpdateContentSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\CreateContentDraftSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\AddRelationSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\CreateContentSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\DeleteContentSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\AddTranslationInfoSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\CopyContentSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\UpdateContentMetadataSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\TranslateVersionSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\DeleteRelationSignal',
'eZ\Publish\Core\SignalSlot\Signal\ContentService\DeleteVersionSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\UpdateLocationSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\HideLocationSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\SwapLocationSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\MoveSubtreeSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\UnhideLocationSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\CreateLocationSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\DeleteLocationSignal',
'eZ\Publish\Core\SignalSlot\Signal\LocationService\CopySubtreeSignal',
];
}
}
40 changes: 40 additions & 0 deletions Tests/eZ/Publish/Slot/CopyContentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RecommendationBundle\Tests\eZ\Publish\Slot;

use eZ\Publish\Core\SignalSlot\Signal\ContentService\CopyContentSignal;

class CopyContentTest extends AbstractSlotTest
{
const DST_CONTENT_ID = 100;

public function testReceiveSignal()
{
$this->assertRecommendationServiceIsNotified([
'updateContent' => [self::DST_CONTENT_ID],
]);

$this->slot->receive($this->createSignal());
}

protected function createSignal()
{
return new CopyContentSignal([
'dstContentId' => self::DST_CONTENT_ID,
]);
}

protected function getSlotClass()
{
return 'EzSystems\RecommendationBundle\eZ\Publish\Slot\CopyContent';
}

protected function getReceivedSignalClasses()
{
return ['eZ\Publish\Core\SignalSlot\Signal\ContentService\CopyContentSignal'];
}
}
57 changes: 57 additions & 0 deletions Tests/eZ/Publish/Slot/CopySubtreeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RecommendationBundle\Tests\eZ\Publish\Slot;

use eZ\Publish\Core\SignalSlot\Signal\LocationService\CopySubtreeSignal;

class CopySubtreeTest extends AbstractPersistenceAwareBaseTest
{
public function testReceiveSignal()
{
$signal = $this->createSignal();

$locationHandler = $this->getMock('\eZ\Publish\SPI\Persistence\Content\Location\Handler');
$locationHandler
->expects($this->once())
->method('loadSubtreeIds')
->with($signal->targetNewSubtreeId)
->willReturn($this->getTargetNewSubtreeId());

$this->persistenceHandler
->expects($this->any())
->method('locationHandler')
->willReturn($locationHandler);

$this->assertRecommendationServiceIsNotified([
'updateContent' => $this->getTargetNewSubtreeId(),
]);

$this->slot->receive($this->createSignal());
}

protected function createSignal()
{
return new CopySubtreeSignal([
'targetNewSubtreeId' => 100,
]);
}

protected function getSlotClass()
{
return '\EzSystems\RecommendationBundle\eZ\Publish\Slot\CopySubtree';
}

protected function getReceivedSignalClasses()
{
return ['eZ\Publish\Core\SignalSlot\Signal\LocationService\CopySubtreeSignal'];
}

private function getTargetNewSubtreeId()
{
return [2016, 2017, 2018];
}
}
Loading

0 comments on commit 5cbf22c

Please sign in to comment.