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

Implement the redirect page as page controller #6392

Merged
merged 27 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions core-bundle/config/controller.yaml
Expand Up @@ -167,6 +167,10 @@ services:
arguments:
- '@contao.framework'

Contao\CoreBundle\Controller\Page\RedirectPageController:
arguments:
- '@contao.insert_tag.parser'

Contao\CoreBundle\Controller\Page\RootPageController:
arguments:
- '@?logger'
Expand Down
3 changes: 3 additions & 0 deletions core-bundle/contao/pages/PageRedirect.php
Expand Up @@ -10,9 +10,12 @@

namespace Contao;

use Contao\CoreBundle\Controller\Page\RedirectPageController;
use Contao\CoreBundle\Util\UrlUtil;
use Symfony\Component\HttpFoundation\RedirectResponse;

trigger_deprecation('contao/core-bundle', '5.3', 'Using the "%s" class has been deprecated and will no longer work in Contao 6.0. Use "%s" instead.', PageRedirect::class, RedirectPageController::class);

/**
* Provide methods to handle a redirect page.
*/
Expand Down
38 changes: 38 additions & 0 deletions core-bundle/src/Controller/Page/RedirectPageController.php
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Controller\Page;

use Contao\CoreBundle\DependencyInjection\Attribute\AsPage;
use Contao\CoreBundle\InsertTag\InsertTagParser;
use Contao\CoreBundle\Util\UrlUtil;
use Contao\PageModel;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

#[AsPage('redirect', '')]
class RedirectPageController
{
public function __construct(private readonly InsertTagParser $insertTagParser)
{
}

public function __invoke(Request $request, PageModel $pageModel): Response
{
$status = 'temporary' === $pageModel->redirect ? Response::HTTP_SEE_OTHER : Response::HTTP_MOVED_PERMANENTLY;
$url = $this->insertTagParser->replaceInline($pageModel->url);
$url = UrlUtil::makeAbsolute($url, $request->getSchemeAndHttpHost().$request->getBasePath().'/');

return new RedirectResponse($url, $status);
}
}
4 changes: 0 additions & 4 deletions core-bundle/src/Routing/Page/PageRegistry.php
Expand Up @@ -237,10 +237,6 @@ private function initializePrefixAndSuffix(): void

private function isParameterless(PageModel $pageModel): bool
{
if ('redirect' === $pageModel->type) {
return true;
}

return 'forward' === $pageModel->type && !$pageModel->alwaysForward;
}
}
82 changes: 82 additions & 0 deletions core-bundle/tests/Controller/Page/RedirectPageControllerTest.php
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Tests\Controller\Page;

use Contao\CoreBundle\Controller\Page\RedirectPageController;
use Contao\CoreBundle\InsertTag\InsertTagParser;
use Contao\CoreBundle\Tests\TestCase;
use Contao\PageModel;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RedirectPageControllerTest extends TestCase
{
/**
* @dataProvider getRedirectPages
*/
public function testRedirectsToUrl(string $redirect, string $url, string $redirectUrl, string|null $insertTagResult = null): void
{
$pageModel = $this->mockClassWithProperties(PageModel::class, [
'redirect' => $redirect,
'url' => $url,
]);

$request = Request::create(
'https://example.com/foobar/index.php/foobar',
'GET',
[],
[],
[],
[
'SCRIPT_FILENAME' => '/foobar/index.php',
'SCRIPT_NAME' => '/foobar/index.php',
],
);

$insertTagParser = $this->createMock(InsertTagParser::class);
$insertTagParser
->expects($this->once())
->method('replaceInline')
->willReturnCallback(
static function (string $value) use ($insertTagResult) {
if (null !== $insertTagResult) {
return $insertTagResult;
}

return $value;
},
)
;

$controller = new RedirectPageController($insertTagParser);
$response = $controller($request, $pageModel);

$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame($redirectUrl, $response->getTargetUrl());

if ('permanent' === $redirect) {
$this->assertSame(Response::HTTP_MOVED_PERMANENTLY, $response->getStatusCode());
} else {
$this->assertSame(Response::HTTP_SEE_OTHER, $response->getStatusCode());
}
}

public function getRedirectPages(): \Generator
{
yield ['permanent', '/foobar/index.php/lorem/ipsum', 'https://example.com/foobar/index.php/lorem/ipsum'];
yield ['temporary', '/foobar/index.php/lorem/ipsum', 'https://example.com/foobar/index.php/lorem/ipsum'];
yield ['permanent', 'lorem/ipsum', 'https://example.com/foobar/lorem/ipsum'];
yield ['permanent', '{{link_url::123}}', 'https://example.com/foobar/index.php/lorem/ipsum', '/foobar/index.php/lorem/ipsum'];
}
}
15 changes: 0 additions & 15 deletions core-bundle/tests/Routing/Page/PageRegistryTest.php
Expand Up @@ -88,21 +88,6 @@ public function testReturnsParameteredPageRouteIfTheAlwaysForwardOptionIsSet():
$this->assertSame('(/.+?)?', $route->getRequirement('parameters'));
}

public function testReturnsUnparameteredPageRouteForRedirectPages(): void
{
$pageModel = $this->mockClassWithProperties(PageModel::class);
$pageModel->type = 'redirect';
$pageModel->alias = 'bar';
$pageModel->urlPrefix = 'foo';
$pageModel->urlSuffix = '.baz';

$registry = new PageRegistry($this->createMock(Connection::class));
$route = $registry->getRoute($pageModel);

$this->assertSame('/foo/bar.baz', $route->getPath());
$this->assertNull($route->getDefault('parameters'));
}

/**
* @dataProvider pageRouteWithPathProvider
*/
Expand Down