Unverified Commit a719eefb authored by alexpott's avatar alexpott

Issue #86287 by g-brodiei, brianV, drupal_was_my_past, ravi.shankar, krlucas,...

Issue #86287 by g-brodiei, brianV, drupal_was_my_past, ravi.shankar, krlucas, wengerk, svendecabooter, chmez, paulocs, alexpott, rohit-rajput-sahab, jonathanshaw, salvis, alienzed, joachim, Dries, webchick, catch, Gábor Hojtsy, quietone: Password reset process ignores the user's language preference
parent 754b8ffa
......@@ -168,11 +168,9 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
* {@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);
$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.'));
......
......@@ -6,6 +6,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
......@@ -27,12 +28,19 @@ class UserPasswordResetTest extends BrowserTestBase {
*/
protected $account;
/**
* Language manager object.
*
* @var \Drupal\language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['block'];
protected static $modules = ['block', 'language'];
/**
* {@inheritdoc}
......@@ -205,6 +213,79 @@ public function testUserPasswordReset() {
$this->assertSession()->statusCodeEquals(403);
}
/**
* Tests password reset functionality when user has set preferred language.
*
* @dataProvider languagePrefixTestProvider
*/
public function testUserPasswordResetPreferredLanguage($setPreferredLangcode, $activeLangcode, $prefix, $visitingUrl, $expectedResetUrl, $unexpectedResetUrl) {
// Set two new languages.
ConfigurableLanguage::createFromLangcode('fr')->save();
ConfigurableLanguage::createFromLangcode('zh-hant')->save();
$this->languageManager = \Drupal::languageManager();
// Set language prefixes.
$config = $this->config('language.negotiation');
$config->set('url.prefixes', ['en' => '', 'fr' => 'fr', 'zh-hant' => 'zh'])->save();
$this->rebuildContainer();
$this->account->preferred_langcode = $setPreferredLangcode;
$this->account->save();
$this->assertSame($setPreferredLangcode, $this->account->getPreferredLangcode(FALSE));
// Test Default langcode is different from active langcode when visiting different.
if ($setPreferredLangcode !== 'en') {
$this->drupalGet($prefix . '/user/password');
$this->assertSame($activeLangcode, $this->getSession()->getResponseHeader('Content-language'));
$this->assertSame('en', $this->languageManager->getDefaultLanguage()->getId());
}
// Test password reset with language prefixes.
$this->drupalGet($visitingUrl);
$edit = ['name' => $this->account->getAccountName()];
$this->submitForm($edit, t('Submit'));
$this->assertValidPasswordReset($edit['name']);
$resetURL = $this->getResetURL();
$this->assertStringContainsString($expectedResetUrl, $resetURL);
$this->assertStringNotContainsString($unexpectedResetUrl, $resetURL);
}
/**
* Data provider for testUserPasswordResetPreferredLanguage().
*
* @return array
*/
public function languagePrefixTestProvider() {
return [
'Test language prefix set as \'\', visiting default with preferred language as en' => [
'setPreferredLangcode' => 'en',
'activeLangcode' => 'en',
'prefix' => '',
'visitingUrl' => 'user/password',
'expectedResetUrl' => 'user/reset',
'unexpectedResetUrl' => 'en/user/reset',
],
'Test language prefix set as fr, visiting zh with preferred language as fr' => [
'setPreferredLangcode' => 'fr',
'activeLangcode' => 'fr',
'prefix' => 'fr',
'visitingUrl' => 'zh/user/password',
'expectedResetUrl' => 'fr/user/reset',
'unexpectedResetUrl' => 'zh/user/reset',
],
'Test language prefix set as zh, visiting zh with preferred language as \'\'' => [
'setPreferredLangcode' => '',
'activeLangcode' => 'zh-hant',
'prefix' => 'zh',
'visitingUrl' => 'zh/user/password',
'expectedResetUrl' => 'user/reset',
'unexpectedResetUrl' => 'zh/user/reset',
],
];
}
/**
* Retrieves password reset email and extracts the login link.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment