diff --git a/app/code/Magento/Quote/Model/QuoteManagement.php b/app/code/Magento/Quote/Model/QuoteManagement.php index 89b8b7a17b697..9afccbb957ab5 100644 --- a/app/code/Magento/Quote/Model/QuoteManagement.php +++ b/app/code/Magento/Quote/Model/QuoteManagement.php @@ -10,6 +10,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Event\ManagerInterface as EventManager; use Magento\Framework\Exception\CouldNotSaveException; +use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\StateException; use Magento\Quote\Api\Data\PaymentInterface; @@ -136,6 +137,11 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface */ private $quoteIdMaskFactory; + /** + * @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage + */ + private $minimumAmountMessage; + /** * @param EventManager $eventManager * @param QuoteValidator $quoteValidator @@ -157,6 +163,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Customer\Api\AccountManagementInterface $accountManagement * @param QuoteFactory $quoteFactory + * @param Quote\Validator\MinimumOrderAmount\ValidationMessage $minimumAmountMessage * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -179,7 +186,8 @@ public function __construct( \Magento\Checkout\Model\Session $checkoutSession, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Api\AccountManagementInterface $accountManagement, - \Magento\Quote\Model\QuoteFactory $quoteFactory + \Magento\Quote\Model\QuoteFactory $quoteFactory, + \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage $minimumAmountMessage = null ) { $this->eventManager = $eventManager; $this->quoteValidator = $quoteValidator; @@ -201,6 +209,8 @@ public function __construct( $this->accountManagement = $accountManagement; $this->customerSession = $customerSession; $this->quoteFactory = $quoteFactory; + $this->minimumAmountMessage = $minimumAmountMessage ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class); } /** @@ -320,6 +330,10 @@ protected function createCustomerCart($customerId, $storeId) public function placeOrder($cartId, PaymentInterface $paymentMethod = null) { $quote = $this->quoteRepository->getActive($cartId); + if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) { + throw new InputException($this->minimumAmountMessage->getMessage()); + } + if ($paymentMethod) { $paymentMethod->setChecks([ \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT, diff --git a/app/code/Magento/Quote/Model/ShippingAddressManagement.php b/app/code/Magento/Quote/Model/ShippingAddressManagement.php index 76ce1483b1546..0e2be5c9e3692 100644 --- a/app/code/Magento/Quote/Model/ShippingAddressManagement.php +++ b/app/code/Magento/Quote/Model/ShippingAddressManagement.php @@ -52,11 +52,6 @@ class ShippingAddressManagement implements \Magento\Quote\Model\ShippingAddressM */ protected $totalsCollector; - /** - * @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage - */ - private $minimumAmountErrorMessage; - /** * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository * @param QuoteAddressValidator $addressValidator @@ -117,10 +112,6 @@ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $addres $address->setSaveInAddressBook($saveInAddressBook); $address->setCollectShippingRates(true); - if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) { - throw new InputException($this->getMinimumAmountErrorMessage()->getMessage()); - } - try { $address->save(); } catch (\Exception $e) { @@ -145,19 +136,4 @@ public function get($cartId) /** @var \Magento\Quote\Model\Quote\Address $address */ return $quote->getShippingAddress(); } - - /** - * @return \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage - * @deprecated - */ - private function getMinimumAmountErrorMessage() - { - if ($this->minimumAmountErrorMessage === null) { - $objectManager = ObjectManager::getInstance(); - $this->minimumAmountErrorMessage = $objectManager->get( - \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class - ); - } - return $this->minimumAmountErrorMessage; - } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php index 43571f4f86a7a..fbc2065845cb9 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php @@ -121,6 +121,11 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase */ protected $quoteMock; + /** + * @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage + */ + private $minimumAmountMessage; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -218,6 +223,8 @@ protected function setUp() 'setCustomerGroupId', 'assignCustomer', 'getPayment', + 'getIsMultiShipping', + 'validateMinimumAmount' ], [], '', @@ -249,6 +256,14 @@ protected function setUp() false ); + $this->minimumAmountMessage = $this->getMock( + \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class, + ['getMessage'], + [], + '', + false + ); + $this->quoteFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteFactory::class, ['create'], [], '', false); $this->model = $objectManager->getObject( \Magento\Quote\Model\QuoteManagement::class, @@ -272,7 +287,8 @@ protected function setUp() 'checkoutSession' => $this->checkoutSessionMock, 'customerSession' => $this->customerSessionMock, 'accountManagement' => $this->accountManagementMock, - 'quoteFactory' => $this->quoteFactoryMock + 'quoteFactory' => $this->quoteFactoryMock, + 'minimumAmountMessage' => $this->minimumAmountMessage ] ); @@ -705,6 +721,10 @@ public function testPlaceOrderIfCustomerIsGuest() ->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST); $this->quoteMock->expects($this->once())->method('setCustomerId')->with(null)->willReturnSelf(); $this->quoteMock->expects($this->once())->method('setCustomerEmail')->with($email)->willReturnSelf(); + $this->quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false); + $this->quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true); + + $this->minimumAmountMessage->expects($this->never())->method('getMessage'); $addressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, ['getEmail'], [], '', false); $addressMock->expects($this->once())->method('getEmail')->willReturn($email); @@ -739,7 +759,8 @@ public function testPlaceOrderIfCustomerIsGuest() 'checkoutSession' => $this->checkoutSessionMock, 'customerSession' => $this->customerSessionMock, 'accountManagement' => $this->accountManagementMock, - 'quoteFactory' => $this->quoteFactoryMock + 'quoteFactory' => $this->quoteFactoryMock, + 'minimumAmountMessage' => $this->minimumAmountMessage ] ); $orderMock = $this->getMock( @@ -797,7 +818,8 @@ public function testPlaceOrder() 'checkoutSession' => $this->checkoutSessionMock, 'customerSession' => $this->customerSessionMock, 'accountManagement' => $this->accountManagementMock, - 'quoteFactory' => $this->quoteFactoryMock + 'quoteFactory' => $this->quoteFactoryMock, + 'minimumAmountMessage' => $this->minimumAmountMessage ] ); $orderMock = $this->getMock( @@ -829,6 +851,11 @@ public function testPlaceOrder() ->method('setCustomerIsGuest') ->with(true); + $this->quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false); + $this->quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true); + + $this->minimumAmountMessage->expects($this->never())->method('getMessage'); + $service->expects($this->once())->method('submit')->willReturn($orderMock); $this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId); @@ -856,6 +883,67 @@ public function testPlaceOrder() $this->assertEquals($orderId, $service->placeOrder($cartId, $paymentMethod)); } + /** + * @expectedException \Magento\Framework\Exception\InputException + * @expectedExceptionMessage Incorrect amount + */ + public function testPlaceOrderWithViolationOfMinimumAmount() + { + $cartId = 323; + + $this->quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false); + $this->quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(false); + + $this->minimumAmountMessage->expects($this->once()) + ->method('getMessage') + ->willReturn(__('Incorrect amount')); + + $this->quoteRepositoryMock->expects($this->once()) + ->method('getActive') + ->with($cartId) + ->willReturn($this->quoteMock); + + /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\QuoteManagement $service */ + $service = $this->getMock( + \Magento\Quote\Model\QuoteManagement::class, + ['submit'], + [ + 'eventManager' => $this->eventManager, + 'quoteValidator' => $this->quoteValidator, + 'orderFactory' => $this->orderFactory, + 'orderManagement' => $this->orderManagement, + 'customerManagement' => $this->customerManagement, + 'quoteAddressToOrder' => $this->quoteAddressToOrder, + 'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress, + 'quoteItemToOrderItem' => $this->quoteItemToOrderItem, + 'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment, + 'userContext' => $this->userContextMock, + 'quoteRepository' => $this->quoteRepositoryMock, + 'customerRepository' => $this->customerRepositoryMock, + 'customerModelFactory' => $this->customerFactoryMock, + 'quoteAddressFactory' => $this->quoteAddressFactory, + 'dataObjectHelper' => $this->dataObjectHelperMock, + 'storeManager' => $this->storeManagerMock, + 'checkoutSession' => $this->checkoutSessionMock, + 'customerSession' => $this->customerSessionMock, + 'accountManagement' => $this->accountManagementMock, + 'quoteFactory' => $this->quoteFactoryMock, + 'minimumAmountMessage' => $this->minimumAmountMessage + ] + ); + + $service->expects($this->never())->method('submit'); + + $paymentMethod = $this->getMock( + \Magento\Quote\Model\Quote\Payment::class, + [], + [], + '', + false + ); + $service->placeOrder($cartId, $paymentMethod); + } + /** * @param $isGuest * @param $isVirtual diff --git a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php index 3380a8e7e2940..a02dc4cc8b253 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php @@ -116,11 +116,6 @@ protected function setUp() 'addressRepository' => $this->addressRepository ] ); - $this->objectManager->setBackwardCompatibleProperty( - $this->service, - 'minimumAmountErrorMessage', - $this->amountErrorMessageMock - ); } /** @@ -190,8 +185,6 @@ public function testSetAddress() ->method('setCollectShippingRates') ->with(true) ->willReturnSelf(); - $quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false); - $quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true); $this->quoteAddressMock->expects($this->once())->method('save')->willReturnSelf(); $this->quoteAddressMock->expects($this->once())->method('getId')->will($this->returnValue($addressId)); @@ -277,70 +270,6 @@ public function testSetAddressWithInabilityToSaveQuote() ->method('setCollectShippingRates') ->with(true) ->willReturnSelf(); - $quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false); - $quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(true); - - $this->service->assign('cart867', $this->quoteAddressMock); - } - - /** - * @expectedException \Magento\Framework\Exception\InputException - * @expectedExceptionMessage Incorrect amount - */ - public function testSetAddressWithViolationOfMinimumAmount() - { - $customerAddressId = 150; - - $quoteMock = $this->getMock( - \Magento\Quote\Model\Quote::class, - ['getIsMultiShipping', 'isVirtual', 'validateMinimumAmount', 'setShippingAddress', 'getShippingAddress'], - [], - '', - false - ); - $this->quoteRepositoryMock->expects($this->once()) - ->method('getActive') - ->with('cart867') - ->willReturn($quoteMock); - $quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(false)); - $quoteMock->expects($this->once()) - ->method('setShippingAddress') - ->with($this->quoteAddressMock) - ->willReturnSelf(); - - $customerAddressMock = $this->getMock(\Magento\Customer\Api\Data\AddressInterface::class); - - $this->addressRepository->expects($this->once()) - ->method('getById') - ->with($customerAddressId) - ->willReturn($customerAddressMock); - - $this->validatorMock->expects($this->once())->method('validate') - ->with($this->quoteAddressMock) - ->willReturn(true); - - $this->quoteAddressMock->expects($this->once())->method('getSaveInAddressBook')->willReturn(1); - $this->quoteAddressMock->expects($this->once())->method('getSameAsBilling')->willReturn(1); - $this->quoteAddressMock->expects($this->once())->method('getCustomerAddressId')->willReturn($customerAddressId); - - $quoteMock->expects($this->exactly(2))->method('getShippingAddress')->willReturn($this->quoteAddressMock); - $this->quoteAddressMock->expects($this->once()) - ->method('importCustomerAddressData') - ->with($customerAddressMock) - ->willReturnSelf(); - - $this->quoteAddressMock->expects($this->once())->method('setSameAsBilling')->with(1)->willReturnSelf(); - $this->quoteAddressMock->expects($this->once())->method('setSaveInAddressBook')->with(1)->willReturnSelf(); - $this->quoteAddressMock->expects($this->once()) - ->method('setCollectShippingRates') - ->with(true) - ->willReturnSelf(); - - $this->amountErrorMessageMock->expects($this->once()) - ->method('getMessage') - ->willReturn(__('Incorrect amount')); - $quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(false); - $quoteMock->expects($this->once())->method('validateMinimumAmount')->with(false)->willReturn(false); $this->service->assign('cart867', $this->quoteAddressMock); }