Skip to content

Commit 91b2e58

Browse files
committed
Merge remote-tracking branch 'mainline/develop' into develop-prs
2 parents 0bdd748 + 999a88b commit 91b2e58

File tree

44 files changed

+1331
-1367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1331
-1367
lines changed

app/code/Magento/CatalogInventory/Test/Unit/Ui/Component/Product/Form/Element/UseConfigSettingsTest.php

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CatalogInventory\Test\Unit\Ui\Component\Product\Form\Element;
77

8+
use Magento\Framework\Serialize\Serializer\Json;
89
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
910
use Magento\CatalogInventory\Ui\Component\Product\Form\Element\UseConfigSettings;
1011
use Magento\Framework\Data\ValueSourceInterface;
@@ -15,16 +16,23 @@ class UseConfigSettingsTest extends \PHPUnit_Framework_TestCase
1516
/**
1617
* @var ObjectManagerHelper
1718
*/
18-
protected $objectManagerHelper;
19+
private $objectManagerHelper;
1920

2021
/**
2122
* @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
2223
*/
23-
protected $contextMock;
24+
private $contextMock;
2425

2526
/**
26-
* @return void
27+
* @var Json|\PHPUnit_Framework_MockObject_MockObject
2728
*/
29+
private $serializerMock;
30+
31+
/**
32+
* @var UseConfigSettings
33+
*/
34+
private $useConfigSettings;
35+
2836
protected function setUp()
2937
{
3038
$this->objectManagerHelper = new ObjectManagerHelper($this);
@@ -43,51 +51,70 @@ protected function setUp()
4351
$this->contextMock->expects($this->any())
4452
->method('getProcessor')
4553
->willReturn($processorMock);
54+
$this->serializerMock = $this->getMock(Json::class);
55+
$this->useConfigSettings = $this->objectManagerHelper->getObject(
56+
UseConfigSettings::class,
57+
[
58+
'context' => $this->contextMock,
59+
'serializer' => $this->serializerMock
60+
]
61+
);
4662
}
4763

48-
/**
49-
* @return void
50-
*/
5164
public function testPrepare()
5265
{
5366
$config = ['valueFromConfig' => 123];
54-
$element = $this->getTestedElement($config);
55-
$element->prepare();
56-
$this->assertEquals($config, $element->getData('config'));
67+
$this->useConfigSettings->setData('config', $config);
68+
$this->useConfigSettings->prepare();
69+
$this->assertEquals($config, $this->useConfigSettings->getData('config'));
5770
}
5871

5972
/**
60-
* @return void
73+
* @param array $expectedResult
74+
* @param string|int $sourceValue
75+
* @param int $serializedCallCount
76+
* @dataProvider prepareSourceDataProvider
6177
*/
62-
public function testPrepareSource()
78+
public function testPrepareSource(array $expectedResult, $sourceValue, $serializedCallCount = 0)
6379
{
6480
/** @var ValueSourceInterface|\PHPUnit_Framework_MockObject_MockObject $source */
6581
$source = $this->getMock(ValueSourceInterface::class);
6682
$source->expects($this->once())
6783
->method('getValue')
68-
->with('someKey')
69-
->willReturn('someData');
84+
->with($expectedResult['keyInConfiguration'])
85+
->willReturn($sourceValue);
86+
87+
$this->serializerMock->expects($this->exactly($serializedCallCount))
88+
->method('unserialize')
89+
->with($sourceValue)
90+
->willReturn($expectedResult['valueFromConfig']);
7091

71-
$config = ['valueFromConfig' => $source, 'keyInConfiguration' => 'someKey'];
72-
$element = $this->getTestedElement($config);
73-
$element->prepare();
92+
$config = array_replace($expectedResult, ['valueFromConfig' => $source]);
93+
$this->useConfigSettings->setData('config', $config);
94+
$this->useConfigSettings->prepare();
7495

75-
$expectedResult =['valueFromConfig' => 'someData', 'keyInConfiguration' => 'someKey'];
76-
$this->assertEquals($expectedResult, $element->getData('config'));
96+
$this->assertEquals($expectedResult, $this->useConfigSettings->getData('config'));
7797
}
7898

