SessionExistsCacheContextTest.php in Drupal 8
File
core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\Cache;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class SessionExistsCacheContextTest extends BrowserTestBase {
public static $modules = [
'session_exists_cache_context_test',
];
protected $defaultTheme = 'stark';
public function testCacheContext() {
$this->dumpHeaders = TRUE;
$this
->assertSessionCookieOnClient(FALSE);
$this
->drupalGet(Url::fromRoute('<front>'));
$this
->assertSessionCookieOnClient(FALSE);
$this
->assertRaw('Session does not exist!');
$this
->assertRaw('[session.exists]=0');
$this
->assertSessionCookieOnClient(FALSE);
$this
->drupalLogin($this->rootUser);
$this
->assertSessionCookieOnClient(TRUE);
$this
->assertRaw('Session exists!');
$this
->assertRaw('[session.exists]=1');
$this
->drupalLogout();
$this
->assertSessionCookieOnClient(FALSE);
$this
->assertRaw('Session does not exist!');
$this
->assertRaw('[session.exists]=0');
$this
->assertSessionCookieOnClient(FALSE);
$this
->drupalGet(Url::fromRoute('<front>', [], [
'query' => [
'trigger_session' => 1,
],
]));
$this
->assertSessionCookieOnClient(TRUE);
$this
->assertRaw('Session does not exist!');
$this
->assertRaw('[session.exists]=0');
$this
->drupalGet(Url::fromRoute('<front>'));
$this
->assertSessionCookieOnClient(TRUE);
$this
->assertRaw('Session exists!');
$this
->assertRaw('[session.exists]=1');
}
public function assertSessionCookieOnClient($expected_present) {
$this
->assertEqual($expected_present, (bool) $this
->getSession()
->getCookie($this
->getSessionName()), 'Session cookie exists.');
}
}