Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FrameworkBundle] keep query in redirect #26281

Merged
merged 1 commit into from Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -54,7 +54,7 @@ public function __construct(UrlGeneratorInterface $router = null, int $httpPort
*
* @throws HttpException In case the route name is empty
*/
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false): Response
public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false, bool $keepQueryParams = false): Response
{
if ('' == $route) {
throw new HttpException($permanent ? 410 : 404);
Expand All @@ -63,6 +63,7 @@ public function redirectAction(Request $request, string $route, bool $permanent
$attributes = array();
if (false === $ignoreAttributes || is_array($ignoreAttributes)) {
$attributes = $request->attributes->get('_route_params');
$attributes = $keepQueryParams ? array_merge($request->query->all(), $attributes) : $attributes;
unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes'], $attributes['keepRequestMethod']);
if ($ignoreAttributes) {
$attributes = array_diff_key($attributes, array_flip($ignoreAttributes));
Expand Down
Expand Up @@ -235,6 +235,40 @@ public function testPathQueryParams($expectedUrl, $path, $queryString)
$this->assertRedirectUrl($returnValue, $expectedUrl);
}

public function testRedirectWithQuery()
{
$scheme = 'http';
$host = 'www.example.com';
$baseUrl = '/base';
$port = 80;

$request = $this->createRequestObject($scheme, $host, $port, $baseUrl, 'base=zaza');
$request->query = new ParameterBag(array('base' => 'zaza'));
$request->attributes = new ParameterBag(array('_route_params' => array('base2' => 'zaza')));
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
$urlGenerator->expects($this->once())->method('generate')->will($this->returnValue('/test?base=zaza&base2=zaza'))->with('/test', array('base' => 'zaza', 'base2' => 'zaza'), UrlGeneratorInterface::ABSOLUTE_URL);

$controller = new RedirectController($urlGenerator);
$this->assertRedirectUrl($controller->redirectAction($request, '/test', false, false, false, true), '/test?base=zaza&base2=zaza');
}

public function testRedirectWithQueryWithRouteParamsOveriding()
{
$scheme = 'http';
$host = 'www.example.com';
$baseUrl = '/base';
$port = 80;

$request = $this->createRequestObject($scheme, $host, $port, $baseUrl, 'base=zaza');
$request->query = new ParameterBag(array('base' => 'zaza'));
$request->attributes = new ParameterBag(array('_route_params' => array('base' => 'zouzou')));
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
$urlGenerator->expects($this->once())->method('generate')->will($this->returnValue('/test?base=zouzou'))->with('/test', array('base' => 'zouzou'), UrlGeneratorInterface::ABSOLUTE_URL);

$controller = new RedirectController($urlGenerator);
$this->assertRedirectUrl($controller->redirectAction($request, '/test', false, false, false, true), '/test?base=zouzou');
}

private function createRequestObject($scheme, $host, $port, $baseUrl, $queryString = '')
{
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
Expand Down