Skip to content

Commit 1222721

Browse files
author
Sergey Semenov
committed
MAGETWO-58796: Add request parameter to CMS event observer
1 parent 6868558 commit 1222721

File tree

6 files changed

+356
-41
lines changed

6 files changed

+356
-41
lines changed

app/code/Magento/Cms/Controller/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function match(\Magento\Framework\App\RequestInterface $request)
9696
$condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);
9797
$this->_eventManager->dispatch(
9898
'cms_controller_router_match_before',
99-
['router' => $this, 'condition' => $condition]
99+
['router' => $this, 'condition' => $condition, 'request' => $request]
100100
);
101101
$identifier = $condition->getIdentifier();
102102

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function prepareResultPage(Action $action, $pageId = null)
156156

157157
$this->_eventManager->dispatch(
158158
'cms_page_render',
159-
['page' => $this->_page, 'controller_action' => $action]
159+
['page' => $this->_page, 'controller_action' => $action, 'request' => $this->_getRequest()]
160160
);
161161

162162
if ($this->_page->getCustomLayoutUpdateXml() && $inRange) {
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Copyright © 2017 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cms\Test\Unit\Controller;
7+
8+
class RouterTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Cms\Controller\Router
12+
*/
13+
private $router;
14+
15+
/**
16+
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $eventManagerMock;
19+
20+
/**
21+
* @var \Magento\Cms\Model\PageFactory|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $pageFactoryMock;
24+
25+
/**
26+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $storeManagerMock;
29+
30+
/**
31+
* @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $storeMock;
34+
35+
/**
36+
* @var \Magento\Framework\App\ActionFactory|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $actionFactoryMock;
39+
40+
protected function setUp()
41+
{
42+
$this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
43+
->getMockForAbstractClass();
44+
45+
$this->pageFactoryMock = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
46+
->disableOriginalConstructor()
47+
->setMethods(['create'])
48+
->getMock();
49+
50+
$this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
51+
->getMockForAbstractClass();
52+
53+
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
54+
->getMockForAbstractClass();
55+
$this->storeManagerMock->expects($this->any())
56+
->method('getStore')
57+
->willReturn($this->storeMock);
58+
59+
$this->actionFactoryMock = $this->getMockBuilder(\Magento\Framework\App\ActionFactory::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
63+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
64+
$this->router = $objectManagerHelper->getObject(
65+
\Magento\Cms\Controller\Router::class,
66+
[
67+
'eventManager' => $this->eventManagerMock,
68+
'pageFactory' => $this->pageFactoryMock,
69+
'storeManager' => $this->storeManagerMock,
70+
'actionFactory' => $this->actionFactoryMock,
71+
]
72+
);
73+
}
74+
75+
public function testMatchCmsControllerRouterMatchBeforeEventParams()
76+
{
77+
$identifier = '/test';
78+
$trimedIdentifier = 'test';
79+
$pageId = 1;
80+
$storeId = 1;
81+
82+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject $requestMock */
83+
$requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
84+
->setMethods([
85+
'getPathInfo',
86+
'setModuleName',
87+
'setControllerName',
88+
'setActionName',
89+
'setParam',
90+
'setAlias',
91+
])
92+
->getMockForAbstractClass();
93+
$requestMock->expects($this->once())
94+
->method('getPathInfo')
95+
->willReturn($identifier);
96+
$requestMock->expects($this->once())
97+
->method('setModuleName')
98+
->with('cms')
99+
->willReturnSelf();
100+
$requestMock->expects($this->once())
101+
->method('setControllerName')
102+
->with('page')
103+
->willReturnSelf();
104+
$requestMock->expects($this->once())
105+
->method('setActionName')
106+
->with('view')
107+
->willReturnSelf();
108+
$requestMock->expects($this->once())
109+
->method('setParam')
110+
->with('page_id', $pageId)
111+
->willReturnSelf();
112+
$requestMock->expects($this->once())
113+
->method('setAlias')
114+
->with(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $trimedIdentifier)
115+
->willReturnSelf();
116+
117+
$condition = new \Magento\Framework\DataObject(['identifier' => $trimedIdentifier, 'continue' => true]);
118+
119+
$this->eventManagerMock->expects($this->once())
120+
->method('dispatch')
121+
->with(
122+
'cms_controller_router_match_before',
123+
[
124+
'router' => $this->router,
125+
'condition' => $condition,
126+
'request' => $requestMock,
127+
]
128+
)
129+
->willReturnSelf();
130+
131+
$pageMock = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
132+
->disableOriginalConstructor()
133+
->getMock();
134+
$pageMock->expects($this->once())
135+
->method('checkIdentifier')
136+
->with($trimedIdentifier, $storeId)
137+
->willReturn($pageId);
138+
139+
$this->pageFactoryMock->expects($this->once())
140+
->method('create')
141+
->willReturn($pageMock);
142+
143+
$this->storeMock->expects($this->once())
144+
->method('getId')
145+
->willReturn($storeId);
146+
147+
$actionMock = $this->getMockBuilder(\Magento\Framework\App\ActionInterface::class)
148+
->getMockForAbstractClass();
149+
150+
$this->actionFactoryMock->expects($this->once())
151+
->method('create')
152+
->with(\Magento\Framework\App\Action\Forward::class)
153+
->willReturn($actionMock);
154+
155+
$this->assertEquals($actionMock, $this->router->match($requestMock));
156+
}
157+
}

