Skip to content

Commit

Permalink
Merge pull request #598 from magento-tango/MAGETWO-52578
Browse files Browse the repository at this point in the history
[Tango] Bug Fixes
  • Loading branch information
Korshenko, Oleksii(okorshenko) committed May 11, 2016
2 parents 392644b + aaa2142 commit ffe26b0
Show file tree
Hide file tree
Showing 17 changed files with 588 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
} else {
$productOptions = [];
}

$product->addData($productData);

if ($wasLockedMedia) {
Expand Down Expand Up @@ -307,20 +308,28 @@ protected function normalize(array $productData)
public function mergeProductOptions($productOptions, $overwriteOptions)
{
if (!is_array($productOptions)) {
$productOptions = [];
return [];
}
if (is_array($overwriteOptions)) {
$options = array_replace_recursive($productOptions, $overwriteOptions);
array_walk_recursive($options, function (&$item) {
if ($item === "") {
$item = null;

if (!is_array($overwriteOptions)) {
return $productOptions;
}

foreach ($productOptions as $index => $option) {
$optionId = $option['option_id'];

if (!isset($overwriteOptions[$optionId])) {
continue;
}

foreach ($overwriteOptions[$optionId] as $fieldName => $overwrite) {
if ($overwrite && isset($option[$fieldName]) && isset($option['default_' . $fieldName])) {
$productOptions[$index][$fieldName] = $option['default_' . $fieldName];
}
});
} else {
$options = $productOptions;
}
}

return $options;
return $productOptions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,25 +326,72 @@ public function testInitialize()
public function mergeProductOptionsDataProvider()
{
return [
[
'options are not array, empty array is returned' => [
null,
[],
[],
],
[
['key' => 'val'],
'replacement is not array, original options are returned' => [
['val'],
null,
['key' => 'val'],
['val'],
],
[
['key' => ['key' => 'val']],
['key' => ['key' => 'val2', 'key2' => 'val2']],
['key' => ['key' => 'val2', 'key2' => 'val2']],
'ids do not match, no replacement occurs' => [
[
[
'option_id' => '3',
'key1' => 'val1',
'default_key1' => 'val2'
]
],
[4 => ['key1' => '1']],
[
[
'option_id' => '3',
'key1' => 'val1',
'default_key1' => 'val2'
]
]
],
[
['key' => ['key' => 'val', 'another_key' => 'another_value']],
['key' => ['key' => 'val2', 'key2' => 'val2']],
['key' => ['key' => 'val2', 'another_key' => 'another_value', 'key2' => 'val2',]],
'key2 is replaced, key1 is not (checkbox is not checked)' => [
[
[
'option_id' => '5',
'key1' => 'val1',
'key2' => 'val2',
'default_key1' => 'val3',
'default_key2' => 'val4'
]
],
[5 => ['key1' => '0', 'key2' => '1']],
[
[
'option_id' => '5',
'key1' => 'val1',
'key2' => 'val4',
'default_key1' => 'val3',
'default_key2' => 'val4'
]
]
],
'key1 is replaced, key2 has no default value' => [
[
[
'option_id' => '7',
'key1' => 'val1',
'key2' => 'val2',
'default_key1' => 'val3'
]
],
[7 => ['key1' => '1', 'key2' => '1']],
[
[
'option_id' => '7',
'key1' => 'val3',
'key2' => 'val2',
'default_key1' => 'val3'
]
],
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,15 @@ protected function getGroupCodeByField(array $meta, $field)

return false;
}

/**
* Format number to have only two decimals after delimiter
*
* @param mixed $value
* @return string
*/
protected function formatFloat($value)
{
return $value !== null ? number_format((float)$value, 2, '.', '') : '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public function modifyData(array $data)

/** @var \Magento\Catalog\Model\Product\Option $option */
foreach ($productOptions as $index => $option) {
$options[$index] = $this->formatFloat(static::FIELD_PRICE_NAME, $option->getData());
$options[$index] = $this->formatFloatByPath(static::FIELD_PRICE_NAME, $option->getData());
$values = $option->getValues() ?: [];

/** @var \Magento\Catalog\Model\Product\Option $value */
foreach ($values as $value) {
$options[$index][static::GRID_TYPE_SELECT_NAME][] = $this->formatFloat(
$options[$index][static::GRID_TYPE_SELECT_NAME][] = $this->formatFloatByPath(
static::FIELD_PRICE_NAME,
$value->getData()
);
Expand All @@ -188,12 +188,12 @@ public function modifyData(array $data)
* @param array $data
* @return array
*/
protected function formatFloat($path, array $data)
protected function formatFloatByPath($path, array $data)
{
$value = $this->arrayManager->get($path, $data);

if (is_numeric($value)) {
$data = $this->arrayManager->replace($path, $data, number_format($value, 2, '.', ''));
$data = $this->arrayManager->replace($path, $data, $this->formatFloat($value));
}

return $data;
Expand Down Expand Up @@ -497,7 +497,7 @@ protected function getImportOptionsModalConfig()
*/
protected function getCommonContainerConfig($sortOrder)
{
return [
$commonContainer = [
'arguments' => [
'data' => [
'config' => [
Expand All @@ -522,6 +522,9 @@ protected function getCommonContainerConfig($sortOrder)
'label' => __('Option Title'),
'component' => 'Magento_Catalog/component/static-type-input',
'valueUpdate' => 'input',
'imports' => [
'optionId' => '${ $.provider }:${ $.parentScope }.option_id'
]
],
],
],
Expand All @@ -531,6 +534,19 @@ protected function getCommonContainerConfig($sortOrder)
static::FIELD_IS_REQUIRE_NAME => $this->getIsRequireFieldConfig(40)
]
];

if ($this->locator->getProduct()->getStoreId()) {
$useDefaultConfig = [
'service' => [
'template' => 'Magento_Catalog/form/element/helper/custom-option-service',
]
];
$titlePath = $this->arrayManager->findPath(static::FIELD_TITLE_NAME, $commonContainer, null)
. static::META_CONFIG_PATH;
$commonContainer = $this->arrayManager->merge($titlePath, $commonContainer, $useDefaultConfig);
}

return $commonContainer;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function modifyData(array $data)
foreach ($attributes as $attribute) {
if (null !== ($attributeValue = $this->setupAttributeData($attribute))) {
if ($attribute->getFrontendInput() === 'price' && is_scalar($attributeValue)) {
$attributeValue = number_format((float)$attributeValue, 2);
$attributeValue = $this->formatFloat($attributeValue);
}
$data[$productId][self::DATA_SOURCE_DEFAULT][$attribute->getAttributeCode()] = $attributeValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ define([
this.parentOption(function (component) {
component.set('label', value ? value : component.get('headerLabel'));
});

return this._super();
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<div class="admin__field-service">
<input type="checkbox"
class="admin__control-checkbox"
attr="
id: $data.uid + '_default',
name: 'options_use_default[' + $data.optionId + '][' + $data.index + ']',
'data-form-part': $data.ns
"
ko-checked="isUseDefault">
<label translate="'Use Default Value'" attr="for: $data.uid + '_default'" class="admin__field-label"></label>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/
namespace Magento\CatalogInventory\Test\Unit\Ui\DataProvider\Product\Form\Modifier;

use Magento\Store\Model\Store;
use Magento\Catalog\Test\Unit\Ui\DataProvider\Product\Form\Modifier\AbstractModifierTest;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Store\Model\Store;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Ui\DataProvider\Product\Form\Modifier\AdvancedInventory;

/**
Expand All @@ -31,6 +32,11 @@ class AdvancedInventoryTest extends AbstractModifierTest
*/
protected $storeMock;

/**
* @var StockConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stockConfigurationMock;

protected function setUp()
{
parent::setUp();
Expand All @@ -43,6 +49,9 @@ protected function setUp()
$this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)
->setMethods(['getData'])
->getMockForAbstractClass();
$this->stockConfigurationMock = $this->getMockBuilder(StockConfigurationInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->stockRegistryMock->expects($this->any())
->method('getStockItem')
Expand All @@ -59,7 +68,9 @@ protected function createModel()
{
return $this->objectManager->getObject(AdvancedInventory::class, [
'locator' => $this->locatorMock,
'stockRegistry' => $this->stockRegistryMock
'stockRegistry' => $this->stockRegistryMock,
'stockConfiguration' => $this->stockConfigurationMock,
'arrayManager' => $this->arrayManagerMock,
]);
}

Expand All @@ -73,9 +84,10 @@ public function testModifyData()
$modelId = 1;
$someData = 1;

$this->productMock->expects($this->any())
->method('getId')
->willReturn($modelId);
$this->productMock->expects($this->any())->method('getId')->willReturn($modelId);

$this->stockConfigurationMock->expects($this->any())->method('getDefaultConfigValue')->willReturn("a:0:{}");

$this->stockItemMock->expects($this->once())->method('getData')->willReturn(['someData']);
$this->stockItemMock->expects($this->once())->method('getManageStock')->willReturn($someData);
$this->stockItemMock->expects($this->once())->method('getQty')->willReturn($someData);
Expand All @@ -90,6 +102,11 @@ public function testModifyData()
$this->stockItemMock->expects($this->once())->method('getQtyIncrements')->willReturn($someData);
$this->stockItemMock->expects($this->once())->method('getIsInStock')->willReturn($someData);

$this->arrayManagerMock->expects($this->once())
->method('set')
->with('1/product/stock_data/min_qty_allowed_in_shopping_cart')
->willReturnArgument(1);

$this->assertArrayHasKey($modelId, $this->getModel()->modifyData([]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public function prepare()
&& isset($config['valueFromConfig'])
&& $config['valueFromConfig'] instanceof ValueSourceInterface
) {
$config['valueFromConfig'] = $config['valueFromConfig']->getValue($config['keyInConfiguration']);
$keyInConfiguration = $config['valueFromConfig']->getValue($config['keyInConfiguration']);
if (!empty($config['unserialized']) && strpos($keyInConfiguration, 'a:') === 0) {
$config['valueFromConfig'] = unserialize($keyInConfiguration);
} else {
$config['valueFromConfig'] = $keyInConfiguration;
}
}
$this->setData('config', (array)$config);

Expand Down
Loading

0 comments on commit ffe26b0

Please sign in to comment.