|
| 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 | + $this->request->clearParams(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @inheritdoc |
| 91 | + */ |
| 92 | + protected function tearDown(): void |
| 93 | + { |
| 94 | + $this->request->clearParams(); |
| 95 | + |
| 96 | + parent::tearDown(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @magentoDataFixture Magento/Swatches/_files/configurable_product_with_images.php |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function testPreSelectedGalleryConfig(): void |
| 104 | + { |
| 105 | + $product = $this->productRepository->get('configurable'); |
| 106 | + $this->block->setProduct($product); |
| 107 | + $configurableAttribute = $this->productAttributeRepository->get('visual_swatch_attribute'); |
| 108 | + $this->request->setQueryValue('visual_swatch_attribute', $configurableAttribute->getOptions()[1]->getValue()); |
| 109 | + $jsonConfig = $this->serializer->unserialize($this->block->getJsonConfig()); |
| 110 | + $this->assertArrayHasKey('preSelectedGallery', $jsonConfig); |
| 111 | + $this->assertStringEndsWith('/m/a/magento_image.jpg', $jsonConfig['preSelectedGallery']['large']); |
| 112 | + $this->assertStringEndsWith('/m/a/magento_image.jpg', $jsonConfig['preSelectedGallery']['medium']); |
| 113 | + $this->assertStringEndsWith('/m/a/magento_image.jpg', $jsonConfig['preSelectedGallery']['small']); |
| 114 | + } |
| 115 | +} |
0 commit comments