3 namespace Drupal\Tests\node\Functional;
6 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
9 * Tests the node access automatic cacheability bubbling logic.
13 * @group cacheability_safeguards
15 class NodeAccessAutoBubblingTest extends NodeTestBase {
17 use AssertPageCacheContextsAndTagsTrait;
24 public static $modules = ['node_access_test', 'node_access_test_auto_bubbling'];
29 protected function setUp() {
32 node_access_rebuild();
34 // Create some content.
35 $this->drupalCreateNode();
36 $this->drupalCreateNode();
37 $this->drupalCreateNode();
38 $this->drupalCreateNode();
42 * Tests that the node grants cache context is auto-added, only when needed.
44 * @see node_query_node_access_alter()
46 public function testNodeAccessCacheabilitySafeguard() {
47 $this->dumpHeaders = TRUE;
49 // The node grants cache context should be added automatically.
50 $this->drupalGet(new Url('node_access_test_auto_bubbling'));
51 $this->assertCacheContext('user.node_grants:view');
53 // The root user has the 'bypass node access' permission, which means the
54 // node grants cache context is not necessary.
55 $this->drupalLogin($this->rootUser);
56 $this->drupalGet(new Url('node_access_test_auto_bubbling'));
57 $this->assertNoCacheContext('user.node_grants:view');
58 $this->drupalLogout();
60 // Uninstall the module with the only hook_node_grants() implementation.
61 $this->container->get('module_installer')->uninstall(['node_access_test']);
62 $this->rebuildContainer();
64 // Because there are no node grants defined, there also is no need for the
65 // node grants cache context to be bubbled.
66 $this->drupalGet(new Url('node_access_test_auto_bubbling'));
67 $this->assertNoCacheContext('user.node_grants:view');