Skip to content

Commit e8e284e

Browse files
MAGETWO-69375: Can't delete last item in cart if Minimum Order is Enable #6151 #9714
2 parents ef4b4b5 + bebc0e9 commit e8e284e

File tree

4 files changed

+57
-98
lines changed

4 files changed

+57
-98
lines changed

app/code/Magento/Quote/Model/QuoteValidator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
namespace Magento\Quote\Model;
88

9+
use Magento\Framework\Exception\LocalizedException;
910
use Magento\Quote\Model\Quote as QuoteEntity;
1011
use Magento\Directory\Model\AllowedCountries;
1112
use Magento\Framework\App\ObjectManager;
13+
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;
1214

1315
/**
1416
* @api
@@ -25,15 +27,25 @@ class QuoteValidator
2527
*/
2628
private $allowedCountryReader;
2729

30+
/**
31+
* @var OrderAmountValidationMessage
32+
*/
33+
private $minimumAmountMessage;
34+
2835
/**
2936
* QuoteValidator constructor.
3037
*
3138
* @param AllowedCountries|null $allowedCountryReader
39+
* @param OrderAmountValidationMessage|null $minimumAmountMessage
3240
*/
33-
public function __construct(AllowedCountries $allowedCountryReader = null)
34-
{
41+
public function __construct(
42+
AllowedCountries $allowedCountryReader = null,
43+
OrderAmountValidationMessage $minimumAmountMessage = null
44+
) {
3545
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
3646
->get(AllowedCountries::class);
47+
$this->minimumAmountMessage = $minimumAmountMessage ?: ObjectManager::getInstance()
48+
->get(OrderAmountValidationMessage::class);
3749
}
3850

3951
/**
@@ -98,6 +110,9 @@ public function validateBeforeSubmit(QuoteEntity $quote)
98110
if (!$quote->getPayment()->getMethod()) {
99111
throw new \Magento\Framework\Exception\LocalizedException(__('Please select a valid payment method.'));
100112
}
113+
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
114+
throw new LocalizedException($this->minimumAmountMessage->getMessage());
115+
}
101116

102117
return $this;
103118
}

app/code/Magento/Quote/Model/ShippingAddressManagement.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ class ShippingAddressManagement implements \Magento\Quote\Model\ShippingAddressM
5252
*/
5353
protected $totalsCollector;
5454

55-
/**
56-
* @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
57-
*/
58-
private $minimumAmountErrorMessage;
59-
6055
/**
6156
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
6257
* @param QuoteAddressValidator $addressValidator
@@ -117,10 +112,6 @@ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $addres
117112
$address->setSaveInAddressBook($saveInAddressBook);
118113
$address->setCollectShippingRates(true);
119114

120-
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
121-
throw new InputException($this->getMinimumAmountErrorMessage()->getMessage());
122-
}
123-
124115
try {
125116
$address->save();
126117
} catch (\Exception $e) {
@@ -145,19 +136,4 @@ public function get($cartId)
145136
/** @var \Magento\Quote\Model\Quote\Address $address */
146137
return $quote->getShippingAddress();
147138
}
148-
149-
/**
150-
* @return \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
151-
* @deprecated
152-
*/
153-
private function getMinimumAmountErrorMessage()
154-
{
155-
if ($this->minimumAmountErrorMessage === null) {
156-
$objectManager = ObjectManager::getInstance();
157-
$this->minimumAmountErrorMessage = $objectManager->get(
158-
\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class
159-
);
160-
}
161-
return $this->minimumAmountErrorMessage;
162-
}
163139
}

app/code/Magento/Quote/Test/Unit/Model/QuoteValidatorTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Quote\Model\Quote\Address;
1010
use Magento\Quote\Model\Quote\Payment;
1111
use Magento\Quote\Model\QuoteValidator;
12+
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;
1213

1314
/**
1415
* Class QuoteValidatorTest
@@ -30,6 +31,11 @@ class QuoteValidatorTest extends \PHPUnit_Framework_TestCase
3031
*/
3132
private $allowedCountryReader;
3233

34+
/**
35+
* @var OrderAmountValidationMessage|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $orderAmountValidationMessage;
38+
3339
/**
3440
* @return void
3541
*/
@@ -38,8 +44,14 @@ protected function setUp()
3844
$this->allowedCountryReader = $this->getMockBuilder(AllowedCountries::class)
3945
->disableOriginalConstructor()
4046
->getMock();
47+
$this->orderAmountValidationMessage = $this->getMockBuilder(OrderAmountValidationMessage::class)
48+
->disableOriginalConstructor()
49+
->getMock();
4150

42-
$this->quoteValidator = new \Magento\Quote\Model\QuoteValidator($this->allowedCountryReader);
51+
$this->quoteValidator = new \Magento\Quote\Model\QuoteValidator(
52+
$this->allowedCountryReader,
53+
$this->orderAmountValidationMessage
54+
);
4355

4456
$this->quoteMock = $this->getMock(
4557
\Magento\Quote\Model\Quote::class,
@@ -51,6 +63,8 @@ protected function setUp()
5163
'setHasError',
5264
'addMessage',
5365
'isVirtual',
66+
'validateMinimumAmount',
67+
'getIsMultiShipping',
5468
'__wakeup'
5569
],
5670
[],
@@ -185,6 +199,31 @@ public function testValidateBeforeSubmitThrowsExceptionIfPaymentMethodIsNotSelec
185199
$this->quoteValidator->validateBeforeSubmit($this->quoteMock);
186200
}
187201

