Skip to content

Commit b94d6fb

Browse files
author
Hayder Sharhan
committed
Merge remote-tracking branch 'remotes/origin/develop' into for-pr
2 parents d9a5a0c + 15d3a28 commit b94d6fb

File tree

20 files changed

+419
-99
lines changed

20 files changed

+419
-99
lines changed

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@
112112
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories</item>
113113
<item name="sortOrder" xsi:type="number">60</item>
114114
</item>
115-
<item name="schedule-design-update" xsi:type="array">
115+
<item name="custom-options" xsi:type="array">
116116
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions</item>
117117
<item name="sortOrder" xsi:type="number">70</item>
118118
</item>
119-
<item name="custom-options" xsi:type="array">
119+
<item name="schedule-design-update" xsi:type="array">
120120
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\ScheduleDesignUpdate</item>
121121
<item name="sortOrder" xsi:type="number">80</item>
122122
</item>

app/code/Magento/CatalogRule/Block/Adminhtml/Promo/Catalog/Edit/Tab/Conditions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $fo
187187
private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName)
188188
{
189189
$conditions->setFormName($formName);
190+
$conditions->setJsFormObject($formName);
190191
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
191192
foreach ($conditions->getConditions() as $condition) {
192193
$this->setConditionFormName($condition, $formName);

app/code/Magento/Checkout/Block/Checkout/LayoutProcessor.php

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Checkout\Block\Checkout;
77

8+
use Magento\Framework\App\ObjectManager;
9+
810
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
911
{
1012
/**
@@ -22,6 +24,11 @@ class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcesso
2224
*/
2325
protected $merger;
2426

27+
/**
28+
* @var \Magento\Customer\Model\Options
29+
*/
30+
private $options;
31+
2532
/**
2633
* @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
2734
* @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
@@ -38,12 +45,21 @@ public function __construct(
3845
}
3946

4047
/**
41-
* Process js Layout of block
42-
*
43-
* @param array $jsLayout
48+
* @deprecated
49+
* @return \Magento\Customer\Model\Options
50+
*/
51+
private function getOptions()
52+
{
53+
if (!is_object($this->options)) {
54+
$this->options = ObjectManager::getInstance()->get(\Magento\Customer\Model\Options::class);
55+
}
56+
return $this->options;
57+
}
58+
59+
/**
4460
* @return array
4561
*/
46-
public function process($jsLayout)
62+
private function getAddressAttributes()
4763
{
4864
/** @var \Magento\Eav\Api\Data\AttributeInterface[] $attributes */
4965
$attributes = $this->attributeMetadataDataProvider->loadAttributesCollection(
@@ -53,16 +69,66 @@ public function process($jsLayout)
5369

5470
$elements = [];
5571
foreach ($attributes as $attribute) {
72+
$code = $attribute->getAttributeCode();
5673
if ($attribute->getIsUserDefined()) {
5774
continue;
5875
}
59-
$elements[$attribute->getAttributeCode()] = $this->attributeMapper->map($attribute);
60-
if (isset($elements[$attribute->getAttributeCode()]['label'])) {
61-
$label = $elements[$attribute->getAttributeCode()]['label'];
62-
$elements[$attribute->getAttributeCode()]['label'] = __($label);
76+
$elements[$code] = $this->attributeMapper->map($attribute);
77+
if (isset($elements[$code]['label'])) {
78+
$label = $elements[$code]['label'];
79+
$elements[$code]['label'] = __($label);
80+
}
81+
}
82+
return $elements;
83+
}
84+
85+
/**
86+
* Convert elements(like prefix and suffix) from inputs to selects when necessary
87+
*
88+
* @param array $elements address attributes
89+
* @param array $attributesToConvert fields and their callbacks
90+
* @return array
91+
*/
92+
private function convertElementsToSelect($elements, $attributesToConvert)
93+
{
94+
$codes = array_keys($attributesToConvert);
95+
foreach (array_keys($elements) as $code) {
96+
if (!in_array($code, $codes)) {
97+
continue;
98+
}
99+
$options = call_user_func($attributesToConvert[$code]);
100+
if (!is_array($options)) {
101+
continue;
102+
}
103+
$elements[$code]['dataType'] = 'select';
104+
$elements[$code]['formElement'] = 'select';
105+
106+
foreach ($options as $key => $value) {
107+
$elements[$code]['options'][] = [
108+
'value' => $key,
109+
'label' => $value,
110+
];
63111
}
64112
}
65113

114+
return $elements;
115+
}
116+
117+
/**
118+
* Process js Layout of block
119+
*
120+
* @param array $jsLayout
121+
* @return array
122+
*/
123+
public function process($jsLayout)
124+
{
125+
$attributesToConvert = [
126+
'prefix' => [$this->getOptions(), 'getNamePrefixOptions'],
127+
'suffix' => [$this->getOptions(), 'getNameSuffixOptions'],
128+
];
129+
130+
$elements = $this->getAddressAttributes();
131+
$elements = $this->convertElementsToSelect($elements, $attributesToConvert);
66132
// The following code is a workaround for custom address attributes
67133
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
68134
['payment']['children']

app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,47 @@
55
*/
66
namespace Magento\Checkout\Controller\Sidebar;
77

8-
use Magento\Checkout\Model\Sidebar;
9-
use Magento\Framework\App\Action\Action;
10-
use Magento\Framework\App\Action\Context;
11-
use Magento\Framework\App\Response\Http;
12-
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Framework\Json\Helper\Data;
14-
use Magento\Framework\View\Result\PageFactory;
15-
use Psr\Log\LoggerInterface;
168

17-
class RemoveItem extends Action
9+
class RemoveItem extends \Magento\Framework\App\Action\Action
1810
{
1911
/**
20-
* @var Sidebar
12+
* @var \Magento\Checkout\Model\Sidebar
2113
*/
2214
protected $sidebar;
2315

2416
/**
25-
* @var LoggerInterface
17+
* @var \Psr\Log\LoggerInterface
2618
*/
2719
protected $logger;
2820

2921
/**
30-
* @var Data
22+
* @var \Magento\Framework\Json\Helper\Data
3123
*/
3224
protected $jsonHelper;
3325

3426
/**
35-
* @var PageFactory
27+
* @var \Magento\Framework\View\Result\PageFactory
3628
*/
3729
protected $resultPageFactory;
3830

3931
/**
40-
* @param Context $context
41-
* @param Sidebar $sidebar
42-
* @param LoggerInterface $logger
43-
* @param Data $jsonHelper
44-
* @param PageFactory $resultPageFactory
45-
* @codeCoverageIgnore
32+
* @var \Magento\Framework\Data\Form\FormKey\Validator
33+
*/
34+
private $formKeyValidator;
35+
36+
/**
37+
* @param \Magento\Framework\App\Action\Context $context
38+
* @param \Magento\Checkout\Model\Sidebar $sidebar
39+
* @param \Psr\Log\LoggerInterface $logger
40+
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
41+
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
4642
*/
4743
public function __construct(
48-
Context $context,
49-
Sidebar $sidebar,
50-
LoggerInterface $logger,
51-
Data $jsonHelper,
52-
PageFactory $resultPageFactory
44+
\Magento\Framework\App\Action\Context $context,
45+
\Magento\Checkout\Model\Sidebar $sidebar,
46+
\Psr\Log\LoggerInterface $logger,
47+
\Magento\Framework\Json\Helper\Data $jsonHelper,
48+
\Magento\Framework\View\Result\PageFactory $resultPageFactory
5349
) {
5450
$this->sidebar = $sidebar;
5551
$this->logger = $logger;
@@ -63,12 +59,15 @@ public function __construct(
6359
*/
6460
public function execute()
6561
{
62+
if (!$this->getFormKeyValidator()->validate($this->getRequest())) {
63+
return $this->resultRedirectFactory->create()->setPath('*/cart/');
64+
}
6665
$itemId = (int)$this->getRequest()->getParam('item_id');
6766
try {
6867
$this->sidebar->checkQuoteItem($itemId);
6968
$this->sidebar->removeQuoteItem($itemId);
7069
return $this->jsonResponse();
71-
} catch (LocalizedException $e) {
70+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
7271
return $this->jsonResponse($e->getMessage());
7372
} catch (\Exception $e) {
7473
$this->logger->critical($e);
@@ -80,7 +79,7 @@ public function execute()
8079
* Compile JSON response
8180
*
8281
* @param string $error
83-
* @return Http
82+
* @return \Magento\Framework\App\Response\Http
8483
*/
8584
protected function jsonResponse($error = '')
8685
{
@@ -90,4 +89,17 @@ protected function jsonResponse($error = '')
9089
$this->jsonHelper->jsonEncode($response)
9190
);
9291
}
92+
93+
/**
94+
* @return \Magento\Framework\Data\Form\FormKey\Validator
95+
* @deprecated
96+
*/
97+
private function getFormKeyValidator()
98+
{
99+
if (!$this->formKeyValidator) {
100+
$this->formKeyValidator = \Magento\Framework\App\ObjectManager::getInstance()
101+
->get(\Magento\Framework\Data\Form\FormKey\Validator::class);
102+
}
103+
return $this->formKeyValidator;
104+
}
93105
}

app/code/Magento/Checkout/Test/Unit/Controller/Sidebar/RemoveItemTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Framework\Exception\LocalizedException;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1010

11+
/**
12+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13+
*/
1114
class RemoveItemTest extends \PHPUnit_Framework_TestCase
1215
{
1316
/** @var \Magento\Checkout\Controller\Sidebar\RemoveItem */
@@ -34,6 +37,11 @@ class RemoveItemTest extends \PHPUnit_Framework_TestCase
3437
/** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */
3538
protected $resultPageFactoryMock;
3639

40+
/**
41+
* @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $resultRedirectFactory;
44+
3745
protected function setUp()
3846
{
3947
$this->sidebarMock = $this->getMock('Magento\Checkout\Model\Sidebar', [], [], '', false);
@@ -50,6 +58,13 @@ protected function setUp()
5058
['representJson']
5159
);
5260
$this->resultPageFactoryMock = $this->getMock('Magento\Framework\View\Result\PageFactory', [], [], '', false);
61+
$this->resultRedirectFactory = $this->getMock(
62+
\Magento\Framework\Controller\Result\RedirectFactory::class,
63+
['create'],
64+
[],
65+
'',
66+
false
67+
);
5368

5469
$this->objectManagerHelper = new ObjectManagerHelper($this);
5570
$this->removeItem = $this->objectManagerHelper->getObject(
@@ -61,12 +76,21 @@ protected function setUp()
6176
'request' => $this->requestMock,
6277
'response' => $this->responseMock,
6378
'resultPageFactory' => $this->resultPageFactoryMock,
79+
'resultRedirectFactory' => $this->resultRedirectFactory
80+
6481
]
6582
);
83+
$formKeyValidatorMock = $this->getMock('Magento\Framework\Data\Form\FormKey\Validator', [], [], '', false);
84+
$this->setPropertyValue($this->removeItem, 'formKeyValidator', $formKeyValidatorMock);
6685
}
6786

6887
public function testExecute()
6988
{
89+
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
90+
->expects($this->once())
91+
->method('validate')
92+
->with($this->requestMock)
93+
->willReturn(true);
7094
$this->requestMock->expects($this->once())
7195
->method('getParam')
7296
->with('item_id', null)
@@ -118,6 +142,11 @@ public function testExecute()
118142

119143
public function testExecuteWithLocalizedException()
120144
{
145+
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
146+
->expects($this->once())
147+
->method('validate')
148+
->with($this->requestMock)
149+
->willReturn(true);
121150
$this->requestMock->expects($this->once())
122151
->method('getParam')
123152
->with('item_id', null)
@@ -158,6 +187,11 @@ public function testExecuteWithLocalizedException()
158187

159188
public function testExecuteWithException()
160189
{
190+
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
191+
->expects($this->once())
192+
->method('validate')
193+
->with($this->requestMock)
194+
->willReturn(true);
161195
$this->requestMock->expects($this->once())
162196
->method('getParam')
163197
->with('item_id', null)
@@ -202,4 +236,52 @@ public function testExecuteWithException()
202236

203237
$this->assertEquals('json represented', $this->removeItem->execute());
204238
}
239+
240+
public function testExecuteWhenFormKeyValidationFailed()
241+
{
242+
$resultRedirect = $this->getMock(\Magento\Framework\Controller\Result\Redirect::class, [], [], '', false);
243+
$resultRedirect->expects($this->once())->method('setPath')->with('*/cart/')->willReturnSelf();
244+
$this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($resultRedirect);
245+
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
246+
->expects($this->once())
247+
->method('validate')
248+
->with($this->requestMock)
249+
->willReturn(false);
250+
$this->assertEquals($resultRedirect, $this->removeItem->execute());
251+
}
252+
253+
/**
254+
* Get any object property value.
255+
*
256+
* @param $object
257+
* @param $property
258+
* @return mixed
259+
* @deprecated
260+
*/
261+
protected function getPropertyValue($object, $property)
262+
{
263+
$reflection = new \ReflectionClass(get_class($object));
264+
$reflectionProperty = $reflection->getProperty($property);
265+
$reflectionProperty->setAccessible(true);
266+
267+
return $reflectionProperty->getValue($object);
268+
}
269+
270+
/**
271+
* Set object property value.
272+
*
273+
* @param $object
274+
* @param $property
275+
* @param $value
276+
* @deprecated
277+
*/
278+
protected function setPropertyValue(&$object, $property, $value)
279+
{
280+
$reflection = new \ReflectionClass(get_class($object));
281+
$reflectionProperty = $reflection->getProperty($property);
282+
$reflectionProperty->setAccessible(true);
283+
$reflectionProperty->setValue($object, $value);
284+
285+
return $object;
286+
}
205287
}

0 commit comments

Comments
 (0)