Skip to content

Commit

Permalink
Add support for redirect origin wildcard, excluding root path
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny van Wijk committed Oct 18, 2024
1 parent 6542e9f commit da28203
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
22 changes: 18 additions & 4 deletions src/Kunstmaan/RedirectBundle/Router/RedirectRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,26 @@ private function initRouteCollection(string $pathInfo): void
return;
}

$routePath = str_contains($redirect->getOrigin(), '/*') ? $pathInfo : $redirect->getOrigin();
$origin = $redirect->getOrigin();
$routePath = str_contains($origin, '/*') ? $pathInfo : $origin;
$targetPath = $redirect->getTarget();
if (str_contains($redirect->getOrigin(), '/*') && str_contains($redirect->getTarget(), '/*')) {
$origin = rtrim($redirect->getOrigin(), '/*');
if (str_contains($origin, '/*') && str_contains($redirect->getTarget(), '/*')) {
if ($origin === '/*' && $routePath === '/') { // exclude root path
return;
}

$origin = rtrim($origin, '/*');
$target = rtrim($redirect->getTarget(), '/*');
$targetPath = str_replace($origin, $target, $pathInfo);
if ($origin === '') {
$targetPath = $target . $pathInfo;
} else {
$targetPath = str_replace($origin, $target, $pathInfo);
}
}

$queryString = $this->context->getQueryString();
if ($queryString) {
$targetPath .= '?'.$queryString;
}

$needsUtf8 = false;
Expand Down
20 changes: 14 additions & 6 deletions src/Kunstmaan/RedirectBundle/Tests/Router/RedirectRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public function testRedirectsForImprovedRouter(string $requestUrl, ?string $expe
$firstDomainConfiguration = $this->getMockBuilder(DomainConfigurationInterface::class)->getMock();
$firstDomainConfiguration->method('getHost')->willReturn('');

$request = Request::create($requestUrl);

$repository = $this->createMock(RedirectRepository::class);
$repository
->method('findByRequestPathAndDomain')
->with($requestUrl, '')
->with($request->getPathInfo(), '')
->willReturn($redirect);

$router = new RedirectRouter($repository, $firstDomainConfiguration);

$context = new RequestContext();
$router->setContext($context->fromRequest(Request::create($requestUrl)));
$redirectResult = $router->match($requestUrl);
$router->setContext($context->fromRequest($request));
$redirectResult = $router->match($request->getPathInfo());

if (null !== $expectedRedirectUrl) {
$this->assertSame($expectedRedirectUrl, $redirectResult['path']);
Expand All @@ -65,9 +67,15 @@ public function urlProviderForImprovedRouter(): iterable
yield 'Catch-all wildcard origin redirect' => ['/wildcard/abc', '/target', $this->getRedirect(4, '/wildcard/*', '/target')];
yield 'Wildcard origin and target redirect' => ['/wildcard/abc/def', '/target/abc/def', $this->getRedirect(5, '/wildcard/*', '/target/*')];
yield 'Wildcard origin and target redirect with utf8' => ['/wildcard/tést', '/target/tést', $this->getRedirect(6, '/wildcard/*', '/target/*')];
yield 'Wildcard origin to external target' => ['/wildcard/test', 'https://www.google.com', $this->getRedirect(6, '/wildcard/*', 'https://www.google.com')];
yield 'Fixed origin to external target' => ['/fixed', 'https://www.google.com', $this->getRedirect(6, '/wildcard/*', 'https://www.google.com')];
yield 'Unknown redirect' => ['/unkown-redirect', null, null];
yield 'Wildcard origin to external target' => ['/wildcard/test', 'https://www.google.com', $this->getRedirect(7, '/wildcard/*', 'https://www.google.com')];
yield 'Fixed origin to external target' => ['/fixed', 'https://www.google.com', $this->getRedirect(8, '/wildcard/*', 'https://www.google.com')];
yield 'Unknown redirect' => ['/unknown-redirect', null, null];
yield 'Wildcard origin to external wildcard target' => ['/wildcard/test', 'https://www.google.com/test', $this->getRedirect(9, '/wildcard/*', 'https://www.google.com/*')];
yield 'Wildcard origin to wildcard root target' => ['/test', 'https://www.google.com/test', $this->getRedirect(10, '/*', 'https://www.google.com/*')];
yield 'Wildcard root origin to wildcard root target redirect' => ['/test/abc/def', 'https://www.google.com/test/abc/def', $this->getRedirect(11, '/*', 'https://www.google.com/*')];
yield 'Wildcard root origin to wildcard root target redirect with query params' => ['/test/abc/def?query=test', 'https://www.google.com/test/abc/def?query=test', $this->getRedirect(12, '/*', 'https://www.google.com/*')];
yield 'Wildcard root origin to wildcard root target with root path should not redirect' => ['/', null, $this->getRedirect(13, '/*', 'https://www.google.com/*')];
yield 'Redirect with query params' => ['/test?query=test', 'https://www.google.com/test?query=test', $this->getRedirect(14, '/test', 'https://www.google.com/test')];
}

private function getRedirect(int $id, string $origin, string $target, bool $permanent = false, ?string $domain = null): Redirect
Expand Down

0 comments on commit da28203

Please sign in to comment.