diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingMethodsOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingMethodsOnCartTest.php index 736ea69440753..eae7a0374d210 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingMethodsOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingMethodsOnCartTest.php @@ -13,6 +13,7 @@ use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; +use Magento\Quote\Model\Quote; /** * Test for setting shipping methods on cart for customer @@ -116,6 +117,33 @@ public function testSetMultipleShippingMethods() $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/423'); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testReSetShippingMethod() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $quote = $this->getQuoteByReversedQuoteId('test_order_1'); + $shippingAddressId = $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode, $carrierCode, $shippingAddressId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('setShippingMethodsOnCart', $response); + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + foreach ($response['setShippingMethodsOnCart']['cart']['shipping_addresses'] as $address) { + self::assertArrayHasKey('address_id', $address); + if ($address['address_id'] == $shippingAddressId) { + self::assertArrayHasKey('selected_shipping_method', $address); + self::assertEquals($methodCode, $address['selected_shipping_method']['method_code']); + self::assertEquals($carrierCode, $address['selected_shipping_method']['carrier_code']); + } + } + } + /** * @param string $maskedQuoteId * @param string $shippingMethodCode @@ -135,18 +163,15 @@ private function prepareMutationQuery( setShippingMethodsOnCart(input: { cart_id: "$maskedQuoteId", - shipping_addresses: [{ + shipping_methods: [{ cart_address_id: $shippingAddressId - shipping_method: { - method_code: "$shippingMethodCode" - carrier_code: "$shippingCarrierCode" - } + method_code: "$shippingMethodCode" + carrier_code: "$shippingCarrierCode" }] }) { - cart { - cart_id, shipping_addresses { + address_id selected_shipping_method { carrier_code method_code @@ -174,6 +199,18 @@ private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): str return $this->quoteIdToMaskedId->execute((int)$quote->getId()); } + /** + * @param string $reversedQuoteId + * @return Quote + */ + private function getQuoteByReversedQuoteId(string $reversedQuoteId): Quote + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $quote; + } + /** * @param string $reversedQuoteId * @param int $customerId 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 f159cb6f6151e..14662148b32dc 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 @@ -7,6 +7,7 @@ namespace Magento\GraphQl\Quote\Guest; +use Magento\Quote\Model\Quote; use Magento\Quote\Model\QuoteFactory; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; @@ -109,6 +110,34 @@ public function testSetMultipleShippingMethods() $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testReSetShippingMethod() + { + $maskedQuoteId = $this->unAssignCustomerFromQuoteAndShippingAddress('test_order_1'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $quote = $this->getQuoteByReversedQuoteId('test_order_1'); + $shippingAddress = $quote->getShippingAddress(); + $shippingAddressId = $shippingAddress->getId(); + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode, $carrierCode, $shippingAddressId); + + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('setShippingMethodsOnCart', $response); + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + foreach ($response['setShippingMethodsOnCart']['cart']['shipping_addresses'] as $address) { + self::assertArrayHasKey('address_id', $address); + if ($address['address_id'] == $shippingAddressId) { + self::assertArrayHasKey('selected_shipping_method', $address); + self::assertEquals($methodCode, $address['selected_shipping_method']['method_code']); + self::assertEquals($carrierCode, $address['selected_shipping_method']['carrier_code']); + } + } + } + /** * @param string $maskedQuoteId * @param string $shippingMethodCode @@ -128,16 +157,15 @@ private function prepareMutationQuery( setShippingMethodsOnCart(input: { cart_id: "$maskedQuoteId", - shipping_addresses: [{ + shipping_methods: [{ cart_address_id: $shippingAddressId - shipping_method: { - method_code: "$shippingMethodCode" - carrier_code: "$shippingCarrierCode" - } + method_code: "$shippingMethodCode" + carrier_code: "$shippingCarrierCode" }] }) { cart { shipping_addresses { + address_id selected_shipping_method { carrier_code method_code @@ -152,6 +180,40 @@ private function prepareMutationQuery( QUERY; } + /** + * @param string $reversedQuoteId + * @param int $customerId + * @return string + */ + private function unAssignCustomerFromQuoteAndShippingAddress( + string $reversedQuoteId + ): string { + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + $quote->setCustomerId(0); + + $shippingAddress = $quote->getShippingAddress(); + $shippingAddress->setCustomerId(0); + $shippingAddress->setCustomerAddressId(0); + + $this->quoteResource->save($quote); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $reversedQuoteId + * @return Quote + */ + private function getQuoteByReversedQuoteId(string $reversedQuoteId): Quote + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $quote; + } + /** * @param string $reversedQuoteId * @return string