/** * @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])); }
public function testCheckContactUsFormDoesNotCheckCaptchaWhenItIsNotRequired() { $this->helperMock->expects($this->any())->method('getCaptcha')->with('contact_us')->willReturn($this->captchaMock); $this->captchaMock->expects($this->any())->method('isRequired')->willReturn(false); $this->captchaMock->expects($this->never())->method('isCorrect'); $this->checkContactUsFormObserver->execute(new \Magento\Framework\Event\Observer()); }
/** * @dataProvider getExpiredImages */ public function testDeleteExpiredImages($website, $isFile, $filename, $mTime, $timeout, $mustDelete) { $this->_storeManager->expects($this->once())->method('getWebsites')->will($this->returnValue(isset($website) ? [$website] : [])); if (isset($website)) { $this->_helper->expects($this->once())->method('getConfig')->with($this->equalTo('timeout'), new \PHPUnit_Framework_Constraint_IsIdentical($website->getDefaultStore()))->will($this->returnValue($timeout)); } else { $this->_helper->expects($this->never())->method('getConfig'); } $this->_adminHelper->expects($this->once())->method('getConfig')->with($this->equalTo('timeout'), new \PHPUnit_Framework_Constraint_IsNull())->will($this->returnValue($timeout)); $timesToCall = isset($website) ? 2 : 1; $this->_directory->expects($this->exactly($timesToCall))->method('read')->will($this->returnValue([$filename])); $this->_directory->expects($this->exactly($timesToCall))->method('isFile')->will($this->returnValue($isFile)); $this->_directory->expects($this->any())->method('stat')->will($this->returnValue(['mtime' => $mTime])); if ($mustDelete) { $this->_directory->expects($this->exactly($timesToCall))->method('delete')->with($filename); } else { $this->_directory->expects($this->never())->method('delete'); } $this->_model->deleteExpiredImages(); }