|
| 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\ReCaptchaSendFriend\Test\Integration; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\App\Request\Http; |
| 12 | +use Magento\Framework\Data\Form\FormKey; |
| 13 | +use Magento\Framework\Exception\InputException; |
| 14 | +use Magento\Framework\Message\MessageInterface; |
| 15 | +use Magento\Framework\Validation\ValidationResult; |
| 16 | +use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; |
| 17 | +use Magento\ReCaptchaValidation\Model\Validator; |
| 18 | +use Magento\Store\Model\ScopeInterface; |
| 19 | +use Magento\TestFramework\App\MutableScopeConfig; |
| 20 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 21 | +use Magento\TestFramework\TestCase\AbstractController; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | + |
| 24 | +/** |
| 25 | + * @magentoAppArea frontend |
| 26 | + * @magentoAppIsolation enabled |
| 27 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 28 | + */ |
| 29 | +class SendFriendFormTest extends AbstractController |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var MutableScopeConfig |
| 33 | + */ |
| 34 | + private $mutableScopeConfig; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var FormKey |
| 38 | + */ |
| 39 | + private $formKey; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ProductRepositoryInterface |
| 43 | + */ |
| 44 | + private $productRepository; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var TransportBuilderMock |
| 48 | + */ |
| 49 | + private $transportMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var ValidationResult|MockObject |
| 53 | + */ |
| 54 | + private $captchaValidationResultMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + protected function setUp() |
| 60 | + { |
| 61 | + parent::setUp(); |
| 62 | + $this->mutableScopeConfig = $this->_objectManager->get(MutableScopeConfig::class); |
| 63 | + $this->formKey = $this->_objectManager->get(FormKey::class); |
| 64 | + |
| 65 | + $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class); |
| 66 | + $this->transportMock = $this->_objectManager->get(TransportBuilderMock::class); |
| 67 | + |
| 68 | + $this->captchaValidationResultMock = $this->createMock(ValidationResult::class); |
| 69 | + $captchaValidationResultMock = $this->createMock(Validator::class); |
| 70 | + $captchaValidationResultMock->expects($this->any()) |
| 71 | + ->method('isValid') |
| 72 | + ->willReturn($this->captchaValidationResultMock); |
| 73 | + $this->_objectManager->addSharedInstance($captchaValidationResultMock, Validator::class); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 78 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 79 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 80 | + * |
| 81 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 82 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 83 | + */ |
| 84 | + public function testGetRequestIfReCaptchaIsDisabled() |
| 85 | + { |
| 86 | + $this->setConfig(false, 'test_public_key', 'test_private_key'); |
| 87 | + |
| 88 | + $this->checkSuccessfulGetResponse(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 93 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 94 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 95 | + * |
| 96 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible |
| 97 | + * |
| 98 | + * It's needed for proper work of "ifconfig" in layout during tests running |
| 99 | + * @magentoConfigFixture default_store recaptcha_frontend/type_for/sendfriend invisible |
| 100 | + */ |
| 101 | + public function testGetRequestIfReCaptchaKeysAreNotConfigured() |
| 102 | + { |
| 103 | + $this->setConfig(true, null, null); |
| 104 | + |
| 105 | + $this->checkSuccessfulGetResponse(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 110 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 111 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 112 | + * |
| 113 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 114 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 115 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible |
| 116 | + * |
| 117 | + * It's needed for proper work of "ifconfig" in layout during tests running |
| 118 | + * @magentoConfigFixture default_store recaptcha_frontend/type_for/sendfriend invisible |
| 119 | + */ |
| 120 | + public function testGetRequestIfReCaptchaIsEnabled() |
| 121 | + { |
| 122 | + $this->setConfig(true, 'test_public_key', 'test_private_key'); |
| 123 | + |
| 124 | + $this->checkSuccessfulGetResponse(true); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 129 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 130 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 131 | + * |
| 132 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 133 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 134 | + */ |
| 135 | + public function testPostRequestIfReCaptchaIsDisabled() |
| 136 | + { |
| 137 | + $this->setConfig(false, 'test_public_key', 'test_private_key'); |
| 138 | + |
| 139 | + $this->checkPostResponse(true); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 144 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 145 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 146 | + * |
| 147 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible |
| 148 | + */ |
| 149 | + public function testPostRequestIfReCaptchaKeysAreNotConfigured() |
| 150 | + { |
| 151 | + $this->setConfig(true, null, null); |
| 152 | + |
| 153 | + $this->checkPostResponse(true); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 158 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 159 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 160 | + * |
| 161 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 162 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 163 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible |
| 164 | + */ |
| 165 | + public function testPostRequestWithSuccessfulReCaptchaValidation() |
| 166 | + { |
| 167 | + $this->setConfig(true, 'test_public_key', 'test_private_key'); |
| 168 | + $this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true); |
| 169 | + |
| 170 | + $this->checkPostResponse( |
| 171 | + true, |
| 172 | + [CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test'] |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 178 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 179 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 180 | + * |
| 181 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 182 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 183 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible |
| 184 | + */ |
| 185 | + public function testPostRequestIfReCaptchaParameterIsMissed() |
| 186 | + { |
| 187 | + $this->setConfig(true, 'test_public_key', 'test_private_key'); |
| 188 | + |
| 189 | + $this->expectException(InputException::class); |
| 190 | + $this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.'); |
| 191 | + |
| 192 | + $this->checkPostResponse(false); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @magentoConfigFixture default_store sendfriend/email/enabled 1 |
| 197 | + * @magentoConfigFixture default_store sendfriend/email/allow_guest 1 |
| 198 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 199 | + * |
| 200 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 201 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 202 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/sendfriend invisible |
| 203 | + */ |
| 204 | + public function testPostRequestWithFailedReCaptchaValidation() |
| 205 | + { |
| 206 | + $this->setConfig(true, 'test_public_key', 'test_private_key'); |
| 207 | + $this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false); |
| 208 | + |
| 209 | + $this->checkPostResponse( |
| 210 | + false, |
| 211 | + [CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test'] |
| 212 | + ); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * @param bool $shouldContainReCaptcha |
| 217 | + */ |
| 218 | + private function checkSuccessfulGetResponse($shouldContainReCaptcha = false) |
| 219 | + { |
| 220 | + $this->dispatch('sendfriend/product/send/id/1'); |
| 221 | + $content = $this->getResponse()->getBody(); |
| 222 | + |
| 223 | + self::assertNotEmpty($content); |
| 224 | + |
| 225 | + $shouldContainReCaptcha |
| 226 | + ? self::assertContains('field-recaptcha', $content) |
| 227 | + : self::assertNotContains('field-recaptcha', $content); |
| 228 | + |
| 229 | + self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * @param bool $isSuccessfulRequest |
| 234 | + * @param array $postValues |
| 235 | + */ |
| 236 | + private function checkPostResponse(bool $isSuccessfulRequest, array $postValues = []) |
| 237 | + { |
| 238 | + $expectedUrl = 'http://localhost/index.php/simple-product.html'; |
| 239 | + |
| 240 | + $this->getRequest() |
| 241 | + ->setParam(\Magento\Framework\App\Response\RedirectInterface::PARAM_NAME_REFERER_URL, $expectedUrl) |
| 242 | + ->setMethod(Http::METHOD_POST) |
| 243 | + ->setPostValue(array_replace_recursive( |
| 244 | + [ |
| 245 | + 'sender' => [ |
| 246 | + 'name' => 'Sender', |
| 247 | + |
| 248 | + 'message' => 'Message', |
| 249 | + ], |
| 250 | + 'recipients' => [ |
| 251 | + 'name' => [ |
| 252 | + 'Recipient', |
| 253 | + ], |
| 254 | + 'email' => [ |
| 255 | + |
| 256 | + ] |
| 257 | + ], |
| 258 | + 'form_key' => $this->formKey->getFormKey(), |
| 259 | + ], |
| 260 | + $postValues |
| 261 | + )); |
| 262 | + |
| 263 | + $this->dispatch('sendfriend/product/sendmail/id/1'); |
| 264 | + |
| 265 | + $this->assertRedirect(self::equalTo($expectedUrl)); |
| 266 | + |
| 267 | + if ($isSuccessfulRequest) { |
| 268 | + $this->assertSessionMessages( |
| 269 | + self::contains( |
| 270 | + 'The link to a friend was sent.' |
| 271 | + ), |
| 272 | + MessageInterface::TYPE_SUCCESS |
| 273 | + ); |
| 274 | + self::assertEmpty($this->getSessionMessages(MessageInterface::TYPE_ERROR)); |
| 275 | + |
| 276 | + $message = $this->transportMock->getSentMessage(); |
| 277 | + self::assertNotEmpty($message); |
| 278 | + self::assertEquals((string)__('Welcome, Recipient'), $message->getSubject()); |
| 279 | + } else { |
| 280 | + $this->assertSessionMessages( |
| 281 | + $this->equalTo(['reCAPTCHA verification failed']), |
| 282 | + MessageInterface::TYPE_ERROR |
| 283 | + ); |
| 284 | + self::assertEmpty($this->transportMock->getSentMessage()); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * @param bool $isEnabled |
| 290 | + * @param string|null $public |
| 291 | + * @param string|null $private |
| 292 | + */ |
| 293 | + private function setConfig(bool $isEnabled, ?string $public, ?string $private): void |
| 294 | + { |
| 295 | + $this->mutableScopeConfig->setValue( |
| 296 | + 'recaptcha_frontend/type_for/sendfriend', |
| 297 | + $isEnabled ? 'invisible' : null, |
| 298 | + ScopeInterface::SCOPE_WEBSITE |
| 299 | + ); |
| 300 | + $this->mutableScopeConfig->setValue( |
| 301 | + 'recaptcha_frontend/type_invisible/public_key', |
| 302 | + $public, |
| 303 | + ScopeInterface::SCOPE_WEBSITE |
| 304 | + ); |
| 305 | + $this->mutableScopeConfig->setValue( |
| 306 | + 'recaptcha_frontend/type_invisible/private_key', |
| 307 | + $private, |
| 308 | + ScopeInterface::SCOPE_WEBSITE |
| 309 | + ); |
| 310 | + } |
| 311 | +} |
0 commit comments