Skip to content

Commit 0bcb3f6

Browse files
authored
Merge pull request #111 from /issues/110
Security package/issues/110 Cover ReCaptchaSendFriend module with integration tests.
2 parents 71e77b3 + 8f067e0 commit 0bcb3f6

File tree

2 files changed

+304
-0
lines changed

2 files changed

+304
-0
lines changed

ReCaptchaNewsletter/Test/Integration/NewsletterFormTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function setUp()
7070
->willReturn($this->captchaValidationResultMock);
7171
$this->_objectManager->addSharedInstance($captchaValidationResultMock, Validator::class);
7272
}
73+
7374
/**
7475
* @magentoConfigFixture default_store customer/captcha/enable 0
7576
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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\ReCaptchaSendFriend\Test\Integration;
9+
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Data\Form\FormKey;
12+
use Magento\Framework\Exception\InputException;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\Framework\Validation\ValidationResult;
15+
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
16+
use Magento\ReCaptchaValidation\Model\Validator;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\TestFramework\App\MutableScopeConfig;
19+
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
20+
use Magento\TestFramework\TestCase\AbstractController;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
23+
/**
24+
* @magentoAppArea frontend
25+
* @magentoAppIsolation enabled
26+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
27+
*/
28+
class SendFriendFormTest extends AbstractController
29+
{
30+
/**
31+
* @var MutableScopeConfig
32+
*/
33+
private $mutableScopeConfig;
34+
35+
/**
36+
* @var FormKey
37+
*/
38+
private $formKey;
39+
40+
/**
41+
* @var TransportBuilderMock
42+
*/
43+
private $transportMock;
44+
45+
/**
46+
* @var ValidationResult|MockObject
47+
*/
48+
private $captchaValidationResultMock;
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
protected function setUp()
54+
{
55+
parent::setUp();
56+
$this->mutableScopeConfig = $this->_objectManager->get(MutableScopeConfig::class);
57+
$this->formKey = $this->_objectManager->get(FormKey::class);
58+
$this->transportMock = $this->_objectManager->get(TransportBuilderMock::class);
59+
60+
$this->captchaValidationResultMock = $this->createMock(ValidationResult::class);
61+
$captchaValidationResultMock = $this->createMock(Validator::class);
62+
$captchaValidationResultMock->expects($this->any())
63+
->method('isValid')
64+
->willReturn($this->captchaValidationResultMock);
65+
$this->_objectManager->addSharedInstance($captchaValidationResultMock, Validator::class);
66+
}
67+
68+
/**
69+
* @magentoConfigFixture default_store customer/captcha/enable 0
70+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
71+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
72+
*
73+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
74+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
75+
*/
76+
public function testGetRequestIfReCaptchaIsDisabled()
77+
{
78+
$this->setConfig(false, 'test_public_key', 'test_private_key');
79+
80+
$this->checkSuccessfulGetResponse();
81+
}
82+
83+
/**
84+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
85+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
86+
* @magentoConfigFixture default_store customer/captcha/enable 0
87+
*
88+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible
89+
*
90+
* It's needed for proper work of "ifconfig" in layout during tests running
91+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/sendfriend invisible
92+
*/
93+
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
94+
{
95+
$this->setConfig(true, null, null);
96+
97+
$this->checkSuccessfulGetResponse();
98+
}
99+
100+
/**
101+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
102+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
103+
* @magentoConfigFixture default_store customer/captcha/enable 0
104+
*
105+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
106+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
107+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible
108+
*
109+
* It's needed for proper work of "ifconfig" in layout during tests running
110+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/sendfriend invisible
111+
*/
112+
public function testGetRequestIfReCaptchaIsEnabled()
113+
{
114+
$this->setConfig(true, 'test_public_key', 'test_private_key');
115+
116+
$this->checkSuccessfulGetResponse(true);
117+
}
118+
119+
/**
120+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
121+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
122+
* @magentoConfigFixture default_store customer/captcha/enable 0
123+
*
124+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
125+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
126+
*/
127+
public function testPostRequestIfReCaptchaIsDisabled()
128+
{
129+
$this->setConfig(false, 'test_public_key', 'test_private_key');
130+
131+
$this->checkPostResponse(true);
132+
}
133+
134+
/**
135+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
136+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
137+
* @magentoConfigFixture default_store customer/captcha/enable 0
138+
*
139+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible
140+
*/
141+
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
142+
{
143+
$this->setConfig(true, null, null);
144+
145+
$this->checkPostResponse(true);
146+
}
147+
148+
/**
149+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
150+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
151+
* @magentoConfigFixture default_store customer/captcha/enable 0
152+
*
153+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
154+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
155+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible
156+
*/
157+
public function testPostRequestWithSuccessfulReCaptchaValidation()
158+
{
159+
$this->setConfig(true, 'test_public_key', 'test_private_key');
160+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true);
161+
162+
$this->checkPostResponse(
163+
true,
164+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test']
165+
);
166+
}
167+
168+
/**
169+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
170+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
171+
* @magentoConfigFixture default_store customer/captcha/enable 0
172+
*
173+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
174+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
175+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible
176+
*/
177+
public function testPostRequestIfReCaptchaParameterIsMissed()
178+
{
179+
$this->setConfig(true, 'test_public_key', 'test_private_key');
180+
181+
$this->expectException(InputException::class);
182+
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');
183+
184+
$this->checkPostResponse(false);
185+
}
186+
187+
/**
188+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
189+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
190+
* @magentoConfigFixture default_store customer/captcha/enable 0
191+
*
192+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
193+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
194+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible
195+
*/
196+
public function testPostRequestWithFailedReCaptchaValidation()
197+
{
198+
$this->setConfig(true, 'test_public_key', 'test_private_key');
199+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
200+
201+
$this->checkPostResponse(
202+
false,
203+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test']
204+
);
205+
}
206+
207+
/**
208+
* @param bool $shouldContainReCaptcha
209+
*/
210+
private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
211+
{
212+
$this->dispatch('sendfriend/product/send/id/1');
213+
$content = $this->getResponse()->getBody();
214+
215+
self::assertNotEmpty($content);
216+
217+
$shouldContainReCaptcha
218+
? self::assertContains('field-recaptcha', $content)
219+
: self::assertNotContains('field-recaptcha', $content);
220+
221+
self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
222+
}
223+
224+
/**
225+
* @param bool $isSuccessfulRequest
226+
* @param array $postValues
227+
*/
228+
private function checkPostResponse(bool $isSuccessfulRequest, array $postValues = [])
229+
{
230+
$expectedUrl = 'http://localhost/index.php/simple-product.html';
231+
232+
$this->getRequest()
233+
->setParam(\Magento\Framework\App\Response\RedirectInterface::PARAM_NAME_REFERER_URL, $expectedUrl)
234+
->setMethod(Http::METHOD_POST)
235+
->setPostValue(array_replace_recursive(
236+
[
237+
'sender' => [
238+
'name' => 'Sender',
239+
'email' => '[email protected]',
240+
'message' => 'Message',
241+
],
242+
'recipients' => [
243+
'name' => [
244+
'Recipient',
245+
],
246+
'email' => [
247+
248+
]
249+
],
250+
'form_key' => $this->formKey->getFormKey(),
251+
],
252+
$postValues
253+
));
254+
255+
$this->dispatch('sendfriend/product/sendmail/id/1');
256+
257+
$this->assertRedirect(self::equalTo($expectedUrl));
258+
259+
if ($isSuccessfulRequest) {
260+
$this->assertSessionMessages(
261+
self::contains(
262+
'The link to a friend was sent.'
263+
),
264+
MessageInterface::TYPE_SUCCESS
265+
);
266+
self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
267+
268+
$message = $this->transportMock->getSentMessage();
269+
self::assertNotEmpty($message);
270+
self::assertEquals((string)__('Welcome, Recipient'), $message->getSubject());
271+
} else {
272+
$this->assertSessionMessages(
273+
$this->equalTo(['reCAPTCHA verification failed']),
274+
MessageInterface::TYPE_ERROR
275+
);
276+
self::assertEmpty($this->transportMock->getSentMessage());
277+
}
278+
}
279+
280+
/**
281+
* @param bool $isEnabled
282+
* @param string|null $public
283+
* @param string|null $private
284+
*/
285+
private function setConfig(bool $isEnabled, ?string $public, ?string $private): void
286+
{
287+
$this->mutableScopeConfig->setValue(
288+
'recaptcha_frontend/type_for/sendfriend',
289+
$isEnabled ? 'invisible' : null,
290+
ScopeInterface::SCOPE_WEBSITE
291+
);
292+
$this->mutableScopeConfig->setValue(
293+
'recaptcha_frontend/type_invisible/public_key',
294+
$public,
295+
ScopeInterface::SCOPE_WEBSITE
296+
);
297+
$this->mutableScopeConfig->setValue(
298+
'recaptcha_frontend/type_invisible/private_key',
299+
$private,
300+
ScopeInterface::SCOPE_WEBSITE
301+
);
302+
}
303+
}

0 commit comments

Comments
 (0)