diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 5681b1aa6607d..939d6b2de67ee 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -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]); } } diff --git a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractTest.php b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractTest.php index 8860c12f0f983..862ecb4cbe028 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractTest.php +++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractTest.php @@ -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 ] ); @@ -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) @@ -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 [ [ [ @@ -152,6 +174,7 @@ public function clearEmptyDataDataProvider() 'product_type' => 'simple', 'name' => 'Simple 01', 'price' => 10, + 'test_attribute' => '1', ], [ 'sku' => 'simple1', @@ -159,26 +182,49 @@ public function clearEmptyDataDataProvider() '_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, + ], + ], ]; } } diff --git a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/custom_attributes.php b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/custom_attributes.php new file mode 100644 index 0000000000000..82c22594f30aa --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/custom_attributes.php @@ -0,0 +1,40 @@ +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(); +} diff --git a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/custom_attributes_rollback.php b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/custom_attributes_rollback.php new file mode 100644 index 0000000000000..f3afb096347ed --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/custom_attributes_rollback.php @@ -0,0 +1,20 @@ +create(\Magento\Eav\Model\Entity\Attribute::class); + $attribute->loadByCode('catalog_product', $attributeCode); + if ($attribute->getId()) { + $attribute->delete(); + } +}