Skip to content

Commit 0092a40

Browse files
committed
MC-42666: GraphQL request returns configurable variants from ALL storeviews
1 parent 56cf69c commit 0092a40

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/ConfigurableProductMultipleStoreViewTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,42 @@ public function testConfigurableProductAssignedToOneWebsite()
3636
self::assertContains('Option 2', $secondWebsiteVariants[1]['attributes'][0]);
3737
}
3838

39+
/**
40+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_children_on_different_websites.php
41+
* @dataProvider childrenAssignedToDifferentWebsitesDataProvider
42+
* @param string $store
43+
* @param string $childSku
44+
* @param string $attributeLabel
45+
*/
46+
public function testConfigurableProductWithChildrenAssignedToDifferentWebsites(
47+
string $store,
48+
string $childSku,
49+
string $attributeLabel
50+
) {
51+
$headers = ['Store' => $store];
52+
$query = $this->getQuery('configurable');
53+
$response = $this->graphQlQuery($query, [], '', $headers);
54+
self::assertCount(1, $response['products']['items']);
55+
$product = $response['products']['items'][0];
56+
self::assertCount(1, $product['variants']);
57+
$variant = $response['products']['items'][0]['variants'][0];
58+
self::assertEquals($childSku, $variant['product']['sku']);
59+
self::assertCount(1, $variant['attributes']);
60+
$attribute = $variant['attributes'][0];
61+
self::assertEquals($attributeLabel, $attribute['label']);
62+
}
63+
64+
/**
65+
* @return array
66+
*/
67+
public function childrenAssignedToDifferentWebsitesDataProvider(): array
68+
{
69+
return [
70+
['default', 'simple_option_2', 'Option 2'],
71+
['fixture_second_store', 'simple_option_1', 'Option 1'],
72+
];
73+
}
74+
3975
/**
4076
* @param string $sku
4177
* @return string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\ProductExtensionFactory;
9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Catalog\Model\Product\Type as ProductType;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
use Magento\Catalog\Model\ProductFactory;
15+
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
16+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
17+
use Magento\Store\Api\WebsiteRepositoryInterface;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
20+
21+
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_website_with_two_stores.php');
22+
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/configurable_attribute.php');
23+
24+
$objectManager = Bootstrap::getObjectManager();
25+
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
26+
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
27+
$attribute = $productAttributeRepository->get('test_configurable');
28+
$options = $attribute->getOptions();
29+
/** @var WebsiteRepositoryInterface $websiteRepository */
30+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
31+
$baseWebsite = $websiteRepository->get('base');
32+
$secondWebsite = $websiteRepository->get('test');
33+
/** @var ProductRepositoryInterface $productRepository */
34+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
35+
$productRepository->cleanCache();
36+
/** @var ProductFactory $productFactory */
37+
$productFactory = $objectManager->get(ProductFactory::class);
38+
$attributeValues = [];
39+
$associatedProductIds = [];
40+
$rootCategoryId = $baseWebsite->getDefaultStore()->getRootCategoryId();
41+
array_shift($options);
42+
43+
foreach ($options as $key => $option) {
44+
$product = $productFactory->create();
45+
$product->setTypeId(ProductType::TYPE_SIMPLE)
46+
->setAttributeSetId($product->getDefaultAttributeSetId())
47+
->setWebsiteIds([$key % 2 ? $baseWebsite->getId() : $secondWebsite->getId()])
48+
->setName('Configurable Option ' . $option->getLabel())
49+
->setSku(strtolower(str_replace(' ', '_', 'simple ' . $option->getLabel())))
50+
->setPrice(150)
51+
->setTestConfigurable($option->getValue())
52+
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
53+
->setStatus(Status::STATUS_ENABLED)
54+
->setCategoryIds([$rootCategoryId])
55+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
56+
$product = $productRepository->save($product);
57+
58+
$attributeValues[] = [
59+
'label' => 'test',
60+
'attribute_id' => $attribute->getId(),
61+
'value_index' => $option->getValue(),
62+
];
63+
$associatedProductIds[] = $product->getId();
64+
}
65+
/** @var Factory $optionsFactory */
66+
$optionsFactory = $objectManager->get(Factory::class);
67+
$configurableAttributesData = [
68+
[
69+
'attribute_id' => $attribute->getId(),
70+
'code' => $attribute->getAttributeCode(),
71+
'label' => $attribute->getStoreLabel(),
72+
'position' => '0',
73+
'values' => $attributeValues,
74+
],
75+
];
76+
$configurableOptions = $optionsFactory->create($configurableAttributesData);
77+
78+
$product = $productFactory->create();
79+
/** @var ProductExtensionFactory $extensionAttributesFactory */
80+
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
81+
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
82+
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
83+
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
84+
$product->setExtensionAttributes($extensionConfigurableAttributes);
85+
86+
$product->setTypeId(Configurable::TYPE_CODE)
87+
->setAttributeSetId($product->getDefaultAttributeSetId())
88+
->setWebsiteIds([$baseWebsite->getId(), $secondWebsite->getId()])
89+
->setName('Configurable Product')
90+
->setSku('configurable')
91+
->setVisibility(Visibility::VISIBILITY_BOTH)
92+
->setStatus(Status::STATUS_ENABLED)
93+
->setCategoryIds([$rootCategoryId])
94+
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
95+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\ConfigurableProduct\Model\DeleteConfigurableProduct;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var DeleteConfigurableProduct $deleteConfigurableProduct */
14+
$deleteConfigurableProduct = $objectManager->get(DeleteConfigurableProduct::class);
15+
$deleteConfigurableProduct->execute('configurable');
16+
17+
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_website_with_two_stores_rollback.php');
18+
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/configurable_attribute_rollback.php');

0 commit comments

Comments
 (0)