You are here

public function AdminUserLanguageUserTest::testCoreFunctionalityOnUserRegistrationAndEdit in Admin User Language 8

Test if an admin language is correctly set on user registration or edit.

File

tests/src/Functional/AdminUserLanguageUserTest.php, line 16

Class

AdminUserLanguageUserTest
Tests entity presave hook to check admin_user_language functionality.

Namespace

Drupal\Tests\admin_user_language\Functional

Code

public function testCoreFunctionalityOnUserRegistrationAndEdit() {
  $activeLanguages = $this
    ->getActiveLanguages();
  $randomLanguage = array_rand($activeLanguages);
  $userAccount = $this
    ->getuCurrentUserAccount();

  // 1 - Checking that the current user has no preferred administration language.
  $adminUserLang = $this
    ->getAdminUserLang($userAccount
    ->id());
  self::assertEquals($adminUserLang, [], 'User has no preferred admin language.');

  // 2 - Enabling the preferred admin language in the module.
  $this
    ->setUserAdminPreferredLang($randomLanguage, TRUE);

  // Making a change in the User profile to trigger the presave hook and expecting
  // the user to have the preferred language to the default one
  $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 . '.');

  // 3 - Changing the user preferred language to none and deactivating the prevent_user_override should result in a successful save
  $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".');

  // 4 - Creating a new user with the module enabled, this should force by default a preferred admin language
  $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 . '.');

  // 5 - Creating another user by selecting no default language in the module settings
  $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".');
}