Skip to content

Commit

Permalink
Do not redirect to the custom 404 page. Just show the content
Browse files Browse the repository at this point in the history
  • Loading branch information
escopecz committed Nov 26, 2024
1 parent 5d12dbe commit 14db05f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/bundles/CoreBundle/Controller/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,22 @@ public function accessDenied($batch = false, $msg = 'mautic.core.url.error.401')
public function notFound($msg = 'mautic.core.url.error.404')
{
$request = $this->getCurrentRequest();

$page_404 = $this->coreParametersHelper->get('404_page');
if (!empty($page_404)) {
$page404 = $this->coreParametersHelper->get('404_page');
if (!empty($page404)) {
$pageModel = $this->getModel('page');
\assert($pageModel instanceof PageModel);
$page = $pageModel->getEntity($page_404);
$page = $pageModel->getEntity($page404);
if (!empty($page) && $page->getIsPublished() && !empty($page->getCustomHtml())) {
$slug = $pageModel->generateSlug($page);
$slug = $pageModel->generateSlug($page);
$response = $this->forward(
'MauticPageBundle:Public:index',
[
'slug' => $slug,
'ignore_mismatch' => true,
]
);

return $this->redirectToRoute('mautic_page_public', ['slug' => $slug]);
return new Response($response->getContent(), Response::HTTP_NOT_FOUND, $response->headers->all());
}
}

Expand Down
39 changes: 39 additions & 0 deletions app/bundles/PageBundle/Tests/Controller/NotFoundFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Mautic\PageBundle\Tests\Controller;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\PageBundle\Entity\Page;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class NotFoundFunctionalTest extends MauticMysqlTestCase
{
protected $useCleanupRollback = false;

public function testCustom404Page(): void
{
// Create a custom 404 page:
$notFoundPage = new Page();
$notFoundPage->setTitle('404 Not Found');
$notFoundPage->setAlias('404-not-found');
$notFoundPage->setCustomHtml('<html><body>Custom 404 Not Found Page</body></html>');

$this->em->persist($notFoundPage);
$this->em->flush();

// Configure the 404 page:
$this->configParams['404_page'] = $notFoundPage->getId();
parent::setUpSymfony($this->configParams);

// Test the custom 404 page:
$crawler = $this->client->request(Request::METHOD_GET, '/s/page-that-does-not-exist');
Assert::assertSame(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
Assert::assertStringContainsString('Custom 404 Not Found Page', $crawler->text());
Assert::assertFalse($this->client->getResponse()->isRedirection(), 'The response should not be a redirect.');
Assert::assertSame('/s/page-that-does-not-exist', $this->client->getRequest()->getRequestUri(), 'The request URI should be the same as the original URI.');
}
}

0 comments on commit 14db05f

Please sign in to comment.