Skip to content

Commit c4bf6a4

Browse files
committed
Merge branch 'MC-39575' into 2.4-develop-sidecar-pr9
2 parents 3a74f27 + 7491d77 commit c4bf6a4

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Swatches\Block\Product\Renderer\Configurable\Listing;
9+
10+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Module\Manager;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Framework\Serialize\SerializerInterface;
16+
use Magento\Framework\View\LayoutInterface;
17+
use Magento\Swatches\Block\Product\Renderer\Listing\Configurable;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Tests for configurable products options block with swatch attribute.
23+
*
24+
* @magentoDbIsolation enabled
25+
* @magentoAppArea frontend
26+
*/
27+
class ConfigurableTest extends TestCase
28+
{
29+
/**
30+
* @var ObjectManagerInterface
31+
*/
32+
private $objectManager;
33+
34+
/**
35+
* @var SerializerInterface
36+
*/
37+
private $serializer;
38+
39+
/**
40+
* @var Configurable
41+
*/
42+
private $block;
43+
44+
/**
45+
* @var ProductRepositoryInterface
46+
*/
47+
private $productRepository;
48+
49+
/**
50+
* @var ProductAttributeRepositoryInterface
51+
*/
52+
private $productAttributeRepository;
53+
54+
/**
55+
* @var RequestInterface
56+
*/
57+
private $request;
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public static function setUpBeforeClass(): void
63+
{
64+
$objectManager = Bootstrap::getObjectManager();
65+
/** @var Manager $moduleManager */
66+
$moduleManager = $objectManager->get(Manager::class);
67+
if (!$moduleManager->isEnabled('Magento_Catalog')) {
68+
self::markTestSkipped('Magento_Catalog module disabled.');
69+
}
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
protected function setUp(): void
76+
{
77+
parent::setUp();
78+
79+
$this->objectManager = Bootstrap::getObjectManager();
80+
$this->serializer = $this->objectManager->get(SerializerInterface::class);
81+
$this->productAttributeRepository = $this->objectManager->get(ProductAttributeRepositoryInterface::class);
82+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
83+
$this->productRepository->cleanCache();
84+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Configurable::class);
85+
$this->request = $this->objectManager->get(RequestInterface::class);
86+
}
87+
88+
/**
89+
* @magentoDataFixture Magento/Swatches/_files/configurable_product_with_images.php
90+
* @return void
91+
*/
92+
public function testPreSelectedGalleryConfig(): void
93+
{
94+
$product = $this->productRepository->get('configurable');
95+
$this->block->setProduct($product);
96+
$configurableAttribute = $this->productAttributeRepository->get('visual_swatch_attribute');
97+
$this->request->setQueryValue('visual_swatch_attribute', $configurableAttribute->getOptions()[1]->getValue());
98+
$jsonConfig = $this->serializer->unserialize($this->block->getJsonConfig());
99+
$this->assertArrayHasKey('preSelectedGallery', $jsonConfig);
100+
$this->assertStringEndsWith('/m/a/magento_image.jpg', $jsonConfig['preSelectedGallery']['large']);
101+
$this->assertStringEndsWith('/m/a/magento_image.jpg', $jsonConfig['preSelectedGallery']['medium']);
102+
$this->assertStringEndsWith('/m/a/magento_image.jpg', $jsonConfig['preSelectedGallery']['small']);
103+
}
104+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
12+
13+
Resolver::getInstance()->requireDataFixture(
14+
'Magento/Catalog/_files/product_image.php'
15+
);
16+
Resolver::getInstance()->requireDataFixture(
17+
'Magento/Swatches/_files/configurable_product_visual_swatch_attribute.php'
18+
);
19+
20+
$objectManager = Bootstrap::getObjectManager();
21+
/** @var ProductRepositoryInterface $productRepository */
22+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
23+
$configurableProduct = $productRepository->get('configurable');
24+
$children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
25+
$images = ['magento_image.jpg', 'magento_small_image.jpg', 'magento_thumbnail.jpg'];
26+
foreach ($children as $index => $product) {
27+
$product->setImage('/m/a/' . $images[$index])
28+
->setSmallImage('/m/a/' . $images[$index])
29+
->setThumbnail('/m/a/' . $images[$index])
30+
->setData('media_gallery', ['images' => [
31+
[
32+
'file' => '/m/a/' . $images[$index],
33+
'position' => 1,
34+
'label' => 'Image Alt Text',
35+
'disabled' => 0,
36+
'media_type' => 'image',
37+
],
38+
]])
39+
->setCanSaveCustomOptions(true)
40+
->save();
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
9+
10+
Resolver::getInstance()->requireDataFixture(
11+
'Magento/Swatches/_files/configurable_product_visual_swatch_attribute_rollback.php'
12+
);
13+
Resolver::getInstance()->requireDataFixture(
14+
'Magento/Catalog/_files/product_image_rollback.php'
15+
);

0 commit comments

Comments
 (0)