Skip to content

Commit bebc0e9

Browse files
committed
MAGETWO-69375: Can't delete last item in cart if Minimum Order is Enable #6151 #9714
1 parent 845f378 commit bebc0e9

File tree

4 files changed

+61
-109
lines changed

4 files changed

+61
-109
lines changed

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Event\ManagerInterface as EventManager;
1212
use Magento\Framework\Exception\CouldNotSaveException;
13-
use Magento\Framework\Exception\InputException;
1413
use Magento\Framework\Exception\LocalizedException;
1514
use Magento\Framework\Exception\StateException;
1615
use Magento\Quote\Api\Data\PaymentInterface;
@@ -137,11 +136,6 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
137136
*/
138137
private $quoteIdMaskFactory;
139138

140-
/**
141-
* @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
142-
*/
143-
private $minimumAmountMessage;
144-
145139
/**
146140
* @param EventManager $eventManager
147141
* @param QuoteValidator $quoteValidator
@@ -163,7 +157,6 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
163157
* @param \Magento\Customer\Model\Session $customerSession
164158
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
165159
* @param QuoteFactory $quoteFactory
166-
* @param Quote\Validator\MinimumOrderAmount\ValidationMessage $minimumAmountMessage
167160
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
168161
*/
169162
public function __construct(
@@ -186,8 +179,7 @@ public function __construct(
186179
\Magento\Checkout\Model\Session $checkoutSession,
187180
\Magento\Customer\Model\Session $customerSession,
188181
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
189-
\Magento\Quote\Model\QuoteFactory $quoteFactory,
190-
\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage $minimumAmountMessage = null
182+
\Magento\Quote\Model\QuoteFactory $quoteFactory
191183
) {
192184
$this->eventManager = $eventManager;
193185
$this->quoteValidator = $quoteValidator;
@@ -209,8 +201,6 @@ public function __construct(
209201
$this->accountManagement = $accountManagement;
210202
$this->customerSession = $customerSession;
211203
$this->quoteFactory = $quoteFactory;
212-
$this->minimumAmountMessage = $minimumAmountMessage ?: \Magento\Framework\App\ObjectManager::getInstance()
213-
->get(\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class);
214204
}
215205

216206
/**
@@ -330,10 +320,6 @@ protected function createCustomerCart($customerId, $storeId)
330320
public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
331321
{
332322
$quote = $this->quoteRepository->getActive($cartId);
333-
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
334-
throw new InputException($this->minimumAmountMessage->getMessage());
335-
}
336-
337323
if ($paymentMethod) {
338324
$paymentMethod->setChecks([
339325
\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,

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/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase
121121
*/
122122
protected $quoteMock;
123123

124-
/**
125-
* @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
126-
*/
127-
private $minimumAmountMessage;
128-
129124
/**
130125
* @var \PHPUnit_Framework_MockObject_MockObject
131126
*/
@@ -223,8 +218,6 @@ protected function setUp()
223218
'setCustomerGroupId',
224219
'assignCustomer',
225220
'getPayment',
226-
'getIsMultiShipping',
227-
'validateMinimumAmount'
228221
],
229222
[],
230223
'',
@@ -256,14 +249,6 @@ protected function setUp()
256249
false
257250
);
258251

259-
$this->minimumAmountMessage = $this->getMock(
260-
\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class,
261-
['getMessage'],
262-
[],
263-
'',
264-
false
265-
);
266-
267252
$this->quoteFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteFactory::class, ['create'], [], '', false);
268253
$this->model = $objectManager->getObject(
269254
\Magento\Quote\Model\QuoteManagement::class,
@@ -287,8 +272,7 @@ protected function setUp()
287272
'checkoutSession' => $this->checkoutSessionMock,
288273
'customerSession' => $this->customerSessionMock,
289274
'accountManagement' => $this->accountManagementMock,
290-
'quoteFactory' => $this->quoteFactoryMock,
291-
'minimumAmountMessage' => $this->minimumAmountMessage
275+
'quoteFactory' => $this->quoteFactoryMock
292276
]
293277
);
294278

@@ -721,10 +705,6 @@ public function testPlaceOrderIfCustomerIsGuest()
721705
->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST);
722706
$this->quoteMock->expects($this->once())->method('setCustomerId')->with(null)->willReturnSelf();
723707
$this->quoteMock->expects($this->once())->method('setCustomerEmail')->with($email)->willReturnSelf();
724-
$this->quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false);
725-
$this->quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true);
726-
727-
$this->minimumAmountMessage->expects($this->never())->method('getMessage');
728708

