Skip to content

Commit e265c83

Browse files
author
Joan He
committed
Merge branch '2.2-develop' into update_symfony
2 parents eb2df6e + e008e80 commit e265c83

File tree

7 files changed

+84
-74
lines changed

7 files changed

+84
-74
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Cart/BuyRequest/SuperAttributeDataProvider.php

+54-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
namespace Magento\ConfigurableProductGraphQl\Model\Cart\BuyRequest;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1013
use Magento\Framework\Stdlib\ArrayManager;
1114
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestDataProviderInterface;
15+
use Magento\Catalog\Api\ProductRepositoryInterface;
16+
use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection;
17+
use Magento\Framework\EntityManager\MetadataPool;
1218

1319
/**
1420
* DataProvider for building super attribute options in buy requests
@@ -20,27 +26,70 @@ class SuperAttributeDataProvider implements BuyRequestDataProviderInterface
2026
*/
2127
private $arrayManager;
2228

29+
/**
30+
* @var ProductRepositoryInterface
31+
*/
32+
private $productRepository;
33+
34+
/**
35+
* @var OptionCollection
36+
*/
37+
private $optionCollection;
38+
39+
/**
40+
* @var MetadataPool
41+
*/
42+
private $metadataPool;
43+
2344
/**
2445
* @param ArrayManager $arrayManager
46+
* @param ProductRepositoryInterface $productRepository
47+
* @param OptionCollection $optionCollection
48+
* @param MetadataPool $metadataPool
2549
*/
2650
public function __construct(
27-
ArrayManager $arrayManager
51+
ArrayManager $arrayManager,
52+
ProductRepositoryInterface $productRepository,
53+
OptionCollection $optionCollection,
54+
MetadataPool $metadataPool
2855
) {
2956
$this->arrayManager = $arrayManager;
57+
$this->productRepository = $productRepository;
58+
$this->optionCollection = $optionCollection;
59+
$this->metadataPool = $metadataPool;
3060
}
3161

3262
/**
3363
* @inheritdoc
3464
*/
3565
public function execute(array $cartItemData): array
3666
{
37-
$superAttributes = $this->arrayManager->get('configurable_attributes', $cartItemData, []);
67+
$parentSku = $this->arrayManager->get('parent_sku', $cartItemData);
68+
if ($parentSku === null) {
69+
return [];
70+
}
71+
$sku = $this->arrayManager->get('data/sku', $cartItemData);
3872

39-
$superAttributesData = [];
40-
foreach ($superAttributes as $superAttribute) {
41-
$superAttributesData[$superAttribute['id']] = $superAttribute['value'];
73+
try {
74+
$parentProduct = $this->productRepository->get($parentSku);
75+
$product = $this->productRepository->get($sku);
76+
} catch (NoSuchEntityException $e) {
77+
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
4278
}
79+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
80+
$this->optionCollection->addProductId((int)$parentProduct->getData($linkField));
81+
$options = $this->optionCollection->getAttributesByProductId((int)$parentProduct->getData($linkField));
4382

83+
$superAttributesData = [];
84+
foreach ($options as $option) {
85+
$code = $option['attribute_code'];
86+
foreach ($option['values'] as $optionValue) {
87+
if ($optionValue['value_index'] === $product->getData($code)) {
88+
$superAttributesData[$option['attribute_id']] = $optionValue['value_index'];
89+
break;
90+
}
91+
}
92+
}
4493
return ['super_attribute' => $superAttributesData];
4594
}
4695
}

app/code/Magento/ConfigurableProductGraphQl/etc/schema.graphqls

