Skip to content

Commit

Permalink
fixed suffix generation by using chain-route-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed May 2, 2017
1 parent c3e4891 commit 9538e85
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
51 changes: 39 additions & 12 deletions Content/PageTreeRouteContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Sulu\Bundle\ArticleBundle\Content;

use Ferrandini\Urlizer;
use PHPCR\ItemNotFoundException;
use PHPCR\NodeInterface;
use PHPCR\PropertyType;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
use Sulu\Bundle\RouteBundle\Generator\ChainRouteGeneratorInterface;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\SimpleContentType;
use Sulu\Component\DocumentManager\DocumentRegistry;

/**
* Provides page_tree_route content-type.
Expand All @@ -32,20 +32,30 @@ class PageTreeRouteContentType extends SimpleContentType
private $template;

/**
* @var PropertyEncoder
* @var DocumentRegistry
*/
private $propertyEncoder;
private $documentRegistry;

/**
* @var ChainRouteGeneratorInterface
*/
private $chainRouteGenerator;

/**
* @param string $template
* @param PropertyEncoder $propertyEncoder
* @param DocumentRegistry $documentRegistry
* @param ChainRouteGeneratorInterface $chainRouteGenerator
*/
public function __construct($template, PropertyEncoder $propertyEncoder)
{
public function __construct(
$template,
DocumentRegistry $documentRegistry,
ChainRouteGeneratorInterface $chainRouteGenerator
) {
parent::__construct('PageTreeRoute');

$this->template = $template;
$this->propertyEncoder = $propertyEncoder;
$this->documentRegistry = $documentRegistry;
$this->chainRouteGenerator = $chainRouteGenerator;
}

/**
Expand Down Expand Up @@ -81,12 +91,13 @@ public function write(
return $this->remove($node, $property, $webspaceKey, $languageCode, $segmentKey);
}

$titlePropertyName = $this->propertyEncoder->localizedContentName('title', $languageCode);
$title = $node->getPropertyValueWithDefault($titlePropertyName, null);

$path = $this->getAttribute('path', $value, '');
$page = $this->getAttribute('page', $value, ['uuid' => null, 'path' => '/']);
$suffix = $this->getAttribute('suffix', $value, Urlizer::urlize($title));

$suffix = $this->getAttribute('suffix', $value);
if (!$suffix) {
$suffix = $this->generateSuffix($node, $languageCode);
}

// generate url if not set
if (!$path) {
Expand Down Expand Up @@ -175,4 +186,20 @@ private function getAttribute($name, array $value, $default = null)

return $value[$name];
}

/**
* Generate a new suffix for document.
*
* @param NodeInterface $node
* @param string $locale
*
* @return string
*/
private function generateSuffix(NodeInterface $node, $locale)
{
$document = $this->documentRegistry->getDocumentForNode($node, $locale);
$route = $this->chainRouteGenerator->generate($document);

return ltrim($route->getPath(), '/');
}
}
3 changes: 2 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@
<service id="sulu_article.content_types.page_tree_route"
class="Sulu\Bundle\ArticleBundle\Content\PageTreeRouteContentType">
<argument>%sulu_article.content-type.page_tree_route.template%</argument>
<argument type="service" id="sulu_document_manager.property_encoder"/>
<argument type="service" id="sulu_document_manager.document_registry"/>
<argument type="service" id="sulu_route.chain_generator"/>

<tag name="sulu.content.type" alias="page_tree_route"/>
</service>
Expand Down
38 changes: 26 additions & 12 deletions Tests/Functional/Content/PageTreeRouteContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@

use PHPCR\NodeInterface;
use PHPCR\PropertyType;
use Prophecy\Argument;
use Sulu\Bundle\ArticleBundle\Content\PageTreeRouteContentType;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\RouteBundle\Generator\ChainRouteGeneratorInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\DocumentManager\DocumentRegistry;

class PageTreeRouteContentTypeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PropertyEncoder
* @var DocumentRegistry
*/
private $propertyEncoder;
private $documentRegistry;

/**
* @var ChainRouteGeneratorInterface
*/
private $chainRouteGenerator;

