public function UserEditTest::testUserChangeSiteLanguage in Drupal 10
Same name and namespace in other branches
- 8 core/modules/user/tests/src/Functional/UserEditTest.php \Drupal\Tests\user\Functional\UserEditTest::testUserChangeSiteLanguage()
- 9 core/modules/user/tests/src/Functional/UserEditTest.php \Drupal\Tests\user\Functional\UserEditTest::testUserChangeSiteLanguage()
Tests that a user is able to change site language.
File
- core/
modules/ user/ tests/ src/ Functional/ UserEditTest.php, line 198
Class
- UserEditTest
- Tests user edit page.
Namespace
Drupal\Tests\user\FunctionalCode
public function testUserChangeSiteLanguage() {
// Install these modules here as these aren't needed for other test methods.
\Drupal::service('module_installer')
->install([
'content_translation',
'language',
]);
// Create and login as an admin user to add a new language and enable
// translation for user accounts.
$adminUser = $this
->drupalCreateUser([
'administer account settings',
'administer languages',
'administer content translation',
'administer users',
'translate any entity',
]);
$this
->drupalLogin($adminUser);
// Add a new language into the system.
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->assertSession()
->pageTextContains('French');
// Enable translation for user accounts.
$edit = [
'language[content_translation]' => 1,
];
$this
->drupalGet('admin/config/people/accounts');
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
// Create a regular user for whom translation will be enabled.
$webUser = $this
->drupalCreateUser();
// Create a translation for a regular user account.
$this
->drupalGet('user/' . $webUser
->id() . '/translations/add/en/fr');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
// Update the site language of the user account.
$edit = [
'preferred_langcode' => 'fr',
];
$this
->drupalGet('user/' . $webUser
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
}