From 70144494ceaaf590301c362bdf238fed51724607 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Tue, 2 May 2017 12:55:52 +0200 Subject: [PATCH] Fixed suffix generation by using chain-route-generator (#163) * fixed suffix generation by using chain-route-generator * removed options --- Content/PageTreeRouteContentType.php | 51 ++++++++++++++----- Resources/config/services.xml | 3 +- .../page-tree-route/page-select/main.js | 2 +- .../page-tree-route/page-select/main.js | 1 - .../Content/PageTreeRouteContentTypeTest.php | 38 +++++++++----- .../Controller/ArticleControllerTest.php | 34 ++++++------- 6 files changed, 85 insertions(+), 44 deletions(-) diff --git a/Content/PageTreeRouteContentType.php b/Content/PageTreeRouteContentType.php index 77e187e76..fe7fd6564 100644 --- a/Content/PageTreeRouteContentType.php +++ b/Content/PageTreeRouteContentType.php @@ -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. @@ -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; } /** @@ -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) { @@ -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(), '/'); + } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index aae5ff962..f14c4fbb6 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -276,7 +276,8 @@ %sulu_article.content-type.page_tree_route.template% - + + diff --git a/Resources/public/dist/components/page-tree-route/page-select/main.js b/Resources/public/dist/components/page-tree-route/page-select/main.js index 90fd68451..0ec824cb5 100644 --- a/Resources/public/dist/components/page-tree-route/page-select/main.js +++ b/Resources/public/dist/components/page-tree-route/page-select/main.js @@ -1 +1 @@ -define(function(){return{defaults:{options:{selected:null,locale:null,selectCallback:function(a){},cancelCallback:function(){}},translations:{overlayTitle:"public.choose"}},initialize:function(){this.id=this.options.selected;var a=$("
"),b=$("
");this.$el.append(a),this.sandbox.start([{name:"overlay@husky",options:{el:a,openOnStart:!0,removeOnClose:!0,skin:"medium",instanceName:"page-select",slides:[{title:this.translations.overlayTitle,data:b,cssClass:"data-source-slide",contentSpacing:!1,okCallback:function(){this.item&&this.options.selectCallback(this.item)}.bind(this),cancelCallback:function(){this.options.cancelCallback()}.bind(this)}]}}]).then(this.startContentDatasource.bind(this,b))},startContentDatasource:function(a){this.sandbox.start([{name:"content-datasource@sulucontent",options:{el:a,singleMarkable:!0,selected:this.options.selected,locale:this.options.locale,selectedUrl:"/admin/api/nodes/{datasource}?tree=true&language={locale}&fields=title,order,published,url&webspace-nodes=all",rootUrl:"/admin/api/nodes?language={locale}&fields=title,order,published&webspace-nodes=all",resultKey:"nodes",instanceName:"internal-link",instanceNamePrefix:"",showStatus:!0,selectCallback:function(a,b,c,d){return this.id===a?this.setItem(null,null):void this.setItem(d,a)}.bind(this)}}])},setItem:function(a,b){this.item=a,this.id=b;var c="activate";a||(c="deactivate"),this.sandbox.emit("husky.overlay.page-select.okbutton."+c)}}}); \ No newline at end of file +define(function(){return{defaults:{options:{selected:null,locale:null,selectCallback:function(a){},cancelCallback:function(){}},translations:{overlayTitle:"public.choose"}},initialize:function(){this.id=this.options.selected;var a=$("
"),b=$("
");this.$el.append(a),this.sandbox.start([{name:"overlay@husky",options:{el:a,openOnStart:!0,removeOnClose:!0,skin:"medium",instanceName:"page-select",slides:[{title:this.translations.overlayTitle,data:b,cssClass:"data-source-slide",contentSpacing:!1,okCallback:function(){this.item&&this.options.selectCallback(this.item)}.bind(this),cancelCallback:function(){this.options.cancelCallback()}.bind(this)}]}}]).then(this.startContentDatasource.bind(this,b))},startContentDatasource:function(a){this.sandbox.start([{name:"content-datasource@sulucontent",options:{el:a,selected:this.options.selected,locale:this.options.locale,selectedUrl:"/admin/api/nodes/{datasource}?tree=true&language={locale}&fields=title,order,published,url&webspace-nodes=all",rootUrl:"/admin/api/nodes?language={locale}&fields=title,order,published&webspace-nodes=all",resultKey:"nodes",instanceName:"internal-link",instanceNamePrefix:"",showStatus:!0,selectCallback:function(a,b,c,d){return this.id===a?this.setItem(null,null):void this.setItem(d,a)}.bind(this)}}])},setItem:function(a,b){this.item=a,this.id=b;var c="activate";a||(c="deactivate"),this.sandbox.emit("husky.overlay.page-select.okbutton."+c)}}}); \ No newline at end of file diff --git a/Resources/public/js/components/page-tree-route/page-select/main.js b/Resources/public/js/components/page-tree-route/page-select/main.js index 4cf1ff918..e69e2defd 100644 --- a/Resources/public/js/components/page-tree-route/page-select/main.js +++ b/Resources/public/js/components/page-tree-route/page-select/main.js @@ -74,7 +74,6 @@ define(function() { name: 'content-datasource@sulucontent', options: { el: $componentContainer, - singleMarkable: true, selected: this.options.selected, locale: this.options.locale, selectedUrl: '/admin/api/nodes/{datasource}?tree=true&language={locale}&fields=title,order,published,url&webspace-nodes=all', diff --git a/Tests/Functional/Content/PageTreeRouteContentTypeTest.php b/Tests/Functional/Content/PageTreeRouteContentTypeTest.php index d3804f014..7d45668c9 100644 --- a/Tests/Functional/Content/PageTreeRouteContentTypeTest.php +++ b/Tests/Functional/Content/PageTreeRouteContentTypeTest.php @@ -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 @@ -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() @@ -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', diff --git a/Tests/Functional/Controller/ArticleControllerTest.php b/Tests/Functional/Controller/ArticleControllerTest.php index 14a990c76..0a08d80e4 100644 --- a/Tests/Functional/Controller/ArticleControllerTest.php +++ b/Tests/Functional/Controller/ArticleControllerTest.php @@ -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']); } @@ -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']]); @@ -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'] ); @@ -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']]); @@ -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'] ); @@ -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); @@ -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'] );