|
| 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\Framework\Exception\AuthenticationException; |
| 11 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 12 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test adding virtual product to Cart |
| 18 | + */ |
| 19 | +class AddVirtualProductToCartTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var CustomerTokenServiceInterface |
| 23 | + */ |
| 24 | + private $customerTokenService; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 28 | + */ |
| 29 | + private $getMaskedQuoteIdByReservedOrderId; |
| 30 | + |
| 31 | + /** |
| 32 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 33 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php |
| 34 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 35 | + */ |
| 36 | + public function testAddVirtualProductToCart() |
| 37 | + { |
| 38 | + $sku = 'virtual_product'; |
| 39 | + $qty = 2; |
| 40 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 41 | + $query = $this->getQuery($maskedQuoteId, $sku, $qty); |
| 42 | + $response = $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 43 | + |
| 44 | + self::assertArrayHasKey('cart', $response['addVirtualProductsToCart']); |
| 45 | + self::assertEquals($qty, $response['addVirtualProductsToCart']['cart']['items'][0]['qty']); |
| 46 | + self::assertEquals($sku, $response['addVirtualProductsToCart']['cart']['items'][0]['product']['sku']); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 51 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php |
| 52 | + */ |
| 53 | + public function testAddVirtualToNonExistentCart() |
| 54 | + { |
| 55 | + $sku = 'virtual_product'; |
| 56 | + $qty = 2; |
| 57 | + $nonExistentMaskedQuoteId = 'non_existent_masked_id'; |
| 58 | + $query = $this->getQuery($nonExistentMaskedQuoteId, $sku, $qty); |
| 59 | + |
| 60 | + $this->expectExceptionMessage( |
| 61 | + "Could not find a cart with ID \"$nonExistentMaskedQuoteId\"" |
| 62 | + ); |
| 63 | + |
| 64 | + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 69 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 70 | + */ |
| 71 | + public function testNonExistentProductToCart() |
| 72 | + { |
| 73 | + $sku = 'virtual_product'; |
| 74 | + $qty = 2; |
| 75 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 76 | + $query = $this->getQuery($maskedQuoteId, $sku, $qty); |
| 77 | + |
| 78 | + $this->expectExceptionMessage( |
| 79 | + "Could not find a product with SKU \"virtual_product\"" |
| 80 | + ); |
| 81 | + |
| 82 | + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 87 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php |
| 88 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 89 | + */ |
| 90 | + public function testAddVirtualProductToGuestCart() |
| 91 | + { |
| 92 | + $sku = 'virtual_product'; |
| 93 | + $qty = 2; |
| 94 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 95 | + $query = $this->getQuery($maskedQuoteId, $sku, $qty); |
| 96 | + |
| 97 | + $this->expectExceptionMessage( |
| 98 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 99 | + ); |
| 100 | + |
| 101 | + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php |
| 106 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php |
| 107 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 108 | + */ |
| 109 | + public function testAddVirtualProductToAnotherCustomerCart() |
| 110 | + { |
| 111 | + $sku = 'virtual_product'; |
| 112 | + $qty = 2; |
| 113 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 114 | + $query = $this->getQuery($maskedQuoteId, $sku, $qty); |
| 115 | + |
| 116 | + $this->expectExceptionMessage( |
| 117 | + "The current user cannot perform operations on cart \"$maskedQuoteId\"" |
| 118 | + ); |
| 119 | + |
| 120 | + $this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com')); |
| 121 | + } |
| 122 | + |
| 123 | + protected function setUp() |
| 124 | + { |
| 125 | + $objectManager = Bootstrap::getObjectManager(); |
| 126 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 127 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param string $maskedQuoteId |
| 132 | + * @return string |
| 133 | + */ |
| 134 | + private function getQuery(string $maskedQuoteId, string $sku, int $qty): string |
| 135 | + { |
| 136 | + return <<<QUERY |
| 137 | +mutation { |
| 138 | + addVirtualProductsToCart(input: { |
| 139 | + cart_id: "{$maskedQuoteId}", |
| 140 | + cartItems: [ |
| 141 | + { |
| 142 | + data: { |
| 143 | + qty: $qty |
| 144 | + sku: "$sku" |
| 145 | + } |
| 146 | + } |
| 147 | + ] |
| 148 | + }) { |
| 149 | + cart { |
| 150 | + items { |
| 151 | + id |
| 152 | + qty |
| 153 | + product { |
| 154 | + sku |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | +QUERY; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Retrieve customer authorization headers |
| 165 | + * |
| 166 | + * @param string $username |
| 167 | + * @param string $password |
| 168 | + * @return array |
| 169 | + * @throws AuthenticationException |
| 170 | + */ |
| 171 | + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array |
| 172 | + { |
| 173 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 174 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 175 | + return $headerMap; |
| 176 | + } |
| 177 | +} |
0 commit comments