View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class UserLanguageCreationTest extends BrowserTestBase {
protected static $modules = [
'user',
'language',
];
protected $defaultTheme = 'stark';
public function testLocalUserCreation() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
'administer users',
]);
$this
->drupalLogin($admin_user);
$langcode = 'fr';
ConfigurableLanguage::createFromLangcode($langcode)
->save();
$edit = [
'language_interface[enabled][language-url]' => TRUE,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$this
->assertSession()
->pageTextContains('Language detection configuration saved.');
$this
->drupalGet($langcode . '/admin/people/create');
$this
->assertTrue($this
->assertSession()
->optionExists("edit-preferred-langcode", $langcode)
->isSelected());
$username = $this
->randomMachineName(10);
$edit = [
'name' => $username,
'mail' => $this
->randomMachineName(4) . '@example.com',
'pass[pass1]' => $username,
'pass[pass2]' => $username,
];
$this
->drupalGet($langcode . '/admin/people/create');
$this
->submitForm($edit, 'Create new account');
$user = user_load_by_name($username);
$this
->assertEquals($langcode, $user
->getPreferredLangcode(), 'New user has correct preferred language set.');
$this
->assertEquals($langcode, $user
->language()
->getId(), 'New user has correct profile language set.');
$this
->drupalLogout();
$this
->drupalGet($langcode . '/user/register');
$this
->assertSession()
->fieldNotExists('language[fr]');
$username = $this
->randomMachineName(10);
$edit = [
'name' => $username,
'mail' => $this
->randomMachineName(4) . '@example.com',
];
$this
->drupalGet($langcode . '/user/register');
$this
->submitForm($edit, 'Create new account');
$user = user_load_by_name($username);
$this
->assertEquals($langcode, $user
->getPreferredLangcode(), 'New user has correct preferred language set.');
$this
->assertEquals($langcode, $user
->language()
->getId(), 'New user has correct profile language set.');
$user_edit = $langcode . '/user/' . $user
->id() . '/edit';
$this
->drupalLogin($admin_user);
$this
->drupalGet($user_edit);
$this
->assertTrue($this
->assertSession()
->optionExists("edit-preferred-langcode", $langcode)
->isSelected());
$user->passRaw = $this
->randomMachineName(10);
$edit = [
'pass[pass1]' => $user->passRaw,
'pass[pass2]' => $user->passRaw,
];
$this
->drupalGet($user_edit);
$this
->submitForm($edit, 'Save');
$this
->drupalLogin($user);
$this
->drupalGet($user_edit);
$this
->assertTrue($this
->assertSession()
->optionExists("edit-preferred-langcode", $langcode)
->isSelected());
}
}