You are here

public function PersistentLoginTest::restartSession in Persistent Login 8

Emulates closing and re-opening of the browser.

1 call to PersistentLoginTest::restartSession()
PersistentLoginTest::testPersistentLogin in Tests/src/Functional/PersistentLoginTest.php
Tests whether a user can be persistently logged in.

File

Tests/src/Functional/PersistentLoginTest.php, line 87

Class

PersistentLoginTest
Tests the persistent login functionality.

Namespace

Drupal\Tests\persistent_login\Functional

Code

public function restartSession() {
  $cookie_jar = $this
    ->getCookieJar();

  // We are going to keep all the cookies with a valid expire time. The
  // \Symfony\Component\BrowserKit\CookieJar::all() method already removes
  // expired cookies, so we need to strip the ones with a null expire time.
  // A null expire time means that the cookie is going to be deleted when the
  // browser is closed.
  $persistent = [];
  foreach ($cookie_jar
    ->all() as $cookie) {
    if (!is_null($cookie
      ->getExpiresTime())) {
      $persistent[] = $cookie;
    }
  }
  $this
    ->getSession()
    ->restart();
  $cookie_jar = $this
    ->getCookieJar();
  foreach ($persistent as $cookie) {
    $cookie_jar
      ->set($cookie);
  }
}