diff --git a/app/code/Magento/Captcha/Observer/CheckUserEditObserver.php b/app/code/Magento/Captcha/Observer/CheckUserEditObserver.php index 9d3cd8d367093..a3921b6cb17aa 100644 --- a/app/code/Magento/Captcha/Observer/CheckUserEditObserver.php +++ b/app/code/Magento/Captcha/Observer/CheckUserEditObserver.php @@ -111,19 +111,21 @@ public function execute(\Magento\Framework\Event\Observer $observer) ) )) { $customerId = $this->customerSession->getCustomerId(); - $this->authentication->processAuthenticationFailure($customerId); - if ($this->authentication->isLocked($customerId)) { - $this->customerSession->logout(); - $this->customerSession->start(); - $message = __( - 'The account is locked. Please wait and try again or contact %1.', - $this->scopeConfig->getValue('contact/email/recipient_email') - ); - $this->messageManager->addError($message); + if ($customerId) { + $this->authentication->processAuthenticationFailure($customerId); + if ($this->authentication->isLocked($customerId)) { + $this->customerSession->logout(); + $this->customerSession->start(); + $message = __( + 'The account is locked. Please wait and try again or contact %1.', + $this->scopeConfig->getValue('contact/email/recipient_email') + ); + $this->messageManager->addError($message); + } + $this->messageManager->addError(__('Incorrect CAPTCHA')); + $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); + $this->redirect->redirect($controller->getResponse(), '*/*/edit'); } - $this->messageManager->addError(__('Incorrect CAPTCHA')); - $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); - $this->redirect->redirect($controller->getResponse(), '*/*/edit'); } } diff --git a/app/code/Magento/Captcha/Test/Unit/Observer/CheckUserEditObserverTest.php b/app/code/Magento/Captcha/Test/Unit/Observer/CheckUserEditObserverTest.php index 26fd8fd928c56..ee5001d146793 100644 --- a/app/code/Magento/Captcha/Test/Unit/Observer/CheckUserEditObserverTest.php +++ b/app/code/Magento/Captcha/Test/Unit/Observer/CheckUserEditObserverTest.php @@ -160,4 +160,53 @@ public function testExecute() $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller])); } + + /** + * @return void + */ + public function testExecuteWithCustomerIdNull() + { + $customerId = null; + $captchaValue = 'some-value'; + + $captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class); + $captcha->expects($this->once()) + ->method('isRequired') + ->willReturn(true); + $captcha->expects($this->once()) + ->method('isCorrect') + ->with($captchaValue) + ->willReturn(false); + + $this->helperMock->expects($this->once()) + ->method('getCaptcha') + ->with(\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID) + ->willReturn($captcha); + + $request = $this->createMock(\Magento\Framework\App\Request\Http::class); + $request->expects($this->any()) + ->method('getPost') + ->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null) + ->willReturn([\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID => $captchaValue]); + + $controller = $this->createMock(\Magento\Framework\App\Action\Action::class); + $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request)); + + $this->captchaStringResolverMock->expects($this->once()) + ->method('resolve') + ->with($request, \Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID) + ->willReturn($captchaValue); + + $customerDataMock = $this->createMock(\Magento\Customer\Model\Data\Customer::class); + + $this->customerSessionMock->expects($this->once()) + ->method('getCustomerId') + ->willReturn($customerId); + + $this->customerSessionMock->expects($this->atLeastOnce()) + ->method('getCustomer') + ->willReturn($customerDataMock); + + $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller])); + } }