|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\ConfigurableProduct; |
| 9 | + |
| 10 | +use Magento\Catalog\Test\Fixture\Category as CategoryFixture; |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\ConfigurableProduct\Test\Fixture\Attribute; |
| 13 | +use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture; |
| 14 | +use Magento\TestFramework\Fixture\DataFixture; |
| 15 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test configurable product queries work correctly |
| 19 | + */ |
| 20 | +class MultipleConfigurableProductWithSameSimpleProductTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Test if multiple configurable product can have same simple product |
| 24 | + * |
| 25 | + */ |
| 26 | + #[ |
| 27 | + DataFixture(CategoryFixture::class, ['name' => 'Test category'], 'test_category'), |
| 28 | + DataFixture(Attribute::class, ['options' => [['label' => 'color', 'sort_order' => 0]]], as:'attribute'), |
| 29 | + DataFixture( |
| 30 | + ProductFixture::class, |
| 31 | + [ |
| 32 | + 'name' => 'Simple Product in Test Category', |
| 33 | + 'sku' => 'simple-product-1', |
| 34 | + 'category_ids' => ['$test_category.id$'], |
| 35 | + 'price' => 10, |
| 36 | + ], |
| 37 | + 'simple_product_1' |
| 38 | + ), |
| 39 | + DataFixture( |
| 40 | + ConfigurableProductFixture::class, |
| 41 | + [ |
| 42 | + 'name' => 'Configurable Product 1', |
| 43 | + 'sku' => 'configurable_product_1', |
| 44 | + 'category_ids' => ['$test_category.id$'], |
| 45 | + '_options' => ['$attribute$'], |
| 46 | + '_links' => [ |
| 47 | + '$simple_product_1$' |
| 48 | + ] |
| 49 | + ], |
| 50 | + 'configurable-product-1' |
| 51 | + ), |
| 52 | + DataFixture( |
| 53 | + ConfigurableProductFixture::class, |
| 54 | + [ |
| 55 | + 'name' => 'Configurable Product 2', |
| 56 | + 'sku' => 'configurable_product_2', |
| 57 | + 'category_ids' => ['$test_category.id$'], |
| 58 | + '_options' => ['$attribute$'], |
| 59 | + '_links' => [ |
| 60 | + '$simple_product_1$' |
| 61 | + ] |
| 62 | + ], |
| 63 | + 'configurable-product-2' |
| 64 | + ), |
| 65 | + ] |
| 66 | + public function testMultipleConfigurableProductCanHaveSameSimpleProduct() |
| 67 | + { |
| 68 | + $query = $this->getGraphQlQuery('"configurable_product_1", "configurable_product_2"'); |
| 69 | + $result = $this->graphQlQuery($query); |
| 70 | + |
| 71 | + self::assertArrayNotHasKey('errors', $result); |
| 72 | + self::assertNotEmpty($result['products']); |
| 73 | + self::assertCount(2, $result['products']['items']); |
| 74 | + self::assertNotEmpty($result['products']['items'][0]['variants'][0]); |
| 75 | + self::assertCount(2, $result['products']['items'][0]['variants'][0]); |
| 76 | + self::assertNotEmpty($result['products']['items'][1]['variants'][0]); |
| 77 | + self::assertCount(2, $result['products']['items'][1]['variants'][0]); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get GraphQl products query with aggregations |
| 82 | + * |
| 83 | + * @param string $skus |
| 84 | + * @return string |
| 85 | + */ |
| 86 | + private function getGraphQlQuery(string $skus) |
| 87 | + { |
| 88 | + return <<<QUERY |
| 89 | +{ |
| 90 | + products(filter: {sku: {in: [{$skus}]}}) { |
| 91 | + items { |
| 92 | + id |
| 93 | + attribute_set_id |
| 94 | + name |
| 95 | + sku |
| 96 | + __typename |
| 97 | + price { |
| 98 | + regularPrice { |
| 99 | + amount { |
| 100 | + currency |
| 101 | + value |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + categories { |
| 106 | + id |
| 107 | + } |
| 108 | + ... on ConfigurableProduct { |
| 109 | + configurable_options { |
| 110 | + id |
| 111 | + attribute_id_v2 |
| 112 | + label |
| 113 | + position |
| 114 | + use_default |
| 115 | + attribute_code |
| 116 | + values { |
| 117 | + value_index |
| 118 | + label |
| 119 | + } |
| 120 | + product_id |
| 121 | + } |
| 122 | + variants { |
| 123 | + product { |
| 124 | + id |
| 125 | + name |
| 126 | + sku |
| 127 | + attribute_set_id |
| 128 | + ... on PhysicalProductInterface { |
| 129 | + weight |
| 130 | + } |
| 131 | + price_range{ |
| 132 | + minimum_price{ |
| 133 | + regular_price{ |
| 134 | + value |
| 135 | + currency |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + attributes { |
| 141 | + uid |
| 142 | + label |
| 143 | + code |
| 144 | + value_index |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | +QUERY; |
| 152 | + } |
| 153 | +} |
0 commit comments