Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 255 additions & 0 deletions ReCaptchaContact/Test/Integration/ContactFormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ReCaptchaContact\Test\Integration;

use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Validation\ValidationResult;
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
use Magento\ReCaptchaValidation\Model\Validator;
use Magento\Store\Model\ScopeInterface;
use Magento\TestFramework\App\MutableScopeConfig;
use Magento\TestFramework\TestCase\AbstractController;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @magentoAppArea frontend
* @magentoAppIsolation enabled
*/
class ContactFormTest extends AbstractController
{
/**
* @var FormKey
*/
private $formKey;

/**
* @var MutableScopeConfig
*/
private $mutableScopeConfig;

/**
* @var ValidationResult|MockObject
*/
private $captchaValidationResultMock;

/**
* @inheritDoc
*/
protected function setUp()
{
parent::setUp();
$this->formKey = $this->_objectManager->get(FormKey::class);
$this->mutableScopeConfig = $this->_objectManager->get(MutableScopeConfig::class);

$this->captchaValidationResultMock = $this->createMock(ValidationResult::class);
$captchaValidatorMock = $this->createMock(Validator::class);
$captchaValidatorMock->expects($this->any())
->method('isValid')
->willReturn($this->captchaValidationResultMock);
$this->_objectManager->addSharedInstance($captchaValidatorMock, Validator::class);
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
*/
public function testGetRequestIfReCaptchaIsDisabled()
{
$this->initConfig(0, 'test_public_key', 'test_private_key');

$this->checkSuccessfulGetResponse();
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
*
* It's needed for proper work of "ifconfig" in layout during tests running
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
*/
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
{
$this->initConfig(1, null, null);

$this->checkSuccessfulGetResponse();
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
*
* It's needed for proper work of "ifconfig" in layout during tests running
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
*/
public function testGetRequestIfReCaptchaIsEnabled()
{
$this->initConfig(1, 'test_public_key', 'test_private_key');

$this->checkSuccessfulGetResponse(true);
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
*/
public function testPostRequestIfReCaptchaIsDisabled()
{
$this->initConfig(0, 'test_public_key', 'test_private_key');

$this->checkPostResponse(true);
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
*/
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
{
$this->initConfig(1, null, null);

$this->checkPostResponse(true);
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
*/
public function testPostRequestWithSuccessfulReCaptchaValidation()
{
$this->initConfig(1, 'test_public_key', 'test_private_key');
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true);

$this->checkPostResponse(
true,
[
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
]
);
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
*/
public function testPostRequestIfReCaptchaParameterIsMissed()
{
$this->initConfig(1, 'test_public_key', 'test_private_key');

$this->expectException(InputException::class);
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');

$this->checkPostResponse(false);
}

/**
* @magentoConfigFixture base_website customer/captcha/enable 0
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
*/
public function testPostRequestWithFailedReCaptchaValidation()
{
$this->initConfig(1, 'test_public_key', 'test_private_key');
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);

$this->checkPostResponse(
false,
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test']
);
}

/**
* @param bool $shouldContainReCaptcha
*/
private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
{
$this->dispatch('contact/index');
$content = $this->getResponse()->getBody();

self::assertNotEmpty($content);

$shouldContainReCaptcha
? $this->assertContains('recaptcha', $content)
: $this->assertNotContains('recaptcha', $content);

self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
}

/**
* @param bool $isSuccessfulRequest
* @param array $postValues
*/
private function checkPostResponse(bool $isSuccessfulRequest, array $postValues = [])
{
$this->getRequest()
->setMethod(HttpRequest::METHOD_POST)
->setPostValue(array_replace_recursive(
[
'form_key' => $this->formKey->getFormKey(),
'name' => 'customer name',
'comment' => 'comment',
'email' => 'user@example.com',
],
$postValues
));

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

$this->assertRedirect($this->stringContains('contact/index'));

if ($isSuccessfulRequest) {
$this->assertSessionMessages(
$this->contains(
"Thanks for contacting us with your comments and questions. We&#039;ll respond to you very soon."
),
MessageInterface::TYPE_SUCCESS
);
$this->assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR));
} else {
$this->assertSessionMessages(
$this->equalTo(['reCAPTCHA verification failed']),
MessageInterface::TYPE_ERROR
);
}
}

/**
* @param int|null $enabled
* @param string|null $public
* @param string|null $private
*/
private function initConfig(?int $enabled, ?string $public, ?string $private): void
{
$this->mutableScopeConfig->setValue(
'recaptcha_frontend/type_for/contact',
$enabled ? 'invisible' : null,
ScopeInterface::SCOPE_WEBSITE
);
$this->mutableScopeConfig->setValue(
'recaptcha_frontend/type_invisible/public_key',
$public,
ScopeInterface::SCOPE_WEBSITE
);
$this->mutableScopeConfig->setValue(
'recaptcha_frontend/type_invisible/private_key',
$private,
ScopeInterface::SCOPE_WEBSITE
);
}
}
1 change: 0 additions & 1 deletion ReCaptchaFrontendUi/view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

'use strict';

// eslint-disable-next-line no-unused-vars
var config = {
config: {
mixins: {
Expand Down
55 changes: 31 additions & 24 deletions ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\ReCaptchaUser\Test\Integration;

use Magento\Backend\Model\UrlInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Validation\ValidationResult;
Expand Down Expand Up @@ -75,6 +76,8 @@ public function testGetRequestIfReCaptchaIsDisabled()
/**
* @magentoAdminConfigFixture admin/captcha/enable 0
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
*
* It's needed for proper work of "ifconfig" in layout during tests running
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
*/
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
Expand All @@ -87,6 +90,8 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured()
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
*
* It's needed for proper work of "ifconfig" in layout during tests running
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
*/
public function testGetRequestIfReCaptchaIsEnabled()
Expand All @@ -108,7 +113,6 @@ public function testPostRequestIfReCaptchaIsDisabled()
* @magentoAdminConfigFixture admin/captcha/enable 0
* @magentoAdminConfigFixture admin/captcha/always_for/backend_forgotpassword 0
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
*/
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
{
Expand All @@ -120,7 +124,6 @@ public function testPostRequestIfReCaptchaKeysAreNotConfigured()
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
*/
public function testPostRequestWithSuccessfulReCaptchaValidation()
{
Expand All @@ -138,18 +141,19 @@ public function testPostRequestWithSuccessfulReCaptchaValidation()
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
* @expectedException \Magento\Framework\Exception\InputException
* @expectedExceptionMessage Can not resolve reCAPTCHA parameter.
*/
public function testPostRequestIfReCaptchaParameterIsMissed()
{
$this->getRequest()->setPostValue(
[
'form_key' => $this->formKey->getFormKey(),
'email' => 'adminUser@example.com'
]
);
$this->getRequest()
->setMethod(Http::METHOD_POST)
->setPostValue(
[
'form_key' => $this->formKey->getFormKey(),
'email' => 'adminUser@example.com'
]
);
$this->dispatch('backend/admin/auth/forgotpassword');

self::assertEmpty($this->transportMock->getSentMessage());
Expand All @@ -160,19 +164,20 @@ public function testPostRequestIfReCaptchaParameterIsMissed()
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
*/
public function testPostRequestWithFailedReCaptchaValidation()
{
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);

$this->getRequest()->setPostValue(
[
'form_key' => $this->formKey->getFormKey(),
'email' => 'adminUser@example.com',
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
]
);
$this->getRequest()
->setMethod(Http::METHOD_POST)
->setPostValue(
[
'form_key' => $this->formKey->getFormKey(),
'email' => 'adminUser@example.com',
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
]
);
$this->dispatch('backend/admin/auth/forgotpassword');

$this->assertSessionMessages(
Expand Down Expand Up @@ -204,13 +209,15 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
*/
private function checkSuccessfulPostResponse(array $postValues = [])
{
$this->getRequest()->setPostValue(array_replace_recursive(
[
'form_key' => $this->formKey->getFormKey(),
'email' => 'adminUser@example.com',
],
$postValues
));
$this->getRequest()
->setMethod(Http::METHOD_POST)
->setPostValue(array_replace_recursive(
[
'form_key' => $this->formKey->getFormKey(),
'email' => 'adminUser@example.com',
],
$postValues
));
$this->dispatch('backend/admin/auth/forgotpassword');

$this->assertRedirect(self::equalTo($this->backendUrl->getRouteUrl('adminhtml')));
Expand Down
Loading