You are here

public function NodeAccessGrantsCacheContextTest::testCacheContext in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Tests/NodeAccessGrantsCacheContextTest.php \Drupal\node\Tests\NodeAccessGrantsCacheContextTest::testCacheContext()

Tests NodeAccessGrantsCacheContext::getContext().

File

core/modules/node/src/Tests/NodeAccessGrantsCacheContextTest.php, line 82
Contains \Drupal\node\Tests\NodeAccessGrantsCacheContextTest.

Class

NodeAccessGrantsCacheContextTest
Tests the node access grants cache context service.

Namespace

Drupal\node\Tests

Code

public function testCacheContext() {
  $this
    ->assertUserCacheContext([
    0 => 'view.all:0;node_access_test_author:0;node_access_all:0',
    1 => 'all',
    2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
    3 => 'view.all:0;node_access_test_author:3',
  ]);

  // Grant view to all nodes (because nid = 0) for users in the
  // 'node_access_all' realm.
  $record = array(
    'nid' => 0,
    'gid' => 0,
    'realm' => 'node_access_all',
    'grant_view' => 1,
    'grant_update' => 0,
    'grant_delete' => 0,
  );
  db_insert('node_access')
    ->fields($record)
    ->execute();

  // Put user accessUser (uid 0) in the realm.
  \Drupal::state()
    ->set('node_access_test.no_access_uid', 0);
  drupal_static_reset('node_access_view_all_nodes');
  $this
    ->assertUserCacheContext([
    0 => 'view.all',
    1 => 'all',
    2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
    3 => 'view.all:0;node_access_test_author:3',
  ]);

  // Put user accessUser (uid 2) in the realm.
  \Drupal::state()
    ->set('node_access_test.no_access_uid', $this->accessUser
    ->id());
  drupal_static_reset('node_access_view_all_nodes');
  $this
    ->assertUserCacheContext([
    0 => 'view.all:0;node_access_test_author:0',
    1 => 'all',
    2 => 'view.all',
    3 => 'view.all:0;node_access_test_author:3',
  ]);

  // Put user noAccessUser (uid 3) in the realm.
  \Drupal::state()
    ->set('node_access_test.no_access_uid', $this->noAccessUser
    ->id());
  drupal_static_reset('node_access_view_all_nodes');
  $this
    ->assertUserCacheContext([
    0 => 'view.all:0;node_access_test_author:0',
    1 => 'all',
    2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
    3 => 'view.all',
  ]);

  // Uninstall the node_access_test module
  $this->container
    ->get('module_installer')
    ->uninstall([
    'node_access_test',
  ]);
  drupal_static_reset('node_access_view_all_nodes');
  $this
    ->assertUserCacheContext([
    0 => 'view.all',
    1 => 'all',
    2 => 'view.all',
    3 => 'view.all',
  ]);
}