1 locale.test LocaleUserLanguageFunctionalTest::testUserLanguageConfiguration()

Test if user can change their default language.


core/modules/locale/tests/locale.test, line 1527
Tests for locale.module.


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


function testUserLanguageConfiguration() {
  global $base_url;

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

  // Add custom language.
  // 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->backdropPost('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Add custom language and disable it.
  // Code for the language.
  $langcode_disabled = 'xx-yy';
  // The English name for the language. This will be translated.
  $name_disabled = $this->randomName(16);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode_disabled,
    'name' => $name_disabled,
    'direction' => '0',
  $this->backdropPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  // Disable the language.
  $edit = array(
    'languages[' . $langcode_disabled . '][enabled]' => FALSE,
  $this->backdropPost('admin/config/regional/language', $edit, t('Save configuration'));

  // Login as normal user and edit account settings.
  $path = 'user/' . $web_user->uid . '/edit';
  // Ensure language settings fieldset is available.
  $this->assertText(t('Language'), 'Language selector available.');
  // Ensure custom language is present.
  $this->assertText($name, 'Language present on form.');
  // Ensure disabled language isn't present.
  $this->assertNoText($name_disabled, 'Disabled language not present on form.');
  // Switch to our custom language.
  $edit = array(
    'language' => $langcode,
  $this->backdropPost($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.
  $elements = $this->xpath('//input[@id=:id]', array(':id' => 'edit-language-' . $langcode));
  $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Default language successfully updated.');