Skip to content

Commit

Permalink
Fix restore when parent page was deleted (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler authored Mar 4, 2024
1 parent b3a6098 commit b0dfa50
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Document/Subscriber/WebspaceSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Events;
use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand Down Expand Up @@ -103,10 +104,14 @@ public function saveProperties(AbstractMappingEvent $event): void
if ($parentPageUuid) {
// we know now it's a `page_tree_route` route
// so load the parent and find out the webspace
$parentDocument = $this->documentManager->find($parentPageUuid, $event->getLocale());
$mainWebspace = $this->documentInspector->getWebspace($parentDocument);
$document->setMainWebspace($mainWebspace);
$document->setAdditionalWebspaces([]);
try {
$parentDocument = $this->documentManager->find($parentPageUuid, $event->getLocale());
$mainWebspace = $this->documentInspector->getWebspace($parentDocument);
$document->setMainWebspace($mainWebspace);
$document->setAdditionalWebspaces([]);
} catch (DocumentNotFoundException $exception) {
// Do nothing, because when the parent page was deleted it should still work while restoring.
}
}

$mainWebspace = $document->getMainWebspace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>
<template xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd">

<key>default_with_page_tree_route</key>

<view>::default_with_page_tree_route</view>
<controller>SuluWebsiteBundle:Default:index</controller>
<cacheLifetime>2400</cacheLifetime>

<tag name="sulu_article.type" type="blog"/>

<properties>
<property name="title" type="text_line" mandatory="true"/>

<property name="routePath" type="page_tree_route">
<tag name="sulu_article.article_route"/>
</property>

<property name="article" type="text_area"/>
</properties>
</template>
104 changes: 104 additions & 0 deletions Tests/Functional/Trash/ArticleTrashItemHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\ArticleBundle\Controller\ArticleController;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Trash\ArticleTrashItemHandler;
use Sulu\Bundle\PageBundle\Document\PageDocument;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Sulu\Bundle\TrashBundle\SuluTrashBundle;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
Expand Down Expand Up @@ -210,4 +211,107 @@ public function testStoreAndRestoreShadowArticle(): void
static::assertSame('en', $restoredArticleEn->getOriginalLocale());
static::assertSame('de', $restoredArticleEn->getShadowLocale());
}

public function testStoreAndRestorePageTreeRoute(): void
{
/** @var PageDocument $page */
$page = $this->documentManager->create('page');
$page->setTitle('test parent page');
$page->setStructureType('default');
$page->setResourceSegment('/test-parent-page');

$this->documentManager->persist($page, 'de', ['parent_path' => '/cmf/sulu_io/contents']);
$this->documentManager->publish($page, 'de');
$this->documentManager->flush();

/** @var ArticleDocument $article1De */
$article1De = $this->documentManager->create(ArticleController::DOCUMENT_TYPE);
$article1De->setParent($this->documentManager->find('/cmf/sulu_io/contents', 'de'));
$article1De->setTitle('test-title-de');
$article1De->setLocale('de');
$article1De->setStructureType('default_with_page_tree_route');
$article1De->setRoutePath($page->getStructure()->getProperty('url')->getValue() . '/test-title-de');
$article1De->getStructure()->bind([
'routePath' => [
'page' => [
'uuid' => $page->getUuid(),
'path' => $page->getStructure()->getProperty('url')->getValue(),
],
'path' => $page->getStructure()->getProperty('url')->getValue() . '/test-title-de',
'suffix' => '/test-title-de',
],
]);
$article1De->setMainWebspace('sulu_io');
$this->documentManager->persist($article1De, 'de');

$this->documentManager->flush();
$originalArticleUuid = $article1De->getUuid();

$trashItem = $this->articleTrashItemHandler->store($article1De);
$this->documentManager->remove($article1De);
$this->documentManager->flush();
$this->documentManager->clear();

static::assertSame($originalArticleUuid, $trashItem->getResourceId());
static::assertSame('test-title-de', $trashItem->getResourceTitle());

/** @var ArticleDocument $restoredArticle */
$restoredArticle = $this->articleTrashItemHandler->restore($trashItem);
static::assertSame($originalArticleUuid, $restoredArticle->getUuid());

/** @var ArticleDocument $restoredArticleDe */
$restoredArticleDe = $this->documentManager->find($originalArticleUuid, 'de');
static::assertSame($originalArticleUuid, $restoredArticleDe->getUuid());
static::assertSame('test-title-de', $restoredArticleDe->getTitle());
static::assertSame('/test-parent-page/test-title-de', $restoredArticleDe->getRoutePath());
static::assertSame('de', $restoredArticleDe->getLocale());
static::assertSame('default_with_page_tree_route', $restoredArticleDe->getStructureType());
static::assertSame('sulu_io', $restoredArticleDe->getMainWebspace());
}

public function testStoreAndRestorePageTreeRouteNoValidParent(): void
{
/** @var ArticleDocument $article1De */
$article1De = $this->documentManager->create(ArticleController::DOCUMENT_TYPE);
$article1De->setParent($this->documentManager->find('/cmf/sulu_io/contents', 'de'));
$article1De->setTitle('test-title-de');
$article1De->setLocale('de');
$article1De->setStructureType('default_with_page_tree_route');
$article1De->getStructure()->bind([
'routePath' => [
'page' => [
'uuid' => 'not-existing-uuid',
'path' => '/not-existing-path',
],
'path' => '/not-existing-path/test-title-de',
'suffix' => '/test-title-de',
],
]);
$article1De->setMainWebspace('sulu_io');
$this->documentManager->persist($article1De, 'de');

$this->documentManager->flush();
$originalArticleUuid = $article1De->getUuid();

$trashItem = $this->articleTrashItemHandler->store($article1De);
$this->documentManager->remove($article1De);
$this->documentManager->flush();
$this->documentManager->clear();

static::assertSame($originalArticleUuid, $trashItem->getResourceId());
static::assertSame('test-title-de', $trashItem->getResourceTitle());

/** @var ArticleDocument $restoredArticle */
$restoredArticle = $this->articleTrashItemHandler->restore($trashItem);
static::assertSame($originalArticleUuid, $restoredArticle->getUuid());

/** @var ArticleDocument $restoredArticleDe */
$restoredArticleDe = $this->documentManager->find($originalArticleUuid, 'de');
static::assertSame($originalArticleUuid, $restoredArticleDe->getUuid());
static::assertSame('test-title-de', $restoredArticleDe->getTitle());
static::assertSame('/not-existing-path/test-title-de', $restoredArticleDe->getRoutePath());
static::assertSame('de', $restoredArticleDe->getLocale());
static::assertSame('default_with_page_tree_route', $restoredArticleDe->getStructureType());
static::assertSame('sulu_io', $restoredArticleDe->getMainWebspace());
}
}

0 comments on commit b0dfa50

Please sign in to comment.