function NodeAccessCacheabilityTest::testNodeAccessCacheContext

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

Tests that the user cache contexts are correctly set.

File

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

Class

NodeAccessCacheabilityTest
Tests the node access automatic cacheability bubbling logic.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeAccessCacheContext() : void {
  // Create a user, with edit/delete own content permission.
  $test_user1 = $this->drupalCreateUser([
    'access content',
    'edit own page content',
    'delete own page content',
  ]);
  $this->drupalLogin($test_user1);
  $node1 = $this->createNode([
    'type' => 'page',
  ]);
  // User should be able to edit/delete their own content.
  // Therefore after the access check in node_node_access the user cache
  // context should be added.
  $this->drupalGet('node/' . $node1->id() . '/edit');
  $this->assertCacheContext('user');
  $this->drupalGet('node/' . $node1->id() . '/delete');
  $this->assertCacheContext('user');
  // Create a user without edit/delete permission.
  $test_user2 = $this->drupalCreateUser([
    'access content',
  ]);
  $this->drupalLogin($test_user2);
  $node2 = $this->createNode([
    'type' => 'page',
  ]);
  // The user shouldn't have access to the node edit/delete pages.
  // Therefore after the access check in node_node_access the user permissions
  // cache context should be added.
  $this->drupalGet('node/' . $node2->id() . '/edit');
  $this->assertCacheContext('user.permissions');
  $this->drupalGet('node/' . $node2->id() . '/delete');
  $this->assertCacheContext('user.permissions');
}

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