Skip to content

Commit 2618198

Browse files
committed
Merge branch 'MC-36954' of https://github.com/magento-mpi/magento2ce into TANGO-PR-10-21-2020-24
2 parents 1585a7b + a56f2c5 commit 2618198

File tree

10 files changed

+183
-19
lines changed

10 files changed

+183
-19
lines changed

app/code/Magento/OfflinePayments/Model/Purchaseorder.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/**
1111
* Class Purchaseorder
1212
*
13+
* Update additional payments fields and validate the payment data
1314
* @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
1415
*
1516
* @api
@@ -68,10 +69,6 @@ public function validate()
6869
{
6970
parent::validate();
7071

71-
if (empty($this->getInfoInstance()->getPoNumber())) {
72-
throw new LocalizedException(__('Purchase order number is a required field.'));
73-
}
74-
7572
return $this;
7673
}
7774
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\OfflinePayments\Plugin;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\OfflinePayments\Model\Purchaseorder;
12+
use Magento\Quote\Model\Quote;
13+
use Magento\Quote\Model\QuoteManagement;
14+
15+
/**
16+
* Class ValidatePurchaseOrderNumber
17+
*
18+
* Validate purchase order number before submit order
19+
*/
20+
class ValidatePurchaseOrderNumber
21+
{
22+
/**
23+
* Before submitOrder plugin.
24+
*
25+
* @param QuoteManagement $subject
26+
* @param Quote $quote
27+
* @param array $orderData
28+
* @return void
29+
* @throws LocalizedException
30+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
31+
*/
32+
public function beforeSubmit(
33+
QuoteManagement $subject,
34+
Quote $quote,
35+
array $orderData = []
36+
): void {
37+
$payment = $quote->getPayment();
38+
if ($payment->getMethod() === Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE
39+
&& empty($payment->getPoNumber())) {
40+
throw new LocalizedException(__('Purchase order number is a required field.'));
41+
}
42+
}
43+
}

app/code/Magento/OfflinePayments/Test/Unit/Model/PurchaseorderTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ public function testAssignData()
6666

6767
public function testValidate()
6868
{
69-
$this->expectException(LocalizedException::class);
70-
$this->expectExceptionMessage('Purchase order number is a required field.');
71-
7269
$data = new DataObject([]);
7370

7471
$addressMock = $this->getMockForAbstractClass(OrderAddressInterface::class);
@@ -84,6 +81,7 @@ public function testValidate()
8481
$this->object->setData('info_instance', $instance);
8582
$this->object->assignData($data);
8683

87-
$this->object->validate();
84+
$result = $this->object->validate();
85+
$this->assertEquals($result, $this->object);
8886
}
8987
}

app/code/Magento/OfflinePayments/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": "~7.3.0||~7.4.0",
99
"magento/framework": "*",
1010
"magento/module-checkout": "*",
11-
"magento/module-payment": "*"
11+
"magento/module-payment": "*",
12+
"magento/module-quote": "*"
1213
},
1314
"suggest": {
1415
"magento/module-config": "*"

app/code/Magento/OfflinePayments/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="\Magento\Quote\Model\QuoteManagement">
17+
<plugin name="validate_purchase_order_number" type="Magento\OfflinePayments\Plugin\ValidatePurchaseOrderNumber"/>
18+
</type>
1619
</config>

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPurchaseOrderPaymentMethodOnCartTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\GraphQl\Quote\Customer;
99

10-
use Exception;
1110
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1211
use Magento\Integration\Api\CustomerTokenServiceInterface;
1312
use Magento\OfflinePayments\Model\Purchaseorder;
@@ -100,9 +99,6 @@ public function testSetPurchaseOrderPaymentMethodOnCartWithSimpleProduct()
10099
*/
101100
public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumber()
102101
{
103-
$this->expectException(\Exception::class);
104-
$this->expectExceptionMessage('Purchase order number is a required field.');
105-
106102
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
107103
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
108104

@@ -122,7 +118,18 @@ public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumbe
122118
}
123119
}
124120
QUERY;
125-
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
121+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
122+
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
123+
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
124+
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
125+
self::assertEquals(
126+
$methodCode,
127+
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']
128+
);
129+
self::assertArrayNotHasKey(
130+
'purchase_order_number',
131+
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']
132+
);
126133
}
127134

128135
/**

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPurchaseOrderPaymentMethodOnCartTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\GraphQl\Quote\Guest;
99

10-
use Exception;
1110
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1211
use Magento\OfflinePayments\Model\Purchaseorder;
1312
use Magento\TestFramework\Helper\Bootstrap;
@@ -91,9 +90,6 @@ public function testSetPurchaseOrderPaymentMethodOnCartWithSimpleProduct()
9190
*/
9291
public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumber()
9392
{
94-
$this->expectException(\Exception::class);
95-
$this->expectExceptionMessage('Purchase order number is a required field.');
96-
9793
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
9894
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
9995

@@ -113,7 +109,18 @@ public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumbe
113109
}
114110
}
115111
QUERY;
116-
$this->graphQlMutation($query);
112+
$response = $this->graphQlMutation($query);
113+
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
114+
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
115+
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
116+
self::assertEquals(
117+
$methodCode,
118+
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']
119+
);
120+
self::assertArrayNotHasKey(
121+
'purchase_order_number',
122+
$response['setPaymentMethodOnCart']['cart']['selected_payment_method']
123+
);
117124
}
118125

