Skip to content

Commit 3b60cd8

Browse files
author
Oleksandr Osadchyi
committed
Merge branch 'MAGETWO-58796' into BUGS
2 parents 26a249d + 6cf87a5 commit 3b60cd8

File tree

5 files changed

+358
-40
lines changed

5 files changed

+358
-40
lines changed

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

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: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,21 @@ class Topmenu extends Template implements IdentityInterface
3434
* Core registry
3535
*
3636
* @var Registry
37+
*
38+
* @deprecated
3739
*/
3840
protected $registry;
3941

42+
/**
43+
* @var NodeFactory
44+
*/
45+
private $nodeFactory;
46+
47+
/**
48+
* @var TreeFactory
49+
*/
50+
private $treeFactory;
51+
4052
/**
4153
* @param Template\Context $context
4254
* @param NodeFactory $nodeFactory
@@ -50,13 +62,8 @@ public function __construct(
5062
array $data = []
5163
) {
5264
parent::__construct($context, $data);
53-
$this->_menu = $nodeFactory->create(
54-
[
55-
'data' => [],
56-
'idField' => 'root',
57-
'tree' => $treeFactory->create()
58-
]
59-
);
65+
$this->nodeFactory = $nodeFactory;
66+
$this->treeFactory = $treeFactory;
6067
}
6168

6269
/**
@@ -81,18 +88,18 @@ public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit =
8188
{
8289
$this->_eventManager->dispatch(
8390
'page_block_html_topmenu_gethtml_before',
84-
['menu' => $this->_menu, 'block' => $this]
91+
['menu' => $this->getMenu(), 'block' => $this, 'request' => $this->getRequest()]
8592
);
8693

87-
$this->_menu->setOutermostClass($outermostClass);
88-
$this->_menu->setChildrenWrapClass($childrenWrapClass);
94+
$this->getMenu()->setOutermostClass($outermostClass);
95+
$this->getMenu()->setChildrenWrapClass($childrenWrapClass);
8996

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

9299
$transportObject = new \Magento\Framework\DataObject(['html' => $html]);
93100
$this->_eventManager->dispatch(
94101
'page_block_html_topmenu_gethtml_after',
95-
['menu' => $this->_menu, 'transportObject' => $transportObject]
102+
['menu' => $this->getMenu(), 'transportObject' => $transportObject]
96103
);
97104
$html = $transportObject->getHtml();
98105
return $html;
@@ -370,10 +377,22 @@ protected function getCacheTags()
370377
/**
371378
* Get menu object.
372379
*
380+
* Creates \Magento\Framework\Data\Tree\Node root node object.
381+
* The creation logic was moved from class constructor into separate method.
382+
*
373383
* @return Node
374384
*/
375385
public function getMenu()
376386
{
387+
if (!$this->_menu) {
388+
$this->_menu = $this->nodeFactory->create(
389+
[
390+
'data' => [],
391+
'idField' => 'root',
392+
'tree' => $this->treeFactory->create()
393+
]
394+
);
395+
}
377396
return $this->_menu;
378397
}
379398
}

0 commit comments

Comments
 (0)