Skip to content

Commit 0bb11c7

Browse files
ENGCOM-6900: Unit test for \Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver #26712
- Merge Pull Request #26712 from karyna-tsymbal-atwix/magento2:unit-test-captcha-observer-CheckUserForgotPasswordBackendObserver - Merged commits: 1. 2449839 2. 17990cb 3. 0d17e0a
2 parents e280215 + 0d17e0a commit 0bb11c7

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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\Captcha\Test\Unit\Observer;
10+
11+
use Magento\Captcha\Helper\Data as DataHelper;
12+
use Magento\Captcha\Model\CaptchaInterface;
13+
use Magento\Captcha\Observer\CaptchaStringResolver;
14+
use Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver;
15+
use Magento\Framework\App\Action\Action;
16+
use Magento\Framework\App\ActionFlag;
17+
use Magento\Framework\App\Request\Http as HttpRequest;
18+
use Magento\Framework\App\Response\Http as HttpResponse;
19+
use Magento\Framework\Event\Observer;
20+
use Magento\Framework\Message\ManagerInterface;
21+
use Magento\Framework\Session\SessionManagerInterface;
22+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
23+
use PHPUnit\Framework\MockObject\MockObject;
24+
use PHPUnit\Framework\TestCase;
25+
26+
/**
27+
* Unit Test for \Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29+
*/
30+
class CheckUserForgotPasswordBackendObserverTest extends TestCase
31+
{
32+
const STUB_EMAIL = 'stub@test.mail';
33+
const STUB_REQUEST_PARAMS = ['STUB_PARAM'];
34+
35+
/**
36+
* @var MockObject|DataHelper
37+
*/
38+
private $helperMock;
39+
40+
/**
41+
* @var MockObject|CaptchaStringResolver
42+
*/
43+
private $captchaStringResolverMock;
44+
45+
/**
46+
* @var MockObject|SessionManagerInterface
47+
*/
48+
private $sessionMock;
49+
50+
/**
51+
* @var MockObject|ActionFlag
52+
*/
53+
private $actionFlagMock;
54+
55+
/**
56+
* @var MockObject|ManagerInterface
57+
*/
58+
private $messageManagerMock;
59+
60+
/**
61+
* @var CheckUserForgotPasswordBackendObserver
62+
*/
63+
private $observer;
64+
65+
/**
66+
* @var MockObject|CaptchaInterface
67+
*/
68+
private $captchaMock;
69+
70+
/**
71+
* @var MockObject|Observer
72+
*/
73+
private $eventObserverMock;
74+
75+
/**
76+
* @var MockObject|Action
77+
*/
78+
private $controllerMock;
79+
80+
/**
81+
* @var MockObject|HttpResponse
82+
*/
83+
private $httpResponseMock;
84+
85+
/**
86+
* @var MockObject|HttpRequest
87+
*/
88+
private $requestMock;
89+
90+
/**
91+
* @inheritDoc
92+
*/
93+
protected function setUp()
94+
{
95+
$formId = 'backend_forgotpassword';
96+
97+
$this->helperMock = $this->createMock(DataHelper::class);
98+
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class);
99+
$this->sessionMock = $this->getMockBuilder(SessionManagerInterface::class)
100+
->setMethods(['setEmail'])
101+
->getMockForAbstractClass();
102+
$this->actionFlagMock = $this->createMock(ActionFlag::class);
103+
$this->messageManagerMock = $this->createMock(ManagerInterface::class);
104+
105+
$objectManager = new ObjectManagerHelper($this);
106+
$this->observer = $objectManager->getObject(
107+
CheckUserForgotPasswordBackendObserver::class,
108+
[
109+
'_helper' => $this->helperMock,
110+
'captchaStringResolver' => $this->captchaStringResolverMock,
111+
'_session' => $this->sessionMock,
112+
'_actionFlag' => $this->actionFlagMock,
113+
'messageManager' => $this->messageManagerMock
114+
]
115+
);
116+
117+
$this->captchaMock = $this->getMockBuilder(CaptchaInterface::class)
118+
->setMethods(['isRequired', 'isCorrect'])
119+
->getMockForAbstractClass();
120+
$this->helperMock->expects($this->once())
121+
->method('getCaptcha')
122+
->with($formId)
123+
->willReturn($this->captchaMock);
124+
125+
$this->requestMock = $this->createMock(HttpRequest::class);
126+
$this->httpResponseMock = $this->createMock(HttpResponse::class);
127+
128+
$this->controllerMock = $this->getMockBuilder(Action::class)
129+
->disableOriginalConstructor()
130+
->setMethods(['getUrl', 'getRequest', 'getResponse'])
131+
->getMockForAbstractClass();
132+
$this->controllerMock->expects($this->any())
133+
->method('getRequest')
134+
->willReturn($this->requestMock);
135+
$this->controllerMock->expects($this->any())
136+
->method('getResponse')
137+
->willReturn($this->httpResponseMock);
138+
139+
$this->eventObserverMock = $this->createPartialMock(Observer::class, ['getControllerAction']);
140+
$this->eventObserverMock->expects($this->any())
141+
->method('getControllerAction')
142+
->willReturn($this->controllerMock);
143+
}
144+
145+
/**
146+
* Test case when Captcha is required and was entered correctly.
147+
*/
148+
public function testExecuteWhenCaptchaIsCorrect()
149+
{
150+
$this->configureRequestMockWithStubValues();
151+
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(true);
152+
$this->captchaMock->expects($this->once())->method('isCorrect')->willReturn(true);
153+
154+
$this->executeOriginalMethodExpectsNoError();
155+
}
156+
157+
/**
158+
* Test case when Captcha is required and was entered incorrectly.
159+
*/
160+
public function testExecuteWhenCaptchaIsIncorrect()
161+
{
162+
$this->configureRequestMockWithStubValues();
163+
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(true);
164+
$this->captchaMock->expects($this->once())->method('isCorrect')->willReturn(false);
165+
166+
$this->sessionMock->expects($this->once())->method('setEmail');
167+
$this->actionFlagMock->expects($this->once())->method('set');
168+
$this->controllerMock->expects($this->once())->method('getUrl');
169+
$this->messageManagerMock->expects($this->once())
170+
->method('addErrorMessage')
171+
->with(__('Incorrect CAPTCHA'));
172+
$this->httpResponseMock->expects($this->once())->method('setRedirect')->willReturnSelf();
173+
174+
$this->observer->execute($this->eventObserverMock);
175+
}
176+
177+
/**
178+
* Test case when Captcha is not required.
179+
*/
180+
public function testExecuteWhenCaptchaIsNotRequired()
181+
{
182+
$this->configureRequestMockWithStubValues();
183+
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(false);
184+
185+
$this->executeOriginalMethodExpectsNoError();
186+
}
187+
188+
/**
189+
* Test case when email is not provided
190+
*/
191+
public function testExecuteWhenEmailParamIsNotPresent()
192+
{
193+
$this->requestMock->expects($this->any())
194+
->method('getParam')
195+
->with('email')
196+
->willReturn(null);
197+
$this->requestMock->expects($this->any())
198+
->method('getParams')
199+
->willReturn(self::STUB_REQUEST_PARAMS);
200+
$this->captchaMock->expects($this->never())->method('isRequired');
201+
$this->captchaMock->expects($this->never())->method('isCorrect');
202+
203+
$this->executeOriginalMethodExpectsNoError();
204+
}
205+
206+
/**
207+
* Stub params for Request Mock
208+
*/
209+
private function configureRequestMockWithStubValues()
210+
{
211+
$this->requestMock->expects($this->any())
212+
->method('getParam')
213+
->with('email')
214+
->willReturn(self::STUB_EMAIL);
215+
$this->requestMock->expects($this->any())
216+
->method('getParams')
217+
->willReturn(self::STUB_REQUEST_PARAMS);
218+
}
219+
220+
/**
221+
* Run original method, expect there is no error
222+
*/
223+
private function executeOriginalMethodExpectsNoError()
224+
{
225+
$this->messageManagerMock->expects($this->never())->method('addErrorMessage');
226+
$this->httpResponseMock->expects($this->never())->method('setRedirect');
227+
228+
$this->observer->execute($this->eventObserverMock);
229+
}
230+
}

0 commit comments

Comments
 (0)