Skip to content

Commit faa3f70

Browse files
author
Joan He
committed
Merge remote-tracking branch 'origin/MAGETWO-36289' into develop
2 parents 353c35a + 59e4ff7 commit faa3f70

File tree

4 files changed

+308
-16
lines changed

4 files changed

+308
-16
lines changed

app/code/Magento/Cms/Block/Adminhtml/Block/Widget/Chooser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function prepareElementHtml(\Magento\Framework\Data\Form\Element\Abstract
8181
if ($element->getValue()) {
8282
$block = $this->_blockFactory->create()->load($element->getValue());
8383
if ($block->getId()) {
84-
$chooser->setLabel($block->getTitle());
84+
$chooser->setLabel($this->escapeHtml($block->getTitle()));
8585
}
8686
}
8787

app/code/Magento/Cms/Block/Adminhtml/Page/Widget/Chooser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function prepareElementHtml(\Magento\Framework\Data\Form\Element\Abstract
9898
if ($element->getValue()) {
9999
$page = $this->_pageFactory->create()->load((int)$element->getValue());
100100
if ($page->getId()) {
101-
$chooser->setLabel($page->getTitle());
101+
$chooser->setLabel($this->escapeHtml($page->getTitle()));
102102
}
103103
}
104104

app/code/Magento/Cms/Test/Unit/Block/Adminhtml/Block/Widget/ChooserTest.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class ChooserTest extends \PHPUnit_Framework_TestCase
3535
*/
3636
protected $urlBuilderMock;
3737

38+
/**
39+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $escaper;
42+
3843
/**
3944
* @var \Magento\Cms\Model\BlockFactory|\PHPUnit_Framework_MockObject_MockObject
4045
*/
@@ -66,6 +71,14 @@ protected function setUp()
6671
$this->urlBuilderMock = $this->getMockBuilder('Magento\Framework\UrlInterface')
6772
->disableOriginalConstructor()
6873
->getMock();
74+
$this->escaper = $this->getMockBuilder('Magento\Framework\Escaper')
75+
->disableOriginalConstructor()
76+
->setMethods(
77+
[
78+
'escapeHtml',
79+
]
80+
)
81+
->getMock();
6982
$this->blockFactoryMock = $this->getMockBuilder('Magento\Cms\Model\BlockFactory')
7083
->setMethods(
7184
[
@@ -90,6 +103,7 @@ protected function setUp()
90103
[
91104
'getTitle',
92105
'load',
106+
'getId',
93107
]
94108
)
95109
->getMock();
@@ -112,15 +126,16 @@ protected function setUp()
112126
$this->context = $objectManager->getObject(
113127
'Magento\Backend\Block\Template\Context',
114128
[
115-
'layout' => $this->layoutMock,
129+
'layout' => $this->layoutMock,
116130
'mathRandom' => $this->mathRandomMock,
117-
'urlBuilder' => $this->urlBuilderMock
131+
'urlBuilder' => $this->urlBuilderMock,
132+
'escaper' => $this->escaper,
118133
]
119134
);
120135
$this->this = $objectManager->getObject(
121136
'Magento\Cms\Block\Adminhtml\Block\Widget\Chooser',
122137
[
123-
'context' => $this->context,
138+
'context' => $this->context,
124139
'blockFactory' => $this->blockFactoryMock
125140
]
126141
);
@@ -135,13 +150,14 @@ protected function setUp()
135150
*/
136151
public function testPrepareElementHtml($elementValue, $modelBlockId)
137152
{
138-
$elementId = 1;
139-
$uniqId = '126hj4h3j73hk7b347jhkl37gb34';
140-
$sourceUrl = 'cms/block_widget/chooser/126hj4h3j73hk7b347jhkl37gb34';
141-
$config = ['key1' => 'value1'];
142-
$fieldsetId = 2;
143-
$html = 'some html';
144-
$title = 'some title';
153+
$elementId = 1;
154+
$uniqId = '126hj4h3j73hk7b347jhkl37gb34';
155+
$sourceUrl = 'cms/block_widget/chooser/126hj4h3j73hk7b347jhkl37gb34';
156+
$config = ['key1' => 'value1'];
157+
$fieldsetId = 2;
158+
$html = 'some html';
159+
$title = 'some "><img src=y onerror=prompt(document.domain)>; title';
160+
$titleEscaped = 'some &quot;&gt;&lt;img src=y onerror=prompt(document.domain)&gt;; title';
145161

146162
$this->this->setConfig($config);
147163
$this->this->setFieldsetId($fieldsetId);
@@ -197,13 +213,18 @@ public function testPrepareElementHtml($elementValue, $modelBlockId)
197213
$this->modelBlockMock->expects($this->any())
198214
->method('getTitle')
199215
->willReturn($title);
200-
$this->chooserMock->expects($this->any())
201-
->method('setLabel')
202-
->with($title)
203-
->willReturnSelf();
204216
$this->chooserMock->expects($this->atLeastOnce())
205217
->method('toHtml')
206218
->willReturn($html);
219+
if (!empty($elementValue) && !empty($modelBlockId)) {
220+
$this->escaper->expects(($this->atLeastOnce()))
221+
->method('escapeHtml')
222+
->willReturn($titleEscaped);
223+
$this->chooserMock->expects($this->atLeastOnce())
224+
->method('setLabel')
225+
->with($titleEscaped)
226+
->willReturnSelf();
227+
}
207228
$this->elementMock->expects($this->atLeastOnce())
208229
->method('setData')
209230
->with('after_element_html', $html)
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cms\Test\Unit\Block\Adminhtml\Page\Widget;
7+
8+
/**
9+
* @covers \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser
10+
*/
11+
class ChooserTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser
15+
*/
16+
protected $this;
17+
18+
/**
19+
* @var \Magento\Backend\Block\Template\Context
20+
*/
21+
protected $context;
22+
23+
/**
24+
* @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $mathRandomMock;
27+
28+
/**
29+
* @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $urlBuilderMock;
32+
33+
/**
34+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $escaper;
37+
38+
/**
39+
* @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $cmsPageMock;
42+
43+
/**
44+
* @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
protected $layoutMock;
47+
48+
/**
49+
* @var \Magento\Cms\Model\PageFactory|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
protected $pageFactoryMock;
52+
53+
/**
54+
* @var \Magento\Framework\Data\Form\Element\AbstractElement|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
protected $elementMock;
57+
58+
/**
59+
* @var \Magento\Framework\View\Element\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
60+
*/
61+
protected $chooserMock;
62+
63+
protected function setUp()
64+
{
65+
$this->layoutMock = $this->getMockBuilder('Magento\Framework\View\LayoutInterface')
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->mathRandomMock = $this->getMockBuilder('Magento\Framework\Math\Random')
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->urlBuilderMock = $this->getMockBuilder('Magento\Framework\UrlInterface')
72+
->disableOriginalConstructor()
73+
->getMock();
74+
$this->escaper = $this->getMockBuilder('Magento\Framework\Escaper')
75+
->disableOriginalConstructor()
76+
->setMethods(
77+
[
78+
'escapeHtml',
79+
]
80+
)
81+
->getMock();
82+
$this->pageFactoryMock = $this->getMockBuilder('Magento\Cms\Model\PageFactory')
83+
->setMethods(
84+
[
85+
'create',
86+
]
87+
)
88+
->disableOriginalConstructor()
89+
->getMock();
90+
$this->elementMock = $this->getMockBuilder('Magento\Framework\Data\Form\Element\AbstractElement')
91+
->disableOriginalConstructor()
92+
->setMethods(
93+
[
94+
'getId',
95+
'getValue',
96+
'setData',
97+
]
98+
)
99+
->getMock();
100+
$this->cmsPageMock = $this->getMockBuilder('Magento\Cms\Model\Page')
101+
->disableOriginalConstructor()
102+
->setMethods(
103+
[
104+
'getTitle',
105+
'load',
106+
'getId',
107+
]
108+
)
109+
->getMock();
110+
$this->chooserMock = $this->getMockBuilder('Magento\Framework\View\Element\BlockInterface')
111+
->disableOriginalConstructor()
112+
->setMethods(
113+
[
114+
'setElement',
115+
'setConfig',
116+
'setFieldsetId',
117+
'setSourceUrl',
118+
'setUniqId',
119+
'setLabel',
120+
'toHtml',
121+
]
122+
)
123+
->getMock();
124+
125+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
126+
$this->context = $objectManager->getObject(
127+
'Magento\Backend\Block\Template\Context',
128+
[
129+
'layout' => $this->layoutMock,
130+
'mathRandom' => $this->mathRandomMock,
131+
'urlBuilder' => $this->urlBuilderMock,
132+
'escaper' => $this->escaper,
133+
]
134+
);
135+
$this->this = $objectManager->getObject(
136+
'Magento\Cms\Block\Adminhtml\Page\Widget\Chooser',
137+
[
138+
'context' => $this->context,
139+
'pageFactory' => $this->pageFactoryMock
140+
]
141+
);
142+
}
143+
144+
/**
145+
* @covers \Magento\Cms\Block\Adminhtml\Block\Widget\Chooser::prepareElementHtml
146+
*
147+
* @param string $elementValue
148+
* @param integer|null $cmsPageId
149+
*
150+
* @dataProvider prepareElementHtmlDataProvider
151+
*/
152+
public function testPrepareElementHtml($elementValue, $cmsPageId)
153+
{
154+
//$elementValue = 12345;
155+
//$cmsPageId = 1;
156+
$elementId = 1;
157+
$uniqId = '126hj4h3j73hk7b347jhkl37gb34';
158+
$sourceUrl = 'cms/page_widget/chooser/126hj4h3j73hk7b347jhkl37gb34';
159+
$config = ['key1' => 'value1'];
160+
$fieldsetId = 2;
161+
$html = 'some html';
162+
$title = 'some "><img src=y onerror=prompt(document.domain)>; title';
163+
$titleEscaped = 'some &quot;&gt;&lt;img src=y onerror=prompt(document.domain)&gt;; title';
164+
165+
$this->this->setConfig($config);
166+
$this->this->setFieldsetId($fieldsetId);
167+
168+
$this->elementMock->expects($this->atLeastOnce())
169+
->method('getId')
170+
->willReturn($elementId);
171+
$this->mathRandomMock->expects($this->atLeastOnce())
172+
->method('getUniqueHash')
173+
->with($elementId)
174+
->willReturn($uniqId);
175+
$this->urlBuilderMock->expects($this->atLeastOnce())
176+
->method('getUrl')
177+
->with('cms/page_widget/chooser', ['uniq_id' => $uniqId])
178+
->willReturn($sourceUrl);
179+
$this->layoutMock->expects($this->atLeastOnce())
180+
->method('createBlock')
181+
->with('Magento\Widget\Block\Adminhtml\Widget\Chooser')
182+
->willReturn($this->chooserMock);
183+
$this->chooserMock->expects($this->atLeastOnce())
184+
->method('setElement')
185+
->with($this->elementMock)
186+
->willReturnSelf();
187+
$this->chooserMock->expects($this->atLeastOnce())
188+
->method('setConfig')
189+
->with($config)
190+
->willReturnSelf();
191+
$this->chooserMock->expects($this->atLeastOnce())
192+
->method('setFieldsetId')
193+
->with($fieldsetId)
194+
->willReturnSelf();
195+
$this->chooserMock->expects($this->atLeastOnce())
196+
->method('setSourceUrl')
197+
->with($sourceUrl)
198+
->willReturnSelf();
199+
$this->chooserMock->expects($this->atLeastOnce())
200+
->method('setUniqId')
201+
->with($uniqId)
202+
->willReturnSelf();
203+
$this->elementMock->expects($this->atLeastOnce())
204+
->method('getValue')
205+
->willReturn($elementValue);
206+
$this->pageFactoryMock->expects($this->any())
207+
->method('create')
208+
->willReturn($this->cmsPageMock);
209+
$this->cmsPageMock->expects($this->any())
210+
->method('load')
211+
->with((int)$elementValue)
212+
->willReturnSelf();
213+
$this->cmsPageMock->expects($this->any())
214+
->method('getId')
215+
->willReturn($cmsPageId);
216+
$this->cmsPageMock->expects($this->any())
217+
->method('getTitle')
218+
->willReturn($title);
219+
$this->chooserMock->expects($this->atLeastOnce())
220+
->method('toHtml')
221+
->willReturn($html);
222+
if (!empty($elementValue) && !empty($cmsPageId)) {
223+
$this->escaper->expects(($this->atLeastOnce()))
224+
->method('escapeHtml')
225+
->willReturn($titleEscaped);
226+
$this->chooserMock->expects($this->atLeastOnce())
227+
->method('setLabel')
228+
->with($titleEscaped)
229+
->willReturnSelf();
230+
}
231+
$this->elementMock->expects($this->atLeastOnce())
232+
->method('setData')
233+
->with('after_element_html', $html)
234+
->willReturnSelf();
235+
236+
$this->assertEquals($this->elementMock, $this->this->prepareElementHtml($this->elementMock));
237+
}
238+
239+
public function prepareElementHtmlDataProvider()
240+
{
241+
return [
242+
'elementValue NOT EMPTY, modelBlockId NOT EMPTY' => [
243+
'elementValue' => 'some value',
244+
'cmsPageId' => 1,
245+
],
246+
'elementValue NOT EMPTY, modelBlockId IS EMPTY' => [
247+
'elementValue' => 'some value',
248+
'cmsPageId' => null,
249+
],
250+
'elementValue IS EMPTY, modelBlockId NEVER REACHED' => [
251+
'elementValue' => '',
252+
'cmsPageId' => 1,
253+
]
254+
];
255+
}
256+
257+
/**
258+
* @covers \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser::getGridUrl
259+
*/
260+
public function testGetGridUrl()
261+
{
262+
$url = 'some url';
263+
264+
$this->urlBuilderMock->expects($this->atLeastOnce())
265+
->method('getUrl')
266+
->with('cms/page_widget/chooser', ['_current' => true])
267+
->willReturn($url);
268+
269+
$this->assertEquals($url, $this->this->getGridUrl());
270+
}
271+
}

0 commit comments

Comments
 (0)