Summary

The user_mail function respects the language value specfied by _user_mail_notify() for the text of user emails, but it ignores it for the tokens in emails, such as password recovery links. Therefore in some cases the text and token languages can be different. For example, you can get email content in french, but the password reset URL comes as "/en/user/reset/..." .

Cause

There are a total of three API setting $langcode along the way to set the language of recovery email.

  1. submitForm() in UserPasswordFrom.php, taking the current browsing langcode and passed to _user_mail_notify().
  2. _user_mail_notify in user.module line 1042, it has a condition written to use the $langcode as $account->getPreferredLangcode if not specified. Here is where it sends an email request by invoking MailManagerInterface::mail() service and passing $langcode in its parameter, which goes through user_mail (hook_mail) to modify it's email contents.
  3. user_mail in user.module line 772, it has set to retrieve it's email content base on getPreferredLangcode of User while token option was set to follow the $message[langcode] variable from MailManagerInterface::mail().

The descriptions above reveals the bug how email's text content doesn't align with content replaced by tokens (URL with langcode prefix in this case) is within user_mail, whereas it should use the langcode that _user_mail_notify() provided as said by @jonathanshaw #23.

To understand the details of discussion, you can start from #21.

Password reset urls and account preferred language

This issue does not fully address password recovery emails not respecting the user's preferred language, because the language is also set in UserPasswordForm::submitForm() and there is an old issue for that: #86287: Password reset process ignores the user's language preference.

Original Problem/Motivation

In the user_mail function, the language manager is set to the preferred langcode of the user.

$language = $language_manager->getLanguage($params['account']->getPreferredLangcode());
$original_language = $language_manager->getConfigOverrideLanguage();
$language_manager->setConfigOverrideLanguage($language);

In some cases if we load config, the mail_config is translated in the language of the user:

$mail_config = \Drupal::config('user.mail');

While loading the token options, an other langcode is set?!

Steps required to reproduce the bug

While sending a "Password Recovery" mail, the mail subject and body is in the language of the user.
The [user:one-time-login-url] token is in another language.

Solution, see patch.

Proposed resolution

New proposal

Quoting #23:

The only thing we need to fix is in user_mail():

$language = $language_manager->getLanguage($params['account']->getPreferredLangcode());

should become

$language = $language_manager->getLanguage($langcode);

user_mail() should use the langcode that _user_mail_notify() has provided.

Original proposal

Set the correct langcode in $token_options array:

  $token_options = ['langcode' => $language->getId(), 'callback' => 'user_mail_tokens', 'clear' => TRUE];

See Patch.

Remaining tasks

None.

User interface changes

None

API changes

None

Data model changes

None.

CommentFileSizeAuthor
#61 2991677-61.patch4.98 KBpaulocs
#61 interdiff-57-61.txt1.32 KBpaulocs
#57 2991677-57.patch4.44 KBpaulocs
#57 interdiff-51-57.txt1.15 KBpaulocs
#52 interdiff-49_51.txt290 bytesg-brodiei
#52 2991677-51.patch5.19 KBg-brodiei
#49 interdiff-41_49.txt3.21 KBg-brodiei
#49 2991677-49.patch5.2 KBg-brodiei
#41 interdiff-37_41.txt3.4 KBg-brodiei
#41 2991677-41.patch5.81 KBg-brodiei
#41 2991677-failtest-3.patch5.16 KBg-brodiei
#37 interdiff-33_37.txt4.59 KBg-brodiei
#37 2991677-37.patch5.86 KBg-brodiei
#37 2991677-failtest-2.patch5.21 KBg-brodiei
#36 Screenshot from 2020-10-19 21-00-53.png35.05 KBg-brodiei
#36 Screenshot from 2020-10-19 21-00-16.png23.46 KBg-brodiei
#33 interdiff-29_33.txt4.74 KBg-brodiei
#33 2991677-33.patch6.03 KBg-brodiei
#33 2991677-failtest.patch5.38 KBg-brodiei
#29 interdiff-26_29.txt3.82 KBg-brodiei
#29 2991677-29.patch4.6 KBg-brodiei
#26 2991677-26.patch666 bytesg-brodiei
#17 Screenshot from 2020-10-06 09-32-26.png16.52 KBg-brodiei
#14 interdiff-13-14.txt788 bytesg-brodiei
#14 2991677-14.patch5.19 KBg-brodiei
#14 2991677-14-failtest.patch4.02 KBg-brodiei
#13 interdiff_3-13.txt3.83 KBraman.b
#13 2991677-13.patch5.15 KBraman.b
#11 Screenshot from 2020-10-01 00-23-37.png32.78 KBg-brodiei
#11 Screenshot from 2020-10-01 00-23-27.png32.31 KBg-brodiei
#3 2991677-core-wrong-language-in-token-options.patch1.21 KBtomhollevoet
drupal_mail_token_options_language.patch1.21 KBtomhollevoet
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tomhollevoet created an issue. See original summary.