+2-7
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,11 @@ type AddConfigurableProductsToCartOutput {
5050

5151
input ConfigurableProductCartItemInput {
5252
data: CartItemInput!
53-
variant_sku: String @deprecated(reason: "Use CartItemInput.sku instead")
54-
configurable_attributes: [ConfigurableCartItemAttributesInput]!
53+
variant_sku: String @deprecated(reason: "Use CartItemInput.sku instead.")
54+
parent_sku: String @doc(description: "Configurable product SKU.")
5555
customizable_options:[CustomizableOptionInput!]
5656
}
5757

58-
input ConfigurableCartItemAttributesInput {
59-
id: Int!
60-
value: Int!
61-
}
62-
6358
type ConfigurableCartItem implements CartItemInterface {
6459
customizable_options: [SelectedCustomizableOption]!
6560
configurable_options: [SelectedConfigurableOption!]! @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableCartItemOptions")

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14-
use Magento\Framework\Stdlib\ArrayManager;
1514
use Magento\Quote\Model\Quote;
1615
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder;
1716

@@ -87,7 +86,11 @@ public function execute(Quote $cart, array $cartItemData): void
8786
*/
8887
private function extractSku(array $cartItemData): string
8988
{
90-
if (!isset($cartItemData['data']['sku']) || empty($cartItemData['data']['sku'])) {
89+
// Need to keep this for configurable product and backward compatibility.
90+
if (!empty($cartItemData['parent_sku'])) {
91+
return (string)$cartItemData['parent_sku'];
92+
}
93+
if (empty($cartItemData['data']['sku'])) {
9194
throw new GraphQlInputException(__('Missed "sku" in cart item data'));
9295
}
9396
return (string)$cartItemData['data']['sku'];

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"friendsofphp/php-cs-fixer": "~2.14.0",
8888
"lusitanian/oauth": "~0.8.10",
8989
"magento/magento-coding-standard": "~3.0.0",
90-
"magento/magento2-functional-testing-framework": "2.4.1",
90+
"magento/magento2-functional-testing-framework": "2.4.3",
9191
"pdepend/pdepend": "2.5.2",
9292
"phpmd/phpmd": "@stable",
9393
"phpunit/phpunit": "~6.5.0",

composer.lock

+10-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+10-43
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ public function testAddConfigurableProductToCart()
4141

4242
$quantity = 2;
4343
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
44-
$sku = $product['sku'];
44+
$parentSku = $product['sku'];
45+
$sku = 'simple_20';
4546
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];
4647
$optionId = $product['configurable_options'][0]['values'][1]['value_index'];
4748

4849
$query = $this->getQuery(
4950
$maskedQuoteId,
51+
$parentSku,
5052
$sku,
51-
$attributeId,
52-
$optionId,
5353
$quantity
5454
);
5555

5656
$response = $this->graphQlMutation($query);
5757

5858
$cartItem = current($response['addConfigurableProductsToCart']['cart']['items']);
5959
self::assertEquals($quantity, $cartItem['quantity']);
60-
self::assertEquals($sku, $cartItem['product']['sku']);
60+
self::assertEquals($parentSku, $cartItem['product']['sku']);
6161
self::assertArrayHasKey('configurable_options', $cartItem);
6262

6363
$option = current($cartItem['configurable_options']);
@@ -67,32 +67,6 @@ public function testAddConfigurableProductToCart()
6767
self::assertArrayHasKey('value_label', $option);
6868
}
6969

70-
/**
71-
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
72-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
73-
* @expectedException \Exception
74-
* @expectedExceptionMessage You need to choose options for your item
75-
*/
76-
public function testAddProductWithInvalidOptions()
77-
{
78-
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable'));
79-
$product = current($searchResponse['products']['items']);
80-
81-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
82-
$sku = $product['sku'];
83-
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];
84-
85-
$query = $this->getQuery(
86-
$maskedQuoteId,
87-
$sku,
88-
$attributeId,
89-
9999,
90-
1
91-
);
92-
93-
$this->graphQlMutation($query);
94-
}
95-
9670
/**
9771
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
9872
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
@@ -105,15 +79,13 @@ public function testAddProductIfQuantityIsNotAvailable()
10579
$product = current($searchResponse['products']['items']);
10680

10781
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
108-
$sku = $product['sku'];
109-
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];
110-
$optionId = $product['configurable_options'][0]['values'][1]['value_index'];
82+
$parentSku = $product['sku'];
83+
$sku = 'simple_20';
11184

11285
$query = $this->getQuery(
11386
$maskedQuoteId,
87+
$parentSku,
11488
$sku,
115-
$attributeId,
116-
$optionId,
11789
2000
11890
);
11991

@@ -122,25 +94,20 @@ public function testAddProductIfQuantityIsNotAvailable()
12294

12395
/**
12496
* @param string $maskedQuoteId
97+
* @param string $parentSku
12598
* @param string $sku
126-
* @param int $optionId
127-
* @param int $value
12899
* @param int $quantity
129100
* @return string
130101
*/
131-
private function getQuery(string $maskedQuoteId, string $sku, int $optionId, int $value, int $quantity): string
102+
private function getQuery(string $maskedQuoteId, string $parentSku, string $sku, int $quantity): string
132103
{
133104
return <<<QUERY
134105
mutation {
135106
addConfigurableProductsToCart(
136107
input:{
137108
cart_id:"{$maskedQuoteId}"
138109
cart_items:{
139-
configurable_attributes:[{
140-
id:{$optionId}
141-
value:{$value}
142-
}
143-
]
110+
parent_sku: "{$parentSku}"
144111
data:{
145112
sku:"{$sku}"
146113
quantity:{$quantity}

dev/tests/functional/composer.json

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
},
55
"require": {
66
"php": "~7.1.3||~7.2.0||~7.3.0",
7-
"magento/mtf": "dev-MC-17912",
7+
"magento/mtf": "1.0.0-rc64",
88
"allure-framework/allure-phpunit": "~1.2.0",
99
"doctrine/annotations": "1.4.*",
1010
"phpunit/phpunit": "~6.5.0",
@@ -19,11 +19,5 @@
1919
"Magento\\": ["lib/Magento/", "testsuites/Magento", "generated/Magento/", "tests/app/Magento/"],
2020
"Test\\": "generated/Test/"
2121
}
22-
},
23-
"repositories": [
24-
{
25-
"type": "vcs",
26-
"url": "https://github.com/magento-techdivision/mtf.git"
27-
}
28-
]
22+
}
2923
}

0 commit comments

Comments
 (0)