|
| 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\OfflineShipping; |
| 9 | + |
| 10 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 11 | +use Magento\Quote\Model\QuoteFactory; |
| 12 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 13 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for setting offline shipping methods on cart |
| 19 | + */ |
| 20 | +class SetOfflineShippingMethodsOnCartTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var QuoteFactory |
| 24 | + */ |
| 25 | + private $quoteFactory; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var CustomerTokenServiceInterface |
| 29 | + */ |
| 30 | + private $customerTokenService; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var QuoteResource |
| 34 | + */ |
| 35 | + private $quoteResource; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 39 | + */ |
| 40 | + private $quoteIdToMaskedId; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->quoteResource = $objectManager->get(QuoteResource::class); |
| 49 | + $this->quoteFactory = $objectManager->get(QuoteFactory::class); |
| 50 | + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 51 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php |
| 56 | + * @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php |
| 57 | + * @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php |
| 58 | + * |
| 59 | + * @param string $carrierCode |
| 60 | + * @param string $methodCode |
| 61 | + * @param float $amount |
| 62 | + * @param string $label |
| 63 | + * @dataProvider offlineShippingMethodDataProvider |
| 64 | + */ |
| 65 | + public function testSetOfflineShippingMethod(string $carrierCode, string $methodCode, float $amount, string $label) |
| 66 | + { |
| 67 | + $quote = $this->quoteFactory->create(); |
| 68 | + $this->quoteResource->load( |
| 69 | + $quote, |
| 70 | + 'test_order_1', |
| 71 | + 'reserved_order_id' |
| 72 | + ); |
| 73 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); |
| 74 | + $shippingAddressId = (int)$quote->getShippingAddress()->getId(); |
| 75 | + |
| 76 | + $query = $this->getQuery( |
| 77 | + $maskedQuoteId, |
| 78 | + $shippingAddressId, |
| 79 | + $carrierCode, |
| 80 | + $methodCode |
| 81 | + ); |
| 82 | + |
| 83 | + $response = $this->sendRequestWithToken($query); |
| 84 | + |
| 85 | + $addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses']; |
| 86 | + self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $carrierCode); |
| 87 | + self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $methodCode); |
| 88 | + self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $amount); |
| 89 | + self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $label); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + public function offlineShippingMethodDataProvider() |
| 96 | + { |
| 97 | + return [ |
| 98 | + 'flatrate_flatrate' => ['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'], |
| 99 | + 'tablerate_bestway' => ['tablerate', 'bestway', 10, 'Best Way - Table Rate'], |
| 100 | + 'freeshipping_freeshipping' => ['freeshipping', 'freeshipping', 0, 'Free Shipping - Free'], |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php |
| 106 | + * @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php |
| 107 | + */ |
| 108 | + public function testSetShippingMethodTwiceInOneRequest() |
| 109 | + { |
| 110 | + $quote = $this->quoteFactory->create(); |
| 111 | + $this->quoteResource->load( |
| 112 | + $quote, |
| 113 | + 'test_order_1', |
| 114 | + 'reserved_order_id' |
| 115 | + ); |
| 116 | + $shippingAddress = $quote->getShippingAddress(); |
| 117 | + $shippingAddressId = $shippingAddress->getId(); |
| 118 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); |
| 119 | + |
| 120 | + $query = <<<QUERY |
| 121 | +mutation { |
| 122 | + setShippingMethodsOnCart(input: { |
| 123 | + cart_id: "$maskedQuoteId" |
| 124 | + shipping_methods: [ |
| 125 | + { |
| 126 | + cart_address_id: $shippingAddressId |
| 127 | + carrier_code: "flatrate" |
| 128 | + method_code: "flatrate" |
| 129 | + } |
| 130 | + { |
| 131 | + cart_address_id: $shippingAddressId |
| 132 | + carrier_code: "freeshipping" |
| 133 | + method_code: "freeshipping" |
| 134 | + } |
| 135 | + ] |
| 136 | + }) { |
| 137 | + cart { |
| 138 | + shipping_addresses { |
| 139 | + selected_shipping_method { |
| 140 | + carrier_code |
| 141 | + method_code |
| 142 | + label |
| 143 | + amount |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | +QUERY; |
| 150 | + self::expectExceptionMessage('You cannot specify multiple shipping methods.'); |
| 151 | + $this->sendRequestWithToken($query); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Generates query for setting the specified shipping method on cart |
| 156 | + * |
| 157 | + * @param int $shippingAddressId |
| 158 | + * @param string $maskedQuoteId |
| 159 | + * @param string $carrierCode |
| 160 | + * @param string $methodCode |
| 161 | + * @return string |
| 162 | + */ |
| 163 | + private function getQuery( |
| 164 | + string $maskedQuoteId, |
| 165 | + int $shippingAddressId, |
| 166 | + string $carrierCode, |
| 167 | + string $methodCode |
| 168 | + ): string { |
| 169 | + return <<<QUERY |
| 170 | +mutation { |
| 171 | + setShippingMethodsOnCart(input: { |
| 172 | + cart_id: "$maskedQuoteId" |
| 173 | + shipping_methods: [ |
| 174 | + { |
| 175 | + cart_address_id: $shippingAddressId |
| 176 | + carrier_code: "$carrierCode" |
| 177 | + method_code: "$methodCode" |
| 178 | + } |
| 179 | + ] |
| 180 | + }) { |
| 181 | + cart { |
| 182 | + shipping_addresses { |
| 183 | + selected_shipping_method { |
| 184 | + carrier_code |
| 185 | + method_code |
| 186 | + label |
| 187 | + amount |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | +QUERY; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Sends a GraphQL request with using a bearer token |
| 198 | + * |
| 199 | + * @param string $query |
| 200 | + * @return array |
| 201 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 202 | + */ |
| 203 | + private function sendRequestWithToken(string $query): array |
| 204 | + { |
| 205 | + $customerToken = $this-> customerTokenService-> createCustomerAccessToken( '[email protected]', 'password'); |
| 206 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 207 | + |
| 208 | + return $this->graphQlQuery($query, [], '', $headerMap); |
| 209 | + } |
| 210 | +} |
0 commit comments