Skip to content

Commit 8a73ff1

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-69857' into 2.1-develop-pr36
2 parents f93d674 + 6bc464e commit 8a73ff1

File tree

4 files changed

+110
-33
lines changed

4 files changed

+110
-33
lines changed

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
namespace Magento\CustomerImportExport\Model\Import;
77

88
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source as Sources;
9+
use Magento\Framework\Stdlib\DateTime;
910
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1213
use Magento\Store\Model\Store;
14+
use Magento\ImportExport\Model\Import;
1315

1416
/**
1517
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -21,7 +23,7 @@ class Address extends AbstractCustomer
2123
/**#@+
2224
* Attribute collection name
2325
*/
24-
const ATTRIBUTE_COLLECTION_NAME = 'Magento\Customer\Model\ResourceModel\Address\Attribute\Collection';
26+
const ATTRIBUTE_COLLECTION_NAME = \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection::class;
2527

2628
/**#@-*/
2729

@@ -217,7 +219,7 @@ class Address extends AbstractCustomer
217219
protected $_addressFactory;
218220

219221
/**
220-
* @var \Magento\Framework\Stdlib\DateTime
222+
* @var DateTime
221223
*/
222224
protected $dateTime;
223225

@@ -270,7 +272,7 @@ class Address extends AbstractCustomer
270272
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
271273
* @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory
272274
* @param \Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory
273-
* @param \Magento\Framework\Stdlib\DateTime $dateTime
275+
* @param DateTime $dateTime
274276
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
275277
* @param array $data
276278
* @param Sources\CountryWithWebsites|null $countryWithWebsites
@@ -294,7 +296,7 @@ public function __construct(
294296
\Magento\Customer\Model\CustomerFactory $customerFactory,
295297
\Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory,
296298
\Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory,
297-
\Magento\Framework\Stdlib\DateTime $dateTime,
299+
DateTime $dateTime,
298300
\Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator,
299301
array $data = [],
300302
Sources\CountryWithWebsites $countryWithWebsites = null
@@ -586,12 +588,8 @@ protected function _prepareDataForUpdate(array $rowData)
586588
$defaults = [];
587589
$newAddress = true;
588590
// get address id
589-
if (isset(
590-
$this->_addresses[$customerId]
591-
) && in_array(
592-
$rowData[self::COLUMN_ADDRESS_ID],
593-
$this->_addresses[$customerId]
594-
)
591+
if (isset($this->_addresses[$customerId])
592+
&& in_array($rowData[self::COLUMN_ADDRESS_ID], $this->_addresses[$customerId])
595593
) {
596594
$newAddress = false;
597595
$addressId = $rowData[self::COLUMN_ADDRESS_ID];
@@ -601,7 +599,7 @@ protected function _prepareDataForUpdate(array $rowData)
601599
$entityRow = [
602600
'entity_id' => $addressId,
603601
'parent_id' => $customerId,
604-
'updated_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
602+
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
605603
];
606604
$websiteId = $this->_websiteCodeToId[$rowData[self::COLUMN_WEBSITE]];
607605

@@ -624,7 +622,16 @@ protected function _prepareDataForUpdate(array $rowData)
624622
$value = $attributeParams['options'][strtolower($rowData[$attributeAlias])];
625623
} elseif ('datetime' == $attributeParams['type']) {
626624
$value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
627-
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
625+
$value = $value->format(DateTime::DATETIME_PHP_FORMAT);
626+
} elseif ('multiselect' == $attributeParams['type']) {
627+
$separator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
628+
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
629+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
630+
$value = str_replace(
631+
$separator,
632+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR,
633+
$rowData[$attributeAlias]
634+
);
628635
} else {
629636
$value = $rowData[$attributeAlias];
630637
}
@@ -645,22 +652,11 @@ protected function _prepareDataForUpdate(array $rowData)
645652
}
646653

647654
// let's try to find region ID
648-
$entityRow['region_id'] = null;
649-
if (!empty($rowData[self::COLUMN_REGION])) {
650-
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
651-
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
652-
653-
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
654-
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
655-
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
656-
$entityRow['region_id'] = $regionId;
657-
}
658-
}
655+
$entityRow = $this->fillRegionData($rowData, $entityRow);
659656

660657
if ($newAddress) {
661658
$entityRowNew = $entityRow;
662-
$entityRowNew['created_at'] =
663-
(new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
659+
$entityRowNew['created_at'] = (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT);
664660
} else {
665661
$entityRowUpdate = $entityRow;
666662
}
@@ -839,7 +835,16 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
839835
continue;
840836
}
841837
if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
842-
$this->isAttributeValid($attributeCode, $attributeParams, $rowData, $rowNumber);
838+
$multiSeparator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
839+
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
840+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
841+
$this->isAttributeValid(
842+
$attributeCode,
843+
$attributeParams,
844+
$rowData,
845+
$rowNumber,
846+
$multiSeparator
847+
);
843848
} elseif ($attributeParams['is_required'] && (!isset(
844849
$this->_addresses[$customerId]
845850
) || !in_array(
@@ -940,4 +945,28 @@ public function setCustomerAttributes($customerAttributes)
940945
$this->_customerAttributes = $customerAttributes;
941946
return $this;
942947
}
948+
949+
/**
950+
* Try to get region_id and set it to $entityRow.
951+
*
952+
* @param array $rowData
953+
* @param array $entityRow
954+
* @return array
955+
*/
956+
private function fillRegionData(array $rowData, array $entityRow)
957+
{
958+
$entityRow['region_id'] = null;
959+
if (!empty($rowData[self::COLUMN_REGION])) {
960+
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
961+
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
962+
963+
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
964+
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
965+
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
966+
$entityRow['region_id'] = $regionId;
967+
}
968+
}
969+
970+
return $entityRow;
971+
}
943972
}

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
99
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1010
use Magento\Framework\App\ResourceConnection;
11+
use Magento\ImportExport\Model\Import;
1112

