From 84a21a4590382a559e4a393559232f03651a576e Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 29 Mar 2019 15:44:30 -0500 Subject: [PATCH] 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 000000000000..8dcba637dc57 --- /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()); - } -}