79-
/**
80-
* @param array $config
81-
* @return UseConfigSettings
82-
*/
83-
protected function getTestedElement(array $config = [])
99+
public function prepareSourceDataProvider()
84100
{
85-
return $this->objectManagerHelper->getObject(
86-
UseConfigSettings::class,
87-
[
88-
'context' => $this->contextMock,
89-
'data' => ['config' => $config]
101+
return [
102+
'valid' => [
103+
'expectedResult' => [
104+
'valueFromConfig' => 2,
105+
'keyInConfiguration' => 'validKey'
106+
],
107+
'sourceValue' => 2
108+
],
109+
'serialized' => [
110+
'expectedResult' => [
111+
'valueFromConfig' => ['32000' => 3],
112+
'keyInConfiguration' => 'serializedKey',
113+
'unserialized' => true
114+
],
115+
'sourceValue' => '{"32000":3}',
116+
'serialziedCallCount' => 1
90117
]
91-
);
118+
];
92119
}
93120
}

app/code/Magento/CatalogInventory/Test/Unit/Ui/DataProvider/Product/Form/Modifier/AdvancedInventoryTest.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CatalogInventory\Test\Unit\Ui\DataProvider\Product\Form\Modifier;
77

8+
use Magento\Framework\Serialize\Serializer\Json;
89
use Magento\Store\Model\Store;
910
use Magento\Catalog\Test\Unit\Ui\DataProvider\Product\Form\Modifier\AbstractModifierTest;
1011
use Magento\CatalogInventory\Api\StockRegistryInterface;
@@ -37,6 +38,11 @@ class AdvancedInventoryTest extends AbstractModifierTest
3738
*/
3839
protected $stockConfigurationMock;
3940

41+
/**
42+
* @var Json|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $serializerMock;
45+
4046
protected function setUp()
4147
{
4248
parent::setUp();
@@ -59,6 +65,7 @@ protected function setUp()
5965
$this->productMock->expects($this->any())
6066
->method('getStore')
6167
->willReturn($this->storeMock);
68+
$this->serializerMock = $this->getMock(Json::class);
6269
}
6370

6471
/**
@@ -71,6 +78,7 @@ protected function createModel()
7178
'stockRegistry' => $this->stockRegistryMock,
7279
'stockConfiguration' => $this->stockConfigurationMock,
7380
'arrayManager' => $this->arrayManagerMock,
81+
'serializer' => $this->serializerMock
7482
]);
7583
}
7684

@@ -79,14 +87,31 @@ public function testModifyMeta()
7987
$this->assertNotEmpty($this->getModel()->modifyMeta(['meta_key' => 'meta_value']));
8088
}
8189

82-
public function testModifyData()
83-
{
84-
$modelId = 1;
85-
$someData = 1;
86-
90+
/**
91+
* @param int $modelId
92+
* @param int $someData
93+
* @param int|string $defaultConfigValue
94+
* @param null|array $unserializedValue
95+
* @param int $serializeCallCount
96+
* @dataProvider modifyDataProvider
97+
*/
98+
public function testModifyData(
99+
$modelId,
100+
$someData,
101+
$defaultConfigValue,
102+
$unserializedValue = null,
103+
$serializeCallCount = 0
104+
) {
87105
$this->productMock->expects($this->any())->method('getId')->willReturn($modelId);
88106

89-
$this->stockConfigurationMock->expects($this->any())->method('getDefaultConfigValue')->willReturn("a:0:{}");
107+
$this->stockConfigurationMock->expects($this->any())
108+
->method('getDefaultConfigValue')
109+
->willReturn($defaultConfigValue);
110+
111+
$this->serializerMock->expects($this->exactly($serializeCallCount))
112+
->method('unserialize')
113+
->with($defaultConfigValue)
114+
->willReturn($unserializedValue);
90115

91116
$this->stockItemMock->expects($this->once())->method('getData')->willReturn(['someData']);
92117
$this->stockItemMock->expects($this->once())->method('getManageStock')->willReturn($someData);
@@ -109,4 +134,12 @@ public function testModifyData()
109134

110135
$this->assertArrayHasKey($modelId, $this->getModel()->modifyData([]));
111136
}
137+
138+
public function modifyDataProvider()
139+
{
140+
return [
141+
[1, 1, 1],
142+
[1, 1, '{"36000":2}', ['36000' => 2], 1]
143+
];
144+
}
112145
}

app/code/Magento/CatalogInventory/Ui/Component/Product/Form/Element/UseConfigSettings.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,35 @@
55
*/
66
namespace Magento\CatalogInventory\Ui\Component\Product\Form\Element;
77

8-
use Magento\Ui\Component\Form\Element\Checkbox;
98
use Magento\Framework\Data\ValueSourceInterface;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
11+
use Magento\Ui\Component\Form\Element\Checkbox;
1012

