You are here

public function NodeAccessCacheabilityTest::testNodeAccessCacheContext in Drupal 9

Same name and namespace in other branches
  1. 8 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 81

Class

NodeAccessCacheabilityTest
Tests the node access automatic cacheability bubbling logic.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeAccessCacheContext() {

  // 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');
}