Skip to content

magento/magento2#12083: Cannot import zero (0) value into custom attribute #12283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}