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

Commit cdb6e3a

Browse files
committed
Support for setting the redirect content as a content document
1 parent e977f7f commit cdb6e3a

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

Adapter/PhpcrOdmAdapter.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,30 @@ class PhpcrOdmAdapter implements AdapterInterface
3131
{
3232
const TAG_NO_MULTILANG = 'no-multilang';
3333

34+
/**
35+
* Set the redirect target to the new auto-routes content document
36+
*/
37+
const REDIRECT_CONTENT = 'content';
38+
39+
/**
40+
* Set the redirect target to the new auto-route itself.
41+
*/
42+
const REDIRECT_ROUTE = 'route';
43+
3444
protected $dm;
3545
protected $baseRoutePath;
3646
protected $autoRouteFqcn;
47+
protected $redirectTarget = self::REDIRECT_ROUTE;
3748

3849
/**
3950
* @param DocumentManager $dm
4051
* @param string $routeBasePath Route path for all routes
4152
* @param string $autoRouteFqcn The FQCN of the AutoRoute document to use
53+
* @param string $redirectTarget The target type to use when
54+
* leaving a redirect route, either self::REDIRECT_ROUTE, or
55+
* self::REDIRECT_CONTENT/
4256
*/
43-
public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn = 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute')
57+
public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn = 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $redirectTarget = self::REDIRECT_ROUTE)
4458
{
4559
$this->dm = $dm;
4660
$this->baseRoutePath = $routeBasePath;
@@ -51,6 +65,7 @@ public function __construct(DocumentManager $dm, $routeBasePath, $autoRouteFqcn
5165
}
5266

5367
$this->autoRouteFqcn = $autoRouteFqcn;
68+
$this->redirectTarget = $redirectTarget;
5469
}
5570

5671
/**
@@ -154,7 +169,21 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
154169
*/
155170
public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute)
156171
{
157-
$referringAutoRoute->setRedirectTarget($newRoute);
172+
switch ($this->redirectTarget) {
173+
case self::REDIRECT_CONTENT:
174+
$target = $newRoute->getContent();
175+
break;
176+
case self::REDIRECT_ROUTE:
177+
$target = $newRoute;
178+
break;
179+
default:
180+
throw new \RuntimeException(sprintf(
181+
'Unknown redirect target type "%s"',
182+
$this->redirectTarget
183+
));
184+
}
185+
186+
$referringAutoRoute->setRedirectTarget($target);
158187
$referringAutoRoute->setType(AutoRouteInterface::TYPE_REDIRECT);
159188
}
160189

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
dev-master
5+
----------
6+
7+
* Support for redirecting to content instead of other routes.
8+
49
1.0.0
510
-----
611

Model/AutoRoute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function setType($type)
5151
$this->setDefault('type', $type);
5252
}
5353

54-
public function setRedirectTarget(AutoRouteInterface $redirectRoute)
54+
public function setRedirectTarget($redirectRoute)
5555
{
5656
$this->redirectRoute = $redirectRoute;
5757
}

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function provideLeaveRedirect()
351351
/**
352352
* @dataProvider provideLeaveRedirect
353353
*/
354-
public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths, $expectedAutoRoutePaths)
354+
public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths, $expectedPaths)
355355
{
356356
$article = new SeoArticleMultilang;
357357
$article->title = 'Hai';
@@ -380,9 +380,9 @@ public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePat
380380
$this->assertEquals(AutoRouteInterface::TYPE_REDIRECT, $redirectRoute->getDefault('type'));
381381
}
382382

383-
foreach ($expectedAutoRoutePaths as $newPath) {
383+
foreach ($expectedPaths as $newPath) {
384384
$autoRoute = $this->getDm()->find(null, $newPath);
385-
$this->assertNotNull($autoRoute, 'Autoroute exists for: ' . $newPath);
385+
$this->assertNotNull($autoRoute, 'Redirect target for: ' . $newPath);
386386
$this->assertEquals(AutoRouteInterface::TYPE_PRIMARY, $autoRoute->getDefault('type'));
387387
}
388388
}

Tests/Unit/Adapter/PhpcrOdmAdapterTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,60 @@ public function testFindRouteForUri()
182182
$res = $this->adapter->findRouteForUri($uri, $this->uriContext->reveal());
183183
$this->assertSame($expectedRoutes, $res);
184184
}
185+
186+
/**
187+
* It should set the redirect target as the content document when configured to do so.
188+
*/
189+
public function testCreateRedirectRouteContent()
190+
{
191+
$adapter = new PhpcrOdmAdapter(
192+
$this->dm->reveal(),
193+
$this->baseRoutePath,
194+
'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute',
195+
PhpcrOdmAdapter::REDIRECT_CONTENT
196+
);
197+
$newRoute = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute');
198+
$newRoute->getContent()->willReturn($this->contentDocument);
199+
200+
$adapter->createRedirectRoute($this->route->reveal(), $newRoute->reveal());
201+
$this->route->setRedirectTarget($this->contentDocument)->shouldHaveBeenCalled();
202+
}
203+
204+
/**
205+
* It should set the redirect target as route when configured to do so.
206+
*/
207+
public function testCreateRedirectRoute()
208+
{
209+
$adapter = new PhpcrOdmAdapter(
210+
$this->dm->reveal(),
211+
$this->baseRoutePath,
212+
'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute',
213+
PhpcrOdmAdapter::REDIRECT_ROUTE
214+
);
215+
$newRoute = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute');
216+
$newRoute->getContent()->shouldNotBeCalled();
217+
218+
$adapter->createRedirectRoute($this->route->reveal(), $newRoute->reveal());
219+
$this->route->setRedirectTarget($newRoute)->shouldHaveBeenCalled();
220+
}
221+
222+
/**
223+
* It should throw an exception if the redirect target type is not valid
224+
*
225+
* @expectedException RuntimeException
226+
* @expectedExceptionMessage Unknown redirect target type
227+
*/
228+
public function testInvalidRedirectTargetType()
229+
{
230+
$adapter = new PhpcrOdmAdapter(
231+
$this->dm->reveal(),
232+
$this->baseRoutePath,
233+
'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute',
234+
'foobar' // invalid
235+
);
236+
$newRoute = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute');
237+
238+
$adapter->createRedirectRoute($this->route->reveal(), $newRoute->reveal());
239+
}
240+
185241
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"prefer-stable": true,
1414
"require": {
1515
"php": ">=5.3.9",
16-
"symfony-cmf/routing-auto": "~1.1",
16+
"symfony-cmf/routing-auto": "dev-remove_typehint@dev",
1717
"symfony-cmf/routing-bundle": "~1.2,>=1.2.0",
1818
"symfony-cmf/core-bundle": "~1.2",
1919
"aferrandini/urlizer": "1.0.*",

0 commit comments

Comments
 (0)