Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Support for setting the redirect content as a content document
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Sep 15, 2015
1 parent e977f7f commit 7bd390f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
33 changes: 31 additions & 2 deletions Adapter/PhpcrOdmAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,6 +65,7 @@ public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn
}

$this->autoRouteFqcn = $autoRouteFqcn;
$this->redirectTarget = $redirectTarget;
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

dev-master
----------

* Support for redirecting to content instead of other routes.

1.0.0
-----

Expand Down
2 changes: 1 addition & 1 deletion Model/AutoRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function setType($type)
$this->setDefault('type', $type);
}

public function setRedirectTarget(AutoRouteInterface $redirectRoute)
public function setRedirectTarget($redirectRoute)
{
$this->redirectRoute = $redirectRoute;
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/EventListener/AutoRouteListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'));
}
}
Expand Down
56 changes: 56 additions & 0 deletions Tests/Unit/Adapter/PhpcrOdmAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit 7bd390f

Please sign in to comment.