Skip to content

Commit

Permalink
Improved check for attribute codes in "additional_attributes" field
Browse files Browse the repository at this point in the history
  • Loading branch information
ccasciotti committed Apr 18, 2017
1 parent c30a05a commit 6cdb992
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2201,9 +2201,13 @@ private function reindexProducts($productIdsToReindex = [])
*/
public function retrieveAttributeByCode($attrCode)
{
/** @var string $attrCode */
$attrCode = mb_strtolower($attrCode);

if (!isset($this->_attributeCache[$attrCode])) {
$this->_attributeCache[$attrCode] = $this->getResource()->getAttribute($attrCode);
}

return $this->_attributeCache[$attrCode];
}

Expand Down Expand Up @@ -2537,6 +2541,7 @@ private function parseAttributesWithoutWrappedValues($attributesData)
continue;
}
list($code, $value) = explode(self::PAIR_NAME_VALUE_SEPARATOR, $attributeData, 2);
$code = mb_strtolower($code);
$preparedAttributes[$code] = $value;
}
return $preparedAttributes;
Expand All @@ -2563,7 +2568,7 @@ private function parseAttributesWithoutWrappedValues($attributesData)
private function parseAttributesWithWrappedValues($attributesData)
{
$attributes = [];
preg_match_all('~((?:[a-z0-9_])+)="((?:[^"]|""|"' . $this->getMultiLineSeparatorForRegexp() . '")+)"+~',
preg_match_all('~((?:[a-zA-Z0-9_])+)="((?:[^"]|""|"' . $this->getMultiLineSeparatorForRegexp() . '")+)"+~',
$attributesData,
$matches
);
Expand All @@ -2572,7 +2577,7 @@ private function parseAttributesWithWrappedValues($attributesData)
$value = 'multiselect' != $attribute->getFrontendInput()
? str_replace('""', '"', $matches[2][$i])
: '"' . $matches[2][$i] . '"';
$attributes[$attributeCode] = $value;
$attributes[mb_strtolower($attributeCode)] = $value;
}
return $attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,68 @@ public function testGetImagesFromRow($rowData, $expectedResult)
);
}

public function testParseAttributesWithoutWrappedValuesWillReturnsLowercasedAttributeCodes()
{
$attributesData = 'PARAM1=value1,param2=value2';
$preparedAttributes = $this->invokeMethod(
$this->importProduct,
'parseAttributesWithoutWrappedValues',
[$attributesData]
);

$this->assertArrayHasKey('param1', $preparedAttributes);
$this->assertEquals('value1', $preparedAttributes['param1']);

$this->assertArrayHasKey('param2', $preparedAttributes);
$this->assertEquals('value2', $preparedAttributes['param2']);

$this->assertArrayNotHasKey('PARAM1', $preparedAttributes);
}

public function testParseAttributesWithWrappedValuesWillReturnsLowercasedAttributeCodes()
{
$attribute1 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
->disableOriginalConstructor()
->setMethods(['getFrontendInput'])
->getMockForAbstractClass();

$attribute1->expects($this->once())
->method('getFrontendInput')
->willReturn('text');

$attribute2 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
->disableOriginalConstructor()
->setMethods(['getFrontendInput'])
->getMockForAbstractClass();

$attribute2->expects($this->once())
->method('getFrontendInput')
->willReturn('multiselect');

$attributeCache = [
'param1' => $attribute1,
'param2' => $attribute2,
];

$this->setPropertyValue($this->importProduct, '_attributeCache', $attributeCache);

$attributesData = 'PARAM1="value1",PARAM2="value2"';
$attributes = $this->invokeMethod(
$this->importProduct,
'parseAttributesWithWrappedValues',
[$attributesData]
);

$this->assertArrayHasKey('param1', $attributes);
$this->assertEquals('value1', $attributes['param1']);

$this->assertArrayHasKey('param2', $attributes);
$this->assertEquals('"value2"', $attributes['param2']);

$this->assertArrayNotHasKey('PARAM1', $attributes);
$this->assertArrayNotHasKey('PARAM2', $attributes);
}

public function getImagesFromRowDataProvider()
{
return [
Expand Down

0 comments on commit 6cdb992

Please sign in to comment.