function NodeAccessCacheabilityTest::testNodeAccessCacheabilitySafeguard

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeAccessCacheabilityTest.php \Drupal\Tests\node\Functional\NodeAccessCacheabilityTest::testNodeAccessCacheabilitySafeguard()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeAccessCacheabilityTest.php \Drupal\Tests\node\Functional\NodeAccessCacheabilityTest::testNodeAccessCacheabilitySafeguard()
  3. 11.x core/modules/node/tests/src/Functional/NodeAccessCacheabilityTest.php \Drupal\Tests\node\Functional\NodeAccessCacheabilityTest::testNodeAccessCacheabilitySafeguard()

Tests that the node grants cache context is auto-added, only when needed.

See also

node_query_node_access_alter()

File

core/modules/node/tests/src/Functional/NodeAccessCacheabilityTest.php, line 62

Class

NodeAccessCacheabilityTest
Tests the node access automatic cacheability bubbling logic.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeAccessCacheabilitySafeguard() : void {
    // The node grants cache context should be added automatically.
    $this->drupalGet(new Url('node_access_test_auto_bubbling'));
    $this->assertCacheContext('user.node_grants:view');
    // The root user has the 'bypass node access' permission, which means the
    // node grants cache context is not necessary.
    $this->drupalLogin($this->rootUser);
    $this->drupalGet(new Url('node_access_test_auto_bubbling'));
    $this->assertNoCacheContext('user.node_grants:view');
    $this->drupalLogout();
    // Uninstall the module with the only hook_node_grants() implementation.
    $this->container
        ->get('module_installer')
        ->uninstall([
        'node_access_test',
    ]);
    $this->rebuildContainer();
    // Because there are no node grants defined, there also is no need for the
    // node grants cache context to be bubbled.
    $this->drupalGet(new Url('node_access_test_auto_bubbling'));
    $this->assertNoCacheContext('user.node_grants:view');
}

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