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);