Skip to content

Commit 5632952

Browse files
author
Stanislav Idolov
authored
ENGCOM-657: #7515: Error when submit customer/account/editPost form and session expired #1187
2 parents c8dc954 + 036be07 commit 5632952

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

app/code/Magento/Captcha/Observer/CheckUserEditObserver.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,21 @@ public function execute(\Magento\Framework\Event\Observer $observer)
111111
)
112112
)) {
113113
$customerId = $this->customerSession->getCustomerId();
114-
$this->authentication->processAuthenticationFailure($customerId);
115-
if ($this->authentication->isLocked($customerId)) {
116-
$this->customerSession->logout();
117-
$this->customerSession->start();
118-
$message = __(
119-
'The account is locked. Please wait and try again or contact %1.',
120-
$this->scopeConfig->getValue('contact/email/recipient_email')
121-
);
122-
$this->messageManager->addError($message);
114+
if ($customerId) {
115+
$this->authentication->processAuthenticationFailure($customerId);
116+
if ($this->authentication->isLocked($customerId)) {
117+
$this->customerSession->logout();
118+
$this->customerSession->start();
119+
$message = __(
120+
'The account is locked. Please wait and try again or contact %1.',
121+
$this->scopeConfig->getValue('contact/email/recipient_email')
122+
);
123+
$this->messageManager->addError($message);
124+
}
125+
$this->messageManager->addError(__('Incorrect CAPTCHA'));
126+
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
127+
$this->redirect->redirect($controller->getResponse(), '*/*/edit');
123128
}
124-
$this->messageManager->addError(__('Incorrect CAPTCHA'));
125-
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
126-
$this->redirect->redirect($controller->getResponse(), '*/*/edit');
127129
}
128130
}
129131

app/code/Magento/Captcha/Test/Unit/Observer/CheckUserEditObserverTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,53 @@ public function testExecute()
160160

161161
$this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
162162
}
163+
164+
/**
165+
* @return void
166+
*/
167+
public function testExecuteWithCustomerIdNull()
168+
{
169+
$customerId = null;
170+
$captchaValue = 'some-value';
171+
172+
$captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
173+
$captcha->expects($this->once())
174+
->method('isRequired')
175+
->willReturn(true);
176+
$captcha->expects($this->once())
177+
->method('isCorrect')
178+
->with($captchaValue)
179+
->willReturn(false);
180+
181+
$this->helperMock->expects($this->once())
182+
->method('getCaptcha')
183+
->with(\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)
184+
->willReturn($captcha);
185+
186+
$request = $this->createMock(\Magento\Framework\App\Request\Http::class);
187+
$request->expects($this->any())
188+
->method('getPost')
189+
->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
190+
->willReturn([\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID => $captchaValue]);
191+
192+
$controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
193+
$controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
194+
195+
$this->captchaStringResolverMock->expects($this->once())
196+
->method('resolve')
197+
->with($request, \Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)
198+
->willReturn($captchaValue);
199+
200+
$customerDataMock = $this->createMock(\Magento\Customer\Model\Data\Customer::class);
201+
202+
$this->customerSessionMock->expects($this->once())
203+
->method('getCustomerId')
204+
->willReturn($customerId);
205+
206+
$this->customerSessionMock->expects($this->atLeastOnce())
207+
->method('getCustomer')
208+
->willReturn($customerDataMock);
209+
210+
$this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
211+
}
163212
}

0 commit comments

Comments
 (0)