/**
* @var PageTreeRouteContentType
Expand Down Expand Up @@ -62,19 +69,18 @@ class PageTreeRouteContentTypeTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->propertyEncoder = $this->prophesize(PropertyEncoder::class);
$this->documentRegistry = $this->prophesize(DocumentRegistry::class);
$this->chainRouteGenerator = $this->prophesize(ChainRouteGeneratorInterface::class);
$this->property = $this->prophesize(PropertyInterface::class);
$this->node = $this->prophesize(NodeInterface::class);

$this->property->getName()->willReturn($this->propertyName);
$this->node->getPropertyValueWithDefault('i18n:' . $this->locale . '-title', null)->willReturn('Test article');
$this->propertyEncoder->localizedContentName(Argument::type('string'), $this->locale)->will(
function ($arguments) {
return 'i18n:' . $arguments[1] . '-' . $arguments[0];
}
);

$this->contentType = new PageTreeRouteContentType($this->template, $this->propertyEncoder->reveal());
$this->contentType = new PageTreeRouteContentType(
$this->template,
$this->documentRegistry->reveal(),
$this->chainRouteGenerator->reveal()
);
}

public function testRead()
Expand Down Expand Up @@ -201,6 +207,14 @@ public function testWriteExistingPageRelation()

public function testWriteGeneratePath()
{
$route = $this->prophesize(RouteInterface::class);
$route->getPath()->willReturn('/test-article');

$document = $this->prophesize(ArticleDocument::class);
$this->chainRouteGenerator->generate($document->reveal())->willReturn($route->reveal());
$this->documentRegistry->getDocumentForNode($this->node->reveal(), $this->locale)
->willReturn($document->reveal());

$value = [
'page' => [
'uuid' => '123-123-123',
Expand Down
34 changes: 17 additions & 17 deletions Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,14 +913,14 @@ public function testPostPageTreeRouteGenerate()
'uuid' => $page->getUuid(),
'path' => $page->getResourceSegment(),
],
'suffix' => 'test-article',
'path' => '/test-page/test-article',
'suffix' => 'articles/test-article',
'path' => '/test-page/articles/test-article',
];

$response = $this->postPageTreeRoute(['page' => $routePathData['page']]);

$this->assertEquals('Test Article', $response['title']);
$this->assertEquals('/test-page/test-article', $response['route']);
$this->assertEquals('/test-page/articles/test-article', $response['route']);
$this->assertEquals($routePathData, $response['routePath']);
}

Expand All @@ -933,8 +933,8 @@ public function testPostPageTreeRouteGeneratePublishPage()
'uuid' => $page->getUuid(),
'path' => $page->getResourceSegment(),
],
'suffix' => 'test-article',
'path' => '/test-page/test-article',
'suffix' => 'articles/test-article',
'path' => '/test-page/articles/test-article',
];

$article = $this->postPageTreeRoute(['page' => $routePathData['page']]);
Expand All @@ -953,15 +953,15 @@ public function testPostPageTreeRouteGeneratePublishPage()
$response = json_decode($client->getResponse()->getContent(), true);

$this->assertEquals('Test Article', $response['title']);
$this->assertEquals('/test-page-2/test-article', $response['route']);
$this->assertEquals('/test-page-2/articles/test-article', $response['route']);
$this->assertEquals(
[
'page' => [
'uuid' => $page->getUuid(),
'path' => $page->getResourceSegment(),
],
'suffix' => 'test-article',
'path' => '/test-page-2/test-article',
'suffix' => 'articles/test-article',
'path' => '/test-page-2/articles/test-article',
],
$response['routePath']
);
Expand All @@ -978,7 +978,7 @@ public function testPostPageTreeRouteGenerateMovePage()
'path' => $page1->getResourceSegment(),
],
'suffix' => 'test-article',
'path' => '/test-page/test-article',
'path' => '/test-page/articles/test-article',
];

$article = $this->postPageTreeRoute(['page' => $routePathData['page']]);
Expand All @@ -996,15 +996,15 @@ public function testPostPageTreeRouteGenerateMovePage()
$response = json_decode($client->getResponse()->getContent(), true);

$this->assertEquals('Test Article', $response['title']);
$this->assertEquals('/page-2/page-1/test-article', $response['route']);
$this->assertEquals('/page-2/page-1/articles/test-article', $response['route']);
$this->assertEquals(
[
'page' => [
'uuid' => $page1->getUuid(),
'path' => $page1->getResourceSegment(),
],
'suffix' => 'test-article',
'path' => '/page-2/page-1/test-article',
'suffix' => 'articles/test-article',
'path' => '/page-2/page-1/articles/test-article',
],
$response['routePath']
);
Expand All @@ -1019,8 +1019,8 @@ public function testPostPageTreeRouteGenerateRemovePage()
'uuid' => $page->getUuid(),
'path' => $page->getResourceSegment(),
],
'suffix' => 'test-article',
'path' => '/test-page/test-article',
'suffix' => 'articles/test-article',
'path' => '/test-page/articles/test-article',
];

$article = $this->postPageTreeRoute($routePathData);
Expand All @@ -1036,12 +1036,12 @@ public function testPostPageTreeRouteGenerateRemovePage()
$response = json_decode($client->getResponse()->getContent(), true);

$this->assertEquals('Test Article', $response['title']);
$this->assertEquals('/test-page/test-article', $response['route']);
$this->assertEquals('/test-page/articles/test-article', $response['route']);
$this->assertEquals(
[
'page' => null,
'suffix' => 'test-article',
'path' => '/test-page/test-article',
'suffix' => 'articles/test-article',
'path' => '/test-page/articles/test-article',
],
$response['routePath']
);
Expand Down

0 comments on commit 9538e85

Please sign in to comment.