tomhollevoet’s picture

tomhollevoet’s picture

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Kristen Pol’s picture

Version: 8.9.x-dev » 9.1.x-dev
Issue tags: +Bug Smash Initiative

Patch applies cleanly to 9.1.x.

Kristen Pol’s picture

Issue tags: +Needs manual testing

Thanks for the issue and patch.

1) Changes seem ok.

2) Marking for manual testing.

3) Kicking off tests for 9.1.x.

Kristen Pol’s picture

Issue tags: +Needs tests

Tests are green. This just needs manual testing now and probably needs automated tests too.

g-brodiei’s picture

Assigned: Unassigned » g-brodiei

I'll work on this!

g-brodiei’s picture

Manually tested patch, and it works like a charm.

Tested by following steps:

  1. Fresh install Drupal 9.1.x.
  2. Enable language module.
  3. Install any second language: "Chinese, traditional".
  4. Set language detection with path prefix, english => en, Chinese, Traditional => zh .
  5. Set language preference as Chinese in admin user account settings.
  6. Use incognito mode to request for recovery email.
  7. Receives mail with "en" recovery link.
  8. Apply patch, and redo recovery email step.
  9. Receives email with "zh" recovery link.

Will continue to work on tests.
Remove "need manual testing.

g-brodiei’s picture

Status: Needs review » Needs work
raman.b’s picture

Assigned: g-brodiei » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
5.15 KB
3.83 KB

Adding test coverage for user password reset with preferred language and language prefixes

g-brodiei’s picture

Thanks @raman.b for this thorough testing patch!!

Added failtest patch, altered the link to request reset password in test to assure it respects user's preferred language instead of site use language.

+++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
@@ -205,6 +209,72 @@ public function testUserPasswordReset() {
+    // Test password reset when language prefixes are set.
+    $this->drupalGet('fr/user/password');
+    $edit = ['name' => $this->account->getAccountName()];
+    $this->drupalPostForm(NULL, $edit, t('Submit'));

Changed to 'user/password'.

Add interdiff, ready for review once the patch passes.

The last submitted patch, 14: 2991677-14-failtest.patch, failed testing. View results

jonathanshaw’s picture

It's right that the token language needs to be consistent with the message language.

But I'm not sure about the fix.

We have two sources for what the language should be: the language specified in $message, and the user's preferred language.

If a language is specified in $message, maybe we should be using that? It seems right to defer to the calling code about language, instead of overriding it.

We could though default to the user's preferred language if no language is set in $message (although at the moment the code doesn't even consider that, is $message['langcode'] always set?).

g-brodiei’s picture

Yes, $message['langcode'] is always set in user_mail_tokens() as I see it.

After running through xdebug, $message['langcode'] is base on the language browsed by the user when they apply to reset password, not the preferred language set in user account, thereby the current site language. Without other second language installed, the preferred language is set to "en" by default.

// browse from /zh/user/password
$message['langcode'] = "zh-hant";

// browse from /en/user/password
$message['langcode'] = "en";

Since all the user related emails are expected to respect the preferred language in account settings, maybe we should not make use of $message['langcode']? I think that's how this fix came out?

