Skip to content

Commit 7df9337

Browse files
committed
Merge remote-tracking branch 'tier4/ACP2E-2850' into PR_10_JAN_2024
2 parents c971859 + 4586c1e commit 7df9337

File tree

4 files changed

+175
-39
lines changed

4 files changed

+175
-39
lines changed

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

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66

77
namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
88

9+
use Magento\Checkout\Api\AgreementsValidatorInterface;
10+
use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
11+
use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
912
use Magento\CheckoutAgreements\Model\AgreementsProvider;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Exception\CouldNotSaveException;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Quote\Api\Data\AddressInterface;
17+
use Magento\Quote\Api\Data\PaymentInterface;
18+
use Magento\Quote\Api\GuestCartRepositoryInterface;
19+
use Magento\Store\Model\App\Emulation;
1020
use Magento\Store\Model\ScopeInterface;
1121
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1222

@@ -40,62 +50,85 @@ class GuestValidation
4050
private $activeStoreAgreementsFilter;
4151

4252
/**
43-
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
44-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
45-
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
53+
* @var GuestCartRepositoryInterface
54+
*/
55+
private GuestCartRepositoryInterface $quoteRepository;
56+
57+
/**
58+
* @var Emulation
59+
*/
60+
private Emulation $storeEmulation;
61+
62+
/**
63+
* @param AgreementsValidatorInterface $agreementsValidator
64+
* @param ScopeConfigInterface $scopeConfiguration
65+
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
4666
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
67+
* @param GuestCartRepositoryInterface $quoteRepository
68+
* @param Emulation $storeEmulation
4769
*/
4870
public function __construct(
4971
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
5072
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
5173
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
52-
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
74+
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
75+
GuestCartRepositoryInterface $quoteRepository,
76+
Emulation $storeEmulation
5377
) {
5478
$this->agreementsValidator = $agreementsValidator;
5579
$this->scopeConfiguration = $scopeConfiguration;
5680
$this->checkoutAgreementsList = $checkoutAgreementsList;
5781
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
82+
$this->quoteRepository = $quoteRepository;
83+
$this->storeEmulation = $storeEmulation;
5884
}
5985

6086
/**
6187
* Validates agreements before save payment information and order placing.
6288
*
63-
* @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
89+
* @param GuestPaymentInformationManagementInterface $subject
6490
* @param string $cartId
6591
* @param string $email
66-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
67-
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
68-
* @throws \Magento\Framework\Exception\CouldNotSaveException
92+
* @param PaymentInterface $paymentMethod
93+
* @param AddressInterface|null $billingAddress
6994
* @return void
7095
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
96+
* @throws CouldNotSaveException|NoSuchEntityException
7197
*/
7298
public function beforeSavePaymentInformationAndPlaceOrder(
73-
\Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
99+
GuestPaymentInformationManagementInterface $subject,
74100
$cartId,
75101
$email,
76-
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
77-
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
102+
PaymentInterface $paymentMethod,
103+
AddressInterface $billingAddress = null
78104
) {
79105
if ($this->isAgreementEnabled()) {
80-
$this->validateAgreements($paymentMethod);
106+
$quote = $this->quoteRepository->get($cartId);
107+
$storeId = $quote->getStoreId();
108+
$this->validateAgreements($paymentMethod, $storeId);
81109
}
82110
}
83111

