Skip to content

Commit 2449839

Browse files
committed
Unit test for \Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver
1 parent 606c3e8 commit 2449839

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
*/
29+
class CheckUserForgotPasswordBackendObserverTest extends TestCase
30+
{
31+
/**
32+
* @var MockObject|DataHelper
33+
*/
34+
private $helperMock;
35+
36+
/**
37+
* @var MockObject|CaptchaStringResolver
38+
*/
39+
private $captchaStringResolverMock;
40+
41+
/**
42+
* @var MockObject|SessionManagerInterface
43+
*/
44+
private $sessionMock;
45+
46+
/**
47+
* @var MockObject|ActionFlag
48+
*/
49+
private $actionFlagMock;
50+
51+
/**
52+
* @var MockObject|ManagerInterface
53+
*/
54+
private $messageManagerMock;
55+
56+
/**
57+
* @var CheckUserForgotPasswordBackendObserver
58+
*/
59+
private $observer;
60+
61+
/**
62+
* @var MockObject
63+
*/
64+
private $captchaMock;
65+
66+
/**
67+
* @var MockObject|Observer
68+
*/
69+
private $eventObserverMock;
70+
71+
/**
72+
* @var MockObject|Action
73+
*/
74+
private $controllerMock;
75+
76+
/**
77+
* @var MockObject|HttpResponse
78+
*/
79+
private $httpResponseMock;
80+
81+
/**
82+
* @inheritDoc
83+
*/
84+
protected function setUp()
85+
{
86+
$formId = 'backend_forgotpassword';
87+
$email = 'stub@test.mail';
88+
$requestParams = ['STUB_PARAM'];
89+
90+
$this->helperMock = $this->createMock(DataHelper::class);
91+
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class);
92+
$this->sessionMock = $this->getMockBuilder(SessionManagerInterface::class)
93+
->setMethods(['setEmail'])
94+
->getMockForAbstractClass();
95+
$this->actionFlagMock = $this->createMock(ActionFlag::class);
96+
$this->messageManagerMock = $this->createMock(ManagerInterface::class);
97+
98+
$objectManager = new ObjectManagerHelper($this);
99+
$this->observer = $objectManager->getObject(
100+
CheckUserForgotPasswordBackendObserver::class,
101+
[
102+
'_helper' => $this->helperMock,
103+
'captchaStringResolver' => $this->captchaStringResolverMock,
104+
'_session' => $this->sessionMock,
105+
'_actionFlag' => $this->actionFlagMock,
106+
'messageManager' => $this->messageManagerMock
107+
]
108+
);
109+
110+
$this->captchaMock = $this->getMockBuilder(CaptchaInterface::class)
111+
->setMethods(['isRequired', 'isCorrect'])
112+
->getMockForAbstractClass();
113+
$this->helperMock->expects($this->once())
114+
->method('getCaptcha')
115+
->with($formId)
116+
->willReturn($this->captchaMock);
117+
118+
$requestMock = $this->createMock(HttpRequest::class);
119+
$requestMock->expects($this->any())
120+
->method('getParam')
121+
->with('email')
122+
->willReturn($email);
123+
$requestMock->expects($this->any())
124+
->method('getParams')
125+
->willReturn($requestParams);
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($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->captchaMock->expects($this->once())->method('isRequired')->willReturn(true);
151+
$this->captchaMock->expects($this->once())->method('isCorrect')->willReturn(true);
152+
$this->messageManagerMock->expects($this->never())->method('addErrorMessage');
153+
$this->httpResponseMock->expects($this->never())->method('setRedirect');
154+
155+
$this->observer->execute($this->eventObserverMock);
156+
}
157+
158+
/**
159+
* Test case when Captcha is required and was entered incorrectly.
160+
*/
161+
public function testExecuteWhenCaptchaIsIncorrect()
162+
{
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->captchaMock->expects($this->once())->method('isRequired')->willReturn(false);
183+
$this->messageManagerMock->expects($this->never())->method('addErrorMessage');
184+
$this->httpResponseMock->expects($this->never())->method('setRedirect');
185+
186+
$this->observer->execute($this->eventObserverMock);
187+
}
188+
}

0 commit comments

Comments
 (0)