If the current language_manager is not override by the preferred language, then body and message language will remain same with the current viewing site prefix language, base on the language detection method, either /en or /zh on my setting.

jonathanshaw’s picture

Where is message['langcode'] set? Should we not set it there if we are going to ignore it in user_mail()?

g-brodiei’s picture

Oh sorry, my bad.
I meant it was passed into the $user_mail().

I'll search for the code where it is set and passed in then 😂

g-brodiei’s picture

I found it, and also lost what to do next here.

The langcode was first set once user submitted the UserPasswordForm.php

// line 170-175
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $langcode = $this->languageManager->getCurrentLanguage()->getId();

    $account = $form_state->getValue('account');
    // Mail one time login URL and instructions using current language.
    $mail = _user_mail_notify('password_reset', $account, $langcode);

Then the actual call of mail service and to send mail + set $message['langcode'] in user.module, the call on mail service will then pass the $langcode as $message['langcode'] to user_mail().

// line 1042
function _user_mail_notify($op, AccountInterface $account, $langcode = NULL) {
  if (\Drupal::config('user.settings')->get('notify.' . $op)) {
    $params['account'] = $account;
    $langcode = $langcode ? $langcode : $account->getPreferredLangcode();

// line 1057
$mail = \Drupal::service('plugin.manager.mail')->mail('user', $op, $account->getEmail(), $langcode, $params, $site_mail);

Are you suggesting that we should set the $langcode earlier to $preferredLangcode() before it's passed into the mail()?
Thanks for all the help and review!

jonathanshaw’s picture

So we have a 3 level API: form, user_mail_notify, and user_mail. Currently we're trying to set the language in all 3!

user_mail_notify invokes MailManagerInterface::mail(). That method has langcode as a required argument.

Therefore I propose we make language determination the responsibility of user_mail_notify.

This has 4 implications:

1) user_mail should trust the language param it is supplied with, and we can trust that it will have one because it can only be invoked via MailManagerInterface::mail().

2) Code that calls user_mail_notify may specify the langcode, and it will be used if provided.

3) Core code that calls user_mail_notify should never specify the language, it should leave it to user_mail_notify to determine.

4) We should trigger a deprecation error if the langcode argument is provided to user_mail_notify. Technically it's an internal method and we could just remove the argument, but I suspect its being used often in contrib and custom code.

g-brodiei’s picture

Okay, I looked into the code within _user_mail_notify, and I think it already has the langcode determination part included within its function already.

function _user_mail_notify($op, AccountInterface $account, $langcode = NULL)

// 1045
    $langcode = $langcode ? $langcode : $account->getPreferredLangcode();

Therefore, I say we change to part at the first part of the api as the fix, the submitForm of UserPasswordForm.php.

Original

