Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckUserLoginObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class CheckUserLoginObserverTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $helperMock;
17 
19  protected $actionFlagMock;
20 
21  /* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
23 
26 
29 
31  protected $customerUrlMock;
32 
35 
38 
40  protected $observer;
41 
46  protected function setUp()
47  {
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']
54  );
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);
59 
60  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
61  $this->observer = $objectManager->getObject(
62  \Magento\Captcha\Observer\CheckUserLoginObserver::class,
63  [
64  'helper' => $this->helperMock,
65  'actionFlag' => $this->actionFlagMock,
66  'messageManager' => $this->messageManagerMock,
67  'customerSession' => $this->customerSessionMock,
68  'captchaStringResolver' => $this->captchaStringResolverMock,
69  'customerUrl' => $this->customerUrlMock,
70  ]
71  );
72 
73  $reflection = new \ReflectionClass(get_class($this->observer));
74  $reflectionProperty = $reflection->getProperty('authentication');
75  $reflectionProperty->setAccessible(true);
76  $reflectionProperty->setValue($this->observer, $this->authenticationMock);
77 
78  $reflectionProperty2 = $reflection->getProperty('customerRepository');
79  $reflectionProperty2->setAccessible(true);
80  $reflectionProperty2->setValue($this->observer, $this->customerRepositoryMock);
81  }
82 
86  public function testExecute()
87  {
88  $formId = 'user_login';
89  $login = 'login';
90  $loginParams = ['username' => $login];
91  $customerId = 7;
92  $redirectUrl = 'http://magento.com/customer/account/login/';
93  $captchaValue = 'some-value';
94 
95  $captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
96  $captcha->expects($this->once())
97  ->method('isRequired')
98  ->with($login)
99  ->willReturn(true);
100  $captcha->expects($this->once())
101  ->method('isCorrect')
102  ->with($captchaValue)
103  ->willReturn(false);
104  $captcha->expects($this->once())
105  ->method('logAttempt')
106  ->with($login);
107 
108  $this->helperMock->expects($this->once())
109  ->method('getCaptcha')
110  ->with($formId)
111  ->willReturn($captcha);
112 
113  $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
114  $response->expects($this->once())
115  ->method('setRedirect')
116  ->with($redirectUrl);
117 
118  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
119  $request->expects($this->any())
120  ->method('getPost')
121  ->with('login')
122  ->willReturn($loginParams);
123 
124  $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
125  $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
126  $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
127 
128  $this->captchaStringResolverMock->expects($this->once())
129  ->method('resolve')
130  ->with($request, $formId)
131  ->willReturn($captchaValue);
132 
133  $customerDataMock = $this->createPartialMock(\Magento\Customer\Model\Data\Customer::class, ['getId']);
134  $customerDataMock->expects($this->once())
135  ->method('getId')
136  ->willReturn($customerId);
137 
138  $this->customerRepositoryMock->expects($this->once())
139  ->method('get')
140  ->with($login)
141  ->willReturn($customerDataMock);
142 
143  $this->authenticationMock->expects($this->once())
144  ->method('processAuthenticationFailure')
145  ->with($customerId);
146 
147  $this->messageManagerMock->expects($this->once())
148  ->method('addError')
149  ->with(__('Incorrect CAPTCHA'));
150 
151  $this->actionFlagMock->expects($this->once())
152  ->method('set')
153  ->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
154 
155  $this->customerSessionMock->expects($this->once())
156  ->method('setUsername')
157  ->with($login);
158 
159  $this->customerSessionMock->expects($this->once())
160  ->method('getBeforeAuthUrl')
161  ->willReturn(false);
162 
163  $this->customerUrlMock->expects($this->once())
164  ->method('getLoginUrl')
165  ->willReturn($redirectUrl);
166 
167  $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
168  }
169 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$captcha
Definition: default.phtml:12
$controller
Definition: info.phtml:14