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