/**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $langcode = $this->languageManager->getCurrentLanguage()->getId();

    $account = $form_state->getValue('account');
    // Mail one time login URL and instructions using current language.
    $mail = _user_mail_notify('password_reset', $account, $langcode);

to this

/**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $account = $form_state->getValue('account');
    // Mail one time login URL and instructions using current language.
    $mail = _user_mail_notify('password_reset', $account, $account->getPreferredLangcode());

Since the account has it's preferred langcode set to it's default language already. I don't think we need to get current langcode from languageManager anymore when the reset form is first submitted.

jonathanshaw’s picture

I found #86287-60: Password reset process ignores the user's language preference which suggests that sometimes there is a purpose to specifying the langcode when calling _user_mail_notify(). I actually suspect that in user_mail_notify we should always be using the current language if there is no account preferred langage, but that's a separate issue.

So I think we should ignore #21.2, #21.3 and #21.4 (sorry for time wasting!)

The only thing we need to fix is in user_mail():
$language = $language_manager->getLanguage($params['account']->getPreferredLangcode());

should become
$language = $language_manager->getLanguage($langcode);

user_mail() should use the langcode that _user_mail_notify() has provided.

g-brodiei’s picture

I agree with you on the point that user_mail should follow the language code given when it's being called in _user_mail_notify, where the ultimate culprit actually started from submitForm() after reading through #86287-60: Password reset process ignores the user's language preference.

The approach of @wengerk in #60 is what I have in mind after our discussion. Once it is fixed, we can set the fix you mentioned on #23 in place all together. Or we should just fix it once and for all together in that issue?

Updated Issue Summary base on #23.
Setting #86287: Password reset process ignores the user's language preference as related issue, which is the blocker.

jonathanshaw’s picture

I think we can do #23 now, no need to postpone this that I can see.

g-brodiei’s picture

Issue tags: +Needs tests
FileSize
666 bytes

Need to add new test for this piece of fix. This is simply fixing the alignment of language sent by recovery email request between content and reset link by allowing user_mail() function to respect the set $langcode in it's variable. It will not fix the issue where user's preferred langcode is ignored.

The test @raman.b wrote #13 should be added to #86287: Password reset process ignores the user's language preference where the user preferred langcode is to be fixed.

Add new patch base on #23.

Have also tested this patch with user preferred langcode as zh-hant.

1. "sending confirmation request on delete" email
2. "sending new account creation" email

It respects the user's language preference as normal.

g-brodiei’s picture

Was running a kernal test on this earlier LoL, and then I discovered that even though _user_mail_notify was invoked directly in UserMailNotifyTest.php, it doesn't go through user_mail or token replacement at all lmao, nor does it have any config within "user.mail". Gonna make use of functional test instead.

Current idea will be:
1. Enable interface translation (locale), language (language).
2. Add new language.
3. Set language prefix.
4. Set account language preference to new foreign lang.
4. Add language name (or identical strings) to subject and body interface of recovery emails, both in original and translated interface relatively.
5. Retrieve the content and find recovery url, and do an assertion.

jonathanshaw’s picture

Maybe UserMailNotifyTest needs to enable the user module or similar basic stuff?

g-brodiei’s picture

I figured out how to set the prefix, langcode and loading config from user module and override config translation for password recovery.

But the token link doesn't come along with the langcode prefix which I set..... in the email content where have I done wrong??

jonathanshaw’s picture

+++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
@@ -94,4 +158,36 @@ public function testUserMailsNotSent($op) {
+    $configLanguageOverride = $this->languageManager->getLanguageConfigOverride('zh-hant', 'user.mail');
+    $configLanguageOverride->set('password_reset.subject', 'zh-hant [user:display-name]')->save();
...
+    $user->set('preferred_langcode', 'zh-hant')->save();
...
+    $return = _user_mail_notify('password_reset', $this->createUser(), $preferredLangcode);

I suggest we want to test:
- user preferred language zh-hant, no preferred language specified as a third parameter to user_mail_notify: email should use zh-hant
- user preferred language zh-hant, fr specified as preferred language for user_mail_notify: email should use fr

But the token link doesn't come along with the langcode prefix which I set..... in the email content where have I done wrong??

Can you make a failing test case for that? It would then be easier to diagnose and be sure I understand what you mean. I can't see any obvious bug ...

g-brodiei’s picture

Yeah sure! I'll work on a fail test first.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

g-brodiei’s picture

Add failtest patch and testing patch.

Both patch will fail on the first assertion when testing the token link with langcode prefix which should exist. This is the part were I'm having trouble at the moment, don't know what will be the reason to allow token link to return with langcode prefix for a kernal test? I had the language url.prefixes set correctly.

    // Set language prefix.
    $config = $this->config('language.negotiation');
    $config->set('url.prefixes', ['en' => 'en', 'zh-hant' => 'zh', 'fr' => 'fr'])->save();
    $this->configFactory->reset('language.negotiation');
    ConfigurableLanguageManager::rebuildServices();
// These will fail no matter what langcode given to _user_mail_notify().
$this->assertMailString('body', 'zh/user/reset', 1);

If we simply comment all three attempts of language prefix assertion ('zh/user/reset', 'fr/user/reset') and re-run the test with fix, it will confirm that the email's content does respect the $langcode variable given to _user_mail_notify() function when provided.

Status: Needs review » Needs work

The last submitted patch, 33: 2991677-33.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jonathanshaw’s picture

The function user_pass_reset_url() (which generates the reset url token value) has no test coverage for language. Maybe try testing that directly, it could be buggy.

g-brodiei’s picture

Discovered that the prefix was added by processOutBound function in file "PathProcesserManager.php" when it runs through the PathProcessorLanguage object. This object has the language prefix upon http request. On the contrary, the kernal testing doesn't go through http request.

I'll try to mimic/add a similar processor object in setup and see how it goes.

Request by website form directly.

Request with phpunit testing tool.

g-brodiei’s picture

I found out what happened with language prefix not added by the token service, the kernal service wasn't rebuilt LoL.

Added fail patch, a working patch to fix $langcode in user_mail.

g-brodiei’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Remove tag needs test, set to Needs review.

jonathanshaw’s picture

Nice! i think this is ready, just some nits:

  1. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -16,6 +18,67 @@ class UserMailNotifyTest extends EntityKernelTestBase {
    +  protected function setUp():void {
    

    All of this setup code is only used in testUserRecovery, which also does its own further setup steps. I'm not sure what should be in setUp() (maybe nothing) and what in testUserRecovery()

  2. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -94,4 +157,68 @@ public function testUserMailsNotSent($op) {
    +    // Current language is 'en'.
    +    $currentLanguage = $this->languageManager->getCurrentLanguage()->getId();
    

    This variable is unused. I suggest asserting it.

  3. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -94,4 +157,68 @@ public function testUserMailsNotSent($op) {
    +    $franch_email = _user_mail_notify('password_reset', $user, 'fr');
    

    Should be $french_email

Last, the IS needs updating to state the problem as we now understand it and the solution as we propose it. It could be much simpler. The current IS has too much history and is confusing.

g-brodiei’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Updated issue summary.
Remove need issue summary tag.

g-brodiei’s picture

Thanks for spending time review and providing constructive feedbacks @jonathanshaw!!

Let's hope this will be the final patch to fix this bug, hahaha.

Providing patch to fix on points in #39.

1. Changed all code for language setup within setUp() to be within testUserRecoveryMailLanguage(), since only this test needed it.
2. Assert current language and preferred language codes with assertSame().
3. Fixed Typo of "Franch" to "French", silly mistake LoL.

The last submitted patch, 41: 2991677-failtest-3.patch, failed testing. View results

jonathanshaw’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Should #86287: Password reset process ignores the user's language preference land first? It kinda feels that way. If it doesn't that the while the password reset email becomes more consistent it moves further away from the user's preferred langcode.

jonathanshaw’s picture

Status: Reviewed & tested by the community » Postponed

Agreed

jonathanshaw’s picture

Status: Postponed » Reviewed & tested by the community

Blocker was committed, thanks to @alexpott.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -16,6 +18,36 @@ class UserMailNotifyTest extends EntityKernelTestBase {
    +  /**
    +   * Modules to enable.
    +   *
    +   * @var array
    +   */
    +  protected static $modules = [
    +    'user',
    +    'system',
    +    'field',
    +    'text',
    +    'filter',
    +    'entity_test',
    +    'locale',
    +    'language',
    +  ];
    

    You only need to add the new modules here... so locale and language I think.

  2. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -16,6 +18,36 @@ class UserMailNotifyTest extends EntityKernelTestBase {
    +  /**
    +   * The configurable language manager used in this test.
    +   *
    +   * @var \Drupal\language\ConfigurableLanguageManagerInterface
    +   */
    +  protected $languageManager;
    +
    +  /**
    +   * The configuration factory used in this test.
    +   *
    +   * @var \Drupal\Core\Config\ConfigFactoryInterface
    +   */
    +  protected $configFactory;
    
    @@ -94,4 +126,93 @@ public function testUserMailsNotSent($op) {
    +    $this->configFactory = $this->container->get('config.factory');
    +    $this->languageManager = $this->container->get('language_manager');
    

    Attaching these services to the test class and then rebuilding the container is a way to cause interesting bugs. Let's not do this.

  3. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -94,4 +126,93 @@ public function testUserMailsNotSent($op) {
    +    $this->configFactory->reset('language.negotiation');
    ...
    +    $this->configFactory->reset('user.mail');
    ...
    +    $this->configFactory->reset('user.mail');
    

    THese are not necessary.

  4. +++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
    @@ -94,4 +126,93 @@ public function testUserMailsNotSent($op) {
    +    // Reset services to apply change.
    +    \Drupal::service('kernel')->rebuildContainer();
    

    Let's do $this->container->get('kernel')->rebuildContainer(); - it'd be nice if a container invalidation caused the container to be rebuilt in a kernel test - because the code is assuming that the container will be rebuilt on the next request.

g-brodiei’s picture

Assigned: Unassigned » g-brodiei
g-brodiei’s picture

1. Fixed patch to meet changes from deprecation #3186752: Deprecate langcode argument to _user_mail_notify() for _user_mail_notify.
2. Revised base on #47. Thank you @alexpott, I had no idea how the flow of config process works in the system, was just looking up other test codes as reference to use.

Tested this patch on local, will fail when fix not applied. So only providing fixed patch here. ;)

g-brodiei’s picture

Assigned: g-brodiei » Unassigned

Status: Needs review » Needs work

The last submitted patch, 49: 2991677-49.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

g-brodiei’s picture

Fix empty space. (coding standard issue)

What is the reason causing the test to fail...?

fail: [Other] Line 0 of sites/default/files/simpletest/phpunit-1129.xml:
PHPUnit Test failed to complete; Error: PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
jonathanshaw’s picture

Specifying the notification language using the $langcode parameter is deprecated in drupal:9.2.0

We are moving so fast we are tripping ourselves up!7

g-brodiei’s picture

Hahaha, so that mean we can't use deprecated code in tests?

jonathanshaw’s picture

Correct, we can't introduce new uses of deprecated code.

paulocs’s picture

Assigned: Unassigned » paulocs
paulocs’s picture

Assigned: paulocs » Unassigned
Status: Needs work » Needs review
FileSize
1.15 KB
4.44 KB

I was wondering if we have to have the tests for when the langcode is specified if the user has already a preferred language.
I did a patch that removes the deprecated code but not sure if it will cover less cases.

Please review.

alexpott’s picture

Status: Needs review » Needs work
+++ b/core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
@@ -105,4 +117,69 @@ public function testUserMailNotifyLangcodeDeprecation() {
+    // Recovery email should respect user preferred langcode by default if
+    // langcode not set.
+    $default_email = _user_mail_notify('password_reset', $user);

+++ b/core/modules/user/user.module
@@ -775,7 +775,7 @@ function user_mail($key, &$message, $params) {
-  $language = $language_manager->getLanguage($params['account']->getPreferredLangcode());

I think that rather than calling _user_mail_notify here you are going to need to call $mail = \Drupal::service('plugin.manager.mail')->mail('user', $op, $account->getEmail(), $langcode, $params, $site_mail);

paulocs’s picture

Assigned: Unassigned » paulocs

Thanks, I'll work on it.

g-brodiei’s picture

Thanks @paulocs, you're the man!! Here's the baton~ My timezone is in the moon phase now LoL.

paulocs’s picture

Assigned: paulocs » Unassigned
Status: Needs work » Needs review
FileSize
1.32 KB
4.98 KB

Thanks hahaha, good night!

jonathanshaw’s picture

Status: Needs review » Reviewed & tested by the community

Looking good now. @alexpott's review points from #47 are addressed and we are no longer using the deprecated $langcode parameter to _user_mail_notify(), but we are still testing things properly.

alexpott’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 34f0542 and pushed to 9.2.x. Thanks!
Committed abe9d46 and pushed to 9.1.x. Thanks!

Backported to 9.1.x as this change makes the function behave the way it should and you would expect it to.

  • alexpott committed 34f0542 on 9.2.x
    Issue #2991677 by g-brodiei, paulocs, tomhollevoet, raman.b,...

  • alexpott committed abe9d46 on 9.1.x
    Issue #2991677 by g-brodiei, paulocs, tomhollevoet, raman.b,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.