|
| 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\Checkout\Controller\Cart; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 13 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 14 | +use Magento\Framework\Data\Form\FormKey; |
| 15 | +use Magento\Framework\Message\MessageInterface; |
| 16 | +use Magento\Quote\Model\Quote\Item as QuoteItem; |
| 17 | +use Magento\TestFramework\TestCase\AbstractController; |
| 18 | + |
| 19 | +/** |
| 20 | + * Integration tests for \Magento\Checkout\Controller\Cart\UpdateItemOptions class. |
| 21 | + */ |
| 22 | +class UpdateItemOptionsTest extends AbstractController |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var FormKey |
| 26 | + */ |
| 27 | + private $formKey; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var CheckoutSession |
| 31 | + */ |
| 32 | + private $checkoutSession; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ProductRepositoryInterface |
| 36 | + */ |
| 37 | + private $productRepository; |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritDoc |
| 41 | + */ |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + parent::setUp(); |
| 45 | + $this->formKey = $this->_objectManager->get(FormKey::class); |
| 46 | + $this->checkoutSession = $this->_objectManager->get(CheckoutSession::class); |
| 47 | + $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Tests that product is successfully updated in the shopping cart. |
| 52 | + * |
| 53 | + * @magentoAppArea frontend |
| 54 | + * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php |
| 55 | + */ |
| 56 | + public function testUpdateProductOptionsInQuote() |
| 57 | + { |
| 58 | + $product = $this->productRepository->get('simple'); |
| 59 | + $quoteItem = $this->checkoutSession->getQuote()->getItemByProduct($product); |
| 60 | + $postData = $this->preparePostData($product, $quoteItem); |
| 61 | + $this->dispatchUpdateItemOptionsRequest($postData); |
| 62 | + $this->assertTrue($this->getResponse()->isRedirect()); |
| 63 | + $this->assertRedirect($this->stringContains('/checkout/cart/')); |
| 64 | + $message = (string)__( |
| 65 | + '%1 was updated in your shopping cart.', |
| 66 | + $product->getName() |
| 67 | + ); |
| 68 | + $this->assertSessionMessages( |
| 69 | + $this->containsEqual($message), |
| 70 | + MessageInterface::TYPE_SUCCESS |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Tests that product can't be updated with an empty shopping cart. |
| 76 | + * |
| 77 | + * @magentoAppArea frontend |
| 78 | + * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php |
| 79 | + */ |
| 80 | + public function testUpdateProductOptionsWithEmptyQuote() |
| 81 | + { |
| 82 | + $product = $this->productRepository->get('simple'); |
| 83 | + $quoteItem = $this->checkoutSession->getQuote()->getItemByProduct($product); |
| 84 | + $postData = $this->preparePostData($product, $quoteItem); |
| 85 | + $this->checkoutSession->clearQuote(); |
| 86 | + $this->dispatchUpdateItemOptionsRequest($postData); |
| 87 | + $this->assertTrue($this->getResponse()->isRedirect()); |
| 88 | + $this->assertRedirect($this->stringContains('/checkout/cart/')); |
| 89 | + $message = (string)__('The quote item isn't found. Verify the item and try again.'); |
| 90 | + $this->assertSessionMessages( |
| 91 | + $this->containsEqual($message), |
| 92 | + MessageInterface::TYPE_ERROR |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Prepare post data for the request. |
| 98 | + * |
| 99 | + * @param ProductInterface $product |
| 100 | + * @param QuoteItem|bool $quoteItem |
| 101 | + * @return array |
| 102 | + */ |
| 103 | + private function preparePostData(ProductInterface $product, $quoteItem): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + 'product' => $product->getId(), |
| 107 | + 'selected_configurable_option' => '', |
| 108 | + 'related_product' => '', |
| 109 | + 'item' => $quoteItem->getId(), |
| 110 | + 'form_key' => $this->formKey->getFormKey(), |
| 111 | + 'qty' => '2', |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Perform request for updating product options in a quote item. |
| 117 | + * |
| 118 | + * @param array $postData |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + private function dispatchUpdateItemOptionsRequest(array $postData): void |
| 122 | + { |
| 123 | + $this->getRequest()->setPostValue($postData); |
| 124 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 125 | + $this->dispatch('checkout/cart/updateItemOptions/id/' . $postData['item']); |
| 126 | + } |
| 127 | +} |
0 commit comments