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

Render the Contao login page even if the current route is not a Contao page #7077

Merged
merged 14 commits into from
May 23, 2024
10 changes: 9 additions & 1 deletion core-bundle/src/EventListener/PrettyErrorScreenListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

use Contao\Config;
use Contao\CoreBundle\Exception\InvalidRequestTokenException;
use Contao\CoreBundle\Exception\NoRootPageFoundException;
use Contao\CoreBundle\Exception\ResponseException;
use Contao\CoreBundle\Exception\RouteParametersException;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\Page\PageRegistry;
use Contao\CoreBundle\Util\LocaleUtil;
use Contao\Frontend;
use Contao\PageModel;
use Contao\StringUtil;
use Symfony\Component\HttpFoundation\AcceptHeader;
Expand Down Expand Up @@ -149,7 +151,13 @@ private function renderErrorScreenByType(int $type, ExceptionEvent $event): void
$pageModel = $request->attributes->get('pageModel');

if (!$pageModel instanceof PageModel) {
return;
// The try/catch must not be merged upstream to Contao 5!
try {
$frontendAdapter = $this->framework->getAdapter(Frontend::class);
$pageModel = $frontendAdapter->getRootPageFromUrl();
} catch (NoRootPageFoundException $exception) {
return;
}
}

$pageAdapter = $this->framework->getAdapter(PageModel::class);
Expand Down
18 changes: 18 additions & 0 deletions core-bundle/tests/EventListener/PrettyErrorScreenListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
use Contao\CoreBundle\Exception\InsufficientAuthenticationException;
use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\CoreBundle\Exception\InternalServerErrorHttpException;
use Contao\CoreBundle\Exception\NoRootPageFoundException;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Exception\ResponseException;
use Contao\CoreBundle\Exception\ServiceUnavailableException;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\Page\PageRegistry;
use Contao\CoreBundle\Routing\Page\PageRoute;
use Contao\CoreBundle\Tests\TestCase;
use Contao\Frontend;
use Contao\PageModel;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -397,6 +400,21 @@ public function testDoesNotLogUnloggableExceptions(): void
$this->assertSame(500, $event->getResponse()->getStatusCode());
}

protected function mockContaoFramework(array $adapters = []): ContaoFramework
{
if (!isset($adapters[Frontend::class])) {
$frontendAdapter = $this->mockAdapter(['getRootPageFromUrl']);
$frontendAdapter
->method('getRootPageFromUrl')
->willThrowException(new NoRootPageFoundException())
;

$adapters[Frontend::class] = $frontendAdapter;
}

return parent::mockContaoFramework($adapters);
}

private function getListener(bool $isBackendUser = false, bool $expectLogging = false, Environment $twig = null, PageModel $errorPage = null, HttpKernelInterface $httpKernel = null): PrettyErrorScreenListener
{
$twig ??= $this->createMock(Environment::class);
Expand Down