Skip to content

Commit 6831e52

Browse files
committed
#31332:Shema changes implementation for configurable product option selection - added tests for media gallery
1 parent 55f90b4 commit 6831e52

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/ConfigurableOptionsSelectionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Eav\Api\Data\AttributeInterface;
1313
use Magento\Eav\Model\AttributeRepository;
1414
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\Uid;
1516
use Magento\Indexer\Model\IndexerFactory;
1617
use Magento\TestFramework\Helper\Bootstrap;
1718
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -36,6 +37,11 @@ class ConfigurableOptionsSelectionTest extends GraphQlAbstract
3637
*/
3738
private $indexerFactory;
3839

40+
/**
41+
* @var Uid
42+
*/
43+
private $idEncoder;
44+
3945
private $firstConfigurableAttribute;
4046

4147
private $secondConfigurableAttribute;
@@ -48,6 +54,7 @@ protected function setUp(): void
4854
$this->attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepository::class);
4955
$this->selectionUidFormatter = Bootstrap::getObjectManager()->create(SelectionUidFormatter::class);
5056
$this->indexerFactory = Bootstrap::getObjectManager()->create(IndexerFactory::class);
57+
$this->idEncoder = Bootstrap::getObjectManager()->create(Uid::class);
5158
}
5259

5360
/**
@@ -85,6 +92,8 @@ public function testSelectedFirstAttributeFirstOption(): void
8592
$product['configurable_product_options_selection']['configurable_options'][0]['values']
8693
)
8794
);
95+
96+
$this->assertMediaGallery($product);
8897
}
8998

9099
/**
@@ -120,6 +129,18 @@ public function testSelectedVariant(): void
120129
self::assertEquals($sku, $product['sku']);
121130
self::assertEmpty($product['configurable_product_options_selection']['configurable_options']);
122131
self::assertNotNull($product['configurable_product_options_selection']['variant']);
132+
133+
$variantId = $this->idEncoder->decode($product['configurable_product_options_selection']['variant']['uid']);
134+
self::assertIsNumeric($variantId);
135+
self::assertIsString($product['configurable_product_options_selection']['variant']['sku']);
136+
$urlKey = 'configurable-option-first-option-1-second-option-1';
137+
self::assertEquals($urlKey, $product['configurable_product_options_selection']['variant']['url_key']);
138+
self::assertMatchesRegularExpression(
139+
"/{$urlKey}/",
140+
$product['configurable_product_options_selection']['variant']['url_path']
141+
);
142+
143+
$this->assertMediaGallery($product);
123144
}
124145

125146
/**
@@ -161,6 +182,8 @@ public function testWithoutSelectedOption(): void
161182
$product['configurable_product_options_selection']['configurable_options'][1]['values']
162183
)
163184
);
185+
186+
$this->assertMediaGallery($product);
164187
}
165188

166189
/**
@@ -345,4 +368,17 @@ private function getOptionsUids(array $options): array
345368
}
346369
return $uids;
347370
}
371+
372+
/**
373+
* Assert media gallery fields
374+
*
375+
* @param array $product
376+
*/
377+
private function assertMediaGallery(array $product): void
378+
{
379+
self::assertNotEmpty($product['configurable_product_options_selection']['media_gallery']);
380+
$image = current($product['configurable_product_options_selection']['media_gallery']);
381+
self::assertIsString($image['url']);
382+
self::assertEquals(false, $image['disabled']);
383+
}
348384
}

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/configurable_products_with_two_attributes_combination.php

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
use Magento\Catalog\Api\ProductRepositoryInterface;
78
use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexerProcessor;
89
use Magento\Catalog\Model\Product;
910
use Magento\Catalog\Model\Product\Attribute\Source\Status;
11+
use Magento\Catalog\Model\Product\Media\Config;
1012
use Magento\Catalog\Model\Product\Type;
1113
use Magento\Catalog\Model\Product\Visibility;
1214
use Magento\Catalog\Setup\CategorySetup;
@@ -15,8 +17,11 @@
1517
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1618
use Magento\Eav\Api\Data\AttributeOptionInterface;
1719
use Magento\Framework\Api\Data\ImageContentInterface;
20+
use Magento\Framework\Filesystem;
21+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1822
use Magento\TestFramework\Helper\Bootstrap;
1923
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
24+
use Magento\Framework\App\Filesystem\DirectoryList;
2025

2126
Resolver::getInstance()->requireDataFixture(
2227
'Magento/ConfigurableProduct/_files/configurable_attribute_first.php'
@@ -25,18 +30,31 @@
2530
'Magento/ConfigurableProduct/_files/configurable_attribute_second.php'
2631
);
2732

33+
$objectManager = Bootstrap::getObjectManager();
34+
2835
/** @var ProductRepositoryInterface $productRepository */
29-
$productRepository = Bootstrap::getObjectManager()
30-
->get(ProductRepositoryInterface::class);
36+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
3137

3238
/** @var $installer CategorySetup */
33-
$installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
39+
$installer = $objectManager->create(CategorySetup::class);
3440

