|
8 | 8 | use Magento\Catalog\Model\Product; |
9 | 9 | use Magento\CatalogInventory\Api\StockRegistryInterface; |
10 | 10 | use Magento\CatalogInventory\Model\Stock; |
| 11 | +use Magento\ConfigurableProduct\Api\CartItemRepositoryTest as ConfigurableCartItemRepositoryTest; |
| 12 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
11 | 14 | use Magento\Quote\Model\Quote; |
12 | 15 | use Magento\Quote\Model\QuoteIdMask; |
13 | 16 | use Magento\Quote\Model\QuoteIdMaskFactory; |
@@ -185,6 +188,136 @@ public function testRemoveItem() |
185 | 188 | $this->assertFalse($quote->hasProductId($productId)); |
186 | 189 | } |
187 | 190 |
|
| 191 | + /** |
| 192 | + * @param int $itemId |
| 193 | + * @param int $qty |
| 194 | + * @throws LocalizedException |
| 195 | + */ |
| 196 | + protected function updateStockForItem($itemId, $qty) |
| 197 | + { |
| 198 | + /** @var \Magento\CatalogInventory\Model\Stock\Status $stockStatus */ |
| 199 | + $stockStatus = $this->objectManager->create(\Magento\CatalogInventory\Model\Stock\Status::class); |
| 200 | + $stockStatus->load($itemId, 'product_id'); |
| 201 | + if (!$stockStatus->getProductId()) { |
| 202 | + $stockStatus->setProductId($itemId); |
| 203 | + } |
| 204 | + $stockStatus->setQty($qty); |
| 205 | + $stockStatus->setStockStatus(1); |
| 206 | + $stockStatus->save(); |
| 207 | + |
| 208 | + /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */ |
| 209 | + $stockItem = $this->objectManager->create(\Magento\CatalogInventory\Model\Stock\Item::class); |
| 210 | + $stockItem->load($itemId, 'product_id'); |
| 211 | + |
| 212 | + if (!$stockItem->getProductId()) { |
| 213 | + $stockItem->setProductId($itemId); |
| 214 | + } |
| 215 | + $stockItem->setUseConfigManageStock(1); |
| 216 | + $stockItem->setQty($qty); |
| 217 | + $stockItem->setIsQtyDecimal(0); |
| 218 | + $stockItem->setIsInStock(1); |
| 219 | + $stockItem->save(); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * @param $cartId |
| 224 | + * @param null $selectedOption |
| 225 | + * @return array |
| 226 | + */ |
| 227 | + protected function getRequestData($cartId, $selectedOption = null) |
| 228 | + { |
| 229 | + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ |
| 230 | + $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 231 | + $product = $productRepository->get(ConfigurableCartItemRepositoryTest::CONFIGURABLE_PRODUCT_SKU); |
| 232 | + |
| 233 | + $configurableProductOptions = $product->getExtensionAttributes()->getConfigurableProductOptions(); |
| 234 | + |
| 235 | + $optionKey = 0; |
| 236 | + if ($selectedOption && isset($options[$selectedOption])) { |
| 237 | + $optionKey = $selectedOption; |
| 238 | + } |
| 239 | + |
| 240 | + $attributeId = $configurableProductOptions[0]->getAttributeId(); |
| 241 | + $options = $configurableProductOptions[0]->getOptions(); |
| 242 | + $optionId = $options[$optionKey]['value_index']; |
| 243 | + |
| 244 | + return [ |
| 245 | + 'cartItem' => [ |
| 246 | + 'sku' => ConfigurableCartItemRepositoryTest::CONFIGURABLE_PRODUCT_SKU, |
| 247 | + 'qty' => 1, |
| 248 | + 'quote_id' => $cartId, |
| 249 | + 'product_option' => [ |
| 250 | + 'extension_attributes' => [ |
| 251 | + 'configurable_item_options' => [ |
| 252 | + [ |
| 253 | + 'option_id' => $attributeId, |
| 254 | + 'option_value' => $optionId |
| 255 | + ] |
| 256 | + ] |
| 257 | + ] |
| 258 | + ] |
| 259 | + ] |
| 260 | + ]; |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php |
| 265 | + */ |
| 266 | + public function testAddAndUpdateConfigurableProductInGuestCart() |
| 267 | + { |
| 268 | + $qty = 4; |
| 269 | + $this->updateStockForItem(10, 100); |
| 270 | + $this->updateStockForItem(20, 100); |
| 271 | + |
| 272 | + /** @var \Magento\Quote\Model\Quote $quote */ |
| 273 | + $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class); |
| 274 | + $quote->load('test_cart_with_configurable', 'reserved_order_id'); |
| 275 | + $quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)->create(); |
| 276 | + $quoteIdMask->load($quote->getId(), 'quote_id'); |
| 277 | + $cartId = $quoteIdMask->getMaskedId(); |
| 278 | + $items = $quote->getAllItems(); |
| 279 | + $this->assertGreaterThan(0, count($items)); |
| 280 | + |
| 281 | + /** @var \Magento\Quote\Model\ResourceModel\Quote\Item|null $item */ |
| 282 | + $item = null; |
| 283 | + /** @var \Magento\Quote\Model\ResourceModel\Quote\Item $quoteItem */ |
| 284 | + foreach ($items as $quoteItem) { |
| 285 | + if ($quoteItem->getProductType() == Configurable::TYPE_CODE) { |
| 286 | + $item = $quoteItem; |
| 287 | + break; |
| 288 | + } |
| 289 | + } |
| 290 | + $this->assertNotNull($item); |
| 291 | + $this->assertNotNull($item->getId()); |
| 292 | + $this->assertEquals(Configurable::TYPE_CODE, $item->getProductType()); |
| 293 | + |
| 294 | + $requestData = $this->getRequestData($cartId, 1); |
| 295 | + $requestData['cartItem']['qty'] = $qty; |
| 296 | + $requestData['cartItem']['item_id'] = $item->getId(); |
| 297 | + |
| 298 | + $serviceInfo = [ |
| 299 | + 'rest' => [ |
| 300 | + 'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $item->getId(), |
| 301 | + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT |
| 302 | + ], |
| 303 | + 'soap' => [ |
| 304 | + 'service' => self::SERVICE_NAME, |
| 305 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 306 | + 'operation' => self::SERVICE_NAME . 'Save', |
| 307 | + ], |
| 308 | + ]; |
| 309 | + $response = $this->_webApiCall($serviceInfo, $requestData); |
| 310 | + |
| 311 | + $this->assertNotNull($response['item_id']); |
| 312 | + $this->assertEquals(Configurable::TYPE_CODE, $response['product_type']); |
| 313 | + $this->assertEquals($quote->getId(), $response['quote_id']); |
| 314 | + $this->assertEquals($qty, $response['qty']); |
| 315 | + $this->assertEquals( |
| 316 | + $response['product_option']['extension_attributes']['configurable_item_options'][0], |
| 317 | + $requestData['cartItem']['product_option']['extension_attributes']['configurable_item_options'][0] |
| 318 | + ); |
| 319 | + } |
| 320 | + |
188 | 321 | /** |
189 | 322 | * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php |
190 | 323 | * @param array $stockData |
|
0 commit comments