diff --git a/ReCaptchaContact/Test/Integration/ContactFormTest.php b/ReCaptchaContact/Test/Integration/ContactFormTest.php index d797d681..d84782a3 100644 --- a/ReCaptchaContact/Test/Integration/ContactFormTest.php +++ b/ReCaptchaContact/Test/Integration/ContactFormTest.php @@ -183,8 +183,8 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false) self::assertNotEmpty($content); $shouldContainReCaptcha - ? $this->assertContains('field-recaptcha', $content) - : $this->assertNotContains('field-recaptcha', $content); + ? self::assertContains('field-recaptcha', $content) + : self::assertNotContains('field-recaptcha', $content); self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); } @@ -209,16 +209,16 @@ private function checkPostResponse(bool $isSuccessfulRequest, array $postValues $this->dispatch('contact/index/post'); - $this->assertRedirect($this->stringContains('contact/index')); + $this->assertRedirect(self::stringContains('contact/index')); if ($isSuccessfulRequest) { $this->assertSessionMessages( - $this->contains( + self::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)); + self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); } else { $this->assertSessionMessages( $this->equalTo(['reCAPTCHA verification failed']), diff --git a/ReCaptchaNewsletter/Test/Integration/NewsletterFormTest.php b/ReCaptchaNewsletter/Test/Integration/NewsletterFormTest.php new file mode 100644 index 00000000..5993ef7e --- /dev/null +++ b/ReCaptchaNewsletter/Test/Integration/NewsletterFormTest.php @@ -0,0 +1,271 @@ +mutableScopeConfig = $this->_objectManager->get(MutableScopeConfig::class); + $this->formKey = $this->_objectManager->get(FormKey::class); + $this->url = $this->_objectManager->get(UrlInterface::class); + $this->subscriberFactory = $this->_objectManager->get(SubscriberFactory::class); + + $this->captchaValidationResultMock = $this->createMock(ValidationResult::class); + $captchaValidationResultMock = $this->createMock(Validator::class); + $captchaValidationResultMock->expects($this->any()) + ->method('isValid') + ->willReturn($this->captchaValidationResultMock); + $this->_objectManager->addSharedInstance($captchaValidationResultMock, Validator::class); + } + /** + * @magentoConfigFixture default_store 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->setConfig(false, 'test_public_key', 'test_private_key'); + + $this->checkSuccessfulGetResponse(); + } + + /** + * @magentoConfigFixture default_store customer/captcha/enable 0 + * @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible + * + * It's needed for proper work of "ifconfig" in layout during tests running + * @magentoConfigFixture default_store recaptcha_frontend/type_for/newsletter invisible + */ + public function testGetRequestIfReCaptchaKeysAreNotConfigured() + { + $this->setConfig(true, null, null); + + $this->checkSuccessfulGetResponse(); + } + + /** + * @magentoConfigFixture default_store 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/newsletter invisible + * + * It's needed for proper work of "ifconfig" in layout during tests running + * @magentoConfigFixture default_store recaptcha_frontend/type_for/newsletter invisible + */ + public function testGetRequestIfReCaptchaIsEnabled() + { + $this->setConfig(true, 'test_public_key', 'test_private_key'); + + $this->checkSuccessfulGetResponse(true); + } + + /** + * @magentoConfigFixture default_store 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->setConfig(false, 'test_public_key', 'test_private_key'); + + $this->checkPostResponse(true); + } + + /** + * @magentoConfigFixture default_store customer/captcha/enable 0 + * @magentoConfigFixture base_website recaptcha_frontend/type_for/newsletter invisible + */ + public function testPostRequestIfReCaptchaKeysAreNotConfigured() + { + $this->setConfig(true, null, null); + + $this->checkPostResponse(true); + } + + /** + * @magentoConfigFixture default_store 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/newsletter invisible + */ + public function testPostRequestWithSuccessfulReCaptchaValidation() + { + $this->setConfig(true, 'test_public_key', 'test_private_key'); + $this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true); + + $this->checkPostResponse( + true, + [CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test'] + ); + } + + /** + * @magentoConfigFixture default_store 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/newsletter invisible + */ + public function testPostRequestIfReCaptchaParameterIsMissed() + { + $this->setConfig(true, 'test_public_key', 'test_private_key'); + + $this->expectException(InputException::class); + $this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.'); + + $this->checkPostResponse(false); + } + + /** + * @magentoConfigFixture default_store 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/newsletter invisible + */ + public function testPostRequestWithFailedReCaptchaValidation() + { + $this->setConfig(true, '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($this->url->getRouteUrl()); + $content = $this->getResponse()->getBody(); + + self::assertNotEmpty($content); + + $shouldContainReCaptcha + ? self::assertContains('field-recaptcha', $content) + : self::assertNotContains('field-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(Http::METHOD_POST) + ->setPostValue(array_replace_recursive( + [ + 'form_key' => $this->formKey->getFormKey(), + 'email' => 'user@example.com', + ], + $postValues + )); + + $this->dispatch('newsletter/subscriber/new'); + + $this->assertRedirect(self::equalTo($this->url->getRouteUrl())); + + if ($isSuccessfulRequest) { + $this->assertSessionMessages( + self::contains( + 'Thank you for your subscription.' + ), + MessageInterface::TYPE_SUCCESS + ); + self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); + } else { + $this->assertSessionMessages( + $this->equalTo(['reCAPTCHA verification failed']), + MessageInterface::TYPE_ERROR + ); + } + } + + /** + * @param bool $isEnabled + * @param string|null $public + * @param string|null $private + */ + private function setConfig(bool $isEnabled, ?string $public, ?string $private): void + { + $this->mutableScopeConfig->setValue( + 'recaptcha_frontend/type_for/newsletter', + $isEnabled ? '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 + ); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->subscriberFactory->create()->loadBySubscriberEmail('user@example.com', 1)->delete(); + } +}