Skip to content

Commit 3488226

Browse files
committed
ACP2E-499: [Cloud-Graphql] in setShippingAddressesOnCart, region fiend does not accept number as input
- With test
1 parent 15cf009 commit 3488226

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
100100
throw new GraphQlInputException(__('Country is not available'));
101101
}
102102

103+
if (!empty($addressInput['region'])) {
104+
$this->normalizeRegion($addressInput);
105+
}
106+
103107
$this->validateRegion($addressInput);
104108

105109
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
@@ -144,6 +148,26 @@ private function validateRegion(array $addressInput): void
144148
}
145149
}
146150

151+
/**
152+
* Normalize region code to region id while requesting to setShippingAddress
153+
*
154+
* @param array $addressInput
155+
*/
156+
private function normalizeRegion(array &$addressInput)
157+
{
158+
$shippingAddressRegion = $addressInput['region'];
159+
if (is_numeric($shippingAddressRegion)) {
160+
$regionCollection = $this->regionCollectionFactory
161+
->create()
162+
->addCountryFilter($addressInput['country_code']);
163+
$allRegions = $regionCollection->toOptionArray();
164+
$regionId = (int) $addressInput['region'];
165+
if (array_key_exists($regionId, $allRegions)) {
166+
$addressInput['region'] = $allRegions[$regionId]['value'];
167+
}
168+
}
169+
}
170+
147171
/**
148172
* Validate the address region when region is required for the country
149173
*

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,70 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct()
139139
$this->assertNewShippingAddressFields($shippingAddressResponse);
140140
}
141141

142+
/**
143+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
144+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
145+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
146+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
147+
*/
148+
public function testSetShippingAddressOnCartWithRegionIdForSimpleProduct()
149+
{
150+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
151+
152+
$query = <<<QUERY
153+
mutation {
154+
setShippingAddressesOnCart(
155+
input: {
156+
cart_id: "$maskedQuoteId"
157+
shipping_addresses: [
158+
{
159+
address: {
160+
firstname: "Francesco"
161+
lastname: "Alba"
162+
company: "Magento"
163+
street: ["Via Solferino", "45"]
164+
city: "Ceriano Laghetto"
165+
region: "1"
166+
postcode: "20816"
167+
country_code: "FR"
168+
telephone: "3273581975",
169+
save_in_address_book: false
170+
}
171+
}
172+
]
173+
}
174+
) {
175+
cart {
176+
shipping_addresses {
177+
firstname
178+
lastname
179+
company
180+
street
181+
city
182+
region {
183+
code
184+
label
185+
}
186+
postcode
187+
telephone
188+
country {
189+
label
190+
code
191+
}
192+
}
193+
}
194+
}
195+
}
196+
QUERY;
197+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
198+
199+
self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
200+
$cartResponse = $response['setShippingAddressesOnCart']['cart'];
201+
self::assertArrayHasKey('shipping_addresses', $cartResponse);
202+
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
203+
$this->assertShippingAddressWithRegionIdFields($shippingAddressResponse);
204+
}
205+
142206
/**
143207
* @magentoApiDataFixture Magento/Customer/_files/customer.php
144208
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
@@ -1819,6 +1883,28 @@ private function assertNewShippingAddressFields(array $shippingAddressResponse):
18191883
$this->assertResponseFields($shippingAddressResponse, $assertionMap);
18201884
}
18211885

1886+
/**
1887+
* Verify the all the whitelisted fields for a New Address with region id Object
1888+
*
1889+
* @param array $shippingAddressResponse
1890+
*/
1891+
private function assertShippingAddressWithRegionIdFields(array $shippingAddressResponse): void
1892+
{
1893+
$assertionMap = [
1894+
['response_field' => 'firstname', 'expected_value' => 'Francesco'],
1895+
['response_field' => 'lastname', 'expected_value' => 'Alba'],
1896+
['response_field' => 'company', 'expected_value' => 'Magento'],
1897+
['response_field' => 'street', 'expected_value' => [0 => 'Via Solferino', 1 => '45']],
1898+
['response_field' => 'city', 'expected_value' => 'Ceriano Laghetto'],
1899+
['response_field' => 'postcode', 'expected_value' => '20816'],
1900+
['response_field' => 'telephone', 'expected_value' => '3273581975'],
1901+
['response_field' => 'region', 'expected_value' => ['code' => '1', 'label' => 'Ain']],
1902+
['response_field' => 'country', 'expected_value' => ['code' => 'FR', 'label' => 'FR']]
1903+
];
1904+
1905+
$this->assertResponseFields($shippingAddressResponse, $assertionMap);
1906+
}
1907+
18221908
/**
18231909
* Verify the all the whitelisted fields for a Address Object
18241910
*

0 commit comments

Comments
 (0)