Skip to content

Commit 4021e52

Browse files
Merge pull request #7544 from magento-cia/2.4.5-bugfixes-032922
Bugfixes
2 parents f605b71 + 13f0588 commit 4021e52

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

app/code/Magento/Checkout/Controller/Sidebar/UpdateItemQty.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Checkout\Controller\Sidebar;
79

810
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
911
use Magento\Checkout\Model\Sidebar;
1012
use Magento\Framework\App\Action\Action;
1113
use Magento\Framework\App\Action\Context;
14+
use Magento\Framework\App\Action\HttpPostActionInterface;
1215
use Magento\Framework\App\ObjectManager;
1316
use Magento\Framework\App\Response\Http;
1417
use Magento\Framework\Exception\LocalizedException;
1518
use Magento\Framework\Json\Helper\Data;
1619
use Psr\Log\LoggerInterface;
1720

18-
class UpdateItemQty extends Action
21+
/**
22+
* Class used to update item quantity.
23+
*/
24+
class UpdateItemQty extends Action implements HttpPostActionInterface
1925
{
2026
/**
2127
* @var Sidebar
@@ -61,12 +67,18 @@ public function __construct(
6167
}
6268

6369
/**
70+
* Action for Quantity update
71+
*
6472
* @return $this
6573
*/
6674
public function execute()
6775
{
6876
$itemId = (int)$this->getRequest()->getParam('item_id');
69-
$itemQty = $this->getRequest()->getParam('item_qty') * 1;
77+
$itemQty = (int)$this->getRequest()->getParam('item_qty');
78+
79+
if ($itemQty <= 0) {
80+
return $this->jsonResponse(__('Invalid Item Quantity Requested.'));
81+
}
7082
$itemQty = $this->quantityProcessor->prepareQuantity($itemQty);
7183

7284
try {

app/code/Magento/Checkout/Test/Unit/Controller/Sidebar/UpdateItemQtyTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use PHPUnit\Framework\TestCase;
2020
use Psr\Log\LoggerInterface;
2121

22+
/**
23+
* Class used to execute test cases for update item quantity
24+
*/
2225
class UpdateItemQtyTest extends TestCase
2326
{
2427
/**
@@ -244,4 +247,36 @@ public function testExecuteWithException(): void
244247

245248
$this->assertEquals('json represented', $this->updateItemQty->execute());
246249
}
250+
251+
/**
252+
* @return void
253+
*/
254+
public function testExecuteWithInvalidItemQty(): void
255+
{
256+
$error = [
257+
'success' => false,
258+
'error_message' => 'Invalid Item Quantity Requested.'
259+
];
260+
$jsonResult = json_encode($error);
261+
$this->requestMock
262+
->method('getParam')
263+
->withConsecutive(['item_id', null], ['item_qty', null])
264+
->willReturnOnConsecutiveCalls('1', '{{7+2}}');
265+
266+
$this->sidebarMock->expects($this->once())
267+
->method('getResponseData')
268+
->with('Invalid Item Quantity Requested.')
269+
->willReturn($error);
270+
271+
$this->jsonHelperMock->expects($this->once())
272+
->method('jsonEncode')
273+
->with($error)
274+
->willReturn($jsonResult);
275+
276+
$this->responseMock->expects($this->once())
277+
->method('representJson')
278+
->willReturn($jsonResult);
279+
280+
$this->assertEquals($jsonResult, $this->updateItemQty->execute());
281+
}
247282
}

app/code/Magento/Checkout/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,4 @@ Payment,Payment
186186
"Show Cross-sell Items in the Shopping Cart","Show Cross-sell Items in the Shopping Cart"
187187
"You added %1 to your <a href=""%2"">shopping cart</a>.","You added %1 to your <a href=""%2"">shopping cart</a>."
188188
"The shipping method is missing. Select the shipping method and try again.","The shipping method is missing. Select the shipping method and try again."
189+
"Invalid Item Quantity Requested.","Invalid Item Quantity Requested."

0 commit comments

Comments
 (0)