class WorkspaceCacheContextTest
Same name and namespace in other branches
- 11.x core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php \Drupal\Tests\workspaces\Functional\WorkspaceCacheContextTest
- 10 core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php \Drupal\Tests\workspaces\Functional\WorkspaceCacheContextTest
- 8.9.x core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php \Drupal\Tests\workspaces\Functional\WorkspaceCacheContextTest
Tests the workspace cache context.
@group workspaces @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\workspaces\Functional\WorkspaceCacheContextTest uses \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait implements \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of WorkspaceCacheContextTest
File
-
core/
modules/ workspaces/ tests/ src/ Functional/ WorkspaceCacheContextTest.php, line 16
Namespace
Drupal\Tests\workspaces\FunctionalView source
class WorkspaceCacheContextTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'node',
'workspaces',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests the 'workspace' cache context.
*/
public function testWorkspaceCacheContext() {
$renderer = \Drupal::service('renderer');
$cache_contexts_manager = \Drupal::service("cache_contexts_manager");
// Check that the 'workspace' cache context is present when the module is
// installed.
$this->drupalGet('<front>');
$this->assertCacheContext('workspace');
$cache_context = new WorkspaceCacheContext(\Drupal::service('workspaces.manager'));
$this->assertSame('live', $cache_context->getContext());
// Create a node and check that its render array contains the proper cache
// context.
$this->drupalCreateContentType([
'type' => 'page',
]);
$node = $this->createNode();
// Get a fully built entity view render array.
$build = \Drupal::entityTypeManager()->getViewBuilder('node')
->view($node, 'full');
// Render it so the default cache contexts are applied.
$renderer->renderRoot($build);
$this->assertContains('workspace', $build['#cache']['contexts']);
$cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])
->getKeys());
$this->assertContains('[workspace]=live', $cid_parts);
// Test that a cache entry is created.
$cid = implode(':', $cid_parts);
$bin = $build['#cache']['bin'];
$this->assertInstanceOf(\stdClass::class, $this->container
->get('cache.' . $bin)
->get($cid));
// Switch to the 'stage' workspace and check that the correct workspace
// cache context is used.
$test_user = $this->drupalCreateUser([
'view any workspace',
]);
$this->drupalLogin($test_user);
$stage = Workspace::load('stage');
$workspace_manager = \Drupal::service('workspaces.manager');
$workspace_manager->setActiveWorkspace($stage);
$cache_context = new WorkspaceCacheContext($workspace_manager);
$this->assertSame('stage', $cache_context->getContext());
$build = \Drupal::entityTypeManager()->getViewBuilder('node')
->view($node, 'full');
// Render it so the default cache contexts are applied.
$renderer->renderRoot($build);
$this->assertContains('workspace', $build['#cache']['contexts']);
$cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])
->getKeys());
$this->assertContains('[workspace]=stage', $cid_parts);
// Test that a cache entry is created.
$cid = implode(':', $cid_parts);
$bin = $build['#cache']['bin'];
$this->assertInstanceOf(\stdClass::class, $this->container
->get('cache.' . $bin)
->get($cid));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.