You are here

public function AutologoutAjaxTest::testStayloggedInByAjax in Automated Logout 8

Test ajax stay logged in callbacks work as expected.

File

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

Class

AutologoutAjaxTest
Test the Autologout ajax endpoints.

Namespace

Drupal\Tests\autologout\Functional

Code

public function testStayloggedInByAjax() {
  $this->moduleConfig
    ->set('timeout', 20)
    ->set('padding', 5)
    ->save();

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

  // Sleep for half the timeout.
  sleep(14);

  // Test that ajax stay logged in works.
  $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 for half the timeout again.
  sleep(14);

  // Check we are still logged in.
  $this
    ->drupalGet('node');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  self::assertTrue($this
    ->drupalUserIsLoggedIn($this->privilegedUser));

  // Logout.
  $this
    ->drupalGet('autologout_ajax_logout');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  self::assertFalse($this
    ->drupalUserIsLoggedIn($this->privilegedUser));

  // Check further requests to set last result in 403.
  $this
    ->drupalGet('autologout_ajax_set_last');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}