From 14381cc59d59b155d86c7da07cdcaf68a0674db4 Mon Sep 17 00:00:00 2001 From: Pablo Lozano Date: Thu, 4 Mar 2021 15:25:52 +0100 Subject: [PATCH] Fix preview webspace without theme (#25) --- EventListener/SetThemeEventListener.php | 6 +++++- .../SetThemeEventListenerTest.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/EventListener/SetThemeEventListener.php b/EventListener/SetThemeEventListener.php index ae06cad..118b545 100644 --- a/EventListener/SetThemeEventListener.php +++ b/EventListener/SetThemeEventListener.php @@ -60,7 +60,11 @@ public function setActiveThemeOnRequest(RequestEvent $event): void */ public function setActiveThemeOnPreviewPreRender(PreRenderEvent $event): void { - $theme = $this->themeRepository->findOneByName($event->getAttribute('webspace')->getTheme()); + $themeName = $event->getAttribute('webspace')->getTheme(); + if (null === $themeName) { + return; + } + $theme = $this->themeRepository->findOneByName($themeName); if (null !== $theme) { $this->themeContext->setTheme($theme); } diff --git a/Tests/Unit/EventListener/SetThemeEventListenerTest.php b/Tests/Unit/EventListener/SetThemeEventListenerTest.php index b7b5f5c..47f49a1 100644 --- a/Tests/Unit/EventListener/SetThemeEventListenerTest.php +++ b/Tests/Unit/EventListener/SetThemeEventListenerTest.php @@ -173,4 +173,24 @@ public function testEventListenerOnPreview(): void $this->assertSame($theme->reveal(), $this->themeContext->getTheme()); } + + public function testEventListenerOnPreviewNoTheme(): void + { + /** @var Webspace|ObjectProphecy webspace */ + $webspace = $this->prophesize(Webspace::class); + $webspace->getTheme()->willReturn(null); + + /** @var RequestAttributes|ObjectProphecy $attributes */ + $attributes = $this->prophesize(RequestAttributes::class); + $attributes->getAttribute('webspace', null)->willReturn($webspace->reveal()); + + $this->themeRepository->findOneByName('theme/name') + ->shouldNotBeCalled(); + + $this->listener->setActiveThemeOnPreviewPreRender( + new PreRenderEvent($attributes->reveal()) + ); + + $this->assertNull($this->themeContext->getTheme()); + } }