Skip to content

Commit 617ae08

Browse files
Merge branch 'MC-30258' into MC-23986
# Conflicts: # app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/Validation.php # app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/ValidationTest.php
2 parents df929c0 + b1d69cb commit 617ae08

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/Validation.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
88

99
use Magento\CheckoutAgreements\Model\AgreementsProvider;
10-
use Magento\Store\Model\ScopeInterface;
1110
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Store\Model\ScopeInterface;
1213

1314
/**
14-
* Checkout agreements validation.
15+
* Class Validation validates the agreement based on the payment method
1516
*/
1617
class Validation
1718
{
@@ -35,22 +36,32 @@ class Validation
3536
*/
3637
private $activeStoreAgreementsFilter;
3738

39+
/**
40+
* Quote repository.
41+
*
42+
* @var \Magento\Quote\Api\CartRepositoryInterface
43+
*/
44+
private $quoteRepository;
45+
3846
/**
3947
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
4048
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
4149
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
4250
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
51+
* @param CartRepositoryInterface $quoteRepository
4352
*/
4453
public function __construct(
4554
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
4655
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
4756
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
48-
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
57+
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
58+
CartRepositoryInterface $quoteRepository
4959
) {
5060
$this->agreementsValidator = $agreementsValidator;
5161
$this->scopeConfiguration = $scopeConfiguration;
5262
$this->checkoutAgreementsList = $checkoutAgreementsList;
5363
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
64+
$this->quoteRepository = $quoteRepository;
5465
}
5566

5667
/**
@@ -76,13 +87,37 @@ public function beforeSavePaymentInformationAndPlaceOrder(
7687
}
7788

7889
/**
79-
* Validates agreements.
90+
* Check validation before saving the payment information
91+
*
92+
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
93+
* @param int $cartId
94+
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
95+
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
96+
* @return void
97+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
98+
* @throws \Magento\Framework\Exception\NoSuchEntityException
99+
* @throws \Magento\Framework\Exception\CouldNotSaveException
100+
*/
101+
public function beforeSavePaymentInformation(
102+
\Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
103+
$cartId,
104+
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
105+
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
106+
) {
107+
$quote = $this->quoteRepository->getActive($cartId);
108+
if ($this->isAgreementEnabled() && !$quote->getIsMultiShipping()) {
109+
$this->validateAgreements($paymentMethod);
110+
}
111+
}
112+
113+
/**
114+
* Validate agreements base on the payment method
80115
*
81116
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
82117
* @throws \Magento\Framework\Exception\CouldNotSaveException
83118
* @return void
84119
*/
85-
private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
120+
protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
86121
{
87122
$agreements = $paymentMethod->getExtensionAttributes() === null
88123
? []

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/ValidationTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Store\Model\ScopeInterface;
1111

1212
/**
13+
* Class ValidationTest validates the agreement based on the payment method
1314
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1415
*/
1516
class ValidationTest extends \PHPUnit\Framework\TestCase
@@ -59,12 +60,24 @@ class ValidationTest extends \PHPUnit\Framework\TestCase
5960
*/
6061
private $agreementsFilterMock;
6162

63+
/**
64+
* @var \PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $quoteMock;
67+
68+
/**
69+
* @var \PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $quoteRepositoryMock;
72+
6273
protected function setUp()
6374
{
6475
$this->agreementsValidatorMock = $this->createMock(\Magento\Checkout\Api\AgreementsValidatorInterface::class);
6576
$this->subjectMock = $this->createMock(\Magento\Checkout\Api\PaymentInformationManagementInterface::class);
6677
$this->paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
6778
$this->addressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
79+
$this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getIsMultiShipping']);
80+
$this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
6881
$this->extensionAttributesMock = $this->createPartialMock(
6982
\Magento\Quote\Api\Data\PaymentExtension::class,
7083
['getAgreementIds']
@@ -81,7 +94,8 @@ protected function setUp()
8194
$this->agreementsValidatorMock,
8295
$this->scopeConfigMock,
8396
$this->checkoutAgreementsListMock,
84-
$this->agreementsFilterMock
97+
$this->agreementsFilterMock,
98+
$this->quoteRepositoryMock
8599
);
86100
}
87101

@@ -95,6 +109,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
95109
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
96110
->willReturn(true);
97111
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
112+
$this->quoteMock
113+
->expects($this->once())
114+
->method('getIsMultiShipping')
115+
->willReturn(false);
116+
$this->quoteRepositoryMock
117+
->expects($this->once())
118+
->method('getActive')
119+
->with($cartId)
120+
->willReturn($this->quoteMock);
98121
$this->agreementsFilterMock->expects($this->once())
99122
->method('buildSearchCriteria')
100123
->willReturn($searchCriteriaMock);
@@ -128,6 +151,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
128151
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
129152
->willReturn(true);
130153
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
154+
$this->quoteMock
155+
->expects($this->once())
156+
->method('getIsMultiShipping')
157+
->willReturn(false);
158+
$this->quoteRepositoryMock
159+
->expects($this->once())
160+
->method('getActive')
161+
->with($cartId)
162+
->willReturn($this->quoteMock);
131163
$this->agreementsFilterMock->expects($this->once())
132164
->method('buildSearchCriteria')
133165
->willReturn($searchCriteriaMock);
@@ -151,4 +183,38 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
151183
"The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
152184
);
153185
}
186+
187+
public function testBeforeSavePaymentInformation()
188+
{
189+
$cartId = 100;
190+
$agreements = [1, 2, 3];
191+
$this->scopeConfigMock
192+
->expects($this->once())
193+
->method('isSetFlag')
194+
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
195+
->willReturn(true);
196+
$this->quoteMock
197+
->expects($this->once())
198+
->method('getIsMultiShipping')
199+
->willReturn(false);
200+
$this->quoteRepositoryMock
201+
->expects($this->once())
202+
->method('getActive')
203+
->with($cartId)
204+
->willReturn($this->quoteMock);
205+
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
206+
$this->agreementsFilterMock->expects($this->once())
207+
->method('buildSearchCriteria')
208+
->willReturn($searchCriteriaMock);
209+
$this->checkoutAgreementsListMock->expects($this->once())
210+
->method('getList')
211+
->with($searchCriteriaMock)
212+
->willReturn([1]);
213+
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
214+
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
215+
$this->paymentMock->expects(static::atLeastOnce())
216+
->method('getExtensionAttributes')
217+
->willReturn($this->extensionAttributesMock);
218+
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
219+
}
154220
}

0 commit comments

Comments
 (0)