![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-re-captcha-contact/Test/Integration/ |
<?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\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(): void { 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 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(): void { $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/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(): void { $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/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(): void { $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(): void { $this->setConfig(false, 'test_public_key', 'test_private_key'); $this->checkSuccessfulPostResponse(); } /** * @magentoConfigFixture default_store customer/captcha/enable 0 * @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible */ public function testPostRequestIfReCaptchaKeysAreNotConfigured(): void { $this->setConfig(true, null, null); $this->checkSuccessfulPostResponse(); } /** * @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/contact invisible */ public function testPostRequestWithSuccessfulReCaptchaValidation(): void { $this->setConfig(true, 'test_public_key', 'test_private_key'); $this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true); $this->checkSuccessfulPostResponse( [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/contact invisible */ public function testPostRequestIfReCaptchaParameterIsMissed(): void { $this->setConfig(true, 'test_public_key', 'test_private_key'); $this->checkFailedPostResponse(); } /** * @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/contact invisible */ public function testPostRequestWithFailedReCaptchaValidation(): void { $this->setConfig(true, 'test_public_key', 'test_private_key'); $this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false); $this->checkFailedPostResponse( [CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test'] ); } /** * @param bool $shouldContainReCaptcha * @return void */ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false): void { $this->dispatch('contact/index'); $content = $this->getResponse()->getBody(); self::assertNotEmpty($content); $shouldContainReCaptcha ? self::assertStringContainsString('field-recaptcha', $content) : self::assertStringNotContainsString('field-recaptcha', $content); self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); } /** * @param array $postValues * @return void */ private function checkSuccessfulPostResponse(array $postValues = []): void { $this->makePostRequest($postValues); $this->assertSessionMessages( self::containsEqual( "Thanks for contacting us with your comments and questions. We'll respond to you very soon." ), MessageInterface::TYPE_SUCCESS ); self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); } /** * @param array $postValues * @return void */ private function checkFailedPostResponse(array $postValues = []): void { $this->makePostRequest($postValues); $this->assertSessionMessages( $this->equalTo(['Something went wrong with reCAPTCHA. Please contact the store owner.']), MessageInterface::TYPE_ERROR ); } /** * @param array $postValues * @return void */ private function makePostRequest(array $postValues = []): void { $this->getRequest() ->setMethod(HttpRequest::METHOD_POST) ->setPostValue(array_replace_recursive( [ 'form_key' => $this->formKey->getFormKey(), 'name' => 'customer name', 'comment' => 'comment', 'email' => '[email protected]', ], $postValues )); $this->dispatch('contact/index/post'); $this->assertRedirect(self::stringContains('contact/index')); } /** * @param bool $isEnabled * @param string|null $public * @param string|null $private * @return void */ private function setConfig(bool $isEnabled, ?string $public, ?string $private): void { $this->mutableScopeConfig->setValue( 'recaptcha_frontend/type_for/contact', $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 ); } public function tearDown(): void { $this->mutableScopeConfig->setValue( 'recaptcha_frontend/type_for/contact', null, ScopeInterface::SCOPE_WEBSITE ); $this->mutableScopeConfig->setValue( 'recaptcha_frontend/type_invisible/public_key', null, ScopeInterface::SCOPE_WEBSITE ); $this->mutableScopeConfig->setValue( 'recaptcha_frontend/type_invisible/private_key', null, ScopeInterface::SCOPE_WEBSITE ); } }