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