You are here

public function SettingsFormTest::testSettingsForm in Automated Logout 8

Tests the behaviour of the settings upon submission.

File

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

Class

SettingsFormTest
Tests the settings form.

Namespace

Drupal\Tests\autologout\Kernel

Code

public function testSettingsForm() {
  $form_builder = $this->container
    ->get('form_builder');
  $settings = $this->configFactory
    ->getEditable('autologout.settings');
  $roles = user_roles();
  $settings
    ->set('max_timeout', 1000)
    ->save();

  // Test that it is possible to set a value above the max_timeout threshold.
  $form_state = (new FormState())
    ->setValues([
    'timeout' => 1500,
    'max_timeout' => 2000,
    'padding' => 60,
    'role_logout' => TRUE,
    'redirect_url' => '/user/login',
  ]);
  foreach ($roles as $key => $role) {
    $form_state
      ->setValue([
      'table',
      $key,
      'enabled',
    ], TRUE);
    $form_state
      ->setValue([
      'table',
      $key,
      'timeout',
    ], 1200);
    $form_state
      ->setValue([
      'table',
      $key,
      'url',
    ], '/user/login');
  }
  $form_builder
    ->submitForm(AutologoutSettingsForm::class, $form_state);
  $this
    ->assertCount(0, $form_state
    ->getErrors());

  // Test that out of range values are picked up.
  $form_state
    ->setValues([
    'timeout' => 2500,
    'max_timeout' => 2000,
    'padding' => 60,
    'role_logout' => TRUE,
    'redirect_url' => '/user/login',
  ]);
  foreach ($roles as $key => $role) {
    $form_state
      ->setValue([
      'table',
      $key,
      'enabled',
    ], TRUE);
    $form_state
      ->setValue([
      'table',
      $key,
      'timeout',
    ], 1200);
    $form_state
      ->setValue([
      'table',
      $key,
      'url',
    ], '/user/login');
  }
  $form_builder
    ->submitForm(AutologoutSettingsForm::class, $form_state);
  $form_errors = $form_state
    ->getErrors();
  $this
    ->assertCount(1, $form_errors);
  $this
    ->assertEquals('The timeout must be an integer greater than or equal to 60 and less then or equal to <em class="placeholder">2000</em>.', $form_errors['timeout']);

  // Test that out of range values are picked up.
  $form_state
    ->setValues([
    'timeout' => 1500,
    'max_timeout' => 2000,
    'padding' => 60,
    'role_logout' => TRUE,
    'redirect_url' => '/user/login',
  ]);
  foreach ($roles as $key => $role) {
    $form_state
      ->setValue([
      'table',
      $key,
      'enabled',
    ], TRUE);
    $form_state
      ->setValue([
      'table',
      $key,
      'timeout',
    ], 2500);
    $form_state
      ->setValue([
      'table',
      $key,
      'url',
    ], '/user/login');
  }
  $form_builder
    ->submitForm(AutologoutSettingsForm::class, $form_state);
  $form_errors = $form_state
    ->getErrors();
  $this
    ->assertCount(1, $form_errors);
  $this
    ->assertEquals(t('%role role timeout must be an integer greater than 60, less then <em class="placeholder">2000</em> or 0 to disable autologout for that role.', [
    '%role' => key($roles),
  ]), $form_errors['table][' . key($roles) . '][timeout']);

  // Test that role timeouts are not validated for disabled roles.
  $form_state
    ->setValues([
    'timeout' => 1500,
    'max_timeout' => 2000,
    'padding' => 60,
    'role_logout' => TRUE,
    'redirect_url' => '/user/login',
  ]);
  foreach ($roles as $key => $role) {
    $form_state
      ->setValue([
      'table',
      $key,
      'enabled',
    ], FALSE);
    $form_state
      ->setValue([
      'table',
      $key,
      'timeout',
    ], 1200);
    $form_state
      ->setValue([
      'table',
      $key,
      'url',
    ], '/user/login');
  }
  $form_builder
    ->submitForm(AutologoutSettingsForm::class, $form_state);
  $this
    ->assertCount(0, $form_state
    ->getErrors());

  // Test clearing of users individual timeout when this becomes disabled.
  $uid = $this->privilegedUser
    ->id();
  $this->userData
    ->set('autologout', $uid, 'timeout', 1600);
  $form_state
    ->setValues([
    'no_individual_logout_threshold' => TRUE,
  ]);
  $form_builder
    ->submitForm(AutologoutSettingsForm::class, $form_state);
  $this
    ->assertAutotimeout($uid, 1500, 'User timeout is cleared when setting no_individual_logout_threshold is activated.');
}