Skip to content

Commit 41c6717

Browse files
committed
MC-30189: Error Import exported .csv file with small profile generated data
1 parent 420a8b6 commit 41c6717

File tree

3 files changed

+88
-35
lines changed

3 files changed

+88
-35
lines changed

setup/src/Magento/Setup/Fixtures/AttributeSet/Pattern.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Setup\Fixtures\AttributeSet;
79

810
/**
@@ -32,7 +34,7 @@ class Pattern
3234
* @param string $name
3335
* @param int $attributesPerSet
3436
* @param int $optionsPerAttribute
35-
* @param callable $attributePattern callback in f($index, $attributeData) format
37+
* @param callable $attributePattern callback in f($index, $attributeData) format
3638
* @return array
3739
*/
3840
public function generateAttributeSet(
@@ -46,9 +48,9 @@ public function generateAttributeSet(
4648
'attributes' => []
4749
];
4850
for ($index = 1; $index <= $attributesPerSet; $index++) {
49-
$attributeData = $this->generateAttribute(
51+
$attributeData = $this->generateAttribute(
5052
$index,
51-
is_array($optionsPerAttribute) ? $optionsPerAttribute[$index-1] : $optionsPerAttribute
53+
is_array($optionsPerAttribute) ? $optionsPerAttribute[$index - 1] : $optionsPerAttribute
5254
);
5355
if (is_callable($attributePattern)) {
5456
$attributeData = $attributePattern($index, $attributeData);
@@ -72,7 +74,7 @@ private function generateAttribute($index, $optionsPerAttribute)
7274
$attribute['attribute_code'] = $attribute['attribute_code'] . $index;
7375
$attribute['frontend_label'] = $attribute['frontend_label'] . $index;
7476
$attribute['options'] = ['option' => $this->generateOptions($optionsPerAttribute)];
75-
$attribute['default_option'] = $attribute['options']['option'][0]['label'];
77+
$attribute['default_value'] = $attribute['options']['option'][0]['value'];
7678
return $attribute;
7779
}
7880

setup/src/Magento/Setup/Fixtures/SimpleProductsFixture.php

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Setup\Fixtures;
89

910
use Magento\Catalog\Model\Product;
1011
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1112
use Magento\Catalog\Model\ProductFactory;
13+
use Magento\Eav\Model\Entity\Attribute;
14+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
1215
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
16+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection as AttributeSetCollection;
1317
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
1418
use Magento\Setup\Model\FixtureGenerator\ProductGenerator;
1519
use Magento\Setup\Model\SearchTermDescriptionGeneratorFactory;
@@ -68,7 +72,7 @@ class SimpleProductsFixture extends Fixture
6872
private $defaultAttributeSetId;
6973

7074
/**
71-
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
75+
* @var Collection
7276
*/
7377
private $attributeCollectionFactory;
7478

@@ -97,6 +101,11 @@ class SimpleProductsFixture extends Fixture
97101
*/
98102
private $priceProvider;
99103

104+
/**
105+
* @var int[]
106+
*/
107+
private $additionalAttributeSetIds;
108+
100109
/**
101110
* @param FixtureModel $fixtureModel
102111
* @param ProductFactory $productFactory
@@ -184,35 +193,38 @@ public function execute()
184193
'Short simple product Description %s'
185194
);
186195

187-
$additionalAttributeSets = $this->getAdditionalAttributeSets();
188-
$attributeSet = function ($index) use ($defaultAttributeSets, $additionalAttributeSets) {
196+
$additionalAttributeSetIds = $this->getAdditionalAttributeSetIds();
197+
$attributeSet = function ($index) use ($defaultAttributeSets, $additionalAttributeSetIds) {
189198
// phpcs:ignore
190199
mt_srand($index);
191200
$attributeSetCount = count(array_keys($defaultAttributeSets));
192201
if ($attributeSetCount > (($index - 1) % (int)$this->fixtureModel->getValue('categories', 30))) {
193-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
202+
// phpcs:ignore Magento2.Security.InsecureFunction
194203
return array_keys($defaultAttributeSets)[mt_rand(0, count(array_keys($defaultAttributeSets)) - 1)];
195204
} else {
196-
$customSetsAmount = count($additionalAttributeSets);
205+
$customSetsAmount = count($additionalAttributeSetIds);
197206
return $customSetsAmount
198-
? $additionalAttributeSets[$index % count($additionalAttributeSets)]['attribute_set_id']
207+
? $additionalAttributeSetIds[$index % $customSetsAmount]
199208
: $this->getDefaultAttributeSetId();
200209
}
201210
};
202211

212+
$additionalAttributeValues = $this->getAdditionalAttributeValues();
203213
$additionalAttributes = function (
204214
$attributeSetId,
205215
$index
206216
) use (
207217
$defaultAttributeSets,
208-
$additionalAttributeSets
218+
$additionalAttributeValues
209219
) {
210220
$attributeValues = [];
211221
// phpcs:ignore
212222
mt_srand($index);
213-
if (isset($defaultAttributeSets[$attributeSetId])) {
214-
foreach ($defaultAttributeSets[$attributeSetId] as $attributeCode => $values) {
215-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
223+
$attributeValuesByAttributeSet = $defaultAttributeSets[$attributeSetId]
224+
?? $additionalAttributeValues[$attributeSetId];
225+
if (!empty($attributeValuesByAttributeSet)) {
226+
foreach ($attributeValuesByAttributeSet as $attributeCode => $values) {
227+
// phpcs:ignore Magento2.Security.InsecureFunction
216228
$attributeValues[$attributeCode] = $values[mt_rand(0, count($values) - 1)];
217229
}
218230
}
@@ -279,10 +291,10 @@ private function getDefaultAttributeSetId()
279291
}
280292

281293
/**
282-
* Get default attribute sets with attributes
294+
* Get default attribute sets with attributes.
283295
*
284-
* @see config/attributeSets.xml
285296
* @return array
297+
* @see config/attributeSets.xml
286298
*/
287299
private function getDefaultAttributeSets()
288300
{
@@ -301,17 +313,7 @@ private function getDefaultAttributeSets()
301313
'attribute_code',
302314
array_column($attributesData, 'attribute_code')
303315
);
304-
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
305-
foreach ($attributeCollection as $attribute) {
306-
$values = [];
307-
$options = $attribute->getOptions();
308-
foreach (($options ?: []) as $option) {
309-
if ($option->getValue()) {
310-
$values[] = $option->getValue();
311-
}
312-
}
313-
$attributes[$attribute->getAttributeSetId()][$attribute->getAttributeCode()] = $values;
314-
}
316+
$attributes = $this->processAttributeValues($attributeCollection, $attributes);
315317
}
316318
}
317319
$attributes[$this->getDefaultAttributeSetId()] = [];
@@ -381,16 +383,64 @@ private function readDescriptionConfig($configSrc)
381383
}
382384

