Skip to content

Can't delete last item in cart if Minimum Order is Enable #6151 #9714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion app/code/Magento/Quote/Model/QuoteManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 0 additions & 24 deletions app/code/Magento/Quote/Model/ShippingAddressManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other check for Minimum Amount occurs in the Checkout Index controller and it is not sufficient, since checkout flow may be performed using Web API without calling the controller.
Please consider moving this check to Place Order call.

throw new InputException($this->getMinimumAmountErrorMessage()->getMessage());
}

try {
$address->save();
} catch (\Exception $e) {
Expand All @@ -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;
}
}
94 changes: 91 additions & 3 deletions app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -218,6 +223,8 @@ protected function setUp()
'setCustomerGroupId',
'assignCustomer',
'getPayment',
'getIsMultiShipping',
'validateMinimumAmount'
],
[],
'',
Expand Down Expand Up @@ -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,
Expand All @@ -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
]
);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ protected function setUp()
'addressRepository' => $this->addressRepository
]
);
$this->objectManager->setBackwardCompatibleProperty(
$this->service,
'minimumAmountErrorMessage',
$this->amountErrorMessageMock
);
}

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
Expand Down