View source
<?php
namespace Drupal\Tests\autologout\Kernel;
use Drupal\autologout\Form\AutologoutSettingsForm;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class SettingsFormTest extends KernelTestBase {
use UserCreationTrait;
public static $modules = [
'user',
'system',
'autologout',
];
protected $privilegedUser;
protected $configFactory;
public $userData;
protected $settingsForm;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('user', [
'users_data',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig('autologout');
$this->configFactory = $this->container
->get('config.factory');
$this->userData = $this->container
->get('user.data');
$this->privilegedUser = $this
->createUser([
'change own logout threshold',
]);
}
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();
$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());
$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']);
$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']);
$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());
$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.');
}
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()));
$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.');
$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.');
$settings
->set('role_logout', TRUE)
->save();
$role_settings
->set('enabled', TRUE)
->set('timeout', 200)
->save();
$this
->assertAutotimeout($uid, 200, 'User timeout uses role value.');
$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.');
$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.');
$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.');
$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.');
}
protected function assertAutotimeout($uid, $expected_timeout, $message = '') {
self::assertEquals($this->container
->get('autologout.manager')
->getUserTimeout($uid), $expected_timeout, $message);
}
}