Skip to content

Commit 1cc6e75

Browse files
authored
Merge pull request #4142 from magento-mpi/PR-2019-4-29
MPI bug fix 2.2
2 parents e11c932 + c0f531d commit 1cc6e75

File tree

13 files changed

+222
-57
lines changed

13 files changed

+222
-57
lines changed

app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/cc-form.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ define(
7979
*/
8080
onError: function (response) {
8181
braintree.showError($t('Payment ' + this.getTitle() + ' can\'t be initialized'));
82-
this.isPlaceOrderActionAllowed(true);
8382
throw response.message;
8483
},
8584

app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/hosted-fields.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ define([
156156
*/
157157
placeOrderClick: function () {
158158
if (this.validateCardType()) {
159-
this.isPlaceOrderActionAllowed(false);
160159
$(this.getSelector('submit')).trigger('click');
161160
}
162161
},

app/code/Magento/Checkout/view/frontend/web/js/view/payment/default.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,25 @@ define([
133133
event.preventDefault();
134134
}
135135

136-
if (this.validate() && additionalValidators.validate()) {
136+
if (this.validate() &&
137+
additionalValidators.validate() &&
138+
this.isPlaceOrderActionAllowed() === true
139+
) {
137140
this.isPlaceOrderActionAllowed(false);
138141

139142
this.getPlaceOrderDeferredObject()
140-
.fail(
141-
function () {
142-
self.isPlaceOrderActionAllowed(true);
143-
}
144-
).done(
143+
.done(
145144
function () {
146145
self.afterPlaceOrder();
147146

148147
if (self.redirectAfterPlaceOrder) {
149148
redirectOnSuccessAction.execute();
150149
}
151150
}
151+
).always(
152+
function () {
153+
self.isPlaceOrderActionAllowed(true);
154+
}
152155
);
153156

154157
return true;

app/code/Magento/Payment/view/frontend/web/js/view/payment/iframe.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ define([
114114
* @override
115115
*/
116116
placeOrder: function () {
117-
if (this.validateHandler() && additionalValidators.validate()) {
117+
var self = this;
118118

119+
if (this.validateHandler() &&
120+
additionalValidators.validate() &&
121+
this.isPlaceOrderActionAllowed() === true
122+
) {
119123
fullScreenLoader.startLoader();
120124

121125
this.isPlaceOrderActionAllowed(false);
@@ -127,8 +131,15 @@ define([
127131
method: this.getCode()
128132
}
129133
)
130-
).done(this.done.bind(this))
131-
.fail(this.fail.bind(this));
134+
).done(
135+
this.done.bind(this)
136+
).fail(
137+
this.fail.bind(this)
138+
).always(
139+
function () {
140+
self.isPlaceOrderActionAllowed(true);
141+
}
142+
);
132143

133144
this.initTimeoutHandler();
134145
}
@@ -192,7 +203,6 @@ define([
192203
*/
193204
fail: function () {
194205
fullScreenLoader.stopLoader();
195-
this.isPlaceOrderActionAllowed(true);
196206

197207
return this;
198208
},

app/code/Magento/Paypal/view/frontend/web/js/view/payment/method-renderer/payflowpro-method.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ define([
7979
placeOrder: function () {
8080
var self = this;
8181

82-
if (this.validateHandler() && additionalValidators.validate()) {
82+
if (this.validateHandler() &&
83+
additionalValidators.validate() &&
84+
this.isPlaceOrderActionAllowed() === true
85+
) {
8386
this.isPlaceOrderActionAllowed(false);
8487
fullScreenLoader.startLoader();
8588
$.when(

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
3737
protected $eventManager;
3838

3939
/**
40-
* @var QuoteValidator
40+
* @var SubmitQuoteValidator
4141
*/
42-
protected $quoteValidator;
42+
private $submitQuoteValidator;
4343

4444
/**
4545
* @var OrderFactory
@@ -148,7 +148,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
148148

149149
/**
150150
* @param EventManager $eventManager
151-
* @param QuoteValidator $quoteValidator
151+
* @param SubmitQuoteValidator $submitQuoteValidator
152152
* @param OrderFactory $orderFactory
153153
* @param OrderManagement $orderManagement
154154
* @param CustomerManagement $customerManagement
@@ -173,7 +173,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
173173
*/
174174
public function __construct(
175175
EventManager $eventManager,
176-
QuoteValidator $quoteValidator,
176+
SubmitQuoteValidator $submitQuoteValidator,
177177
OrderFactory $orderFactory,
178178
OrderManagement $orderManagement,
179179
CustomerManagement $customerManagement,
@@ -196,7 +196,7 @@ public function __construct(
196196
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository = null
197197
) {
198198
$this->eventManager = $eventManager;
199-
$this->quoteValidator = $quoteValidator;
199+
$this->submitQuoteValidator = $submitQuoteValidator;
200200
$this->orderFactory = $orderFactory;
201201
$this->orderManagement = $orderManagement;
202202
$this->customerManagement = $customerManagement;
@@ -282,6 +282,7 @@ public function assignCustomer($cartId, $customerId, $storeId)
282282
throw new StateException(
283283
__('Cannot assign customer to the given cart. Customer already has active cart.')
284284
);
285+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
285286
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
286287
}
287288

@@ -454,7 +455,7 @@ protected function resolveItems(QuoteEntity $quote)
454455
protected function submitQuote(QuoteEntity $quote, $orderData = [])
455456
{
456457
$order = $this->orderFactory->create();
457-
$this->quoteValidator->validateBeforeSubmit($quote);
458+
$this->submitQuoteValidator->validateQuote($quote);
458459
if (!$quote->getCustomerIsGuest()) {
459460
if ($quote->getCustomerId()) {
460461
$this->_prepareCustomerQuote($quote);
@@ -509,6 +510,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
509510
$order->setCustomerFirstname($quote->getCustomerFirstname());
510511
$order->setCustomerMiddlename($quote->getCustomerMiddlename());
511512
$order->setCustomerLastname($quote->getCustomerLastname());
513+
$this->submitQuoteValidator->validateOrder($order);
512514

513515
$this->eventManager->dispatch(
514516
'sales_model_service_quote_submit_before',
@@ -625,12 +627,13 @@ private function rollbackAddresses(
625627
'exception' => $e,
626628
]
627629
);
630+
// phpcs:ignore Magento2.Exceptions.ThrowCatch
628631
} catch (\Exception $consecutiveException) {
629632
$message = sprintf(
630633
"An exception occurred on 'sales_model_service_quote_submit_failure' event: %s",
631634
$consecutiveException->getMessage()
632635
);
633-
636+
// phpcs:ignore Magento2.Exceptions.DirectThrow
634637
throw new \Exception($message, 0, $e);
635638
}
636639
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Sales\Model\Order;
12+
use Magento\Sales\Model\Order\Address\Validator as OrderAddressValidator;
13+
14+
/**
15+
* Validates quote and order before quote submit.
16+
*/
17+
class SubmitQuoteValidator
18+
{
19+
/**
20+
* @var QuoteValidator
21+
*/
22+
private $quoteValidator;
23+
24+
/**
25+
* @var OrderAddressValidator
26+
*/
27+
private $orderAddressValidator;
28+
29+
/**
30+
* @param QuoteValidator $quoteValidator
31+
* @param OrderAddressValidator $orderAddressValidator
32+
*/
33+
public function __construct(
34+
QuoteValidator $quoteValidator,
35+
OrderAddressValidator $orderAddressValidator
36+
) {
37+
$this->quoteValidator = $quoteValidator;
38+
$this->orderAddressValidator = $orderAddressValidator;
39+
}
40+
41+
/**
42+
* Validates quote.
43+
*
44+
* @param Quote $quote
45+
* @return void
46+
* @throws LocalizedException
47+
*/
48+
public function validateQuote(Quote $quote)
49+
{
50+
$this->quoteValidator->validateBeforeSubmit($quote);
51+
}
52+
53+
/**
54+
* Validates order.
55+
*
56+
* @param Order $order
57+
* @return void
58+
* @throws LocalizedException
59+
*/
60+
public function validateOrder(Order $order)
61+
{
62+
foreach ($order->getAddresses() as $address) {
63+
$errors = $this->orderAddressValidator->validate($address);
64+
if (!empty($errors)) {
65+
throw new LocalizedException(
66+
__("Failed address validation:\n%1", implode("\n", $errors))
67+
);
68+
}
69+
}
70+
}
71+
}

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
/**
1414
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1515
* @SuppressWarnings(PHPMD.TooManyFields)
16+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
17+
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
1618
*/
1719
class QuoteManagementTest extends \PHPUnit\Framework\TestCase
1820
{
@@ -22,9 +24,9 @@ class QuoteManagementTest extends \PHPUnit\Framework\TestCase
2224
protected $model;
2325

2426
/**
25-
* @var \Magento\Quote\Model\QuoteValidator|\PHPUnit_Framework_MockObject_MockObject
27+
* @var \Magento\Quote\Model\SubmitQuoteValidator|\PHPUnit_Framework_MockObject_MockObject
2628
*/
27-
protected $quoteValidator;
29+
protected $submitQuoteValidator;
2830

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

146-
$this->quoteValidator = $this->createMock(\Magento\Quote\Model\QuoteValidator::class);
148+
$this->submitQuoteValidator = $this->createMock(\Magento\Quote\Model\SubmitQuoteValidator::class);
147149
$this->eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
148150
$this->orderFactory = $this->createPartialMock(
149151
\Magento\Sales\Api\Data\OrderInterfaceFactory::class,
@@ -210,7 +212,7 @@ protected function setUp()
210212
\Magento\Quote\Model\QuoteManagement::class,
211213
[
212214
'eventManager' => $this->eventManager,
213-
'quoteValidator' => $this->quoteValidator,
215+
'submitQuoteValidator' => $this->submitQuoteValidator,
214216
'orderFactory' => $this->orderFactory,
215217
'orderManagement' => $this->orderManagement,
216218
'customerManagement' => $this->customerManagement,
@@ -560,7 +562,9 @@ public function testSubmit()
560562
$shippingAddress
561563
);
562564

563-
$this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
565+
$this->submitQuoteValidator->expects($this->once())
566+
->method('validateQuote')
567+
->with($quote);
564568
$this->quoteAddressToOrder->expects($this->once())
565569
->method('convert')
566570
->with($shippingAddress, $orderData)
@@ -655,7 +659,7 @@ public function testPlaceOrderIfCustomerIsGuest()
655659
->setConstructorArgs(
656660
[
657661
'eventManager' => $this->eventManager,
658-
'quoteValidator' => $this->quoteValidator,
662+
'quoteValidator' => $this->submitQuoteValidator,
659663
'orderFactory' => $this->orderFactory,
660664
'orderManagement' => $this->orderManagement,
661665
'customerManagement' => $this->customerManagement,
@@ -712,7 +716,7 @@ public function testPlaceOrder()
712716
->setConstructorArgs(
713717
[
714718
'eventManager' => $this->eventManager,
715-
'quoteValidator' => $this->quoteValidator,
719+
'quoteValidator' => $this->submitQuoteValidator,
716720
'orderFactory' => $this->orderFactory,
717721
'orderManagement' => $this->orderManagement,
718722
'customerManagement' => $this->customerManagement,
@@ -934,6 +938,9 @@ protected function prepareOrderFactory(
934938
return $order;
935939
}
936940

941+
/**
942+
* @throws NoSuchEntityException
943+
*/
937944
public function testGetCartForCustomer()
938945
{
939946
$customerId = 100;
@@ -978,6 +985,9 @@ protected function setPropertyValue(&$object, $property, $value)
978985
return $object;
979986
}
980987

988+
/**
989+
* @throws \Magento\Framework\Exception\LocalizedException
990+
*/
981991
public function testSubmitForCustomer()
982992
{
983993
$orderData = [];
@@ -1010,7 +1020,8 @@ public function testSubmitForCustomer()
10101020
$shippingAddress
10111021
);
10121022

1013-
$this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
1023+
$this->submitQuoteValidator->method('validateQuote')
1024+
->with($quote);
10141025
$this->quoteAddressToOrder->expects($this->once())
10151026
->method('convert')
10161027
->with($shippingAddress, $orderData)

0 commit comments

Comments
 (0)