|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types = 1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote\Customer; |
| 9 | + |
| 10 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test for get available shipping methods |
| 17 | + */ |
| 18 | +class GetAvailableShippingMethodsTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var CustomerTokenServiceInterface |
| 22 | + */ |
| 23 | + private $customerTokenService; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 27 | + */ |
| 28 | + private $getMaskedQuoteIdByReservedOrderId; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | + protected function setUp() |
| 34 | + { |
| 35 | + $objectManager = Bootstrap::getObjectManager(); |
| 36 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 37 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test case: get available shipping methods from current customer quote |
| 42 | + * |
| 43 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 44 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 45 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 46 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 47 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 48 | + */ |
| 49 | + public function testGetAvailableShippingMethods() |
| 50 | + { |
| 51 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 52 | + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); |
| 53 | + |
| 54 | + self::assertArrayHasKey('cart', $response); |
| 55 | + self::assertArrayHasKey('shipping_addresses', $response['cart']); |
| 56 | + self::assertCount(1, $response['cart']['shipping_addresses']); |
| 57 | + self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]); |
| 58 | + self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); |
| 59 | + |
| 60 | + $expectedAddressData = [ |
| 61 | + 'amount' => 10, |
| 62 | + 'base_amount' => 10, |
| 63 | + 'carrier_code' => 'flatrate', |
| 64 | + 'carrier_title' => 'Flat Rate', |
| 65 | + 'error_message' => '', |
| 66 | + 'method_code' => 'flatrate', |
| 67 | + 'method_title' => 'Fixed', |
| 68 | + 'price_incl_tax' => 10, |
| 69 | + 'price_excl_tax' => 10, |
| 70 | + ]; |
| 71 | + self::assertEquals( |
| 72 | + $expectedAddressData, |
| 73 | + $response['cart']['shipping_addresses'][0]['available_shipping_methods'][0] |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * _security |
| 79 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 80 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 81 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 82 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 83 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 84 | + */ |
| 85 | + public function testGetAvailableShippingMethodsFromGuestCart() |
| 86 | + { |
| 87 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 88 | + $query = $this->getQuery($maskedQuoteId); |
| 89 | + |
| 90 | + $this->expectExceptionMessage( |
| 91 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 92 | + ); |
| 93 | + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test case: get available shipping methods from quote of another customer |
| 98 | + * |
| 99 | + * _security |
| 100 | + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php |
| 101 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 102 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 103 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 104 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 105 | + */ |
| 106 | + public function testGetAvailableShippingMethodsFromAnotherCustomerCart() |
| 107 | + { |
| 108 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 109 | + $query = $this->getQuery($maskedQuoteId); |
| 110 | + |
| 111 | + $this->expectExceptionMessage( |
| 112 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 113 | + ); |
| 114 | + $this-> graphQlQuery( $query, [], '', $this-> getHeaderMap( '[email protected]')); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test case: get available shipping methods when all shipping methods are disabled |
| 119 | + * |
| 120 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 121 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 122 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 123 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 124 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 125 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php |
| 126 | + */ |
| 127 | + public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent() |
| 128 | + { |
| 129 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 130 | + $response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap()); |
| 131 | + |
| 132 | + self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Test case: get available shipping methods from non-existent cart |
| 137 | + * |
| 138 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 139 | + * @expectedException \Exception |
| 140 | + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" |
| 141 | + */ |
| 142 | + public function testGetAvailableShippingMethodsOfNonExistentCart() |
| 143 | + { |
| 144 | + $maskedQuoteId = 'non_existent_masked_id'; |
| 145 | + $query = $this->getQuery($maskedQuoteId); |
| 146 | + |
| 147 | + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param string $maskedQuoteId |
| 152 | + * @return string |
| 153 | + */ |
| 154 | + private function getQuery(string $maskedQuoteId): string |
| 155 | + { |
| 156 | + return <<<QUERY |
| 157 | +query { |
| 158 | + cart (cart_id: "{$maskedQuoteId}") { |
| 159 | + shipping_addresses { |
| 160 | + available_shipping_methods { |
| 161 | + amount |
| 162 | + base_amount |
| 163 | + carrier_code |
| 164 | + carrier_title |
| 165 | + error_message |
| 166 | + method_code |
| 167 | + method_title |
| 168 | + price_excl_tax |
| 169 | + price_incl_tax |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | +QUERY; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @param string $username |
| 179 | + * @param string $password |
| 180 | + * @return array |
| 181 | + */ |
| 182 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 183 | + { |
| 184 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 185 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 186 | + return $headerMap; |
| 187 | + } |
| 188 | +} |
0 commit comments