Skip to content

Commit

Permalink
fixed draft state of article when sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jun 6, 2017
1 parent 52d5d81 commit 9bfc331
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Document/Serializer/ArticleWebsiteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
use Sulu\Component\Content\Compat\StructureInterface;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\ContentTypeManagerInterface;
use Sulu\Component\Util\SortUtils;

/**
* Extends serializer with addtional functionallity to prepare article(-page) data.
* Extends serializer with additional functionality to prepare article(-page) data.
*/
class ArticleWebsiteSubscriber implements EventSubscriberInterface
{
Expand Down Expand Up @@ -155,6 +156,8 @@ public function resolveContentForArticleOnPostSerialize(ObjectEvent $event)

if (null !== $children && $context->attributes->containsKey('pageNumber')) {
$pages = array_values(is_array($children) ? $children : iterator_to_array($children));
$pages = SortUtils::multisort($pages, 'pageNumber');

$pageNumber = $context->attributes->get('pageNumber')->get();
if ($pageNumber !== 1) {
$article = $pages[$pageNumber - 2];
Expand Down
9 changes: 9 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Document\WorkflowStage;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\CopyEvent;
Expand Down Expand Up @@ -255,6 +256,14 @@ public function persistPageDataOnReorder(ReorderEvent $event)
$node = $this->documentInspector->getNode($document);

$this->setPageData($document, $node, $document->getLocale());

$document->setWorkflowStage(WorkflowStage::TEST);
$this->documentManager->persist($document, $this->documentInspector->getLocale($document));

$this->documents[$document->getUuid()] = [
'uuid' => $document->getUuid(),
'locale' => $document->getLocale(),
];
}

/**
Expand Down
62 changes: 60 additions & 2 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
use Sulu\Component\Content\Exception\ResourceLocatorAlreadyExistsException;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\DocumentManager\Behavior\Mapping\ChildrenBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\CopyEvent;
use Sulu\Component\DocumentManager\Event\PublishEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\ReorderEvent;
use Sulu\Component\DocumentManager\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -138,6 +140,7 @@ public static function getSubscribedEvents()
['handleRemove', 1024],
],
Events::PUBLISH => ['handlePublish', -2000],
Events::REORDER => ['handleReorder', -1000],
Events::COPY => ['handleCopy', -2000],
];
}
Expand Down Expand Up @@ -187,6 +190,35 @@ public function handlePersist(AbstractMappingEvent $event)
$event->getNode()->setProperty($propertyName, $route->getPath());
}

/**
* Regenerate routes for siblings on reorder.
*
* @param ReorderEvent $event
*/
public function handleReorder(ReorderEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof RoutablePageBehavior || !$document instanceof ParentBehavior) {
return;
}

$parentDocument = $document->getParent();
if (!$parentDocument instanceof ChildrenBehavior) {
return;
}

$locale = $this->documentInspector->getLocale($parentDocument);
$propertyName = $this->getRoutePathPropertyName($parentDocument->getStructureType(), $locale);
foreach ($parentDocument->getChildren() as $childDocument) {
$node = $this->documentInspector->getNode($childDocument);

$route = $this->chainRouteGenerator->generate($childDocument);
$childDocument->setRoutePath($route->getPath());

$node->setProperty($propertyName, $route->getPath());
}
}

/**
* Handle publish event and generate route and the child-routes.
*
Expand Down Expand Up @@ -244,21 +276,47 @@ public function handlePublish(PublishEvent $event)
*/
private function createOrUpdatePageRoute(RoutablePageBehavior $document, $locale)
{
$route = $document->getRoute();
$route = $this->reallocateExistingRoute($document, $locale);
if ($route) {
return $route;
}

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

if ($route) {
$document->setRoute($route);

return $this->routeManager->update($document);
return $this->routeManager->update($document, null, false);
}

return $this->routeManager->create($document);
}

/**
* Reallocates existing route to given document.
*
* @param RoutablePageBehavior $document
* @param string $locale
*
* @return RouteInterface
*/
private function reallocateExistingRoute(RoutablePageBehavior $document, $locale)
{
$route = $this->routeRepository->findByPath($document->getRoutePath(), $locale);
if (!$route) {
return;
}

$route->setEntityClass(get_class($document));
$route->setEntityId($document->getId());
$route->setHistory($document->getId());

return $route;
}

/**
* Create or update for given document.
*
Expand Down
6 changes: 6 additions & 0 deletions Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Document\WorkflowStage;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\FlushEvent;
Expand Down Expand Up @@ -493,6 +494,7 @@ public function testPersistPageDataOnReorder()
$this->document->getChildren()->willReturn(new \ArrayIterator([$child->reveal()]));

$this->documentInspector->getLocalizationState($child->reveal())->willReturn(LocalizationState::LOCALIZED);
$this->documentInspector->getLocale($this->document->reveal())->willReturn('de');

$propertyName = 'i18n:' . $this->locale . '-' . ArticleSubscriber::PAGES_PROPERTY;
$this->propertyEncoder->localizedSystemName(ArticleSubscriber::PAGES_PROPERTY, $this->locale)->willReturn(
Expand All @@ -501,6 +503,10 @@ public function testPersistPageDataOnReorder()

$this->document->setPages($pages)->shouldBeCalled();
$node->setProperty($propertyName, json_encode($pages))->shouldBeCalled();
$this->document->setWorkflowStage(WorkflowStage::TEST)->shouldBeCalled();
$this->document->setWorkflowStage(WorkflowStage::TEST);

$this->documentManager->persist($this->document->reveal(), 'de')->shouldBeCalled();

$this->articleSubscriber->persistPageDataOnReorder($event->reveal());
}
Expand Down
64 changes: 64 additions & 0 deletions Tests/Unit/Document/Subscriber/RoutableSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Prophecy\Argument;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutableBehavior;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutablePageBehavior;
use Sulu\Bundle\ArticleBundle\Document\Subscriber\RoutableSubscriber;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
Expand All @@ -25,15 +26,18 @@
use Sulu\Bundle\RouteBundle\Manager\ConflictResolverInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\StructureBehavior;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\PropertyMetadata;
use Sulu\Component\Content\Metadata\StructureMetadata;
use Sulu\Component\DocumentManager\Behavior\Mapping\ChildrenBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\CopyEvent;
use Sulu\Component\DocumentManager\Event\HydrateEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\ReorderEvent;

class RoutableSubscriberTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -372,4 +376,64 @@ public function testHandleCopy()

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

public function testHandleReorder()
{
$this->document->willImplement(ParentBehavior::class);

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

$parentDocument = $this->prophesize(ChildrenBehavior::class);
$parentDocument->willImplement(StructureBehavior::class);
$this->document->getParent()->willReturn($parentDocument->reveal());
$this->documentInspector->getLocale($parentDocument)->willReturn('de');

$parentDocument->getStructureType()->willReturn('default');

$metadata = $this->prophesize(StructureMetadata::class);
$metadata->hasTag(Argument::any())->willReturn(false);

$this->metadataFactory->getStructureMetadata('article', 'default')->willReturn($metadata->reveal());
$this->propertyEncoder->localizedSystemName('routePath', 'de')->willReturn('i18n:de-routePath');

$children = [
$this->prophesize(RoutablePageBehavior::class),
$this->prophesize(RoutablePageBehavior::class),
$this->prophesize(RoutablePageBehavior::class),
];
$parentDocument->getChildren()
->willReturn([$children[0]->reveal(), $children[1]->reveal(), $children[2]->reveal()]);

$nodes = [
$this->prophesize(NodeInterface::class),
$this->prophesize(NodeInterface::class),
$this->prophesize(NodeInterface::class),
];
$this->documentInspector->getNode($children[0]->reveal())->willReturn($nodes[0]->reveal());
$this->documentInspector->getNode($children[1]->reveal())->willReturn($nodes[1]->reveal());
$this->documentInspector->getNode($children[2]->reveal())->willReturn($nodes[2]->reveal());

$routes = [
$this->prophesize(RouteInterface::class),
$this->prophesize(RouteInterface::class),
$this->prophesize(RouteInterface::class),
];
$routes[0]->getPath()->willReturn('/test-1');
$routes[1]->getPath()->willReturn('/test-2');
$routes[2]->getPath()->willReturn('/test-3');
$this->chainGenerator->generate($children[0]->reveal())->willReturn($routes[0]->reveal());
$this->chainGenerator->generate($children[1]->reveal())->willReturn($routes[1]->reveal());
$this->chainGenerator->generate($children[2]->reveal())->willReturn($routes[2]->reveal());

$children[0]->setRoutePath('/test-1')->shouldBeCalled();
$children[1]->setRoutePath('/test-2')->shouldBeCalled();
$children[2]->setRoutePath('/test-3')->shouldBeCalled();

$nodes[0]->setProperty('i18n:de-routePath', '/test-1')->shouldBeCalled();
$nodes[1]->setProperty('i18n:de-routePath', '/test-2')->shouldBeCalled();
$nodes[2]->setProperty('i18n:de-routePath', '/test-3')->shouldBeCalled();

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

0 comments on commit 9bfc331

Please sign in to comment.