Skip to content

Commit b209b88

Browse files
authored
Merge pull request #114 from /issues/113
Cover ReCaptchaReview module with integration tests.
2 parents 0bcb3f6 + e770858 commit b209b88

File tree

3 files changed

+279
-3
lines changed

3 files changed

+279
-3
lines changed

ReCaptchaNewsletter/Test/Integration/NewsletterFormTest.php

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

ReCaptchaSendFriend/Test/Integration/SendFriendFormTest.php

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

1010
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\App\Response\RedirectInterface;
1112
use Magento\Framework\Data\Form\FormKey;
1213
use Magento\Framework\Exception\InputException;
1314
use Magento\Framework\Message\MessageInterface;
@@ -230,7 +231,7 @@ private function checkPostResponse(bool $isSuccessfulRequest, array $postValues
230231
$expectedUrl = 'http://localhost/index.php/simple-product.html';
231232

232233
$this->getRequest()
233-
->setParam(\Magento\Framework\App\Response\RedirectInterface::PARAM_NAME_REFERER_URL, $expectedUrl)
234+
->setParam(RedirectInterface::PARAM_NAME_REFERER_URL, $expectedUrl)
234235
->setMethod(Http::METHOD_POST)
235236
->setPostValue(array_replace_recursive(
236237
[
@@ -270,7 +271,7 @@ private function checkPostResponse(bool $isSuccessfulRequest, array $postValues
270271
self::assertEquals((string)__('Welcome, Recipient'), $message->getSubject());
271272
} else {
272273
$this->assertSessionMessages(
273-
$this->equalTo(['reCAPTCHA verification failed']),
274+
self::equalTo(['reCAPTCHA verification failed']),
274275
MessageInterface::TYPE_ERROR
275276
);
276277
self::assertEmpty($this->transportMock->getSentMessage());

0 commit comments

Comments
 (0)