class SessionExistsCacheContextTest
Same name and namespace in other branches
- 11.x core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php \Drupal\Tests\system\Functional\Cache\SessionExistsCacheContextTest
- 10 core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php \Drupal\Tests\system\Functional\Cache\SessionExistsCacheContextTest
- 8.9.x core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php \Drupal\Tests\system\Functional\Cache\SessionExistsCacheContextTest
Tests the 'session.exists' cache context service.
@group Cache
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\system\Functional\Cache\SessionExistsCacheContextTest implements \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of SessionExistsCacheContextTest
File
-
core/
modules/ system/ tests/ src/ Functional/ Cache/ SessionExistsCacheContextTest.php, line 13
Namespace
Drupal\Tests\system\Functional\CacheView source
class SessionExistsCacheContextTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'session_exists_cache_context_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests \Drupal\Core\Cache\Context\SessionExistsCacheContext::getContext().
*/
public function testCacheContext() {
// 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');
}
/**
* Asserts whether a session cookie is present on the client or not.
*
* @internal
*/
public function assertSessionCookieOnClient(bool $expected_present) : void {
$this->assertEquals($expected_present, (bool) $this->getSession()
->getCookie($this->getSessionName()), 'Session cookie exists.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.