202+
/**
203+
* @expectedException \Magento\Framework\Exception\LocalizedException
204+
* @expectedExceptionMessage Minimum Order Amount Exceeded.
205+
*/
206+
public function testValidateBeforeSubmitThrowsExceptionIfMinimumOrderAmount()
207+
{
208+
$paymentMock = $this->getMock(\Magento\Quote\Model\Quote\Payment::class, [], [], '', false);
209+
$paymentMock->expects($this->once())->method('getMethod')->willReturn('checkmo');
210+
211+
$billingAddressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
212+
$billingAddressMock->expects($this->any())->method('validate')->willReturn(true);
213+
214+
$this->quoteMock->expects($this->any())->method('getBillingAddress')->willReturn($billingAddressMock);
215+
$this->quoteMock->expects($this->any())->method('getPayment')->willReturn($paymentMock);
216+
$this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(true);
217+
218+
$this->quoteMock->expects($this->any())->method('getIsMultiShipping')->willReturn(false);
219+
$this->quoteMock->expects($this->any())->method('validateMinimumAmount')->willReturn(false);
220+
221+
$this->orderAmountValidationMessage->expects($this->once())->method('getMessage')
222+
->willReturn(__("Minimum Order Amount Exceeded."));
223+
224+
$this->quoteValidator->validateBeforeSubmit($this->quoteMock);
225+
}
226+
188227
/**
189228
* Test case when country id not present in allowed countries list.
190229
*

app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ protected function setUp()
116116
'addressRepository' => $this->addressRepository
117117
]
118118
);
119-
$this->objectManager->setBackwardCompatibleProperty(
120-
$this->service,
121-
'minimumAmountErrorMessage',
122-
$this->amountErrorMessageMock
123-
);
124119
}
125120

126121
/**
@@ -190,8 +185,6 @@ public function testSetAddress()
190185
->method('setCollectShippingRates')
191186
->with(true)
192187
->willReturnSelf();
193-
$quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false);
194-
$quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true);
195188

196189
$this->quoteAddressMock->expects($this->once())->method('save')->willReturnSelf();
197190
$this->quoteAddressMock->expects($this->once())->method('getId')->will($this->returnValue($addressId));
@@ -277,70 +270,6 @@ public function testSetAddressWithInabilityToSaveQuote()
277270
->method('setCollectShippingRates')
278271
->with(true)
279272
->willReturnSelf();
280-
$quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false);
281-
$quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true);
282-
283-
$this->service->assign('cart867', $this->quoteAddressMock);
284-
}
285-
286-
/**
287-
* @expectedException \Magento\Framework\Exception\InputException
288-
* @expectedExceptionMessage Incorrect amount
289-
*/
290-
public function testSetAddressWithViolationOfMinimumAmount()
291-
{
292-
$customerAddressId = 150;
293-
294-
$quoteMock = $this->getMock(
295-
\Magento\Quote\Model\Quote::class,
296-
['getIsMultiShipping', 'isVirtual', 'validateMinimumAmount', 'setShippingAddress', 'getShippingAddress'],
297-
[],
298-
'',
299-
false
300-
);
301-
$this->quoteRepositoryMock->expects($this->once())
302-
->method('getActive')
303-
->with('cart867')
304-
->willReturn($quoteMock);
305-
$quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(false));
306-
$quoteMock->expects($this->once())
307-
->method('setShippingAddress')
308-
->with($this->quoteAddressMock)
309-
->willReturnSelf();
310-
311-
$customerAddressMock = $this->getMock(\Magento\Customer\Api\Data\AddressInterface::class);
312-
313-
$this->addressRepository->expects($this->once())
314-
->method('getById')
315-
->with($customerAddressId)
316-
->willReturn($customerAddressMock);
317-
318-
$this->validatorMock->expects($this->once())->method('validate')
319-
->with($this->quoteAddressMock)
320-
->willReturn(true);
321-
322-
$this->quoteAddressMock->expects($this->once())->method('getSaveInAddressBook')->willReturn(1);
323-
$this->quoteAddressMock->expects($this->once())->method('getSameAsBilling')->willReturn(1);
324-
$this->quoteAddressMock->expects($this->once())->method('getCustomerAddressId')->willReturn($customerAddressId);
325-
326-
$quoteMock->expects($this->exactly(2))->method('getShippingAddress')->willReturn($this->quoteAddressMock);
327-
$this->quoteAddressMock->expects($this->once())
328-
->method('importCustomerAddressData')
329-
->with($customerAddressMock)
330-
->willReturnSelf();
331-
332-
$this->quoteAddressMock->expects($this->once())->method('setSameAsBilling')->with(1)->willReturnSelf();
333-
$this->quoteAddressMock->expects($this->once())->method('setSaveInAddressBook')->with(1)->willReturnSelf();
334-
$this->quoteAddressMock->expects($this->once())
335-
->method('setCollectShippingRates')
336-
->with(true)
337-
->willReturnSelf();
338-
339-
$this->amountErrorMessageMock->expects($this->once())
340-
->method('getMessage')
341-
->willReturn(__('Incorrect amount'));
342-
$quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false);
343-
$quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(false);
344273

345274
$this->service->assign('cart867', $this->quoteAddressMock);
346275
}

0 commit comments

Comments
 (0)