Skip to content

Commit

Permalink
Pass route params to redirect tag (#3801)
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell authored Jun 8, 2021
1 parent a4ebc24 commit 16e17e9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
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());
}
}
}

0 comments on commit 16e17e9

Please sign in to comment.