-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Unit test for \Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver #26712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 3 commits into
magento:2.4-develop
from
karyna-t:unit-test-captcha-observer-CheckUserForgotPasswordBackendObserver
Feb 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletions
230
app/code/Magento/Captcha/Test/Unit/Observer/CheckUserForgotPasswordBackendObserverTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Captcha\Test\Unit\Observer; | ||
|
||
use Magento\Captcha\Helper\Data as DataHelper; | ||
use Magento\Captcha\Model\CaptchaInterface; | ||
use Magento\Captcha\Observer\CaptchaStringResolver; | ||
use Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver; | ||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\ActionFlag; | ||
use Magento\Framework\App\Request\Http as HttpRequest; | ||
use Magento\Framework\App\Response\Http as HttpResponse; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Message\ManagerInterface; | ||
use Magento\Framework\Session\SessionManagerInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit Test for \Magento\Captcha\Observer\CheckUserForgotPasswordBackendObserver | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class CheckUserForgotPasswordBackendObserverTest extends TestCase | ||
{ | ||
const STUB_EMAIL = 'stub@test.mail'; | ||
const STUB_REQUEST_PARAMS = ['STUB_PARAM']; | ||
|
||
/** | ||
* @var MockObject|DataHelper | ||
*/ | ||
private $helperMock; | ||
|
||
/** | ||
* @var MockObject|CaptchaStringResolver | ||
*/ | ||
private $captchaStringResolverMock; | ||
|
||
/** | ||
* @var MockObject|SessionManagerInterface | ||
*/ | ||
private $sessionMock; | ||
|
||
/** | ||
* @var MockObject|ActionFlag | ||
*/ | ||
private $actionFlagMock; | ||
|
||
/** | ||
* @var MockObject|ManagerInterface | ||
*/ | ||
private $messageManagerMock; | ||
|
||
/** | ||
* @var CheckUserForgotPasswordBackendObserver | ||
*/ | ||
private $observer; | ||
|
||
/** | ||
* @var MockObject|CaptchaInterface | ||
*/ | ||
private $captchaMock; | ||
|
||
/** | ||
* @var MockObject|Observer | ||
*/ | ||
private $eventObserverMock; | ||
|
||
/** | ||
* @var MockObject|Action | ||
*/ | ||
private $controllerMock; | ||
|
||
/** | ||
* @var MockObject|HttpResponse | ||
*/ | ||
private $httpResponseMock; | ||
|
||
/** | ||
* @var MockObject|HttpRequest | ||
*/ | ||
private $requestMock; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$formId = 'backend_forgotpassword'; | ||
|
||
$this->helperMock = $this->createMock(DataHelper::class); | ||
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class); | ||
$this->sessionMock = $this->getMockBuilder(SessionManagerInterface::class) | ||
->setMethods(['setEmail']) | ||
->getMockForAbstractClass(); | ||
$this->actionFlagMock = $this->createMock(ActionFlag::class); | ||
$this->messageManagerMock = $this->createMock(ManagerInterface::class); | ||
|
||
$objectManager = new ObjectManagerHelper($this); | ||
$this->observer = $objectManager->getObject( | ||
CheckUserForgotPasswordBackendObserver::class, | ||
[ | ||
'_helper' => $this->helperMock, | ||
'captchaStringResolver' => $this->captchaStringResolverMock, | ||
'_session' => $this->sessionMock, | ||
'_actionFlag' => $this->actionFlagMock, | ||
'messageManager' => $this->messageManagerMock | ||
] | ||
); | ||
|
||
$this->captchaMock = $this->getMockBuilder(CaptchaInterface::class) | ||
->setMethods(['isRequired', 'isCorrect']) | ||
->getMockForAbstractClass(); | ||
$this->helperMock->expects($this->once()) | ||
->method('getCaptcha') | ||
->with($formId) | ||
->willReturn($this->captchaMock); | ||
|
||
$this->requestMock = $this->createMock(HttpRequest::class); | ||
$this->httpResponseMock = $this->createMock(HttpResponse::class); | ||
|
||
$this->controllerMock = $this->getMockBuilder(Action::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getUrl', 'getRequest', 'getResponse']) | ||
->getMockForAbstractClass(); | ||
$this->controllerMock->expects($this->any()) | ||
->method('getRequest') | ||
->willReturn($this->requestMock); | ||
$this->controllerMock->expects($this->any()) | ||
->method('getResponse') | ||
->willReturn($this->httpResponseMock); | ||
|
||
$this->eventObserverMock = $this->createPartialMock(Observer::class, ['getControllerAction']); | ||
$this->eventObserverMock->expects($this->any()) | ||
->method('getControllerAction') | ||
->willReturn($this->controllerMock); | ||
} | ||
|
||
/** | ||
* Test case when Captcha is required and was entered correctly. | ||
*/ | ||
public function testExecuteWhenCaptchaIsCorrect() | ||
{ | ||
$this->configureRequestMockWithStubValues(); | ||
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(true); | ||
$this->captchaMock->expects($this->once())->method('isCorrect')->willReturn(true); | ||
|
||
$this->executeOriginalMethodExpectsNoError(); | ||
} | ||
|
||
/** | ||
* Test case when Captcha is required and was entered incorrectly. | ||
*/ | ||
public function testExecuteWhenCaptchaIsIncorrect() | ||
{ | ||
$this->configureRequestMockWithStubValues(); | ||
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(true); | ||
$this->captchaMock->expects($this->once())->method('isCorrect')->willReturn(false); | ||
|
||
$this->sessionMock->expects($this->once())->method('setEmail'); | ||
$this->actionFlagMock->expects($this->once())->method('set'); | ||
$this->controllerMock->expects($this->once())->method('getUrl'); | ||
$this->messageManagerMock->expects($this->once()) | ||
->method('addErrorMessage') | ||
->with(__('Incorrect CAPTCHA')); | ||
$this->httpResponseMock->expects($this->once())->method('setRedirect')->willReturnSelf(); | ||
|
||
$this->observer->execute($this->eventObserverMock); | ||
} | ||
|
||
/** | ||
* Test case when Captcha is not required. | ||
*/ | ||
public function testExecuteWhenCaptchaIsNotRequired() | ||
{ | ||
$this->configureRequestMockWithStubValues(); | ||
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(false); | ||
|
||
$this->executeOriginalMethodExpectsNoError(); | ||
} | ||
|
||
/** | ||
* Test case when email is not provided | ||
*/ | ||
public function testExecuteWhenEmailParamIsNotPresent() | ||
{ | ||
$this->requestMock->expects($this->any()) | ||
->method('getParam') | ||
->with('email') | ||
->willReturn(null); | ||
$this->requestMock->expects($this->any()) | ||
->method('getParams') | ||
->willReturn(self::STUB_REQUEST_PARAMS); | ||
$this->captchaMock->expects($this->never())->method('isRequired'); | ||
$this->captchaMock->expects($this->never())->method('isCorrect'); | ||
|
||
$this->executeOriginalMethodExpectsNoError(); | ||
} | ||
|
||
/** | ||
* Stub params for Request Mock | ||
*/ | ||
private function configureRequestMockWithStubValues() | ||
{ | ||
$this->requestMock->expects($this->any()) | ||
->method('getParam') | ||
->with('email') | ||
->willReturn(self::STUB_EMAIL); | ||
$this->requestMock->expects($this->any()) | ||
->method('getParams') | ||
->willReturn(self::STUB_REQUEST_PARAMS); | ||
} | ||
|
||
/** | ||
* Run original method, expect there is no error | ||
*/ | ||
private function executeOriginalMethodExpectsNoError() | ||
{ | ||
$this->messageManagerMock->expects($this->never())->method('addErrorMessage'); | ||
$this->httpResponseMock->expects($this->never())->method('setRedirect'); | ||
|
||
$this->observer->execute($this->eventObserverMock); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.