View source
<?php
namespace Drupal\user\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
class UserLanguageCreationTest extends WebTestBase {
public static $modules = array(
'user',
'language',
);
function testLocalUserCreation() {
$admin_user = $this
->drupalCreateUser(array(
'administer languages',
'access administration pages',
'administer users',
));
$this
->drupalLogin($admin_user);
$langcode = 'fr';
ConfigurableLanguage::createFromLangcode($langcode)
->save();
$edit = array(
'language_interface[enabled][language-url]' => TRUE,
);
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$this
->assertText(t('Language detection configuration saved.'), 'Set language negotiation.');
$this
->drupalGet($langcode . '/admin/people/create');
$this
->assertOptionSelected("edit-preferred-langcode", $langcode, 'Global language set in the language selector.');
$username = $this
->randomMachineName(10);
$edit = array(
'name' => $username,
'mail' => $this
->randomMachineName(4) . '@example.com',
'pass[pass1]' => $username,
'pass[pass2]' => $username,
);
$this
->drupalPostForm($langcode . '/admin/people/create', $edit, t('Create new account'));
$user = user_load_by_name($username);
$this
->assertEqual($user
->getPreferredLangcode(), $langcode, 'New user has correct preferred language set.');
$this
->assertEqual($user
->language()
->getId(), $langcode, 'New user has correct profile language set.');
$this
->drupalLogout();
$this
->drupalGet($langcode . '/user/register');
$this
->assertNoFieldByName('language[fr]', 'Language selector is not accessible.');
$username = $this
->randomMachineName(10);
$edit = array(
'name' => $username,
'mail' => $this
->randomMachineName(4) . '@example.com',
);
$this
->drupalPostForm($langcode . '/user/register', $edit, t('Create new account'));
$user = user_load_by_name($username);
$this
->assertEqual($user
->getPreferredLangcode(), $langcode, 'New user has correct preferred language set.');
$this
->assertEqual($user
->language()
->getId(), $langcode, 'New user has correct profile language set.');
$user_edit = $langcode . '/user/' . $user
->id() . '/edit';
$this
->drupalLogin($admin_user);
$this
->drupalGet($user_edit);
$this
->assertOptionSelected("edit-preferred-langcode", $langcode, 'Language selector is accessible and correct language is selected.');
$user->pass_raw = $this
->randomMachineName(10);
$edit = array(
'pass[pass1]' => $user->pass_raw,
'pass[pass2]' => $user->pass_raw,
);
$this
->drupalPostForm($user_edit, $edit, t('Save'));
$this
->drupalLogin($user);
$this
->drupalGet($user_edit);
$this
->assertOptionSelected("edit-preferred-langcode", $langcode, 'Language selector is accessible and correct language is selected.');
}
}