Skip to content

magento/magento2#: Replace deprecated addError, addSuccess, addException methods in Magento/Customer/Controller/Account/CreatePost.php #24197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 58 additions & 20 deletions app/code/Magento/Customer/Controller/Account/CreatePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Phrase;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\AccountManagementInterface;
Expand Down Expand Up @@ -118,6 +119,11 @@ class CreatePost extends AbstractAccount implements CsrfAwareActionInterface, Ht
*/
protected $session;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var AccountRedirect
*/
Expand Down Expand Up @@ -365,20 +371,19 @@ public function execute()
);
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId());
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
$email = $this->customerUrl->getEmailConfirmationUrl($customer->getEmail());
// @codingStandardsIgnoreStart
$this->messageManager->addSuccess(
__(
'You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.',
$email
)
$this->messageManager->addComplexSuccessMessage(
'confirmAccountSuccessMessage',
[
'url' => $this->customerUrl->getEmailConfirmationUrl($customer->getEmail()),
]
);
// @codingStandardsIgnoreEnd
$url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->success($url));
} else {
$this->session->setCustomerDataAsLoggedIn($customer);
$this->messageManager->addSuccess($this->getSuccessMessage());

$this->messageManager->addMessage($this->getMessageManagerSuccessMessage());

$requestedRedirect = $this->accountRedirect->getRedirectCookie();
if (!$this->scopeConfig->getValue('customer/startup/redirect_dashboard') && $requestedRedirect) {
$resultRedirect->setUrl($this->_redirect->success($requestedRedirect));
Expand All @@ -395,23 +400,21 @@ public function execute()

return $resultRedirect;
} catch (StateException $e) {
$url = $this->urlModel->getUrl('customer/account/forgotpassword');
// @codingStandardsIgnoreStart
$message = __(
'There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.',
$url
$this->messageManager->addComplexErrorMessage(
'customerAlreadyExistsErrorMessage',
[
'url' => $this->urlModel->getUrl('customer/account/forgotpassword'),
]
);
// @codingStandardsIgnoreEnd
$this->messageManager->addError($message);
} catch (InputException $e) {
$this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
$this->messageManager->addErrorMessage($e->getMessage());
foreach ($e->getErrors() as $error) {
$this->messageManager->addError($this->escaper->escapeHtml($error->getMessage()));
$this->messageManager->addErrorMessage($error->getMessage());
}
} catch (LocalizedException $e) {
$this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer.'));
$this->messageManager->addExceptionMessage($e, __('We can\'t save the customer.'));
}

$this->session->setCustomerFormData($this->getRequest()->getPostValue());
Expand All @@ -437,6 +440,8 @@ protected function checkPasswordConfirmation($password, $confirmation)
/**
* Retrieve success message
*
* @deprecated
* @see getMessageManagerSuccessMessage()
* @return string
*/
protected function getSuccessMessage()
Expand All @@ -462,4 +467,37 @@ protected function getSuccessMessage()
}
return $message;
}

/**
* Retrieve success message manager message
*
* @return MessageInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getMessageManagerSuccessMessage(): MessageInterface
{
if ($this->addressHelper->isVatValidationEnabled()) {
if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) {
$identifier = 'customerVatShippingAddressSuccessMessage';
} else {
$identifier = 'customerVatBillingAddressSuccessMessage';
}

$message = $this->messageManager
->createMessage(MessageInterface::TYPE_SUCCESS, $identifier)
->setData(
[
'url' => $this->urlModel->getUrl('customer/address/edit'),
]
);
} else {
$message = $this->messageManager
->createMessage(MessageInterface::TYPE_SUCCESS)
->setText(
__('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName())
);
}

return $message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,13 @@ public function testSuccessMessage(

$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
['password', null, $password],
['password_confirmation', null, $password],
['is_subscribed', false, true],
]);
->willReturnMap(
[
['password', null, $password],
['password_confirmation', null, $password],
['is_subscribed', false, true],
]
);

$this->customerMock->expects($this->once())
->method('setAddresses')
Expand All @@ -371,7 +373,7 @@ public function testSuccessMessage(
->with($this->equalTo($customerId));

$this->messageManagerMock->expects($this->any())
->method('addSuccess')
->method('addSuccessMessage')
->with($this->stringContains($successMessage))
->will($this->returnSelf());

Expand Down Expand Up @@ -477,11 +479,13 @@ public function testSuccessRedirect(

$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap([
['password', null, $password],
['password_confirmation', null, $password],
['is_subscribed', false, true],
]);
->willReturnMap(
[
['password', null, $password],
['password_confirmation', null, $password],
['is_subscribed', false, true],
]
);

$this->customerMock->expects($this->once())
->method('setAddresses')
Expand All @@ -502,16 +506,18 @@ public function testSuccessRedirect(
->with($this->equalTo($customerId));

$this->messageManagerMock->expects($this->any())
->method('addSuccess')
->method('addSuccessMessage')
->with($this->stringContains($successMessage))
->will($this->returnSelf());

$this->urlMock->expects($this->any())
->method('getUrl')
->willReturnMap([
['*/*/index', ['_secure' => true], $successUrl],
['*/*/create', ['_secure' => true], $successUrl],
]);
->willReturnMap(
[
['*/*/index', ['_secure' => true], $successUrl],
['*/*/create', ['_secure' => true], $successUrl],
]
);
$this->redirectMock->expects($this->once())
->method('success')
->with($this->equalTo($successUrl))
Expand Down
32 changes: 31 additions & 1 deletion app/code/Magento/Customer/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,34 @@
</argument>
</arguments>
</type>
</config>
<type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool">
<arguments>
<argument name="configurationsMap" xsi:type="array">
<item name="customerAlreadyExistsErrorMessage" xsi:type="array">
<item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
<item name="data" xsi:type="array">
<item name="template" xsi:type="string">Magento_Customer::messages/customerAlreadyExistsErrorMessage.phtml</item>
</item>
</item>
<item name="confirmAccountSuccessMessage" xsi:type="array">
<item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
<item name="data" xsi:type="array">
<item name="template" xsi:type="string">Magento_Customer::messages/confirmAccountSuccessMessage.phtml</item>
</item>
</item>
<item name="customerVatShippingAddressSuccessMessage" xsi:type="array">
<item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
<item name="data" xsi:type="array">
<item name="template" xsi:type="string">Magento_Customer::messages/customerVatShippingAddressSuccessMessage.phtml</item>
</item>
</item>
<item name="customerVatBillingAddressSuccessMessage" xsi:type="array">
<item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
<item name="data" xsi:type="array">
<item name="template" xsi:type="string">Magento_Customer::messages/customerVatBillingAddressSuccessMessage.phtml</item>
</item>
</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var \Magento\Framework\View\Element\Template $block */
?>
<?= $block->escapeHtml(__('You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.', $block->getData('url')), ['a']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var \Magento\Framework\View\Element\Template $block */
?>
<?= $block->escapeHtml(__('There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.', $block->getData('url')), ['a']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var \Magento\Framework\View\Element\Template $block */
?>
<?= $block->escapeHtml(__('If you are a registered VAT customer, please <a href="%1">click here</a> to enter your billing address for proper VAT calculation.', $block->getData('url')), ['a']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var \Magento\Framework\View\Element\Template $block */
?>
<?= $block->escapeHtml(__('If you are a registered VAT customer, please <a href="%1">click here</a> to enter your shipping address for proper VAT calculation.', $block->getData('url')), ['a']);