Skip to content

Commit eb01752

Browse files
committed
Merge remote-tracking branch 'origin/MC-30258' into 2.4-develop-pr9
2 parents c2e2646 + b1d69cb commit eb01752

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

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

Lines changed: 25 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-
* Class Validation
15+
* Class Validation validates the agreement based on the payment method
1516
*/
1617
class Validation
1718
{
@@ -35,25 +36,37 @@ 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
/**
68+
* Check validation before saving the payment information and place order
69+
*
5770
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
5871
* @param int $cartId
5972
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
@@ -74,26 +87,32 @@ public function beforeSavePaymentInformationAndPlaceOrder(
7487
}
7588

7689
/**
90+
* Check validation before saving the payment information
91+
*
7792
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
7893
* @param int $cartId
7994
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
8095
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
81-
* @throws \Magento\Framework\Exception\CouldNotSaveException
8296
* @return void
8397
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
98+
* @throws \Magento\Framework\Exception\NoSuchEntityException
99+
* @throws \Magento\Framework\Exception\CouldNotSaveException
84100
*/
85101
public function beforeSavePaymentInformation(
86102
\Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
87103
$cartId,
88104
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
89105
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
90106
) {
91-
if ($this->isAgreementEnabled()) {
107+
$quote = $this->quoteRepository->getActive($cartId);
108+
if ($this->isAgreementEnabled() && !$quote->getIsMultiShipping()) {
92109
$this->validateAgreements($paymentMethod);
93110
}
94111
}
95112

96113
/**
114+
* Validate agreements base on the payment method
115+
*
97116
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
98117
* @throws \Magento\Framework\Exception\CouldNotSaveException
99118
* @return void
@@ -116,6 +135,7 @@ protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $
116135

117136
/**
118137
* Verify if agreement validation needed
138+
*
119139
* @return bool
120140
*/
121141
protected function isAgreementEnabled()

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Store\Model\ScopeInterface;
1111

1212
/**
13-
* Class ValidationTest
13+
* Class ValidationTest validates the agreement based on the payment method
1414
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1515
*/
1616
class ValidationTest extends \PHPUnit\Framework\TestCase
@@ -60,12 +60,24 @@ class ValidationTest extends \PHPUnit\Framework\TestCase
6060
*/
6161
private $agreementsFilterMock;
6262

63+
/**
64+
* @var \PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $quoteMock;
67+
68+
/**
69+
* @var \PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $quoteRepositoryMock;
72+
6373
protected function setUp()
6474
{
6575
$this->agreementsValidatorMock = $this->createMock(\Magento\Checkout\Api\AgreementsValidatorInterface::class);
6676
$this->subjectMock = $this->createMock(\Magento\Checkout\Api\PaymentInformationManagementInterface::class);
6777
$this->paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
6878
$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);
6981
$this->extensionAttributesMock = $this->createPartialMock(
7082
\Magento\Quote\Api\Data\PaymentExtension::class,
7183
['getAgreementIds']
@@ -82,7 +94,8 @@ protected function setUp()
8294
$this->agreementsValidatorMock,
8395
$this->scopeConfigMock,
8496
$this->checkoutAgreementsListMock,
85-
$this->agreementsFilterMock
97+
$this->agreementsFilterMock,
98+
$this->quoteRepositoryMock
8699
);
87100
}
88101

@@ -96,6 +109,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
96109
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
97110
->willReturn(true);
98111
$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);
99121
$this->agreementsFilterMock->expects($this->once())
100122
->method('buildSearchCriteria')
101123
->willReturn($searchCriteriaMock);
@@ -124,6 +146,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
124146
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
125147
->willReturn(true);
126148
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
149+
$this->quoteMock
150+
->expects($this->once())
151+
->method('getIsMultiShipping')
152+
->willReturn(false);
153+
$this->quoteRepositoryMock
154+
->expects($this->once())
155+
->method('getActive')
156+
->with($cartId)
157+
->willReturn($this->quoteMock);
127158
$this->agreementsFilterMock->expects($this->once())
128159
->method('buildSearchCriteria')
129160
->willReturn($searchCriteriaMock);
@@ -152,6 +183,15 @@ public function testBeforeSavePaymentInformation()
152183
->method('isSetFlag')
153184
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
154185
->willReturn(true);
186+
$this->quoteMock
187+
->expects($this->once())
188+
->method('getIsMultiShipping')
189+
->willReturn(false);
190+
$this->quoteRepositoryMock
191+
->expects($this->once())
192+
->method('getActive')
193+
->with($cartId)
194+
->willReturn($this->quoteMock);
155195
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
156196
$this->agreementsFilterMock->expects($this->once())
157197
->method('buildSearchCriteria')

0 commit comments

Comments
 (0)