84112
/**
85113
* Validates agreements.
86114
*
87-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
88-
* @throws \Magento\Framework\Exception\CouldNotSaveException
115+
* @param PaymentInterface $paymentMethod
116+
* @param int $storeId
89117
* @return void
118+
* @throws CouldNotSaveException
90119
*/
91-
private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
120+
private function validateAgreements(PaymentInterface $paymentMethod, int $storeId)
92121
{
93122
$agreements = $paymentMethod->getExtensionAttributes() === null
94123
? []
95124
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
96125

97-
if (!$this->agreementsValidator->isValid($agreements)) {
98-
throw new \Magento\Framework\Exception\CouldNotSaveException(
126+
$this->storeEmulation->startEnvironmentEmulation($storeId);
127+
$isValid = $this->agreementsValidator->isValid($agreements);
128+
$this->storeEmulation->stopEnvironmentEmulation();
129+
130+
if (!$isValid) {
131+
throw new CouldNotSaveException(
99132
__(
100133
"The order wasn't placed. "
101134
. "First, agree to the terms and conditions, then try placing your order again."

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66

77
namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
88

9+
use Magento\Checkout\Api\AgreementsValidatorInterface;
10+
use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
911
use Magento\CheckoutAgreements\Model\AgreementsProvider;
1012
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
13+
use Magento\CheckoutAgreements\Model\EmulateStore;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\Exception\CouldNotSaveException;
1116
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Api\Data\PaymentInterface;
18+
use Magento\Store\Model\App\Emulation;
1219
use Magento\Store\Model\ScopeInterface;
1320

1421
/**
@@ -37,31 +44,37 @@ class Validation
3744
private $activeStoreAgreementsFilter;
3845

3946
/**
40-
* Quote repository.
41-
*
4247
* @var \Magento\Quote\Api\CartRepositoryInterface
4348
*/
4449
private $quoteRepository;
4550

4651
/**
47-
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
48-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
49-
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
52+
* @var Emulation
53+
*/
54+
private Emulation $storeEmulation;
55+
56+
/**
57+
* @param AgreementsValidatorInterface $agreementsValidator
58+
* @param ScopeConfigInterface $scopeConfiguration
59+
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
5060
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
5161
* @param CartRepositoryInterface $quoteRepository
62+
* @param Emulation $storeEmulation
5263
*/
5364
public function __construct(
5465
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
5566
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
5667
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
5768
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
58-
CartRepositoryInterface $quoteRepository
69+
CartRepositoryInterface $quoteRepository,
70+
Emulation $storeEmulation
5971
) {
6072
$this->agreementsValidator = $agreementsValidator;
6173
$this->scopeConfiguration = $scopeConfiguration;
6274
$this->checkoutAgreementsList = $checkoutAgreementsList;
6375
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
6476
$this->quoteRepository = $quoteRepository;
77+
$this->storeEmulation = $storeEmulation;
6578
}
6679

6780
/**
@@ -82,24 +95,31 @@ public function beforeSavePaymentInformationAndPlaceOrder(
8295
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
8396
) {
8497
if ($this->isAgreementEnabled()) {
85-
$this->validateAgreements($paymentMethod);
98+
$quote = $this->quoteRepository->get($cartId);
99+
$storeId = $quote->getStoreId();
100+
$this->validateAgreements($paymentMethod, $storeId);
86101
}
87102
}
88103

89104
/**
90105
* Validate agreements base on the payment method
91106
*
92-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
93-
* @throws \Magento\Framework\Exception\CouldNotSaveException
107+
* @param PaymentInterface $paymentMethod
108+
* @param int $storeId
94109
* @return void
110+
* @throws CouldNotSaveException
95111
*/
96-
protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
112+
private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod, int $storeId)
97113
{
98114
$agreements = $paymentMethod->getExtensionAttributes() === null
99115
? []
100116
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
101117

102-
if (!$this->agreementsValidator->isValid($agreements)) {
118+
$this->storeEmulation->startEnvironmentEmulation($storeId);
119+
$isValid = $this->agreementsValidator->isValid($agreements);
120+
$this->storeEmulation->stopEnvironmentEmulation();
121+
122+
if (!$isValid) {
103123
throw new \Magento\Framework\Exception\CouldNotSaveException(
104124
__(
105125
"The order wasn't placed. "

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
use Magento\CheckoutAgreements\Model\Checkout\Plugin\GuestValidation;
1616
use Magento\Framework\Api\SearchCriteria;
1717
use Magento\Framework\App\Config\ScopeConfigInterface;
18+
use Magento\Quote\Api\CartRepositoryInterface;
1819
use Magento\Quote\Api\Data\AddressInterface;
19-
use Magento\Quote\Api\Data\PaymentExtension;
20+
use Magento\Quote\Api\Data\PaymentExtensionInterface;
2021
use Magento\Quote\Api\Data\PaymentInterface;
22+
use Magento\Quote\Api\GuestCartRepositoryInterface;
23+
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
24+
use Magento\Quote\Model\Quote;
25+
use Magento\Store\Model\App\Emulation;
2126
use Magento\Store\Model\ScopeInterface;
2227
use PHPUnit\Framework\MockObject\MockObject;
2328
use PHPUnit\Framework\MockObject\RuntimeException;
@@ -73,6 +78,26 @@ class GuestValidationTest extends TestCase
7378
*/
7479
private $agreementsFilterMock;
7580

81+
/**
82+
* @var Quote|MockObject
83+
*/
84+
private Quote|MockObject $quoteMock;
85+
86+
/**
87+
* @var MaskedQuoteIdToQuoteId|MockObject
88+
*/
89+
private MaskedQuoteIdToQuoteId|MockObject $maskedQuoteIdToQuoteIdMock;
90+
91+
/**
92+
* @var CartRepositoryInterface|MockObject
93+
*/
94+
private CartRepositoryInterface|MockObject $cartRepositoryMock;
95+
96+
/**
97+
* @var Emulation|MockObject
98+
*/
99+
private Emulation|MockObject $storeEmulationMock;
100+
76101
protected function setUp(): void
77102
{
78103
$this->agreementsValidatorMock = $this->getMockForAbstractClass(AgreementsValidatorInterface::class);
@@ -87,18 +112,34 @@ protected function setUp(): void
87112
$this->agreementsFilterMock = $this->createMock(
88113
ActiveStoreAgreementsFilter::class
89114
);
115+
$this->quoteMock = $this->createMock(Quote::class);
116+
$this->maskedQuoteIdToQuoteIdMock = $this->createMock(MaskedQuoteIdToQuoteId::class);
117+
$this->cartRepositoryMock = $this->createMock(GuestCartRepositoryInterface::class);
118+
$this->storeEmulationMock = $this->createMock(Emulation::class);
119+
120+
$storeId = 1;
121+
$this->quoteMock->expects($this->once())
122+
->method('getStoreId')
123+
->willReturn($storeId);
124+
$this->cartRepositoryMock->expects($this->once())
125+
->method('get')
126+
->with('0CQwCntNHR4yN9P5PUAzbxatvDvBXOce')
127+
->willReturn($this->quoteMock);
90128

91129
$this->model = new GuestValidation(
92130
$this->agreementsValidatorMock,
93131
$this->scopeConfigMock,
94132
$this->checkoutAgreementsListMock,
95-
$this->agreementsFilterMock
133+
$this->agreementsFilterMock,
134+
$this->cartRepositoryMock,
135+
$this->storeEmulationMock
96136
);
97137
}
98138

99139
public function testBeforeSavePaymentInformationAndPlaceOrder()
100140
{
101-
$cartId = '100';
141+
$storeId = 1;
142+
$cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce';
102143
$email = 'email@example.com';
103144
$agreements = [1, 2, 3];
104145
$this->scopeConfigMock
@@ -119,6 +160,11 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
119160
$this->paymentMock->expects(static::atLeastOnce())
120161
->method('getExtensionAttributes')
121162
->willReturn($this->extensionAttributesMock);
163+
$this->storeEmulationMock->expects($this->once())
164+
->method('startEnvironmentEmulation')
165+
->with($storeId);
166+
$this->storeEmulationMock->expects($this->once())
167+
->method('stopEnvironmentEmulation');
122168
$this->model->beforeSavePaymentInformationAndPlaceOrder(
123169
$this->subjectMock,
124170
$cartId,
@@ -131,7 +177,8 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
131177
public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid()
132178
{
133179
$this->expectException('Magento\Framework\Exception\CouldNotSaveException');
134-
$cartId = 100;
180+
$storeId = 1;
181+
$cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce';
135182
$email = 'email@example.com';
136183
$agreements = [1, 2, 3];
137184
$this->scopeConfigMock
@@ -152,6 +199,11 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
152199
$this->paymentMock->expects(static::atLeastOnce())
153200
->method('getExtensionAttributes')
154201
->willReturn($this->extensionAttributesMock);
202+
$this->storeEmulationMock->expects($this->once())
203+
->method('startEnvironmentEmulation')
204+
->with($storeId);
205+
$this->storeEmulationMock->expects($this->once())
206+
->method('stopEnvironmentEmulation');
155207
$this->model->beforeSavePaymentInformationAndPlaceOrder(
156208
$this->subjectMock,
157209
$cartId,
@@ -172,9 +224,10 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
172224
*/
173225
private function getPaymentExtension(): MockObject
174226
{
175-
$mockBuilder = $this->getMockBuilder(PaymentExtension::class);
227+
$mockBuilder = $this->getMockBuilder(PaymentExtensionInterface::class)
228+
->disableOriginalConstructor();
176229
try {
177-
$mockBuilder->addMethods(['getAgreementIds']);
230+
$mockBuilder->addMethods(['getAgreementIds', 'setAgreementIds']);
178231
} catch (RuntimeException $e) {
179232
// Payment extension already generated.
180233
}

0 commit comments

Comments
 (0)