/** * Unlock customer on success login attempt. * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Customer\Model\Customer $customer */ $customer = $observer->getEvent()->getData('model'); $this->authentication->unlock($customer->getId()); return $this; }
/** * @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])); }
/** * @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 = 1; $observerMock = $this->getMock('Magento\\Framework\\Event\\Observer', [], [], '', false); $eventMock = $this->getMock('Magento\\Framework\\Event', ['getData'], [], '', false); $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock); $eventMock->expects($this->once())->method('getData')->with('model')->willReturn($this->customerModelMock); $this->customerModelMock->expects($this->once())->method('getId')->willReturn($customerId); $this->authenticationMock->expects($this->once())->method('unlock')->with($customerId); $this->customerLoginSuccessObserver->execute($observerMock); }
/** * @param int $testNumber * @param string $exceptionClass * @param string $errorMessage * * @dataProvider changeEmailExceptionDataProvider */ public function testChangeEmailException($testNumber, $exceptionClass, $errorMessage) { $customerId = 1; $password = '******'; $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->getMockForAbstractClass(); $currentCustomerMock = $this->getCurrentCustomerMock($customerId, $address); $newCustomerMock = $this->getNewCustomerMock($customerId, $address); $this->customerExtractor->expects($this->once())->method('extract')->with('customer_account_edit', $this->request)->willReturn($newCustomerMock); $this->validator->expects($this->once())->method('validate')->with($this->request)->willReturn(true); $this->request->expects($this->once())->method('isPost')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($currentCustomerMock); $this->request->expects($this->any())->method('getParam')->with('change_email')->willReturn(true); $this->request->expects($this->once())->method('getPost')->with('current_password')->willReturn($password); $exception = new $exceptionClass($errorMessage); $this->authenticationMock->expects($this->once())->method('authenticate')->willThrowException($exception); $this->messageManager->expects($this->once())->method('addError')->with($errorMessage)->willReturnSelf(); if ($testNumber == 1) { $this->resultRedirect->expects($this->once())->method('setPath')->with('*/*/edit')->willReturnSelf(); } if ($testNumber == 2) { $this->customerSession->expects($this->once())->method('logout'); $this->customerSession->expects($this->once())->method('start'); $this->resultRedirect->expects($this->once())->method('setPath')->with('customer/account/login')->willReturnSelf(); } $this->assertSame($this->resultRedirect, $this->model->execute()); }
/** * Unlock specified customer * * @return \Magento\Backend\Model\View\Result\Redirect */ public function execute() { $customerId = $this->getRequest()->getParam('customer_id'); try { // unlock customer if ($customerId) { $this->authentication->unlock($customerId); $this->getMessageManager()->addSuccess(__('Customer has been unlocked successfully.')); } } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setPath('customer/index/edit', ['id' => $customerId]); }
/** * 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; }
/** * @return void */ public function testExecuteWithException() { $customerId = 1; $phrase = new \Magento\Framework\Phrase('some error'); $this->requestMock->expects($this->once())->method('getParam')->with($this->equalTo('customer_id'))->will($this->returnValue($customerId)); $this->authenticationMock->expects($this->once())->method('unlock')->with($customerId)->willThrowException(new \Exception($phrase)); $this->messageManagerMock->expects($this->once())->method('addError'); $this->controller->execute(); }