Skip to content

Commit

Permalink
fixed serialization context of article and article-page
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Mar 28, 2017
1 parent 767004f commit 96311f6
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 37 deletions.
18 changes: 14 additions & 4 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public function getAction($uuid, Request $request)

return $this->handleView(
$this->view($document)->setSerializationContext(
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage'])
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage'])
)
);
}
Expand All @@ -224,7 +226,9 @@ public function postAction(Request $request)

return $this->handleView(
$this->view($document)->setSerializationContext(
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage'])
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage'])
)
);
}
Expand Down Expand Up @@ -260,7 +264,9 @@ public function putAction(Request $request, $uuid)

return $this->handleView(
$this->view($document)->setSerializationContext(
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage'])
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage'])
)
);
}
Expand Down Expand Up @@ -348,7 +354,11 @@ public function postTriggerAction($uuid, Request $request)

// prepare view
$view = $this->view($data, $data !== null ? 200 : 204);
$view->setSerializationContext(SerializationContext::create()->setGroups(['defaultPage']));
$view->setSerializationContext(
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage'])
);
} catch (RestException $exc) {
$view = $this->view($exc->toArray(), 400);
}
Expand Down
23 changes: 18 additions & 5 deletions Controller/ArticlePageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Document\Form\ArticlePageDocumentType;
use Sulu\Bundle\ArticleBundle\Exception\ArticlePageNotFoundException;
use Sulu\Bundle\ArticleBundle\Exception\ParameterNotAllowedException;
use Sulu\Component\Content\Form\Exception\InvalidFormException;
use Sulu\Component\Content\Mapper\ContentMapperInterface;
Expand Down Expand Up @@ -44,6 +45,8 @@ class ArticlePageController extends RestController implements ClassResourceInter
* @param Request $request
*
* @return Response
*
* @throws ArticlePageNotFoundException
*/
public function getAction($articleUuid, $uuid, Request $request)
{
Expand All @@ -57,9 +60,15 @@ public function getAction($articleUuid, $uuid, Request $request)
]
);

if ($articleUuid !== $document->getParent()->getUuid()) {
throw new ArticlePageNotFoundException($uuid, $articleUuid);
}

return $this->handleView(
$this->view($document)->setSerializationContext(
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage'])
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticlePage', 'smallArticle'])
)
);
}
Expand All @@ -80,12 +89,14 @@ public function postAction($articleUuid, Request $request)
$data = $request->request->all();

$this->persistDocument($data, $document, $locale, $articleUuid);
$this->handleActionParameter($action, $document, $locale);
$this->handleActionParameter($action, $document->getParent(), $locale);
$this->getDocumentManager()->flush();

return $this->handleView(
$this->view($document)->setSerializationContext(
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage'])
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticlePage', 'smallArticle'])
)
);
}
Expand Down Expand Up @@ -117,12 +128,14 @@ public function putAction($articleUuid, $uuid, Request $request)
$this->get('sulu_hash.request_hash_checker')->checkHash($request, $document, $document->getUuid());

$this->persistDocument($data, $document, $locale, $articleUuid);
$this->handleActionParameter($action, $document, $locale);
$this->handleActionParameter($action, $document->getParent(), $locale);
$this->getDocumentManager()->flush();

return $this->handleView(
$this->view($document)->setSerializationContext(
SerializationContext::create()->setSerializeNull(true)->setGroups(['defaultPage'])
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticlePage', 'smallArticle'])
)
);
}
Expand Down
6 changes: 5 additions & 1 deletion Controller/VersionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public function postTriggerAction(Request $request, $uuid, $version)

$data = $this->getDocumentManager()->find($uuid, $locale);
$view = $this->view($data, $data !== null ? Response::HTTP_OK : Response::HTTP_NO_CONTENT);
$view->setSerializationContext(SerializationContext::create()->setGroups(['defaultPage']));
$view->setSerializationContext(
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['defaultPage', 'defaultArticle', 'smallArticlePage'])
);

