Skip to content

Commit 41e9ec0

Browse files
committed
magento#12083: Cannot import zero (0) value into custom attribute
1 parent e18f1d2 commit 41e9ec0

File tree

4 files changed

+118
-18
lines changed

4 files changed

+118
-18
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
534534
public function clearEmptyData(array $rowData)
535535
{
536536
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
537-
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {
537+
if (!$attrParams['is_static'] && !isset($rowData[$attrCode])) {
538538
unset($rowData[$attrCode]);
539539
}
540540
}

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractTest.php

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,25 @@ class AbstractTest extends \PHPUnit\Framework\TestCase
1212
*/
1313
protected $_model;
1414

15+
/**
16+
* @var \Magento\TestFramework\ObjectManager
17+
*/
18+
private $objectManager;
19+
1520
/**
1621
* On product import abstract class methods level it doesn't matter what product type is using.
1722
* That is why current tests are using simple product entity type by default
1823
*/
1924
protected function setUp()
2025
{
21-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
22-
$params = [$objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class), 'simple'];
26+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
27+
$params = [$this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class), 'simple'];
2328
$this->_model = $this->getMockForAbstractClass(
2429
\Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType::class,
2530
[
26-
$objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class),
27-
$objectManager->get(\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class),
28-
$objectManager->get(\Magento\Framework\App\ResourceConnection::class),
31+
$this->objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class),
32+
$this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class),
33+
$this->objectManager->get(\Magento\Framework\App\ResourceConnection::class),
2934
$params
3035
]
3136
);
@@ -130,6 +135,11 @@ public function prepareAttributesWithDefaultValueForSaveDataProvider()
130135
}
131136

132137
/**
138+
* Test cleaning imported attribute data from empty values (note '0' is not empty).
139+
*
140+
* @magentoDbIsolation enabled
141+
* @magentoAppIsolation enabled
142+
* @magentoDataFixture Magento/CatalogImportExport/Model/Import/_files/custom_attributes.php
133143
* @dataProvider clearEmptyDataDataProvider
134144
*/
135145
public function testClearEmptyData($rowData, $expectedAttributes)
@@ -141,8 +151,14 @@ public function testClearEmptyData($rowData, $expectedAttributes)
141151
}
142152
}
143153

154+
/**
155+
* Data provider for testClearEmptyData.
156+
*
157+
* @return array
158+
*/
144159
public function clearEmptyDataDataProvider()
145160
{
161+
// We use sku attribute to test static attributes.
146162
return [
147163
[
148164
[
@@ -152,33 +168,57 @@ public function clearEmptyDataDataProvider()
152168
'product_type' => 'simple',
153169
'name' => 'Simple 01',
154170
'price' => 10,
171+
'test_attribute' => '1',
155172
],
156173
[
157174
'sku' => 'simple1',
158175
'store_view_code' => '',
159176
'_attribute_set' => 'Default',
160177
'product_type' => 'simple',
161178
'name' => 'Simple 01',
162-
'price' => 10
179+
'price' => 10,
180+
'test_attribute' => '1',
163181
],
164182
],
165183
[
166184
[
167-
'sku' => '',
168-
'store_view_code' => 'German',
185+
'sku' => '0',
186+
'store_view_code' => '',
169187
'_attribute_set' => 'Default',
170-
'product_type' => '',
171-
'name' => 'Simple 01 German',
172-
'price' => '',
188+
'product_type' => 'simple',
189+
'name' => 'Simple 01',
190+
'price' => 10,
191+
'test_attribute' => '0',
173192
],
174193
[
175-
'sku' => '',
176-
'store_view_code' => 'German',
194+
'sku' => '0',
195+
'store_view_code' => '',
177196
'_attribute_set' => 'Default',
178-
'product_type' => '',
179-
'name' => 'Simple 01 German'
180-
]
181-
]
197+
'product_type' => 'simple',
198+
'name' => 'Simple 01',
199+
'price' => 10,
200+
'test_attribute' => '0',
201+
],
202+
],
203+
[
204+
[
205+
'sku' => null,
206+
'store_view_code' => '',
207+
'_attribute_set' => 'Default',
208+
'product_type' => 'simple',
209+
'name' => 'Simple 01',
210+
'price' => 10,
211+
'test_attribute' => null,
212+
],
213+
[
214+
'sku' => null,
215+
'store_view_code' => '',
216+
'_attribute_set' => 'Default',
217+
'product_type' => 'simple',
218+
'name' => 'Simple 01',
219+
'price' => 10,
220+
],
221+
],
182222
];
183223
}
184224
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
/** @var \Magento\Eav\Model\Entity\Type $entityType */
10+
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class);
11+
$entityType->loadByCode('catalog_product');
12+
$entityTypeId = $entityType->getId();
13+
14+
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
15+
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
16+
$attributeSet->load('default', 'attribute_set_name');
17+
$attributeSetId = $attributeSet->getId();
18+
19+
$attributeGroupId = $attributeSet->getDefaultGroupId($entityType->getDefaultAttributeSetId());
20+
21+
$attributeData = [
22+
[
23+
'attribute_code' => 'test_attribute',
24+
'entity_type_id' => $entityTypeId,
25+
'backend_type' => 'varchar',
26+
'is_required' => 1,
27+
'is_user_defined' => 1,
28+
'is_unique' => 0,
29+
'attribute_set_id' => $attributeSetId,
30+
'attribute_group_id' => $attributeGroupId,
31+
],
32+
];
33+
34+
foreach ($attributeData as $data) {
35+
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
36+
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
37+
$attribute->setData($data);
38+
$attribute->setIsStatic(true);
39+
$attribute->save();
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
$attributeCodes = [
10+
'test_attribute',
11+
];
12+
13+
foreach ($attributeCodes as $attributeCode) {
14+
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
15+
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
16+
$attribute->loadByCode('catalog_product', $attributeCode);
17+
if ($attribute->getId()) {
18+
$attribute->delete();
19+
}
20+
}

0 commit comments

Comments
 (0)