You are here

public function SessionExistsCacheContextTest::testCacheContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php \Drupal\Tests\system\Functional\Cache\SessionExistsCacheContextTest::testCacheContext()
  2. 10 core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php \Drupal\Tests\system\Functional\Cache\SessionExistsCacheContextTest::testCacheContext()

Tests \Drupal\Core\Cache\Context\SessionExistsCacheContext::getContext().

File

core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php, line 30

Class

SessionExistsCacheContextTest
Tests the 'session.exists' cache context service.

Namespace

Drupal\Tests\system\Functional\Cache

Code

public function testCacheContext() {
  $this->dumpHeaders = TRUE;

  // 1. No session (anonymous).
  $this
    ->assertSessionCookieOnClient(FALSE);
  $this
    ->drupalGet(Url::fromRoute('<front>'));
  $this
    ->assertSessionCookieOnClient(FALSE);
  $this
    ->assertSession()
    ->pageTextContains('Session does not exist!');
  $this
    ->assertSession()
    ->responseContains('[session.exists]=0');

  // 2. Session (authenticated).
  $this
    ->assertSessionCookieOnClient(FALSE);
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->assertSessionCookieOnClient(TRUE);
  $this
    ->assertSession()
    ->pageTextContains('Session exists!');
  $this
    ->assertSession()
    ->responseContains('[session.exists]=1');
  $this
    ->drupalLogout();
  $this
    ->assertSessionCookieOnClient(FALSE);
  $this
    ->assertSession()
    ->pageTextContains('Session does not exist!');
  $this
    ->assertSession()
    ->responseContains('[session.exists]=0');

  // 3. Session (anonymous).
  $this
    ->assertSessionCookieOnClient(FALSE);
  $this
    ->drupalGet(Url::fromRoute('<front>', [], [
    'query' => [
      'trigger_session' => 1,
    ],
  ]));
  $this
    ->assertSessionCookieOnClient(TRUE);
  $this
    ->assertSession()
    ->pageTextContains('Session does not exist!');
  $this
    ->assertSession()
    ->responseContains('[session.exists]=0');
  $this
    ->drupalGet(Url::fromRoute('<front>'));
  $this
    ->assertSessionCookieOnClient(TRUE);
  $this
    ->assertSession()
    ->pageTextContains('Session exists!');
  $this
    ->assertSession()
    ->responseContains('[session.exists]=1');
}