Skip to content

Commit 5dd29c8

Browse files
authored
ENGCOM-5753: magento/magento2#: Captcha. Improvement. Replace deprecated addError with addErrorMessage. #24340
2 parents 4808977 + 02de316 commit 5dd29c8

16 files changed

+132
-54
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Framework\App\Request\DataPersistorInterface;
1010
use Magento\Framework\App\ObjectManager;
1111

12+
/**
13+
* Class CheckContactUsFormObserver
14+
*/
1215
class CheckContactUsFormObserver implements ObserverInterface
1316
{
1417
/**
@@ -76,7 +79,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7679
/** @var \Magento\Framework\App\Action\Action $controller */
7780
$controller = $observer->getControllerAction();
7881
if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
79-
$this->messageManager->addError(__('Incorrect CAPTCHA.'));
82+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA.'));
8083
$this->getDataPersistor()->set($formId, $controller->getRequest()->getPostValue());
8184
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
8285
$this->redirect->redirect($controller->getResponse(), 'contact/index/index');

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
use Magento\Framework\Event\ObserverInterface;
99

10+
/**
11+
* Class CheckForgotpasswordObserver
12+
*/
1013
class CheckForgotpasswordObserver implements ObserverInterface
1114
{
1215
/**
@@ -69,7 +72,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6972
/** @var \Magento\Framework\App\Action\Action $controller */
7073
$controller = $observer->getControllerAction();
7174
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
72-
$this->messageManager->addError(__('Incorrect CAPTCHA'));
75+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
7376
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
7477
$this->redirect->redirect($controller->getResponse(), '*/*/forgotpassword');
7578
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
use Magento\Framework\Event\ObserverInterface;
99

10+
/**
11+
* Class CheckUserCreateObserver
12+
*
13+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
14+
*/
1015
class CheckUserCreateObserver implements ObserverInterface
1116
{
1217
/**
@@ -86,7 +91,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8691
/** @var \Magento\Framework\App\Action\Action $controller */
8792
$controller = $observer->getControllerAction();
8893
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
89-
$this->messageManager->addError(__('Incorrect CAPTCHA'));
94+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
9095
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
9196
$this->_session->setCustomerFormData($controller->getRequest()->getPostValue());
9297
$url = $this->_urlManager->getUrl('*/*/create', ['_nosecret' => true]);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212

1313
/**
14-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
* Class CheckUserEditObserver
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1517
*/
1618
class CheckUserEditObserver implements ObserverInterface
1719
{
18-
/**
19-
* Form ID
20-
*/
2120
const FORM_ID = 'user_edit';
2221

2322
/**
@@ -96,7 +95,8 @@ public function __construct(
9695
* Check Captcha On Forgot Password Page
9796
*
9897
* @param \Magento\Framework\Event\Observer $observer
99-
* @return $this
98+
* @return $this|void
99+
* @throws \Magento\Framework\Exception\SessionException
100100
*/
101101
public function execute(\Magento\Framework\Event\Observer $observer)
102102
{
@@ -119,9 +119,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
119119
'The account is locked. Please wait and try again or contact %1.',
120120
$this->scopeConfig->getValue('contact/email/recipient_email')
121121
);
122-
$this->messageManager->addError($message);
122+
$this->messageManager->addErrorMessage($message);
123123
}
124-
$this->messageManager->addError(__('Incorrect CAPTCHA'));
124+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
125125
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
126126
$this->redirect->redirect($controller->getResponse(), '*/*/edit');
127127
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
use Magento\Framework\Event\ObserverInterface;
99

10+
/**
11+
* Class CheckUserForgotPasswordBackendObserver
12+
*
13+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
14+
*/
1015
class CheckUserForgotPasswordBackendObserver implements ObserverInterface
1116
{
1217
/**
@@ -76,7 +81,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7681
) {
7782
$this->_session->setEmail((string)$controller->getRequest()->getPost('email'));
7883
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
79-
$this->messageManager->addError(__('Incorrect CAPTCHA'));
84+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
8085
$controller->getResponse()->setRedirect(
8186
$controller->getUrl('*/*/forgotpassword', ['_nosecret' => true])
8287
);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
namespace Magento\Captcha\Observer;
88

9+
use Magento\Customer\Api\CustomerRepositoryInterface;
910
use Magento\Customer\Model\AuthenticationInterface;
1011
use Magento\Framework\Event\ObserverInterface;
1112
use Magento\Framework\Exception\NoSuchEntityException;
12-
use Magento\Customer\Api\CustomerRepositoryInterface;
1313

1414
/**
1515
* Check captcha on user login page observer.
@@ -64,6 +64,8 @@ class CheckUserLoginObserver implements ObserverInterface
6464
protected $authentication;
6565

6666
/**
67+
* CheckUserLoginObserver constructor.
68+
*
6769
* @param \Magento\Captcha\Helper\Data $helper
6870
* @param \Magento\Framework\App\ActionFlag $actionFlag
6971
* @param \Magento\Framework\Message\ManagerInterface $messageManager
@@ -125,8 +127,7 @@ private function getAuthentication()
125127
* Check captcha on user login page
126128
*
127129
* @param \Magento\Framework\Event\Observer $observer
128-
* @throws NoSuchEntityException
129-
* @return $this
130+
* @return $this|void
130131
*/
131132
public function execute(\Magento\Framework\Event\Observer $observer)
132133
{
@@ -143,10 +144,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
143144
try {
144145
$customer = $this->getCustomerRepository()->get($login);
145146
$this->getAuthentication()->processAuthenticationFailure($customer->getId());
147+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
146148
} catch (NoSuchEntityException $e) {
147149
//do nothing as customer existence is validated later in authenticate method
148150
}
149-
$this->messageManager->addError(__('Incorrect CAPTCHA'));
151+
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
150152
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
151153
$this->_session->setUsername($login);
152154
$beforeUrl = $this->_session->getBeforeAuthUrl();

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ protected function setUp()
6969
$this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
7070
$this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
7171
$this->captchaStringResolverMock = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
72-
$this->sessionMock = $this->createPartialMock(\Magento\Framework\Session\SessionManager::class, ['addError']);
72+
$this->sessionMock = $this->createPartialMock(
73+
\Magento\Framework\Session\SessionManager::class,
74+
['addErrorMessage']
75+
);
7376
$this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
7477
->getMockForAbstractClass();
7578

@@ -116,7 +119,7 @@ public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid()
116119
$this->helperMock->expects($this->any())
117120
->method('getCaptcha')
118121
->with($formId)->willReturn($this->captchaMock);
119-
$this->sessionMock->expects($this->never())->method('addError');
122+
$this->sessionMock->expects($this->never())->method('addErrorMessage');
120123

121124
$this->checkContactUsFormObserver->execute(
122125
new \Magento\Framework\Event\Observer(['controller_action' => $controller])
@@ -163,7 +166,7 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
163166
->method('getCaptcha')
164167
->with($formId)
165168
->willReturn($this->captchaMock);
166-
$this->messageManagerMock->expects($this->once())->method('addError')->with($warningMessage);
169+
$this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with($warningMessage);
167170
$this->actionFlagMock->expects($this->once())
168171
->method('set')
169172
->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function testCheckForgotpasswordRedirects()
138138
)->will(
139139
$this->returnValue($this->_captcha)
140140
);
141-
$this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
141+
$this->_messageManager->expects($this->once())->method('addErrorMessage')->with($warningMessage);
142142
$this->_actionFlag->expects(
143143
$this->once()
144144
)->method(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function testCheckUserCreateRedirectsError()
151151
)->will(
152152
$this->returnValue($this->_captcha)
153153
);
154-
$this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
154+
$this->_messageManager->expects($this->once())->method('addErrorMessage')->with($warningMessage);
155155
$this->_actionFlag->expects(
156156
$this->once()
157157
)->method(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function testExecute()
146146

147147
$message = __('The account is locked. Please wait and try again or contact %1.', $email);
148148
$this->messageManagerMock->expects($this->exactly(2))
149-
->method('addError')
149+
->method('addErrorMessage')
150150
->withConsecutive([$message], [__('Incorrect CAPTCHA')]);
151151

152152
$this->actionFlagMock->expects($this->once())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testExecute()
145145
->with($customerId);
146146

147147
$this->messageManagerMock->expects($this->once())
148-
->method('addError')
148+
->method('addErrorMessage')
149149
->with(__('Incorrect CAPTCHA'));
150150

151151
$this->actionFlagMock->expects($this->once())

app/code/Magento/Customer/Controller/Account/Confirmation.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Customer\Controller\Account;
87

8+
use Magento\Customer\Api\AccountManagementInterface;
9+
use Magento\Customer\Controller\AbstractAccount;
10+
use Magento\Customer\Model\Session;
911
use Magento\Customer\Model\Url;
12+
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
13+
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1014
use Magento\Framework\App\Action\Context;
11-
use Magento\Customer\Model\Session;
1215
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Exception\State\InvalidTransitionException;
1317
use Magento\Framework\View\Result\PageFactory;
1418
use Magento\Store\Model\StoreManagerInterface;
15-
use Magento\Customer\Api\AccountManagementInterface;
16-
use Magento\Framework\Exception\State\InvalidTransitionException;
1719

18-
class Confirmation extends \Magento\Customer\Controller\AbstractAccount
20+
/**
21+
* Class Confirmation. Send confirmation link to specified email
22+
*/
23+
class Confirmation extends AbstractAccount implements HttpGetActionInterface, HttpPostActionInterface
1924
{
2025
/**
2126
* @var \Magento\Store\Model\StoreManagerInterface
@@ -91,11 +96,11 @@ public function execute()
9196
$email,
9297
$this->storeManager->getStore()->getWebsiteId()
9398
);
94-
$this->messageManager->addSuccess(__('Please check your email for confirmation key.'));
99+
$this->messageManager->addSuccessMessage(__('Please check your email for confirmation key.'));
95100
} catch (InvalidTransitionException $e) {
96-
$this->messageManager->addSuccess(__('This email does not require confirmation.'));
101+
$this->messageManager->addSuccessMessage(__('This email does not require confirmation.'));
97102
} catch (\Exception $e) {
98-
$this->messageManager->addException($e, __('Wrong email.'));
103+
$this->messageManager->addExceptionMessage($e, __('Wrong email.'));
99104
$resultRedirect->setPath('*/*/*', ['email' => $email, '_secure' => true]);
100105
return $resultRedirect;
101106
}

app/code/Magento/Customer/Model/Customer.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ public function getSharingConfig()
394394
public function authenticate($login, $password)
395395
{
396396
$this->loadByEmail($login);
397-
if ($this->getConfirmation() && $this->isConfirmationRequired()) {
397+
if ($this->getConfirmation() &&
398+
$this->accountConfirmation->isConfirmationRequired($this->getWebsiteId(), $this->getId(), $this->getEmail())
399+
) {
398400
throw new EmailNotConfirmedException(
399401
__("This account isn't confirmed. Verify and try again.")
400402
);
@@ -415,8 +417,9 @@ public function authenticate($login, $password)
415417
/**
416418
* Load customer by email
417419
*
418-
* @param string $customerEmail
419-
* @return $this
420+
* @param string $customerEmail
421+
* @return $this
422+
* @throws \Magento\Framework\Exception\LocalizedException
420423
*/
421424
public function loadByEmail($customerEmail)
422425
{
@@ -427,8 +430,9 @@ public function loadByEmail($customerEmail)
427430
/**
428431
* Change customer password
429432
*
430-
* @param string $newPassword
431-
* @return $this
433+
* @param string $newPassword
434+
* @return $this
435+
* @throws \Magento\Framework\Exception\LocalizedException
432436
*/
433437
public function changePassword($newPassword)
434438
{
@@ -440,6 +444,7 @@ public function changePassword($newPassword)
440444
* Get full customer name
441445
*
442446
* @return string
447+
* @throws \Magento\Framework\Exception\LocalizedException
443448
*/
444449
public function getName()
445450
{
@@ -462,8 +467,9 @@ public function getName()
462467
/**
463468
* Add address to address collection
464469
*
465-
* @param Address $address
466-
* @return $this
470+
* @param Address $address
471+
* @return $this
472+
* @throws \Magento\Framework\Exception\LocalizedException
467473
*/
468474
public function addAddress(Address $address)
469475
{
@@ -487,6 +493,7 @@ public function getAddressById($addressId)
487493
*
488494
* @param int $addressId
489495
* @return Address
496+
* @throws \Magento\Framework\Exception\LocalizedException
490497
*/
491498
public function getAddressItemById($addressId)
492499
{
@@ -506,7 +513,8 @@ public function getAddressCollection()
506513
/**
507514
* Customer addresses collection
508515
*
509-
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
516+
* @return ResourceModel\Address\Collection
517+
* @throws \Magento\Framework\Exception\LocalizedException
510518
*/
511519
public function getAddressesCollection()
512520
{
@@ -538,6 +546,7 @@ public function getAddresses()
538546
* Retrieve all customer attributes
539547
*
540548
* @return Attribute[]
549+
* @throws \Magento\Framework\Exception\LocalizedException
541550
*/
542551
public function getAttributes()
543552
{
@@ -591,7 +600,8 @@ public function hashPassword($password, $salt = true)
591600
* Validate password with salted hash
592601
*
593602
* @param string $password
594-
* @return boolean
603+
* @return bool
604+
* @throws \Exception
595605
*/
596606
public function validatePassword($password)
597607
{
@@ -805,6 +815,7 @@ public function isConfirmationRequired()
805815
*/
806816
public function getRandomConfirmationKey()
807817
{
818+
// phpcs:ignore Magento2.Security.InsecureFunction
808819
return md5(uniqid());
809820
}
810821

0 commit comments

Comments
 (0)