|
| 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 getting cart totals for registered customer |
| 17 | + */ |
| 18 | +class CartTotalsTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var CustomerTokenServiceInterface |
| 22 | + */ |
| 23 | + private $customerTokenService; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 27 | + */ |
| 28 | + private $getMaskedQuoteIdByReservedOrderId; |
| 29 | + |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + $objectManager = Bootstrap::getObjectManager(); |
| 33 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 34 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 39 | + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php |
| 40 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 41 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php |
| 42 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 43 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 44 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 45 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 46 | + */ |
| 47 | + public function testGetCartTotalsWithTaxApplied() |
| 48 | + { |
| 49 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 50 | + $query = $this->getQuery($maskedQuoteId); |
| 51 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 52 | + |
| 53 | + self::assertArrayHasKey('prices', $response['cart']); |
| 54 | + $pricesResponse = $response['cart']['prices']; |
| 55 | + self::assertEquals(21.5, $pricesResponse['grand_total']['value']); |
| 56 | + self::assertEquals(21.5, $pricesResponse['subtotal_including_tax']['value']); |
| 57 | + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); |
| 58 | + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); |
| 59 | + |
| 60 | + $appliedTaxesResponse = $pricesResponse['applied_taxes']; |
| 61 | + |
| 62 | + self::assertEquals('US-TEST-*-Rate-1', $appliedTaxesResponse[0]['label']); |
| 63 | + self::assertEquals(1.5, $appliedTaxesResponse[0]['amount']['value']); |
| 64 | + self::assertEquals('USD', $appliedTaxesResponse[0]['amount']['currency']); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 69 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 70 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 71 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 72 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 73 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 74 | + */ |
| 75 | + public function testGetTotalsWithNoTaxApplied() |
| 76 | + { |
| 77 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 78 | + $query = $this->getQuery($maskedQuoteId); |
| 79 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 80 | + |
| 81 | + $pricesResponse = $response['cart']['prices']; |
| 82 | + self::assertEquals(20, $pricesResponse['grand_total']['value']); |
| 83 | + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); |
| 84 | + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); |
| 85 | + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); |
| 86 | + self::assertEmpty($pricesResponse['applied_taxes']); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The totals calculation is based on quote address. |
| 91 | + * But the totals should be calculated even if no address is set |
| 92 | + * |
| 93 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 94 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 95 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 96 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 97 | + */ |
| 98 | + public function testGetCartTotalsWithNoAddressSet() |
| 99 | + { |
| 100 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 101 | + $query = $this->getQuery($maskedQuoteId); |
| 102 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 103 | + |
| 104 | + $pricesResponse = $response['cart']['prices']; |
| 105 | + self::assertEquals(20, $pricesResponse['grand_total']['value']); |
| 106 | + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); |
| 107 | + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); |
| 108 | + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); |
| 109 | + self::assertEmpty($pricesResponse['applied_taxes']); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * _security |
| 114 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 115 | + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php |
| 116 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 117 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php |
| 118 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 119 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 120 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 121 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 122 | + */ |
| 123 | + public function testGetTotalsFromGuestCart() |
| 124 | + { |
| 125 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 126 | + $query = $this->getQuery($maskedQuoteId); |
| 127 | + |
| 128 | + $this->expectExceptionMessage( |
| 129 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 130 | + ); |
| 131 | + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * _security |
| 136 | + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php |
| 137 | + * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php |
| 138 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 139 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php |
| 140 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 141 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 142 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 143 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 144 | + */ |
| 145 | + public function testGetTotalsFromAnotherCustomerCart() |
| 146 | + { |
| 147 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 148 | + $query = $this->getQuery($maskedQuoteId); |
| 149 | + |
| 150 | + $this->expectExceptionMessage( |
| 151 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 152 | + ); |
| 153 | + $this-> graphQlQuery( $query, [], '', $this-> getHeaderMap( '[email protected]')); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Generates GraphQl query for retrieving cart totals |
| 158 | + * |
| 159 | + * @param string $maskedQuoteId |
| 160 | + * @return string |
| 161 | + */ |
| 162 | + private function getQuery(string $maskedQuoteId): string |
| 163 | + { |
| 164 | + return <<<QUERY |
| 165 | +{ |
| 166 | + cart(cart_id: "$maskedQuoteId") { |
| 167 | + prices { |
| 168 | + grand_total { |
| 169 | + value, |
| 170 | + currency |
| 171 | + } |
| 172 | + subtotal_including_tax { |
| 173 | + value |
| 174 | + currency |
| 175 | + } |
| 176 | + subtotal_excluding_tax { |
| 177 | + value |
| 178 | + currency |
| 179 | + } |
| 180 | + subtotal_with_discount_excluding_tax { |
| 181 | + value |
| 182 | + currency |
| 183 | + } |
| 184 | + applied_taxes { |
| 185 | + label |
| 186 | + amount { |
| 187 | + value |
| 188 | + currency |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | +QUERY; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @param string $username |
| 199 | + * @param string $password |
| 200 | + * @return array |
| 201 | + */ |
| 202 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 203 | + { |
| 204 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 205 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 206 | + return $headerMap; |
| 207 | + } |
| 208 | +} |
0 commit comments