Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckUserEditObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class CheckUserEditObserverTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $helperMock;
17 
19  protected $actionFlagMock;
20 
21  /* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
23 
25  protected $redirectMock;
26 
29 
32 
35 
37  protected $scopeConfigMock;
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->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
52  $this->captchaStringResolverMock = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
53  $this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->customerSessionMock = $this->createPartialMock(
58  \Magento\Customer\Model\Session::class,
59  ['getCustomerId', 'getCustomer', 'logout', 'start']
60  );
61  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
62 
63  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
64  $this->observer = $objectManager->getObject(
65  \Magento\Captcha\Observer\CheckUserEditObserver::class,
66  [
67  'helper' => $this->helperMock,
68  'actionFlag' => $this->actionFlagMock,
69  'messageManager' => $this->messageManagerMock,
70  'redirect' => $this->redirectMock,
71  'captchaStringResolver' => $this->captchaStringResolverMock,
72  'authentication' => $this->authenticationMock,
73  'customerSession' => $this->customerSessionMock,
74  'scopeConfig' => $this->scopeConfigMock,
75  ]
76  );
77  }
78 
82  public function testExecute()
83  {
84  $customerId = 7;
85  $captchaValue = 'some-value';
87  $redirectUrl = 'http://magento.com/customer/account/edit/';
88 
89  $captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
90  $captcha->expects($this->once())
91  ->method('isRequired')
92  ->willReturn(true);
93  $captcha->expects($this->once())
94  ->method('isCorrect')
95  ->with($captchaValue)
96  ->willReturn(false);
97 
98  $this->helperMock->expects($this->once())
99  ->method('getCaptcha')
100  ->with(\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)
101  ->willReturn($captcha);
102 
103  $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
104  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
105  $request->expects($this->any())
106  ->method('getPost')
107  ->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
108  ->willReturn([\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID => $captchaValue]);
109 
110  $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
111  $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
112  $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
113 
114  $this->captchaStringResolverMock->expects($this->once())
115  ->method('resolve')
116  ->with($request, \Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)
117  ->willReturn($captchaValue);
118 
119  $customerDataMock = $this->createMock(\Magento\Customer\Model\Data\Customer::class);
120 
121  $this->customerSessionMock->expects($this->once())
122  ->method('getCustomerId')
123  ->willReturn($customerId);
124 
125  $this->customerSessionMock->expects($this->atLeastOnce())
126  ->method('getCustomer')
127  ->willReturn($customerDataMock);
128 
129  $this->authenticationMock->expects($this->once())
130  ->method('processAuthenticationFailure')
131  ->with($customerId);
132  $this->authenticationMock->expects($this->once())
133  ->method('isLocked')
134  ->with($customerId)
135  ->willReturn(true);
136 
137  $this->customerSessionMock->expects($this->once())
138  ->method('logout');
139  $this->customerSessionMock->expects($this->once())
140  ->method('start');
141 
142  $this->scopeConfigMock->expects($this->once())
143  ->method('getValue')
144  ->with('contact/email/recipient_email')
145  ->willReturn($email);
146 
147  $message = __('The account is locked. Please wait and try again or contact %1.', $email);
148  $this->messageManagerMock->expects($this->exactly(2))
149  ->method('addError')
150  ->withConsecutive([$message], [__('Incorrect CAPTCHA')]);
151 
152  $this->actionFlagMock->expects($this->once())
153  ->method('set')
154  ->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
155 
156  $this->redirectMock->expects($this->once())
157  ->method('redirect')
158  ->with($response, '*/*/edit')
159  ->willReturn($redirectUrl);
160 
161  $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
162  }
163 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
$email
Definition: details.phtml:13
__()
Definition: __.php:13
$message
$captcha
Definition: default.phtml:12
$controller
Definition: info.phtml:14