From def6657d5ce295e74782484d91604b3c8f8c30c9 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sat, 15 Jun 2019 10:53:12 -0400 Subject: [PATCH 1/4] Add test reproducing magento/graphql-ce#658 --- .../Guest/SetShippingMethodsOnCartTest.php | 146 +++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php index 0c2bf1453b547..55b90b4c9a8eb 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php @@ -185,6 +185,31 @@ public function testSetShippingMethodWithWrongParameters(string $input, string $ $this->graphQlMutation($query); } + /** + * Test region code returns as expected following a failure to set shipping methods + * + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @throws Exception + */ + public function testShippingRegionOnMethodSetError() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $setAddressesResult = $this->graphQlMutation($this->getSetShippingAddressWithLowerCaseCountryOnCartMutation($maskedQuoteId)); + $setAddresses = $setAddressesResult['setShippingAddressesOnCart']['cart']['shipping_addresses']; + + $this->expectException(\Exception::class); + try { + $this->graphQlMutation($this->getInvalidSetShippingMethodMutation($maskedQuoteId)); + } catch (\Exception $e) { + $currentShippingAddresses = $this->queryShippingAddresses($maskedQuoteId); + $this->assertEquals($setAddresses[0]['region']['code'], $currentShippingAddresses[0]['region']['code']); + throw $e; + } + } + /** * @return array * @SuppressWarnings(PHPMD.ExcessiveMethodLength) @@ -331,7 +356,7 @@ public function testSetShippingMethodToCustomerCart() ); $this->graphQlMutation($query); } - + /** * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php @@ -397,4 +422,123 @@ private function getQuery( } QUERY; } + + /** + * Get mutation setting shipping address on cart with lowercase country code + * + * @param string $maskedQuoteId + * @return string + */ + private function getSetShippingAddressWithLowerCaseCountryOnCartMutation(string $maskedQuoteId): string + { + return <<graphQlQuery($query); + return $result['cart']['shipping_addresses'] ?? []; + } } From 5e83ddf275b325737e780cf5ce9dd084b06457f6 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sat, 15 Jun 2019 11:30:13 -0400 Subject: [PATCH 2/4] Transform quote address country code to uppercase Fixes magento/graphql-ce#658 Magento countryIds are uppercase and storing country_id as lowercase in quote address items can cause undesired behavior when the Directory module determines if a region is allowed for a given country. Specifically this check fails when fetching the region code for a quote address: https://github.com/magento/graphql-ce/blob/a2fc3b98bf52ff7741579d16042b9c6376ecebfc/app/code/Magento/Customer/Model/Address/AbstractAddress.php#L400 --- .../QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 6b0e2a311bf44..dd53070cc0ebd 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -74,6 +74,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s } if (null === $customerAddressId) { + $addressInput['country_code'] = strtoupper($addressInput['country_code']); $shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); } else { $customer = $this->getCustomer->execute($context); From e01560c14f30a82ed4f80fced106583876d1ecb0 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sat, 15 Jun 2019 12:19:04 -0400 Subject: [PATCH 3/4] fix static test failures --- .../GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php index 55b90b4c9a8eb..cc9484a2ba0d7 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php @@ -197,7 +197,9 @@ public function testShippingRegionOnMethodSetError() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); - $setAddressesResult = $this->graphQlMutation($this->getSetShippingAddressWithLowerCaseCountryOnCartMutation($maskedQuoteId)); + $setAddressesResult = $this->graphQlMutation( + $this->getSetShippingAddressWithLowerCaseCountryOnCartMutation($maskedQuoteId) + ); $setAddresses = $setAddressesResult['setShippingAddressesOnCart']['cart']['shipping_addresses']; $this->expectException(\Exception::class); From 2c05ca60337f83a70910d48cb08ee3397c0422ed Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 20 Jun 2019 16:01:26 -0500 Subject: [PATCH 4/4] magento/graphql-ce#658: region.code inside shipping_addresses resets to null if error occured during setShippingMethodsOnCart --- .../Customer/SetShippingAddressOnCartTest.php | 53 +++++++ .../Guest/SetShippingAddressOnCartTest.php | 104 +++++++++++++ .../Guest/SetShippingMethodsOnCartTest.php | 146 ------------------ 3 files changed, 157 insertions(+), 146 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php index 5022274b5f061..15ee125955062 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php @@ -602,6 +602,59 @@ public function testSetShippingAddressToGuestCart() $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + */ + public function testSetShippingAddressWithLowerCaseCountry() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); + + self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']); + $address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']); + + $this->assertEquals('US', $address['country']['code']); + $this->assertEquals('CA', $address['region']['code']); + } + /** * Verify the all the whitelisted fields for a New Address Object * diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php index 9751584a48731..537c8f09a0a98 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php @@ -407,6 +407,110 @@ public function testSetShippingAddressOnNonExistentCart() $this->graphQlMutation($query); } + /** + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + */ + public function testSetShippingAddressWithLowerCaseCountry() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $query = <<graphQlMutation($query); + + self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']); + $address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']); + + $this->assertEquals('US', $address['country']['code']); + $this->assertEquals('CA', $address['region']['code']); + } + + /** + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + */ + public function testSetShippingAddressWithLowerCaseRegion() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $query = <<graphQlMutation($query); + + self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']); + $address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']); + + $this->assertEquals('US', $address['country']['code']); + $this->assertEquals('CA', $address['region']['code']); + } + /** * Verify the all the whitelisted fields for a New Address Object * diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php index cc9484a2ba0d7..60c8bee75e2ad 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php @@ -185,33 +185,6 @@ public function testSetShippingMethodWithWrongParameters(string $input, string $ $this->graphQlMutation($query); } - /** - * Test region code returns as expected following a failure to set shipping methods - * - * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php - * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php - * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php - * @throws Exception - */ - public function testShippingRegionOnMethodSetError() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); - - $setAddressesResult = $this->graphQlMutation( - $this->getSetShippingAddressWithLowerCaseCountryOnCartMutation($maskedQuoteId) - ); - $setAddresses = $setAddressesResult['setShippingAddressesOnCart']['cart']['shipping_addresses']; - - $this->expectException(\Exception::class); - try { - $this->graphQlMutation($this->getInvalidSetShippingMethodMutation($maskedQuoteId)); - } catch (\Exception $e) { - $currentShippingAddresses = $this->queryShippingAddresses($maskedQuoteId); - $this->assertEquals($setAddresses[0]['region']['code'], $currentShippingAddresses[0]['region']['code']); - throw $e; - } - } - /** * @return array * @SuppressWarnings(PHPMD.ExcessiveMethodLength) @@ -424,123 +397,4 @@ private function getQuery( } QUERY; } - - /** - * Get mutation setting shipping address on cart with lowercase country code - * - * @param string $maskedQuoteId - * @return string - */ - private function getSetShippingAddressWithLowerCaseCountryOnCartMutation(string $maskedQuoteId): string - { - return <<graphQlQuery($query); - return $result['cart']['shipping_addresses'] ?? []; - } }