Skip to content

Commit a03bd50

Browse files
security-package/issues/105: Cover ReCaptchaContact module with integration tests - updated.
1 parent 2cf2592 commit a03bd50

File tree

1 file changed

+82
-152
lines changed

1 file changed

+82
-152
lines changed

ReCaptchaContact/Test/Integration/ContactFormTest.php

Lines changed: 82 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,41 @@
77

88
namespace Magento\ReCaptchaContact\Test\Integration;
99

10-
use Magento\Framework\Exception\InputException;
11-
use Magento\TestFramework\App\ReinitableConfig;
1210
use Magento\Framework\App\Request\Http as HttpRequest;
1311
use Magento\Framework\Data\Form\FormKey;
12+
use Magento\Framework\Exception\InputException;
1413
use Magento\Framework\Message\MessageInterface;
15-
use Magento\Store\Model\ScopeInterface;
16-
use Magento\ReCaptcha\Model\CaptchaValidator;
17-
use Magento\ReCaptchaApi\Api\CaptchaValidatorInterface;
14+
use Magento\Framework\Validation\ValidationResult;
1815
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
16+
use Magento\ReCaptchaValidation\Model\Validator;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\TestFramework\App\MutableScopeConfig;
1919
use Magento\TestFramework\TestCase\AbstractController;
2020
use PHPUnit\Framework\MockObject\MockObject;
2121

2222
/**
2323
* Test for \Magento\ReCaptchaContact\Observer\ContactFormObserver class.
24+
*
25+
* @magentoAppArea frontend
26+
* @magentoDbIsolation enabled
27+
* @magentoAppIsolation enabled
2428
*/
25-
class ContactFormObserverTest extends AbstractController
29+
class ContactFormTest extends AbstractController
2630
{
2731
/**
2832
* @var FormKey
2933
*/
3034
private $formKey;
3135

3236
/**
33-
* @var CaptchaValidatorInterface|MockObject
37+
* @var MutableScopeConfig
3438
*/
35-
private $captchaValidatorMock;
39+
private $mutableScopeConfig;
3640

3741
/**
38-
* @var ReinitableConfig
42+
* @var ValidationResult|MockObject
3943
*/
40-
private $settingsConfiguration;
44+
private $captchaValidationResultMock;
4145

4246
/**
4347
* @inheritDoc
@@ -46,162 +50,88 @@ protected function setUp()
4650
{
4751
parent::setUp();
4852
$this->formKey = $this->_objectManager->get(FormKey::class);
49-
$this->settingsConfiguration = $this->_objectManager->get(ReinitableConfig::class);
50-
51-
$this->captchaValidatorMock = $this->createMock(CaptchaValidatorInterface::class);
52-
$this->_objectManager->addSharedInstance($this->captchaValidatorMock, CaptchaValidator::class);
53+
$this->mutableScopeConfig = $this->_objectManager->get(MutableScopeConfig::class);
54+
55+
$this->captchaValidationResultMock = $this->createMock(ValidationResult::class);
56+
$captchaValidatorMock = $this->createMock(Validator::class);
57+
$captchaValidatorMock->expects($this->any())
58+
->method('isValid')
59+
->willReturn($this->captchaValidationResultMock);
60+
$this->_objectManager->addSharedInstance($captchaValidatorMock, Validator::class);
5361
}
5462

55-
/**
56-
* @magentoDbIsolation enabled
57-
* @magentoAppIsolation enabled
58-
*/
5963
public function testGetRequestIfReCaptchaIsDisabled()
6064
{
61-
$this->settingsRecaptcha(false, false);
65+
$this->initConfig(0, 'test_public_key', 'test_private_key');
66+
6267
$this->checkSuccessfulGetResponse();
6368
}
6469

6570
/**
66-
* @magentoDbIsolation enabled
67-
* @magentoAppIsolation enabled
71+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
6872
*/
6973
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
7074
{
71-
$this->settingsRecaptcha(true, false);
75+
$this->initConfig(1, null, null);
76+
7277
$this->checkSuccessfulGetResponse();
7378
}
7479

7580
/**
76-
* @magentoDbIsolation enabled
77-
* @magentoAppIsolation enabled
81+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
7882
*/
7983
public function testGetRequestIfReCaptchaIsEnabled()
8084
{
81-
$this->settingsRecaptcha(true, true);
85+
$this->initConfig(1, 'test_public_key', 'test_private_key');
86+
8287
$this->checkSuccessfulGetResponse(true);
8388
}
8489

85-
/**
86-
* @magentoDbIsolation enabled
87-
* @magentoAppIsolation enabled
88-
*/
89-
public function testPostRequestIfReCaptchaIsDisabled()
90+
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
9091
{
91-
$this->settingsRecaptcha(false, false);
92-
$this->checkSuccessfulPostResponse();
93-
94-
$this->assertSessionMessages(
95-
$this->contains(
96-
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
97-
),
98-
MessageInterface::TYPE_SUCCESS
99-
);
92+
$this->initConfig(1, null, null);
93+
94+
$this->checkSuccessfulPostResponse(true);
10095
}
10196

102-
/**
103-
* @magentoDbIsolation enabled
104-
* @magentoAppIsolation enabled
105-
*/
106-
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
97+
public function testPostRequestIfReCaptchaIsDisabled()
10798
{
108-
$this->settingsRecaptcha(true, false);
109-
$this->checkSuccessfulPostResponse();
110-
111-
$this->assertSessionMessages(
112-
$this->contains(
113-
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
114-
),
115-
MessageInterface::TYPE_SUCCESS
116-
);
99+
$this->initConfig(0, 'test_public_key', 'test_private_key');
100+
101+
$this->checkSuccessfulPostResponse(true);
117102
}
118103

119-
/**
120-
* @magentoDbIsolation enabled
121-
* @magentoAppIsolation enabled
122-
*/
123104
public function testPostRequestWithSuccessfulReCaptchaValidation()
124105
{
125-
$this->settingsRecaptcha(true, true);
126-
127-
$this->captchaValidatorMock->expects($this->once())->method('isValid')->willReturn(true);
106+
$this->initConfig(1, 'test_public_key', 'test_private_key');
107+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true);
128108

129109
$this->checkSuccessfulPostResponse(
130-
[
131-
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
132-
]
133-
);
134-
135-
$this->assertSessionMessages(
136-
$this->contains(
137-
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
138-
),
139-
MessageInterface::TYPE_SUCCESS
110+
true,
111+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test_response']
140112
);
141113
}
142114

