From 312f7ea498f9642ea20c0c217840a73ab23f1739 Mon Sep 17 00:00:00 2001
From: Vidar Langseid
Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.
-EOF; - - throw new Exception($message, 0, $e); - } else { - throw $e; + } catch (APINotFoundException $e) { + $message = sprintf('Location (%s) not found or not available in requested language (%s)', $location->id, $language); + $this->logger->warning( + sprintf('%s %s', $message, 'when loading the preview page'), + ['exception' => $e] + ); + if ($this->debugMode) { + throw new BadStateException('Preview page', $message, $e); } + + return new Response($message); + } catch (Exception $e) { + return $this->buildResponseForGenericPreviewError($location, $content, $e); } $response->headers->addCacheControlDirective('no-cache', true); $response->setPrivate(); @@ -187,6 +201,41 @@ protected function getForwardRequest( $forwardRequestParameters ); } + + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + */ + private function buildResponseForGenericPreviewError(Location $location, Content $content, Exception $e): Response + { + $message = ''; + try { + if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) { + $message = <<Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.
+ EOF; + } + } catch (Exception $innerException) { + $message = 'An exception occurred when handling page preview exception'; + $this->logger->warning( + 'Unable to check if location uses a custom controller when loading the preview page', + ['exception' => $innerException] + ); + } + + $this->logger->warning('Unable to load the preview page', ['exception' => $e]); + + $message .= <<See logs for more information
+EOF; + + if ($this->debugMode) { + throw new BadStateException('Preview page', $message, $e); + } + + return new Response($message); + } } class_alias(PreviewController::class, 'eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController'); diff --git a/tests/lib/MVC/Symfony/Controller/Controller/Content/PreviewControllerTest.php b/tests/lib/MVC/Symfony/Controller/Controller/Content/PreviewControllerTest.php index 04eee4c2c1..7040853152 100644 --- a/tests/lib/MVC/Symfony/Controller/Controller/Content/PreviewControllerTest.php +++ b/tests/lib/MVC/Symfony/Controller/Controller/Content/PreviewControllerTest.php @@ -12,6 +12,7 @@ use Ibexa\Contracts\Core\Repository\LocationService; use Ibexa\Contracts\Core\Repository\Values\Content\Content; use Ibexa\Contracts\Core\Repository\Values\Content\Location; +use Ibexa\Core\Base\Exceptions\NotFoundException; use Ibexa\Core\Base\Exceptions\UnauthorizedException; use Ibexa\Core\Helper\ContentPreviewHelper; use Ibexa\Core\Helper\PreviewLocationProvider; @@ -19,6 +20,7 @@ use Ibexa\Core\MVC\Symfony\SiteAccess; use Ibexa\Core\MVC\Symfony\View\CustomLocationControllerChecker; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -51,6 +53,9 @@ final class PreviewControllerTest extends TestCase /** @var \Ibexa\Core\MVC\Symfony\View\CustomLocationControllerChecker&\PHPUnit\Framework\MockObject\MockObject */ protected CustomLocationControllerChecker $controllerChecker; + /** @var \Psr\Log\LoggerInterface&\PHPUnit\Framework\MockObject\MockObject */ + private LoggerInterface $logger; + protected function setUp(): void { parent::setUp(); @@ -62,6 +67,7 @@ protected function setUp(): void $this->authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); $this->locationProvider = $this->createMock(PreviewLocationProvider::class); $this->controllerChecker = $this->createMock(CustomLocationControllerChecker::class); + $this->logger = $this->createMock(LoggerInterface::class); } protected function getPreviewController(): PreviewController @@ -73,7 +79,9 @@ protected function getPreviewController(): PreviewController $this->previewHelper, $this->authorizationChecker, $this->locationProvider, - $this->controllerChecker + $this->controllerChecker, + false, + $this->logger ); } @@ -128,6 +136,42 @@ public function testPreviewCanUserFail(): void $controller->previewContentAction(new Request(), $contentId, $versionNo, $lang, 'test'); } + public function testPreviewWithLogMessage(): void + { + $controller = $this->getPreviewController(); + $contentId = 123; + $lang = 'eng-GB'; + $versionNo = 3; + $content = $this->createMock(Content::class); + + $location = $this->createMock(Location::class); + $location->method('__get')->with('id')->willReturn('42'); + + $siteAccess = $this->createMock(SiteAccess::class); + $this->locationProvider + ->method('loadMainLocationByContent') + ->with($content) + ->willReturn($location) + ; + $this->contentService + ->method('loadContent') + ->with($contentId, [$lang], $versionNo) + ->willReturn($content) + ; + + $this->authorizationChecker->method('isGranted')->willReturn(true); + $siteAccess->name = 'test'; + $this->previewHelper->method('getOriginalSiteAccess')->willReturn($siteAccess); + $this->httpKernel->method('handle')->willThrowException(new NotFoundException('Foo Property', 'foobar')); + + $this->logger + ->expects(self::once()) + ->method('warning') + ->with('Location (42) not found or not available in requested language (eng-GB) when loading the preview page'); + + $controller->previewContentAction(new Request(), $contentId, $versionNo, $lang, 'test'); + } + /** * @return iterable