Skip to content

Commit

Permalink
Fix error when naming storyblok page "storyblok"
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Villanueva committed Apr 11, 2021
1 parent d7f145c commit e346350
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
29 changes: 24 additions & 5 deletions Test/Unit/Controller/Index/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit e346350

Please sign in to comment.