Skip to content

Commit

Permalink
MAGETWO-69375: Can't delete last item in cart if Minimum Order is Ena…
Browse files Browse the repository at this point in the history
…ble #6151 #9714
  • Loading branch information
ishakhsuvarov authored May 26, 2017
2 parents ef4b4b5 + bebc0e9 commit e8e284e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 98 deletions.
19 changes: 17 additions & 2 deletions app/code/Magento/Quote/Model/QuoteValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

namespace Magento\Quote\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Quote as QuoteEntity;
use Magento\Directory\Model\AllowedCountries;
use Magento\Framework\App\ObjectManager;
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;

/**
* @api
Expand All @@ -25,15 +27,25 @@ class QuoteValidator
*/
private $allowedCountryReader;

/**
* @var OrderAmountValidationMessage
*/
private $minimumAmountMessage;

/**
* QuoteValidator constructor.
*
* @param AllowedCountries|null $allowedCountryReader
* @param OrderAmountValidationMessage|null $minimumAmountMessage
*/
public function __construct(AllowedCountries $allowedCountryReader = null)
{
public function __construct(
AllowedCountries $allowedCountryReader = null,
OrderAmountValidationMessage $minimumAmountMessage = null
) {
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
->get(AllowedCountries::class);
$this->minimumAmountMessage = $minimumAmountMessage ?: ObjectManager::getInstance()
->get(OrderAmountValidationMessage::class);
}

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

return $this;
}
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())) {
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;
}
}
41 changes: 40 additions & 1 deletion app/code/Magento/Quote/Test/Unit/Model/QuoteValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Payment;
use Magento\Quote\Model\QuoteValidator;
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;

/**
* Class QuoteValidatorTest
Expand All @@ -30,6 +31,11 @@ class QuoteValidatorTest extends \PHPUnit_Framework_TestCase
*/
private $allowedCountryReader;

/**
* @var OrderAmountValidationMessage|\PHPUnit_Framework_MockObject_MockObject
*/
private $orderAmountValidationMessage;

/**
* @return void
*/
Expand All @@ -38,8 +44,14 @@ protected function setUp()
$this->allowedCountryReader = $this->getMockBuilder(AllowedCountries::class)
->disableOriginalConstructor()
->getMock();
$this->orderAmountValidationMessage = $this->getMockBuilder(OrderAmountValidationMessage::class)
->disableOriginalConstructor()
->getMock();

$this->quoteValidator = new \Magento\Quote\Model\QuoteValidator($this->allowedCountryReader);
$this->quoteValidator = new \Magento\Quote\Model\QuoteValidator(
$this->allowedCountryReader,
$this->orderAmountValidationMessage
);

$this->quoteMock = $this->getMock(
\Magento\Quote\Model\Quote::class,
Expand All @@ -51,6 +63,8 @@ protected function setUp()
'setHasError',
'addMessage',
'isVirtual',
'validateMinimumAmount',
'getIsMultiShipping',
'__wakeup'
],
[],
Expand Down Expand Up @@ -185,6 +199,31 @@ public function testValidateBeforeSubmitThrowsExceptionIfPaymentMethodIsNotSelec
$this->quoteValidator->validateBeforeSubmit($this->quoteMock);
}

/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Minimum Order Amount Exceeded.
*/
public function testValidateBeforeSubmitThrowsExceptionIfMinimumOrderAmount()
{
$paymentMock = $this->getMock(\Magento\Quote\Model\Quote\Payment::class, [], [], '', false);
$paymentMock->expects($this->once())->method('getMethod')->willReturn('checkmo');

$billingAddressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
$billingAddressMock->expects($this->any())->method('validate')->willReturn(true);

$this->quoteMock->expects($this->any())->method('getBillingAddress')->willReturn($billingAddressMock);
$this->quoteMock->expects($this->any())->method('getPayment')->willReturn($paymentMock);
$this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(true);

$this->quoteMock->expects($this->any())->method('getIsMultiShipping')->willReturn(false);
$this->quoteMock->expects($this->any())->method('validateMinimumAmount')->willReturn(false);

$this->orderAmountValidationMessage->expects($this->once())->method('getMessage')
->willReturn(__("Minimum Order Amount Exceeded."));

$this->quoteValidator->validateBeforeSubmit($this->quoteMock);
}

/**
* Test case when country id not present in allowed countries list.
*
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

0 comments on commit e8e284e

Please sign in to comment.