|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\SendFriend\Test\Unit\Model; |
| 10 | + |
| 11 | +use Magento\Authorization\Model\UserContextInterface; |
| 12 | +use Magento\Captcha\Helper\Data; |
| 13 | +use Magento\Captcha\Model\DefaultModel; |
| 14 | +use Magento\Captcha\Observer\CaptchaStringResolver; |
| 15 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 16 | +use Magento\Framework\App\RequestInterface; |
| 17 | +use Magento\Framework\Exception\LocalizedException; |
| 18 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 19 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 20 | +use Magento\SendFriend\Model\CaptchaValidator; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use PHPUnit_Framework_MockObject_MockObject; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test CaptchaValidatorTest |
| 26 | + */ |
| 27 | +class CaptchaValidatorTest extends TestCase |
| 28 | +{ |
| 29 | + const FORM_ID = 'product_sendtofriend_form'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CaptchaValidator |
| 33 | + */ |
| 34 | + private $model; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CaptchaStringResolver|PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $captchaStringResolverMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var UserContextInterface|PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $currentUserMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var CustomerRepositoryInterface|PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $customerRepositoryMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Data|PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $captchaHelperMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var DefaultModel|PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + private $captchaMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var RequestInterface|PHPUnit_Framework_MockObject_MockObject |
| 63 | + */ |
| 64 | + private $requestMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * Set Up |
| 68 | + */ |
| 69 | + protected function setUp() |
| 70 | + { |
| 71 | + $objectManager = new ObjectManager($this); |
| 72 | + |
| 73 | + $this->captchaHelperMock = $this->createMock(Data::class); |
| 74 | + $this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class); |
| 75 | + $this->currentUserMock = $this->getMockBuilder(UserContextInterface::class) |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + $this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class); |
| 78 | + $this->captchaMock = $this->createMock(DefaultModel::class); |
| 79 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMock(); |
| 80 | + |
| 81 | + $this->model = $objectManager->getObject( |
| 82 | + CaptchaValidator::class, |
| 83 | + [ |
| 84 | + 'captchaHelper' => $this->captchaHelperMock, |
| 85 | + 'captchaStringResolver' => $this->captchaStringResolverMock, |
| 86 | + 'currentUser' => $this->currentUserMock, |
| 87 | + 'customerRepository' => $this->customerRepositoryMock, |
| 88 | + ] |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Testing the captcha validation before sending the email |
| 94 | + * |
| 95 | + * @dataProvider captchaProvider |
| 96 | + * |
| 97 | + * @param bool $captchaIsRequired |
| 98 | + * @param bool $captchaWordIsValid |
| 99 | + * |
| 100 | + * @throws LocalizedException |
| 101 | + * @throws NoSuchEntityException |
| 102 | + */ |
| 103 | + public function testCaptchaValidationOnSend(bool $captchaIsRequired, bool $captchaWordIsValid) |
| 104 | + { |
| 105 | + $word = 'test-word'; |
| 106 | + $this->captchaHelperMock->expects($this->once())->method('getCaptcha')->with(static::FORM_ID) |
| 107 | + ->will($this->returnValue($this->captchaMock)); |
| 108 | + $this->captchaMock->expects($this->once())->method('isRequired') |
| 109 | + ->will($this->returnValue($captchaIsRequired)); |
| 110 | + |
| 111 | + if ($captchaIsRequired) { |
| 112 | + $this->captchaStringResolverMock->expects($this->once())->method('resolve') |
| 113 | + ->with($this->requestMock, static::FORM_ID)->will($this->returnValue($word)); |
| 114 | + $this->captchaMock->expects($this->once())->method('isCorrect')->with($word) |
| 115 | + ->will($this->returnValue($captchaWordIsValid)); |
| 116 | + } |
| 117 | + |
| 118 | + $this->model->validateSending($this->requestMock); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Testing the wrong used word for captcha |
| 123 | + * |
| 124 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 125 | + * @expectedExceptionMessage Incorrect CAPTCHA |
| 126 | + */ |
| 127 | + public function testWrongCaptcha() |
| 128 | + { |
| 129 | + $word = 'test-word'; |
| 130 | + $captchaIsRequired = true; |
| 131 | + $captchaWordIsCorrect = false; |
| 132 | + $this->captchaHelperMock->expects($this->once())->method('getCaptcha')->with(static::FORM_ID) |
| 133 | + ->will($this->returnValue($this->captchaMock)); |
| 134 | + $this->captchaMock->expects($this->once())->method('isRequired') |
| 135 | + ->will($this->returnValue($captchaIsRequired)); |
| 136 | + $this->captchaStringResolverMock->expects($this->any())->method('resolve') |
| 137 | + ->with($this->requestMock, static::FORM_ID)->will($this->returnValue($word)); |
| 138 | + $this->captchaMock->expects($this->any())->method('isCorrect')->with($word) |
| 139 | + ->will($this->returnValue($captchaWordIsCorrect)); |
| 140 | + |
| 141 | + $this->model->validateSending($this->requestMock); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Providing captcha settings |
| 146 | + * |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + public function captchaProvider(): array |
| 150 | + { |
| 151 | + return [ |
| 152 | + [ |
| 153 | + true, |
| 154 | + true |
| 155 | + ], [ |
| 156 | + false, |
| 157 | + false |
| 158 | + ] |
| 159 | + ]; |
| 160 | + } |
| 161 | +} |
0 commit comments