119126
/**

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Customer\Model\Vat;
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
1515
use Magento\Framework\DataObject;
16+
use Magento\Framework\Exception\CouldNotSaveException;
1617
use Magento\Framework\Exception\LocalizedException;
1718
use Magento\Framework\Exception\StateException;
1819
use Magento\Framework\ObjectManagerInterface;
@@ -134,6 +135,49 @@ public function testSubmitGuestCustomer(): void
134135
self::assertEquals(3, $quoteAfterOrderPlaced->getCustomerTaxClassId());
135136
}
136137

138+
/**
139+
* Creates order with purchase_order payment method
140+
*
141+
* @magentoAppIsolation enabled
142+
* @magentoDataFixture Magento/Sales/_files/quote_with_purchase_order.php
143+
*
144+
* @return void
145+
* @throws CouldNotSaveException
146+
*/
147+
public function testSubmitWithPurchaseOrder(): void
148+
{
149+
$paymentMethodName = 'purchaseorder';
150+
$poNumber = '12345678';
151+
$quote = $this->getQuoteByReservedOrderId->execute('test_order_1');
152+
$quote->getPayment()->setPoNumber($poNumber);
153+
$quote->collectTotals()->save();
154+
$orderId = $this->cartManagement->placeOrder($quote->getId());
155+
$order = $this->orderRepository->get($orderId);
156+
$orderItems = $order->getItems();
157+
$this->assertCount(1, $orderItems);
158+
$payment = $order->getPayment();
159+
$this->assertEquals($paymentMethodName, $payment->getMethod());
160+
$this->assertEquals($poNumber, $payment->getPoNumber());
161+
}
162+
163+
/**
164+
* Creates order with purchase_order payment method without po_number
165+
*
166+
* @magentoAppIsolation enabled
167+
* @magentoDataFixture Magento/Sales/_files/quote_with_purchase_order.php
168+
*
169+
* @return void
170+
* @throws CouldNotSaveException
171+
*/
172+
public function testSubmitWithPurchaseOrderWithException(): void
173+
{
174+
$this->expectException(LocalizedException::class);
175+
$this->expectExceptionMessage('Purchase order number is a required field.');
176+
177+
$quote = $this->getQuoteByReservedOrderId->execute('test_order_1');
178+
$this->cartManagement->placeOrder($quote->getId());
179+
}
180+
137181
/**
138182
* Tries to create order with product that has child items and one of them was deleted.
139183
*
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
use Magento\Quote\Model\Quote\Address\Rate;
9+
use Magento\Quote\Model\QuoteFactory;
10+
use Magento\Quote\Model\QuoteIdMaskFactory;
11+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
14+
15+
Resolver::getInstance()->requireDataFixture('Magento/Checkout/_files/quote_with_address.php');
16+
/** @var QuoteFactory $quoteFactory */
17+
$quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class);
18+
/** @var QuoteResource $quoteResource */
19+
$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class);
20+
$quote = $quoteFactory->create();
21+
$quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
22+
23+
/** @var $rate Rate */
24+
$rate = Bootstrap::getObjectManager()->create(
25+
Rate::class
26+
);
27+
$rate->setCode('freeshipping_freeshipping');
28+
$rate->getPrice(100);
29+
30+
$quote->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
31+
$quote->getShippingAddress()->addShippingRate($rate);
32+
$quote->getPayment()->setMethod('purchaseorder');
33+
34+
$quote->collectTotals();
35+
$quote->save();
36+
$quote->getPayment()->setMethod('purchaseorder');
37+
38+
$quoteIdMask = Bootstrap::getObjectManager()
39+
->create(QuoteIdMaskFactory::class)
40+
->create();
41+
$quoteIdMask->setQuoteId($quote->getId());
42+
$quoteIdMask->setDataChanges(true);
43+
$quoteIdMask->save();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Rollback for quote_with_purchase_order.php fixture.
4+
*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
declare(strict_types=1);
9+
10+
use Magento\Framework\Registry;
11+
use Magento\Quote\Model\Quote;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
14+
15+
Resolver::getInstance()->requireDataFixture('Magento/Sales/_files/default_rollback.php');
16+
17+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
18+
$objectManager = Bootstrap::getObjectManager();
19+
$objectManager->get(Registry::class)->unregister('quote');
20+
$quote = $objectManager->create(Quote::class);
21+
$quote->load('test_order_1', 'reserved_order_id')->delete();

0 commit comments

Comments
 (0)