|
| 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\ProductVariantDataExporter\Test\Integration; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\ConfigurableProductDataExporter\Model\Provider\Product\ProductVariants\ConfigurableId; |
| 13 | +use RuntimeException; |
| 14 | +use Throwable; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test class for configurable product variants export |
| 18 | + */ |
| 19 | +class ConfigurableProductVariantsTest extends AbstractProductVariantsTest |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Test configurable product variants. |
| 23 | + * |
| 24 | + * @magentoAppIsolation enabled |
| 25 | + * @magentoDbIsolation disabled |
| 26 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_two_attributes.php |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function testConfigurableVariants(): void |
| 30 | + { |
| 31 | + try { |
| 32 | + $expected = $this->getExpectedProductVariants(['simple_10','simple_20']); |
| 33 | + |
| 34 | + $variantSimple10 = $this->idResolver->resolve([ |
| 35 | + ConfigurableId::PARENT_SKU_KEY => 'configurable', |
| 36 | + ConfigurableId::CHILD_SKU_KEY => 'simple_10' |
| 37 | + ]); |
| 38 | + $variantSimple20 = $this->idResolver->resolve([ |
| 39 | + ConfigurableId::PARENT_SKU_KEY => 'configurable', |
| 40 | + ConfigurableId::CHILD_SKU_KEY => 'simple_20' |
| 41 | + ]); |
| 42 | + $actual = $this->productVariantsFeed->getFeedByIds( |
| 43 | + [$variantSimple10, $variantSimple20] |
| 44 | + )['feed']; |
| 45 | + |
| 46 | + $diff = $this->arrayUtils->recursiveDiff($expected, $actual); |
| 47 | + self::assertEquals([], $diff, "Product variants response doesn't equal expected response"); |
| 48 | + |
| 49 | + } catch (Throwable $e) { |
| 50 | + $this->fail($e->getMessage()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test that variants are deleted as expected. |
| 56 | + * |
| 57 | + * @magentoDbIsolation disabled |
| 58 | + * @magentoAppIsolation enabled |
| 59 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function testDeleteConfigurableProductVariants(): void |
| 64 | + { |
| 65 | + try { |
| 66 | + $configurable = $this->productRepository->get('configurable'); |
| 67 | + $configurableId = $configurable->getId(); |
| 68 | + $variantSimple10 = $this->idResolver->resolve([ |
| 69 | + ConfigurableId::PARENT_SKU_KEY => 'configurable', |
| 70 | + ConfigurableId::CHILD_SKU_KEY => 'simple_10' |
| 71 | + ]); |
| 72 | + $variantSimple20 = $this->idResolver->resolve([ |
| 73 | + ConfigurableId::PARENT_SKU_KEY => 'configurable', |
| 74 | + ConfigurableId::CHILD_SKU_KEY => 'simple_20' |
| 75 | + ]); |
| 76 | + $realVariantsData = $this->productVariantsFeed->getFeedByIds( |
| 77 | + [$variantSimple10, $variantSimple20] |
| 78 | + )['feed']; |
| 79 | + $this->assertCount(2, $realVariantsData); |
| 80 | + |
| 81 | + $simple = $this->productRepository->get('simple_10'); //id10 and id20 |
| 82 | + $this->deleteProduct($simple->getSku()); |
| 83 | + $this->runIndexer([$configurableId]); |
| 84 | + |
| 85 | + $emptyVariantsData = $this->productVariantsFeed->getFeedByIds( |
| 86 | + [$variantSimple10, $variantSimple20] |
| 87 | + )['feed']; |
| 88 | + $this->assertCount(1, $emptyVariantsData); //id20 |
| 89 | + $deletedVariantsData = $this->productVariantsFeed->getDeletedByIds( |
| 90 | + [$variantSimple10, $variantSimple20] |
| 91 | + ); |
| 92 | + $this->assertCount(1, $deletedVariantsData); //id10 |
| 93 | + } catch (Throwable $e) { |
| 94 | + $this->fail($e->getMessage()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Delete product variant |
| 100 | + * |
| 101 | + * @param string $productSku |
| 102 | + * @throws RuntimeException |
| 103 | + */ |
| 104 | + private function deleteProduct(string $productSku): void |
| 105 | + { |
| 106 | + $this->registry->unregister('isSecureArea'); |
| 107 | + $this->registry->register('isSecureArea', true); |
| 108 | + |
| 109 | + try { |
| 110 | + $this->productRepository->deleteById($productSku); |
| 111 | + } catch (Throwable $e) { |
| 112 | + throw new RuntimeException('Could not delete product ' . $productSku); |
| 113 | + } |
| 114 | + |
| 115 | + $this->registry->unregister('isSecureArea'); |
| 116 | + $this->registry->register('isSecureArea', false); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get the expected variants for the first combination of options being tested. |
| 121 | + * |
| 122 | + * @param array $simples |
| 123 | + * @return array |
| 124 | + */ |
| 125 | + private function getExpectedProductVariants(array $simples): array |
| 126 | + { |
| 127 | + $variants = [ |
| 128 | + 'simple_10' => |
| 129 | + [ |
| 130 | + 'id' => '8a880c29baa2ec8a5068350ec04f5b7d', |
| 131 | + 'optionValues' => |
| 132 | + [ |
| 133 | + [ |
| 134 | + 'attributeCode' => 'test_configurable_first', |
| 135 | + 'label' => 'First Option 1', |
| 136 | +// 'valueIndex' => '107', //Skipped because they are unique |
| 137 | +// 'uid' => 'Y29uZmlndXJhYmxlLzIwOS8xMDc=', //Skipped because they are unique |
| 138 | + ], |
| 139 | + [ |
| 140 | + 'attributeCode' => 'test_configurable_second', |
| 141 | + 'label' => 'Second Option 1', |
| 142 | + ], |
| 143 | + ], |
| 144 | + 'parentId' => '1', |
| 145 | + 'productId' => '10', |
| 146 | + 'parentSku' => 'configurable', |
| 147 | + 'productSku' => 'simple_10', |
| 148 | + 'deleted' => false, |
| 149 | + ], |
| 150 | + 'simple_20' => |
| 151 | + [ |
| 152 | + 'id' => 'b91c35230afd24649f2ff60c79e7e7ba', |
| 153 | + 'optionValues' => |
| 154 | + [ |
| 155 | + [ |
| 156 | + 'attributeCode' => 'test_configurable_first', |
| 157 | + 'label' => 'First Option 2', |
| 158 | + ], |
| 159 | + [ |
| 160 | + 'attributeCode' => 'test_configurable_second', |
| 161 | + 'label' => 'Second Option 2', |
| 162 | + ], |
| 163 | + ], |
| 164 | + 'parentId' => '1', |
| 165 | + 'productId' => '20', |
| 166 | + 'parentSku' => 'configurable', |
| 167 | + 'productSku' => 'simple_20', |
| 168 | + 'deleted' => false, |
| 169 | + ], |
| 170 | + ]; |
| 171 | + return array_values(array_intersect_key($variants, array_flip($simples))); |
| 172 | + } |
| 173 | +} |
0 commit comments