From 4f19d038cf47cca7664cf9664ee667573200e2da Mon Sep 17 00:00:00 2001 From: Yogesh Suhagiya Date: Tue, 12 Mar 2019 18:39:04 +0530 Subject: [PATCH 01/26] Corrected Mutation Query for SetShippingMethodsOnCartTest --- .../Quote/Guest/SetShippingMethodsOnCartTest.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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..b452c2cd9d6d2 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 @@ -113,7 +113,7 @@ public function testSetMultipleShippingMethods() * @param string $maskedQuoteId * @param string $shippingMethodCode * @param string $shippingCarrierCode - * @param string $shippingAddressId + * @param int $shippingAddressId * @return string * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ @@ -121,21 +121,20 @@ private function prepareMutationQuery( string $maskedQuoteId, string $shippingMethodCode, string $shippingCarrierCode, - string $shippingAddressId + int $shippingAddressId ) : string { return << Date: Wed, 13 Mar 2019 17:14:51 +0530 Subject: [PATCH 02/26] Covered all the cases of SetShippingMethod for Guest --- .../Guest/SetShippingMethodsOnCartTest.php | 400 +++++++++++++++++- ...ote_with_simple_product_saved_rollback.php | 2 + ...h_virtual_product_and_address_rollback.php | 4 + ...te_with_virtual_product_saved_rollback.php | 2 + .../_files/simple_product_rollback.php | 24 ++ 5 files changed, 419 insertions(+), 13 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php 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 b452c2cd9d6d2..e76974b5290e5 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 @@ -12,6 +12,7 @@ use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; +use Magento\Quote\Model\Quote\Address as QuoteAddress; /** * Test for setting shipping methods on cart for guest @@ -33,6 +34,11 @@ class SetShippingMethodsOnCartTest extends GraphQlAbstract */ private $quoteIdToMaskedId; + /** + * @var QuoteAddress + */ + private $quoteAddress; + /** * @inheritdoc */ @@ -42,71 +48,409 @@ protected function setUp() $this->quoteResource = $objectManager->get(QuoteResource::class); $this->quoteFactory = $objectManager->get(QuoteFactory::class); $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->quoteAddress = $objectManager->get(QuoteAddress::class); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php + */ public function testShippingMethodWithVirtualProduct() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'test_order_with_virtual_product_without_address'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $quoteAddressId = $this->getQuoteAddressIdByReversedQuoteId($reservedOrderId); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + self::expectException(\Exception::class); + self::expectExceptionMessage('Carrier with such method not found: ' . $methodCode . ', ' . $methodCode . ''); + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ public function testShippingMethodWithSimpleProduct() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'test_order_with_simple_product_without_address'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $quoteAddressId = $this->getQuoteAddressIdByReversedQuoteId($reservedOrderId); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('setShippingMethodsOnCart', $response); + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + + $selectedShippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $selectedShippingMethod); + self::assertEquals($carrierCode, $selectedShippingMethod['selected_shipping_method']['carrier_code']); + self::assertEquals($methodCode, $selectedShippingMethod['selected_shipping_method']['method_code']); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage Required parameter "cart_address_id" is missing. + */ public function testShippingMethodWithSimpleProductWithoutAddress() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'test_order_with_simple_product_without_address'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $quoteAddressId = 0; + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage Required parameter "method_code" is missing. + */ public function testSetShippingMethodWithMissedRequiredParameters() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = ''; + $carrierCode = 'flatrate'; + $reservedOrderId = 'test_order_with_simple_product_without_address'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $quoteAddressId = $this->getQuoteAddressIdByReversedQuoteId($reservedOrderId); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + */ public function testSetNonExistentShippingMethod() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'non-existed-method-code'; + $carrierCode = 'non-carrier-method-code'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, 'guest_quote', 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + self::expectException(\Exception::class); + self::expectExceptionMessage('Carrier with such method not found: ' . $carrierCode . ', ' . $methodCode . ''); + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ public function testSetShippingMethodIfAddressIsNotBelongToCart() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, 'test_order_with_virtual_product', 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + self::expectException(\Exception::class); + self::expectExceptionMessage('The current user cannot use cart address with ID "' . $quoteAddressId . '"'); + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @expectedException \Magento\Framework\Exception\NoSuchEntityException + * @expectedExceptionMessage No such entity with cartId = 0 + */ public function testSetShippingMethodToNonExistentCart() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'non_existent_cart_reversed_quote_id'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $quoteAddressId = 1; + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + */ public function testSetShippingMethodToGuestCart() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('setShippingMethodsOnCart', $response); + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + + $selectedShippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $selectedShippingMethod); + self::assertEquals($carrierCode, $selectedShippingMethod['selected_shipping_method']['carrier_code']); + self::assertEquals($methodCode, $selectedShippingMethod['selected_shipping_method']['method_code']); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ public function testSetShippingMethodToAnotherCustomerCart() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_virtual_product'); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + self::expectException(\Exception::class); + self::expectExceptionMessage( + 'The current user cannot perform operations on cart "' . $maskedQuoteId . '"' + ); + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + */ public function testSetShippingMethodToNonExistentCartAddress() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $quoteAddressId = 1963425585; + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + self::expectException(\Exception::class); + self::expectExceptionMessage( + 'Could not find a cart address with ID "' . $quoteAddressId . '"' + ); + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + */ public function testSetShippingMethodToGuestCartAddress() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('setShippingMethodsOnCart', $response); + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + + $selectedShippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $selectedShippingMethod); + self::assertEquals($carrierCode, $selectedShippingMethod['selected_shipping_method']['carrier_code']); + self::assertEquals($methodCode, $selectedShippingMethod['selected_shipping_method']['method_code']); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ public function testSetShippingMethodToAnotherCustomerCartAddress() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'test_order_1'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode, + $carrierCode, + $quoteAddressId + ); + + self::expectException(\Exception::class); + self::expectExceptionMessage( + 'The current user cannot perform operations on cart "' . $maskedQuoteId . '"' + ); + $this->graphQlQuery($query); } + /** + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php + * @expectedException \Exception + * @expectedExceptionMessage You cannot specify multiple shipping methods. + */ public function testSetMultipleShippingMethods() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422'); + $methodCode = 'flatrate'; + $carrierCode = 'flatrate'; + $reservedOrderId = 'guest_quote'; + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $shippingAddressId = (int) $quote->getShippingAddress()->getId(); + + $query = <<graphQlQuery($query); } /** @@ -163,4 +507,34 @@ private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): str return $this->quoteIdToMaskedId->execute((int)$quote->getId()); } + + /** + * @param string $reversedQuoteId + * @return int + */ + private function getQuoteAddressIdByReversedQuoteId(string $reversedQuoteId): int + { + $guestAddress = $this->quoteAddress->setData([ + 'firstname'=> 'John', + 'lastname'=> 'Smith', + 'company'=> 'Company Name', + 'street'=> 'Green str, 67', + 'city'=> 'CityM', + 'region' => 'AL', + 'postcode'=> 75477, + 'telephone'=> 3468676, + 'country_id'=> 'US', + 'region_id' => 1 + ]); + + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + $quote->setBillingAddress($guestAddress); + $quote->setShippingAddress($guestAddress); + $quote->collectTotals(); + $this->quoteResource->save($quote); + + return (int) $quote->getShippingAddress()->getId(); + } } diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php index 105a981ccfc84..39b758447221c 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php @@ -14,3 +14,5 @@ /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ $quoteIdMask = $objectManager->create(\Magento\Quote\Model\QuoteIdMask::class); $quoteIdMask->delete($quote->getId()); + +require 'simple_product_rollback.php'; diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php index 2b906bdc022f5..52ce4b4006db8 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php @@ -13,3 +13,7 @@ /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ $quoteIdMask = $objectManager->create(\Magento\Quote\Model\QuoteIdMask::class); $quoteIdMask->delete($quote->getId()); + +require __DIR__ . '/../../Customer/_files/customer_rollback.php'; +require __DIR__ . '/../../Customer/_files/customer_address_rollback.php'; +require __DIR__ . '/../../../Magento/Catalog/_files/product_virtual_rollback.php'; diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php index b3224bb527442..afcb7f56f8d1b 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_saved_rollback.php @@ -14,3 +14,5 @@ /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ $quoteIdMask = $objectManager->create(\Magento\Quote\Model\QuoteIdMask::class); $quoteIdMask->delete($quote->getId()); + +require __DIR__ . '/../../../Magento/Catalog/_files/product_virtual_rollback.php'; diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php new file mode 100644 index 0000000000000..f03784519f371 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php @@ -0,0 +1,24 @@ +get(\Magento\Framework\Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + +try { + $product = $productRepository->get('simple', false, null, true); + $productRepository->delete($product); +} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { + //Product already removed +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); From 4bdc30ed09f7093c772d359daecd7980e9555e46 Mon Sep 17 00:00:00 2001 From: Yogesh Suhagiya Date: Wed, 13 Mar 2019 17:32:33 +0530 Subject: [PATCH 03/26] Fixed issue of undefined variable: objectManager --- .../Magento/Checkout/_files/simple_product_rollback.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php index f03784519f371..43beafe822043 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php @@ -3,9 +3,11 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +/** @var \Magento\Framework\ObjectManagerInterface $objectManager */ +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); /** @var \Magento\Framework\Registry $registry */ -$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class); +$registry = $objectManager->get(\Magento\Framework\Registry::class); $registry->unregister('isSecureArea'); $registry->register('isSecureArea', true); From f9ecace0ff70df87cf8c08de17ee5cf557f8d339 Mon Sep 17 00:00:00 2001 From: Yogesh Suhagiya Date: Wed, 13 Mar 2019 18:52:02 +0530 Subject: [PATCH 04/26] Renamed variable and keep name length under 20. --- .../Guest/SetShippingMethodsOnCartTest.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) 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 e76974b5290e5..f6444027d0ca3 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 @@ -100,10 +100,10 @@ public function testShippingMethodWithSimpleProduct() self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); - $selectedShippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - self::assertArrayHasKey('selected_shipping_method', $selectedShippingMethod); - self::assertEquals($carrierCode, $selectedShippingMethod['selected_shipping_method']['carrier_code']); - self::assertEquals($methodCode, $selectedShippingMethod['selected_shipping_method']['method_code']); + $shippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingMethod); + self::assertEquals($carrierCode, $shippingMethod['selected_shipping_method']['carrier_code']); + self::assertEquals($methodCode, $shippingMethod['selected_shipping_method']['method_code']); } /** @@ -264,10 +264,10 @@ public function testSetShippingMethodToGuestCart() self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); - $selectedShippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - self::assertArrayHasKey('selected_shipping_method', $selectedShippingMethod); - self::assertEquals($carrierCode, $selectedShippingMethod['selected_shipping_method']['carrier_code']); - self::assertEquals($methodCode, $selectedShippingMethod['selected_shipping_method']['method_code']); + $shippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingMethod); + self::assertEquals($carrierCode, $shippingMethod['selected_shipping_method']['carrier_code']); + self::assertEquals($methodCode, $shippingMethod['selected_shipping_method']['method_code']); } /** @@ -360,10 +360,10 @@ public function testSetShippingMethodToGuestCartAddress() self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); - $selectedShippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - self::assertArrayHasKey('selected_shipping_method', $selectedShippingMethod); - self::assertEquals($carrierCode, $selectedShippingMethod['selected_shipping_method']['carrier_code']); - self::assertEquals($methodCode, $selectedShippingMethod['selected_shipping_method']['method_code']); + $shippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingMethod); + self::assertEquals($carrierCode, $shippingMethod['selected_shipping_method']['carrier_code']); + self::assertEquals($methodCode, $shippingMethod['selected_shipping_method']['method_code']); } /** From 36a59815ee8fcbb7f78ab1b360afd3f250df4d67 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 13 Mar 2019 17:03:04 +0200 Subject: [PATCH 05/26] 428 - Test coverage: GetAvailableShippingMethodsTest --- .../GetAvailableShippingMethodsTest.php | 184 ++++++++++++++++++ .../Quote/GetAvailableShippingMethodsTest.php | 121 ------------ .../Guest/GetAvailableShippingMethodsTest.php | 162 +++++++++++++++ .../disable_all_active_shipping_methods.php | 39 ++++ ...e_all_active_shipping_methods_rollback.php | 33 ++++ 5 files changed, 418 insertions(+), 121 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php new file mode 100644 index 0000000000000..9a8b3a254c435 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -0,0 +1,184 @@ +quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteResource = $objectManager->create(QuoteResource::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * Test case: get available shipping methods from current customer quote + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetAvailableShippingMethods() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]); + self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); + + $expectedAddressData = [ + 'amount' => 10, + 'base_amount' => 10, + 'carrier_code' => 'flatrate', + 'carrier_title' => 'Flat Rate', + 'error_message' => '', + 'method_code' => 'flatrate', + 'method_title' => 'Fixed', + 'price_incl_tax' => 10, + 'price_excl_tax' => 10, + ]; + self::assertEquals( + $expectedAddressData, + $response['cart']['shipping_addresses'][0]['available_shipping_methods'][0] + ); + } + + /** + * Test case: get available shipping methods from quote of another customer + * + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetAvailableShippingMethodsFromAnotherCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + + $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap('customer2@search.example.com')); + } + + /** + * Test case: get available shipping methods when all shipping methods are disabled + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Checkout/_files/disable_all_active_shipping_methods.php + */ + public function testGetAvailableShippingMethodsIfShippingsAreNotSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); + + self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); + } + + /** + * Test case: get available shipping methods from non-existent cart + * + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @expectedException \Exception + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" + */ + public function testGetAvailableShippingMethodsOfNonExistentCart() + { + $maskedQuoteId = 'non_existent_masked_id'; + $query = $this->getQuery($maskedQuoteId); + + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getQuery( + string $maskedQuoteId + ): string { + return <<customerTokenService->createCustomerAccessToken($username, $password); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + return $headerMap; + } + + /** + * @param string $reservedOrderId + * @return string + */ + private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php deleted file mode 100644 index 1a0ccbd198c37..0000000000000 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php +++ /dev/null @@ -1,121 +0,0 @@ -customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - $this->quoteResource = $objectManager->create(QuoteResource::class); - $this->quote = $objectManager->create(Quote::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testGetAvailableShippingMethods() - { - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - - $query = <<graphQlQuery( - $query, - [], - '', - $this->getCustomerAuthHeaders('customer@example.com', 'password') - ); - self::assertArrayHasKey('cart', $response); - self::assertArrayHasKey('shipping_addresses', $response['cart']); - self::assertCount(1, $response['cart']['shipping_addresses']); - self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]); - self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); - - $expectedAddressData = [ - 'amount' => 10, - 'base_amount' => 10, - 'carrier_code' => 'flatrate', - 'carrier_title' => 'Flat Rate', - 'error_message' => '', - 'method_code' => 'flatrate', - 'method_title' => 'Fixed', - 'price_incl_tax' => 10, - 'price_excl_tax' => 10, - ]; - self::assertEquals( - $expectedAddressData, - $response['cart']['shipping_addresses'][0]['available_shipping_methods'][0] - ); - } - - /** - * @param string $email - * @param string $password - * @return array - */ - private function getCustomerAuthHeaders(string $email, string $password): array - { - $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); - return ['Authorization' => 'Bearer ' . $customerToken]; - } -} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php new file mode 100644 index 0000000000000..8834fd5bfccc5 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php @@ -0,0 +1,162 @@ +quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteResource = $objectManager->create(QuoteResource::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * Test case: get available shipping methods from current customer quote + * + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + */ + public function testGetAvailableShippingMethodsFromGuestCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); + + self::assertArrayHasKey('cart', $response); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]); + self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); + + $expectedAddressData = [ + 'amount' => 5, + 'base_amount' => 5, + 'carrier_code' => 'flatrate', + 'carrier_title' => 'Flat Rate', + 'error_message' => '', + 'method_code' => 'flatrate', + 'method_title' => 'Fixed', + 'price_incl_tax' => 5, + 'price_excl_tax' => 5, + ]; + self::assertEquals( + $expectedAddressData, + $response['cart']['shipping_addresses'][0]['available_shipping_methods'][0] + ); + } + + /** + * Test case: get available shipping methods from customer's quote + * + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetAvailableShippingMethodsFromAnotherCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + + $this->graphQlQuery($this->getQuery($maskedQuoteId)); + } + + /** + * Test case: get available shipping methods when all shipping methods are disabled + * + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/Checkout/_files/disable_all_active_shipping_methods.php + */ + public function testGetAvailableShippingMethodsIfShippingsAreNotSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); + + self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); + } + + /** + * Test case: get available shipping methods from non-existent cart + * + * @expectedException \Exception + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" + */ + public function testGetAvailableShippingMethodsOfNonExistentCart() + { + $maskedQuoteId = 'non_existent_masked_id'; + + $this->graphQlQuery($this->getQuery($maskedQuoteId)); + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getQuery( + string $maskedQuoteId + ): string { + return <<quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php new file mode 100644 index 0000000000000..925a3bd6503aa --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php @@ -0,0 +1,39 @@ +get(Magento\Shipping\Model\Config::class); +$rollbackConfigKey = 'test/carriers/disabled_shipment_methods'; +$configData = []; +$disabledShipmentMethods = []; + +// Get all active Shipping Methods +foreach ($shippingConfig->getAllCarriers() as $carrierCode => $carrierModel) { + if (!$carrierModel->isActive()) { + continue; + } + + $carrierConfigKey = sprintf('carriers/%s/active', $carrierCode); + $configData[$carrierConfigKey] = 0; + $disabledShipmentMethods[] = $carrierCode; +} + +// Remember all manually disabled Shipping Methods for rollback +$configData[$rollbackConfigKey] = implode(',', $disabledShipmentMethods); + +/** @var Config $defConfig */ +$defConfig = $objectManager->create(Config::class); +$defConfig->setScope(ScopeConfigInterface::SCOPE_TYPE_DEFAULT); + +foreach ($configData as $key => $value) { + $defConfig->setDataByPath($key, $value); + $defConfig->save(); +} diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php new file mode 100644 index 0000000000000..a6d7a30e091b1 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php @@ -0,0 +1,33 @@ +create(WriterInterface::class); +$rollbackConfigValue = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class) + ->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID) + ->getConfig($rollbackConfigKey); + +$disabledShipmentMethods = []; +if (!empty($rollbackConfigValue)) { + $disabledShipmentMethods = explode(',', $rollbackConfigValue); +} + +if (count($disabledShipmentMethods)) { + foreach ($disabledShipmentMethods as $keyToRemove) { + $configWriter->delete(sprintf('carriers/%s/active', $keyToRemove)); + } +} +$configWriter->delete($rollbackConfigKey); + +$scopeConfig = $objectManager->get(ScopeConfigInterface::class); +$scopeConfig->clean(); From 10a220200eccec74bdb92a85d1dcea8d4d573031 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 13 Mar 2019 18:51:48 +0200 Subject: [PATCH 06/26] 428 - Test coverage: GetAvailableShippingMethodsTest Fix for PHP Fatal error: Uncaught Exception: Exception occurred when running the "\/home\/travis\/build\/magento\/graphql-ce\/dev\/tests\/integration\/testsuite\/Magento\/Sales\/_files\/guest_quote_with_addresses.php" fixture: URL key for specified store already exists. in /home/travis/build/magento/graphql-ce/dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiDataFixture.php:114 --- .../GetAvailableShippingMethodsTest.php | 37 ++++++++++--------- .../Guest/GetAvailableShippingMethodsTest.php | 37 ++++++++++--------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php index 9a8b3a254c435..336eaf4a0b3a6 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -3,7 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -declare(strict_types=1); +declare(strict_types = 1); namespace Magento\GraphQl\Quote\Customer; @@ -44,11 +44,11 @@ class GetAvailableShippingMethodsTest extends GraphQlAbstract */ protected function setUp() { - $objectManager = Bootstrap::getObjectManager(); - $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteResource = $objectManager->create(QuoteResource::class); + $objectManager = Bootstrap::getObjectManager(); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteResource = $objectManager->create(QuoteResource::class); $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); } /** @@ -59,7 +59,7 @@ protected function setUp() public function testGetAvailableShippingMethods() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); + $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId), [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response); self::assertArrayHasKey('shipping_addresses', $response['cart']); @@ -68,13 +68,13 @@ public function testGetAvailableShippingMethods() self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); $expectedAddressData = [ - 'amount' => 10, - 'base_amount' => 10, - 'carrier_code' => 'flatrate', - 'carrier_title' => 'Flat Rate', - 'error_message' => '', - 'method_code' => 'flatrate', - 'method_title' => 'Fixed', + 'amount' => 10, + 'base_amount' => 10, + 'carrier_code' => 'flatrate', + 'carrier_title' => 'Flat Rate', + 'error_message' => '', + 'method_code' => 'flatrate', + 'method_title' => 'Fixed', 'price_incl_tax' => 10, 'price_excl_tax' => 10, ]; @@ -98,7 +98,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap('customer2@search.example.com')); + $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId), [], '', $this->getHeaderMap('customer2@search.example.com')); } /** @@ -110,7 +110,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() public function testGetAvailableShippingMethodsIfShippingsAreNotSet() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); + $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId), [], '', $this->getHeaderMap()); self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); } @@ -125,7 +125,7 @@ public function testGetAvailableShippingMethodsIfShippingsAreNotSet() public function testGetAvailableShippingMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; - $query = $this->getQuery($maskedQuoteId); + $query = $this->getAvailableShippingMethodsQuery($maskedQuoteId); $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } @@ -134,9 +134,10 @@ public function testGetAvailableShippingMethodsOfNonExistentCart() * @param string $maskedQuoteId * @return string */ - private function getQuery( + private function getAvailableShippingMethodsQuery( string $maskedQuoteId - ): string { + ): string + { return <<quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteResource = $objectManager->create(QuoteResource::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $objectManager = Bootstrap::getObjectManager(); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteResource = $objectManager->create(QuoteResource::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); } /** @@ -52,7 +52,7 @@ protected function setUp() public function testGetAvailableShippingMethodsFromGuestCart() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); - $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); + $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); self::assertArrayHasKey('cart', $response); self::assertArrayHasKey('shipping_addresses', $response['cart']); @@ -61,13 +61,13 @@ public function testGetAvailableShippingMethodsFromGuestCart() self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); $expectedAddressData = [ - 'amount' => 5, - 'base_amount' => 5, - 'carrier_code' => 'flatrate', - 'carrier_title' => 'Flat Rate', - 'error_message' => '', - 'method_code' => 'flatrate', - 'method_title' => 'Fixed', + 'amount' => 5, + 'base_amount' => 5, + 'carrier_code' => 'flatrate', + 'carrier_title' => 'Flat Rate', + 'error_message' => '', + 'method_code' => 'flatrate', + 'method_title' => 'Fixed', 'price_incl_tax' => 5, 'price_excl_tax' => 5, ]; @@ -90,7 +90,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - $this->graphQlQuery($this->getQuery($maskedQuoteId)); + $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); } /** @@ -102,7 +102,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() public function testGetAvailableShippingMethodsIfShippingsAreNotSet() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); - $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); + $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); } @@ -117,16 +117,17 @@ public function testGetAvailableShippingMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; - $this->graphQlQuery($this->getQuery($maskedQuoteId)); + $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); } /** * @param string $maskedQuoteId * @return string */ - private function getQuery( + private function getAvailableShippingMethodsQuery( string $maskedQuoteId - ): string { + ): string + { return << Date: Thu, 14 Mar 2019 17:30:23 +0200 Subject: [PATCH 07/26] 428 - Test coverage: GetAvailableShippingMethodsTest --- .../GetAvailableShippingMethodsTest.php | 10 ++--- .../Guest/GetAvailableShippingMethodsTest.php | 2 +- .../disable_all_active_shipping_methods.php | 39 ------------------- ...e_all_active_shipping_methods_rollback.php | 33 ---------------- .../disable_offline_shipping_methods.php | 22 +++++++++++ ...able_offline_shipping_methods_rollback.php | 18 +++++++++ .../_files/guest_quote_with_addresses.php | 2 +- 7 files changed, 47 insertions(+), 79 deletions(-) delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php delete mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php create mode 100644 dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php index 336eaf4a0b3a6..c81d2d99f3514 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -68,15 +68,15 @@ public function testGetAvailableShippingMethods() self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); $expectedAddressData = [ - 'amount' => 10, - 'base_amount' => 10, + 'amount' => 0, + 'base_amount' => 0, 'carrier_code' => 'flatrate', 'carrier_title' => 'Flat Rate', 'error_message' => '', 'method_code' => 'flatrate', 'method_title' => 'Fixed', - 'price_incl_tax' => 10, - 'price_excl_tax' => 10, + 'price_incl_tax' => 0, + 'price_excl_tax' => 0, ]; self::assertEquals( $expectedAddressData, @@ -105,7 +105,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() * Test case: get available shipping methods when all shipping methods are disabled * * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - * @magentoApiDataFixture Magento/Checkout/_files/disable_all_active_shipping_methods.php + * @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php */ public function testGetAvailableShippingMethodsIfShippingsAreNotSet() { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php index 21ba48ee8a5ec..3b955fb53f6b1 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php @@ -97,7 +97,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() * Test case: get available shipping methods when all shipping methods are disabled * * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @magentoApiDataFixture Magento/Checkout/_files/disable_all_active_shipping_methods.php + * @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php */ public function testGetAvailableShippingMethodsIfShippingsAreNotSet() { diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php deleted file mode 100644 index 925a3bd6503aa..0000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods.php +++ /dev/null @@ -1,39 +0,0 @@ -get(Magento\Shipping\Model\Config::class); -$rollbackConfigKey = 'test/carriers/disabled_shipment_methods'; -$configData = []; -$disabledShipmentMethods = []; - -// Get all active Shipping Methods -foreach ($shippingConfig->getAllCarriers() as $carrierCode => $carrierModel) { - if (!$carrierModel->isActive()) { - continue; - } - - $carrierConfigKey = sprintf('carriers/%s/active', $carrierCode); - $configData[$carrierConfigKey] = 0; - $disabledShipmentMethods[] = $carrierCode; -} - -// Remember all manually disabled Shipping Methods for rollback -$configData[$rollbackConfigKey] = implode(',', $disabledShipmentMethods); - -/** @var Config $defConfig */ -$defConfig = $objectManager->create(Config::class); -$defConfig->setScope(ScopeConfigInterface::SCOPE_TYPE_DEFAULT); - -foreach ($configData as $key => $value) { - $defConfig->setDataByPath($key, $value); - $defConfig->save(); -} diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php deleted file mode 100644 index a6d7a30e091b1..0000000000000 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/disable_all_active_shipping_methods_rollback.php +++ /dev/null @@ -1,33 +0,0 @@ -create(WriterInterface::class); -$rollbackConfigValue = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class) - ->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID) - ->getConfig($rollbackConfigKey); - -$disabledShipmentMethods = []; -if (!empty($rollbackConfigValue)) { - $disabledShipmentMethods = explode(',', $rollbackConfigValue); -} - -if (count($disabledShipmentMethods)) { - foreach ($disabledShipmentMethods as $keyToRemove) { - $configWriter->delete(sprintf('carriers/%s/active', $keyToRemove)); - } -} -$configWriter->delete($rollbackConfigKey); - -$scopeConfig = $objectManager->get(ScopeConfigInterface::class); -$scopeConfig->clean(); diff --git a/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php b/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php new file mode 100644 index 0000000000000..30c64ab7a42a2 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php @@ -0,0 +1,22 @@ +get(WriterInterface::class); + +$configWriter->save('carriers/flatrate/active', 0); +$configWriter->save('carriers/tablerate/active', 0); +$configWriter->save('carriers/freeshipping/active', 0); + +$scopeConfig = $objectManager->get(ScopeConfigInterface::class); +$scopeConfig->clean(); diff --git a/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php b/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php new file mode 100644 index 0000000000000..31a16f42adfce --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php @@ -0,0 +1,18 @@ +create(WriterInterface::class); + +$configWriter->delete('carriers/flatrate/active'); +$configWriter->delete('carriers/tablerate/active'); +$configWriter->delete('carriers/freeshipping/active'); diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php b/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php index b8f2ca38e2489..f25cf8e927ca3 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php @@ -14,7 +14,7 @@ $product = $objectManager->create(\Magento\Catalog\Model\Product::class); $product->setTypeId('simple') ->setAttributeSetId($product->getDefaultAttributeSetId()) - ->setName('Simple Product') + ->setName('Simple Product For Guest Quote') ->setSku('simple-product-guest-quote') ->setPrice(10) ->setTaxClassId(0) From d2a7142d4dc2ce27fbd451c6c43dcf951f0d7f2d Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Fri, 15 Mar 2019 13:43:26 +0200 Subject: [PATCH 08/26] 428 - Test coverage: GetAvailableShippingMethodsTest Fix 1) Magento\GraphQl\Quote\Customer\GetAvailableShippingMethodsTest::testGetAvailableShippingMethods Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@ Array ( - 'amount' => 0 - 'base_amount' => 0 + 'amount' => 10 + 'base_amount' => 10 'carrier_code' => 'flatrate' 'carrier_title' => 'Flat Rate' 'error_message' => '' 'method_code' => 'flatrate' 'method_title' => 'Fixed' - 'price_incl_tax' => 0 - 'price_excl_tax' => 0 + 'price_incl_tax' => 10 + 'price_excl_tax' => 10 --- .../Quote/Customer/GetAvailableShippingMethodsTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php index c81d2d99f3514..da5801cb9a06e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -68,15 +68,15 @@ public function testGetAvailableShippingMethods() self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); $expectedAddressData = [ - 'amount' => 0, - 'base_amount' => 0, + 'amount' => 10, + 'base_amount' => 10, 'carrier_code' => 'flatrate', 'carrier_title' => 'Flat Rate', 'error_message' => '', 'method_code' => 'flatrate', 'method_title' => 'Fixed', - 'price_incl_tax' => 0, - 'price_excl_tax' => 0, + 'price_incl_tax' => 10, + 'price_excl_tax' => 10, ]; self::assertEquals( $expectedAddressData, From 60d40e1681c8e2e7121373df2dba38d9357ed12a Mon Sep 17 00:00:00 2001 From: Roman Rudavskyi Date: Sat, 16 Mar 2019 16:27:58 +0200 Subject: [PATCH 09/26] Add API Test For Selected Shipping Method functionality #478 --- .../Customer/SelectedShippingMethodTest.php | 223 ++++++++++++++++++ .../Guest/SelectedShippingMethodTest.php | 220 +++++++++++++++++ 2 files changed, 443 insertions(+) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SelectedShippingMethodTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SelectedShippingMethodTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SelectedShippingMethodTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SelectedShippingMethodTest.php new file mode 100644 index 0000000000000..a3e9188bb82f2 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SelectedShippingMethodTest.php @@ -0,0 +1,223 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + $this->filterBuilder = $this->objectManager->create( + \Magento\Framework\Api\FilterBuilder::class + ); + $this->sortOrderBuilder = $this->objectManager->create( + \Magento\Framework\Api\SortOrderBuilder::class + ); + $this->searchCriteriaBuilder = $this->objectManager->create( + \Magento\Framework\Api\SearchCriteriaBuilder::class + ); + $this->quoteResource = $this->objectManager->get( + \Magento\Quote\Model\ResourceModel\Quote::class + ); + $this->quoteFactory = $this->objectManager->get( + \Magento\Quote\Model\QuoteFactory::class + ); + $this->quoteIdToMaskedId = $this->objectManager->get( + \Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface::class + ); + $this->customerTokenService = $this->objectManager->get( + \Magento\Integration\Api\CustomerTokenServiceInterface::class + ); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetCartWithShippingMethod() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + self::assertInternalType( + 'array', + $response['cart']['shipping_addresses'][0]['selected_shipping_method'], + 'There are no selected shipping method for customer cart!' + ); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetShippingMethodFromCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + + $selectedShippingMethod = $this->getSelectedShippingMethod($response); + + self::assertEquals('flatrate', $selectedShippingMethod['carrier_code']); + self::assertEquals('flatrate', $selectedShippingMethod['method_code']); + self::assertEquals('Flat Rate - Fixed', $selectedShippingMethod['label']); + self::assertEquals(0, $selectedShippingMethod['amount']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php + */ + public function testGetShippingMethodIfShippingMethodIsNotSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $query = $this->getQuery($maskedQuoteId); + + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + + $selectedShippingMethod = $this->getSelectedShippingMethod($response); + + self::assertNull($selectedShippingMethod['carrier_code']); + self::assertNull($selectedShippingMethod['method_code']); + self::assertNull($selectedShippingMethod['label']); + self::assertNull($selectedShippingMethod['amount']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php + */ + public function testGetShippingMethodOfNonExistentCart() + { + $query = $this->getQuery('nonExistentCart'); + self::expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class); + self::expectExceptionMessage( + 'GraphQL response contains errors: Could not find a cart with ID "nonExistentCart"' + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * Retrieve quote by given reserved order ID + * + * @param string $reservedOrderId + * @return \Magento\Quote\Model\Quote + * @throws \InvalidArgumentException + */ + protected function getCart($reservedOrderId) + { + /** @var $cart \Magento\Quote\Model\Quote */ + $cart = $this->objectManager->get(\Magento\Quote\Model\Quote::class); + $cart->load($reservedOrderId, 'reserved_order_id'); + if (!$cart->getId()) { + throw new \InvalidArgumentException('There is no quote with provided reserved order ID.'); + } + return $cart; + } + + /** + * @param string $reservedOrderId + * @return string + */ + private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $username + * @param string $password + * @return array + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + return $headerMap; + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getQuery( + string $maskedQuoteId + ): string { + return <<objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + $this->filterBuilder = $this->objectManager->create( + \Magento\Framework\Api\FilterBuilder::class + ); + $this->sortOrderBuilder = $this->objectManager->create( + \Magento\Framework\Api\SortOrderBuilder::class + ); + $this->searchCriteriaBuilder = $this->objectManager->create( + \Magento\Framework\Api\SearchCriteriaBuilder::class + ); + $this->quoteResource = $this->objectManager->get( + \Magento\Quote\Model\ResourceModel\Quote::class + ); + $this->quoteFactory = $this->objectManager->get( + \Magento\Quote\Model\QuoteFactory::class + ); + $this->quoteIdToMaskedId = $this->objectManager->get( + \Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface::class + ); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetCartWithShippingMethod() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $this->unAssignCustomerFromQuote('test_order_1'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + self::assertInternalType( + 'array', + $response['cart']['shipping_addresses'][0]['selected_shipping_method'], + 'There are no selected shipping method for customer cart!' + ); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php + */ + public function testGetShippingMethodFromCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $this->unAssignCustomerFromQuote('test_order_1'); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + + $selectedShippingMethod = $this->getSelectedShippingMethod($response); + + self::assertEquals('flatrate', $selectedShippingMethod['carrier_code']); + self::assertEquals('flatrate', $selectedShippingMethod['method_code']); + self::assertEquals('Flat Rate - Fixed', $selectedShippingMethod['label']); + self::assertEquals(0, $selectedShippingMethod['amount']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php + */ + public function testGetShippingMethodIfShippingMethodIsNotSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $query = $this->getQuery($maskedQuoteId); + $this->unAssignCustomerFromQuote('test_order_1'); + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + + $selectedShippingMethod = $this->getSelectedShippingMethod($response); + + self::assertNull($selectedShippingMethod['carrier_code']); + self::assertNull($selectedShippingMethod['method_code']); + self::assertNull($selectedShippingMethod['label']); + self::assertNull($selectedShippingMethod['amount']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php + */ + public function testGetShippingMethodOfNonExistentCart() + { + $query = $this->getQuery('nonExistentCart'); + self::expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class); + self::expectExceptionMessage( + 'GraphQL response contains errors: Could not find a cart with ID "nonExistentCart"' + ); + $this->graphQlQuery($query); + } + + /** + * Retrieve quote by given reserved order ID + * + * @param string $reservedOrderId + * @return \Magento\Quote\Model\Quote + * @throws \InvalidArgumentException + */ + protected function getCart($reservedOrderId) + { + /** @var $cart \Magento\Quote\Model\Quote */ + $cart = $this->objectManager->get(\Magento\Quote\Model\Quote::class); + $cart->load($reservedOrderId, 'reserved_order_id'); + if (!$cart->getId()) { + throw new \InvalidArgumentException('There is no quote with provided reserved order ID.'); + } + return $cart; + } + + /** + * @param string $reservedOrderId + * @return string + */ + private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getQuery( + string $maskedQuoteId + ): string { + return <<quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + $quote->setCustomerId(0); + $this->quoteResource->save($quote); + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} From 5d9a64e4ffffb037523233ffea3da941c83fa90f Mon Sep 17 00:00:00 2001 From: Volodymyr Vygovskyi Date: Sat, 16 Mar 2019 23:17:02 +0200 Subject: [PATCH 10/26] issue #485 [Test coverage] testReSetShippingMethod --- .../Customer/SetShippingMethodsOnCartTest.php | 51 +++++++++++-- .../Guest/SetShippingMethodsOnCartTest.php | 72 +++++++++++++++++-- 2 files changed, 111 insertions(+), 12 deletions(-) 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 From 2efa3088925324ecd3c3e24951d2dfb8aa8a46d5 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Wed, 20 Mar 2019 15:18:47 +0200 Subject: [PATCH 11/26] Resolve random fails on Bamboo with \Magento\GraphQl\Quote\CouponTest::testApplyCouponToCartWithNoItems --- .../testsuite/Magento/GraphQl/Quote/CouponTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php index 54fdc3ac0f360..ce409bbcfb0a8 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php @@ -92,14 +92,16 @@ public function testApplyCouponTwice() */ public function testApplyCouponToCartWithNoItems() { - $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/191'); $couponCode = '2?ds5!2d'; $this->quoteResource->load($this->quote, 'test_order_1', 'reserved_order_id'); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $cartId = (int)$this->quote->getId(); + $maskedQuoteId = $this->quoteIdToMaskedId->execute($cartId); $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); - self::expectExceptionMessageRegExp('/Cart doesn\'t contain products/'); + self::expectExceptionMessage( + str_replace('$cartId', $cartId, 'The "$cartId" Cart doesn\'t contain products.') + ); $this->graphQlQuery($query); } From 99134c0a1ee7161177ca65889e85b62d2b5f24f8 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Mon, 25 Mar 2019 12:16:58 -0500 Subject: [PATCH 12/26] GraphQL-428: Test coverage: GetAvailableShippingMethodsTest --- .../GetAvailablePaymentMethodsTest.php | 10 ++++----- .../GetAvailableShippingMethodsTest.php | 21 ++++++++++++------- .../Guest/GetAvailablePaymentMethodsTest.php | 8 +++---- .../Guest/GetAvailableShippingMethodsTest.php | 20 +++++++++--------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php index a7287ada093b8..00627cb893103 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php @@ -54,7 +54,7 @@ protected function setUp() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php */ - public function testGetCartWithPaymentMethods() + public function testGetAvailablePaymentMethods() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_item_with_items'); $query = $this->getQuery($maskedQuoteId); @@ -74,7 +74,7 @@ public function testGetCartWithPaymentMethods() * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php */ - public function testGetPaymentMethodsFromGuestCart() + public function testGetAvailablePaymentMethodsFromGuestCart() { $guestQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId( 'test_order_with_virtual_product_without_address' @@ -91,7 +91,7 @@ public function testGetPaymentMethodsFromGuestCart() * @magentoApiDataFixture Magento/Customer/_files/three_customers.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php */ - public function testGetPaymentMethodsFromAnotherCustomerCart() + public function testGetAvailablePaymentMethodsFromAnotherCustomerCart() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_item_with_items'); $query = $this->getQuery($maskedQuoteId); @@ -106,7 +106,7 @@ public function testGetPaymentMethodsFromAnotherCustomerCart() * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php * @magentoApiDataFixture Magento/Payment/_files/disable_all_active_payment_methods.php */ - public function testGetPaymentMethodsIfPaymentsAreNotSet() + public function testGetAvailablePaymentMethodsIfAllPaymentsAreDisabled() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_item_with_items'); $query = $this->getQuery($maskedQuoteId); @@ -120,7 +120,7 @@ public function testGetPaymentMethodsIfPaymentsAreNotSet() * @expectedException \Exception * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" */ - public function testGetPaymentMethodsOfNonExistentCart() + public function testGetAvailablePaymentMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; $query = $this->getQuery($maskedQuoteId); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php index da5801cb9a06e..6f5a9b5d99cbf 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -46,9 +46,9 @@ protected function setUp() { $objectManager = Bootstrap::getObjectManager(); $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteResource = $objectManager->create(QuoteResource::class); + $this->quoteResource = $objectManager->get(QuoteResource::class); $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); } /** @@ -59,7 +59,7 @@ protected function setUp() public function testGetAvailableShippingMethods() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId), [], '', $this->getHeaderMap()); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response); self::assertArrayHasKey('shipping_addresses', $response['cart']); @@ -98,7 +98,12 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId), [], '', $this->getHeaderMap('customer2@search.example.com')); + $this->graphQlQuery( + $this->getQuery($maskedQuoteId), + [], + '', + $this->getHeaderMap('customer2@search.example.com') + ); } /** @@ -107,10 +112,10 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php * @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php */ - public function testGetAvailableShippingMethodsIfShippingsAreNotSet() + public function testGetAvailableShippingMethodsIfAllShippingsAreDisabled() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId), [], '', $this->getHeaderMap()); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); } @@ -125,7 +130,7 @@ public function testGetAvailableShippingMethodsIfShippingsAreNotSet() public function testGetAvailableShippingMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; - $query = $this->getAvailableShippingMethodsQuery($maskedQuoteId); + $query = $this->getQuery($maskedQuoteId); $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } @@ -134,7 +139,7 @@ public function testGetAvailableShippingMethodsOfNonExistentCart() * @param string $maskedQuoteId * @return string */ - private function getAvailableShippingMethodsQuery( + private function getQuery( string $maskedQuoteId ): string { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php index c4dd9af490c99..d083e8a859e9f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php @@ -47,7 +47,7 @@ protected function setUp() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php */ - public function testGetCartWithPaymentMethods() + public function testGetAvailablePaymentMethods() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_with_simple_product_without_address'); $query = $this->getQuery($maskedQuoteId); @@ -73,7 +73,7 @@ public function testGetCartWithPaymentMethods() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php */ - public function testGetPaymentMethodsFromCustomerCart() + public function testGetAvailablePaymentMethodsFromCustomerCart() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); $query = $this->getQuery($maskedQuoteId); @@ -88,7 +88,7 @@ public function testGetPaymentMethodsFromCustomerCart() * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * @magentoApiDataFixture Magento/Payment/_files/disable_all_active_payment_methods.php */ - public function testGetPaymentMethodsIfPaymentsAreNotSet() + public function testGetPaymentMethodsIfPaymentsAreDisabled() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_with_simple_product_without_address'); $query = $this->getQuery($maskedQuoteId); @@ -101,7 +101,7 @@ public function testGetPaymentMethodsIfPaymentsAreNotSet() * @expectedException \Exception * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" */ - public function testGetPaymentMethodsOfNonExistentCart() + public function testGetAvailablePaymentMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; $query = $this->getQuery($maskedQuoteId); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php index 3b955fb53f6b1..c9f8f37669e5d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php @@ -40,8 +40,8 @@ protected function setUp() { $objectManager = Bootstrap::getObjectManager(); $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteResource = $objectManager->create(QuoteResource::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); } /** @@ -49,10 +49,10 @@ protected function setUp() * * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php */ - public function testGetAvailableShippingMethodsFromGuestCart() + public function testGetAvailableShippingMethods() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); - $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); self::assertArrayHasKey('cart', $response); self::assertArrayHasKey('shipping_addresses', $response['cart']); @@ -82,7 +82,7 @@ public function testGetAvailableShippingMethodsFromGuestCart() * * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php */ - public function testGetAvailableShippingMethodsFromAnotherCustomerCart() + public function testGetAvailableShippingMethodsFromCustomerCart() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); @@ -90,7 +90,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); + $this->graphQlQuery($this->getQuery($maskedQuoteId)); } /** @@ -99,10 +99,10 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php * @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php */ - public function testGetAvailableShippingMethodsIfShippingsAreNotSet() + public function testGetAvailableShippingMethodsIfShippingsAreDisabled() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); - $response = $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); } @@ -117,14 +117,14 @@ public function testGetAvailableShippingMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; - $this->graphQlQuery($this->getAvailableShippingMethodsQuery($maskedQuoteId)); + $this->graphQlQuery($this->getQuery($maskedQuoteId)); } /** * @param string $maskedQuoteId * @return string */ - private function getAvailableShippingMethodsQuery( + private function getQuery( string $maskedQuoteId ): string { From 8136c3816dfa47b1f15d7ae61b8f75b9074fb02e Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Mon, 25 Mar 2019 20:30:19 +0200 Subject: [PATCH 13/26] 428 - Test coverage: GetAvailableShippingMethodsTest 1. Rollback fixture changes --- .../Magento/Sales/_files/guest_quote_with_addresses.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php b/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php index f25cf8e927ca3..b8f2ca38e2489 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php @@ -14,7 +14,7 @@ $product = $objectManager->create(\Magento\Catalog\Model\Product::class); $product->setTypeId('simple') ->setAttributeSetId($product->getDefaultAttributeSetId()) - ->setName('Simple Product For Guest Quote') + ->setName('Simple Product') ->setSku('simple-product-guest-quote') ->setPrice(10) ->setTaxClassId(0) From 0b8bebc0698d8433d62e620b8d4ee111ec21797a Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Mon, 25 Mar 2019 15:47:31 -0500 Subject: [PATCH 14/26] GraphQL-428: Test coverage: GetAvailableShippingMethodsTest --- .../Quote/Customer/GetAvailableShippingMethodsTest.php | 4 +--- .../GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php index 6f5a9b5d99cbf..5ca3307371d2f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -139,9 +139,7 @@ public function testGetAvailableShippingMethodsOfNonExistentCart() * @param string $maskedQuoteId * @return string */ - private function getQuery( - string $maskedQuoteId - ): string + private function getQuery(string $maskedQuoteId): string { return << Date: Tue, 26 Mar 2019 12:16:55 +0530 Subject: [PATCH 15/26] Removed duplicate case & applied logic fixes --- .../Guest/SetShippingMethodsOnCartTest.php | 75 +++++++------------ 1 file changed, 27 insertions(+), 48 deletions(-) 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 f6444027d0ca3..eadc131ee464a 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 @@ -71,21 +71,25 @@ public function testShippingMethodWithVirtualProduct() ); self::expectException(\Exception::class); - self::expectExceptionMessage('Carrier with such method not found: ' . $methodCode . ', ' . $methodCode . ''); + self::expectExceptionMessage('You can\'t set shipping methods for virtual products'); $this->graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php */ public function testShippingMethodWithSimpleProduct() { $methodCode = 'flatrate'; $carrierCode = 'flatrate'; - $reservedOrderId = 'test_order_with_simple_product_without_address'; + $reservedOrderId = 'guest_quote'; $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - $quoteAddressId = $this->getQuoteAddressIdByReversedQuoteId($reservedOrderId); + + /** @var Quote $quote */ + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); + $quoteAddressId = (int) $quote->getShippingAddress()->getId(); $query = $this->prepareMutationQuery( $maskedQuoteId, @@ -109,27 +113,38 @@ public function testShippingMethodWithSimpleProduct() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * @expectedException \Exception - * @expectedExceptionMessage Required parameter "cart_address_id" is missing. */ - public function testShippingMethodWithSimpleProductWithoutAddress() + public function testShippingMethodWithSimpleProductWithoutAddress(int $addressId, string $message) { $methodCode = 'flatrate'; $carrierCode = 'flatrate'; $reservedOrderId = 'test_order_with_simple_product_without_address'; $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - $quoteAddressId = 0; - $query = $this->prepareMutationQuery( $maskedQuoteId, $methodCode, $carrierCode, - $quoteAddressId + $addressId ); + $this->expectExceptionMessage($message); $this->graphQlQuery($query); } + /** + * @return array + */ + public function dataProviderWithMissedRequiredParameters() + { + return [ + 'shipping_methods' => [ + 0, + 'Required parameter "cart_address_id" is missing.', + ] + ]; + } + /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * @expectedException \Exception @@ -213,16 +228,15 @@ public function testSetShippingMethodIfAddressIsNotBelongToCart() /** * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @expectedException \Magento\Framework\Exception\NoSuchEntityException - * @expectedExceptionMessage No such entity with cartId = 0 + * @expectedException \Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" */ public function testSetShippingMethodToNonExistentCart() { $methodCode = 'flatrate'; $carrierCode = 'flatrate'; - $reservedOrderId = 'non_existent_cart_reversed_quote_id'; - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $maskedQuoteId = 'non_existent_masked_id'; $quoteAddressId = 1; $query = $this->prepareMutationQuery( @@ -235,41 +249,6 @@ public function testSetShippingMethodToNonExistentCart() $this->graphQlQuery($query); } - /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - */ - public function testSetShippingMethodToGuestCart() - { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $quoteAddressId - ); - - $response = $this->graphQlQuery($query); - - self::assertArrayHasKey('setShippingMethodsOnCart', $response); - self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); - self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); - - $shippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - self::assertArrayHasKey('selected_shipping_method', $shippingMethod); - self::assertEquals($carrierCode, $shippingMethod['selected_shipping_method']['carrier_code']); - self::assertEquals($methodCode, $shippingMethod['selected_shipping_method']['method_code']); - } - /** * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php From c2f15b388db0ba654920289f32a3e3e0def072e2 Mon Sep 17 00:00:00 2001 From: Yogesh Suhagiya Date: Tue, 26 Mar 2019 12:53:33 +0530 Subject: [PATCH 16/26] Added data Provider --- .../GraphQl/Quote/Guest/SetShippingMethodsOnCartTest.php | 3 +++ 1 file changed, 3 insertions(+) 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 eadc131ee464a..2fc919fd86689 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 @@ -112,6 +112,9 @@ public function testShippingMethodWithSimpleProduct() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @dataProvider dataProviderWithMissedRequiredParameters + * @param int $addressId + * @param string $message * @expectedException \Exception */ public function testShippingMethodWithSimpleProductWithoutAddress(int $addressId, string $message) From 38dd5a03bdfe3c3aa600afcdf2518d7f0a3c6996 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Tue, 26 Mar 2019 09:36:47 +0200 Subject: [PATCH 17/26] 428 - Test coverage: GetAvailableShippingMethodsTest 1. Fix for "URL key for specified store already exists" --- .../_files/quote_with_simple_product_saved_rollback.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php index 105a981ccfc84..2e333bbcb3049 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php @@ -1,11 +1,13 @@ create(\Magento\Quote\Model\Quote::class); From 18e41ba22fcd5126c7d1731781538f65ce997ab6 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Tue, 26 Mar 2019 12:07:27 +0200 Subject: [PATCH 18/26] 428 - Test coverage: GetAvailableShippingMethodsTest --- .../_files/simple_product_rollback.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php new file mode 100644 index 0000000000000..43beafe822043 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php @@ -0,0 +1,26 @@ +get(\Magento\Framework\Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + +try { + $product = $productRepository->get('simple', false, null, true); + $productRepository->delete($product); +} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { + //Product already removed +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); From 63cbf8b0e47617c8fb3f381d7a7e5114f77eea35 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Tue, 26 Mar 2019 12:11:43 +0200 Subject: [PATCH 19/26] Resolve random fails on Bamboo with \Magento\GraphQl\Quote\CouponTest::testApplyCouponToCartWithNoItems --- .../testsuite/Magento/GraphQl/Quote/CouponTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php index ce409bbcfb0a8..810fc793fb1e2 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php @@ -100,7 +100,7 @@ public function testApplyCouponToCartWithNoItems() $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); self::expectExceptionMessage( - str_replace('$cartId', $cartId, 'The "$cartId" Cart doesn\'t contain products.') + 'The "' . $cartId . '" Cart doesn\'t contain products.' ); $this->graphQlQuery($query); } From 829a39b3578f2236e8fe5645822a57afe42a5666 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 11:25:43 -0500 Subject: [PATCH 20/26] GraphQL-422: Test coverage: SetShippingMethodsOnCartTest for Guest --- .../Model/Cart/GetQuoteAddress.php | 21 +- .../Model/Cart/SetShippingMethodsOnCart.php | 3 +- .../Customer/SetBillingAddressOnCartTest.php | 18 +- .../Customer/SetPaymentMethodOnCartTest.php | 12 +- .../GetMaskedQuoteIdByReservedOrderId.php | 3 +- ...uoteShippingAddressIdByReservedQuoteId.php | 53 ++ .../Guest/SetBillingAddressOnCartTest.php | 29 +- .../Guest/SetPaymentMethodOnCartTest.php | 12 +- .../Guest/SetShippingMethodsOnCartTest.php | 610 +++++++----------- ...h_virtual_product_and_address_rollback.php | 2 +- .../_files/simple_product_rollback.php | 2 +- .../Quote/_files/guest/quote_with_address.php | 49 ++ .../guest/quote_with_address_rollback.php | 28 + .../_files/set_flatrate_shipping_method.php | 36 ++ 14 files changed, 474 insertions(+), 404 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetQuoteShippingAddressIdByReservedQuoteId.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php b/app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php index 1fb737d964139..89124c594dd87 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php @@ -8,10 +8,10 @@ namespace Magento\QuoteGraphQl\Model\Cart; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; -use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Quote\Api\Data\AddressInterface; use Magento\Quote\Api\Data\AddressInterfaceFactory; +use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource; /** @@ -44,14 +44,14 @@ public function __construct( /** * Get quote address * + * @param CartInterface $cart * @param int $quoteAddressId * @param int|null $customerId * @return AddressInterface - * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException * @throws GraphQlAuthorizationException + * @throws GraphQlNoSuchEntityException */ - public function execute(int $quoteAddressId, ?int $customerId): AddressInterface + public function execute(CartInterface $cart, int $quoteAddressId, ?int $customerId): AddressInterface { $quoteAddress = $this->quoteAddressFactory->create(); @@ -62,14 +62,15 @@ public function execute(int $quoteAddressId, ?int $customerId): AddressInterface ); } - $quoteAddressCustomerId = (int)$quoteAddress->getCustomerId(); - - /* Guest cart, allow operations */ - if (!$quoteAddressCustomerId && null === $customerId) { - return $quoteAddress; + // TODO: GetQuoteAddress::execute should depend only on AddressInterface contract + // https://github.com/magento/graphql-ce/issues/550 + if ($quoteAddress->getQuoteId() !== $cart->getId()) { + throw new GraphQlNoSuchEntityException( + __('Cart does not contain address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId]) + ); } - if ($quoteAddressCustomerId !== $customerId) { + if ((int)$quoteAddress->getCustomerId() !== (int)$customerId) { throw new GraphQlAuthorizationException( __( 'The current user cannot use cart address with ID "%cart_address_id"', diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php index 37e0118423745..4104e179160d9 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php @@ -69,8 +69,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s } $methodCode = $shippingMethodInput['method_code']; - $quoteAddress = $this->getQuoteAddress->execute($cartAddressId, $context->getUserId()); - + $quoteAddress = $this->getQuoteAddress->execute($cart, $cartAddressId, $context->getUserId()); $this->assignShippingMethodToCart->execute($cart, $quoteAddress, $carrierCode, $methodCode); } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php index 129375debe068..88e7b93dd1d08 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php @@ -429,6 +429,7 @@ public function testSetBillingAddressIfCustomerIsNotOwnerOfAddress() /** * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Customer/_files/customer_address.php * @expectedException \Exception * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" */ @@ -436,10 +437,19 @@ public function testSetBillingAddressOnNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; $query = <<getQuery($maskedQuoteId, $methodCode); $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetMaskedQuoteIdByReservedOrderId.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetMaskedQuoteIdByReservedOrderId.php index c5a4e8af02a58..eab362c3a0a6f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetMaskedQuoteIdByReservedOrderId.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetMaskedQuoteIdByReservedOrderId.php @@ -7,6 +7,7 @@ namespace Magento\GraphQl\Quote; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\QuoteFactory; @@ -51,7 +52,7 @@ public function __construct( * * @param string $reversedOrderId * @return string - * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws NoSuchEntityException */ public function execute(string $reversedOrderId): string { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetQuoteShippingAddressIdByReservedQuoteId.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetQuoteShippingAddressIdByReservedQuoteId.php new file mode 100644 index 0000000000000..fa42ad4d71fb2 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetQuoteShippingAddressIdByReservedQuoteId.php @@ -0,0 +1,53 @@ +quoteFactory = $quoteFactory; + $this->quoteResource = $quoteResource; + } + + /** + * Get quote shipping address id by reserved order id + * + * @param string $reversedOrderId + * @return int + */ + public function execute(string $reversedOrderId): int + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedOrderId, 'reserved_order_id'); + + return (int)$quote->getShippingAddress()->getId(); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php index 3808ce38b9d7b..ae0a1a0e822ac 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php @@ -248,10 +248,30 @@ public function testSetBillingAddressOnNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; $query = <<expectExceptionMessage($message); $this->graphQlQuery($query); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php index 8f37f00c3db7f..c9078fd84f6bc 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php @@ -113,15 +113,9 @@ public function testSetNonExistentPaymentMethod() public function testSetPaymentOnNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; - $query = <<getQuery($maskedQuoteId, $methodCode); $this->graphQlQuery($query); } 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 07ccfc7bf727e..6289d88de6ee4 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,12 +7,10 @@ namespace Magento\GraphQl\Quote\Guest; -use Magento\Quote\Model\QuoteFactory; -use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; -use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; +use Magento\GraphQl\Quote\GetQuoteShippingAddressIdByReservedQuoteId; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; -use Magento\Quote\Model\Quote\Address as QuoteAddress; /** * Test for setting shipping methods on cart for guest @@ -20,24 +18,14 @@ class SetShippingMethodsOnCartTest extends GraphQlAbstract { /** - * @var QuoteResource + * @var GetMaskedQuoteIdByReservedOrderId */ - private $quoteResource; + private $getMaskedQuoteIdByReservedOrderId; /** - * @var QuoteFactory + * @var GetQuoteShippingAddressIdByReservedQuoteId */ - private $quoteFactory; - - /** - * @var QuoteIdToMaskedQuoteIdInterface - */ - private $quoteIdToMaskedId; - - /** - * @var QuoteAddress - */ - private $quoteAddress; + private $getQuoteShippingAddressIdByReservedQuoteId; /** * @inheritdoc @@ -45,410 +33,355 @@ class SetShippingMethodsOnCartTest extends GraphQlAbstract protected function setUp() { $objectManager = Bootstrap::getObjectManager(); - $this->quoteResource = $objectManager->get(QuoteResource::class); - $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); - $this->quoteAddress = $objectManager->get(QuoteAddress::class); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php - */ - public function testShippingMethodWithVirtualProduct() - { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'test_order_with_virtual_product_without_address'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - $quoteAddressId = $this->getQuoteAddressIdByReversedQuoteId($reservedOrderId); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $quoteAddressId + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); + $this->getQuoteShippingAddressIdByReservedQuoteId = $objectManager->get( + GetQuoteShippingAddressIdByReservedQuoteId::class ); - - self::expectException(\Exception::class); - self::expectExceptionMessage('You can\'t set shipping methods for virtual products'); - $this->graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php */ - public function testShippingMethodWithSimpleProduct() + public function testSetShippingMethodOnCartWithSimpleProduct() { - $methodCode = 'flatrate'; + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + $methodCode = 'flatrate'; + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); - $query = $this->prepareMutationQuery( + $query = $this->getQuery( $maskedQuoteId, $methodCode, $carrierCode, $quoteAddressId ); - $response = $this->graphQlQuery($query); self::assertArrayHasKey('setShippingMethodsOnCart', $response); self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - $shippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - self::assertArrayHasKey('selected_shipping_method', $shippingMethod); - self::assertEquals($carrierCode, $shippingMethod['selected_shipping_method']['carrier_code']); - self::assertEquals($methodCode, $shippingMethod['selected_shipping_method']['method_code']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php - * @dataProvider dataProviderWithMissedRequiredParameters - * @param int $addressId - * @param string $message - * @expectedException \Exception - */ - public function testShippingMethodWithSimpleProductWithoutAddress(int $addressId, string $message) - { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'test_order_with_simple_product_without_address'; + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $addressId - ); + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); + self::assertEquals($carrierCode, $shippingAddress['selected_shipping_method']['carrier_code']); - $this->expectExceptionMessage($message); - $this->graphQlQuery($query); + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); } /** - * @return array - */ - public function dataProviderWithMissedRequiredParameters() - { - return [ - 'shipping_methods' => [ - 0, - 'Required parameter "cart_address_id" is missing.', - ] - ]; - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * * @expectedException \Exception - * @expectedExceptionMessage Required parameter "method_code" is missing. + * @expectedExceptionMessage The shipping address is missing. Set the address and try again. */ - public function testSetShippingMethodWithMissedRequiredParameters() + public function testSetShippingMethodOnCartWithSimpleProductAndWithoutAddress() { - $methodCode = ''; + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $carrierCode = 'flatrate'; - $reservedOrderId = 'test_order_with_simple_product_without_address'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - $quoteAddressId = $this->getQuoteAddressIdByReversedQuoteId($reservedOrderId); + $methodCode = 'flatrate'; + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); - $query = $this->prepareMutationQuery( + $query = $this->getQuery( $maskedQuoteId, $methodCode, $carrierCode, $quoteAddressId ); - $this->graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php */ - public function testSetNonExistentShippingMethod() + public function testReSetShippingMethod() { - $methodCode = 'non-existed-method-code'; - $carrierCode = 'non-carrier-method-code'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, 'guest_quote', 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $carrierCode = 'freeshipping'; + $methodCode = 'freeshipping'; + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); - $query = $this->prepareMutationQuery( + $query = $this->getQuery( $maskedQuoteId, $methodCode, $carrierCode, $quoteAddressId ); + $response = $this->graphQlQuery($query); - self::expectException(\Exception::class); - self::expectExceptionMessage('Carrier with such method not found: ' . $carrierCode . ', ' . $methodCode . ''); - $this->graphQlQuery($query); - } - - /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php - */ - public function testSetShippingMethodIfAddressIsNotBelongToCart() - { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; + self::assertArrayHasKey('setShippingMethodsOnCart', $response); + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, 'test_order_with_virtual_product', 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); + self::assertEquals($carrierCode, $shippingAddress['selected_shipping_method']['carrier_code']); - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $quoteAddressId - ); - - self::expectException(\Exception::class); - self::expectExceptionMessage('The current user cannot use cart address with ID "' . $quoteAddressId . '"'); - $this->graphQlQuery($query); + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); } /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @expectedException \Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException - * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * + * @param string $input + * @param string $message + * @dataProvider dataProviderSetShippingMethodWithWrongParameters + * @throws \Exception */ - public function testSetShippingMethodToNonExistentCart() + public function testSetShippingMethodWithWrongParameters(string $input, string $message) { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - - $maskedQuoteId = 'non_existent_masked_id'; - $quoteAddressId = 1; - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $quoteAddressId - ); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); + $input = str_replace(['cart_id_value', 'cart_address_id_value'], [$maskedQuoteId, $quoteAddressId], $input); + $query = <<expectExceptionMessage($message); $this->graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + * @return array + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function testSetShippingMethodToAnotherCustomerCart() + public function dataProviderSetShippingMethodWithWrongParameters(): array { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_virtual_product'); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $quoteAddressId - ); - - self::expectException(\Exception::class); - self::expectExceptionMessage( - 'The current user cannot perform operations on cart "' . $maskedQuoteId . '"' - ); - $this->graphQlQuery($query); + return [ + 'missed_cart_id' => [ + 'shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "flatrate" + method_code: "flatrate" + }]', + 'Required parameter "cart_id" is missing' + ], + 'missed_shipping_methods' => [ + 'cart_id: "cart_id_value"', + 'Required parameter "shipping_methods" is missing' + ], + 'shipping_methods_are_empty' => [ + 'cart_id: "cart_id_value" shipping_methods: []', + 'Required parameter "shipping_methods" is missing' + ], + 'missed_cart_address_id' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + carrier_code: "flatrate" + method_code: "flatrate" + }]', + 'Required parameter "cart_address_id" is missing.' + ], + 'non_existent_cart_address_id' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: -1 + carrier_code: "flatrate" + method_code: "flatrate" + }]', + 'Could not find a cart address with ID "-1"' + ], + 'missed_carrier_code' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: cart_address_id_value + method_code: "flatrate" + }]', + 'Field ShippingMethodInput.carrier_code of required type String! was not provided.' + ], + 'empty_carrier_code' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "" + method_code: "flatrate" + }]', + 'Required parameter "carrier_code" is missing.' + ], + 'non_existent_carrier_code' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "wrong-carrier-code" + method_code: "flatrate" + }]', + 'Carrier with such method not found: wrong-carrier-code, flatrate' + ], + 'missed_method_code' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "flatrate" + }]', + 'Required parameter "method_code" is missing.' + ], + 'empty_method_code' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "flatrate" + method_code: "" + }]', + 'Required parameter "method_code" is missing.' + ], + 'non_existent_method_code' => [ + 'cart_id: "cart_id_value", shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "flatrate" + method_code: "wrong-carrier-code" + }]', + 'Carrier with such method not found: flatrate, wrong-carrier-code' + ], + 'non_existent_shopping_cart' => [ + 'cart_id: "non_existent_masked_id", shipping_methods: [{ + cart_address_id: cart_address_id_value + carrier_code: "flatrate" + method_code: "flatrate" + }]', + 'Could not find a cart with ID "non_existent_masked_id"' + ], + ]; } /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @expectedException \Exception + * @expectedExceptionMessage You cannot specify multiple shipping methods. */ - public function testSetShippingMethodToNonExistentCartAddress() + public function testSetMultipleShippingMethods() { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $quoteAddressId = 1963425585; - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode, - $carrierCode, - $quoteAddressId - ); - - self::expectException(\Exception::class); - self::expectExceptionMessage( - 'Could not find a cart address with ID "' . $quoteAddressId . '"' - ); + $query = <<graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * + * @expectedException \Exception */ - public function testSetShippingMethodToGuestCartAddress() + public function testSetShippingMethodToCustomerCart() { - $methodCode = 'flatrate'; + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); - - $query = $this->prepareMutationQuery( + $methodCode = 'flatrate'; + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); + $query = $this->getQuery( $maskedQuoteId, $methodCode, $carrierCode, $quoteAddressId ); - $response = $this->graphQlQuery($query); - - self::assertArrayHasKey('setShippingMethodsOnCart', $response); - self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); - self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); - - $shippingMethod = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); - self::assertArrayHasKey('selected_shipping_method', $shippingMethod); - self::assertEquals($carrierCode, $shippingMethod['selected_shipping_method']['carrier_code']); - self::assertEquals($methodCode, $shippingMethod['selected_shipping_method']['method_code']); + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * _security + * + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/quote_with_address.php */ - public function testSetShippingMethodToAnotherCustomerCartAddress() + public function testSetShippingMethodIfGuestIsNotOwnerOfAddress() { - $methodCode = 'flatrate'; + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $carrierCode = 'flatrate'; - $reservedOrderId = 'test_order_1'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $quoteAddressId = (int) $quote->getShippingAddress()->getId(); - - $query = $this->prepareMutationQuery( + $methodCode = 'flatrate'; + $anotherQuoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('guest_quote_with_address'); + $query = $this->getQuery( $maskedQuoteId, $methodCode, $carrierCode, - $quoteAddressId + $anotherQuoteAddressId ); - self::expectException(\Exception::class); - self::expectExceptionMessage( - 'The current user cannot perform operations on cart "' . $maskedQuoteId . '"' + $this->expectExceptionMessage( + "Cart does not contain address with ID \"{$anotherQuoteAddressId}\"" ); $this->graphQlQuery($query); } - /** - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php - * @expectedException \Exception - * @expectedExceptionMessage You cannot specify multiple shipping methods. - */ - public function testSetMultipleShippingMethods() - { - $methodCode = 'flatrate'; - $carrierCode = 'flatrate'; - $reservedOrderId = 'guest_quote'; - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId($reservedOrderId); - - /** @var Quote $quote */ - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - $shippingAddressId = (int) $quote->getShippingAddress()->getId(); - - $query = <<graphQlQuery($query); - } - /** * @param string $maskedQuoteId * @param string $shippingMethodCode * @param string $shippingCarrierCode * @param int $shippingAddressId * @return string - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function prepareMutationQuery( + private function getQuery( string $maskedQuoteId, string $shippingMethodCode, string $shippingCarrierCode, int $shippingAddressId - ) : string { + ): string { return <<quoteFactory->create(); - $this->quoteResource->load($quote, $reversedOrderId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } - - /** - * @param string $reversedQuoteId - * @return int - */ - private function getQuoteAddressIdByReversedQuoteId(string $reversedQuoteId): int - { - $guestAddress = $this->quoteAddress->setData([ - 'firstname'=> 'John', - 'lastname'=> 'Smith', - 'company'=> 'Company Name', - 'street'=> 'Green str, 67', - 'city'=> 'CityM', - 'region' => 'AL', - 'postcode'=> 75477, - 'telephone'=> 3468676, - 'country_id'=> 'US', - 'region_id' => 1 - ]); - - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); - - $quote->setBillingAddress($guestAddress); - $quote->setShippingAddress($guestAddress); - $quote->collectTotals(); - $this->quoteResource->save($quote); - - return (int) $quote->getShippingAddress()->getId(); - } } diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php index 52ce4b4006db8..402ad030ed857 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_virtual_product_and_address_rollback.php @@ -16,4 +16,4 @@ require __DIR__ . '/../../Customer/_files/customer_rollback.php'; require __DIR__ . '/../../Customer/_files/customer_address_rollback.php'; -require __DIR__ . '/../../../Magento/Catalog/_files/product_virtual_rollback.php'; +require __DIR__ . '/../../Catalog/_files/product_virtual_rollback.php'; diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php index 43beafe822043..d8744873af00c 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php @@ -13,7 +13,7 @@ $registry->register('isSecureArea', true); /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ -$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); +$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class); try { $product = $productRepository->get('simple', false, null, true); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address.php new file mode 100644 index 0000000000000..60d2f1c49d240 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address.php @@ -0,0 +1,49 @@ +get(GuestCartManagementInterface::class); +/** @var CartRepositoryInterface $cartRepository */ +$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class); +/** @var MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId */ +$maskedQuoteIdToQuoteId = Bootstrap::getObjectManager()->get(MaskedQuoteIdToQuoteIdInterface::class); +/** @var AddressInterfaceFactory $quoteAddressFactory */ +$quoteAddressFactory = Bootstrap::getObjectManager()->get(AddressInterfaceFactory::class); +/** @var DataObjectHelper $dataObjectHelper */ +$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class); +/** @var ShippingAddressManagementInterface $shippingAddressManagement */ +$shippingAddressManagement = Bootstrap::getObjectManager()->get(ShippingAddressManagementInterface::class); + +$cartHash = $guestCartManagement->createEmptyCart(); +$cartId = $maskedQuoteIdToQuoteId->execute($cartHash); +$cart = $cartRepository->get($cartId); +$cart->setReservedOrderId('guest_quote_with_address'); +$cartRepository->save($cart); + +$quoteAddressData = [ + AddressInterface::KEY_TELEPHONE => 4435555, + AddressInterface::KEY_POSTCODE => 78717, + AddressInterface::KEY_COUNTRY_ID => 'US', + AddressInterface::KEY_CITY => 'CityA', + AddressInterface::KEY_COMPANY => 'CompanyName', + AddressInterface::KEY_STREET => 'Andora str, 121', + AddressInterface::KEY_LASTNAME => 'Smith', + AddressInterface::KEY_FIRSTNAME => 'John', + AddressInterface::KEY_REGION_ID => 1, +]; +$quoteAddress = $quoteAddressFactory->create(); +$dataObjectHelper->populateWithArray($quoteAddress, $quoteAddressData, AddressInterfaceFactory::class); +$shippingAddressManagement->assign($cartId, $quoteAddress); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address_rollback.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address_rollback.php new file mode 100644 index 0000000000000..d9f894abf45b4 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/guest/quote_with_address_rollback.php @@ -0,0 +1,28 @@ +get(QuoteFactory::class); +/** @var QuoteResource $quoteResource */ +$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class); +/** @var QuoteIdMaskFactory $quoteIdMaskFactory */ +$quoteIdMaskFactory = Bootstrap::getObjectManager()->get(QuoteIdMaskFactory::class); + +$quote = $quoteFactory->create(); +$quoteResource->load($quote, 'guest_quote_with_address', 'reserved_order_id'); +$quoteResource->delete($quote); + +/** @var QuoteIdMask $quoteIdMask */ +$quoteIdMask = $quoteIdMaskFactory->create(); +$quoteIdMask->setQuoteId($quote->getId()) + ->delete(); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php new file mode 100644 index 0000000000000..1e7c10d251be0 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php @@ -0,0 +1,36 @@ +get(QuoteFactory::class); +/** @var QuoteResource $quoteResource */ +$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class); +/** @var ShippingInformationInterfaceFactory $shippingInformationFactory */ +$shippingInformationFactory = Bootstrap::getObjectManager()->get(ShippingInformationInterfaceFactory::class); +/** @var ShippingInformationManagementInterface $shippingInformationManagement */ +$shippingInformationManagement = Bootstrap::getObjectManager()->get(ShippingInformationManagementInterface::class); + +$quote = $quoteFactory->create(); +$quoteResource->load($quote, 'test_quote', 'reserved_order_id'); +$quoteAddress = $quote->getShippingAddress(); + +/** @var ShippingInformationInterface $shippingInformation */ +$shippingInformation = $shippingInformationFactory->create([ + 'data' => [ + ShippingInformationInterface::SHIPPING_ADDRESS => $quoteAddress, + ShippingInformationInterface::SHIPPING_CARRIER_CODE => 'flatrate', + ShippingInformationInterface::SHIPPING_METHOD_CODE => 'flatrate', + ], +]); +$shippingInformationManagement->saveAddressInformation($quote->getId(), $shippingInformation); From b64170a5782be475c6ad3a7df74facd13a78a1ed Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 12:25:58 -0500 Subject: [PATCH 21/26] GraphQL-428: Test coverage: GetAvailableShippingMethodsTest --- .../GetAvailableShippingMethodsTest.php | 92 +++++++++---------- .../Guest/GetAvailableShippingMethodsTest.php | 77 ++++++---------- ...ote_with_simple_product_saved_rollback.php | 4 +- .../_files/simple_product_rollback.php | 2 +- .../disable_offline_shipping_methods.php | 1 + ...able_offline_shipping_methods_rollback.php | 1 + 6 files changed, 80 insertions(+), 97 deletions(-) rename dev/tests/integration/testsuite/Magento/{OfflineShipping => GraphQl/Quote}/_files/disable_offline_shipping_methods.php (89%) rename dev/tests/integration/testsuite/Magento/{OfflineShipping => GraphQl/Quote}/_files/disable_offline_shipping_methods_rollback.php (86%) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php index 5ca3307371d2f..71cf0201951a3 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php @@ -7,10 +7,8 @@ namespace Magento\GraphQl\Quote\Customer; +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; use Magento\Integration\Api\CustomerTokenServiceInterface; -use Magento\Quote\Model\QuoteFactory; -use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; -use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; @@ -25,19 +23,9 @@ class GetAvailableShippingMethodsTest extends GraphQlAbstract private $customerTokenService; /** - * @var QuoteIdToMaskedQuoteIdInterface + * @var GetMaskedQuoteIdByReservedOrderId */ - private $quoteIdToMaskedId; - - /** - * @var QuoteFactory - */ - private $quoteFactory; - - /** - * @var QuoteResource - */ - private $quoteResource; + private $getMaskedQuoteIdByReservedOrderId; /** * @inheritdoc @@ -45,20 +33,22 @@ class GetAvailableShippingMethodsTest extends GraphQlAbstract protected function setUp() { $objectManager = Bootstrap::getObjectManager(); - $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteResource = $objectManager->get(QuoteResource::class); + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); } /** * Test case: get available shipping methods from current customer quote * - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php */ public function testGetAvailableShippingMethods() { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response); @@ -84,40 +74,62 @@ public function testGetAvailableShippingMethods() ); } + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + */ + public function testGetAvailableShippingMethodsFromGuestCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + /** * Test case: get available shipping methods from quote of another customer * + * _security * @magentoApiDataFixture Magento/Customer/_files/three_customers.php - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php */ public function testGetAvailableShippingMethodsFromAnotherCustomerCart() { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); $this->expectExceptionMessage( "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - - $this->graphQlQuery( - $this->getQuery($maskedQuoteId), - [], - '', - $this->getHeaderMap('customer2@search.example.com') - ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com')); } /** * Test case: get available shipping methods when all shipping methods are disabled * - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - * @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php */ - public function testGetAvailableShippingMethodsIfAllShippingsAreDisabled() + public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent() { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); - self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); + self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']); } /** @@ -173,16 +185,4 @@ private function getHeaderMap(string $username = 'customer@example.com', string $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; return $headerMap; } - - /** - * @param string $reservedOrderId - * @return string - */ - private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string - { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php index 9522d65991cd2..38fda50ba5836 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php @@ -7,9 +7,7 @@ namespace Magento\GraphQl\Quote\Guest; -use Magento\Quote\Model\QuoteFactory; -use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; -use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; @@ -19,19 +17,9 @@ class GetAvailableShippingMethodsTest extends GraphQlAbstract { /** - * @var QuoteIdToMaskedQuoteIdInterface + * @var GetMaskedQuoteIdByReservedOrderId */ - private $quoteIdToMaskedId; - - /** - * @var QuoteFactory - */ - private $quoteFactory; - - /** - * @var QuoteResource - */ - private $quoteResource; + private $getMaskedQuoteIdByReservedOrderId; /** * @inheritdoc @@ -39,19 +27,20 @@ class GetAvailableShippingMethodsTest extends GraphQlAbstract protected function setUp() { $objectManager = Bootstrap::getObjectManager(); - $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteResource = $objectManager->get(QuoteResource::class); - $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); } /** * Test case: get available shipping methods from current customer quote * - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php */ public function testGetAvailableShippingMethods() { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); self::assertArrayHasKey('cart', $response); @@ -61,15 +50,15 @@ public function testGetAvailableShippingMethods() self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); $expectedAddressData = [ - 'amount' => 5, - 'base_amount' => 5, + 'amount' => 10, + 'base_amount' => 10, 'carrier_code' => 'flatrate', 'carrier_title' => 'Flat Rate', 'error_message' => '', 'method_code' => 'flatrate', 'method_title' => 'Fixed', - 'price_incl_tax' => 5, - 'price_excl_tax' => 5, + 'price_incl_tax' => 10, + 'price_excl_tax' => 10, ]; self::assertEquals( $expectedAddressData, @@ -78,33 +67,38 @@ public function testGetAvailableShippingMethods() } /** - * Test case: get available shipping methods from customer's quote - * - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php */ public function testGetAvailableShippingMethodsFromCustomerCart() { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $this->expectExceptionMessage( "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - $this->graphQlQuery($this->getQuery($maskedQuoteId)); } /** * Test case: get available shipping methods when all shipping methods are disabled * - * @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php - * @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php */ - public function testGetAvailableShippingMethodsIfShippingsAreDisabled() + public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent() { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote'); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $response = $this->graphQlQuery($this->getQuery($maskedQuoteId)); - self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods'])); + self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']); } /** @@ -116,8 +110,9 @@ public function testGetAvailableShippingMethodsIfShippingsAreDisabled() public function testGetAvailableShippingMethodsOfNonExistentCart() { $maskedQuoteId = 'non_existent_masked_id'; + $query = $this->getQuery($maskedQuoteId); - $this->graphQlQuery($this->getQuery($maskedQuoteId)); + $this->graphQlQuery($query); } /** @@ -146,16 +141,4 @@ private function getQuery(string $maskedQuoteId): string } QUERY; } - - /** - * @param string $reservedOrderId - * @return string - */ - private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string - { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } } diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php index 2e333bbcb3049..105a981ccfc84 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_simple_product_saved_rollback.php @@ -1,13 +1,11 @@ create(\Magento\Quote\Model\Quote::class); diff --git a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php index 43beafe822043..d8744873af00c 100644 --- a/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Checkout/_files/simple_product_rollback.php @@ -13,7 +13,7 @@ $registry->register('isSecureArea', true); /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ -$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); +$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class); try { $product = $productRepository->get('simple', false, null, true); diff --git a/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php similarity index 89% rename from dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php rename to dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php index 30c64ab7a42a2..c5d0468dbfacb 100644 --- a/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods.php +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167 declare(strict_types=1); use Magento\Framework\App\Config\Storage\Writer; diff --git a/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/disable_offline_shipping_methods_rollback.php similarity index 86% rename from dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php rename to dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/disable_offline_shipping_methods_rollback.php index 31a16f42adfce..384ffbdf51f3c 100644 --- a/dev/tests/integration/testsuite/Magento/OfflineShipping/_files/disable_offline_shipping_methods_rollback.php +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/disable_offline_shipping_methods_rollback.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167 declare(strict_types=1); use Magento\Framework\App\Config\Storage\Writer; From 3cd2bd88cba9b5cc80ce26182455577897e9d5c4 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 13:47:44 -0500 Subject: [PATCH 22/26] GraphQL-191: Resolve random fails on Bamboo with \Magento\GraphQl\Quote\CouponTest::testApplyCouponToCartWithNoItems --- .../QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php | 6 +++++- .../testsuite/Magento/GraphQl/Quote/CouponTest.php | 8 +++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php index 5429d5333b25b..4de0464681186 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php @@ -75,7 +75,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value try { $this->couponManagement->set($cartId, $couponCode); } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); + $message = $e->getMessage(); + if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) { + $message = 'Cart does not contain products.'; + } + throw new GraphQlNoSuchEntityException(__($message), $e); } catch (CouldNotSaveException $e) { throw new LocalizedException(__($e->getMessage()), $e); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php index 810fc793fb1e2..828784ca27885 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CouponTest.php @@ -89,19 +89,17 @@ public function testApplyCouponTwice() /** * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php + * @expectedException \Exception + * @expectedExceptionMessage Cart does not contain products. */ public function testApplyCouponToCartWithNoItems() { $couponCode = '2?ds5!2d'; $this->quoteResource->load($this->quote, 'test_order_1', 'reserved_order_id'); - $cartId = (int)$this->quote->getId(); - $maskedQuoteId = $this->quoteIdToMaskedId->execute($cartId); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); - self::expectExceptionMessage( - 'The "' . $cartId . '" Cart doesn\'t contain products.' - ); $this->graphQlQuery($query); } From 79f9244a847c1553533b04c4aabba36c8e096d09 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Fri, 29 Mar 2019 22:20:24 +0200 Subject: [PATCH 23/26] magento/graphql-ce#502: Add missed rollback --- .../_files/three_customers_rollback.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php b/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php new file mode 100644 index 0000000000000..64330be259fca --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php @@ -0,0 +1,43 @@ +get(\Magento\Framework\Registry::class); +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +$customersToRemove = [ + 'customer@search.example.com', + 'customer2@search.example.com', + 'customer3@search.example.com', +]; + +/** + * @var Magento\Customer\Api\CustomerRepositoryInterface + */ +$customerRepository = $objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class); +/** + * @var RequestThrottler $throttler + */ +$throttler = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(RequestThrottler::class); +foreach ($customersToRemove as $customerEmail) { + try { + $customer = $customerRepository->get($customerEmail); + $customerRepository->delete($customer); + } catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { + // not found + } + + /* Unlock account if it was locked for tokens retrieval */ + $throttler->resetAuthenticationFailuresCount($customerEmail, RequestThrottler::USER_TYPE_CUSTOMER); +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); From 84a21a4590382a559e4a393559232f03651a576e Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 15:44:30 -0500 Subject: [PATCH 24/26] GraphQL-478: [Test Coverage] 'Selected Shipping Method' functionality --- .../GetSelectedShippingMethodTest.php | 180 ++++++++++++++ .../Customer/SelectedShippingMethodTest.php | 223 ------------------ .../Guest/GetSelectedShippingMethodTest.php | 136 +++++++++++ .../Guest/SelectedShippingMethodTest.php | 220 ----------------- 4 files changed, 316 insertions(+), 443 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SelectedShippingMethodTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSelectedShippingMethodTest.php delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SelectedShippingMethodTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php new file mode 100644 index 0000000000000..8dcba637dc57d --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php @@ -0,0 +1,180 @@ +getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php + */ + public function testGetSelectedShippingMethod() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + + $shippingAddress = current($response['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); + + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); + self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']); + + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); + self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']); + } + + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php + */ + public function testGetSelectedShippingMethodFromGuestCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php + */ + public function testGetSelectedShippingMethodFromAnotherCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com')); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + */ + public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + + $shippingAddress = current($response['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); + + self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']); + self::assertNull($shippingAddress['selected_shipping_method']['method_code']); + self::assertNull($shippingAddress['selected_shipping_method']['label']); + self::assertNull($shippingAddress['selected_shipping_method']['amount']); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * + * @expectedException \Exception + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" + */ + public function testGetGetSelectedShippingMethodOfNonExistentCart() + { + $maskedQuoteId = 'non_existent_masked_id'; + $query = $this->getQuery($maskedQuoteId); + + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @param string $username + * @param string $password + * @return array + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + return $headerMap; + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getQuery(string $maskedQuoteId): string + { + return <<objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - - $this->filterBuilder = $this->objectManager->create( - \Magento\Framework\Api\FilterBuilder::class - ); - $this->sortOrderBuilder = $this->objectManager->create( - \Magento\Framework\Api\SortOrderBuilder::class - ); - $this->searchCriteriaBuilder = $this->objectManager->create( - \Magento\Framework\Api\SearchCriteriaBuilder::class - ); - $this->quoteResource = $this->objectManager->get( - \Magento\Quote\Model\ResourceModel\Quote::class - ); - $this->quoteFactory = $this->objectManager->get( - \Magento\Quote\Model\QuoteFactory::class - ); - $this->quoteIdToMaskedId = $this->objectManager->get( - \Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface::class - ); - $this->customerTokenService = $this->objectManager->get( - \Magento\Integration\Api\CustomerTokenServiceInterface::class - ); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php - */ - public function testGetCartWithShippingMethod() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $query = $this->getQuery($maskedQuoteId); - $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - - self::assertArrayHasKey('cart', $response); - self::assertInternalType( - 'array', - $response['cart']['shipping_addresses'][0]['selected_shipping_method'], - 'There are no selected shipping method for customer cart!' - ); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php - */ - public function testGetShippingMethodFromCustomerCart() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $query = $this->getQuery($maskedQuoteId); - $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - - self::assertArrayHasKey('cart', $response); - - $selectedShippingMethod = $this->getSelectedShippingMethod($response); - - self::assertEquals('flatrate', $selectedShippingMethod['carrier_code']); - self::assertEquals('flatrate', $selectedShippingMethod['method_code']); - self::assertEquals('Flat Rate - Fixed', $selectedShippingMethod['label']); - self::assertEquals(0, $selectedShippingMethod['amount']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php - */ - public function testGetShippingMethodIfShippingMethodIsNotSet() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $query = $this->getQuery($maskedQuoteId); - - $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - - self::assertArrayHasKey('cart', $response); - - $selectedShippingMethod = $this->getSelectedShippingMethod($response); - - self::assertNull($selectedShippingMethod['carrier_code']); - self::assertNull($selectedShippingMethod['method_code']); - self::assertNull($selectedShippingMethod['label']); - self::assertNull($selectedShippingMethod['amount']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php - */ - public function testGetShippingMethodOfNonExistentCart() - { - $query = $this->getQuery('nonExistentCart'); - self::expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class); - self::expectExceptionMessage( - 'GraphQL response contains errors: Could not find a cart with ID "nonExistentCart"' - ); - $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - } - - /** - * Retrieve quote by given reserved order ID - * - * @param string $reservedOrderId - * @return \Magento\Quote\Model\Quote - * @throws \InvalidArgumentException - */ - protected function getCart($reservedOrderId) - { - /** @var $cart \Magento\Quote\Model\Quote */ - $cart = $this->objectManager->get(\Magento\Quote\Model\Quote::class); - $cart->load($reservedOrderId, 'reserved_order_id'); - if (!$cart->getId()) { - throw new \InvalidArgumentException('There is no quote with provided reserved order ID.'); - } - return $cart; - } - - /** - * @param string $reservedOrderId - * @return string - */ - private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string - { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } - - /** - * @param string $username - * @param string $password - * @return array - */ - private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array - { - $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); - $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; - return $headerMap; - } - - /** - * @param string $maskedQuoteId - * @return string - */ - private function getQuery( - string $maskedQuoteId - ): string { - return <<getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php + */ + public function testGetSelectedShippingMethod() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + + $shippingAddress = current($response['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); + + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); + self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']); + + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); + self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']); + } + + /** + * _security + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php + */ + public function testGetSelectedShippingMethodFromCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php + */ + public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId); + + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + + $shippingAddress = current($response['cart']['shipping_addresses']); + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); + + self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']); + self::assertNull($shippingAddress['selected_shipping_method']['method_code']); + self::assertNull($shippingAddress['selected_shipping_method']['label']); + self::assertNull($shippingAddress['selected_shipping_method']['amount']); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" + */ + public function testGetSelectedShippingMethodOfNonExistentCart() + { + $maskedQuoteId = 'non_existent_masked_id'; + $query = $this->getQuery($maskedQuoteId); + $this->graphQlQuery($query); + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getQuery(string $maskedQuoteId): string + { + return <<objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - - $this->filterBuilder = $this->objectManager->create( - \Magento\Framework\Api\FilterBuilder::class - ); - $this->sortOrderBuilder = $this->objectManager->create( - \Magento\Framework\Api\SortOrderBuilder::class - ); - $this->searchCriteriaBuilder = $this->objectManager->create( - \Magento\Framework\Api\SearchCriteriaBuilder::class - ); - $this->quoteResource = $this->objectManager->get( - \Magento\Quote\Model\ResourceModel\Quote::class - ); - $this->quoteFactory = $this->objectManager->get( - \Magento\Quote\Model\QuoteFactory::class - ); - $this->quoteIdToMaskedId = $this->objectManager->get( - \Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface::class - ); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php - */ - public function testGetCartWithShippingMethod() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $this->unAssignCustomerFromQuote('test_order_1'); - $query = $this->getQuery($maskedQuoteId); - $response = $this->graphQlQuery($query); - - self::assertArrayHasKey('cart', $response); - self::assertInternalType( - 'array', - $response['cart']['shipping_addresses'][0]['selected_shipping_method'], - 'There are no selected shipping method for customer cart!' - ); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php - */ - public function testGetShippingMethodFromCustomerCart() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $this->unAssignCustomerFromQuote('test_order_1'); - $query = $this->getQuery($maskedQuoteId); - $response = $this->graphQlQuery($query); - - self::assertArrayHasKey('cart', $response); - - $selectedShippingMethod = $this->getSelectedShippingMethod($response); - - self::assertEquals('flatrate', $selectedShippingMethod['carrier_code']); - self::assertEquals('flatrate', $selectedShippingMethod['method_code']); - self::assertEquals('Flat Rate - Fixed', $selectedShippingMethod['label']); - self::assertEquals(0, $selectedShippingMethod['amount']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php - */ - public function testGetShippingMethodIfShippingMethodIsNotSet() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1'); - $query = $this->getQuery($maskedQuoteId); - $this->unAssignCustomerFromQuote('test_order_1'); - $response = $this->graphQlQuery($query); - - self::assertArrayHasKey('cart', $response); - - $selectedShippingMethod = $this->getSelectedShippingMethod($response); - - self::assertNull($selectedShippingMethod['carrier_code']); - self::assertNull($selectedShippingMethod['method_code']); - self::assertNull($selectedShippingMethod['label']); - self::assertNull($selectedShippingMethod['amount']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address.php - */ - public function testGetShippingMethodOfNonExistentCart() - { - $query = $this->getQuery('nonExistentCart'); - self::expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class); - self::expectExceptionMessage( - 'GraphQL response contains errors: Could not find a cart with ID "nonExistentCart"' - ); - $this->graphQlQuery($query); - } - - /** - * Retrieve quote by given reserved order ID - * - * @param string $reservedOrderId - * @return \Magento\Quote\Model\Quote - * @throws \InvalidArgumentException - */ - protected function getCart($reservedOrderId) - { - /** @var $cart \Magento\Quote\Model\Quote */ - $cart = $this->objectManager->get(\Magento\Quote\Model\Quote::class); - $cart->load($reservedOrderId, 'reserved_order_id'); - if (!$cart->getId()) { - throw new \InvalidArgumentException('There is no quote with provided reserved order ID.'); - } - return $cart; - } - - /** - * @param string $reservedOrderId - * @return string - */ - private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string - { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } - - /** - * @param string $maskedQuoteId - * @return string - */ - private function getQuery( - string $maskedQuoteId - ): string { - return <<quoteFactory->create(); - $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); - $quote->setCustomerId(0); - $this->quoteResource->save($quote); - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } -} From 300e5b46b27a05548e30b33229c483f27dfa1ff2 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 18:07:31 -0500 Subject: [PATCH 25/26] GraphQL-502: [Test Coverage] We do not have a rollback for Magento/Customer/_files/three_customers.php fixture --- .../Magento/Customer/_files/three_customers_rollback.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php b/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php index 64330be259fca..8e13c1c25162b 100644 --- a/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php +++ b/dev/tests/integration/testsuite/Magento/Customer/_files/three_customers_rollback.php @@ -32,7 +32,10 @@ $customer = $customerRepository->get($customerEmail); $customerRepository->delete($customer); } catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { - // not found + /** + * Tests which are wrapped with MySQL transaction clear all data by transaction rollback. + */ + continue; } /* Unlock account if it was locked for tokens retrieval */ From 2b59efac37e4cdd59eb480e92021fcc7b43bc0c0 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 19:06:32 -0500 Subject: [PATCH 26/26] GraphQL-428: Test coverage: GetAvailableShippingMethodsTest -- fix static tests --- .../QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php | 4 ---- .../GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php | 3 +++ .../GraphQl/Quote/Guest/GetSelectedShippingMethodTest.php | 3 +++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php index 4104e179160d9..730cf1b0ffee3 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodsOnCart.php @@ -7,11 +7,7 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\Framework\Exception\InputException; -use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Framework\Exception\StateException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; -use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Quote\Api\Data\CartInterface; diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php index 8dcba637dc57d..16d085c1d09be 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedShippingMethodTest.php @@ -12,6 +12,9 @@ use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; +/** + * Test for get selected shipping method + */ class GetSelectedShippingMethodTest extends GraphQlAbstract { /** diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSelectedShippingMethodTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSelectedShippingMethodTest.php index e00f79b1a99a0..c31b48ccc1087 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSelectedShippingMethodTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSelectedShippingMethodTest.php @@ -11,6 +11,9 @@ use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; +/** + * Test for get selected shipping method + */ class GetSelectedShippingMethodTest extends GraphQlAbstract { /**