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