Skip to content

Commit 614fb69

Browse files
ENGCOM-7133: Implement ActionInterface for \cms/page/view\ #27298
- Merge Pull Request #27298 from lbajsarowicz/magento2:refactor/cms-page-view - Merged commits: 1. f49060b 2. 4fd4f99 3. 4a7adeb
2 parents be49d90 + 4a7adeb commit 614fb69

File tree

3 files changed

+92
-54
lines changed

3 files changed

+92
-54
lines changed
Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,78 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\Cms\Controller\Page;
89

9-
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Cms\Helper\Page as PageHelper;
1011
use Magento\Framework\App\Action\HttpGetActionInterface;
11-
use Magento\Framework\App\Action\Action;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\Controller\Result\ForwardFactory;
15+
use Magento\Framework\Controller\ResultInterface;
1216

1317
/**
1418
* Custom page for storefront. Needs to be accessible by POST because of the store switching.
1519
*/
16-
class View extends Action implements HttpGetActionInterface, HttpPostActionInterface
20+
class View implements HttpGetActionInterface, HttpPostActionInterface
1721
{
1822
/**
19-
* @var \Magento\Framework\Controller\Result\ForwardFactory
23+
* @var ForwardFactory
2024
*/
2125
protected $resultForwardFactory;
2226

2327
/**
24-
* @param \Magento\Framework\App\Action\Context $context
25-
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
28+
* @var RequestInterface
29+
*/
30+
private $request;
31+
32+
/**
33+
* @var PageHelper
34+
*/
35+
private $pageHelper;
36+
37+
/**
38+
* @param RequestInterface $request
39+
* @param PageHelper $pageHelper
40+
* @param ForwardFactory $resultForwardFactory
2641
*/
2742
public function __construct(
28-
\Magento\Framework\App\Action\Context $context,
29-
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
43+
RequestInterface $request,
44+
PageHelper $pageHelper,
45+
ForwardFactory $resultForwardFactory
3046
) {
47+
$this->request = $request;
48+
$this->pageHelper = $pageHelper;
3149
$this->resultForwardFactory = $resultForwardFactory;
32-
parent::__construct($context);
3350
}
3451

3552
/**
3653
* View CMS page action
3754
*
38-
* @return \Magento\Framework\Controller\ResultInterface
55+
* @return ResultInterface
3956
*/
4057
public function execute()
4158
{
42-
$pageId = $this->getRequest()->getParam('page_id', $this->getRequest()->getParam('id', false));
43-
$resultPage = $this->_objectManager->get(\Magento\Cms\Helper\Page::class)->prepareResultPage($this, $pageId);
59+
$resultPage = $this->pageHelper->prepareResultPage($this, $this->getPageId());
4460
if (!$resultPage) {
4561
$resultForward = $this->resultForwardFactory->create();
4662
return $resultForward->forward('noroute');
4763
}
4864
return $resultPage;
4965
}
66+
67+
/**
68+
* Returns Page ID if provided or null
69+
*
70+
* @return int|null
71+
*/
72+
private function getPageId(): ?int
73+
{
74+
$id = $this->request->getParam('page_id') ?? $this->request->getParam('id');
75+
76+
return $id ? (int)$id : null;
77+
}
5078
}

app/code/Magento/Cms/Helper/Page.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
use Magento\Cms\Model\Page\CustomLayoutManagerInterface;
99
use Magento\Cms\Model\Page\CustomLayoutRepositoryInterface;
1010
use Magento\Cms\Model\Page\IdentityMap;
11-
use Magento\Framework\App\Action\Action;
11+
use Magento\Framework\App\ActionInterface;
12+
use Magento\Framework\App\Helper\AbstractHelper;
1213
use Magento\Framework\App\ObjectManager;
1314
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\View\Result\Page as ResultPage;
1416