break;
default:
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Document\Structure\ArticleBridge;
use Sulu\Bundle\ArticleBundle\Document\Structure\ArticlePageBridge;
use Sulu\Bundle\ArticleBundle\Exception\ArticlePageNotFoundException;
use Sulu\Bundle\ArticleBundle\Exception\ParameterNotAllowedException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -100,6 +101,7 @@ public function prepend(ContainerBuilder $container)
'exception' => [
'codes' => [
ParameterNotAllowedException::class => 400,
ArticlePageNotFoundException::class => 404,
],
],
]
Expand Down
59 changes: 59 additions & 0 deletions Exception/ArticlePageNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Exception;

/**
* Thrown when article-page not found in article.
*/
class ArticlePageNotFoundException extends \Exception
{
/**
* @var string
*/
private $pageUuid;

/**
* @var string
*/
private $articleUuid;

/**
* @param string $pageUuid
* @param string $articleUuid
*/
public function __construct($pageUuid, $articleUuid)
{
parent::__construct(sprintf('Page "%s" not found in article "%s".', $pageUuid, $articleUuid));
$this->pageUuid = $pageUuid;
$this->articleUuid = $articleUuid;
}

/**
* Returns page-uuid.
*
* @return string
*/
public function getPageUuid()
{
return $this->pageUuid;
}

/**
* Returns article-uuid.
*
* @return string
*/
public function getArticleUuid()
{
return $this->articleUuid;
}
}
34 changes: 19 additions & 15 deletions Resources/config/serializer/Document.ArticleDocument.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,35 @@
<h:parameter name="uuid" value="expr(object.getUuid())"/>
<h:parameter name="locale" value="expr(object.getLocale())"/>
</h:href>
<h:exclusion groups="defaultPage"/>
<h:exclusion groups="defaultArticle,smallArticle"/>
</h:relation>

<property name="uuid" serialized-name="id" type="string" groups="defaultPage,smallPage,preview"/>
<h:relation rel="pages">
<h:embedded>
<h:content>expr(object.getChildren())</h:content>
</h:embedded>
<h:exclusion groups="defaultArticle"/>
</h:relation>

<property name="uuid" serialized-name="id" type="string" groups="defaultArticle,smallArticle,preview"/>
<property name="nodeName" type="string"/>
<property name="path" type="string"/>
<property name="routePath" serialized-name="route" type="string" groups="website,defaultPage,smallPage,preview"/>
<property name="routePath" serialized-name="route" type="string" groups="website,defaultArticle,smallArticle,preview"/>

<property name="locale" type="string" groups="preview"/>
<property name="originalLocale" type="string" groups="preview"/>
<property name="structure" type="Sulu\Component\Content\Document\Structure\Structure" groups="preview"/>
<property name="structureType" type="string" groups="preview"/>

<property name="title" type="string" groups="defaultPage,smallPage,preview"/>
<property name="creator" type="integer" groups="website,defaultPage,preview"/>
<property name="changer" type="integer" groups="website,defaultPage,preview"/>
<property name="created" type="DateTime" groups="website,defaultPage,preview"/>
<property name="changed" type="DateTime" groups="website,defaultPage,preview"/>
<property name="title" type="string" groups="defaultArticle,smallArticle,preview"/>
<property name="creator" type="integer" groups="website,defaultArticle,preview"/>
<property name="changer" type="integer" groups="website,defaultArticle,preview"/>
<property name="created" type="DateTime" groups="website,defaultArticle,preview"/>
<property name="changed" type="DateTime" groups="website,defaultArticle,preview"/>

