Skip to content

Commit 7b6fc3c

Browse files
committed
ACP2E-1338: Google reCaptcha in Incorrect position
1 parent 3798cb1 commit 7b6fc3c

File tree

2 files changed

+348
-0
lines changed

2 files changed

+348
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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\ReCaptchaPaypal\Test\Unit\Model;
9+
10+
use Magento\Framework\Session\SessionManager;
11+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
12+
use Magento\Quote\Api\Data\CartInterface;
13+
use Magento\ReCaptchaPaypal\Model\ReCaptchaSession;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class ReCaptchaSessionTest extends TestCase
18+
{
19+
/**
20+
* @var TimezoneInterface|MockObject
21+
*/
22+
private $timezone;
23+
24+
/**
25+
* @var SessionManager|MockObject
26+
*/
27+
private $transparentSession;
28+
29+
/**
30+
* @var SessionManager|MockObject
31+
*/
32+
private $checkoutSession;
33+
34+
/**
35+
* @var ReCaptchaSession
36+
*/
37+
private $model;
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
protected function setUp(): void
43+
{
44+
parent::setUp();
45+
$this->timezone = $this->getMockForAbstractClass(TimezoneInterface::class);
46+
$this->transparentSession = $this->getMockBuilder(SessionManager::class)
47+
->disableOriginalConstructor()
48+
->onlyMethods(['getData'])
49+
->addMethods(['setData', 'unsetData'])
50+
->getMock();
51+
$this->checkoutSession = $this->getMockBuilder(SessionManager::class)
52+
->disableOriginalConstructor()
53+
->addMethods(['getQuote'])
54+
->getMock();
55+
$this->model = new ReCaptchaSession(
56+
$this->timezone,
57+
$this->transparentSession,
58+
$this->checkoutSession
59+
);
60+
}
61+
62+
public function testSaveIfThereIsNoActiveQuote(): void
63+
{
64+
$this->checkoutSession->expects($this->once())
65+
->method('getQuote')
66+
->willReturn(null);
67+
$this->assertFalse($this->model->save());
68+
}
69+
70+
public function testSaveIfThereIsActiveQuote(): void
71+
{
72+
$quote = $this->getMockForAbstractClass(CartInterface::class);
73+
$quote->expects($this->once())
74+
->method('getId')
75+
->willReturn(1);
76+
$this->checkoutSession->expects($this->exactly(2))
77+
->method('getQuote')
78+
->willReturn($quote);
79+
$this->timezone->expects($this->once())
80+
->method('date')
81+
->willReturn(new \Datetime('@1670607221'));
82+
$this->transparentSession->expects($this->once())
83+
->method('setData')
84+
->with('paypal_payflowpro_recaptcha', ['quote_id' => 1, 'verified_at' => 1670607221]);
85+
$this->assertTrue($this->model->save());
86+
}
87+
88+
public function testIsInvalidIfQuoteIdIsMissing(): void
89+
{
90+
$this->transparentSession->expects($this->once())
91+
->method('getData')
92+
->with('paypal_payflowpro_recaptcha')
93+
->willReturn(null);
94+
$this->assertFalse($this->model->isValid(1));
95+
}
96+
97+
public function testIsInvalidIfQuoteIdDoesNotMatch(): void
98+
{
99+
$this->transparentSession->expects($this->once())
100+
->method('getData')
101+
->with('paypal_payflowpro_recaptcha')
102+
->willReturn(['quote_id' => 2, 'verified_at' => 1670607221]);
103+
$this->assertFalse($this->model->isValid(1));
104+
}
105+
106+
public function testIsInvalidIfExpired(): void
107+
{
108+
$this->timezone->expects($this->once())
109+
->method('date')
110+
->willReturn(new \Datetime('@1670607342'));
111+
$this->transparentSession->expects($this->once())
112+
->method('getData')
113+
->with('paypal_payflowpro_recaptcha')
114+
->willReturn(['quote_id' => 1, 'verified_at' => 1670607221]);
115+
$this->assertFalse($this->model->isValid(1));
116+
}
117+
118+
public function testIsInvalidIfNotExpired(): void
119+
{
120+
$this->timezone->expects($this->once())
121+
->method('date')
122+
->willReturn(new \Datetime('@1670607340'));
123+
$this->transparentSession->expects($this->once())
124+
->method('getData')
125+
->with('paypal_payflowpro_recaptcha')
126+
->willReturn(['quote_id' => 1, 'verified_at' => 1670607221]);
127+
$this->transparentSession->expects($this->once())
128+
->method('unsetData')
129+
->with('paypal_payflowpro_recaptcha');
130+
$this->assertTrue($this->model->isValid(1));
131+
}
132+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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\ReCaptchaPaypal\Test\Unit\Observer;
9+
10+
use Magento\Framework\App\Action\AbstractAction;
11+
use Magento\Framework\App\ActionFlag;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\App\ResponseInterface;
14+
use Magento\Framework\Event\Observer;
15+
use Magento\Framework\Serialize\SerializerInterface;
16+
use Magento\Framework\Validation\ValidationResult;
17+
use Magento\ReCaptchaPaypal\Model\ReCaptchaSession;
18+
use Magento\ReCaptchaPaypal\Observer\PayPalObserver;
19+
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
20+
use Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface;
21+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
22+
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface;
23+
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
24+
use Magento\ReCaptchaValidationApi\Model\ValidationErrorMessagesProvider;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
use PHPUnit\Framework\TestCase;
27+
use Psr\Log\LoggerInterface;
28+
29+
class PayPalObserverTest extends TestCase
30+
{
31+
/**
32+
* @var ValidatorInterface|MockObject
33+
*/
34+
private $captchaValidator;
35+
36+
/**
37+
* @var IsCaptchaEnabledInterface|MockObject
38+
*/
39+
private $isCaptchaEnabled;
40+
41+
/**
42+
* @var ReCaptchaSession|MockObject
43+
*/
44+
private $reCaptchaSession;
45+
46+
/**
47+
* @var PayPalObserver
48+
*/
49+
private $model;
50+
51+
/**
52+
* @var Observer
53+
*/
54+
private $observer;
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
protected function setUp(): void
60+
{
61+
parent::setUp();
62+
$captchaResponseResolver = $this->getMockForAbstractClass(CaptchaResponseResolverInterface::class);
63+
$validationConfigResolver = $this->getMockForAbstractClass(ValidationConfigResolverInterface::class);
64+
$this->captchaValidator = $this->getMockForAbstractClass(ValidatorInterface::class);
65+
$actionFlag = $this->createMock(ActionFlag::class);
66+
$serializer = $this->getMockForAbstractClass(SerializerInterface::class);
67+
$this->isCaptchaEnabled = $this->getMockForAbstractClass(IsCaptchaEnabledInterface::class);
68+
$logger = $this->getMockForAbstractClass(LoggerInterface::class);
69+
$errorMessageConfig = $this->getMockForAbstractClass(ErrorMessageConfigInterface::class);
70+
$validationErrorMessagesProvider = $this->createMock(ValidationErrorMessagesProvider::class);
71+
$this->reCaptchaSession = $this->createMock(ReCaptchaSession::class);
72+
$this->model = new PayPalObserver(
73+
$captchaResponseResolver,
74+
$validationConfigResolver,
75+
$this->captchaValidator,
76+
$actionFlag,
77+
$serializer,
78+
$this->isCaptchaEnabled,
79+
$logger,
80+
$errorMessageConfig,
81+
$validationErrorMessagesProvider,
82+
$this->reCaptchaSession
83+
);
84+
$controller = $this->getMockBuilder(AbstractAction::class)
85+
->disableOriginalConstructor()
86+
->onlyMethods(['getRequest', 'getResponse'])
87+
->getMockForAbstractClass();
88+
$request = $this->getMockForAbstractClass(RequestInterface::class);
89+
$response = $this->getMockBuilder(ResponseInterface::class)
90+
->disableOriginalConstructor()
91+
->addMethods(['representJson'])
92+
->getMockForAbstractClass();
93+
$controller->method('getRequest')->willReturn($request);
94+
$controller->method('getResponse')->willReturn($response);
95+
$this->observer = new Observer(['controller_action' => $controller]);
96+
}
97+
98+
/**
99+
* @param array $mocks
100+
* @dataProvider executeDataProvider
101+
*/
102+
public function testExecute(array $mocks): void
103+
{
104+
$validationResult = $this->createMock(ValidationResult::class);
105+
$validationResult->expects($mocks['validationResult'][0]['expects'] ?? $this->never())
106+
->method('isValid')->willReturn($mocks['validationResult'][0]['willReturn'] ?? false);
107+
$this->captchaValidator->expects($mocks['captchaValidator'][0]['expects'] ?? $this->never())
108+
->method('isValid')
109+
->willReturn($validationResult);
110+
unset($mocks['validationResult'], $mocks['captchaValidator']);
111+
$this->configureMock($mocks);
112+
$this->model->execute($this->observer);
113+
}
114+
115+
public function executeDataProvider(): array
116+
{
117+
return [
118+
[
119+
[
120+
'isCaptchaEnabled' => [
121+
[
122+
'method' => 'isCaptchaEnabledFor',
123+
'willReturnMap' => [
124+
['paypal_payflowpro', false],
125+
['place_order', false],
126+
]
127+
]
128+
],
129+
'reCaptchaSession' => [
130+
[
131+
'method' => 'save',
132+
'expects' => $this->never(),
133+
]
134+
]
135+
]
136+
],
137+
[
138+
[
139+
'isCaptchaEnabled' => [
140+
[
141+
'method' => 'isCaptchaEnabledFor',
142+
'willReturnMap' => [
143+
['paypal_payflowpro', true],
144+
['place_order', false],
145+
]
146+
]
147+
],
148+
'reCaptchaSession' => [
149+
[
150+
'method' => 'save',
151+
'expects' => $this->never(),
152+
]
153+
],
154+
'captchaValidator' => [
155+
[
156+
'method' => 'isValid',
157+
'expects' => $this->once(),
158+
]
159+
],
160+
'validationResult' => [
161+
[
162+
'method' => 'isValid',
163+
'expects' => $this->once(),
164+
'willReturn' => true,
165+
]
166+
]
167+
]
168+
],
169+
[
170+
[
171+
'isCaptchaEnabled' => [
172+
[
173+
'method' => 'isCaptchaEnabledFor',
174+
'willReturnMap' => [
175+
['paypal_payflowpro', true],
176+
['place_order', true],
177+
]
178+
]
179+
],
180+
'reCaptchaSession' => [
181+
[
182+
'method' => 'save',
183+
'expects' => $this->once(),
184+
]
185+
],
186+
'captchaValidator' => [
187+
[
188+
'method' => 'isValid',
189+
'expects' => $this->once(),
190+
]
191+
],
192+
'validationResult' => [
193+
[
194+
'method' => 'isValid',
195+
'expects' => $this->once(),
196+
'willReturn' => true,
197+
]
198+
]
199+
]
200+
]
201+
];
202+
}
203+
204+
private function configureMock(array $mocks): void
205+
{
206+
foreach ($mocks as $prop => $propMocks) {
207+
foreach ($propMocks as $mock) {
208+
$builder = $this->$prop->expects($mock['expects'] ?? $this->any());
209+
unset($mock['expects']);
210+
foreach ($mock as $method => $args) {
211+
$builder->$method(...[$args]);
212+
}
213+
}
214+
}
215+
}
216+
}

0 commit comments

Comments
 (0)