View source
<?php
namespace Drupal\Tests\admin_user_language\Functional;
class AdminUserLanguageUserTest extends AdminUserLanguageBrowserTestBase {
public function testCoreFunctionalityOnUserRegistrationAndEdit() {
$activeLanguages = $this
->getActiveLanguages();
$randomLanguage = array_rand($activeLanguages);
$userAccount = $this
->getuCurrentUserAccount();
$adminUserLang = $this
->getAdminUserLang($userAccount
->id());
self::assertEquals($adminUserLang, [], 'User has no preferred admin language.');
$this
->setUserAdminPreferredLang($randomLanguage, TRUE);
$user = $this
->getDrupalUser($userAccount
->id());
$user
->set('name', mt_rand());
$user
->save();
$adminUserLang = $this
->getAdminUserLang($this
->getuCurrentUserAccount()
->id());
self::assertEquals($adminUserLang, [
[
'value' => $randomLanguage,
],
], 'User has the preferred admin language set to ' . $randomLanguage . '.');
$this
->setUserAdminPreferredLang($randomLanguage, FALSE);
$user = $this
->getDrupalUser($userAccount
->id());
$user
->set('preferred_admin_langcode', FALSE);
$user
->save();
$adminUserLang = $this
->getAdminUserLang($this
->getuCurrentUserAccount()
->id());
self::assertEquals($adminUserLang, [
[
'value' => '',
],
], 'User has the preferred admin language set to "none".');
$admin_user = $this
->drupalCreateUser([
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$adminUserLang = $this
->getAdminUserLang($admin_user
->id());
self::assertEquals($adminUserLang, [
[
'value' => $randomLanguage,
],
], 'The newly created user has the preferred admin language set to ' . $randomLanguage . '.');
$this
->setUserAdminPreferredLang('-1', FALSE);
$admin_user = $this
->drupalCreateUser([
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$adminUserLang = $this
->getAdminUserLang($admin_user
->id());
self::assertEquals($adminUserLang, [], 'The newly created user has the preferred admin language set to "none".');
}
protected function setUserAdminPreferredLang($langCode, $preventOverride = FALSE) {
$config = \Drupal::configFactory()
->getEditable('admin_user_language.settings');
$config
->set('default_language_to_assign', $langCode)
->set('prevent_user_override', $preventOverride)
->save();
}
private function getuCurrentUserAccount() {
return $this->container
->get('current_user')
->getAccount();
}
private function getDrupalUser($uid) {
return \Drupal::entityTypeManager()
->getStorage('user')
->load($uid);
}
private function getAdminUserLang($uid) {
$user = $this
->getDrupalUser($uid);
return $user
->get('preferred_admin_langcode')
->getValue();
}
}