View source
<?php
namespace Drupal\Tests\autologout\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
class AutologoutAjaxTest extends BrowserTestBase {
public static $modules = [
'node',
'system_test',
'views',
'user',
'autologout',
'menu_ui',
'block',
];
protected $privilegedUser;
protected $moduleConfig;
public function setUp() {
parent::setUp();
$this->privilegedUser = $this
->drupalCreateUser([
'access content',
'administer site configuration',
'access site reports',
'access administration pages',
'bypass node access',
'administer content types',
'administer nodes',
'administer autologout',
'change own logout threshold',
]);
$this
->drupalLogin($this->privilegedUser);
$this
->config('system.site')
->set('page.front', 'node')
->save();
$this->moduleConfig = $this->container
->get('config.factory')
->getEditable('autologout.settings');
}
public function testAutologoutByAjax() {
$this->moduleConfig
->set('timeout', 100)
->set('padding', 10)
->save();
$this
->drupalGet('node');
$this
->assertSession()
->statusCodeEquals(200);
$result = Json::decode($this
->drupalGet('autologout_ajax_get_time_left'));
self::assertEquals('insert', $result[0]['command'], 'autologout_ajax_get_time_left returns an insert command for adding the jstimer onto the page');
self::assertEquals('#timer', $result[0]['selector'], 'autologout_ajax_get_time_left specifies the #timer selector.');
$remainingTime = 0;
if (!empty($result[1]['settings']['time']) && is_int($result[1]['settings']['time'])) {
$remainingTime = $result[1]['settings']['time'];
}
self::assertTrue($remainingTime > 0, 'autologout_ajax_get_time_left returns the remaining time as a positive integer');
$this
->drupalGet('autologout_ajax_logout');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('node');
$this
->assertSession()
->statusCodeEquals(200);
self::assertFalse($this
->drupalUserIsLoggedIn($this->privilegedUser));
$this
->drupalGet('autologout_ajax_get_time_left');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('autologout_ajax_logout');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testStayloggedInByAjax() {
$this->moduleConfig
->set('timeout', 20)
->set('padding', 5)
->save();
$this
->drupalGet('node');
$this
->assertSession()
->statusCodeEquals(200);
self::assertTrue($this
->drupalUserIsLoggedIn($this->privilegedUser));
sleep(14);
$result = Json::decode($this
->drupalGet('autologout_ajax_set_last'));
$this
->assertSession()
->statusCodeEquals(200);
self::assertEquals('insert', $result[0]['command'], 'autologout_ajax_set_last returns an insert command for adding the jstimer onto the page');
self::assertEquals('#timer', $result[0]['selector'], 'autologout_ajax_set_last specifies the #timer selector.');
sleep(14);
$this
->drupalGet('node');
$this
->assertSession()
->statusCodeEquals(200);
self::assertTrue($this
->drupalUserIsLoggedIn($this->privilegedUser));
$this
->drupalGet('autologout_ajax_logout');
$this
->assertSession()
->statusCodeEquals(200);
self::assertFalse($this
->drupalUserIsLoggedIn($this->privilegedUser));
$this
->drupalGet('autologout_ajax_set_last');
$this
->assertSession()
->statusCodeEquals(403);
}
}