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 8e18db911eb02..1e186e5a932e7 100755 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -431,7 +431,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) { if (!$attrParams['is_static']) { if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) { - $resultAttrs[$attrCode] = 'select' == $attrParams['type'] + $resultAttrs[$attrCode] = in_array($attrParams['type'], ['select', 'boolean']) ? $attrParams['options'][strtolower($rowData[$attrCode])] : $rowData[$attrCode]; if ('multiselect' == $attrParams['type']) { diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Validator.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Validator.php index 136317cc80e8e..fc134c38895bc 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Validator.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Validator.php @@ -135,6 +135,7 @@ public function isAttributeValid($attrCode, array $attrParams, array $rowData) $valid = $this->numericValidation($attrCode, $attrParams['type']); break; case 'select': + case 'boolean': case 'multiselect': $values = explode(Product::PSEUDO_MULTI_LINE_SEPARATOR, $rowData[$attrCode]); $valid = true; diff --git a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Type/AbstractTypeTest.php b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Type/AbstractTypeTest.php index 7b263cb88bf9e..76bbd0be1a897 100644 --- a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Type/AbstractTypeTest.php +++ b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Type/AbstractTypeTest.php @@ -117,40 +117,59 @@ protected function setUp() '', false ); - - $entityAttributes = [[ - 'attribute_id' => 'attribute_id', - 'attribute_set_name' => 'attributeSetName', - ]]; - - $this->entityModel->expects($this->any())->method('getEntityTypeId')->willReturn(3); - $this->entityModel->expects($this->any())->method('getAttributeOptions')->willReturn(['option1', 'option2']); - $attrSetColFactory->expects($this->any())->method('create')->willReturn($attrSetCollection); - $attrSetCollection->expects($this->any())->method('setEntityTypeFilter')->willReturn([$attributeSet]); - $attrColFactory->expects($this->any())->method('create')->willReturn($attrCollection); - $attrCollection->expects($this->any())->method('setAttributeSetFilter')->willReturn([$attribute]); - $attributeSet->expects($this->any())->method('getId')->willReturn(1); - $attributeSet->expects($this->any())->method('getAttributeSetName')->willReturn('attribute_set_name'); - $attribute->expects($this->any())->method('getAttributeCode')->willReturn('attr_code'); - $attribute->expects($this->any())->method('getId')->willReturn('1'); $attribute->expects($this->any())->method('getIsVisible')->willReturn(true); $attribute->expects($this->any())->method('getIsGlobal')->willReturn(true); $attribute->expects($this->any())->method('getIsRequired')->willReturn(true); $attribute->expects($this->any())->method('getIsUnique')->willReturn(true); $attribute->expects($this->any())->method('getFrontendLabel')->willReturn('frontend_label'); - $attribute->expects($this->any())->method('isStatic')->willReturn(true); $attribute->expects($this->any())->method('getApplyTo')->willReturn(['simple']); $attribute->expects($this->any())->method('getDefaultValue')->willReturn('default_value'); $attribute->expects($this->any())->method('usesSource')->willReturn(true); - $attribute->expects($this->any())->method('getFrontendInput')->willReturn('multiselect'); + + + $entityAttributes = [ + [ + 'attribute_id' => 'attribute_id', + 'attribute_set_name' => 'attributeSetName', + ], + [ + 'attribute_id' => 'boolean_attribute', + 'attribute_set_name' => 'attributeSetName' + ] + ]; + $attribute1 = clone $attribute; + $attribute2 = clone $attribute; + + $attribute1->expects($this->any())->method('getId')->willReturn('1'); + $attribute1->expects($this->any())->method('getAttributeCode')->willReturn('attr_code'); + $attribute1->expects($this->any())->method('getFrontendInput')->willReturn('multiselect'); + $attribute1->expects($this->any())->method('isStatic')->willReturn(true); + + $attribute2->expects($this->any())->method('getId')->willReturn('2'); + $attribute2->expects($this->any())->method('getAttributeCode')->willReturn('boolean_attribute'); + $attribute2->expects($this->any())->method('getFrontendInput')->willReturn('boolean'); + $attribute2->expects($this->any())->method('isStatic')->willReturn(false); + + $this->entityModel->expects($this->any())->method('getEntityTypeId')->willReturn(3); + $this->entityModel->expects($this->any())->method('getAttributeOptions')->willReturnOnConsecutiveCalls( + ['option1', 'option2'], + ['yes' => 1, 'no' => 0] + ); + $attrSetColFactory->expects($this->any())->method('create')->willReturn($attrSetCollection); + $attrSetCollection->expects($this->any())->method('setEntityTypeFilter')->willReturn([$attributeSet]); + $attrColFactory->expects($this->any())->method('create')->willReturn($attrCollection); + $attrCollection->expects($this->any())->method('setAttributeSetFilter')->willReturn([$attribute1, $attribute2]); + $attributeSet->expects($this->any())->method('getId')->willReturn(1); + $attributeSet->expects($this->any())->method('getAttributeSetName')->willReturn('attribute_set_name'); + $attrCollection ->expects($this->any()) ->method('addFieldToFilter') ->with( 'main_table.attribute_id', - ['in' => ['attribute_id']] + ['in' => ['attribute_id', 'boolean_attribute']] ) - ->willReturn([$attribute]); + ->willReturn([$attribute1, $attribute2]); $this->connection = $this->getMock( 'Magento\Framework\DB\Adapter\Pdo\Mysql', @@ -343,6 +362,7 @@ public function addAttributeOptionDataProvider() /** * @param $object * @param $property + * @return mixed */ protected function getPropertyValue(&$object, $property) { @@ -366,4 +386,14 @@ protected function setPropertyValue(&$object, $property, $value) $reflectionProperty->setValue($object, $value); return $object; } + + public function testPrepareAttributesWithDefaultValueForSave() + { + $rowData = [ + '_attribute_set' => 'attributeSetName', + 'boolean_attribute' => 'Yes' + ]; + $result = $this->simpleType->prepareAttributesWithDefaultValueForSave($rowData); + $this->assertEquals(['boolean_attribute' => 1], $result); + } } diff --git a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ValidatorTest.php b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ValidatorTest.php index 338c976d5649c..c358decc64084 100755 --- a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ValidatorTest.php +++ b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ValidatorTest.php @@ -74,6 +74,25 @@ protected function setUp() $this->validator->setContext($this->context)->init(); } + public function testIsBooleanAttributeValid() + { + $this->context->expects($this->any())->method('getBehavior') + ->willReturn(\Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE); + $result = $this->validator->isAttributeValid( + 'boolean_attribute', + [ + 'type' => 'boolean', + 'apply_to' => ['simple'], + 'is_required' => false + ], + [ + 'product_type' => 'simple', + 'boolean_attribute' => 'Yes' + ] + ); + $this->assertTrue($result); + } + public function testIsValidCorrect() { $value = ['product_type' => 'simple']; diff --git a/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php b/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php index a4a43e8ba839d..98ee9d755669b 100644 --- a/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php +++ b/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php @@ -399,7 +399,7 @@ public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute\Abstract foreach (is_array($option['value']) ? $option['value'] : [$option] as $innerOption) { if (strlen($innerOption['value'])) { // skip ' -- Please Select -- ' option - $options[$innerOption['value']] = $innerOption[$index]; + $options[$innerOption['value']] = (string)$innerOption[$index]; } } } diff --git a/app/code/Magento/ImportExport/Model/Import.php b/app/code/Magento/ImportExport/Model/Import.php index 4a42c4abab403..bdbcf53a3e3b0 100644 --- a/app/code/Magento/ImportExport/Model/Import.php +++ b/app/code/Magento/ImportExport/Model/Import.php @@ -314,10 +314,11 @@ public function getOperationResultMessages($validationResult) */ public static function getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { - if ($attribute->usesSource() && in_array($attribute->getFrontendInput(), ['select', 'multiselect'])) { - return $attribute->getFrontendInput() == 'multiselect' ? 'multiselect' : 'select'; + $frontendInput = $attribute->getFrontendInput(); + if ($attribute->usesSource() && in_array($frontendInput, ['select', 'multiselect', 'boolean'])) { + return $frontendInput; } elseif ($attribute->isStatic()) { - return $attribute->getFrontendInput() == 'date' ? 'datetime' : 'varchar'; + return $frontendInput == 'date' ? 'datetime' : 'varchar'; } else { return $attribute->getBackendType(); } diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/ImportTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/ImportTest.php index 1083fc1e15acf..ea9e21c1fe30f 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/ImportTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/ImportTest.php @@ -322,7 +322,13 @@ public function testGetOperationResultMessages() */ public function testGetAttributeType() { - $this->markTestIncomplete('This test has not been implemented yet.'); + /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ + $attribute = $this->getMockBuilder('\Magento\Eav\Model\Entity\Attribute\AbstractAttribute') + ->setMethods(['getFrontendInput', 'usesSource']) + ->disableOriginalConstructor()->getMock(); + $attribute->expects($this->any())->method('getFrontendInput')->willReturn('boolean'); + $attribute->expects($this->any())->method('usesSource')->willReturn(true); + $this->assertEquals('boolean', $this->import->getAttributeType($attribute)); } /**