1517
/**
1618
* CMS Page Helper
19+
*
1720
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1821
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1922
* @SuppressWarnings(PHPMD.NPathComplexity)
2023
*/
21-
class Page extends \Magento\Framework\App\Helper\AbstractHelper
24+
class Page extends AbstractHelper
2225
{
2326
/**
2427
* CMS no-route config path
@@ -146,14 +149,14 @@ public function __construct(
146149
/**
147150
* Return result CMS page
148151
*
149-
* @param Action $action
152+
* @param ActionInterface $action
150153
* @param int $pageId
151-
* @return \Magento\Framework\View\Result\Page|bool
154+
* @return ResultPage|bool
152155
*/
153-
public function prepareResultPage(Action $action, $pageId = null)
156+
public function prepareResultPage(ActionInterface $action, $pageId = null)
154157
{
155158
if ($pageId !== null && $pageId !== $this->_page->getId()) {
156-
$delimiterPosition = strrpos($pageId, '|');
159+
$delimiterPosition = strrpos((string)$pageId, '|');
157160
if ($delimiterPosition) {
158161
$pageId = substr($pageId, 0, $delimiterPosition);
159162
}
@@ -180,7 +183,7 @@ public function prepareResultPage(Action $action, $pageId = null)
180183
$this->_design->setDesignTheme($this->_page->getCustomTheme());
181184
}
182185
}
183-
/** @var \Magento\Framework\View\Result\Page $resultPage */
186+
/** @var ResultPage $resultPage */
184187
$resultPage = $this->resultPageFactory->create();
185188
$this->setLayoutType($inRange, $resultPage);
186189
$resultPage->addHandle('cms_page_view');
@@ -247,8 +250,8 @@ public function getPageUrl($pageId = null)
247250
* Set layout type
248251
*
249252
* @param bool $inRange
250-
* @param \Magento\Framework\View\Result\Page $resultPage
251-
* @return \Magento\Framework\View\Result\Page
253+
* @param ResultPage $resultPage
254+
* @return ResultPage
252255
*/
253256
protected function setLayoutType($inRange, $resultPage)
254257
{

app/code/Magento/Cms/Test/Unit/Controller/Page/ViewTest.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,80 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Cms\Test\Unit\Controller\Page;
79

8-
class ViewTest extends \PHPUnit\Framework\TestCase
10+
use Magento\Cms\Controller\Page\View;
11+
use Magento\Cms\Helper\Page as PageHelper;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Controller\Result\Forward;
14+
use Magento\Framework\Controller\Result\ForwardFactory;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
16+
use Magento\Framework\View\Result\Page;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class ViewTest extends TestCase
921
{
22+
private const STUB_PAGE_ID = 2;
23+
1024
/**
11-
* @var \Magento\Cms\Controller\Page\View
25+
* @var View
1226
*/
1327
protected $controller;
1428

1529
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
30+
* @var MockObject
1731
*/
18-
protected $cmsHelperMock;
32+
protected $pageHelperMock;
1933

2034
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
35+
* @var MockObject|RequestInterface
2236
*/
2337
protected $requestMock;
2438

2539
/**
26-
* @var \Magento\Framework\Controller\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
40+
* @var MockObject|ForwardFactory
2741
*/
2842
protected $forwardFactoryMock;
2943

3044
/**
31-
* @var \Magento\Framework\Controller\Result\Forward|\PHPUnit_Framework_MockObject_MockObject
45+
* @var MockObject|Forward
3246
*/
3347
protected $forwardMock;
3448

3549
/**
36-
* @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
50+
* @var MockObject|Page
3751
*/
3852
protected $resultPageMock;
3953

40-
/**
41-
* @var string
42-
*/
43-
protected $pageId = '2';
44-
4554
protected function setUp()
4655
{
47-
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48-
$objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
49-
$responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
50-
$this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
56+
$objectManager = new ObjectManagerHelper($this);
57+
58+
$this->resultPageMock = $this->getMockBuilder(Page::class)
5159
->disableOriginalConstructor()
5260
->getMock();
53-
$this->forwardFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\ForwardFactory::class)
61+
$this->forwardFactoryMock = $this->getMockBuilder(ForwardFactory::class)
5462
->setMethods(['create'])
5563
->disableOriginalConstructor()
5664
->getMock();
57-
$this->forwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class)
65+
$this->forwardMock = $this->getMockBuilder(Forward::class)
5866
->disableOriginalConstructor()
5967
->getMock();
6068
$this->forwardFactoryMock->expects($this->any())
6169
->method('create')
6270
->willReturn($this->forwardMock);
6371

64-
$this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
65-
$this->cmsHelperMock = $this->createMock(\Magento\Cms\Helper\Page::class);
66-
$objectManagerMock->expects($this->once())->method('get')->willReturn($this->cmsHelperMock);
67-
$this->controller = $helper->getObject(
68-
\Magento\Cms\Controller\Page\View::class,
72+
$this->requestMock = $this->createMock(RequestInterface::class);
73+
$this->pageHelperMock = $this->createMock(PageHelper::class);
74+
75+
$this->controller = $objectManager->getObject(
76+
View::class,
6977
[
70-
'response' => $responseMock,
71-
'objectManager' => $objectManagerMock,
7278
'request' => $this->requestMock,
79+
'pageHelper' => $this->pageHelperMock,
7380
'resultForwardFactory' => $this->forwardFactoryMock
7481
]
7582
);
@@ -81,13 +88,13 @@ public function testExecuteResultPage()
8188
->method('getParam')
8289
->willReturnMap(
8390
[
84-
['page_id', $this->pageId, $this->pageId],
85-
['id', false, $this->pageId]
91+
['page_id', null, self::STUB_PAGE_ID],
92+
['id', null, self::STUB_PAGE_ID]
8693
]
8794
);
88-
$this->cmsHelperMock->expects($this->once())
95+
$this->pageHelperMock->expects($this->once())
8996
->method('prepareResultPage')
90-
->with($this->controller, $this->pageId)
97+
->with($this->controller, self::STUB_PAGE_ID)
9198
->willReturn($this->resultPageMock);
9299
$this->assertSame($this->resultPageMock, $this->controller->execute());
93100
}
@@ -98,8 +105,8 @@ public function testExecuteResultForward()
98105
->method('getParam')
99106
->willReturnMap(
100107
[
101-
['page_id', $this->pageId, $this->pageId],
102-
['id', false, $this->pageId]
108+
['page_id', null, self::STUB_PAGE_ID],
109+
['id', null, self::STUB_PAGE_ID]
103110
]
104111
);
105112
$this->forwardMock->expects($this->once())

0 commit comments

Comments
 (0)