function UserLanguageTest::testUserLanguageConfiguration

Test if user can change their default language.

File

drupal/core/modules/user/lib/Drupal/user/Tests/UserLanguageTest.php, line 35
Definition of Drupal\user\Tests\UserLanguageTest.

Class

UserLanguageTest
Functional tests for a user's ability to change their default language.

Namespace

Drupal\user\Tests

Code

function testUserLanguageConfiguration() {
  global $base_url;

  // User to add and remove language.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer languages',
    'access administration pages',
  ));

  // User to change their default language.
  $web_user = $this
    ->drupalCreateUser();

  // Add custom language.
  $this
    ->drupalLogin($admin_user);

  // Code for the language.
  $langcode = 'xx';

  // The English name for the language.
  $name = $this
    ->randomName(16);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'name' => $name,
    'direction' => '0',
  );
  $this
    ->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  $this
    ->drupalLogout();

  // Login as normal user and edit account settings.
  $this
    ->drupalLogin($web_user);
  $path = 'user/' . $web_user->uid . '/edit';
  $this
    ->drupalGet($path);

  // Ensure language settings widget is available.
  $this
    ->assertText(t('Language'), 'Language selector available.');

  // Ensure custom language is present.
  $this
    ->assertText($name, 'Language present on form.');

  // Switch to our custom language.
  $edit = array(
    'preferred_langcode' => $langcode,
  );
  $this
    ->drupalPost($path, $edit, t('Save'));

  // Ensure form was submitted successfully.
  $this
    ->assertText(t('The changes have been saved.'), 'Changes were saved.');

  // Check if language was changed.
  $this
    ->assertOptionSelected('edit-preferred-langcode', $langcode, 'Default language successfully updated.');
  $this
    ->drupalLogout();
}