diff --git a/ReCaptchaContact/Test/Integration/ContactFormTest.php b/ReCaptchaContact/Test/Integration/ContactFormTest.php new file mode 100644 index 00000000..1666c0b7 --- /dev/null +++ b/ReCaptchaContact/Test/Integration/ContactFormTest.php @@ -0,0 +1,255 @@ +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'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 + ); + } +} diff --git a/ReCaptchaFrontendUi/view/frontend/requirejs-config.js b/ReCaptchaFrontendUi/view/frontend/requirejs-config.js index 217a1ebe..f115340b 100644 --- a/ReCaptchaFrontendUi/view/frontend/requirejs-config.js +++ b/ReCaptchaFrontendUi/view/frontend/requirejs-config.js @@ -5,7 +5,6 @@ 'use strict'; -// eslint-disable-next-line no-unused-vars var config = { config: { mixins: { diff --git a/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php b/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php index 6e38e76a..10048ab9 100644 --- a/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php +++ b/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php @@ -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; @@ -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() @@ -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() @@ -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() { @@ -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() { @@ -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()); @@ -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( @@ -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'))); diff --git a/ReCaptchaUser/Test/Integration/LoginFormTest.php b/ReCaptchaUser/Test/Integration/LoginFormTest.php index 795bbf26..4d9d8527 100644 --- a/ReCaptchaUser/Test/Integration/LoginFormTest.php +++ b/ReCaptchaUser/Test/Integration/LoginFormTest.php @@ -9,6 +9,7 @@ use Magento\Backend\Model\Auth; 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; @@ -80,6 +81,8 @@ public function testGetRequestIfReCaptchaIsDisabled() * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * + * It's needed for proper work of "ifconfig" in layout during tests running * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testGetRequestIfReCaptchaKeysAreNotConfigured() @@ -93,6 +96,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_login invisible + * + * It's needed for proper work of "ifconfig" in layout during tests running * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testGetRequestIfReCaptchaIsEnabled() @@ -115,7 +120,6 @@ public function testPostRequestIfReCaptchaIsDisabled() * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible - * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestIfReCaptchaKeysAreNotConfigured() { @@ -128,7 +132,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_login invisible - * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestWithSuccessfulReCaptchaValidation() { @@ -147,19 +150,20 @@ 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_login invisible - * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestIfReCaptchaParameterIsMissed() { - $this->getRequest()->setPostValue( - [ - 'form_key' => $this->formKey->getFormKey(), - 'login' => [ - 'username' => Bootstrap::ADMIN_NAME, - 'password' => Bootstrap::ADMIN_PASSWORD, - ], - ] - ); + $this->getRequest() + ->setMethod(Http::METHOD_POST) + ->setPostValue( + [ + 'form_key' => $this->formKey->getFormKey(), + 'login' => [ + 'username' => Bootstrap::ADMIN_NAME, + 'password' => Bootstrap::ADMIN_PASSWORD, + ], + ] + ); $this->dispatch('backend/admin/index/index'); // Location header is different than in the successful case @@ -177,22 +181,23 @@ 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_login invisible - * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestWithFailedReCaptchaValidation() { $this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false); - $this->getRequest()->setPostValue( - [ - 'form_key' => $this->formKey->getFormKey(), - 'login' => [ - 'username' => Bootstrap::ADMIN_NAME, - 'password' => Bootstrap::ADMIN_PASSWORD, - ], - CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test', - ] - ); + $this->getRequest() + ->setMethod(Http::METHOD_POST) + ->setPostValue( + [ + 'form_key' => $this->formKey->getFormKey(), + 'login' => [ + 'username' => Bootstrap::ADMIN_NAME, + 'password' => Bootstrap::ADMIN_PASSWORD, + ], + CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test', + ] + ); $this->dispatch('backend/admin/index/index'); // Location header is different than in the successful case @@ -228,16 +233,18 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false): vo */ private function checkSuccessfulPostResponse(array $postValues = []): void { - $this->getRequest()->setPostValue(array_replace_recursive( - [ - 'form_key' => $this->formKey->getFormKey(), - 'login' => [ - 'username' => Bootstrap::ADMIN_NAME, - 'password' => Bootstrap::ADMIN_PASSWORD, + $this->getRequest() + ->setMethod(Http::METHOD_POST) + ->setPostValue(array_replace_recursive( + [ + 'form_key' => $this->formKey->getFormKey(), + 'login' => [ + 'username' => Bootstrap::ADMIN_NAME, + 'password' => Bootstrap::ADMIN_PASSWORD, + ], ], - ], - $postValues - )); + $postValues + )); $this->dispatch('backend/admin/index/index'); $this->assertRedirect(self::equalTo('backend/admin/index/index'));