|
| 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\Captcha\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\Captcha\Model\DefaultModel as CaptchaModel; |
| 11 | +use Magento\Captcha\Observer\CheckGuestCheckoutObserver; |
| 12 | +use Magento\Captcha\Helper\Data as CaptchaDataHelper; |
| 13 | +use Magento\Framework\App\Action\Action; |
| 14 | +use Magento\Framework\App\ActionFlag; |
| 15 | +use Magento\Captcha\Observer\CaptchaStringResolver; |
| 16 | +use Magento\Checkout\Model\Type\Onepage; |
| 17 | +use Magento\Framework\App\Request\Http; |
| 18 | +use Magento\Framework\App\Response\Http as HttpResponse; |
| 19 | +use Magento\Framework\Event\Observer; |
| 20 | +use Magento\Framework\Json\Helper\Data as JsonHelper; |
| 21 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 22 | +use Magento\Quote\Model\Quote; |
| 23 | + |
| 24 | +/** |
| 25 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 26 | + */ |
| 27 | +class CheckGuestCheckoutObserverTest extends \PHPUnit\Framework\TestCase |
| 28 | +{ |
| 29 | + const FORM_ID = 'guest_checkout'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CheckGuestCheckoutObserver |
| 33 | + */ |
| 34 | + private $checkGuestCheckoutObserver; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ObjectManager |
| 38 | + */ |
| 39 | + private $objectManager; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Observer |
| 43 | + */ |
| 44 | + private $observer; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var HttpResponse|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $responseMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var HttpResponse|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $requestMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var ActionFlag|\PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + private $actionFlagMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject |
| 63 | + */ |
| 64 | + private $captchaStringResolverMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var JsonHelper|\PHPUnit_Framework_MockObject_MockObject |
| 68 | + */ |
| 69 | + private $jsonHelperMock; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var CaptchaModel|\PHPUnit_Framework_MockObject_MockObject |
| 73 | + */ |
| 74 | + private $captchaModelMock; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var Quote|\PHPUnit_Framework_MockObject_MockObject |
| 78 | + */ |
| 79 | + private $quoteModelMock; |
| 80 | + |
| 81 | + /** |
| 82 | + * @var Action|\PHPUnit_Framework_MockObject_MockObject |
| 83 | + */ |
| 84 | + private $controllerMock; |
| 85 | + |
| 86 | + protected function setUp() |
| 87 | + { |
| 88 | + $onepageModelTypeMock = $this->createMock(Onepage::class); |
| 89 | + $captchaHelperMock = $this->createMock(CaptchaDataHelper::class); |
| 90 | + $this->objectManager = new ObjectManager($this); |
| 91 | + $this->actionFlagMock = $this->createMock(ActionFlag::class); |
| 92 | + $this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class); |
| 93 | + $this->captchaModelMock = $this->createMock(CaptchaModel::class); |
| 94 | + $this->quoteModelMock = $this->createMock(Quote::class); |
| 95 | + $this->controllerMock = $this->createMock(Action::class); |
| 96 | + $this->requestMock = $this->createMock(Http::class); |
| 97 | + $this->responseMock = $this->createMock(HttpResponse::class); |
| 98 | + $this->observer = new Observer(['controller_action' => $this->controllerMock]); |
| 99 | + $this->jsonHelperMock = $this->createMock(JsonHelper::class); |
| 100 | + |
| 101 | + $this->checkGuestCheckoutObserver = $this->objectManager->getObject( |
| 102 | + CheckGuestCheckoutObserver::class, |
| 103 | + [ |
| 104 | + 'helper' => $captchaHelperMock, |
| 105 | + 'actionFlag' => $this->actionFlagMock, |
| 106 | + 'captchaStringResolver' => $this->captchaStringResolverMock, |
| 107 | + 'typeOnepage' => $onepageModelTypeMock, |
| 108 | + 'jsonHelper' => $this->jsonHelperMock |
| 109 | + ] |
| 110 | + ); |
| 111 | + |
| 112 | + $captchaHelperMock->expects($this->once()) |
| 113 | + ->method('getCaptcha') |
| 114 | + ->with(self::FORM_ID) |
| 115 | + ->willReturn($this->captchaModelMock); |
| 116 | + $onepageModelTypeMock->expects($this->once()) |
| 117 | + ->method('getQuote') |
| 118 | + ->willReturn($this->quoteModelMock); |
| 119 | + } |
| 120 | + |
| 121 | + public function testCheckGuestCheckoutForRegister() |
| 122 | + { |
| 123 | + $this->quoteModelMock->expects($this->once()) |
| 124 | + ->method('getCheckoutMethod') |
| 125 | + ->willReturn(Onepage::METHOD_REGISTER); |
| 126 | + $this->captchaModelMock->expects($this->never()) |
| 127 | + ->method('isRequired'); |
| 128 | + |
| 129 | + $this->checkGuestCheckoutObserver->execute($this->observer); |
| 130 | + } |
| 131 | + |
| 132 | + public function testCheckGuestCheckoutWithNoCaptchaRequired() |
| 133 | + { |
| 134 | + $this->quoteModelMock->expects($this->once()) |
| 135 | + ->method('getCheckoutMethod') |
| 136 | + ->willReturn(Onepage::METHOD_GUEST); |
| 137 | + $this->captchaModelMock->expects($this->once()) |
| 138 | + ->method('isRequired') |
| 139 | + ->willReturn(false); |
| 140 | + $this->captchaModelMock->expects($this->never()) |
| 141 | + ->method('isCorrect'); |
| 142 | + |
| 143 | + $this->checkGuestCheckoutObserver->execute($this->observer); |
| 144 | + } |
| 145 | + |
| 146 | + public function testCheckGuestCheckoutWithIncorrectCaptcha() |
| 147 | + { |
| 148 | + $captchaValue = 'some_word'; |
| 149 | + $encodedJsonValue = '{}'; |
| 150 | + |
| 151 | + $this->quoteModelMock->expects($this->once()) |
| 152 | + ->method('getCheckoutMethod') |
| 153 | + ->willReturn(Onepage::METHOD_GUEST); |
| 154 | + $this->captchaModelMock->expects($this->once()) |
| 155 | + ->method('isRequired') |
| 156 | + ->willReturn(true); |
| 157 | + $this->controllerMock->expects($this->once()) |
| 158 | + ->method('getRequest') |
| 159 | + ->willReturn($this->requestMock); |
| 160 | + $this->controllerMock->expects($this->once()) |
| 161 | + ->method('getResponse') |
| 162 | + ->willReturn($this->responseMock); |
| 163 | + $this->controllerMock->expects($this->once()) |
| 164 | + ->method('getResponse') |
| 165 | + ->willReturn($this->responseMock); |
| 166 | + $this->captchaStringResolverMock->expects($this->once()) |
| 167 | + ->method('resolve') |
| 168 | + ->with($this->requestMock, self::FORM_ID) |
| 169 | + ->willReturn($captchaValue); |
| 170 | + $this->captchaModelMock->expects($this->once()) |
| 171 | + ->method('isCorrect') |
| 172 | + ->with($captchaValue) |
| 173 | + ->willReturn(false); |
| 174 | + $this->actionFlagMock->expects($this->once()) |
| 175 | + ->method('set') |
| 176 | + ->with('', Action::FLAG_NO_DISPATCH, true); |
| 177 | + $this->jsonHelperMock->expects($this->once()) |
| 178 | + ->method('jsonEncode') |
| 179 | + ->willReturn($encodedJsonValue); |
| 180 | + $this->responseMock->expects($this->once()) |
| 181 | + ->method('representJson') |
| 182 | + ->with($encodedJsonValue); |
| 183 | + |
| 184 | + $this->checkGuestCheckoutObserver->execute($this->observer); |
| 185 | + } |
| 186 | + |
| 187 | + public function testCheckGuestCheckoutWithCorrectCaptcha() |
| 188 | + { |
| 189 | + $this->quoteModelMock->expects($this->once()) |
| 190 | + ->method('getCheckoutMethod') |
| 191 | + ->willReturn(Onepage::METHOD_GUEST); |
| 192 | + $this->captchaModelMock->expects($this->once()) |
| 193 | + ->method('isRequired') |
| 194 | + ->willReturn(true); |
| 195 | + $this->controllerMock->expects($this->once()) |
| 196 | + ->method('getRequest') |
| 197 | + ->willReturn($this->requestMock); |
| 198 | + $this->captchaStringResolverMock->expects($this->once()) |
| 199 | + ->method('resolve') |
| 200 | + ->with($this->requestMock, self::FORM_ID) |
| 201 | + ->willReturn('some_word'); |
| 202 | + $this->captchaModelMock->expects($this->once()) |
| 203 | + ->method('isCorrect') |
| 204 | + ->with('some_word') |
| 205 | + ->willReturn(true); |
| 206 | + $this->actionFlagMock->expects($this->never()) |
| 207 | + ->method('set'); |
| 208 | + |
| 209 | + $this->checkGuestCheckoutObserver->execute($this->observer); |
| 210 | + } |
| 211 | +} |
0 commit comments