Skip to content

Commit

Permalink
Merge branch 'ezp26961-url_alias_redirect_location_id_header' into 6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Mar 21, 2017
2 parents 739e8ba + 69d0c16 commit 0cc073c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ public function onKernelRequestRedirect(GetResponseEvent $event)
$semanticPathinfo = $siteaccess->matcher->analyseLink($semanticPathinfo);
}

$headers = [];
if ($request->attributes->has('locationId')) {
$headers[ 'X-Location-Id'] = $request->attributes->get('locationId');
}
$event->setResponse(
new RedirectResponse(
$semanticPathinfo . ($queryString ? "?$queryString" : ''),
301
301,
$headers
)
);
$event->stopPropagation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,29 @@ public function testOnKernelRequestRedirect()
$this->assertTrue($event->isPropagationStopped());
}

public function testOnKernelRequestRedirectWithLocationId()
{
$queryParameters = array('some' => 'thing');
$cookieParameters = array('cookie' => 'value');
$request = Request::create('/test_sa/foo/bar', 'GET', $queryParameters, $cookieParameters);
$semanticPathinfo = '/foo/something';
$request->attributes->set('semanticPathinfo', $semanticPathinfo);
$request->attributes->set('needsRedirect', true);
$request->attributes->set('locationId', 123);
$request->attributes->set('siteaccess', new SiteAccess());

$event = new GetResponseEvent($this->httpKernel, $request, HttpKernelInterface::MASTER_REQUEST);
$this->requestEventListener->onKernelRequestRedirect($event);
$this->assertTrue($event->hasResponse());
/** @var RedirectResponse $response */
$response = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame("$semanticPathinfo?some=thing", $response->getTargetUrl());
$this->assertSame(301, $response->getStatusCode());
$this->assertEquals(123, $response->headers->get('X-Location-Id'));
$this->assertTrue($event->isPropagationStopped());
}

public function testOnKernelRequestRedirectPrependSiteaccess()
{
$queryParameters = array('some' => 'thing');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function testMatchRequestLocationCaseRedirectWithRootLocation()
$this->assertSame($locationId, $request->attributes->get('locationId'));
$this->assertTrue($request->attributes->get('needsRedirect'));
$this->assertSame($path, $request->attributes->get('semanticPathinfo'));
$this->assertSame($locationId, $request->attributes->get('locationId'));
}

public function testMatchRequestLocationCaseRedirectWithRootRootLocation()
Expand Down
3 changes: 3 additions & 0 deletions eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public function matchRequest(Request $request)
// Specify not to prepend siteaccess while redirecting when applicable since it would be already present (see UrlAliasGenerator::doGenerate())
$request->attributes->set('prependSiteaccessOnRedirect', false);
} elseif ($this->needsCaseRedirect($urlAlias, $requestedPath, $pathPrefix)) {
if ($urlAlias->destination instanceof Location) {
$request->attributes->set('locationId', $urlAlias->destination->id);
}
$request->attributes->set('semanticPathinfo', $this->removePathPrefix($urlAlias->path, $pathPrefix));
$request->attributes->set('needsRedirect', true);
}
Expand Down

0 comments on commit 0cc073c

Please sign in to comment.