3 namespace Drupal\Tests\system\Functional\Cache;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests the 'session.exists' cache context service.
13 class SessionExistsCacheContextTest extends BrowserTestBase {
20 public static $modules = ['session_exists_cache_context_test'];
23 * Tests \Drupal\Core\Cache\Context\SessionExistsCacheContext::getContext().
25 public function testCacheContext() {
26 $this->dumpHeaders = TRUE;
28 // 1. No session (anonymous).
29 $this->assertSessionCookieOnClient(FALSE);
30 $this->drupalGet(Url::fromRoute('<front>'));
31 $this->assertSessionCookieOnClient(FALSE);
32 $this->assertRaw('Session does not exist!');
33 $this->assertRaw('[session.exists]=0');
35 // 2. Session (authenticated).
36 $this->assertSessionCookieOnClient(FALSE);
37 $this->drupalLogin($this->rootUser);
38 $this->assertSessionCookieOnClient(TRUE);
39 $this->assertRaw('Session exists!');
40 $this->assertRaw('[session.exists]=1');
41 $this->drupalLogout();
42 $this->assertSessionCookieOnClient(FALSE);
43 $this->assertRaw('Session does not exist!');
44 $this->assertRaw('[session.exists]=0');
46 // 3. Session (anonymous).
47 $this->assertSessionCookieOnClient(FALSE);
48 $this->drupalGet(Url::fromRoute('<front>', [], ['query' => ['trigger_session' => 1]]));
49 $this->assertSessionCookieOnClient(TRUE);
50 $this->assertRaw('Session does not exist!');
51 $this->assertRaw('[session.exists]=0');
52 $this->drupalGet(Url::fromRoute('<front>'));
53 $this->assertSessionCookieOnClient(TRUE);
54 $this->assertRaw('Session exists!');
55 $this->assertRaw('[session.exists]=1');
59 * Asserts whether a session cookie is present on the client or not.
61 public function assertSessionCookieOnClient($expected_present) {
62 $this->assertEqual($expected_present, (bool) $this->getSession()->getCookie($this->getSessionName()), 'Session cookie exists.');