48 $this->helperMock = $this->createMock(\
Magento\Captcha\Helper\Data::class);
49 $this->actionFlagMock = $this->createMock(\
Magento\Framework\
App\ActionFlag::class);
50 $this->messageManagerMock = $this->createMock(\
Magento\Framework\Message\ManagerInterface::class);
51 $this->customerSessionMock = $this->createPartialMock(
52 \
Magento\Customer\Model\Session::class,
53 [
'setUsername',
'getBeforeAuthUrl']
55 $this->captchaStringResolverMock = $this->createMock(\
Magento\Captcha\Observer\CaptchaStringResolver::class);
56 $this->customerUrlMock = $this->createMock(\
Magento\Customer\Model\Url::class);
57 $this->customerRepositoryMock = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
58 $this->authenticationMock = $this->createMock(AuthenticationInterface::class);
60 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
62 \
Magento\Captcha\Observer\CheckUserLoginObserver::class,
64 'helper' => $this->helperMock,
65 'actionFlag' => $this->actionFlagMock,
66 'messageManager' => $this->messageManagerMock,
67 'customerSession' => $this->customerSessionMock,
68 'captchaStringResolver' => $this->captchaStringResolverMock,
69 'customerUrl' => $this->customerUrlMock,
73 $reflection = new \ReflectionClass(get_class($this->observer));
74 $reflectionProperty = $reflection->getProperty(
'authentication');
75 $reflectionProperty->setAccessible(
true);
76 $reflectionProperty->setValue($this->observer, $this->authenticationMock);
78 $reflectionProperty2 = $reflection->getProperty(
'customerRepository');
79 $reflectionProperty2->setAccessible(
true);
80 $reflectionProperty2->setValue($this->observer, $this->customerRepositoryMock);
88 $formId =
'user_login';
90 $loginParams = [
'username' => $login];
92 $redirectUrl =
'http://magento.com/customer/account/login/';
93 $captchaValue =
'some-value';
95 $captcha = $this->createMock(\
Magento\Captcha\Model\DefaultModel::class);
97 ->method(
'isRequired')
101 ->method(
'isCorrect')
102 ->with($captchaValue)
105 ->method(
'logAttempt')
108 $this->helperMock->expects($this->once())
109 ->method(
'getCaptcha')
115 ->method(
'setRedirect')
116 ->with($redirectUrl);
122 ->willReturn($loginParams);
125 $controller->expects($this->any())->method(
'getRequest')->will($this->returnValue(
$request));
128 $this->captchaStringResolverMock->expects($this->once())
131 ->willReturn($captchaValue);
133 $customerDataMock = $this->createPartialMock(\
Magento\Customer\Model\Data\Customer::class, [
'getId']);
134 $customerDataMock->expects($this->once())
138 $this->customerRepositoryMock->expects($this->once())
141 ->willReturn($customerDataMock);
143 $this->authenticationMock->expects($this->once())
144 ->method(
'processAuthenticationFailure')
147 $this->messageManagerMock->expects($this->once())
149 ->with(
__(
'Incorrect CAPTCHA'));
151 $this->actionFlagMock->expects($this->once())
153 ->with(
'', \
Magento\Framework\
App\Action\Action::FLAG_NO_DISPATCH,
true);
155 $this->customerSessionMock->expects($this->once())
156 ->method(
'setUsername')
159 $this->customerSessionMock->expects($this->once())
160 ->method(
'getBeforeAuthUrl')
163 $this->customerUrlMock->expects($this->once())
164 ->method(
'getLoginUrl')
165 ->willReturn($redirectUrl);
167 $this->observer->execute(
new \
Magento\Framework\Event\Observer([
'controller_action' =>
$controller]));
$captchaStringResolverMock