diff --git a/Adapter/PhpcrOdmAdapter.php b/Adapter/PhpcrOdmAdapter.php index 320d9f5..1f39f46 100644 --- a/Adapter/PhpcrOdmAdapter.php +++ b/Adapter/PhpcrOdmAdapter.php @@ -31,16 +31,30 @@ class PhpcrOdmAdapter implements AdapterInterface { const TAG_NO_MULTILANG = 'no-multilang'; + /** + * Set the redirect target to the new auto-routes content document + */ + const REDIRECT_CONTENT = 'content'; + + /** + * Set the redirect target to the new auto-route itself. + */ + const REDIRECT_ROUTE = 'route'; + protected $dm; protected $baseRoutePath; protected $autoRouteFqcn; + protected $redirectTarget = self::REDIRECT_ROUTE; /** * @param DocumentManager $dm * @param string $routeBasePath Route path for all routes * @param string $autoRouteFqcn The FQCN of the AutoRoute document to use + * @param string $redirectTarget The target type to use when + * leaving a redirect route, either self::REDIRECT_ROUTE, or + * self::REDIRECT_CONTENT/ */ - public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn = 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute') + public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn = 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $redirectTarget = self::REDIRECT_ROUTE) { $this->dm = $dm; $this->baseRoutePath = $routeBasePath; @@ -51,6 +65,7 @@ public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn } $this->autoRouteFqcn = $autoRouteFqcn; + $this->redirectTarget = $redirectTarget; } /** @@ -154,7 +169,21 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR */ public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute) { - $referringAutoRoute->setRedirectTarget($newRoute); + switch ($this->redirectTarget) { + case self::REDIRECT_CONTENT: + $target = $newRoute->getContent(); + break; + case self::REDIRECT_ROUTE: + $target = $newRoute; + break; + default: + throw new \RuntimeException(sprintf( + 'Unknown redirect target type "%s"', + $this->redirectTarget + )); + } + + $referringAutoRoute->setRedirectTarget($target); $referringAutoRoute->setType(AutoRouteInterface::TYPE_REDIRECT); } diff --git a/CHANGELOG.md b/CHANGELOG.md index 950b797..ed104fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Changelog ========= +dev-master +---------- + +* Support for redirecting to content instead of other routes. + 1.0.0 ----- diff --git a/Model/AutoRoute.php b/Model/AutoRoute.php index 29f6077..396666f 100644 --- a/Model/AutoRoute.php +++ b/Model/AutoRoute.php @@ -51,7 +51,7 @@ public function setType($type) $this->setDefault('type', $type); } - public function setRedirectTarget(AutoRouteInterface $redirectRoute) + public function setRedirectTarget($redirectRoute) { $this->redirectRoute = $redirectRoute; } diff --git a/Tests/Functional/EventListener/AutoRouteListenerTest.php b/Tests/Functional/EventListener/AutoRouteListenerTest.php index d7bbbe1..dc83113 100644 --- a/Tests/Functional/EventListener/AutoRouteListenerTest.php +++ b/Tests/Functional/EventListener/AutoRouteListenerTest.php @@ -351,7 +351,7 @@ public function provideLeaveRedirect() /** * @dataProvider provideLeaveRedirect */ - public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths, $expectedAutoRoutePaths) + public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths, $expectedPaths) { $article = new SeoArticleMultilang; $article->title = 'Hai'; @@ -380,9 +380,9 @@ public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePat $this->assertEquals(AutoRouteInterface::TYPE_REDIRECT, $redirectRoute->getDefault('type')); } - foreach ($expectedAutoRoutePaths as $newPath) { + foreach ($expectedPaths as $newPath) { $autoRoute = $this->getDm()->find(null, $newPath); - $this->assertNotNull($autoRoute, 'Autoroute exists for: ' . $newPath); + $this->assertNotNull($autoRoute, 'Redirect target for: ' . $newPath); $this->assertEquals(AutoRouteInterface::TYPE_PRIMARY, $autoRoute->getDefault('type')); } } diff --git a/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php b/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php index b8dec41..b0b2563 100644 --- a/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php +++ b/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php @@ -182,4 +182,60 @@ public function testFindRouteForUri() $res = $this->adapter->findRouteForUri($uri, $this->uriContext->reveal()); $this->assertSame($expectedRoutes, $res); } + + /** + * It should set the redirect target as the content document when configured to do so. + */ + public function testCreateRedirectRouteContent() + { + $adapter = new PhpcrOdmAdapter( + $this->dm->reveal(), + $this->baseRoutePath, + 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', + PhpcrOdmAdapter::REDIRECT_CONTENT + ); + $newRoute = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute'); + $newRoute->getContent()->willReturn($this->contentDocument); + + $adapter->createRedirectRoute($this->route->reveal(), $newRoute->reveal()); + $this->route->setRedirectTarget($this->contentDocument)->shouldHaveBeenCalled(); + } + + /** + * It should set the redirect target as route when configured to do so. + */ + public function testCreateRedirectRoute() + { + $adapter = new PhpcrOdmAdapter( + $this->dm->reveal(), + $this->baseRoutePath, + 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', + PhpcrOdmAdapter::REDIRECT_ROUTE + ); + $newRoute = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute'); + $newRoute->getContent()->shouldNotBeCalled(); + + $adapter->createRedirectRoute($this->route->reveal(), $newRoute->reveal()); + $this->route->setRedirectTarget($newRoute)->shouldHaveBeenCalled(); + } + + /** + * It should throw an exception if the redirect target type is not valid + * + * @expectedException RuntimeException + * @expectedExceptionMessage Unknown redirect target type + */ + public function testInvalidRedirectTargetType() + { + $adapter = new PhpcrOdmAdapter( + $this->dm->reveal(), + $this->baseRoutePath, + 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', + 'foobar' // invalid + ); + $newRoute = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute'); + + $adapter->createRedirectRoute($this->route->reveal(), $newRoute->reveal()); + } + }