1 locale.test LocaleUserCreationTest::testLocalUserCreation()

Functional test for language handling during user creation.

File

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

Class

LocaleUserCreationTest
Functional test for language handling during user creation.

Code

function testLocalUserCreation() {
  // User to add and remove language and create new users.
  $admin_user = $this->backdropCreateUser(array('administer languages', 'access administration pages', 'administer users'));
  $this->backdropLogin($admin_user);

  // Add predefined language.
  $langcode = 'fr';
  $edit = array(
    'predefined_langcode' => 'fr',
  );
  $this->backdropPost('admin/config/regional/language/add', $edit, t('Add language'));
  $this->assertText('French', 'Language added successfully.');
  $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');

  // Set language negotiation.
  $edit = array(
    'language[locale-url][enabled]' => TRUE,
  );
  $this->backdropPost('admin/config/regional/language/detection', $edit, t('Save settings'));
  $this->assertText(t('Language negotiation configuration saved.'), 'Set language negotiation.');

  // Check if the language selector is available on admin/people/create and
  // set to the currently active language.
  $this->backdropGet($langcode . '/admin/people/create');
  $this->assertFieldChecked("edit-language-$langcode", 'Global language set in the language selector.');

  // Create a user with the admin/people/create form and check if the correct
  // language is set.
  $username = $this->randomName(10);
  $password = $this->randomName(10);
  $edit = array(
    'name' => $username,
    'mail' => $this->randomName(4) . '@example.com',
    'pass' => $password,
  );

  $this->backdropPost($langcode . '/admin/people/create', $edit, t('Create new account'));

  $user = user_load_by_name($username);
  $this->assertEqual($user->language, $langcode, 'New user has correct language set.');

  // Register a new user and check if the language selector is hidden.
  $this->backdropLogout();

  $this->backdropGet($langcode . '/user/register');
  $this->assertNoFieldByName('language[fr]', 'Language selector is not accessible.');

  $username = $this->randomName(10);
  $edit = array(
    'name' => $username,
    'mail' => $this->randomName(4) . '@example.com',
  );

  $this->backdropPost($langcode . '/user/register', $edit, t('Create new account'));

  $user = user_load_by_name($username);
  $this->assertEqual($user->language, $langcode, 'New user has correct language set.');

  // Test if the admin can use the language selector and if the
  // correct language is was saved.
  $user_edit = $langcode . '/user/' . $user->uid . '/edit';

  $this->backdropLogin($admin_user);
  $this->backdropGet($user_edit);
  $this->assertFieldChecked("edit-language-$langcode", 'Language selector is accessible and correct language is selected.');

  // Set pass_raw so we can login the new user.
  $user->pass_raw = $this->randomName(10);
  $edit = array(
    'pass' => $user->pass_raw,
  );

  $this->backdropPost($user_edit, $edit, t('Save'));

  $this->backdropLogin($user);
  $this->backdropGet($user_edit);
  $this->assertFieldChecked("edit-language-$langcode", 'Language selector is accessible and correct language is selected.');
}