Skip to content

Commit 1f99ad8

Browse files
committed
ACP2E-2267: The requested qty is not available message issues
- probing solution
1 parent c3aa0a5 commit 1f99ad8

File tree

4 files changed

+15
-129
lines changed

4 files changed

+15
-129
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,10 +132,6 @@ define([
132132
$('body').trigger(self.options.processStop);
133133
}
134134

135-
if (res.messages) {
136-
$(self.options.messagesSelector).html(res.messages);
137-
}
138-
139135
if (res.backUrl) {
140136
eventData = {
141137
'form': form,
@@ -157,6 +153,10 @@ define([
157153
return;
158154
}
159155

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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,6 @@ protected function goBack($backUrl = null, $product = null)
216216
];
217217
}
218218
}
219-
if (!empty($this->messageManager->getMessages()->getErrors())) {
220-
$result['messages'] = $this->messageManager->getMessages()->getErrors();
221-
}
222219

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

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

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

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

10-
use Magento\Catalog\Api\ProductRepositoryInterface;
11-
use Magento\Catalog\Model\Product;
1210
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;
1611
use Magento\Framework\App\RequestInterface;
17-
use Magento\Framework\App\ResponseInterface;
1812
use Magento\Framework\Controller\Result\Redirect;
1913
use Magento\Framework\Controller\Result\RedirectFactory;
2014
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;
2415
use Magento\Framework\Message\ManagerInterface;
25-
use Magento\Framework\Message\MessageInterface;
26-
use Magento\Framework\ObjectManagerInterface;
27-
use Magento\Framework\Phrase;
2816
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
29-
use Magento\Store\Api\Data\StoreInterface;
30-
use Magento\Store\Model\StoreManagerInterface;
3117
use PHPUnit\Framework\MockObject\MockObject;
3218
use PHPUnit\Framework\TestCase;
3319

34-
/**
35-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36-
*/
3720
class AddTest extends TestCase
3821
{
3922
/**
@@ -66,31 +49,6 @@ class AddTest extends TestCase
6649
*/
6750
private $cartAdd;
6851

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-
9452
/**
9553
* Init mocks for tests.
9654
*
@@ -107,19 +65,10 @@ protected function setUp(): void
10765
->getMock();
10866
$this->request = $this->getMockBuilder(RequestInterface::class)
10967
->disableOriginalConstructor()
110-
->addMethods(['isAjax'])
111-
->getMockForAbstractClass();
68+
->getmock();
11269
$this->messageManager = $this->getMockBuilder(ManagerInterface::class)
11370
->disableOriginalConstructor()
11471
->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();
12372

12473
$this->objectManagerHelper = new ObjectManagerHelper($this);
12574
$this->cartAdd = $this->objectManagerHelper->getObject(
@@ -128,12 +77,7 @@ protected function setUp(): void
12877
'_formKeyValidator' => $this->formKeyValidator,
12978
'resultRedirectFactory' => $this->resultRedirectFactory,
13079
'_request' => $this->request,
131-
'messageManager' => $this->messageManager,
132-
'cart' => $this->cart,
133-
'storeManager' => $this->storeManager,
134-
'productRepository' => $this->productRepository,
135-
'context' => $this->context,
136-
'checkoutSession' => $this->checkoutSession
80+
'messageManager' => $this->messageManager
13781
]
13882
);
13983
}
@@ -156,63 +100,4 @@ public function testExecute()
156100
$redirect->expects($this->once())->method('setPath')->with($path)->willReturnSelf();
157101
$this->assertEquals($redirect, $this->cartAdd->execute());
158102
}
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-
}
218103
}

app/code/Magento/Theme/view/frontend/web/js/view/messages.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ define([
4444
disposableCustomerData: 'messages'
4545
});
4646

47-
// Force to clean obsolete messages
48-
if (!_.isEmpty(this.messages().messages)) {
49-
customerData.set('messages', {});
50-
}
51-
5247
$.mage.cookies.set('mage-messages', '', {
5348
samesite: 'strict',
5449
domain: ''
@@ -62,6 +57,15 @@ define([
6257
* @return {String}
6358
*/
6459
prepareMessageForHtml: function (message) {
60+
if (!_.isEmpty(this.messages().messages)) {
61+
let messages = [];
62+
for (let i =0; i < this.messages().messages.length; i++) {
63+
if (this.messages().messages[i] !== message) {
64+
messages.push(this.messages().messages[i]);
65+
}
66+
}
67+
customerData.set('messages', messages);
68+
}
6569
return escaper.escapeHtml(message, this.allowedTags);
6670
}
6771
});

0 commit comments

Comments
 (0)