/**
  * @return void
  */
 public function testExecute()
 {
     $formId = 'user_login';
     $login = '******';
     $loginParams = ['username' => $login];
     $customerId = 7;
     $redirectUrl = 'http://magento.com/customer/account/login/';
     $captchaValue = 'some-value';
     $captcha = $this->getMock('Magento\\Captcha\\Model\\DefaultModel', [], [], '', false);
     $captcha->expects($this->once())->method('isRequired')->with($login)->willReturn(true);
     $captcha->expects($this->once())->method('isCorrect')->with($captchaValue)->willReturn(false);
     $captcha->expects($this->once())->method('logAttempt')->with($login);
     $this->helperMock->expects($this->once())->method('getCaptcha')->with($formId)->willReturn($captcha);
     $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $response->expects($this->once())->method('setRedirect')->with($redirectUrl);
     $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $request->expects($this->any())->method('getPost')->with('login')->willReturn($loginParams);
     $controller = $this->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false);
     $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
     $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
     $this->captchaStringResolverMock->expects($this->once())->method('resolve')->with($request, $formId)->willReturn($captchaValue);
     $customerDataMock = $this->getMock('\\Magento\\Customer\\Model\\Data\\Customer', ['getId'], [], '', false);
     $customerDataMock->expects($this->once())->method('getId')->willReturn($customerId);
     $this->customerRepositoryMock->expects($this->once())->method('get')->with($login)->willReturn($customerDataMock);
     $this->authenticationMock->expects($this->once())->method('processAuthenticationFailure')->with($customerId);
     $this->messageManagerMock->expects($this->once())->method('addError')->with(__('Incorrect CAPTCHA'));
     $this->actionFlagMock->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
     $this->customerSessionMock->expects($this->once())->method('setUsername')->with($login);
     $this->customerSessionMock->expects($this->once())->method('getBeforeAuthUrl')->willReturn(false);
     $this->customerUrlMock->expects($this->once())->method('getLoginUrl')->willReturn($redirectUrl);
     $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
 }
 /**
  * @return void
  */
 public function testExecute()
 {
     $customerId = 7;
     $captchaValue = 'some-value';
     $email = '*****@*****.**';
     $redirectUrl = 'http://magento.com/customer/account/edit/';
     $captcha = $this->getMock('Magento\\Captcha\\Model\\DefaultModel', [], [], '', false);
     $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);
     $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $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->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false);
     $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
     $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
     $this->captchaStringResolverMock->expects($this->once())->method('resolve')->with($request, \Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)->willReturn($captchaValue);
     $customerDataMock = $this->getMock('\\Magento\\Customer\\Model\\Data\\Customer', [], [], '', false);
     $this->customerSessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
     $this->customerSessionMock->expects($this->atLeastOnce())->method('getCustomer')->willReturn($customerDataMock);
     $this->authenticationMock->expects($this->once())->method('processAuthenticationFailure')->with($customerId);
     $this->authenticationMock->expects($this->once())->method('isLocked')->with($customerId)->willReturn(true);
     $this->customerSessionMock->expects($this->once())->method('logout');
     $this->customerSessionMock->expects($this->once())->method('start');
     $this->scopeConfigMock->expects($this->once())->method('getValue')->with('contact/email/recipient_email')->willReturn($email);
     $message = __('The account is locked. Please wait and try again or contact %1.', $email);
     $this->messageManagerMock->expects($this->exactly(2))->method('addError')->withConsecutive([$message], [__('Incorrect CAPTCHA')]);
     $this->actionFlagMock->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
     $this->redirectMock->expects($this->once())->method('redirect')->with($response, '*/*/edit')->willReturn($redirectUrl);
     $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
 }
 /**
  * Check Captcha On User Login Backend Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @throws \Magento\Framework\Exception\Plugin\AuthenticationException
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'backend_login';
     $captchaModel = $this->_helper->getCaptcha($formId);
     $login = $observer->getEvent()->getUsername();
     if ($captchaModel->isRequired($login)) {
         if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($this->_request, $formId))) {
             $captchaModel->logAttempt($login);
             throw new PluginAuthenticationException(__('Incorrect CAPTCHA.'));
         }
     }
     $captchaModel->logAttempt($login);
     return $this;
 }
 /**
  * Check CAPTCHA on Contact Us page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'contact_us';
     $captcha = $this->_helper->getCaptcha($formId);
     if ($captcha->isRequired()) {
         /** @var \Magento\Framework\App\Action\Action $controller */
         $controller = $observer->getControllerAction();
         if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
             $this->messageManager->addError(__('Incorrect CAPTCHA.'));
             $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->redirect->redirect($controller->getResponse(), 'contact/index/index');
         }
     }
 }
 /**
  * Check captcha on user login page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @throws NoSuchEntityException
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'user_login';
     $captchaModel = $this->_helper->getCaptcha($formId);
     $controller = $observer->getControllerAction();
     $loginParams = $controller->getRequest()->getPost('login');
     $login = is_array($loginParams) && array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
     if ($captchaModel->isRequired($login)) {
         $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId);
         if (!$captchaModel->isCorrect($word)) {
             try {
                 $customer = $this->getCustomerRepository()->get($login);
                 $this->getAuthentication()->processAuthenticationFailure($customer->getId());
             } catch (NoSuchEntityException $e) {
                 //do nothing as customer existance is validated later in authenticate method
             }
             $this->messageManager->addError(__('Incorrect CAPTCHA'));
             $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->_session->setUsername($login);
             $beforeUrl = $this->_session->getBeforeAuthUrl();
             $url = $beforeUrl ? $beforeUrl : $this->_customerUrl->getLoginUrl();
             $controller->getResponse()->setRedirect($url);
         }
     }
     $captchaModel->logAttempt($login);
     return $this;
 }
 public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCaptchaIsRequiredAndInvalid()
 {
     $formId = 'contact_us';
     $captchaValue = 'some-value';
     $warningMessage = 'Incorrect CAPTCHA.';
     $redirectRoutePath = 'contact/index/index';
     $redirectUrl = 'http://magento.com/contacts/';
     $postData = ['name' => 'Some Name'];
     $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $request->expects($this->any())->method('getPost')->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)->willReturn([$formId => $captchaValue]);
     $request->expects($this->once())->method('getPostValue')->willReturn($postData);
     $this->redirectMock->expects($this->once())->method('redirect')->with($response, $redirectRoutePath, [])->willReturn($redirectUrl);
     $controller = $this->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false);
     $controller->expects($this->any())->method('getRequest')->willReturn($request);
     $controller->expects($this->any())->method('getResponse')->willReturn($response);
     $this->captchaMock->expects($this->any())->method('isRequired')->willReturn(true);
     $this->captchaMock->expects($this->once())->method('isCorrect')->with($captchaValue)->willReturn(false);
     $this->captchaStringResolverMock->expects($this->once())->method('resolve')->with($request, $formId)->willReturn($captchaValue);
     $this->helperMock->expects($this->any())->method('getCaptcha')->with($formId)->willReturn($this->captchaMock);
     $this->messageManagerMock->expects($this->once())->method('addError')->with($warningMessage);
     $this->actionFlagMock->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
     $this->dataPersistorMock->expects($this->once())->method('set')->with($formId, $postData);
     $this->checkContactUsFormObserver->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
 }
 /**
  * Check Captcha On Forgot Password Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $captchaModel = $this->helper->getCaptcha(self::FORM_ID);
     if ($captchaModel->isRequired()) {
         /** @var \Magento\Framework\App\Action\Action $controller */
         $controller = $observer->getControllerAction();
         if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), self::FORM_ID))) {
             try {
                 $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
                 $this->accountManagementHelper->processCustomerLockoutData($customer->getId());
                 $this->customerRepository->save($customer);
             } catch (NoSuchEntityException $e) {
                 //do nothing as customer existance is validated later in authenticate method
             }
             $this->workWithLock();
             $this->messageManager->addError(__('Incorrect CAPTCHA'));
             $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->redirect->redirect($controller->getResponse(), '*/*/edit');
         }
     }
     $customer = $this->customerSession->getCustomer();
     $login = $customer->getEmail();
     $captchaModel->logAttempt($login);
     return $this;
 }
 /**
  * Check Captcha On Forgot Password Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $captchaModel = $this->helper->getCaptcha(self::FORM_ID);
     if ($captchaModel->isRequired()) {
         /** @var \Magento\Framework\App\Action\Action $controller */
         $controller = $observer->getControllerAction();
         if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), self::FORM_ID))) {
             $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);
             }
             $this->messageManager->addError(__('Incorrect CAPTCHA'));
             $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->redirect->redirect($controller->getResponse(), '*/*/edit');
         }
     }
     $customer = $this->customerSession->getCustomer();
     $login = $customer->getEmail();
     $captchaModel->logAttempt($login);
     return $this;
 }
 /**
  * Check Captcha On Checkout as Guest Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'guest_checkout';
     $captchaModel = $this->_helper->getCaptcha($formId);
     $checkoutMethod = $this->_typeOnepage->getQuote()->getCheckoutMethod();
     if ($checkoutMethod == \Magento\Checkout\Model\Type\Onepage::METHOD_GUEST) {
         if ($captchaModel->isRequired()) {
             $controller = $observer->getControllerAction();
             if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
                 $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
                 $result = ['error' => 1, 'message' => __('Incorrect CAPTCHA')];
                 $controller->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
             }
         }
     }
     return $this;
 }
 /**
  * Check Captcha On User Login Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'user_create';
     $captchaModel = $this->_helper->getCaptcha($formId);
     if ($captchaModel->isRequired()) {
         /** @var \Magento\Framework\App\Action\Action $controller */
         $controller = $observer->getControllerAction();
         if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
             $this->messageManager->addError(__('Incorrect CAPTCHA'));
             $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->_session->setCustomerFormData($controller->getRequest()->getPostValue());
             $url = $this->_urlManager->getUrl('*/*/create', ['_nosecret' => true]);
             $controller->getResponse()->setRedirect($this->redirect->error($url));
         }
     }
     return $this;
 }
 /**
  * Check Captcha On User Login Backend Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @throws \Magento\Framework\Exception\Plugin\AuthenticationException
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'backend_forgotpassword';
     $captchaModel = $this->_helper->getCaptcha($formId);
     $controller = $observer->getControllerAction();
     $email = (string) $observer->getControllerAction()->getRequest()->getParam('email');
     $params = $observer->getControllerAction()->getRequest()->getParams();
     if (!empty($email) && !empty($params)) {
         if ($captchaModel->isRequired()) {
             if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
                 $this->_session->setEmail((string) $controller->getRequest()->getPost('email'));
                 $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
                 $this->messageManager->addError(__('Incorrect CAPTCHA'));
                 $controller->getResponse()->setRedirect($controller->getUrl('*/*/forgotpassword', ['_nosecret' => true]));
             }
         }
     }
     return $this;
 }
 /**
  * Check Captcha On User Login Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $formId = 'user_login';
     $captchaModel = $this->_helper->getCaptcha($formId);
     $controller = $observer->getControllerAction();
     $loginParams = $controller->getRequest()->getPost('login');
     $login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
     if ($captchaModel->isRequired($login)) {
         $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId);
         if (!$captchaModel->isCorrect($word)) {
             $this->messageManager->addError(__('Incorrect CAPTCHA'));
             $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->_session->setUsername($login);
             $beforeUrl = $this->_session->getBeforeAuthUrl();
             $url = $beforeUrl ? $beforeUrl : $this->_customerUrl->getLoginUrl();
             $controller->getResponse()->setRedirect($url);
         }
     }
     $captchaModel->logAttempt($login);
     return $this;
 }
 public function testCheckForgotpasswordRedirects()
 {
     $formId = 'user_forgotpassword';
     $captchaValue = 'some-value';
     $warningMessage = 'Incorrect CAPTCHA';
     $redirectRoutePath = '*/*/forgotpassword';
     $redirectUrl = 'http://magento.com/customer/account/forgotpassword/';
     $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $request->expects($this->any())->method('getPost')->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)->will($this->returnValue([$formId => $captchaValue]));
     $this->redirect->expects($this->once())->method('redirect')->with($response, $redirectRoutePath, [])->will($this->returnValue($redirectUrl));
     $controller = $this->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false);
     $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
     $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
     $this->_captcha->expects($this->any())->method('isRequired')->will($this->returnValue(true));
     $this->_captcha->expects($this->once())->method('isCorrect')->with($captchaValue)->will($this->returnValue(false));
     $this->captchaStringResolver->expects($this->once())->method('resolve')->with($request, $formId)->will($this->returnValue($captchaValue));
     $this->_helper->expects($this->any())->method('getCaptcha')->with($formId)->will($this->returnValue($this->_captcha));
     $this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
     $this->_actionFlag->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
     $this->checkForgotpasswordObserver->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
 }
 public function testCheckUserCreateRedirectsError()
 {
     $formId = 'user_create';
     $captchaValue = 'some-value';
     $warningMessage = 'Incorrect CAPTCHA';
     $redirectRoutePath = '*/*/create';
     $redirectUrl = 'http://magento.com/customer/account/create/';
     $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $this->redirect->expects($this->once())->method('error')->with($redirectUrl)->will($this->returnValue($redirectUrl));
     $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $response->expects($this->once())->method('setRedirect')->with($redirectUrl);
     $this->_urlManager->expects($this->once())->method('getUrl')->with($redirectRoutePath, ['_nosecret' => true])->will($this->returnValue($redirectUrl));
     $controller = $this->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false);
     $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
     $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
     $this->_captcha->expects($this->any())->method('isRequired')->will($this->returnValue(true));
     $this->_captcha->expects($this->once())->method('isCorrect')->with($captchaValue)->will($this->returnValue(false));
     $this->captchaStringResolver->expects($this->once())->method('resolve')->with($request, $formId)->will($this->returnValue($captchaValue));
     $this->_helper->expects($this->any())->method('getCaptcha')->with($formId)->will($this->returnValue($this->_captcha));
     $this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
     $this->_actionFlag->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
     $this->checkUserCreateObserver->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
 }