app/code/Magento/Cms/Test/Unit/Helper/PageTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ class PageTest extends \PHPUnit_Framework_TestCase
113113
*/
114114
protected $resultPageFactory;
115115

116+
/**
117+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
118+
*/
119+
private $httpRequestMock;
120+
116121
protected function setUp()
117122
{
118123
$this->actionMock = $this->getMockBuilder(\Magento\Framework\App\Action\Action::class)
@@ -157,6 +162,8 @@ protected function setUp()
157162
->getMockForAbstractClass();
158163
$this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
159164
->getMockForAbstractClass();
165+
$this->httpRequestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
166+
->getMockForAbstractClass();
160167
$this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
161168
->disableOriginalConstructor()
162169
->getMock();
@@ -184,7 +191,8 @@ protected function setUp()
184191
\Magento\Framework\App\Helper\Context::class,
185192
[
186193
'eventManager' => $this->eventManagerMock,
187-
'urlBuilder' => $this->urlBuilderMock
194+
'urlBuilder' => $this->urlBuilderMock,
195+
'httpRequest' => $this->httpRequestMock,
188196
]
189197
);
190198

@@ -318,7 +326,8 @@ public function testPrepareResultPage(
318326
'cms_page_render',
319327
[
320328
'page' => $this->pageMock,
321-
'controller_action' => $this->actionMock
329+
'controller_action' => $this->actionMock,
330+
'request' => $this->httpRequestMock,
322331
]
323332
);
324333
$this->pageMock->expects($this->any())

app/code/Magento/Theme/Block/Html/Topmenu.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ class Topmenu extends Template implements IdentityInterface
3333
/**
3434
* Core registry
3535
*
36+
* Deprecated since not used in this class.
37+
*
3638
* @var Registry
39+
*
40+
* @deprecated
3741
*/
3842
protected $registry;
3943

44+
/**
45+
* @var NodeFactory
46+
*/
47+
private $nodeFactory;
48+
49+
/**
50+
* @var TreeFactory
51+
*/
52+
private $treeFactory;
53+
4054
/**
4155
* @param Template\Context $context
4256
* @param NodeFactory $nodeFactory
@@ -50,13 +64,8 @@ public function __construct(
5064
array $data = []
5165
) {
5266
parent::__construct($context, $data);
53-
$this->_menu = $nodeFactory->create(
54-
[
55-
'data' => [],
56-
'idField' => 'root',
57-
'tree' => $treeFactory->create()
58-
]
59-
);
67+
$this->nodeFactory = $nodeFactory;
68+
$this->treeFactory = $treeFactory;
6069
}
6170

6271
/**
@@ -81,18 +90,18 @@ public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit =
8190
{
8291
$this->_eventManager->dispatch(
8392
'page_block_html_topmenu_gethtml_before',
84-
['menu' => $this->_menu, 'block' => $this]
93+
['menu' => $this->getMenu(), 'block' => $this, 'request' => $this->getRequest()]
8594
);
8695

87-
$this->_menu->setOutermostClass($outermostClass);
88-
$this->_menu->setChildrenWrapClass($childrenWrapClass);
96+
$this->getMenu()->setOutermostClass($outermostClass);
97+
$this->getMenu()->setChildrenWrapClass($childrenWrapClass);
8998

90-
$html = $this->_getHtml($this->_menu, $childrenWrapClass, $limit);
99+
$html = $this->_getHtml($this->getMenu(), $childrenWrapClass, $limit);
91100

92101
$transportObject = new \Magento\Framework\DataObject(['html' => $html]);
93102
$this->_eventManager->dispatch(
94103
'page_block_html_topmenu_gethtml_after',
95-
['menu' => $this->_menu, 'transportObject' => $transportObject]
104+
['menu' => $this->getMenu(), 'transportObject' => $transportObject]
96105
);
97106
$html = $transportObject->getHtml();
98107
return $html;
@@ -374,6 +383,15 @@ protected function getCacheTags()
374383
*/
375384
public function getMenu()
376385
{
386+
if (!$this->_menu) {
387+
$this->_menu = $this->nodeFactory->create(
388+
[
389+
'data' => [],
390+
'idField' => 'root',
391+
'tree' => $this->treeFactory->create()
392+
]
393+
);
394+
}
377395
return $this->_menu;
378396
}
379397
}

0 commit comments

Comments
 (0)