Skip to content

Commit fd20147

Browse files
committed
ACP2E-2267: The requested qty is not available message issues
- implemented solution - added unit test
1 parent 1819863 commit fd20147

File tree

3 files changed

+127
-10
lines changed

3 files changed

+127
-10
lines changed

app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ define([
132132
$('body').trigger(self.options.processStop);
133133
}
134134

135+
if (res.messages) {
136+
$(self.options.messagesSelector).html(res.messages);
137+
}
138+
135139
if (res.backUrl) {
136140
eventData = {
137141
'form': form,
@@ -153,10 +157,6 @@ define([
153157
return;
154158
}
155159

156-
if (res.messages) {
157-
$(self.options.messagesSelector).html(res.messages);
158-
}
159-
160160
if (res.minicart) {
161161
$(self.options.minicartSelector).replaceWith(res.minicart);
162162
$(self.options.minicartSelector).trigger('contentUpdated');

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Controller\ResultInterface;
1616
use Magento\Framework\Exception\NoSuchEntityException;
1717
use Magento\Framework\Filter\LocalizedToNormalized;
18+
use Magento\Store\Model\StoreManagerInterface;
1819

1920
/**
2021
* Controller for processing add to cart action.
@@ -52,7 +53,7 @@ public function __construct(
5253
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
5354
CustomerCart $cart,
5455
ProductRepositoryInterface $productRepository,
55-
?RequestQuantityProcessor $quantityProcessor = null
56+
?RequestQuantityProcessor $quantityProcessor = null,
5657
) {
5758
parent::__construct(
5859
$context,
@@ -76,9 +77,7 @@ protected function _initProduct()
7677
{
7778
$productId = (int)$this->getRequest()->getParam('product');
7879
if ($productId) {
79-
$storeId = $this->_objectManager->get(
80-
\Magento\Store\Model\StoreManagerInterface::class
81-
)->getStore()->getId();
80+
$storeId = $this->_storeManager->getStore()->getId();
8281
try {
8382
return $this->productRepository->getById($productId, false, $storeId);
8483
} catch (NoSuchEntityException $e) {
@@ -217,6 +216,9 @@ protected function goBack($backUrl = null, $product = null)
217216
];
218217
}
219218
}
219+
if (!empty($this->messageManager->getMessages()->getErrors())) {
220+
$result['messages'] = $this->messageManager->getMessages()->getErrors();
221+
}
220222

221223
$this->getResponse()->representJson(
222224
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)

app/code/Magento/Checkout/Test/Unit/Controller/Cart/AddTest.php

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,33 @@
77

88
namespace Magento\Checkout\Test\Unit\Controller\Cart;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
1012
use Magento\Checkout\Controller\Cart\Add;
13+
use Magento\Checkout\Model\Cart as CustomerCart;
14+
use Magento\Checkout\Model\Session;
15+
use Magento\Framework\App\Action\Context;
1116
use Magento\Framework\App\RequestInterface;
17+
use Magento\Framework\App\ResponseInterface;
1218
use Magento\Framework\Controller\Result\Redirect;
1319
use Magento\Framework\Controller\Result\RedirectFactory;
1420
use Magento\Framework\Data\Form\FormKey\Validator;
21+
use Magento\Framework\Exception\LocalizedException;
22+
use Magento\Framework\Json\Helper\Data;
23+
use Magento\Framework\Message\Collection;
1524
use Magento\Framework\Message\ManagerInterface;
25+
use Magento\Framework\Message\MessageInterface;
26+
use Magento\Framework\ObjectManagerInterface;
27+
use Magento\Framework\Phrase;
1628
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
29+
use Magento\Store\Api\Data\StoreInterface;
30+
use Magento\Store\Model\StoreManagerInterface;
1731
use PHPUnit\Framework\MockObject\MockObject;
1832
use PHPUnit\Framework\TestCase;
1933

34+
/**
35+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36+
*/
2037
class AddTest extends TestCase
2138
{
2239
/**
@@ -49,6 +66,31 @@ class AddTest extends TestCase
4966
*/
5067
private $cartAdd;
5168

69+
/**
70+
* @var CustomerCart|MockObject
71+
*/
72+
private CustomerCart $cart;
73+
74+
/**
75+
* @var StoreManagerInterface|StoreManagerInterface&MockObject|MockObject
76+
*/
77+
private StoreManagerInterface $storeManager;
78+
79+
/**
80+
* @var ProductRepositoryInterface
81+
*/
82+
private ProductRepositoryInterface $productRepository;
83+
84+
/**
85+
* @var Context|Context&MockObject|MockObject
86+
*/
87+
private Context $context;
88+
89+
/**
90+
* @var Session|Session&MockObject|MockObject
91+
*/
92+
private Session $checkoutSession;
93+
5294
/**
5395
* Init mocks for tests.
5496
*
@@ -65,10 +107,19 @@ protected function setUp(): void
65107
->getMock();
66108
$this->request = $this->getMockBuilder(RequestInterface::class)
67109
->disableOriginalConstructor()
68-
->getmock();
110+
->addMethods(['isAjax'])
111+
->getMockForAbstractClass();
69112
$this->messageManager = $this->getMockBuilder(ManagerInterface::class)
70113
->disableOriginalConstructor()
71114
->getMockForAbstractClass();
115+
$this->cart = $this->createMock(CustomerCart::class);
116+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
117+
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);
118+
$this->context = $this->createMock(Context::class);
119+
$this->checkoutSession = $this->getMockBuilder(Session::class)
120+
->disableOriginalConstructor()
121+
->addMethods(['getRedirectUrl', 'getUseNotice'])
122+
->getMock();
72123

73124
$this->objectManagerHelper = new ObjectManagerHelper($this);
74125
$this->cartAdd = $this->objectManagerHelper->getObject(
@@ -77,7 +128,12 @@ protected function setUp(): void
77128
'_formKeyValidator' => $this->formKeyValidator,
78129
'resultRedirectFactory' => $this->resultRedirectFactory,
79130
'_request' => $this->request,
80-
'messageManager' => $this->messageManager
131+
'messageManager' => $this->messageManager,
132+
'cart' => $this->cart,
133+
'storeManager' => $this->storeManager,
134+
'productRepository' => $this->productRepository,
135+
'context' => $this->context,
136+
'checkoutSession' => $this->checkoutSession
81137
]
82138
);
83139
}
@@ -100,4 +156,63 @@ public function testExecute()
100156
$redirect->expects($this->once())->method('setPath')->with($path)->willReturnSelf();
101157
$this->assertEquals($redirect, $this->cartAdd->execute());
102158
}
159+
160+
/**
161+
* @return void
162+
*/
163+
public function testExecuteWithError(): void
164+
{
165+
$helper = $this->createMock(Data::class);
166+
$escaper = $this->createMock(\Magento\Framework\Escaper::class);
167+
$escaper->expects($this->once())->method('escapeHtml');
168+
$objectManager = $this->createMock(ObjectManagerInterface::class);
169+
$objectManager->expects($this->any())
170+
->method('get')
171+
->willReturnOnConsecutiveCalls($escaper, $helper);
172+
$this->context->expects($this->any())->method('getObjectManager')->willReturn($objectManager);
173+
$response = $this->getMockBuilder(ResponseInterface::class)
174+
->disableOriginalConstructor()
175+
->addMethods(['representJson'])
176+
->getMockForAbstractClass();
177+
$response->expects($this->once())->method('representJson');
178+
$this->context->expects($this->any())->method('getResponse')->willReturn($response);
179+
$this->formKeyValidator->expects($this->once())->method('validate')->with($this->request)->willReturn(true);
180+
$this->request->expects($this->exactly(2))
181+
->method('getParam')
182+
->willReturnOnConsecutiveCalls(1, [], []);
183+
$this->request->expects($this->once())
184+
->method('isAjax')
185+
->willReturn(true);
186+
$store = $this->createMock(StoreInterface::class);
187+
$store->expects($this->once())->method('getId');
188+
$this->storeManager->expects($this->once())->method('getStore')->willReturn($store);
189+
$product = $this->createMock(Product::class);
190+
$this->productRepository->expects($this->once())->method('getById')->willReturn($product);
191+
$this->cart->expects($this->once())
192+
->method('save')
193+
->willThrowException(new LocalizedException(new Phrase('error')));
194+
$this->messageManager->expects($this->once())->method('addErrorMessage');
195+
$message = $this->createMock(MessageInterface::class);
196+
$error = $this->createMock(Collection::class);
197+
$error->expects($this->exactly(2))->method('getErrors')->willReturn([$message]);
198+
$this->messageManager->expects($this->exactly(2))->method('getMessages')->willReturn($error);
199+
$this->checkoutSession->expects($this->once())->method('getRedirectUrl')->willReturn('redirect');
200+
201+
$cartAdd = $this->objectManagerHelper->getObject(
202+
Add::class,
203+
[
204+
'_formKeyValidator' => $this->formKeyValidator,
205+
'resultRedirectFactory' => $this->resultRedirectFactory,
206+
'_request' => $this->request,
207+
'messageManager' => $this->messageManager,
208+
'cart' => $this->cart,
209+
'storeManager' => $this->storeManager,
210+
'productRepository' => $this->productRepository,
211+
'context' => $this->context,
212+
'checkoutSession' => $this->checkoutSession
213+
]
214+
);
215+
216+
$cartAdd->execute();
217+
}
103218
}

0 commit comments

Comments
 (0)