Skip to content

Commit 062924b

Browse files
authored
ENGCOM-5912: graphQl-812: test Add Variation From Another Configurable Product To Cart #866
2 parents e4bc785 + 96a1b87 commit 062924b

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-0
lines changed

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

+62
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\GraphQl\ConfigurableProduct;
99

10+
use Exception;
1011
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1112
use Magento\TestFramework\Helper\Bootstrap;
1213
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -139,6 +140,67 @@ public function testAddMultipleConfigurableProductToCart()
139140
}
140141
}
141142

143+
/**
144+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
145+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
146+
*
147+
* @expectedException Exception
148+
* @expectedExceptionMessage You need to choose options for your item.
149+
*/
150+
public function testAddVariationFromAnotherConfigurableProductWithTheSameSuperAttributeToCart()
151+
{
152+
$this->markTestSkipped(
153+
'Magento automatically selects the correct child product according to the super attribute
154+
https://github.com/magento/graphql-ce/issues/940'
155+
);
156+
157+
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable_12345'));
158+
$product = current($searchResponse['products']['items']);
159+
160+
$quantity = 2;
161+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
162+
$parentSku = $product['sku'];
163+
164+
$sku = 'simple_20';
165+
166+
$query = $this->getQuery(
167+
$maskedQuoteId,
168+
$parentSku,
169+
$sku,
170+
$quantity
171+
);
172+
173+
$this->graphQlMutation($query);
174+
}
175+
176+
/**
177+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_different_super_attribute.php
178+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
179+
*
180+
* @expectedException Exception
181+
* @expectedExceptionMessage You need to choose options for your item.
182+
*/
183+
public function testAddVariationFromAnotherConfigurableProductWithDifferentSuperAttributeToCart()
184+
{
185+
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable_12345'));
186+
$product = current($searchResponse['products']['items']);
187+
188+
$quantity = 2;
189+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
190+
$parentSku = $product['sku'];
191+
192+
$sku = 'simple_20';
193+
194+
$query = $this->getQuery(
195+
$maskedQuoteId,
196+
$parentSku,
197+
$sku,
198+
$quantity
199+
);
200+
201+
$this->graphQlMutation($query);
202+
}
203+
142204
/**
143205
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
144206
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\TestFramework\Helper\Bootstrap;
8+
use Magento\Eav\Api\AttributeRepositoryInterface;
9+
10+
$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
11+
$attribute2 = $eavConfig->getAttribute('catalog_product', 'test_configurable_2');
12+
13+
$eavConfig->clear();
14+
15+
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
16+
$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
17+
18+
if (!$attribute2->getId()) {
19+
20+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
21+
$attribute2 = Bootstrap::getObjectManager()->create(
22+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
23+
);
24+
25+
/** @var AttributeRepositoryInterface $attributeRepository */
26+
$attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
27+
28+
$attribute2->setData(
29+
[
30+
'attribute_code' => 'test_configurable_2',
31+
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
32+
'is_global' => 1,
33+
'is_user_defined' => 1,
34+
'frontend_input' => 'select',
35+
'is_unique' => 0,
36+
'is_required' => 0,
37+
'is_searchable' => 0,
38+
'is_visible_in_advanced_search' => 0,
39+
'is_comparable' => 0,
40+
'is_filterable' => 0,
41+
'is_filterable_in_search' => 0,
42+
'is_used_for_promo_rules' => 0,
43+
'is_html_allowed_on_front' => 1,
44+
'is_visible_on_front' => 0,
45+
'used_in_product_listing' => 0,
46+
'used_for_sort_by' => 0,
47+
'frontend_label' => ['Test Configurable 2'],
48+
'backend_type' => 'int',
49+
'option' => [
50+
'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']],
51+
'order' => ['option_0' => 1, 'option_1' => 2],
52+
],
53+
]
54+
);
55+
56+
$attributeRepository->save($attribute2);
57+
58+
/* Assign attribute to attribute set */
59+
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute2->getId());
60+
}
61+
62+
$eavConfig->clear();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\Registry $registry */
8+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
9+
10+
$registry->unregister('isSecureArea');
11+
$registry->register('isSecureArea', true);
12+
$productCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
13+
->get(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
14+
foreach ($productCollection as $product) {
15+
$product->delete();
16+
}
17+
18+
$eavConfig = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
19+
$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable_2');
20+
if ($attribute instanceof \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
21+
&& $attribute->getId()
22+
) {
23+
$attribute->delete();
24+
}
25+
$eavConfig->clear();
26+
27+
$registry->unregister('isSecureArea');
28+
$registry->register('isSecureArea', false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\Catalog\Api\ProductRepositoryInterface;
7+
use Magento\Catalog\Model\Product;
8+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
9+
use Magento\Catalog\Model\Product\Type;
10+
use Magento\Catalog\Model\Product\Visibility;
11+
use Magento\Catalog\Setup\CategorySetup;
12+
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
13+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
14+
use Magento\Eav\Api\Data\AttributeOptionInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
require __DIR__ . '/configurable_attribute.php';
18+
require __DIR__ . '/configurable_attribute_2.php';
19+
20+
/** @var ProductRepositoryInterface $productRepository */
21+
$productRepository = Bootstrap::getObjectManager()
22+
->get(ProductRepositoryInterface::class);
23+
24+
/** @var $installer CategorySetup */
25+
$installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
26+
27+
/* Create simple products per each option value*/
28+
/** @var AttributeOptionInterface[] $options */
29+
$options = $attribute->getOptions();
30+
31+
$attributeValues = [];
32+
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
33+
$associatedProductIds = [];
34+
$productIds = [10, 20];
35+
array_shift($options); //remove the first option which is empty
36+
37+
foreach ($options as $option) {
38+
/** @var $product Product */
39+
$product = Bootstrap::getObjectManager()->create(Product::class);
40+
$productId = array_shift($productIds);
41+
$product->setTypeId(Type::TYPE_SIMPLE)
42+
->setId($productId)
43+
->setAttributeSetId($attributeSetId)
44+
->setWebsiteIds([1])
45+
->setName('Configurable Option' . $option->getLabel())
46+
->setSku('simple_' . $productId)
47+
->setPrice($productId)
48+
->setTestConfigurable($option->getValue())
49+
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
50+
->setStatus(Status::STATUS_ENABLED)
51+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
52+
$product = $productRepository->save($product);
53+
54+
$attributeValues[] = [
55+
'label' => 'test',
56+
'attribute_id' => $attribute->getId(),
57+
'value_index' => $option->getValue(),
58+
];
59+
$associatedProductIds[] = $product->getId();
60+
}
61+
62+
/** @var $product Product */
63+
$product = Bootstrap::getObjectManager()->create(Product::class);
64+
/** @var Factory $optionsFactory */
65+
$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
66+
$configurableAttributesData = [
67+
[
68+
'attribute_id' => $attribute->getId(),
69+
'code' => $attribute->getAttributeCode(),
70+
'label' => $attribute->getStoreLabel(),
71+
'position' => '0',
72+
'values' => $attributeValues,
73+
],
74+
];
75+
$configurableOptions = $optionsFactory->create($configurableAttributesData);
76+
$extensionConfigurableAttributes = $product->getExtensionAttributes();
77+
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
78+
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
79+
$product->setExtensionAttributes($extensionConfigurableAttributes);
80+
81+
$product->setTypeId(Configurable::TYPE_CODE)
82+
->setId(1)
83+
->setAttributeSetId($attributeSetId)
84+
->setWebsiteIds([1])
85+
->setName('Configurable Product')
86+
->setSku('configurable')
87+
->setVisibility(Visibility::VISIBILITY_BOTH)
88+
->setStatus(Status::STATUS_ENABLED)
89+
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
90+
$productRepository->cleanCache();
91+
$productRepository->save($product);
92+
93+
/* Create simple products per each option value*/
94+
/** @var AttributeOptionInterface[] $options */
95+
$options = $attribute2->getOptions();
96+
97+
$attributeValues = [];
98+
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
99+
$associatedProductIds = [];
100+
$productIds = [30, 40];
101+
array_shift($options); //remove the first option which is empty
102+
103+
foreach ($options as $option) {
104+
/** @var $product Product */
105+
$product = Bootstrap::getObjectManager()->create(Product::class);
106+
$productId = array_shift($productIds);
107+
$product->setTypeId(Type::TYPE_SIMPLE)
108+
->setId($productId)
109+
->setAttributeSetId($attributeSetId)
110+
->setWebsiteIds([1])
111+
->setName('Configurable Option' . $option->getLabel())
112+
->setSku('simple_' . $productId)
113+
->setPrice($productId)
114+
->setTestConfigurable2($option->getValue())
115+
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
116+
->setStatus(Status::STATUS_ENABLED)
117+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
118+
$product = $productRepository->save($product);
119+
120+
$attributeValues[] = [
121+
'label' => 'test',
122+
'attribute_id' => $attribute2->getId(),
123+
'value_index' => $option->getValue(),
124+
];
125+
$associatedProductIds[] = $product->getId();
126+
}
127+
128+
/** @var $product Product */
129+
$product = Bootstrap::getObjectManager()->create(Product::class);
130+
131+
/** @var Factory $optionsFactory */
132+
$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
133+
134+
$configurableAttributesData = [
135+
[
136+
'attribute_id' => $attribute2->getId(),
137+
'code' => $attribute2->getAttributeCode(),
138+
'label' => $attribute2->getStoreLabel(),
139+
'position' => '1',
140+
'values' => $attributeValues,
141+
],
142+
];
143+
144+
$configurableOptions = $optionsFactory->create($configurableAttributesData);
145+
146+
$extensionConfigurableAttributes = $product->getExtensionAttributes();
147+
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
148+
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
149+
150+
$product->setExtensionAttributes($extensionConfigurableAttributes);
151+
152+
$product->setTypeId(Configurable::TYPE_CODE)
153+
->setId(11)
154+
->setAttributeSetId($attributeSetId)
155+
->setWebsiteIds([1])
156+
->setName('Configurable Product 12345')
157+
->setSku('configurable_12345')
158+
->setVisibility(Visibility::VISIBILITY_BOTH)
159+
->setStatus(Status::STATUS_ENABLED)
160+
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
161+
$productRepository->cleanCache();
162+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
require __DIR__ . '/configurable_products_rollback.php';
7+
8+
$registry->unregister('isSecureArea');
9+
$registry->register('isSecureArea', true);
10+
11+
require __DIR__ . '/configurable_attribute_2_rollback.php';
12+
13+
$registry->unregister('isSecureArea');
14+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)