Skip to content

Commit 2536b10

Browse files
committed
#6151 - moved validation of minimum order amount to placeOrder in QuoteManagement
1 parent 39e2efd commit 2536b10

File tree

4 files changed

+105
-95
lines changed

4 files changed

+105
-95
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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;
1314
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\Exception\StateException;
1516
use Magento\Quote\Api\Data\PaymentInterface;
@@ -136,6 +137,11 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
136137
*/
137138
private $quoteIdMaskFactory;
138139

140+
/**
141+
* @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
142+
*/
143+
private $minimumAmountErrorMessage;
144+
139145
/**
140146
* @param EventManager $eventManager
141147
* @param QuoteValidator $quoteValidator
@@ -157,6 +163,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
157163
* @param \Magento\Customer\Model\Session $customerSession
158164
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
159165
* @param QuoteFactory $quoteFactory
166+
* @param Quote\Validator\MinimumOrderAmount\ValidationMessage $minimumAmountErrorMessage
160167
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
161168
*/
162169
public function __construct(
@@ -179,7 +186,8 @@ public function __construct(
179186
\Magento\Checkout\Model\Session $checkoutSession,
180187
\Magento\Customer\Model\Session $customerSession,
181188
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
182-
\Magento\Quote\Model\QuoteFactory $quoteFactory
189+
\Magento\Quote\Model\QuoteFactory $quoteFactory,
190+
\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage $minimumAmountErrorMessage
183191
) {
184192
$this->eventManager = $eventManager;
185193
$this->quoteValidator = $quoteValidator;
@@ -201,6 +209,7 @@ public function __construct(
201209
$this->accountManagement = $accountManagement;
202210
$this->customerSession = $customerSession;
203211
$this->quoteFactory = $quoteFactory;
212+
$this->minimumAmountErrorMessage = $minimumAmountErrorMessage;
204213
}
205214

206215
/**
@@ -320,6 +329,10 @@ protected function createCustomerCart($customerId, $storeId)
320329
public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
321330
{
322331
$quote = $this->quoteRepository->getActive($cartId);
332+
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
333+
throw new InputException($this->minimumAmountErrorMessage->getMessage());
334+
}
335+
323336
if ($paymentMethod) {
324337
$paymentMethod->setChecks([
325338
\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,

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

Lines changed: 0 additions & 20 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
@@ -141,19 +136,4 @@ public function get($cartId)
141136
/** @var \Magento\Quote\Model\Quote\Address $address */
142137
return $quote->getShippingAddress();
143138
}
144-
145-
/**
146-
* @return \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
147-
* @deprecated
148-
*/
149-
private function getMinimumAmountErrorMessage()
150-
{
151-
if ($this->minimumAmountErrorMessage === null) {
152-
$objectManager = ObjectManager::getInstance();
153-
$this->minimumAmountErrorMessage = $objectManager->get(
154-
\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class
155-
);
156-
}
157-
return $this->minimumAmountErrorMessage;
158-
}
159139
}

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

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

124+
/**
125+
* @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
126+
*/
127+
protected $minimumAmountErrorMessage;
128+
124129
/**
125130
* @var \PHPUnit_Framework_MockObject_MockObject
126131
*/
@@ -218,6 +223,8 @@ protected function setUp()
218223
'setCustomerGroupId',
219224
'assignCustomer',
220225
'getPayment',
226+
'getIsMultiShipping',
227+
'validateMinimumAmount'
221228
],
222229
[],
223230
'',
@@ -249,6 +256,14 @@ protected function setUp()
249256
false
250257
);
251258

259+
$this->minimumAmountErrorMessage = $this->getMock(
260+
\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class,
261+
['getMessage'],
262+
[],
263+
'',
264+
false
265+
);
266+
252267
$this->quoteFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteFactory::class, ['create'], [], '', false);
253268
$this->model = $objectManager->getObject(
254269
\Magento\Quote\Model\QuoteManagement::class,
@@ -272,7 +287,8 @@ protected function setUp()
272287
'checkoutSession' => $this->checkoutSessionMock,
273288
'customerSession' => $this->customerSessionMock,
274289
'accountManagement' => $this->accountManagementMock,
275-
'quoteFactory' => $this->quoteFactoryMock
290+
'quoteFactory' => $this->quoteFactoryMock,
291+
'minimumAmountErrorMessage' => $this->minimumAmountErrorMessage
276292
]
277293
);
278294

@@ -705,6 +721,10 @@ public function testPlaceOrderIfCustomerIsGuest()
705721
->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST);
706722
$this->quoteMock->expects($this->once())->method('setCustomerId')->with(null)->willReturnSelf();
707723
$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->minimumAmountErrorMessage->expects($this->never())->method('getMessage');
708728

709729
$addressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, ['getEmail'], [], '', false);
710730
$addressMock->expects($this->once())->method('getEmail')->willReturn($email);
@@ -739,7 +759,8 @@ public function testPlaceOrderIfCustomerIsGuest()
739759
'checkoutSession' => $this->checkoutSessionMock,
740760
'customerSession' => $this->customerSessionMock,
741761
'accountManagement' => $this->accountManagementMock,
742-
'quoteFactory' => $this->quoteFactoryMock
762+
'quoteFactory' => $this->quoteFactoryMock,
763+
'minimumAmountErrorMessage' => $this->minimumAmountErrorMessage
743764
]
744765
);
745766
$orderMock = $this->getMock(
@@ -797,7 +818,8 @@ public function testPlaceOrder()
797818
'checkoutSession' => $this->checkoutSessionMock,
798819
'customerSession' => $this->customerSessionMock,
799820
'accountManagement' => $this->accountManagementMock,
800-
'quoteFactory' => $this->quoteFactoryMock
821+
'quoteFactory' => $this->quoteFactoryMock,
822+
'minimumAmountErrorMessage' => $this->minimumAmountErrorMessage
801823
]
802824
);
803825
$orderMock = $this->getMock(
@@ -829,6 +851,11 @@ public function testPlaceOrder()
829851
->method('setCustomerIsGuest')
830852
->with(true);
831853

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->minimumAmountErrorMessage->expects($this->never())->method('getMessage');
858+
832859
$service->expects($this->once())->method('submit')->willReturn($orderMock);
833860

834861
$this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId);
@@ -856,6 +883,67 @@ public function testPlaceOrder()
856883
$this->assertEquals($orderId, $service->placeOrder($cartId, $paymentMethod));
857884
}
858885

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->minimumAmountErrorMessage->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+
'minimumAmountErrorMessage' => $this->minimumAmountErrorMessage
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+
859947
/**
860948
* @param $isGuest
861949
* @param $isVirtual

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)