function WorkspaceCacheContextTest::testWorkspaceCacheContext

Same name in other branches
  1. 9 core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php \Drupal\Tests\workspaces\Functional\WorkspaceCacheContextTest::testWorkspaceCacheContext()
  2. 8.9.x core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php \Drupal\Tests\workspaces\Functional\WorkspaceCacheContextTest::testWorkspaceCacheContext()
  3. 10 core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php \Drupal\Tests\workspaces\Functional\WorkspaceCacheContextTest::testWorkspaceCacheContext()

Tests the 'workspace' cache context.

File

core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php, line 36

Class

WorkspaceCacheContextTest
Tests the workspace cache context.

Namespace

Drupal\Tests\workspaces\Functional

Code

public function testWorkspaceCacheContext() : void {
    $renderer = \Drupal::service('renderer');
    $cache_contexts_manager = \Drupal::service("cache_contexts_manager");
    
    /** @var \Drupal\Core\Cache\VariationCacheFactoryInterface $variation_cache_factory */
    $variation_cache_factory = $this->container
        ->get('variation_cache_factory');
    // 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']);
    $context_tokens = $cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])
        ->getKeys();
    $this->assertContains('[workspace]=live', $context_tokens);
    // Test that a cache entry is created.
    $cache_bin = $variation_cache_factory->get($build['#cache']['bin']);
    $this->assertInstanceOf(\stdClass::class, $cache_bin->get($build['#cache']['keys'], CacheableMetadata::createFromRenderArray($build)));
    // 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']);
    $context_tokens = $cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])
        ->getKeys();
    $this->assertContains('[workspace]=stage', $context_tokens);
    // Test that a cache entry is created.
    $cache_bin = $variation_cache_factory->get($build['#cache']['bin']);
    $this->assertInstanceOf(\stdClass::class, $cache_bin->get($build['#cache']['keys'], CacheableMetadata::createFromRenderArray($build)));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.