Skip to content

Commit

Permalink
Fixed metadata for article to sync remove (sulu#155)
Browse files Browse the repository at this point in the history
* fixed metadata for article

* fixed routable-subscriber to remove all routes (locales)
  • Loading branch information
wachterjohannes committed Apr 25, 2017
1 parent da4246d commit 0bedd55
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Document/Subscriber/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function setStructureTypeToParent(PersistEvent $event)
*/
public function handleMetadataLoad(MetadataLoadEvent $event)
{
if (!$event->getMetadata()->getReflectionClass()->implementsInterface(ArticleInterface::class)) {
if ($event->getMetadata()->getClass() !== ArticlePageDocument::class) {
return;
}

Expand Down
22 changes: 22 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sulu\Component\DocumentManager\Event\CopyEvent;
use Sulu\Component\DocumentManager\Event\FlushEvent;
use Sulu\Component\DocumentManager\Event\HydrateEvent;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\PublishEvent;
use Sulu\Component\DocumentManager\Event\RemoveDraftEvent;
Expand Down Expand Up @@ -126,6 +127,7 @@ public static function getSubscribedEvents()
Events::REMOVE_DRAFT => [['handleScheduleIndex', -1024], ['removeDraftChildren', 0]],
Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
Events::COPY => ['handleCopy'],
Events::METADATA_LOAD => ['handleMetadataLoad'],
];
}

Expand Down Expand Up @@ -491,4 +493,24 @@ public function setChildrenStructureType(PersistEvent $event)
}
}
}

/**
* Extend metadata for article-page.
*
* @param MetadataLoadEvent $event
*/
public function handleMetadataLoad(MetadataLoadEvent $event)
{
if ($event->getMetadata()->getClass() !== ArticleDocument::class) {
return;
}

$event->getMetadata()->addFieldMapping(
'pageTitle',
[
'encoding' => 'system_localized',
'property' => 'suluPageTitle',
]
);
}
}
23 changes: 16 additions & 7 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,24 @@ public function handleRemove(RemoveEvent $event)
return;
}

$route = $this->routeRepository->findByPath($document->getRoutePath(), $document->getOriginalLocale());
if (!$route) {
return;
}
$locales = $this->documentInspector->getLocales($document);
foreach ($locales as $locale) {
$localizedDocument = $this->documentManager->find($document->getUuid(), $locale);

$route = $this->routeRepository->findByEntity(
$localizedDocument->getClass(),
$localizedDocument->getUuid(),
$locale
);
if (!$route) {
continue;
}

$this->entityManager->remove($route);
$this->entityManager->remove($route);

if ($document instanceof ChildrenBehavior) {
$this->removeChildRoutes($document);
if ($document instanceof ChildrenBehavior) {
$this->removeChildRoutes($document);
}
}

$this->entityManager->flush();
Expand Down
84 changes: 52 additions & 32 deletions Tests/Unit/Document/Subscriber/RoutableSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,64 @@ public function testHandlePersistUpdate()

public function testHandleRemove()
{
$this->documentInspector->getLocales($this->document->reveal())->willReturn(['de', 'en']);

$event = $this->prophesize(RemoveEvent::class);
$event->getDocument()->willReturn($this->document->reveal());

$route = $this->prophesize(RouteInterface::class);
$this->document->getUuid()->willReturn('123-123-123');
$this->document->getRoutePath()->willReturn('/test');
$this->document->getClass()->willReturn(ArticleDocument::class);
$this->document->getOriginalLocale()->willReturn('de');
$this->routeRepository->findByPath('/test', 'de')->willReturn($route->reveal());

$this->entityManager->remove($route->reveal())->shouldBeCalled();
$this->documentManager->find('123-123-123', 'de')->willReturn($this->document->reveal());
$this->documentManager->find('123-123-123', 'en')->willReturn($this->document->reveal());

$routeDE = $this->prophesize(RouteInterface::class);
$routeEN = $this->prophesize(RouteInterface::class);
$this->routeRepository->findByEntity(ArticleDocument::class, '123-123-123', 'de')
->willReturn($routeDE->reveal());
$this->routeRepository->findByEntity(ArticleDocument::class, '123-123-123', 'en')
->willReturn($routeEN->reveal());

$this->entityManager->remove($routeDE->reveal())->shouldBeCalled();
$this->entityManager->remove($routeEN->reveal())->shouldBeCalled();
$this->entityManager->flush()->shouldBeCalled();

$this->routableSubscriber->handleRemove($event->reveal());
}

public function testHandleRemoveWithChildren()
{
$this->document->willImplement(ChildrenBehavior::class);

$this->documentInspector->getLocales($this->document->reveal())->willReturn(['de']);

$event = $this->prophesize(RemoveEvent::class);
$event->getDocument()->willReturn($this->document->reveal());

$this->document->getUuid()->willReturn('123-123-123');
$this->document->getRoutePath()->willReturn('/test');
$this->document->getOriginalLocale()->willReturn('de');
$this->document->getClass()->willReturn(ArticleDocument::class);
$route1 = $this->prophesize(RouteInterface::class);
$this->routeRepository->findByEntity(ArticleDocument::class, '123-123-123', 'de')
->willReturn($route1->reveal());

$this->documentManager->find('123-123-123', 'de')->willReturn($this->document->reveal());

$child = $this->prophesize(RoutableBehavior::class);
$child->willImplement(ChildrenBehavior::class);
$child->getChildren()->willReturn([]);
$this->document->getChildren()->willReturn([$child->reveal()]);

$child->getRoutePath()->willReturn('/test/test-2');
$child->getOriginalLocale()->willReturn('de');
$route2 = $this->prophesize(RouteInterface::class);
$this->routeRepository->findByPath('/test/test-2', 'de')->willReturn($route2->reveal());

$this->entityManager->remove($route1->reveal())->shouldBeCalled();
$this->entityManager->remove($route2->reveal())->shouldBeCalled();
$this->entityManager->flush()->shouldBeCalled();

$this->routableSubscriber->handleRemove($event->reveal());
Expand Down Expand Up @@ -323,33 +372,4 @@ public function testHandleCopy()

$this->routableSubscriber->handleCopy($event->reveal());
}

public function testHandleRemoveWithChildren()
{
$this->document->willImplement(ChildrenBehavior::class);

$event = $this->prophesize(RemoveEvent::class);
$event->getDocument()->willReturn($this->document->reveal());

$this->document->getRoutePath()->willReturn('/test');
$this->document->getOriginalLocale()->willReturn('de');
$route1 = $this->prophesize(RouteInterface::class);
$this->routeRepository->findByPath('/test', 'de')->willReturn($route1->reveal());

$child = $this->prophesize(RoutableBehavior::class);
$child->willImplement(ChildrenBehavior::class);
$child->getChildren()->willReturn([]);
$this->document->getChildren()->willReturn([$child->reveal()]);

$child->getRoutePath()->willReturn('/test/test-2');
$child->getOriginalLocale()->willReturn('de');
$route2 = $this->prophesize(RouteInterface::class);
$this->routeRepository->findByPath('/test/test-2', 'de')->willReturn($route2->reveal());

$this->entityManager->remove($route1->reveal())->shouldBeCalled();
$this->entityManager->remove($route2->reveal())->shouldBeCalled();
$this->entityManager->flush()->shouldBeCalled();

$this->routableSubscriber->handleRemove($event->reveal());
}
}

0 comments on commit 0bedd55

Please sign in to comment.