1113
/**
1214
* Class UseConfigSettings sets default value from configuration
1315
*/
1416
class UseConfigSettings extends Checkbox
1517
{
18+
/** @var Json */
19+
private $serializer;
20+
21+
/**
22+
* @param ContextInterface $context
23+
* @param array $components
24+
* @param array $data
25+
* @param Json|null $serializer
26+
*/
27+
public function __construct(
28+
ContextInterface $context,
29+
$components = [],
30+
array $data = [],
31+
Json $serializer = null
32+
) {
33+
parent::__construct($context, $components, $data);
34+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
35+
}
36+
1637
/**
1738
* Prepare component configuration
1839
*
@@ -26,11 +47,13 @@ public function prepare()
2647
&& $config['valueFromConfig'] instanceof ValueSourceInterface
2748
) {
2849
$keyInConfiguration = $config['valueFromConfig']->getValue($config['keyInConfiguration']);
29-
if (!empty($config['unserialized']) && strpos($keyInConfiguration, 'a:') === 0) {
30-
$config['valueFromConfig'] = unserialize($keyInConfiguration);
31-
} else {
32-
$config['valueFromConfig'] = $keyInConfiguration;
50+
if (!empty($config['unserialized']) && is_string($keyInConfiguration)) {
51+
$unserializedValue = $this->serializer->unserialize($keyInConfiguration);
52+
if (json_last_error() === JSON_ERROR_NONE) {
53+
$keyInConfiguration = $unserializedValue;
54+
}
3355
}
56+
$config['valueFromConfig'] = $keyInConfiguration;
3457
}
3558
$this->setData('config', (array)$config);
3659

app/code/Magento/CatalogInventory/Ui/DataProvider/Product/Form/Modifier/AdvancedInventory.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Model\Locator\LocatorInterface;
1010
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
1111
use Magento\CatalogInventory\Api\StockRegistryInterface;
12+
use Magento\Framework\Serialize\Serializer\Json;
1213
use Magento\Framework\Stdlib\ArrayManager;
1314
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1415
use Magento\CatalogInventory\Api\StockConfigurationInterface;
@@ -45,22 +46,30 @@ class AdvancedInventory extends AbstractModifier
4546
*/
4647
private $meta = [];
4748

49+
/**
50+
* @var Json
51+
*/
52+
private $serializer;
53+
4854
/**
4955
* @param LocatorInterface $locator
5056
* @param StockRegistryInterface $stockRegistry
5157
* @param ArrayManager $arrayManager
5258
* @param StockConfigurationInterface $stockConfiguration
59+
* @param Json|null $serializer
5360
*/
5461
public function __construct(
5562
LocatorInterface $locator,
5663
StockRegistryInterface $stockRegistry,
5764
ArrayManager $arrayManager,
58-
StockConfigurationInterface $stockConfiguration
65+
StockConfigurationInterface $stockConfiguration,
66+
Json $serializer = null
5967
) {
6068
$this->locator = $locator;
6169
$this->stockRegistry = $stockRegistry;
6270
$this->arrayManager = $arrayManager;
6371
$this->stockConfiguration = $stockConfiguration;
72+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
6473
}
6574

6675
/**
@@ -89,21 +98,19 @@ public function modifyData(array $data)
8998
}
9099

91100
if (!empty($this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY))) {
92-
$minSaleQtyData = null;
93-
$defaultConfigValue = $this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY);
101+
$minSaleQtyData = $this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY);
94102

95-
if (strpos($defaultConfigValue, 'a:') === 0) {
103+
if (is_string($minSaleQtyData)) {
96104
// Set data source for dynamicRows Minimum Qty Allowed in Shopping Cart
97-
$minSaleQtyValue = unserialize($defaultConfigValue);
98-
99-
foreach ($minSaleQtyValue as $group => $qty) {
100-
$minSaleQtyData[] = [
101-
StockItemInterface::CUSTOMER_GROUP_ID => $group,
102-
StockItemInterface::MIN_SALE_QTY => $qty
103-
];
105+
$unserializedMinSaleQty = $this->serializer->unserialize($minSaleQtyData);
106+
if (is_array($unserializedMinSaleQty) && json_last_error() === JSON_ERROR_NONE) {
107+
$minSaleQtyData = array_map(function ($group, $qty) {
108+
return [
109+
StockItemInterface::CUSTOMER_GROUP_ID => $group,
110+
StockItemInterface::MIN_SALE_QTY => $qty
111+
];
112+
}, array_keys($unserializedMinSaleQty), array_values($unserializedMinSaleQty));
104113
}
105-
} else {
106-
$minSaleQtyData = $defaultConfigValue;
107114
}
108115

109116
$path = $modelId . '/' . self::DATA_SOURCE_DEFAULT . '/stock_data/min_qty_allowed_in_shopping_cart';

0 commit comments

Comments
 (0)