Skip to content

Commit 7a4a953

Browse files
committed
MC-29009: Error Import exported .csv file with small profile generated data
1 parent f956769 commit 7a4a953

File tree

3 files changed

+85
-35
lines changed

3 files changed

+85
-35
lines changed

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

Lines changed: 5 additions & 4 deletions
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+
67
namespace Magento\Setup\Fixtures\AttributeSet;
78

89
/**
@@ -32,7 +33,7 @@ class Pattern
3233
* @param string $name
3334
* @param int $attributesPerSet
3435
* @param int $optionsPerAttribute
35-
* @param callable $attributePattern callback in f($index, $attributeData) format
36+
* @param callable $attributePattern callback in f($index, $attributeData) format
3637
* @return array
3738
*/
3839
public function generateAttributeSet(
@@ -46,9 +47,9 @@ public function generateAttributeSet(
4647
'attributes' => []
4748
];
4849
for ($index = 1; $index <= $attributesPerSet; $index++) {
49-
$attributeData = $this->generateAttribute(
50+
$attributeData = $this->generateAttribute(
5051
$index,
51-
is_array($optionsPerAttribute) ? $optionsPerAttribute[$index-1] : $optionsPerAttribute
52+
is_array($optionsPerAttribute) ? $optionsPerAttribute[$index - 1] : $optionsPerAttribute
5253
);
5354
if (is_callable($attributePattern)) {
5455
$attributeData = $attributePattern($index, $attributeData);
@@ -72,7 +73,7 @@ private function generateAttribute($index, $optionsPerAttribute)
7273
$attribute['attribute_code'] = $attribute['attribute_code'] . $index;
7374
$attribute['frontend_label'] = $attribute['frontend_label'] . $index;
7475
$attribute['options'] = ['option' => $this->generateOptions($optionsPerAttribute)];
75-
$attribute['default_option'] = $attribute['options']['option'][0]['label'];
76+
$attribute['default_value'] = $attribute['options']['option'][0]['value'];
7677
return $attribute;
7778
}
7879

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

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
use Magento\Catalog\Model\Product;
1010
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1111
use Magento\Catalog\Model\ProductFactory;
12+
use Magento\Eav\Model\Entity\Attribute;
13+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
1214
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
15+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection as AttributeSetCollection;
1316
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
1417
use Magento\Setup\Model\FixtureGenerator\ProductGenerator;
1518
use Magento\Setup\Model\SearchTermDescriptionGeneratorFactory;
@@ -68,7 +71,7 @@ class SimpleProductsFixture extends Fixture
6871
private $defaultAttributeSetId;
6972

7073
/**
71-
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
74+
* @var Collection
7275
*/
7376
private $attributeCollectionFactory;
7477

@@ -97,6 +100,11 @@ class SimpleProductsFixture extends Fixture
97100
*/
98101
private $priceProvider;
99102

103+
/**
104+
* @var int[]
105+
*/
106+
private $additionalAttributeSetIds;
107+
100108
/**
101109
* @param FixtureModel $fixtureModel
102110
* @param ProductFactory $productFactory
@@ -184,35 +192,38 @@ public function execute()
184192
'Short simple product Description %s'
185193
);
186194

187-
$additionalAttributeSets = $this->getAdditionalAttributeSets();
188-
$attributeSet = function ($index) use ($defaultAttributeSets, $additionalAttributeSets) {
195+
$additionalAttributeSetIds = $this->getAdditionalAttributeSetIds();
196+
$attributeSet = function ($index) use ($defaultAttributeSets, $additionalAttributeSetIds) {
189197
// phpcs:ignore
190198
mt_srand($index);
191199
$attributeSetCount = count(array_keys($defaultAttributeSets));
192200
if ($attributeSetCount > (($index - 1) % (int)$this->fixtureModel->getValue('categories', 30))) {
193-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
201+
// phpcs:ignore Magento2.Security.InsecureFunction
194202
return array_keys($defaultAttributeSets)[mt_rand(0, count(array_keys($defaultAttributeSets)) - 1)];
195203
} else {
196-
$customSetsAmount = count($additionalAttributeSets);
204+
$customSetsAmount = count($additionalAttributeSetIds);
197205
return $customSetsAmount
198-
? $additionalAttributeSets[$index % count($additionalAttributeSets)]['attribute_set_id']
206+
? $additionalAttributeSetIds[$index % $customSetsAmount]
199207
: $this->getDefaultAttributeSetId();
200208
}
201209
};
202210

211+
$additionalAttributeValues = $this->getAdditionalAttributeValues();
203212
$additionalAttributes = function (
204213
$attributeSetId,
205214
$index
206215
) use (
207216
$defaultAttributeSets,
208-
$additionalAttributeSets
217+
$additionalAttributeValues
209218
) {
210219
$attributeValues = [];
211220
// phpcs:ignore
212221
mt_srand($index);
213-
if (isset($defaultAttributeSets[$attributeSetId])) {
214-
foreach ($defaultAttributeSets[$attributeSetId] as $attributeCode => $values) {
215-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
222+
$attributeValuesByAttributeSet = $defaultAttributeSets[$attributeSetId]
223+
?? $additionalAttributeValues[$attributeSetId];
224+
if (!empty($attributeValuesByAttributeSet)) {
225+
foreach ($attributeValuesByAttributeSet as $attributeCode => $values) {
226+
// phpcs:ignore Magento2.Security.InsecureFunction
216227
$attributeValues[$attributeCode] = $values[mt_rand(0, count($values) - 1)];
217228
}
218229
}
@@ -279,10 +290,10 @@ private function getDefaultAttributeSetId()
279290
}
280291

281292
/**
282-
* Get default attribute sets with attributes
293+
* Get default attribute sets with attributes.
283294
*
284-
* @see config/attributeSets.xml
285295
* @return array
296+
* @see config/attributeSets.xml
286297
*/
287298
private function getDefaultAttributeSets()
288299
{
@@ -301,17 +312,7 @@ private function getDefaultAttributeSets()
301312
'attribute_code',
302313
array_column($attributesData, 'attribute_code')
303314
);
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-
}
315+
$attributes = $this->processAttributeValues($attributeCollection, $attributes);
315316
}
316317
}
317318
$attributes[$this->getDefaultAttributeSetId()] = [];
@@ -381,16 +382,64 @@ private function readDescriptionConfig($configSrc)
381382
}
382383

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testGenerateAttributeSet()
2828
'frontend_label' => 'Attribute 1',
2929
'frontend_input' => 'select',
3030
'backend_type' => 1,
31-
'default_option' => 'option 1',
31+
'default_value' => 'option_1',
3232
'options' => [
3333
'option' => [
3434
[

0 commit comments

Comments
 (0)