Skip to content

Commit 0dd84cd

Browse files
⏫ Forwardport of #12283 to 2.3-develop branch
1 parent 8e77e2f commit 0dd84cd

File tree

4 files changed

+124
-18
lines changed

4 files changed

+124
-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: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,31 @@ 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(
32+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory::class
33+
),
34+
$this->objectManager->get(
35+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class
36+
),
37+
$this->objectManager->get(
38+
\Magento\Framework\App\ResourceConnection::class
39+
),
2940
$params
3041
]
3142
);
@@ -130,6 +141,11 @@ public function prepareAttributesWithDefaultValueForSaveDataProvider()
130141
}
131142

132143
/**
144+
* Test cleaning imported attribute data from empty values (note '0' is not empty).
145+
*
146+
* @magentoDbIsolation enabled
147+
* @magentoAppIsolation enabled
148+
* @magentoDataFixture Magento/CatalogImportExport/Model/Import/_files/custom_attributes.php
133149
* @dataProvider clearEmptyDataDataProvider
134150
*/
135151
public function testClearEmptyData($rowData, $expectedAttributes)
@@ -141,8 +157,14 @@ public function testClearEmptyData($rowData, $expectedAttributes)
141157
}
142158
}
143159

160+
/**
161+
* Data provider for testClearEmptyData.
162+
*
163+
* @return array
164+
*/
144165
public function clearEmptyDataDataProvider()
145166
{
167+
// We use sku attribute to test static attributes.
146168
return [
147169
[
148170
[
@@ -152,33 +174,57 @@ public function clearEmptyDataDataProvider()
152174
'product_type' => 'simple',
153175
'name' => 'Simple 01',
154176
'price' => 10,
177+
'test_attribute' => '1',
155178
],
156179
[
157180
'sku' => 'simple1',
158181
'store_view_code' => '',
159182
'_attribute_set' => 'Default',
160183
'product_type' => 'simple',
161184
'name' => 'Simple 01',
162-
'price' => 10
185+
'price' => 10,
186+
'test_attribute' => '1',
163187
],
164188
],
165189
[
166190
[
167-
'sku' => '',
168-
'store_view_code' => 'German',
191+
'sku' => '0',
192+
'store_view_code' => '',
169193
'_attribute_set' => 'Default',
170-
'product_type' => '',
171-
'name' => 'Simple 01 German',
172-
'price' => '',
194+
'product_type' => 'simple',
195+
'name' => 'Simple 01',
196+
'price' => 10,
197+
'test_attribute' => '0',
173198
],
174199
[
175-
'sku' => '',
176-
'store_view_code' => 'German',
200+
'sku' => '0',
201+
'store_view_code' => '',
177202
'_attribute_set' => 'Default',
178-
'product_type' => '',
179-
'name' => 'Simple 01 German'
180-
]
181-
]
203+
'product_type' => 'simple',
204+
'name' => 'Simple 01',
205+
'price' => 10,
206+
'test_attribute' => '0',
207+
],
208+
],
209+
[
210+
[
211+
'sku' => null,
212+
'store_view_code' => '',
213+
'_attribute_set' => 'Default',
214+
'product_type' => 'simple',
215+
'name' => 'Simple 01',
216+
'price' => 10,
217+
'test_attribute' => null,
218+
],
219+
[
220+
'sku' => null,
221+
'store_view_code' => '',
222+
'_attribute_set' => 'Default',
223+
'product_type' => 'simple',
224+
'name' => 'Simple 01',
225+
'price' => 10,
226+
],
227+
],
182228
];
183229
}
184230
}
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)