3541
/** @var \Magento\Eav\Model\Config $eavConfig */
36-
$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
42+
$eavConfig = $objectManager->get(\Magento\Eav\Model\Config::class);
3743
$firstAttribute = $eavConfig->getAttribute(Product::ENTITY, 'test_configurable_first');
3844
$secondAttribute = $eavConfig->getAttribute(Product::ENTITY, 'test_configurable_second');
3945

46+
/** @var Config $config */
47+
$config = $objectManager->get(Config::class);
48+
49+
/** @var Filesystem $filesystem */
50+
$filesystem = $objectManager->get(Filesystem::class);
51+
52+
/** @var WriteInterface $mediaDirectory */
53+
$mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
54+
$mediaPath = $mediaDirectory->getAbsolutePath();
55+
$baseTmpMediaPath = $config->getBaseTmpMediaPath();
56+
$mediaDirectory->create($baseTmpMediaPath);
57+
4058
/* Create simple products per each option value*/
4159
/** @var AttributeOptionInterface[] $firstAttributeOptions */
4260
$firstAttributeOptions = $firstAttribute->getOptions();
@@ -48,6 +66,8 @@
4866
$firstAttributeValues = [];
4967
$secondAttributeValues = [];
5068
$testImagePath = __DIR__ . '/magento_image.jpg';
69+
$mediaImage = $mediaPath . '/' . $baseTmpMediaPath . '/magento_image.jpg';
70+
copy($testImagePath, $mediaImage);
5171

5272
array_shift($firstAttributeOptions);
5373
array_shift($secondAttributeOptions);
@@ -65,7 +85,10 @@
6585
$qty = 100;
6686
$isInStock = 1;
6787
}
68-
$product = Bootstrap::getObjectManager()->create(Product::class);
88+
89+
$image = '/m/a/magento_image.jpg';
90+
91+
$product = $objectManager->create(Product::class);
6992
$product->setTypeId(Type::TYPE_SIMPLE)
7093
->setAttributeSetId($attributeSetId)
7194
->setWebsiteIds([1])
@@ -79,15 +102,15 @@
79102
->setStockData(
80103
['use_config_manage_stock' => 1, 'qty' => $qty, 'is_qty_decimal' => 0, 'is_in_stock' => $isInStock]
81104
)
82-
->setImage('/m/a/magento_image.jpg')
83-
->setSmallImage('/m/a/magento_image.jpg')
84-
->setThumbnail('/m/a/magento_image.jpg')
105+
->setImage($image)
106+
->setSmallImage($image)
107+
->setThumbnail($image)
85108
->setData(
86109
'media_gallery',
87110
[
88111
'images' => [
89112
[
90-
'file' => '/m/a/magento_image.jpg',
113+
'file' => $image,
91114
'position' => 1,
92115
'label' => 'Image Alt Text',
93116
'disabled' => 0,
@@ -113,11 +136,12 @@
113136
foreach ($customAttributes as $attributeCode => $attributeValue) {
114137
$product->setCustomAttributes($customAttributes);
115138
}
139+
116140
$product = $productRepository->save($product);
117141
$associatedProductIds[] = $product->getId();
118142

119-
/** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
120-
$stockItem = Bootstrap::getObjectManager()->create(Item::class);
143+
/** @var Item $stockItem */
144+
$stockItem = $objectManager->create(Item::class);
121145
$stockItem->load($product->getId(), 'product_id');
122146

123147
if (!$stockItem->getProductId()) {
@@ -135,17 +159,16 @@
135159
'value_index' => $secondAttributeOption->getValue(),
136160
];
137161
}
138-
139162
}
140163

141-
$indexerProcessor = Bootstrap::getObjectManager()->get(PriceIndexerProcessor::class);
164+
$indexerProcessor = $objectManager->get(PriceIndexerProcessor::class);
142165
$indexerProcessor->reindexList($associatedProductIds, true);
143166

144167
/** @var $product Product */
145-
$product = Bootstrap::getObjectManager()->create(Product::class);
168+
$product = $objectManager->create(Product::class);
146169

147170
/** @var Factory $optionsFactory */
148-
$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
171+
$optionsFactory = $objectManager->create(Factory::class);
149172

150173
$configurableAttributesData = [
151174
[
@@ -180,9 +203,15 @@
180203
->setSku('configurable_12345')
181204
->setVisibility(Visibility::VISIBILITY_BOTH)
182205
->setStatus(Status::STATUS_ENABLED)
183-
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
206+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
207+
->addImageToMediaGallery(
208+
$mediaImage,
209+
['image', 'small_image', 'thumbnail'],
210+
false,
211+
false
212+
);
184213
$productRepository->cleanCache();
185214
$product = $productRepository->save($product);
186215

187-
$indexerProcessor = Bootstrap::getObjectManager()->get(PriceIndexerProcessor::class);
216+
$indexerProcessor = $objectManager->get(PriceIndexerProcessor::class);
188217
$indexerProcessor->reindexRow($product->getId(), true);

0 commit comments

Comments
 (0)