-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1461 from magento-qwerty/MAGETWO-71215-fix
Fixed issues: - MAGETWO-71215: [EAV] Junk attribute values created when scheduling new staging update
- Loading branch information
Showing
4 changed files
with
273 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...de/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper/AttributeFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; | ||
|
||
use \Magento\Catalog\Model\Product; | ||
|
||
/** | ||
* Class provides functionality to check and filter data came with product form. | ||
* | ||
* The main goal is to avoid database population with empty(null) attribute values. | ||
*/ | ||
class AttributeFilter | ||
{ | ||
/** | ||
* Method provides product data check and its further filtration. | ||
* | ||
* Filtration helps us to avoid unnecessary empty product data to be saved. | ||
* Empty data will be preserved only if user explicitly set it. | ||
* | ||
* @param Product $product | ||
* @param array $productData | ||
* @param array $useDefaults | ||
* @return array | ||
*/ | ||
public function prepareProductAttributes(Product $product, array $productData, array $useDefaults) | ||
{ | ||
foreach ($productData as $attribute => $value) { | ||
$considerUseDefaultsAttribute = !isset($useDefaults[$attribute]) || $useDefaults[$attribute] === "1"; | ||
if ($value === '' && $considerUseDefaultsAttribute) { | ||
/** @var $product Product */ | ||
if ((bool)$product->getData($attribute) === (bool)$value) { | ||
unset($productData[$attribute]); | ||
} | ||
} | ||
} | ||
return $productData; | ||
} | ||
} |
203 changes: 203 additions & 0 deletions
203
...alog/Test/Unit/Controller/Adminhtml/Product/Initialization/Helper/AttributeFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Initialization\Helper; | ||
|
||
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\AttributeFilter; | ||
use Magento\Catalog\Model\Product; | ||
|
||
class AttributeFilterTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var AttributeFilter | ||
*/ | ||
protected $model; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $objectManagerMock; | ||
|
||
/** | ||
* @var Product|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $productMock; | ||
|
||
protected function setUp() | ||
{ | ||
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
$this->model = $objectHelper->getObject(AttributeFilter::class); | ||
} | ||
|
||
/** | ||
* @param array $requestProductData | ||
* @param array $useDefaults | ||
* @param array $expectedProductData | ||
* @param array $initialProductData | ||
* @dataProvider setupInputDataProvider | ||
*/ | ||
public function testPrepareProductAttributes( | ||
$requestProductData, | ||
$useDefaults, | ||
$expectedProductData, | ||
$initialProductData | ||
) { | ||
$productMockMap = $this->getMockBuilder(Product::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getData']) | ||
->getMock(); | ||
|
||
if (!empty($initialProductData)) { | ||
$productMockMap->expects($this->any())->method('getData')->willReturnMap($initialProductData); | ||
} | ||
|
||
$actualProductData = $this->model->prepareProductAttributes($productMockMap, $requestProductData, $useDefaults); | ||
$this->assertEquals($expectedProductData, $actualProductData); | ||
} | ||
|
||
/** | ||
* @return array | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function setupInputDataProvider() | ||
{ | ||
return [ | ||
'create_new_product' => [ | ||
'productData' => [ | ||
'name' => 'testName', | ||
'sku' => 'testSku', | ||
'price' => '100', | ||
'description' => '' | ||
], | ||
'useDefaults' => [], | ||
'expectedProductData' => [ | ||
'name' => 'testName', | ||
'sku' => 'testSku', | ||
'price' => '100' | ||
], | ||
'initialProductData' => [] | ||
], | ||
'update_product_without_use_defaults' => [ | ||
'productData' => [ | ||
'name' => 'testName2', | ||
'sku' => 'testSku2', | ||
'price' => '101', | ||
'description' => '', | ||
'special_price' => null | ||
], | ||
'useDefaults' => [], | ||
'expectedProductData' => [ | ||
'name' => 'testName2', | ||
'sku' => 'testSku2', | ||
'price' => '101', | ||
'special_price' => null | ||
], | ||
'initialProductData' => [ | ||
['name', 'testName2'], | ||
['sku', 'testSku2'], | ||
['price', '101'], | ||
['special_price', null] | ||
] | ||
], | ||
'update_product_without_use_defaults_2' => [ | ||
'productData' => [ | ||
'name' => 'testName2', | ||
'sku' => 'testSku2', | ||
'price' => '101', | ||
'description' => 'updated description', | ||
'special_price' => null | ||
], | ||
'useDefaults' => [], | ||
'expectedProductData' => [ | ||
'name' => 'testName2', | ||
'sku' => 'testSku2', | ||
'price' => '101', | ||
'description' => 'updated description', | ||
'special_price' => null | ||
], | ||
'initialProductData' => [ | ||
['name', 'testName2'], | ||
['sku', 'testSku2'], | ||
['price', '101'], | ||
['special_price', null] | ||
] | ||
], | ||
'update_product_with_use_defaults' => [ | ||
'productData' => [ | ||
'name' => 'testName2', | ||
'sku' => 'testSku2', | ||
'price' => '101', | ||
'description' => '', | ||
'special_price' => null | ||
], | ||
'useDefaults' => [ | ||
'description' => '0' | ||
], | ||
'expectedProductData' => [ | ||
'name' => 'testName2', | ||
'sku' => 'testSku2', | ||
'price' => '101', | ||
'special_price' => null, | ||
'description' => '' | ||
], | ||
'initialProductData' => [ | ||
['name', 'testName2'], | ||
['sku', 'testSku2'], | ||
['price', '101'], | ||
['special_price', null], | ||
['description', 'descr text'] | ||
] | ||
], | ||
'update_product_with_use_defaults_2' => [ | ||
'requestProductData' => [ | ||
'name' => 'testName3', | ||
'sku' => 'testSku3', | ||
'price' => '103', | ||
'description' => 'descr modified', | ||
'special_price' => '100' | ||
], | ||
'useDefaults' => [ | ||
'description' => '0' | ||
], | ||
'expectedProductData' => [ | ||
'name' => 'testName3', | ||
'sku' => 'testSku3', | ||
'price' => '103', | ||
'special_price' => '100', | ||
'description' => 'descr modified' | ||
], | ||
'initialProductData' => [ | ||
['name', null,'testName2'], | ||
['sku', null, 'testSku2'], | ||
['price', null, '101'], | ||
['description', null, 'descr text'] | ||
] | ||
], | ||
'update_product_with_use_defaults_3' => [ | ||
'requestProductData' => [ | ||
'name' => 'testName3', | ||
'sku' => 'testSku3', | ||
'price' => '103', | ||
'special_price' => '100' | ||
], | ||
'useDefaults' => [ | ||
'description' => '1' | ||
], | ||
'expectedProductData' => [ | ||
'name' => 'testName3', | ||
'sku' => 'testSku3', | ||
'price' => '103', | ||
'special_price' => '100', | ||
], | ||
'initialProductData' => [ | ||
['name', null,'testName2'], | ||
['sku', null, 'testSku2'], | ||
['price', null, '101'], | ||
['description', null, 'descr text'] | ||
] | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters