Skip to content

Commit c088db0

Browse files
authored
Merge pull request #107 from /issues/105
security-package/issues/105: Cover ReCaptchaContact module with integration tests.
2 parents 2693c09 + 643e874 commit c088db0

File tree

4 files changed

+325
-57
lines changed

4 files changed

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

ReCaptchaFrontendUi/view/frontend/requirejs-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
'use strict';
77

8-
// eslint-disable-next-line no-unused-vars
98
var config = {
109
config: {
1110
mixins: {

ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\ReCaptchaUser\Test\Integration;
99

1010
use Magento\Backend\Model\UrlInterface;
11+
use Magento\Framework\App\Request\Http;
1112
use Magento\Framework\Data\Form\FormKey;
1213
use Magento\Framework\Message\MessageInterface;
1314
use Magento\Framework\Validation\ValidationResult;
@@ -75,6 +76,8 @@ public function testGetRequestIfReCaptchaIsDisabled()
7576
/**
7677
* @magentoAdminConfigFixture admin/captcha/enable 0
7778
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
79+
*
80+
* It's needed for proper work of "ifconfig" in layout during tests running
7881
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
7982
*/
8083
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
@@ -87,6 +90,8 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured()
8790
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
8891
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
8992
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
93+
*
94+
* It's needed for proper work of "ifconfig" in layout during tests running
9095
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
9196
*/
9297
public function testGetRequestIfReCaptchaIsEnabled()
@@ -108,7 +113,6 @@ public function testPostRequestIfReCaptchaIsDisabled()
108113
* @magentoAdminConfigFixture admin/captcha/enable 0
109114
* @magentoAdminConfigFixture admin/captcha/always_for/backend_forgotpassword 0
110115
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
111-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
112116
*/
113117
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
114118
{
@@ -120,7 +124,6 @@ public function testPostRequestIfReCaptchaKeysAreNotConfigured()
120124
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
121125
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
122126
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
123-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
124127
*/
125128
public function testPostRequestWithSuccessfulReCaptchaValidation()
126129
{
@@ -138,18 +141,19 @@ public function testPostRequestWithSuccessfulReCaptchaValidation()
138141
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
139142
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
140143
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
141-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
142144
* @expectedException \Magento\Framework\Exception\InputException
143145
* @expectedExceptionMessage Can not resolve reCAPTCHA parameter.
144146
*/
145147
public function testPostRequestIfReCaptchaParameterIsMissed()
146148
{
147-
$this->getRequest()->setPostValue(
148-
[
149-
'form_key' => $this->formKey->getFormKey(),
150-
'email' => '[email protected]'
151-
]
152-
);
149+
$this->getRequest()
150+
->setMethod(Http::METHOD_POST)
151+
->setPostValue(
152+
[
153+
'form_key' => $this->formKey->getFormKey(),
154+
'email' => '[email protected]'
155+
]
156+
);
153157
$this->dispatch('backend/admin/auth/forgotpassword');
154158

155159
self::assertEmpty($this->transportMock->getSentMessage());
@@ -160,19 +164,20 @@ public function testPostRequestIfReCaptchaParameterIsMissed()
160164
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
161165
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
162166
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
163-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
164167
*/
165168
public function testPostRequestWithFailedReCaptchaValidation()
166169
{
167170
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
168171

169-
$this->getRequest()->setPostValue(
170-
[
171-
'form_key' => $this->formKey->getFormKey(),
172-
'email' => '[email protected]',
173-
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
174-
]
175-
);
172+
$this->getRequest()
173+
->setMethod(Http::METHOD_POST)
174+
->setPostValue(
175+
[
176+
'form_key' => $this->formKey->getFormKey(),
177+
'email' => '[email protected]',
178+
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
179+
]
180+
);
176181
$this->dispatch('backend/admin/auth/forgotpassword');
177182

178183
$this->assertSessionMessages(
@@ -204,13 +209,15 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
204209
*/
205210
private function checkSuccessfulPostResponse(array $postValues = [])
206211
{
207-
$this->getRequest()->setPostValue(array_replace_recursive(
208-
[
209-
'form_key' => $this->formKey->getFormKey(),
210-
'email' => '[email protected]',
211-
],
212-
$postValues
213-
));
212+
$this->getRequest()
213+
->setMethod(Http::METHOD_POST)
214+
->setPostValue(array_replace_recursive(
215+
[
216+
'form_key' => $this->formKey->getFormKey(),
217+
'email' => '[email protected]',
218+
],
219+
$postValues
220+
));
214221
$this->dispatch('backend/admin/auth/forgotpassword');
215222

216223
$this->assertRedirect(self::equalTo($this->backendUrl->getRouteUrl('adminhtml')));

0 commit comments

Comments
 (0)