You are here

public function AutologoutAjaxTest::testAutologoutByAjax in Automated Logout 8

Test ajax logout callbacks work as expected.

File

tests/src/Functional/AutologoutAjaxTest.php, line 75

Class

AutologoutAjaxTest
Test the Autologout ajax endpoints.

Namespace

Drupal\Tests\autologout\Functional

Code

public function testAutologoutByAjax() {
  $this->moduleConfig
    ->set('timeout', 100)
    ->set('padding', 10)
    ->save();

  // Check that the user can access the page after login.
  $this
    ->drupalGet('node');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Test the time remaining callback works as expected.
  $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');

  // Test that ajax logout works as expected.
  $this
    ->drupalGet('autologout_ajax_logout');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Check we are now logged out.
  $this
    ->drupalGet('node');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  self::assertFalse($this
    ->drupalUserIsLoggedIn($this->privilegedUser));

  // Check further get time remaining requests return access denied.
  $this
    ->drupalGet('autologout_ajax_get_time_left');
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Check further logout requests result in access denied.
  $this
    ->drupalGet('autologout_ajax_logout');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}