Skip to content

Commit 35ed234

Browse files
committed
Merge remote-tracking branch 'origin/MC-32805' into 2.4-develop-pr134
2 parents ab226a4 + 025c8c6 commit 35ed234

File tree

2 files changed

+154
-17
lines changed

2 files changed

+154
-17
lines changed

app/code/Magento/Checkout/Controller/Cart/UpdateItemOptions.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76

87
namespace Magento\Checkout\Controller\Cart;
98

9+
use Magento\Checkout\Controller\Cart;
10+
use Magento\Checkout\Helper\Cart as CartHelper;
1011
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
12+
use Magento\Framework\Controller\Result\Redirect;
13+
use Magento\Framework\DataObject;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Locale\ResolverInterface;
16+
use Psr\Log\LoggerInterface;
1117

12-
class UpdateItemOptions extends \Magento\Checkout\Controller\Cart implements HttpPostActionInterface
18+
/**
19+
* Process updating product options in a cart item.
20+
*/
21+
class UpdateItemOptions extends Cart implements HttpPostActionInterface
1322
{
1423
/**
15-
* Update product configuration for a cart item
24+
* Update product configuration for a cart item.
1625
*
17-
* @return \Magento\Framework\Controller\Result\Redirect
26+
* @return Redirect
1827
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1928
* @SuppressWarnings(PHPMD.NPathComplexity)
2029
*/
@@ -28,27 +37,27 @@ public function execute()
2837
}
2938
try {
3039
if (isset($params['qty'])) {
31-
$filter = new \Zend_Filter_LocalizedToNormalized(
32-
['locale' => $this->_objectManager->get(
33-
\Magento\Framework\Locale\ResolverInterface::class
34-
)->getLocale()]
40+
$inputFilter = new \Zend_Filter_LocalizedToNormalized(
41+
[
42+
'locale' => $this->_objectManager->get(ResolverInterface::class)->getLocale(),
43+
]
3544
);
36-
$params['qty'] = $filter->filter($params['qty']);
45+
$params['qty'] = $inputFilter->filter($params['qty']);
3746
}
3847

3948
$quoteItem = $this->cart->getQuote()->getItemById($id);
4049
if (!$quoteItem) {
41-
throw new \Magento\Framework\Exception\LocalizedException(
50+
throw new LocalizedException(
4251
__("The quote item isn't found. Verify the item and try again.")
4352
);
4453
}
4554

46-
$item = $this->cart->updateItem($id, new \Magento\Framework\DataObject($params));
55+
$item = $this->cart->updateItem($id, new DataObject($params));
4756
if (is_string($item)) {
48-
throw new \Magento\Framework\Exception\LocalizedException(__($item));
57+
throw new LocalizedException(__($item));
4958
}
5059
if ($item->getHasError()) {
51-
throw new \Magento\Framework\Exception\LocalizedException(__($item->getMessage()));
60+
throw new LocalizedException(__($item->getMessage()));
5261
}
5362

5463
$related = $this->getRequest()->getParam('related_product');
@@ -73,7 +82,7 @@ public function execute()
7382
}
7483
return $this->_goBack($this->_url->getUrl('checkout/cart'));
7584
}
76-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
85+
} catch (LocalizedException $e) {
7786
if ($this->_checkoutSession->getUseNotice(true)) {
7887
$this->messageManager->addNoticeMessage($e->getMessage());
7988
} else {
@@ -87,14 +96,15 @@ public function execute()
8796
if ($url) {
8897
return $this->resultRedirectFactory->create()->setUrl($url);
8998
} else {
90-
$cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
91-
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($cartUrl));
99+
$cartUrl = $this->_objectManager->get(CartHelper::class)->getCartUrl();
100+
return $this->resultRedirectFactory->create()->setUrl($cartUrl);
92101
}
93102
} catch (\Exception $e) {
94103
$this->messageManager->addExceptionMessage($e, __('We can\'t update the item right now.'));
95-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
104+
$this->_objectManager->get(LoggerInterface::class)->critical($e);
96105
return $this->_goBack();
97106
}
107+
98108
return $this->resultRedirectFactory->create()->setPath('*/*');
99109
}
100110
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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&#039;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

Comments
 (0)