Skip to content

Commit 5202297

Browse files
author
lestare
committed
MAGETWO-61564: [Backport] - [Github] Can't modify product link widget conditions #6616 - for 2.0
1 parent d385dbb commit 5202297

File tree

6 files changed

+299
-22
lines changed

6 files changed

+299
-22
lines changed

app/code/Magento/CatalogWidget/Block/Product/Widget/Conditions.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ public function __construct(
8282
protected function _construct()
8383
{
8484
$widget = $this->registry->registry('current_widget_instance');
85+
$widgetOptions = $this->getLayout()->getBlock('wysiwyg_widget.options');
86+
8587
if ($widget) {
8688
$widgetParameters = $widget->getWidgetParameters();
87-
if (isset($widgetParameters['conditions'])) {
88-
$this->rule->loadPost($widgetParameters);
89-
}
89+
} elseif ($widgetOptions) {
90+
$widgetParameters = $widgetOptions->getWidgetValues();
91+
}
92+
93+
if (isset($widgetParameters['conditions'])) {
94+
$this->rule->loadPost($widgetParameters);
9095
}
9196
}
9297

app/code/Magento/CatalogWidget/Test/Unit/Controller/Adminhtml/Product/Widget/ConditionsTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1010

11+
/**
12+
* Test class for \Magento\CatalogWidget\Controller\Adminhtml\Product\Widget\Conditions
13+
*/
1114
class ConditionsTest extends \PHPUnit_Framework_TestCase
1215
{
1316
/**
@@ -37,16 +40,16 @@ class ConditionsTest extends \PHPUnit_Framework_TestCase
3740

3841
protected function setUp()
3942
{
40-
$this->rule = $this->getMock('Magento\CatalogWidget\Model\Rule', [], [], '', false);
41-
$this->response = $this->getMockBuilder('\Magento\Framework\App\ResponseInterface')
43+
$this->rule = $this->getMock(\Magento\CatalogWidget\Model\Rule::class, [], [], '', false);
44+
$this->response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
4245
->setMethods(['setBody', 'sendResponse'])
4346
->disableOriginalConstructor()
4447
->getMock();
4548
$this->response->expects($this->once())->method('setBody')->will($this->returnSelf());
4649

4750
$objectManagerHelper = new ObjectManagerHelper($this);
4851
$arguments = $objectManagerHelper->getConstructArguments(
49-
'Magento\CatalogWidget\Controller\Adminhtml\Product\Widget\Conditions',
52+
\Magento\CatalogWidget\Controller\Adminhtml\Product\Widget\Conditions::class,
5053
[
5154
'rule' => $this->rule,
5255
'response' => $this->response
@@ -56,7 +59,7 @@ protected function setUp()
5659

5760
$this->objectManager = $arguments['context']->getObjectManager();
5861
$this->controller = $objectManagerHelper->getObject(
59-
'Magento\CatalogWidget\Controller\Adminhtml\Product\Widget\Conditions',
62+
\Magento\CatalogWidget\Controller\Adminhtml\Product\Widget\Conditions::class,
6063
$arguments
6164
);
6265
}
@@ -69,7 +72,7 @@ public function testExecute()
6972
$this->request->expects($this->at(2))->method('getParam')->with('form')
7073
->will($this->returnValue('request_form_param_value'));
7174

72-
$condition = $this->getMockBuilder('Magento\CatalogWidget\Model\Rule\Condition\Product')
75+
$condition = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\Product::class)
7376
->setMethods([
7477
'setId',
7578
'setType',
@@ -82,7 +85,7 @@ public function testExecute()
8285
->getMock();
8386
$condition->expects($this->once())->method('setId')->with('1--1')->will($this->returnSelf());
8487
$condition->expects($this->once())->method('setType')
85-
->with('Magento\CatalogWidget\Model\Rule\Condition\Product')
88+
->with(\Magento\CatalogWidget\Model\Rule\Condition\Product::class)
8689
->will($this->returnSelf());
8790
$condition->expects($this->once())->method('setRule')->with($this->rule)->will($this->returnSelf());
8891
$condition->expects($this->once())->method('setPrefix')->with('conditions')->will($this->returnSelf());

app/code/Magento/Widget/Controller/Adminhtml/Widget/LoadOptions.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
*/
77
namespace Magento\Widget\Controller\Adminhtml\Widget;
88

9+
use Magento\Framework\App\ObjectManager;
10+
911
class LoadOptions extends \Magento\Backend\App\Action
1012
{
13+
/**
14+
* @var \Magento\Widget\Helper\Conditions
15+
*/
16+
private $conditionsHelper;
17+
1118
/**
1219
* Ajax responder for loading plugin options form
1320
*
@@ -18,13 +25,19 @@ public function execute()
1825
try {
1926
$this->_view->loadLayout();
2027
if ($paramsJson = $this->getRequest()->getParam('widget')) {
21-
$request = $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonDecode($paramsJson);
28+
$request = $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)
29+
->jsonDecode($paramsJson);
2230
if (is_array($request)) {
2331
$optionsBlock = $this->_view->getLayout()->getBlock('wysiwyg_widget.options');
2432
if (isset($request['widget_type'])) {
2533
$optionsBlock->setWidgetType($request['widget_type']);
2634
}
2735
if (isset($request['values'])) {
36+
$request['values'] = array_map('htmlspecialchars_decode', $request['values']);
37+
if (isset($request['values']['conditions_encoded'])) {
38+
$request['values']['conditions'] =
39+
$this->getConditionsHelper()->decode($request['values']['conditions_encoded']);
40+
}
2841
$optionsBlock->setWidgetValues($request['values']);
2942
}
3043
}
@@ -37,4 +50,18 @@ public function execute()
3750
);
3851
}
3952
}
53+
54+
55+
/**
56+
* @return \Magento\Widget\Helper\Conditions
57+
* @deprecated
58+
*/
59+
private function getConditionsHelper()
60+
{
61+
if (!$this->conditionsHelper) {
62+
$this->conditionsHelper = ObjectManager::getInstance()->get(\Magento\Widget\Helper\Conditions::class);
63+
}
64+
65+
return $this->conditionsHelper;
66+
}
4067
}

app/code/Magento/Widget/Model/Widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public function getWidgetDeclaration($type, $params = [], $asIs = true)
294294
}
295295
}
296296
if ($value) {
297-
$directive .= sprintf(' %s="%s"', $name, $value);
297+
$directive .= sprintf(' %s="%s"', $name, $this->escaper->escapeQuote($value));
298298
}
299299
}
300300
$directive .= '}}';
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Widget\Test\Unit\Controller\Adminhtml\Widget;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
9+
use Magento\Widget\Controller\Adminhtml\Widget\LoadOptions;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Framework\App\ViewInterface;
12+
use Magento\Widget\Helper\Conditions as ConditionsHelper;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\App\ResponseInterface;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Framework\App\RequestInterface;
17+
18+
/**
19+
* Test class for \Magento\Widget\Controller\Adminhtml\Widget\LoadOptions
20+
*/
21+
class LoadOptionsTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @var ObjectManagerHelper
25+
*/
26+
private $objectManagerHelper;
27+
28+
/**
29+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $contextMock;
32+
33+
/**
34+
* @var ViewInterface|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $viewMock;
37+
38+
/**
39+
* @var ConditionsHelper|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $conditionsHelperMock;
42+
43+
/**
44+
* @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $responseMock;
47+
48+
/**
49+
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
private $objectManagerMock;
52+
53+
/**
54+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
private $requestMock;
57+
58+
/**
59+
* @var LoadOptions
60+
*/
61+
private $loadOptions;
62+
63+
/**
64+
* return void
65+
*/
66+
protected function setUp()
67+
{
68+
$this->objectManagerHelper = new ObjectManagerHelper($this);
69+
$this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
70+
$this->viewMock = $this->getMockForAbstractClass(ViewInterface::class);
71+
$this->requestMock = $this->getMockForAbstractClass(RequestInterface::class);
72+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
73+
->setMethods(['representJson'])
74+
->getMockForAbstractClass();
75+
$this->contextMock = $this->getMockBuilder(Context::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->contextMock->expects($this->once())
79+
->method('getView')
80+
->willReturn($this->viewMock);
81+
$this->contextMock->expects($this->once())
82+
->method('getRequest')
83+
->willReturn($this->requestMock);
84+
$this->contextMock->expects($this->once())
85+
->method('getResponse')
86+
->willReturn($this->responseMock);
87+
$this->contextMock->expects($this->once())
88+
->method('getObjectManager')
89+
->willReturn($this->objectManagerMock);
90+
$this->conditionsHelperMock = $this->getMockBuilder(ConditionsHelper::class)
91+
->disableOriginalConstructor()
92+
->getMock();
93+
94+
$this->loadOptions = $this->objectManagerHelper->getObject(
95+
LoadOptions::class,
96+
['context' => $this->contextMock]
97+
);
98+
$this->objectManagerHelper->setBackwardCompatibleProperty(
99+
$this->loadOptions,
100+
'conditionsHelper',
101+
$this->conditionsHelperMock
102+
);
103+
}
104+
105+
/**
106+
* @return void
107+
*/
108+
public function dtestExecuteWithException()
109+
{
110+
$jsonResult = '{"error":true,"message":"Some error"}';
111+
$errorMessage = 'Some error';
112+
113+
/** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $jsonDataHelperMock */
114+
$jsonDataHelperMock = $this->getMockBuilder(\Magento\Framework\Json\Helper\Data::class)
115+
->disableOriginalConstructor()
116+
->getMock();
117+
$jsonDataHelperMock->expects($this->once())
118+
->method('jsonEncode')
119+
->with(['error' => true, 'message' => $errorMessage])
120+
->willReturn($jsonResult);
121+
122+
$this->viewMock->expects($this->once())
123+
->method('loadLayout')
124+
->willThrowException(new LocalizedException(__($errorMessage)));
125+
$this->objectManagerMock->expects($this->once())
126+
->method('get')
127+
->with(\Magento\Framework\Json\Helper\Data::class)
128+
->willReturn($jsonDataHelperMock);
129+
$this->responseMock->expects($this->once())
130+
->method('representJson')
131+
->with($jsonResult)
132+
->willReturnArgument(0);
133+
134+
$this->loadOptions->execute();
135+
}
136+
137+
/**
138+
* @return void
139+
*/
140+
public function testExecute()
141+
{
142+
$widgetType = 'Magento\SomeWidget';
143+
$conditionsEncoded = 'a:3:{s:5:"value";i:1;s:8:"operator";s:2:"==";s:9:"attribute";s:2:"id";}';
144+
$conditionsDecoded = [
145+
'value' => 1,
146+
'operator' => '==',
147+
'attribute' => 'id',
148+
];
149+
$widgetJsonParams = '{"widget_type":"Magento\\Widget","values":{"title":"&quot;Test&quot;", "":}}';
150+
$widgetArrayParams = [
151+
'widget_type' => $widgetType,
152+
'values' => [
153+
'title' => '&quot;Test&quot;',
154+
'conditions_encoded' => $conditionsEncoded,
155+
],
156+
];
157+
$resultWidgetArrayParams = [
158+
'widget_type' => $widgetType,
159+
'values' => [
160+
'title' => '"Test"',
161+
'conditions_encoded' => $conditionsEncoded,
162+
'conditions' => $conditionsDecoded,
163+
],
164+
];
165+
166+
/** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $jsonDataHelperMock */
167+
$jsonDataHelperMock = $this->getMockBuilder(\Magento\Framework\Json\Helper\Data::class)
168+
->disableOriginalConstructor()
169+
->getMock();
170+
$jsonDataHelperMock->expects($this->once())
171+
->method('jsonDecode')
172+
->with($widgetJsonParams)
173+
->willReturn($widgetArrayParams);
174+
175+
$this->viewMock->expects($this->once())
176+
->method('loadLayout');
177+
$this->requestMock->expects($this->once())
178+
->method('getParam')
179+
->with('widget')
180+
->willReturn($widgetJsonParams);
181+
$this->objectManagerMock->expects($this->once())
182+
->method('get')
183+
->with(\Magento\Framework\Json\Helper\Data::class)
184+
->willReturn($jsonDataHelperMock);
185+
186+
/** @var \Magento\Framework\View\Element\BlockInterface|\PHPUnit_Framework_MockObject_MockObject $blockMock */
187+
$blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
188+
->setMethods(['setWidgetType', 'setWidgetValues'])
189+
->getMockForAbstractClass();
190+
$blockMock->expects($this->once())
191+
->method('setWidgetType')
192+
->with($widgetType)
193+
->willReturnSelf();
194+
$blockMock->expects($this->once())
195+
->method('setWidgetValues')
196+
->with($resultWidgetArrayParams['values'])
197+
->willReturnSelf();
198+
199+
/** @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject $layoutMock */
200+
$layoutMock = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class);
201+
$layoutMock->expects($this->once())
202+
->method('getBlock')
203+
->with('wysiwyg_widget.options')
204+
->willReturn($blockMock);
205+
206+
$this->conditionsHelperMock->expects($this->once())
207+
->method('decode')
208+
->with($conditionsEncoded)
209+
->willReturn($conditionsDecoded);
210+
$this->viewMock->expects($this->once())
211+
->method('getLayout')
212+
->willReturn($layoutMock);
213+
$this->viewMock->expects($this->once())
214+
->method('renderLayout');
215+
216+
$this->loadOptions->execute();
217+
}
218+
}

0 commit comments

Comments
 (0)