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

Pass route params to redirect tag #3801

Merged
merged 1 commit into from
Jun 8, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Tags/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public function wildcard($tag)
public function index()
{
if ($route = $this->params->get('route')) {
return $this->redirect(route($route));
return $this->redirect(route(
$route,
$this->params->forget('route')->all()
));
}

return $this->redirect(
Expand Down
80 changes: 80 additions & 0 deletions tests/Tags/RedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Tests\Tags;

use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Route;
use Statamic\Facades\Parse;
use Statamic\Facades\Site;
use Tests\TestCase;

class RedirectTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Site::setConfig(['sites' => [
'en' => ['url' => '/'],
'fr' => ['url' => '/fr'],
]]);
}

protected function resolveApplicationConfiguration($app)
{
parent::resolveApplicationConfiguration($app);

$app->booted(function () {
Route::statamic('/named-route', 'test')->name('named-route');

Route::statamic('/route-with-params/{foo}/baz', 'test')->name('param-route');
});
}

private function tag($tag, $data = [])
{
return (string) Parse::template($tag, $data);
}

/** @test */
public function it_throws_http_response_exception()
{
$this->expectException(HttpResponseException::class);
$this->tag('{{ redirect to="/foo" }}');
}

/** @test */
public function it_redirects_to()
{
try {
$this->tag('{{ redirect to="/foo" }}');
} catch (HttpResponseException $e) {
$response = $e->getResponse();
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals('http://localhost/foo', $response->getTargetUrl());
}
}

/** @test */
public function it_redirects_to_route()
{
try {
$this->tag('{{ redirect route="named-route" }}');
} catch (HttpResponseException $e) {
$response = $e->getResponse();
$this->assertEquals('http://localhost/named-route', $response->getTargetUrl());
}
}

/** @test */
public function it_redirects_to_route_with_query_params()
{
try {
$this->tag('{{ redirect route="param-route" foo="bar" }}');
} catch (HttpResponseException $e) {
$response = $e->getResponse();
$this->assertEquals('http://localhost/route-with-params/bar/baz', $response->getTargetUrl());
}
}
}