Skip to content

Commit 2c05ca6

Browse files
committed
magento/graphql-ce#658: region.code inside shipping_addresses resets to null if error occured during setShippingMethodsOnCart
1 parent e01560c commit 2c05ca6

File tree

3 files changed

+157
-146
lines changed

3 files changed

+157
-146
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,59 @@ public function testSetShippingAddressToGuestCart()
602602
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
603603
}
604604

605+
/**
606+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
607+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
608+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
609+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
610+
*/
611+
public function testSetShippingAddressWithLowerCaseCountry()
612+
{
613+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
614+
615+
$query = <<<QUERY
616+
mutation {
617+
setShippingAddressesOnCart(
618+
input: {
619+
cart_id: "{$maskedQuoteId}"
620+
shipping_addresses: [
621+
{
622+
address: {
623+
firstname: "John"
624+
lastname: "Doe"
625+
street: ["6161 West Centinella Avenue"]
626+
city: "Culver City"
627+
region: "CA"
628+
postcode: "90230"
629+
country_code: "us"
630+
telephone: "555-555-55-55"
631+
}
632+
}
633+
]
634+
}
635+
) {
636+
cart {
637+
shipping_addresses {
638+
region {
639+
code
640+
}
641+
country {
642+
code
643+
}
644+
}
645+
}
646+
}
647+
}
648+
QUERY;
649+
$result = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
650+
651+
self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']);
652+
$address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']);
653+
654+
$this->assertEquals('US', $address['country']['code']);
655+
$this->assertEquals('CA', $address['region']['code']);
656+
}
657+
605658
/**
606659
* Verify the all the whitelisted fields for a New Address Object
607660
*

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,110 @@ public function testSetShippingAddressOnNonExistentCart()
407407
$this->graphQlMutation($query);
408408
}
409409

410+
/**
411+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
412+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
413+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
414+
*/
415+
public function testSetShippingAddressWithLowerCaseCountry()
416+
{
417+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
418+
419+
$query = <<<QUERY
420+
mutation {
421+
setShippingAddressesOnCart(
422+
input: {
423+
cart_id: "{$maskedQuoteId}"
424+
shipping_addresses: [
425+
{
426+
address: {
427+
firstname: "John"
428+
lastname: "Doe"
429+
street: ["6161 West Centinella Avenue"]
430+
city: "Culver City"
431+
region: "CA"
432+
postcode: "90230"
433+
country_code: "us"
434+
telephone: "555-555-55-55"
435+
}
436+
}
437+
]
438+
}
439+
) {
440+
cart {
441+
shipping_addresses {
442+
region {
443+
code
444+
}
445+
country {
446+
code
447+
}
448+
}
449+
}
450+
}
451+
}
452+
QUERY;
453+
$result = $this->graphQlMutation($query);
454+
455+
self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']);
456+
$address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']);
457+
458+
$this->assertEquals('US', $address['country']['code']);
459+
$this->assertEquals('CA', $address['region']['code']);
460+
}
461+
462+
/**
463+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
464+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
465+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
466+
*/
467+
public function testSetShippingAddressWithLowerCaseRegion()
468+
{
469+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
470+
471+
$query = <<<QUERY
472+
mutation {
473+
setShippingAddressesOnCart(
474+
input: {
475+
cart_id: "{$maskedQuoteId}"
476+
shipping_addresses: [
477+
{
478+
address: {
479+
firstname: "John"
480+
lastname: "Doe"
481+
street: ["6161 West Centinella Avenue"]
482+
city: "Culver City"
483+
region: "ca"
484+
postcode: "90230"
485+
country_code: "US"
486+
telephone: "555-555-55-55"
487+
}
488+
}
489+
]
490+
}
491+
) {
492+
cart {
493+
shipping_addresses {
494+
region {
495+
code
496+
}
497+
country {
498+
code
499+
}
500+
}
501+
}
502+
}
503+
}
504+
QUERY;
505+
$result = $this->graphQlMutation($query);
506+
507+
self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']);
508+
$address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']);
509+
510+
$this->assertEquals('US', $address['country']['code']);
511+
$this->assertEquals('CA', $address['region']['code']);
512+
}
513+
410514
/**
411515
* Verify the all the whitelisted fields for a New Address Object
412516
*

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -185,33 +185,6 @@ public function testSetShippingMethodWithWrongParameters(string $input, string $
185185
$this->graphQlMutation($query);
186186
}
187187

188-
/**
189-
* Test region code returns as expected following a failure to set shipping methods
190-
*
191-
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
192-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
193-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
194-
* @throws Exception
195-
*/
196-
public function testShippingRegionOnMethodSetError()
197-
{
198-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
199-
200-
$setAddressesResult = $this->graphQlMutation(
201-
$this->getSetShippingAddressWithLowerCaseCountryOnCartMutation($maskedQuoteId)
202-
);
203-
$setAddresses = $setAddressesResult['setShippingAddressesOnCart']['cart']['shipping_addresses'];
204-
205-
$this->expectException(\Exception::class);
206-
try {
207-
$this->graphQlMutation($this->getInvalidSetShippingMethodMutation($maskedQuoteId));
208-
} catch (\Exception $e) {
209-
$currentShippingAddresses = $this->queryShippingAddresses($maskedQuoteId);
210-
$this->assertEquals($setAddresses[0]['region']['code'], $currentShippingAddresses[0]['region']['code']);
211-
throw $e;
212-
}
213-
}
214-
215188
/**
216189
* @return array
217190
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -424,123 +397,4 @@ private function getQuery(
424397
}
425398
QUERY;
426399
}
427-
428-
/**
429-
* Get mutation setting shipping address on cart with lowercase country code
430-
*
431-
* @param string $maskedQuoteId
432-
* @return string
433-
*/
434-
private function getSetShippingAddressWithLowerCaseCountryOnCartMutation(string $maskedQuoteId): string
435-
{
436-
return <<<QUERY
437-
mutation {
438-
setShippingAddressesOnCart(
439-
input: {
440-
cart_id: "{$maskedQuoteId}"
441-
shipping_addresses: [
442-
{
443-
address: {
444-
firstname: "John"
445-
lastname: "Doe"
446-
street: ["6161 West Centinella Avenue"]
447-
city: "Culver City"
448-
region: "CA"
449-
postcode: "90230"
450-
country_code: "us"
451-
telephone: "555-555-55-55"
452-
save_in_address_book: false
453-
}
454-
}
455-
]
456-
}
457-
) {
458-
cart {
459-
shipping_addresses {
460-
firstname
461-
lastname
462-
city
463-
postcode
464-
region {
465-
label
466-
code
467-
}
468-
selected_shipping_method {
469-
carrier_code
470-
method_code
471-
}
472-
available_shipping_methods {
473-
carrier_code
474-
method_code
475-
carrier_title
476-
method_title
477-
}
478-
}
479-
}
480-
}
481-
}
482-
QUERY;
483-
}
484-
485-
/**
486-
* Get mutation setting invalid shipping method on cart
487-
*
488-
* @param string $maskedQuoteId
489-
* @return string
490-
*/
491-
private function getInvalidSetShippingMethodMutation(string $maskedQuoteId): string
492-
{
493-
return <<<QUERY
494-
mutation {
495-
setShippingMethodsOnCart(input: {
496-
cart_id: "{$maskedQuoteId}",
497-
shipping_methods: [{
498-
carrier_code: "flatrate"
499-
method_code: "wrong-carrier-code"
500-
}]
501-
}) {
502-
cart {
503-
shipping_addresses {
504-
selected_shipping_method {
505-
carrier_code
506-
}
507-
}
508-
}
509-
}
510-
}
511-
QUERY;
512-
}
513-
514-
/**
515-
* Get current shipping addresses for a given masked quote id
516-
*
517-
* @param string $maskedQuoteId
518-
* @return array
519-
* @throws Exception
520-
*/
521-
private function queryShippingAddresses(string $maskedQuoteId): array
522-
{
523-
$query = <<<QUERY
524-
{
525-
cart(cart_id:"{$maskedQuoteId}") {
526-
shipping_addresses {
527-
street
528-
city
529-
postcode
530-
region {
531-
label
532-
code
533-
}
534-
country {
535-
code
536-
label
537-
}
538-
}
539-
}
540-
}
541-
QUERY;
542-
543-
$result = $this->graphQlQuery($query);
544-
return $result['cart']['shipping_addresses'] ?? [];
545-
}
546400
}

0 commit comments

Comments
 (0)