|
| 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\Sales; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test Reorder with different types of product. |
| 18 | + */ |
| 19 | +class ReorderMultipleProductsTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Customer Id |
| 23 | + */ |
| 24 | + private const CUSTOMER_ID = 1; |
| 25 | + |
| 26 | + /** |
| 27 | + * Order Number |
| 28 | + */ |
| 29 | + private const ORDER_NUMBER = '100000001'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Customer email |
| 33 | + */ |
| 34 | + private const CUSTOMER_EMAIL = 'customer@example.com'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CustomerTokenServiceInterface |
| 38 | + */ |
| 39 | + private $customerTokenService; |
| 40 | + |
| 41 | + /** |
| 42 | + * @inheritDoc |
| 43 | + */ |
| 44 | + protected function setUp() |
| 45 | + { |
| 46 | + parent::setUp(); |
| 47 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 48 | + |
| 49 | + // be sure previous tests didn't left customer quote |
| 50 | + /** @var CartRepositoryInterface $cartRepository */ |
| 51 | + $cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class); |
| 52 | + try { |
| 53 | + $quote = $cartRepository->getForCustomer(self::CUSTOMER_ID); |
| 54 | + $cartRepository->delete($quote); |
| 55 | + } catch (NoSuchEntityException $e) { |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @magentoApiDataFixture Magento/Sales/_files/order_with_different_types_of_product.php |
| 61 | + */ |
| 62 | + public function testMutplipleProducts() |
| 63 | + { |
| 64 | + $response = $this->makeReorderForDefaultCustomer(self::ORDER_NUMBER); |
| 65 | + |
| 66 | + $expectedResponse = [ |
| 67 | + 'userInputErrors' => [], |
| 68 | + 'cart' => [ |
| 69 | + 'email' => 'customer@example.com', |
| 70 | + 'total_quantity' => 5, |
| 71 | + 'items' => [ |
| 72 | + [ |
| 73 | + 'quantity' => 1, |
| 74 | + 'product' => [ |
| 75 | + 'sku' => 'simple-2', |
| 76 | + ], |
| 77 | + ], |
| 78 | + [ |
| 79 | + 'quantity' => 1, |
| 80 | + 'product' => [ |
| 81 | + 'sku' => 'configurable-1', |
| 82 | + ], |
| 83 | + 'configurable_options' => [ |
| 84 | + [ |
| 85 | + 'option_label' => 'Test Configurable', |
| 86 | + 'value_label' => 'Option 1', |
| 87 | + ], |
| 88 | + ], |
| 89 | + ], |
| 90 | + [ |
| 91 | + 'quantity' => 1, |
| 92 | + 'product' => |
| 93 | + [ |
| 94 | + 'sku' => 'virtual_product', |
| 95 | + ], |
| 96 | + ], |
| 97 | + [ |
| 98 | + 'quantity' => 1, |
| 99 | + 'product' => [ |
| 100 | + 'sku' => 'bundle-product-radio-required-option', |
| 101 | + ], |
| 102 | + 'bundle_options' => [ |
| 103 | + [ |
| 104 | + 'label' => 'Radio Options', |
| 105 | + 'type' => 'radio', |
| 106 | + 'values' => [ |
| 107 | + [ |
| 108 | + 'label' => 'Simple Product', |
| 109 | + 'price' => 10, |
| 110 | + 'quantity' => 1, |
| 111 | + ], |
| 112 | + ], |
| 113 | + ], |
| 114 | + ], |
| 115 | + ], |
| 116 | + [ |
| 117 | + 'quantity' => 1, |
| 118 | + 'product' => [ |
| 119 | + 'sku' => 'downloadable-product', |
| 120 | + ], |
| 121 | + 'links' => [ |
| 122 | + [ |
| 123 | + 'title' => 'Downloadable Product Link', |
| 124 | + ], |
| 125 | + ], |
| 126 | + ], |
| 127 | + ], |
| 128 | + ], |
| 129 | + ]; |
| 130 | + $this->assertResponseFields($response['reorderItems'] ?? [], $expectedResponse); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param string $email |
| 135 | + * @param string $password |
| 136 | + * @return array |
| 137 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 138 | + */ |
| 139 | + private function getCustomerAuthHeaders(string $email, string $password): array |
| 140 | + { |
| 141 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); |
| 142 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Execute GraphQL Mutation for default customer (make reorder) |
| 147 | + * |
| 148 | + * @param string $orderId |
| 149 | + * @return array|bool|float|int|string |
| 150 | + * @throws \Exception |
| 151 | + */ |
| 152 | + private function makeReorderForDefaultCustomer(string $orderId = self::ORDER_NUMBER) |
| 153 | + { |
| 154 | + $query = $this->getQuery($orderId); |
| 155 | + $currentPassword = 'password'; |
| 156 | + return $this->graphQlMutation( |
| 157 | + $query, |
| 158 | + [], |
| 159 | + '', |
| 160 | + $this->getCustomerAuthHeaders(self::CUSTOMER_EMAIL, $currentPassword) |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param string $orderNumber |
| 166 | + * @return string |
| 167 | + */ |
| 168 | + protected function getQuery($orderNumber): string |
| 169 | + { |
| 170 | + return |
| 171 | + <<<MUTATION |
| 172 | +mutation { |
| 173 | + reorderItems(orderNumber: "{$orderNumber}") { |
| 174 | + userInputErrors { |
| 175 | + path |
| 176 | + code |
| 177 | + message |
| 178 | + } |
| 179 | + cart { |
| 180 | + email |
| 181 | + total_quantity |
| 182 | + items { |
| 183 | + quantity |
| 184 | + product { |
| 185 | + sku |
| 186 | + } |
| 187 | + ... on ConfigurableCartItem { |
| 188 | + configurable_options { |
| 189 | + option_label |
| 190 | + value_label |
| 191 | + } |
| 192 | + } |
| 193 | + ... on BundleCartItem { |
| 194 | + bundle_options { |
| 195 | + label |
| 196 | + type |
| 197 | + values { |
| 198 | + label |
| 199 | + price |
| 200 | + quantity |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + ... on DownloadableCartItem { |
| 205 | + links { |
| 206 | + title |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
| 213 | +MUTATION; |
| 214 | + } |
| 215 | +} |
0 commit comments