From e3463507560724259673c2a9674fcb3864a22095 Mon Sep 17 00:00:00 2001 From: Javier Villanueva Date: Sun, 11 Apr 2021 10:44:14 +0100 Subject: [PATCH] Fix error when naming storyblok page "storyblok" --- Controller/Index/Index.php | 5 ++++ Test/Unit/Controller/Index/IndexTest.php | 29 ++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php index ce96cfb..0591dad 100644 --- a/Controller/Index/Index.php +++ b/Controller/Index/Index.php @@ -6,6 +6,7 @@ use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Exception\NotFoundException; use Magento\Framework\App\Action\HttpGetActionInterface; class Index extends Action implements HttpGetActionInterface @@ -26,6 +27,10 @@ public function execute(): ResultInterface { $story = $this->getRequest()->getParam('story', null); + if (!$story) { + throw new NotFoundException(__('Story parameter is missing.')); + } + /** @var Page $resultPage */ $resultPage = $this->pageFactory->create(); $resultPage = $this->setMetaFields($resultPage, $story); diff --git a/Test/Unit/Controller/Index/IndexTest.php b/Test/Unit/Controller/Index/IndexTest.php index e0bd116..f36f1e3 100644 --- a/Test/Unit/Controller/Index/IndexTest.php +++ b/Test/Unit/Controller/Index/IndexTest.php @@ -11,6 +11,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Magento\Framework\View\Result\PageFactory; use MediaLounge\Storyblok\Controller\Index\Index; +use Magento\Framework\Exception\NotFoundException; use Magento\Framework\View\Element\BlockInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; @@ -59,7 +60,7 @@ protected function setUp(): void $layoutMock = $this->getMockForAbstractClass(LayoutInterface::class); $layoutMock - ->expects($this->once()) + ->expects($this->any()) ->method('getBlock') ->with('storyblok.page') ->willReturn($this->blockMock); @@ -78,23 +79,23 @@ protected function setUp(): void $this->configMock = $this->createMock(Config::class); $this->configMock - ->expects($this->atLeastOnce()) + ->expects($this->any()) ->method('getTitle') ->willReturn($this->titleMock); $pageMock = $this->createMock(Page::class); $pageMock - ->expects($this->atLeastOnce()) + ->expects($this->any()) ->method('getConfig') ->willReturn($this->configMock); $pageMock - ->expects($this->atLeastOnce()) + ->expects($this->any()) ->method('getLayout') ->willReturn($layoutMock); $this->pageFactoryMock = $this->createMock(PageFactory::class); $this->pageFactoryMock - ->expects($this->once()) + ->expects($this->any()) ->method('create') ->willReturn($pageMock); @@ -131,6 +132,24 @@ public function testExecute() $controller->execute(); } + public function testMissingStory() + { + $this->expectException(NotFoundException::class); + + $this->requestMock + ->expects($this->atLeastOnce()) + ->method('getParam') + ->with('story', null) + ->willReturn(null); + + $controller = $this->objectManagerHelper->getObject(Index::class, [ + 'context' => $this->contextMock, + 'pageFactory' => $this->pageFactoryMock, + ]); + + $controller->execute(); + } + public function testSetMetaFields() { $fixtureStoryArray = require __DIR__ . '../../../_files/story_with_meta_fields.php';