You are here

public function UserPasswordResetTest::testUserPasswordResetPreferredLanguage in Drupal 9

Tests password reset functionality when user has set preferred language.

@dataProvider languagePrefixTestProvider

File

core/modules/user/tests/src/Functional/UserPasswordResetTest.php, line 238

Class

UserPasswordResetTest
Ensure that password reset methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

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, 'Submit');
  $this
    ->assertValidPasswordReset($edit['name']);
  $resetURL = $this
    ->getResetURL();
  $this
    ->assertStringContainsString($expectedResetUrl, $resetURL);
  $this
    ->assertStringNotContainsString($unexpectedResetUrl, $resetURL);
}