You are here

public function SettingsFormTest::testTimeoutPrecedence in Automated Logout 8

Tests the precedence of the timeouts.

This tests the following function: _autologout_get_user_timeout();

File

tests/src/Kernel/SettingsFormTest.php, line 187

Class

SettingsFormTest
Tests the settings form.

Namespace

Drupal\Tests\autologout\Kernel

Code

public function testTimeoutPrecedence() {
  $settings = $this->configFactory
    ->getEditable('autologout.settings');
  $user_settings = $this->container
    ->get('user.data');
  $uid = $this->privilegedUser
    ->id();
  $role_settings = $this->configFactory
    ->getEditable('autologout.role.' . key(user_roles()));

  // Default used if no role is specified.
  $settings
    ->set('timeout', 100)
    ->set('role_logout', FALSE)
    ->save();
  $role_settings
    ->set('enabled', FALSE)
    ->set('timeout', 200)
    ->save();
  $this
    ->assertAutotimeout($uid, 100, 'User timeout uses default if no other option is set.');

  // Default used if role is selected but no user role is selected.
  $settings
    ->set('role_logout', TRUE)
    ->save();
  $role_settings
    ->set('enabled', FALSE)
    ->set('timeout', 200)
    ->save();
  $this
    ->assertAutotimeout($uid, 100, 'User timeout uses default if role timeouts are used but not one of the current user.');

  // Role timeout is used if user role is selected.
  $settings
    ->set('role_logout', TRUE)
    ->save();
  $role_settings
    ->set('enabled', TRUE)
    ->set('timeout', 200)
    ->save();
  $this
    ->assertAutotimeout($uid, 200, 'User timeout uses role value.');

  // Role timeout is used if user role is selected.
  $settings
    ->set('role_logout', TRUE)
    ->save();
  $role_settings
    ->set('enabled', TRUE)
    ->set('timeout', 0)
    ->save();
  $this
    ->assertAutotimeout($uid, 0, 'User timeout uses role value of 0 if set for one of the user roles.');

  // Role timeout used if personal timeout is an empty string.
  $settings
    ->set('role_logout', TRUE)
    ->save();
  $role_settings
    ->set('enabled', TRUE)
    ->set('timeout', 200)
    ->save();
  $user_settings
    ->set('autologout', $uid, 'timeout', '');
  $user_settings
    ->set('autologout', $uid, 'enabled', FALSE);
  $this
    ->assertAutotimeout($uid, 200, 'User timeout uses role value if personal value is an empty string.');

  // Default timeout used if personal timeout is an empty string.
  $settings
    ->set('role_logout', TRUE)
    ->save();
  $role_settings
    ->set('enabled', FALSE)
    ->set('timeout', 200)
    ->save();
  $user_settings
    ->set('autologout', $uid, 'timeout', '');
  $user_settings
    ->set('autologout', $uid, 'enabled', FALSE);
  $this
    ->assertAutotimeout($uid, 100, 'User timeout uses default value if personal value is an empty string and no role timeout is specified.');

  // Personal timeout used if set.
  $settings
    ->set('role_logout', TRUE)
    ->save();
  $role_settings
    ->set('enabled', FALSE)
    ->set('timeout', 200)
    ->save();
  $user_settings
    ->set('autologout', $uid, 'timeout', 300);
  $user_settings
    ->set('autologout', $uid, 'enabled', TRUE);
  $this
    ->assertAutotimeout($uid, 300, 'User timeout uses personal timeout.');
}