1213
/**
1314
* Import entity abstract model
@@ -638,11 +639,17 @@ public function getMasterAttributeCode()
638639
* @param array $attributeParams Attribute params
639640
* @param array $rowData Row data
640641
* @param int $rowNumber
642+
* @param string $multiSeparator
641643
* @return bool
642644
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
643645
*/
644-
public function isAttributeValid($attributeCode, array $attributeParams, array $rowData, $rowNumber)
645-
{
646+
public function isAttributeValid(
647+
$attributeCode,
648+
array $attributeParams,
649+
array $rowData,
650+
$rowNumber,
651+
$multiSeparator = Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
652+
) {
646653
$message = '';
647654
switch ($attributeParams['type']) {
648655
case 'varchar':
@@ -657,7 +664,13 @@ public function isAttributeValid($attributeCode, array $attributeParams, array $
657664
break;
658665
case 'select':
659666
case 'multiselect':
660-
$valid = isset($attributeParams['options'][strtolower($rowData[$attributeCode])]);
667+
$valid = true;
668+
foreach (explode($multiSeparator, strtolower($rowData[$attributeCode])) as $value) {
669+
$valid = isset($attributeParams['options'][$value]);
670+
if (!$valid) {
671+
break;
672+
}
673+
}
661674
$message = self::ERROR_INVALID_ATTRIBUTE_OPTION;
662675
break;
663676
case 'int':

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEav.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class AbstractEav extends \Magento\ImportExport\Model\Import\AbstractEn
1616
/**
1717
* Attribute collection name
1818
*/
19-
const ATTRIBUTE_COLLECTION_NAME = 'Magento\Framework\Data\Collection';
19+
const ATTRIBUTE_COLLECTION_NAME = \Magento\Framework\Data\Collection::class;
2020

2121
/**
2222
* Store manager
@@ -222,9 +222,14 @@ public function getAttributeOptions(
222222
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
223223
$value = is_array($option['value']) ? $option['value'] : [$option];
224224
foreach ($value as $innerOption) {
225+
// skip ' -- Please Select -- ' option
225226
if (strlen($innerOption['value'])) {
226-
// skip ' -- Please Select -- ' option
227-
$options[strtolower($innerOption[$index])] = $innerOption['value'];
227+
if ($attribute->isStatic()) {
228+
$options[strtolower($innerOption[$index])] = $innerOption['value'];
229+
} else {
230+
// Non-static attributes flip keys an values
231+
$options[$innerOption['value']] = $innerOption[$index];
232+
}
228233
}
229234
}
230235
}

app/code/Magento/ImportExport/Test/Unit/Model/Import/EntityAbstractTest.php renamed to app/code/Magento/ImportExport/Test/Unit/Model/Import/AbstractEntityTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Magento\ImportExport\Model\Import\AbstractEntity;
1515

16-
class EntityAbstractTest extends \Magento\ImportExport\Test\Unit\Model\Import\AbstractImportTestCase
16+
class AbstractEntityTest extends \Magento\ImportExport\Test\Unit\Model\Import\AbstractImportTestCase
1717
{
1818
/**
1919
* Abstract import entity model
@@ -402,6 +402,36 @@ public function testIsAttributeValid(array $data)
402402
$this->assertEquals(1, $this->_model->getErrorAggregator()->getErrorsCount(), 'Wrong count of errors');
403403
}
404404

405+
/**
406+
* Test for method isAttributeValid() for multiselect attribute with custom separator.
407+
*
408+
* @return void
409+
*/
410+
public function testMultiSelectIsAttributeValidWithCustomSeparator()
411+
{
412+
$data = $this->_getDataSet(
413+
'test3',
414+
'select',
415+
'1;3',
416+
'custom',
417+
null,
418+
[1 => 'test1', 2 => 'test2', 3 => 'test3']
419+
);
420+
$attributeCode = $data['code'];
421+
$attributeParams = [
422+
'type' => $data['type'],
423+
'options' => isset($data['options']) ? $data['options'] : null,
424+
'is_unique' => isset($data['is_unique']) ? $data['is_unique'] : null,
425+
];
426+
427+
$rowData = [$attributeCode => $data['valid_value']];
428+
$this->assertTrue($this->_model->isAttributeValid($attributeCode, $attributeParams, $rowData, 0, ';'));
429+
430+
$rowData[$attributeCode] = $data['invalid_value'];
431+
$this->assertFalse($this->_model->isAttributeValid($attributeCode, $attributeParams, $rowData, 0, ';'));
432+
$this->assertEquals(1, $this->_model->getErrorAggregator()->getErrorsCount(), 'Wrong count of errors');
433+
}
434+
405435
/**
406436
* Data provide which retrieve data for test attributes
407437
*

0 commit comments

Comments
 (0)