Skip to content

Commit 8db32a7

Browse files
committed
MAGETWO-83820: [2.3.x] SKU parameter in capital letters on CSV ignored by database in Import but OK'd by validator
1 parent d76b6c1 commit 8db32a7

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Api\Data\ProductInterface;
1212
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
1313
use Magento\Framework\EntityManager\MetadataPool;
14+
use Magento\Framework\Exception\LocalizedException;
1415

1516
/**
1617
* Importing configurable products
@@ -32,16 +33,24 @@ class Configurable extends \Magento\CatalogImportExport\Model\Import\Product\Typ
3233

3334
const ERROR_DUPLICATED_VARIATIONS = 'duplicatedVariations';
3435

36+
const ERROR_UNIDENTIFIABLE_VARIATION = 'unidentifiableVariation';
37+
3538
/**
3639
* Validation failure message template definitions
3740
*
3841
* @var array
3942
*/
4043
protected $_messageTemplates = [
41-
self::ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER => 'Attribute with code "%s" is not super',
42-
self::ERROR_INVALID_OPTION_VALUE => 'Invalid option value for attribute "%s"',
43-
self::ERROR_INVALID_WEBSITE => 'Invalid website code for super attribute',
44-
self::ERROR_DUPLICATED_VARIATIONS => 'SKU %s contains duplicated variations',
44+
self::ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER =>
45+
'Attribute with code "%s" is not super',
46+
self::ERROR_INVALID_OPTION_VALUE =>
47+
'Invalid option value for attribute "%s"',
48+
self::ERROR_INVALID_WEBSITE =>
49+
'Invalid website code for super attribute',
50+
self::ERROR_DUPLICATED_VARIATIONS =>
51+
'SKU %s contains duplicated variations',
52+
self::ERROR_UNIDENTIFIABLE_VARIATION =>
53+
'Configurable variation "%s" is unidentifiable',
4554
];
4655

4756
/**
@@ -470,13 +479,14 @@ protected function _processSuperData()
470479
* @param array $rowData
471480
*
472481
* @return array
482+
* @throws LocalizedException
473483
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
474484
* @SuppressWarnings(PHPMD.NPathComplexity)
475485
*/
476486
protected function _parseVariations($rowData)
477487
{
478488
$additionalRows = [];
479-
if (!isset($rowData['configurable_variations'])) {
489+
if (empty($rowData['configurable_variations'])) {
480490
return $additionalRows;
481491
}
482492
$variations = explode(ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR, $rowData['configurable_variations']);
@@ -489,7 +499,8 @@ protected function _parseVariations($rowData)
489499
$nameAndValue = explode(ImportProduct::PAIR_NAME_VALUE_SEPARATOR, $nameAndValue);
490500
if (!empty($nameAndValue)) {
491501
$value = isset($nameAndValue[1]) ? trim($nameAndValue[1]) : '';
492-
$fieldName = trim($nameAndValue[0]);
502+
// Ignoring field names' case.
503+
$fieldName = strtolower(trim($nameAndValue[0]));
493504
if ($fieldName) {
494505
$fieldAndValuePairs[$fieldName] = $value;
495506
}
@@ -510,8 +521,19 @@ protected function _parseVariations($rowData)
510521
$additionalRow = [];
511522
$position += 1;
512523
}
524+
} else {
525+
$errorCode = self::ERROR_UNIDENTIFIABLE_VARIATION;
526+
throw new LocalizedException(
527+
__(
528+
sprintf(
529+
$this->_messageTemplates[$errorCode],
530+
$variation
531+
)
532+
)
533+
);
513534
}
514535
}
536+
515537
return $additionalRows;
516538
}
517539

@@ -822,7 +844,14 @@ protected function configurableInBunch($bunch)
822844
public function isRowValid(array $rowData, $rowNum, $isNewProduct = true)
823845
{
824846
$error = false;
825-
$dataWithExtraVirtualRows = $this->_parseVariations($rowData);
847+
try {
848+
$dataWithExtraVirtualRows = $this->_parseVariations($rowData);
849+
} catch (LocalizedException $exception) {
850+
$this->_entityModel->addRowError($exception->getMessage(), $rowNum);
851+
852+
return false;
853+
}
854+
826855
$skus = [];
827856
$rowData['price'] = isset($rowData['price']) && $rowData['price'] ? $rowData['price'] : '0.00';
828857
if (!empty($dataWithExtraVirtualRows)) {
@@ -846,6 +875,7 @@ public function isRowValid(array $rowData, $rowNum, $isNewProduct = true)
846875
}
847876
$error |= !parent::isRowValid($option, $rowNum, $isNewProduct);
848877
}
878+
849879
return !$error;
850880
}
851881

app/code/Magento/ConfigurableImportExport/Test/Unit/Model/Import/Product/Type/ConfigurableTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,56 @@ public function testIsRowValid()
560560
'_type' => 'configurable',
561561
'_product_websites' => 'website_1',
562562
];
563+
// Checking that variations' field names are case-insensitive with this
564+
// product.
565+
$caseInsensitiveSKU = 'configurableskuI22CaseInsensitive';
566+
$caseInsensitiveProduct = [
567+
'sku' => $caseInsensitiveSKU,
568+
'store_view_code' => null,
569+
'attribute_set_code' => 'Default',
570+
'product_type' => 'configurable',
571+
'name' => 'Configurable Product 21',
572+
'product_websites' => 'website_1',
573+
'configurable_variation_labels' => 'testattr2=Select Color, testattr3=Select Size',
574+
'configurable_variations' => 'SKU=testconf2-attr2val1-testattr3v1,'
575+
. 'testattr2=attr2val1,'
576+
. 'testattr3=testattr3v1,'
577+
. 'display=1|sku=testconf2-attr2val1-testattr3v2,'
578+
. 'testattr2=attr2val1,'
579+
. 'testattr3=testattr3v2,'
580+
. 'display=0',
581+
'_store' => null,
582+
'_attribute_set' => 'Default',
583+
'_type' => 'configurable',
584+
'_product_websites' => 'website_1',
585+
];
563586
$bunch[] = $badProduct;
587+
$bunch[] = $caseInsensitiveProduct;
564588
// Set _attributes to avoid error in Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType.
565589
$this->setPropertyValue($this->configurable, '_attributes', [
566590
$badProduct[\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET] => [],
567591
]);
592+
// Avoiding errors about attributes not being super
593+
$this->setPropertyValue(
594+
$this->configurable,
595+
'_superAttributes',
596+
[
597+
'testattr2' => ['options' => ['attr2val1' => 1]],
598+
'testattr3' => [
599+
'options' => [
600+
'testattr3v2' => 1,
601+
'testattr3v1' => 1,
602+
],
603+
],
604+
]
605+
);
568606

569607
foreach ($bunch as $rowData) {
570608
$result = $this->configurable->isRowValid($rowData, 0, !isset($this->_oldSku[$rowData['sku']]));
571609
$this->assertNotNull($result);
610+
if ($rowData['sku'] === $caseInsensitiveSKU) {
611+
$this->assertTrue($result);
612+
}
572613
}
573614
}
574615

0 commit comments

Comments
 (0)