<property name="published" type="DateTime" groups="website,defaultPage,smallPage,preview"/>
<property name="author" type="integer" groups="website,defaultPage,preview"/>
<property name="authored" type="DateTime" groups="website,defaultPage,smallPage,preview"/>
<property name="extensions" type="Sulu\Component\Content\Document\Extension\ExtensionContainer" serialized-name="ext" groups="defaultPage,preview"/>
<property name="children" serialized-name="pages" groups="defaultPage,preview">
<type><![CDATA[array<Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument>]]></type>
</property>
<property name="published" type="DateTime" groups="website,defaultArticle,smallArticle,preview"/>
<property name="author" type="integer" groups="website,defaultArticle,preview"/>
<property name="authored" type="DateTime" groups="website,defaultArticle,smallArticle,preview"/>
<property name="extensions" type="Sulu\Component\Content\Document\Extension\ExtensionContainer" serialized-name="ext" groups="defaultArticle,preview"/>
</class>
</serializer>
24 changes: 15 additions & 9 deletions Resources/config/serializer/Document.ArticlePageDocument.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
<h:parameter name="articleUuid" value="expr(object.getParent().getUuid())"/>
<h:parameter name="locale" value="expr(object.getLocale())"/>
</h:href>
<h:exclusion groups="defaultPage"/>
<h:exclusion groups="defaultArticlePage,smallArticlePage"/>
</h:relation>

<property name="uuid" serialized-name="id" type="string" groups="defaultPage,smallPage,preview"/>
<h:relation rel="article">
<h:embedded>
<h:content>expr(object.getParent())</h:content>
</h:embedded>
<h:exclusion groups="defaultArticlePage"/>
</h:relation>

<property name="uuid" serialized-name="id" type="string" groups="defaultArticlePage,smallArticlePage,preview"/>
<property name="nodeName" type="string"/>
<property name="path" type="string"/>

Expand All @@ -20,12 +27,11 @@
<property name="structureType" type="string" groups="preview"/>

<property name="title" serialized-name="articleTitle" type="string" groups="preview"/>
<property name="pageNumber" type="integer" groups="website,defaultPage,preview"/>
<property name="creator" type="integer" groups="website,defaultPage,preview"/>
<property name="changer" type="integer" groups="website,defaultPage,preview"/>
<property name="created" type="DateTime" groups="website,defaultPage,preview"/>
<property name="changed" type="DateTime" groups="website,defaultPage,preview"/>

<property name="published" type="DateTime" groups="website,defaultPage,smallPage,preview"/>
<property name="pageNumber" type="integer" groups="website,defaultArticlePage,smallArticlePage,preview"/>
<property name="creator" type="integer" groups="website,defaultArticlePage,smallArticlePage,preview"/>
<property name="changer" type="integer" groups="website,defaultArticlePage,smallArticlePage,preview"/>
<property name="created" type="DateTime" groups="website,defaultArticlePage,smallArticlePage,preview"/>
<property name="changed" type="DateTime" groups="website,defaultArticlePage,smallArticlePage,preview"/>
<property name="published" type="DateTime" groups="website,defaultArticlePage,smallArticlePage,preview"/>
</class>
</serializer>
8 changes: 5 additions & 3 deletions Tests/Functional/Controller/ArticlePageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ public function testPost($title = 'Test-Article', $pageTitle = 'Test-Page', $tem
$this->assertEquals($template, $response['template']);
$this->assertEquals(2, $response['pageNumber']);

$this->assertEquals($article['id'], $response['_embedded']['article']['id']);

$article = $this->getArticle($article['id']);
$this->assertCount(1, $article['pages']);
$this->assertEquals($response['id'], reset($article['pages'])['id']);
$this->assertCount(1, $article['_embedded']['pages']);
$this->assertEquals($response['id'], reset($article['_embedded']['pages'])['id']);

$articleViewDocument = $this->findViewDocument($article['id'], 'de');
$this->assertCount(1, $articleViewDocument->getPages());
Expand Down Expand Up @@ -164,7 +166,7 @@ public function testDelete()
$this->assertHttpStatusCode(204, $client->getResponse());

$article = $this->getArticle($article['id']);
$this->assertCount(0, $article['pages']);
$this->assertCount(0, $article['_embedded']['pages']);

$articleViewDocument = $this->findViewDocument($article['id'], 'de');
$this->assertCount(0, $articleViewDocument->getPages());
Expand Down

0 comments on commit 96311f6

Please sign in to comment.