383385
/**
384-
* Get additional attribute sets
386+
* Get additional attribute set ids.
387+
*
388+
* @return int[]
389+
*/
390+
private function getAdditionalAttributeSetIds()
391+
{
392+
if (null === $this->additionalAttributeSetIds) {
393+
/** @var AttributeSetCollection $sets */
394+
$sets = $this->attributeSetCollectionFactory->create();
395+
$sets->addFieldToFilter(
396+
'attribute_set_name',
397+
['like' => AttributeSetsFixture::PRODUCT_SET_NAME . '%']
398+
);
399+
$this->additionalAttributeSetIds = $sets->getAllIds();
400+
}
401+
402+
return $this->additionalAttributeSetIds;
403+
}
404+
405+
/**
406+
* Get values of additional attributes.
385407
*
386-
* @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection[]
408+
* @return array
387409
*/
388-
private function getAdditionalAttributeSets()
410+
private function getAdditionalAttributeValues(): array
389411
{
390-
/** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $sets */
391-
$sets = $this->attributeSetCollectionFactory->create();
392-
$sets->addFieldToFilter('attribute_set_name', ['like' => AttributeSetsFixture::PRODUCT_SET_NAME . '%']);
412+
$attributeCollection = $this->attributeCollectionFactory->create();
413+
$attributeCollection->setAttributeSetsFilter($this->getAdditionalAttributeSetIds())
414+
->addFieldToFilter('attribute_code', ['like' => 'attribute_set%']);
415+
$attributeCollection->getSelect()->columns(['entity_attribute.attribute_set_id']);
416+
417+
return $this->processAttributeValues($attributeCollection);
418+
}
393419

394-
return $sets->getData();
420+
/**
421+
* Maps attribute values by attribute set and attribute code.
422+
*
423+
* @param Collection $attributeCollection
424+
* @param array $attributes
425+
* @return array
426+
*/
427+
private function processAttributeValues(
428+
Collection $attributeCollection,
429+
array $attributes = []
430+
): array {
431+
/** @var Attribute $attribute */
432+
foreach ($attributeCollection as $attribute) {
433+
$values = [];
434+
$options = $attribute->getOptions() ?? [];
435+
$attributeSetId = $attribute->getAttributeSetId() ?? $this->getDefaultAttributeSetId();
436+
foreach ($options as $option) {
437+
if ($option->getValue()) {
438+
$values[] = $option->getValue();
439+
}
440+
}
441+
$attributes[$attributeSetId][$attribute->getAttributeCode()] = $values;
442+
}
443+
444+
return $attributes;
395445
}
396446
}

setup/src/Magento/Setup/Test/Unit/Fixtures/AttributeSet/PatternTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Setup\Test\Unit\Fixtures\AttributeSet;
89

@@ -28,7 +29,7 @@ public function testGenerateAttributeSet()
2829
'frontend_label' => 'Attribute 1',
2930
'frontend_input' => 'select',
3031
'backend_type' => 1,
31-
'default_option' => 'option 1',
32+
'default_value' => 'option_1',
3233
'options' => [
3334
'option' => [
3435
[

0 commit comments

Comments
 (0)