Skip to content

Commit 775c08c

Browse files
authored
Merge pull request #109 from /issues/108
security-package/issues/108: Cover ReCaptchaNewsletter module with integration tests.
2 parents c84d8db + 822bf8e commit 775c08c

File tree

2 files changed

+276
-5
lines changed

2 files changed

+276
-5
lines changed

ReCaptchaContact/Test/Integration/ContactFormTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
183183
self::assertNotEmpty($content);
184184

185185
$shouldContainReCaptcha
186-
? $this->assertContains('field-recaptcha', $content)
187-
: $this->assertNotContains('field-recaptcha', $content);
186+
? self::assertContains('field-recaptcha', $content)
187+
: self::assertNotContains('field-recaptcha', $content);
188188

189189
self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
190190
}
@@ -209,16 +209,16 @@ private function checkPostResponse(bool $isSuccessfulRequest, array $postValues
209209

210210
$this->dispatch('contact/index/post');
211211

212-
$this->assertRedirect($this->stringContains('contact/index'));
212+
$this->assertRedirect(self::stringContains('contact/index'));
213213

214214
if ($isSuccessfulRequest) {
215215
$this->assertSessionMessages(
216-
$this->contains(
216+
self::contains(
217217
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
218218
),
219219
MessageInterface::TYPE_SUCCESS
220220
);
221-
$this->assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
221+
self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
222222
} else {
223223
$this->assertSessionMessages(
224224
$this->equalTo(['reCAPTCHA verification failed']),
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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\ReCaptchaNewsletter\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\UrlInterface;
15+
use Magento\Framework\Validation\ValidationResult;
16+
use Magento\Newsletter\Model\SubscriberFactory;
17+
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
18+
use Magento\ReCaptchaValidation\Model\Validator;
19+
use Magento\Store\Model\ScopeInterface;
20+
use Magento\TestFramework\App\MutableScopeConfig;
21+
use Magento\TestFramework\TestCase\AbstractController;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
24+
/**
25+
* @magentoAppArea frontend
26+
* @magentoAppIsolation enabled
27+
*/
28+
class NewsletterFormTest extends AbstractController
29+
{
30+
/**
31+
* @var MutableScopeConfig
32+
*/
33+
private $mutableScopeConfig;
34+
35+
/**
36+
* @var FormKey
37+
*/
38+
private $formKey;
39+
40+
/**
41+
* @var UrlInterface
42+
*/
43+
private $url;
44+
45+
/**
46+
* @var SubscriberFactory
47+
*/
48+
private $subscriberFactory;
49+
50+
/**
51+
* @var ValidationResult|MockObject
52+
*/
53+
private $captchaValidationResultMock;
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
protected function setUp()
59+
{
60+
parent::setUp();
61+
$this->mutableScopeConfig = $this->_objectManager->get(MutableScopeConfig::class);
62+
$this->formKey = $this->_objectManager->get(FormKey::class);
63+
$this->url = $this->_objectManager->get(UrlInterface::class);
64+
$this->subscriberFactory = $this->_objectManager->get(SubscriberFactory::class);
65+
66+
$this->captchaValidationResultMock = $this->createMock(ValidationResult::class);
67+
$captchaValidationResultMock = $this->createMock(Validator::class);
68+
$captchaValidationResultMock->expects($this->any())
69+
->method('isValid')
70+
->willReturn($this->captchaValidationResultMock);
71+
$this->_objectManager->addSharedInstance($captchaValidationResultMock, Validator::class);
72+
}
73+
/**
74+
* @magentoConfigFixture default_store customer/captcha/enable 0
75+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
76+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
77+
*/
78+
public function testGetRequestIfReCaptchaIsDisabled()
79+
{
80+
$this->setConfig(false, 'test_public_key', 'test_private_key');
81+
82+
$this->checkSuccessfulGetResponse();
83+
}
84+
85+
/**
86+
* @magentoConfigFixture default_store customer/captcha/enable 0
87+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible
88+
*
89+
* It's needed for proper work of "ifconfig" in layout during tests running
90+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/newsletter invisible
91+
*/
92+
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
93+
{
94+
$this->setConfig(true, null, null);
95+
96+
$this->checkSuccessfulGetResponse();
97+
}
98+
99+
/**
100+
* @magentoConfigFixture default_store customer/captcha/enable 0
101+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
102+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
103+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible
104+
*
105+
* It's needed for proper work of "ifconfig" in layout during tests running
106+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/newsletter invisible
107+
*/
108+
public function testGetRequestIfReCaptchaIsEnabled()
109+
{
110+
$this->setConfig(true, 'test_public_key', 'test_private_key');
111+
112+
$this->checkSuccessfulGetResponse(true);
113+
}
114+
115+
/**
116+
* @magentoConfigFixture default_store customer/captcha/enable 0
117+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
118+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
119+
*/
120+
public function testPostRequestIfReCaptchaIsDisabled()
121+
{
122+
$this->setConfig(false, 'test_public_key', 'test_private_key');
123+
124+
$this->checkPostResponse(true);
125+
}
126+
127+
/**
128+
* @magentoConfigFixture default_store customer/captcha/enable 0
129+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible
130+
*/
131+
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
132+
{
133+
$this->setConfig(true, null, null);
134+
135+
$this->checkPostResponse(true);
136+
}
137+
138+
/**
139+
* @magentoConfigFixture default_store customer/captcha/enable 0
140+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
141+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
142+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible
143+
*/
144+
public function testPostRequestWithSuccessfulReCaptchaValidation()
145+
{
146+
$this->setConfig(true, 'test_public_key', 'test_private_key');
147+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true);
148+
149+
$this->checkPostResponse(
150+
true,
151+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test']
152+
);
153+
}
154+
155+
/**
156+
* @magentoConfigFixture default_store customer/captcha/enable 0
157+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
158+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
159+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible
160+
*/
161+
public function testPostRequestIfReCaptchaParameterIsMissed()
162+
{
163+
$this->setConfig(true, 'test_public_key', 'test_private_key');
164+
165+
$this->expectException(InputException::class);
166+
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');
167+
168+
$this->checkPostResponse(false);
169+
}
170+
171+
/**
172+
* @magentoConfigFixture default_store customer/captcha/enable 0
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/newsletter invisible
176+
*/
177+
public function testPostRequestWithFailedReCaptchaValidation()
178+
{
179+
$this->setConfig(true, 'test_public_key', 'test_private_key');
180+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
181+
182+
$this->checkPostResponse(
183+
false,
184+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test']
185+
);
186+
}
187+
188+
/**
189+
* @param bool $shouldContainReCaptcha
190+
*/
191+
private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
192+
{
193+
$this->dispatch($this->url->getRouteUrl());
194+
$content = $this->getResponse()->getBody();
195+
196+
self::assertNotEmpty($content);
197+
198+
$shouldContainReCaptcha
199+
? self::assertContains('field-recaptcha', $content)
200+
: self::assertNotContains('field-recaptcha', $content);
201+
202+
self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
203+
}
204+
205+
/**
206+
* @param bool $isSuccessfulRequest
207+
* @param array $postValues
208+
*/
209+
private function checkPostResponse(bool $isSuccessfulRequest, array $postValues = [])
210+
{
211+
$this->getRequest()
212+
->setMethod(Http::METHOD_POST)
213+
->setPostValue(array_replace_recursive(
214+
[
215+
'form_key' => $this->formKey->getFormKey(),
216+
'email' => '[email protected]',
217+
],
218+
$postValues
219+
));
220+
221+
$this->dispatch('newsletter/subscriber/new');
222+
223+
$this->assertRedirect(self::equalTo($this->url->getRouteUrl()));
224+
225+
if ($isSuccessfulRequest) {
226+
$this->assertSessionMessages(
227+
self::contains(
228+
'Thank you for your subscription.'
229+
),
230+
MessageInterface::TYPE_SUCCESS
231+
);
232+
self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
233+
} else {
234+
$this->assertSessionMessages(
235+
$this->equalTo(['reCAPTCHA verification failed']),
236+
MessageInterface::TYPE_ERROR
237+
);
238+
}
239+
}
240+
241+
/**
242+
* @param bool $isEnabled
243+
* @param string|null $public
244+
* @param string|null $private
245+
*/
246+
private function setConfig(bool $isEnabled, ?string $public, ?string $private): void
247+
{
248+
$this->mutableScopeConfig->setValue(
249+
'recaptcha_frontend/type_for/newsletter',
250+
$isEnabled ? 'invisible' : null,
251+
ScopeInterface::SCOPE_WEBSITE
252+
);
253+
$this->mutableScopeConfig->setValue(
254+
'recaptcha_frontend/type_invisible/public_key',
255+
$public,
256+
ScopeInterface::SCOPE_WEBSITE
257+
);
258+
$this->mutableScopeConfig->setValue(
259+
'recaptcha_frontend/type_invisible/private_key',
260+
$private,
261+
ScopeInterface::SCOPE_WEBSITE
262+
);
263+
}
264+
265+
protected function tearDown(): void
266+
{
267+
parent::tearDown();
268+
269+
$this->subscriberFactory->create()->loadBySubscriberEmail('[email protected]', 1)->delete();
270+
}
271+
}

0 commit comments

Comments
 (0)