|
| 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\ConfigurableProduct\Block\Product\View\Type; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Framework\Serialize\SerializerInterface; |
| 16 | +use Magento\Framework\View\LayoutInterface; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\Store\ExecuteInStoreContext; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class check configurable product options displaying per stores |
| 24 | + * |
| 25 | + * @magentoDbIsolation disabled |
| 26 | + */ |
| 27 | +class MultiStoreConfigurableViewOnProductPageTest extends TestCase |
| 28 | +{ |
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** @var ProductRepositoryInterface */ |
| 33 | + private $productRepository; |
| 34 | + |
| 35 | + /** @var StoreManagerInterface */ |
| 36 | + private $storeManager; |
| 37 | + |
| 38 | + /** @var LayoutInterface */ |
| 39 | + private $layout; |
| 40 | + |
| 41 | + /** @var SerializerInterface */ |
| 42 | + private $serializer; |
| 43 | + |
| 44 | + /** @var ProductResource */ |
| 45 | + private $productResource; |
| 46 | + |
| 47 | + /** @var ExecuteInStoreContext */ |
| 48 | + private $executeInStoreContext; |
| 49 | + |
| 50 | + /** |
| 51 | + * @inheritdoc |
| 52 | + */ |
| 53 | + protected function setUp() |
| 54 | + { |
| 55 | + parent::setUp(); |
| 56 | + |
| 57 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 58 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 59 | + $this->productRepository->cleanCache(); |
| 60 | + $this->storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 61 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 62 | + $this->serializer = $this->objectManager->get(SerializerInterface::class); |
| 63 | + $this->productResource = $this->objectManager->get(ProductResource::class); |
| 64 | + $this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_different_option_labeles_per_stores.php |
| 69 | + * |
| 70 | + * @dataProvider expectedLabelsDataProvider |
| 71 | + * |
| 72 | + * @param array $expectedStoreData |
| 73 | + * @param array $expectedSecondStoreData |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testMultiStoreLabelView(array $expectedStoreData, array $expectedSecondStoreData): void |
| 77 | + { |
| 78 | + $this->executeInStoreContext->execute('default', [$this, 'assertProductLabel'], $expectedStoreData); |
| 79 | + $this->executeInStoreContext->execute('fixturestore', [$this, 'assertProductLabel'], $expectedSecondStoreData); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function expectedLabelsDataProvider(): array |
| 86 | + { |
| 87 | + return [ |
| 88 | + [ |
| 89 | + 'options_first_store' => [ |
| 90 | + 'simple_option_1_default_store' => [ |
| 91 | + 'label' => 'Option 1 Default Store', |
| 92 | + ], |
| 93 | + 'simple_option_2_default_store' => [ |
| 94 | + 'label' => 'Option 2 Default Store', |
| 95 | + ], |
| 96 | + 'simple_option_3_default_store' => [ |
| 97 | + 'label' => 'Option 3 Default Store', |
| 98 | + ], |
| 99 | + ], |
| 100 | + 'options_second_store' => [ |
| 101 | + 'simple_option_1_default_store' => [ |
| 102 | + 'label' => 'Option 1 Second Store', |
| 103 | + ], |
| 104 | + 'simple_option_2_default_store' => [ |
| 105 | + 'label' => 'Option 2 Second Store', |
| 106 | + ], |
| 107 | + 'simple_option_3_default_store' => [ |
| 108 | + 'label' => 'Option 3 Second Store', |
| 109 | + ], |
| 110 | + ], |
| 111 | + ], |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Assert configurable product labels config |
| 117 | + * |
| 118 | + * @param $expectedStoreData |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + public function assertProductLabel($expectedStoreData): void |
| 122 | + { |
| 123 | + $product = $this->productRepository->get('configurable', false, null, true); |
| 124 | + $config = $this->getBlockConfig($product)['attributes'] ?? null; |
| 125 | + $this->assertNotNull($config); |
| 126 | + $this->assertAttributeConfig($expectedStoreData, reset($config)); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_two_websites.php |
| 131 | + * |
| 132 | + * @dataProvider expectedProductDataProvider |
| 133 | + * |
| 134 | + * @param array $expectedProducts |
| 135 | + * @param array $expectedSecondStoreProducts |
| 136 | + * @return void |
| 137 | + */ |
| 138 | + public function testMultiStoreOptionsView(array $expectedProducts, array $expectedSecondStoreProducts): void |
| 139 | + { |
| 140 | + $this->prepareConfigurableProduct('configurable', 'fixture_second_store'); |
| 141 | + $this->executeInStoreContext->execute('default', [$this, 'assertProductConfig'], $expectedProducts); |
| 142 | + $this->executeInStoreContext->execute( |
| 143 | + 'fixture_second_store', |
| 144 | + [$this, 'assertProductConfig'], |
| 145 | + $expectedSecondStoreProducts |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @return array |
| 151 | + */ |
| 152 | + public function expectedProductDataProvider(): array |
| 153 | + { |
| 154 | + return [ |
| 155 | + [ |
| 156 | + 'expected_store_products' => ['simple_option_1', 'simple_option_2'], |
| 157 | + 'expected_second_store_products' => ['simple_option_2'], |
| 158 | + ], |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Assert configurable product config |
| 164 | + * |
| 165 | + * @param $expectedProducts |
| 166 | + * @return void |
| 167 | + */ |
| 168 | + public function assertProductConfig($expectedProducts): void |
| 169 | + { |
| 170 | + $product = $this->productRepository->get('configurable', false, null, true); |
| 171 | + $config = $this->getBlockConfig($product)['index'] ?? null; |
| 172 | + $this->assertNotNull($config); |
| 173 | + $this->assertProducts($expectedProducts, $config); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Prepare configurable product to test |
| 178 | + * |
| 179 | + * @param string $sku |
| 180 | + * @param string $storeCode |
| 181 | + * @return void |
| 182 | + */ |
| 183 | + private function prepareConfigurableProduct(string $sku, string $storeCode): void |
| 184 | + { |
| 185 | + $product = $this->productRepository->get($sku, false, null, true); |
| 186 | + $productToUpdate = $product->getTypeInstance()->getUsedProductCollection($product) |
| 187 | + ->setPageSize(1)->getFirstItem(); |
| 188 | + $this->assertNotEmpty($productToUpdate->getData(), 'Configurable product does not have a child'); |
| 189 | + $this->executeInStoreContext->execute($storeCode, [$this, 'setProductDisabled'], $productToUpdate); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Assert product options display per stores |
| 194 | + * |
| 195 | + * @param array $expectedProducts |
| 196 | + * @param array $config |
| 197 | + * @return void |
| 198 | + */ |
| 199 | + private function assertProducts(array $expectedProducts, array $config): void |
| 200 | + { |
| 201 | + $this->assertCount(count($expectedProducts), $config); |
| 202 | + $idsBySkus = $this->productResource->getProductsIdsBySkus($expectedProducts); |
| 203 | + |
| 204 | + foreach ($idsBySkus as $productId) { |
| 205 | + $this->assertArrayHasKey($productId, $config); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Set product status attribute to disabled |
| 211 | + * |
| 212 | + * @param ProductInterface $product |
| 213 | + * @param string $storeCode |
| 214 | + * @return void |
| 215 | + */ |
| 216 | + public function setProductDisabled(ProductInterface $product): void |
| 217 | + { |
| 218 | + $product->setStatus(Status::STATUS_DISABLED); |
| 219 | + $this->productRepository->save($product); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Get block config |
| 224 | + * |
| 225 | + * @param ProductInterface $product |
| 226 | + * @return array |
| 227 | + */ |
| 228 | + private function getBlockConfig(ProductInterface $product): array |
| 229 | + { |
| 230 | + $block = $this->layout->createBlock(Configurable::class); |
| 231 | + $block->setProduct($product); |
| 232 | + |
| 233 | + return $this->serializer->unserialize($block->getJsonConfig()); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Assert configurable product config |
| 238 | + * |
| 239 | + * @param array $expectedData |
| 240 | + * @param array $actualOptions |
| 241 | + * @return void |
| 242 | + */ |
| 243 | + private function assertAttributeConfig(array $expectedData, array $actualOptions): void |
| 244 | + { |
| 245 | + $skus = array_keys($expectedData); |
| 246 | + $idBySkuMap = $this->productResource->getProductsIdsBySkus($skus); |
| 247 | + array_walk($actualOptions['options'], function (&$option) { |
| 248 | + unset($option['id']); |
| 249 | + }); |
| 250 | + foreach ($expectedData as $sku => &$option) { |
| 251 | + $option['products'] = [$idBySkuMap[$sku]]; |
| 252 | + } |
| 253 | + $this->assertEquals(array_values($expectedData), $actualOptions['options']); |
| 254 | + } |
| 255 | +} |
0 commit comments