729709
$addressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, ['getEmail'], [], '', false);
730710
$addressMock->expects($this->once())->method('getEmail')->willReturn($email);
@@ -759,8 +739,7 @@ public function testPlaceOrderIfCustomerIsGuest()
759739
'checkoutSession' => $this->checkoutSessionMock,
760740
'customerSession' => $this->customerSessionMock,
761741
'accountManagement' => $this->accountManagementMock,
762-
'quoteFactory' => $this->quoteFactoryMock,
763-
'minimumAmountMessage' => $this->minimumAmountMessage
742+
'quoteFactory' => $this->quoteFactoryMock
764743
]
765744
);
766745
$orderMock = $this->getMock(
@@ -818,8 +797,7 @@ public function testPlaceOrder()
818797
'checkoutSession' => $this->checkoutSessionMock,
819798
'customerSession' => $this->customerSessionMock,
820799
'accountManagement' => $this->accountManagementMock,
821-
'quoteFactory' => $this->quoteFactoryMock,
822-
'minimumAmountMessage' => $this->minimumAmountMessage
800+
'quoteFactory' => $this->quoteFactoryMock
823801
]
824802
);
825803
$orderMock = $this->getMock(
@@ -851,11 +829,6 @@ public function testPlaceOrder()
851829
->method('setCustomerIsGuest')
852830
->with(true);
853831

854-
$this->quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false);
855-
$this->quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true);
856-
857-
$this->minimumAmountMessage->expects($this->never())->method('getMessage');
858-
859832
$service->expects($this->once())->method('submit')->willReturn($orderMock);
860833

861834
$this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId);
@@ -883,67 +856,6 @@ public function testPlaceOrder()
883856
$this->assertEquals($orderId, $service->placeOrder($cartId, $paymentMethod));
884857
}
885858

886-
/**
887-
* @expectedException \Magento\Framework\Exception\InputException
888-
* @expectedExceptionMessage Incorrect amount
889-
*/
890-
public function testPlaceOrderWithViolationOfMinimumAmount()
891-
{
892-
$cartId = 323;
893-
894-
$this->quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false);
895-
$this->quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(false);
896-
897-
$this->minimumAmountMessage->expects($this->once())
898-
->method('getMessage')
899-
->willReturn(__('Incorrect amount'));
900-
901-
$this->quoteRepositoryMock->expects($this->once())
902-
->method('getActive')
903-
->with($cartId)
904-
->willReturn($this->quoteMock);
905-
906-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\QuoteManagement $service */
907-
$service = $this->getMock(
908-
\Magento\Quote\Model\QuoteManagement::class,
909-
['submit'],
910-
[
911-
'eventManager' => $this->eventManager,
912-
'quoteValidator' => $this->quoteValidator,
913-
'orderFactory' => $this->orderFactory,
914-
'orderManagement' => $this->orderManagement,
915-
'customerManagement' => $this->customerManagement,
916-
'quoteAddressToOrder' => $this->quoteAddressToOrder,
917-
'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
918-
'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
919-
'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
920-
'userContext' => $this->userContextMock,
921-
'quoteRepository' => $this->quoteRepositoryMock,
922-
'customerRepository' => $this->customerRepositoryMock,
923-
'customerModelFactory' => $this->customerFactoryMock,
924-
'quoteAddressFactory' => $this->quoteAddressFactory,
925-
'dataObjectHelper' => $this->dataObjectHelperMock,
926-
'storeManager' => $this->storeManagerMock,
927-
'checkoutSession' => $this->checkoutSessionMock,
928-
'customerSession' => $this->customerSessionMock,
929-
'accountManagement' => $this->accountManagementMock,
930-
'quoteFactory' => $this->quoteFactoryMock,
931-
'minimumAmountMessage' => $this->minimumAmountMessage
932-
]
933-
);
934-
935-
$service->expects($this->never())->method('submit');
936-
937-
$paymentMethod = $this->getMock(
938-
\Magento\Quote\Model\Quote\Payment::class,
939-
[],
940-
[],
941-
'',
942-
false
943-
);
944-
$service->placeOrder($cartId, $paymentMethod);
945-
}
946-
947859
/**
948860
* @param $isGuest
949861
* @param $isVirtual

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
*

0 commit comments

Comments
 (0)