|
| 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\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test for assigning customer to the guest cart |
| 18 | + */ |
| 19 | +class AssignCustomerToGuestCartTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 23 | + */ |
| 24 | + private $quoteIdToMaskedId; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var GetQuoteByReservedOrderId |
| 28 | + */ |
| 29 | + private $getQuoteByReservedOrderId; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CustomerTokenServiceInterface |
| 33 | + */ |
| 34 | + private $customerTokenService; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritdoc |
| 38 | + */ |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $objectManager = Bootstrap::getObjectManager(); |
| 42 | + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 43 | + $this->getQuoteByReservedOrderId = $objectManager->get(GetQuoteByReservedOrderId::class); |
| 44 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test for assigning customer to the guest cart |
| 49 | + * |
| 50 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php |
| 51 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 52 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 53 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 54 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 55 | + */ |
| 56 | + public function testAssignCustomerToGuestCart(): void |
| 57 | + { |
| 58 | + $guestQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_virtual_product_without_address'); |
| 59 | + $guestQuoteItem = $guestQuote->getAllVisibleItems()[0]; |
| 60 | + $guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId()); |
| 61 | + |
| 62 | + $customerQuote = $this->getQuoteByReservedOrderId->execute('test_quote'); |
| 63 | + $customerQuoteItem = $customerQuote->getAllVisibleItems()[0]; |
| 64 | + |
| 65 | + $response = $this->graphQlMutation( |
| 66 | + $this->getAssignCustomerToGuestCartMutation($guestQuoteMaskedId), |
| 67 | + [], |
| 68 | + '', |
| 69 | + $this->getHeaderMap() |
| 70 | + ); |
| 71 | + $this->assertArrayHasKey('assignCustomerToGuestCart', $response); |
| 72 | + $this->assertArrayHasKey('items', $response['assignCustomerToGuestCart']); |
| 73 | + $items = $response['assignCustomerToGuestCart']['items']; |
| 74 | + $this->assertCount(2,$items); |
| 75 | + |
| 76 | + $this->assertEquals($customerQuoteItem->getQty(), $items[1]['quantity']); |
| 77 | + $this->assertEquals($customerQuoteItem->getSku(), $items[1]['product']['sku']); |
| 78 | + |
| 79 | + $this->assertEquals($guestQuoteItem->getQty(), $items[0]['quantity']); |
| 80 | + $this->assertEquals($guestQuoteItem->getSku(), $items[0]['product']['sku']); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test that customer cart is expired after assigning |
| 85 | + * |
| 86 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php |
| 87 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 88 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 89 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 90 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 91 | + */ |
| 92 | + public function testCustomerCartExpiryAfterAssigning(): void |
| 93 | + { |
| 94 | + $this->expectException(\Exception::class); |
| 95 | + $this->expectExceptionMessage('The cart isn\'t active.'); |
| 96 | + |
| 97 | + $guestQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_virtual_product_without_address'); |
| 98 | + $guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId()); |
| 99 | + |
| 100 | + $customerQuote = $this->getQuoteByReservedOrderId->execute('test_quote'); |
| 101 | + $customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId()); |
| 102 | + |
| 103 | + $this->graphQlMutation( |
| 104 | + $this->getAssignCustomerToGuestCartMutation($guestQuoteMaskedId), |
| 105 | + [], |
| 106 | + '', |
| 107 | + $this->getHeaderMap() |
| 108 | + ); |
| 109 | + $this->graphQlMutation( |
| 110 | + $this->getCartQuery($customerQuoteMaskedId), |
| 111 | + [], |
| 112 | + '', |
| 113 | + $this->getHeaderMap() |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test for assigning customer to non existent cart |
| 119 | + * |
| 120 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 121 | + */ |
| 122 | + public function testAssigningCustomerToNonExistentCart(): void |
| 123 | + { |
| 124 | + $guestQuoteMaskedId = "non_existent_masked_id"; |
| 125 | + $this->expectException(\Exception::class); |
| 126 | + $this->expectExceptionMessage("Could not find a cart with ID \"{$guestQuoteMaskedId}\""); |
| 127 | + |
| 128 | + $this->graphQlMutation( |
| 129 | + $this->getAssignCustomerToGuestCartMutation($guestQuoteMaskedId), |
| 130 | + [], |
| 131 | + '', |
| 132 | + $this->getHeaderMap() |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test for assigning customer to the customer cart |
| 138 | + * |
| 139 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 140 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 141 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 142 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 143 | + */ |
| 144 | + public function testAssignCustomerToCustomerCart(): void |
| 145 | + { |
| 146 | + $customerQuote = $this->getQuoteByReservedOrderId->execute('test_quote'); |
| 147 | + $customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId()); |
| 148 | + |
| 149 | + $this->expectException(\Exception::class); |
| 150 | + $this->expectExceptionMessage("The current user cannot perform operations on cart \"{$customerQuoteMaskedId}\""); |
| 151 | + |
| 152 | + $this->graphQlMutation( |
| 153 | + $this->getAssignCustomerToGuestCartMutation($customerQuoteMaskedId), |
| 154 | + [], |
| 155 | + '', |
| 156 | + $this->getHeaderMap() |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Create the assignCustomerToGuestCart mutation |
| 162 | + * |
| 163 | + * @param string $guestQuoteMaskedId |
| 164 | + * @return string |
| 165 | + */ |
| 166 | + private function getAssignCustomerToGuestCartMutation(string $guestQuoteMaskedId): string |
| 167 | + { |
| 168 | + return <<<QUERY |
| 169 | +mutation { |
| 170 | + assignCustomerToGuestCart( |
| 171 | + cart_id: "{$guestQuoteMaskedId}" |
| 172 | + ){ |
| 173 | + items { |
| 174 | + quantity |
| 175 | + product { |
| 176 | + sku |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +QUERY; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Get cart query |
| 186 | + * |
| 187 | + * @param string $maskedId |
| 188 | + * @return string |
| 189 | + */ |
| 190 | + private function getCartQuery(string $maskedId): string |
| 191 | + { |
| 192 | + return <<<QUERY |
| 193 | +{ |
| 194 | + cart(cart_id: "{$maskedId}") { |
| 195 | + items { |
| 196 | + quantity |
| 197 | + product { |
| 198 | + sku |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | +QUERY; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Retrieve customer authorization headers |
| 208 | + * |
| 209 | + * @param string $username |
| 210 | + * @param string $password |
| 211 | + * @return array |
| 212 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 213 | + */ |
| 214 | + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array |
| 215 | + { |
| 216 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 217 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 218 | + return $headerMap; |
| 219 | + } |
| 220 | +} |
0 commit comments