143-
/**
144-
* @throws \Magento\Framework\Exception\LocalizedException
145-
*
146-
* @magentoDbIsolation enabled
147-
* @magentoAppIsolation enabled
148-
*/
149-
public function testPostRequestIfReCaptchaParameterIsMissed()
115+
public function testPostRequestWithFailedReCaptchaValidation()
150116
{
151-
$this->settingsRecaptcha(true, true);
152-
153-
$this->expectException(InputException::class);
154-
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');
117+
$this->initConfig(1, 'test_public_key', 'test_private_key');
118+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
155119

156-
$this->getRequest()->setPostValue(
157-
[
158-
'form_key' => $this->formKey->getFormKey(),
159-
'name' => 'customer name',
160-
'comment' => 'comment',
161-
'email' => '[email protected]',
162-
]
163-
)->setMethod(HttpRequest::METHOD_POST);
164-
165-
$this->dispatch('contact/index/post');
166-
167-
$this->assertRedirect($this->stringContains('contact/index'));
168-
169-
$this->assertSessionMessages(
170-
self::equalTo(['Can not resolve reCAPTCHA parameter.']),
171-
MessageInterface::TYPE_ERROR
120+
$this->checkSuccessfulPostResponse(
121+
false,
122+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test_response']
172123
);
173124
}
174125

175-
/**
176-
* @magentoDbIsolation enabled
177-
* @magentoAppIsolation enabled
178-
*/
179-
public function testPostRequestWithFailedReCaptchaValidation()
126+
public function testPostRequestIfReCaptchaParameterIsMissed()
180127
{
181-
$this->settingsRecaptcha(true, true);
182-
183-
$this->captchaValidatorMock->expects($this->once())->method('isValid')->willReturn(false);
184-
185-
$this->getRequest()->setPostValue(
186-
[
187-
'form_key' => $this->formKey->getFormKey(),
188-
'name' => 'customer name',
189-
'comment' => 'comment',
190-
'email' => '[email protected]',
191-
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
192-
]
193-
194-
)->setMethod(HttpRequest::METHOD_POST);
195-
196-
$this->dispatch('contact/index/post');
197-
198-
$this->assertRedirect($this->stringContains('contact/index'));
128+
$this->initConfig(1, 'test_public_key', 'test_private_key');
129+
$this->captchaValidationResultMock->expects($this->never())->method('isValid');
130+
$this->expectException(InputException::class);
131+
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');
199132

200-
$this->assertSessionMessages(
201-
$this->equalTo(
202-
['You cannot proceed with such operation, your reCAPTCHA reputation is too low.']
203-
),
204-
MessageInterface::TYPE_ERROR
133+
$this->checkSuccessfulPostResponse(
134+
false
205135
);
206136
}
207137

@@ -223,10 +153,12 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
223153
}
224154

225155
/**
156+
* @param bool $result
226157
* @param array $postValues
227-
* @throws \Magento\Framework\Exception\LocalizedException
158+
* @throws LocalizedException
159+
* @throws \Magento\Framework\Exception\NoSuchEntityException
228160
*/
229-
private function checkSuccessfulPostResponse(array $postValues = [])
161+
private function checkSuccessfulPostResponse(bool $result, array $postValues = [])
230162
{
231163
$this->getRequest()->setPostValue(array_replace_recursive(
232164
[
@@ -241,35 +173,33 @@ private function checkSuccessfulPostResponse(array $postValues = [])
241173
$this->dispatch('contact/index/post');
242174

243175
$this->assertRedirect($this->stringContains('contact/index'));
244-
}
245176

246-
/**
247-
* @param bool $captchaIsEnabledForContact
248-
* @param bool $captchaIsConfigured
249-
*/
250-
private function settingsRecaptcha($captchaIsEnabledForContact = false, $captchaIsConfigured = false)
251-
{
252-
if ($captchaIsEnabledForContact) {
253-
$this->settingsConfiguration->setValue(
254-
'recaptcha/frontend/enabled_for_contact',
255-
(int)$captchaIsEnabledForContact,
256-
ScopeInterface::SCOPE_WEBSITES
257-
);
258-
}
259-
260-
261-
if ($captchaIsConfigured) {
262-
$this->settingsConfiguration->setValue(
263-
'recaptcha/frontend/public_key',
264-
'test_public_key',
265-
ScopeInterface::SCOPE_WEBSITES
177+
if ($result) {
178+
$this->assertSessionMessages(
179+
$this->contains(
180+
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
181+
),
182+
MessageInterface::TYPE_SUCCESS
266183
);
267-
$this->settingsConfiguration->setValue(
268-
'recaptcha/frontend/private_key',
269-
'test_private_key',
270-
ScopeInterface::SCOPE_WEBSITES
184+
$this->assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
185+
} else {
186+
$this->assertSessionMessages(
187+
$this->equalTo(['reCAPTCHA verification failed']),
188+
MessageInterface::TYPE_ERROR
271189
);
272190
}
273191
}
274192

193+
/**
194+
* @param int|null $enabled
195+
* @param string|null $public
196+
* @param string|null $private
197+
*/
198+
private function initConfig(?int $enabled, ?string $public, ?string $private): void
199+
{
200+
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_for/newsletter', null, ScopeInterface::SCOPE_WEBSITE);
201+
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_for/contact', $enabled ? 'invisible' : null, ScopeInterface::SCOPE_WEBSITE);
202+
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_invisible/public_key', $public, ScopeInterface::SCOPE_WEBSITE);
203+
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_invisible/private_key', $private, ScopeInterface::SCOPE_WEBSITE);
204+
}
275205
}

0 commit comments

Comments
 (0)