Skip to content

Commit

Permalink
Merge pull request #4142 from magento-mpi/PR-2019-4-29
Browse files Browse the repository at this point in the history
MPI bug fix 2.2
  • Loading branch information
viktym authored May 2, 2019
2 parents e11c932 + c0f531d commit 1cc6e75
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ define(
*/
onError: function (response) {
braintree.showError($t('Payment ' + this.getTitle() + ' can\'t be initialized'));
this.isPlaceOrderActionAllowed(true);
throw response.message;
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ define([
*/
placeOrderClick: function () {
if (this.validateCardType()) {
this.isPlaceOrderActionAllowed(false);
$(this.getSelector('submit')).trigger('click');
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,25 @@ define([
event.preventDefault();
}

if (this.validate() && additionalValidators.validate()) {
if (this.validate() &&
additionalValidators.validate() &&
this.isPlaceOrderActionAllowed() === true
) {
this.isPlaceOrderActionAllowed(false);

this.getPlaceOrderDeferredObject()
.fail(
function () {
self.isPlaceOrderActionAllowed(true);
}
).done(
.done(
function () {
self.afterPlaceOrder();

if (self.redirectAfterPlaceOrder) {
redirectOnSuccessAction.execute();
}
}
).always(
function () {
self.isPlaceOrderActionAllowed(true);
}
);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ define([
* @override
*/
placeOrder: function () {
if (this.validateHandler() && additionalValidators.validate()) {
var self = this;

if (this.validateHandler() &&
additionalValidators.validate() &&
this.isPlaceOrderActionAllowed() === true
) {
fullScreenLoader.startLoader();

this.isPlaceOrderActionAllowed(false);
Expand All @@ -127,8 +131,15 @@ define([
method: this.getCode()
}
)
).done(this.done.bind(this))
.fail(this.fail.bind(this));
).done(
this.done.bind(this)
).fail(
this.fail.bind(this)
).always(
function () {
self.isPlaceOrderActionAllowed(true);
}
);

this.initTimeoutHandler();
}
Expand Down Expand Up @@ -192,7 +203,6 @@ define([
*/
fail: function () {
fullScreenLoader.stopLoader();
this.isPlaceOrderActionAllowed(true);

return this;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ define([
placeOrder: function () {
var self = this;

if (this.validateHandler() && additionalValidators.validate()) {
if (this.validateHandler() &&
additionalValidators.validate() &&
this.isPlaceOrderActionAllowed() === true
) {
this.isPlaceOrderActionAllowed(false);
fullScreenLoader.startLoader();
$.when(
Expand Down
17 changes: 10 additions & 7 deletions app/code/Magento/Quote/Model/QuoteManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
protected $eventManager;

/**
* @var QuoteValidator
* @var SubmitQuoteValidator
*/
protected $quoteValidator;
private $submitQuoteValidator;

/**
* @var OrderFactory
Expand Down Expand Up @@ -148,7 +148,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface

/**
* @param EventManager $eventManager
* @param QuoteValidator $quoteValidator
* @param SubmitQuoteValidator $submitQuoteValidator
* @param OrderFactory $orderFactory
* @param OrderManagement $orderManagement
* @param CustomerManagement $customerManagement
Expand All @@ -173,7 +173,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
*/
public function __construct(
EventManager $eventManager,
QuoteValidator $quoteValidator,
SubmitQuoteValidator $submitQuoteValidator,
OrderFactory $orderFactory,
OrderManagement $orderManagement,
CustomerManagement $customerManagement,
Expand All @@ -196,7 +196,7 @@ public function __construct(
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository = null
) {
$this->eventManager = $eventManager;
$this->quoteValidator = $quoteValidator;
$this->submitQuoteValidator = $submitQuoteValidator;
$this->orderFactory = $orderFactory;
$this->orderManagement = $orderManagement;
$this->customerManagement = $customerManagement;
Expand Down Expand Up @@ -282,6 +282,7 @@ public function assignCustomer($cartId, $customerId, $storeId)
throw new StateException(
__('Cannot assign customer to the given cart. Customer already has active cart.')
);
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
}

Expand Down Expand Up @@ -454,7 +455,7 @@ protected function resolveItems(QuoteEntity $quote)
protected function submitQuote(QuoteEntity $quote, $orderData = [])
{
$order = $this->orderFactory->create();
$this->quoteValidator->validateBeforeSubmit($quote);
$this->submitQuoteValidator->validateQuote($quote);
if (!$quote->getCustomerIsGuest()) {
if ($quote->getCustomerId()) {
$this->_prepareCustomerQuote($quote);
Expand Down Expand Up @@ -509,6 +510,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
$order->setCustomerFirstname($quote->getCustomerFirstname());
$order->setCustomerMiddlename($quote->getCustomerMiddlename());
$order->setCustomerLastname($quote->getCustomerLastname());
$this->submitQuoteValidator->validateOrder($order);

$this->eventManager->dispatch(
'sales_model_service_quote_submit_before',
Expand Down Expand Up @@ -625,12 +627,13 @@ private function rollbackAddresses(
'exception' => $e,
]
);
// phpcs:ignore Magento2.Exceptions.ThrowCatch
} catch (\Exception $consecutiveException) {
$message = sprintf(
"An exception occurred on 'sales_model_service_quote_submit_failure' event: %s",
$consecutiveException->getMessage()
);

// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception($message, 0, $e);
}
}
Expand Down
71 changes: 71 additions & 0 deletions app/code/Magento/Quote/Model/SubmitQuoteValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address\Validator as OrderAddressValidator;

/**
* Validates quote and order before quote submit.
*/
class SubmitQuoteValidator
{
/**
* @var QuoteValidator
*/
private $quoteValidator;

/**
* @var OrderAddressValidator
*/
private $orderAddressValidator;

/**
* @param QuoteValidator $quoteValidator
* @param OrderAddressValidator $orderAddressValidator
*/
public function __construct(
QuoteValidator $quoteValidator,
OrderAddressValidator $orderAddressValidator
) {
$this->quoteValidator = $quoteValidator;
$this->orderAddressValidator = $orderAddressValidator;
}

/**
* Validates quote.
*
* @param Quote $quote
* @return void
* @throws LocalizedException
*/
public function validateQuote(Quote $quote)
{
$this->quoteValidator->validateBeforeSubmit($quote);
}

/**
* Validates order.
*
* @param Order $order
* @return void
* @throws LocalizedException
*/
public function validateOrder(Order $order)
{
foreach ($order->getAddresses() as $address) {
$errors = $this->orderAddressValidator->validate($address);
if (!empty($errors)) {
throw new LocalizedException(
__("Failed address validation:\n%1", implode("\n", $errors))
);
}
}
}
}
27 changes: 19 additions & 8 deletions app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
*/
class QuoteManagementTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -22,9 +24,9 @@ class QuoteManagementTest extends \PHPUnit\Framework\TestCase
protected $model;

/**
* @var \Magento\Quote\Model\QuoteValidator|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Quote\Model\SubmitQuoteValidator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $quoteValidator;
protected $submitQuoteValidator;

/**
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
Expand Down Expand Up @@ -143,7 +145,7 @@ protected function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->quoteValidator = $this->createMock(\Magento\Quote\Model\QuoteValidator::class);
$this->submitQuoteValidator = $this->createMock(\Magento\Quote\Model\SubmitQuoteValidator::class);
$this->eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
$this->orderFactory = $this->createPartialMock(
\Magento\Sales\Api\Data\OrderInterfaceFactory::class,
Expand Down Expand Up @@ -210,7 +212,7 @@ protected function setUp()
\Magento\Quote\Model\QuoteManagement::class,
[
'eventManager' => $this->eventManager,
'quoteValidator' => $this->quoteValidator,
'submitQuoteValidator' => $this->submitQuoteValidator,
'orderFactory' => $this->orderFactory,
'orderManagement' => $this->orderManagement,
'customerManagement' => $this->customerManagement,
Expand Down Expand Up @@ -560,7 +562,9 @@ public function testSubmit()
$shippingAddress
);

$this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
$this->submitQuoteValidator->expects($this->once())
->method('validateQuote')
->with($quote);
$this->quoteAddressToOrder->expects($this->once())
->method('convert')
->with($shippingAddress, $orderData)
Expand Down Expand Up @@ -655,7 +659,7 @@ public function testPlaceOrderIfCustomerIsGuest()
->setConstructorArgs(
[
'eventManager' => $this->eventManager,
'quoteValidator' => $this->quoteValidator,
'quoteValidator' => $this->submitQuoteValidator,
'orderFactory' => $this->orderFactory,
'orderManagement' => $this->orderManagement,
'customerManagement' => $this->customerManagement,
Expand Down Expand Up @@ -712,7 +716,7 @@ public function testPlaceOrder()
->setConstructorArgs(
[
'eventManager' => $this->eventManager,
'quoteValidator' => $this->quoteValidator,
'quoteValidator' => $this->submitQuoteValidator,
'orderFactory' => $this->orderFactory,
'orderManagement' => $this->orderManagement,
'customerManagement' => $this->customerManagement,
Expand Down Expand Up @@ -934,6 +938,9 @@ protected function prepareOrderFactory(
return $order;
}

/**
* @throws NoSuchEntityException
*/
public function testGetCartForCustomer()
{
$customerId = 100;
Expand Down Expand Up @@ -978,6 +985,9 @@ protected function setPropertyValue(&$object, $property, $value)
return $object;
}

/**
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function testSubmitForCustomer()
{
$orderData = [];
Expand Down Expand Up @@ -1010,7 +1020,8 @@ public function testSubmitForCustomer()
$shippingAddress
);

$this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
$this->submitQuoteValidator->method('validateQuote')
->with($quote);
$this->quoteAddressToOrder->expects($this->once())
->method('convert')
->with($shippingAddress, $orderData)
Expand Down
Loading

0 comments on commit 1cc6e75

Please sign in to comment.