Skip to content

Commit

Permalink
Merge forwardport of #12283 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/12283.patch (created by @p-bystritsky) based on commit(s):
  1. 41e9ec0
  2. 89d613c

Fixed GitHub Issues in 2.3-develop branch:
  - #12083: Cannot import zero (0) value into custom attribute (reported by @gwilliams01)
  • Loading branch information
magento-engcom-team authored Jan 29, 2018
2 parents 2eca1d0 + 703d9e0 commit f9976b9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
public function clearEmptyData(array $rowData)
{
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {
if (!$attrParams['is_static'] && !isset($rowData[$attrCode])) {
unset($rowData[$attrCode]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,31 @@ class AbstractTest extends \PHPUnit\Framework\TestCase
*/
protected $_model;

/**
* @var \Magento\TestFramework\ObjectManager
*/
private $objectManager;

/**
* On product import abstract class methods level it doesn't matter what product type is using.
* That is why current tests are using simple product entity type by default
*/
protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$params = [$objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class), 'simple'];
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$params = [$this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class), 'simple'];
$this->_model = $this->getMockForAbstractClass(
\Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType::class,
[
$objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class),
$objectManager->get(\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class),
$objectManager->get(\Magento\Framework\App\ResourceConnection::class),
$this->objectManager->get(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class
),
$this->objectManager->get(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class
),
$this->objectManager->get(
\Magento\Framework\App\ResourceConnection::class
),
$params
]
);
Expand Down Expand Up @@ -130,6 +141,11 @@ public function prepareAttributesWithDefaultValueForSaveDataProvider()
}

/**
* Test cleaning imported attribute data from empty values (note '0' is not empty).
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/CatalogImportExport/Model/Import/_files/custom_attributes.php
* @dataProvider clearEmptyDataDataProvider
*/
public function testClearEmptyData($rowData, $expectedAttributes)
Expand All @@ -141,8 +157,14 @@ public function testClearEmptyData($rowData, $expectedAttributes)
}
}

/**
* Data provider for testClearEmptyData.
*
* @return array
*/
public function clearEmptyDataDataProvider()
{
// We use sku attribute to test static attributes.
return [
[
[
Expand All @@ -152,33 +174,57 @@ public function clearEmptyDataDataProvider()
'product_type' => 'simple',
'name' => 'Simple 01',
'price' => 10,
'test_attribute' => '1',
],
[
'sku' => 'simple1',
'store_view_code' => '',
'_attribute_set' => 'Default',
'product_type' => 'simple',
'name' => 'Simple 01',
'price' => 10
'price' => 10,
'test_attribute' => '1',
],
],
[
[
'sku' => '',
'store_view_code' => 'German',
'sku' => '0',
'store_view_code' => '',
'_attribute_set' => 'Default',
'product_type' => '',
'name' => 'Simple 01 German',
'price' => '',
'product_type' => 'simple',
'name' => 'Simple 01',
'price' => 10,
'test_attribute' => '0',
],
[
'sku' => '',
'store_view_code' => 'German',
'sku' => '0',
'store_view_code' => '',
'_attribute_set' => 'Default',
'product_type' => '',
'name' => 'Simple 01 German'
]
]
'product_type' => 'simple',
'name' => 'Simple 01',
'price' => 10,
'test_attribute' => '0',
],
],
[
[
'sku' => null,
'store_view_code' => '',
'_attribute_set' => 'Default',
'product_type' => 'simple',
'name' => 'Simple 01',
'price' => 10,
'test_attribute' => null,
],
[
'sku' => null,
'store_view_code' => '',
'_attribute_set' => 'Default',
'product_type' => 'simple',
'name' => 'Simple 01',
'price' => 10,
],
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var \Magento\Eav\Model\Entity\Type $entityType */
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class);
$entityType->loadByCode('catalog_product');
$entityTypeId = $entityType->getId();

/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
$attributeSet->load('default', 'attribute_set_name');
$attributeSetId = $attributeSet->getId();

$attributeGroupId = $attributeSet->getDefaultGroupId($entityType->getDefaultAttributeSetId());

$attributeData = [
[
'attribute_code' => 'test_attribute',
'entity_type_id' => $entityTypeId,
'backend_type' => 'varchar',
'is_required' => 1,
'is_user_defined' => 1,
'is_unique' => 0,
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
],
];

foreach ($attributeData as $data) {
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
$attribute->setData($data);
$attribute->setIsStatic(true);
$attribute->save();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

$attributeCodes = [
'test_attribute',
];

foreach ($attributeCodes as $attributeCode) {
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
$attribute->loadByCode('catalog_product', $attributeCode);
if ($attribute->getId()) {
$attribute->delete();
}
}

0 comments on commit f9976b9

Please sign in to comment.