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