Skip to content

Commit

Permalink
fixed move page-tree for article-pages (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored and alexander-schranz committed Jul 4, 2017
1 parent 26959ca commit 4422876
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
42 changes: 35 additions & 7 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ public function handlePersist(AbstractMappingEvent $event)
$document->setRoutePath($route->getPath());

$event->getNode()->setProperty($propertyName, $route->getPath());

if (!$document instanceof ChildrenBehavior) {
return;
}

foreach ($document->getChildren() as $child) {
if (!$child instanceof RoutablePageBehavior) {
continue;
}

$route = $this->conflictResolver->resolve($this->chainRouteGenerator->generate($child));
$child->setRoutePath($route->getPath());

$node = $this->documentInspector->getNode($child);
$node->setProperty($propertyName, $route->getPath());
}
}

/**
Expand Down Expand Up @@ -305,17 +321,29 @@ private function createOrUpdatePageRoute(RoutablePageBehavior $document, $locale
*/
private function reallocateExistingRoute(RoutablePageBehavior $document, $locale)
{
$route = $this->routeRepository->findByPath($document->getRoutePath(), $locale);
if (!$route) {
$newRoute = $this->routeRepository->findByPath($document->getRoutePath(), $locale);
if (!$newRoute) {
return;
}

$route->setEntityClass(get_class($document));
$route->setEntityId($document->getId());
$route->setTarget(null);
$route->setHistory(false);
$oldRoute = $this->routeRepository->findByEntity(get_class($document), $document->getUuid(), $locale);
$history = $this->routeRepository->findHistoryByEntity(get_class($document), $document->getUuid(), $locale);
foreach (array_merge($history, [$oldRoute]) as $historyRoute) {
if ($historyRoute->getId() === $newRoute->getId()) {
continue;
}

$historyRoute->setTarget($newRoute);
$historyRoute->setHistory(true);
$newRoute->addHistory($historyRoute);
}

$newRoute->setEntityClass(get_class($document));
$newRoute->setEntityId($document->getId());
$newRoute->setTarget(null);
$newRoute->setHistory(false);

return $route;
return $newRoute;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion PageTree/PageTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ private function updateArticle(ArticleDocument $article, BasePageDocument $docum
$article->setRoutePath($path);
}

if (WorkflowStage::PUBLISHED === $article->getWorkflowStage()) {
$workflowStage = $article->getWorkflowStage();

$this->documentManager->persist($article, $locale);
if (WorkflowStage::PUBLISHED === $workflowStage) {
$this->documentManager->publish($article, $locale);
}
}
Expand Down
61 changes: 61 additions & 0 deletions Tests/Functional/PageTree/PageTreeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ public function testMove()
$this->assertEquals('/test-2/article-3', $result[2]->getRoutePath());
}

public function testMoveArticlePage()
{
$pages = [
$this->createPage('Test 1', '/test-1'),
$this->createPage('Test 2', '/test-2'),
];

$article = $this->createArticle($this->getPathData($pages[0], 'article-1'));
$articlePages = [
$this->createArticlePage($article, 'Test page 1'),
$this->createArticlePage($article, 'Test page 2'),
$this->createArticlePage($article, 'Test page 3'),
];

$this->documentManager->flush();
$this->documentManager->clear();

$this->pageTreeRepository->move('/test-1', $pages[1]);
$this->documentManager->flush();
$this->documentManager->clear();

$result = $this->documentManager->find($article['id'], $this->locale);
$this->assertEquals('/test-2/article-1', $result->getRoutePath());

$children = array_values(iterator_to_array($result->getChildren()));

$this->assertEquals('/test-2/article-1/page-2', $children[0]->getRoutePath());
$this->assertEquals('/test-2/article-1/page-3', $children[1]->getRoutePath());
$this->assertEquals('/test-2/article-1/page-4', $children[2]->getRoutePath());
}

/**
* Returns property value for given page.
*
Expand Down Expand Up @@ -165,6 +196,36 @@ private function createArticle(array $routePathData, $title = 'Test Article')
return json_decode($client->getResponse()->getContent(), true);
}

/**
* Create article page.
*
* @param array $article
* @param string $pageTitle
* @param string $template
* @param string $locale
*
* @return array
*/
private function createArticlePage(
array $article,
$pageTitle = 'Test-Page',
$template = 'default_pages',
$locale = 'de'
) {
$client = $this->createAuthenticatedClient();
$client->request(
'POST',
'/api/articles/' . $article['id'] . '/pages?locale=' . $locale,
[
'pageTitle' => $pageTitle,
'template' => $template,
]
);
$this->assertHttpStatusCode(200, $client->getResponse());

return json_decode($client->getResponse()->getContent(), true);
}

/**
* Create a new page.
*
Expand Down

0 comments on commit 4422876

Please sign in to comment.