X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fworkspaces%2Ftests%2Fsrc%2FFunctional%2FWorkspaceCacheContextTest.php;fp=web%2Fcore%2Fmodules%2Fworkspaces%2Ftests%2Fsrc%2FFunctional%2FWorkspaceCacheContextTest.php;h=f47ae7f544d6ab12165afa926445b1b96ecc9235;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php b/web/core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php new file mode 100644 index 000000000..f47ae7f54 --- /dev/null +++ b/web/core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php @@ -0,0 +1,84 @@ +dumpHeaders = TRUE; + + $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(''); + $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->assertTrue(in_array('workspace', $build['#cache']['contexts'], TRUE)); + + $cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])->getKeys()); + $this->assertTrue(in_array('[workspace]=live', $cid_parts, TRUE)); + + // Test that a cache entry is created. + $cid = implode(':', $cid_parts); + $bin = $build['#cache']['bin']; + $this->assertTrue($this->container->get('cache.' . $bin)->get($cid), 'The entity render element has been cached.'); + + // 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->assertTrue(in_array('workspace', $build['#cache']['contexts'], TRUE)); + + $cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])->getKeys()); + $this->assertTrue(in_array('[workspace]=stage', $cid_parts, TRUE)); + } + +}