diff -u b/core/modules/user/src/Form/UserPasswordForm.php b/core/modules/user/src/Form/UserPasswordForm.php --- b/core/modules/user/src/Form/UserPasswordForm.php +++ b/core/modules/user/src/Form/UserPasswordForm.php @@ -169,12 +169,8 @@ */ public function submitForm(array &$form, FormStateInterface $form_state) { $account = $form_state->getValue('account'); - - // $langcode will fallback to default langcode if property preferred_lancode is not set. - $langcode = $account->getPreferredLangcode(); - // Mail one time login URL and instructions using current language. - $mail = _user_mail_notify('password_reset', $account, $langcode); + $mail = _user_mail_notify('password_reset', $account); if (!empty($mail)) { $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getAccountName(), '%email' => $account->getEmail()]); $this->messenger()->addStatus($this->t('Further instructions have been sent to your email address.')); diff -u b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php --- b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php +++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php @@ -226,21 +226,7 @@ $default_language_code = $this->languageManager->getDefaultLanguage()->getId(); // Test password reset when language prefixes are not set. - $this->drupalGet('user/password'); - $edit = ['name' => $this->account->getAccountName()]; - $this->drupalPostForm(NULL, $edit, t('Submit')); - $this->assertValidPasswordReset($edit['name']); - - $this->assertMailString('body', 'You may now log in by clicking this link or copying and pasting it into your browser:', 1); - $resetURL = $this->getResetURL(); - $this->assertStringNotContainsString("$default_language_code/user/reset/", $resetURL); - - // Ensure user can login with one-time login link. - $this->drupalGet($resetURL); - $this->drupalPostForm(NULL, [], t('Log in')); - $this->assertSession()->linkExists('Log out'); - $this->assertSession()->titleEquals($this->account->getAccountName() . ' | Drupal'); - $this->drupalLogout(); + $this->assertLanguagePrefixResetUrl('user/password', '/user/reset', "$default_language_code/user/reset"); // Set language prefixes. $config = $this->config('language.negotiation'); @@ -249,25 +235,15 @@ $this->rebuildContainer(); $this->drupalGet('zh/user/password'); - - // Test all language code set for browser, default. - $this->assertSame('zh-hant', $this->drupalGetHeader('Content-language')); - $this->assertSame('en', $this->languageManager->getDefaultLanguage()->getId()); - - // Test browser active language and default language is different. - $this->assertNotSame($this->languageManager->getDefaultLanguage()->getId(), $this->drupalGetHeader('Content-language')); + $this->assertLangcodeDifferent('zh-hant', 'en'); // Set preferred language for user account. $this->account->preferred_langcode = 'fr'; $this->account->save(); $this->assertSame('fr', $this->account->getPreferredLangcode()); - // Test reset URL respects preferred language over other lanugage settings. - $edit = ['name' => $this->account->getAccountName()]; - $this->submitForm($edit, t('Submit')); - $this->assertMailString('body', '/fr/user/reset', 1); - $resetURL = $this->getResetURL(); - $this->assertStringNotContainsString('/zh/user/reset', $resetURL); + // Test reset URL respects preferred language over other language settings. + $this->assertLanguagePrefixResetUrl('zh/user/password', '/fr/user/reset', '/zh/user/reset'); // Unset preferred language for user account. $this->account->preferred_langcode = NULL; @@ -276,16 +252,7 @@ // Test reset URL respect default language over browser language when // preferred language is no set. - $this->drupalGet("zh/user/password"); - $this->assertSame('zh-hant', $this->drupalGetHeader('Content-language')); - $this->assertSame('en', $this->languageManager->getDefaultLanguage()->getId()); - $this->assertNotSame($this->languageManager->getDefaultLanguage()->getId(), $this->drupalGetHeader('Content-language')); - - $edit = ['name' => $this->account->getAccountName()]; - $this->submitForm($edit, t('Submit')); - $this->assertMailString('body', '/user/reset', 1); - $resetURL = $this->getResetURL(); - $this->assertStringNotContainsString('/zh/user/reset', $resetURL); + $this->assertLanguagePrefixResetUrl('zh/user/password', '/user/reset', '/zh/user/reset'); // Unset language prefixes. $config = $this->config('language.negotiation'); @@ -294,4 +261,40 @@ /** + * Helper method for language prefix and reset url assertion. + * + * @param string $url + * Url for drupalGet. + * @param string $expectContainString + * String to expect from reset Url. + * @param string $expectNotContainString + * String not to expect from reset Url. + */ + public function assertLanguagePrefixResetUrl($url, $expectContainString, $expectNotContainString) { + $this->drupalGet($url); + $edit = ['name' => $this->account->getAccountName()]; + $this->drupalPostForm(NULL, $edit, t('Submit')); + $this->assertValidPasswordReset($edit['name']); + + $resetURL = $this->getResetURL(); + $this->assertStringContainsString($expectContainString, $resetURL); + $this->assertStringNotContainsString($expectNotContainString, $resetURL); + } + + /** + * Helper method to assert expected active language and + * default lanugage is different. + * + * @param string $activeLangcode + * The active language code of your current session. + * @param string $defaultLangcode + * The default language code of your site settings. + */ + public function assertLangcodeDifferent($activeLangcode, $defaultLangcode) { + $this->assertSame($activeLangcode, $this->drupalGetHeader('Content-language')); + $this->assertSame($defaultLangcode, $this->languageManager->getDefaultLanguage()->getId()); + $this->assertNotSame($this->languageManager->getDefaultLanguage()->getId(), $this->drupalGetHeader('Content-language')); + } + + /** * Retrieves password reset email and extracts the login link. */