Skip to content

Commit 35feb07

Browse files
committed
ACP2E-2141: allow only string keys
1 parent a7977ef commit 35feb07

File tree

2 files changed

+198
-1
lines changed

2 files changed

+198
-1
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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\GraphQl\Quote\Guest;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
14+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
15+
use Magento\TestFramework\Fixture\DataFixture;
16+
use Magento\TestFramework\Fixture\DataFixtureStorage;
17+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\TestFramework\TestCase\GraphQlAbstract;
20+
21+
/**
22+
* Get estimate for cart with GraphQl query and variables
23+
*/
24+
class SetShippingAddressForEstimateWithVariablesTest extends GraphQlAbstract
25+
{
26+
/**
27+
* @var ProductRepositoryInterface
28+
*/
29+
private $productRepository;
30+
31+
/**
32+
* @var DataFixtureStorage
33+
*/
34+
private $fixtures;
35+
36+
/**
37+
* @var QuoteIdToMaskedQuoteIdInterface
38+
*/
39+
private $quoteIdToMaskedQuoteIdInterface;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp(): void
45+
{
46+
$objectManager = Bootstrap::getObjectManager();
47+
48+
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
49+
$this->quoteIdToMaskedQuoteIdInterface = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
50+
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
51+
}
52+
53+
/**
54+
* @throws NoSuchEntityException
55+
* @throws \Exception
56+
*/
57+
#[
58+
DataFixture(ProductFixture::class, as: 'product'),
59+
DataFixture(GuestCartFixture::class, as: 'cart'),
60+
]
61+
public function testAddProductsToEmptyCartWithVariables(): void
62+
{
63+
$product = $this->fixtures->get('product');
64+
$cart = $this->fixtures->get('cart');
65+
66+
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
67+
$query = $this->getAddToCartMutation();
68+
$variables = $this->getAddToCartVariables($maskedQuoteId, 1, $product->getSku());
69+
$response = $this->graphQlMutation($query, $variables);
70+
$result = $response['addProductsToCart'];
71+
72+
self::assertEmpty($result['user_errors']);
73+
self::assertCount(1, $result['cart']['items']);
74+
75+
$query = $this->getSetShippingAddressForEstimateMutation();
76+
$variables = $this->getSetShippingAddressForEstimateVariables($maskedQuoteId);
77+
$response = $this->graphQlMutation($query, $variables);
78+
$result = $response['setShippingAddressesOnCart'];
79+
80+
81+
$cartItem = $result['cart']['items'][0];
82+
self::assertEquals($product->getSku(), $cartItem['product']['sku']);
83+
self::assertEquals(1, $cartItem['quantity']);
84+
self::assertEquals("SetShippingAddressesOnCartOutput", $result['__typename']);
85+
}
86+
87+
/**
88+
* Returns GraphQl mutation for adding item to cart
89+
*
90+
* @return string
91+
*/
92+
private function getSetShippingAddressForEstimateMutation(): string
93+
{
94+
return <<<MUTATION
95+
mutation SetShippingAddressForEstimate(\$cartId: String!, \$address: CartAddressInput!) {
96+
setShippingAddressesOnCart(
97+
input: {cart_id: \$cartId, shipping_addresses: [{address: \$address}]}
98+
) {
99+
cart {
100+
id
101+
__typename
102+
items {
103+
id
104+
product {
105+
name
106+
sku
107+
}
108+
quantity
109+
}
110+
}
111+
__typename
112+
}
113+
}
114+
MUTATION;
115+
}
116+
117+
private function getSetShippingAddressForEstimateVariables(
118+
string $maskedQuoteId
119+
): array {
120+
return
121+
[
122+
'cartId' => $maskedQuoteId,
123+
'address' =>
124+
[
125+
'city' => 'New York',
126+
'firstname' => 'Test',
127+
'lastname' => 'Test',
128+
'street' => ['line 1', 'line 2'],
129+
'telephone' => '1234567890',
130+
'postcode' => '11371',
131+
'region' => 'NY',
132+
'country_code' => 'US'
133+
]
134+
];
135+
}
136+
137+
/**
138+
* Returns GraphQl mutation for adding item to cart
139+
*
140+
* @return string
141+
*/
142+
private function getAddToCartMutation(): string
143+
{
144+
return <<<MUTATION
145+
mutation (\$cartId: String!, \$products: [CartItemInput!]!) {
146+
addProductsToCart(cartId: \$cartId, cartItems: \$products) {
147+
cart {
148+
id
149+
items {
150+
uid
151+
quantity
152+
product {
153+
sku
154+
name
155+
thumbnail {
156+
url
157+
__typename
158+
}
159+
__typename
160+
}
161+
prices {
162+
price {
163+
value
164+
currency
165+
}
166+
}
167+
}
168+
}
169+
user_errors {
170+
code
171+
message
172+
}
173+
}
174+
}
175+
MUTATION;
176+
}
177+
178+
private function getAddToCartVariables(
179+
string $maskedQuoteId,
180+
int $qty,
181+
string $sku
182+
): array {
183+
return
184+
[
185+
'cartId' => $maskedQuoteId,
186+
'products' => [
187+
[
188+
'sku' => $sku,
189+
'parent_sku' => $sku,
190+
'quantity' => $qty
191+
]
192+
]
193+
];
194+
}
195+
}

lib/internal/Magento/Framework/GraphQl/Query/Fields.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ private function extractVariables(array &$fields, array $variables): void
102102
if (is_array($value)) {
103103
$this->extractVariables($fields, $value);
104104
} else {
105-
$fields[$key] = $key;
105+
if (is_string($key)) {
106+
$fields[$key] = $key;
107+
}
106108
}
107109
}
108110
}

0 commit comments

Comments
 (0)