3 namespace Drupal\Tests\node\Functional;
6 * Tests the node access grants cache context service.
11 class NodeAccessGrantsCacheContextTest extends NodeTestBase {
18 public static $modules = ['node_access_test'];
21 * User with permission to view content.
23 protected $accessUser;
26 * User without permission to view content.
28 protected $noAccessUser;
33 protected function setUp() {
36 node_access_rebuild();
38 // Create some content.
39 $this->drupalCreateNode();
40 $this->drupalCreateNode();
41 $this->drupalCreateNode();
42 $this->drupalCreateNode();
44 // Create user with simple node access permission. The 'node test view'
45 // permission is implemented and granted by the node_access_test module.
46 $this->accessUser = $this->drupalCreateUser(['access content overview', 'access content', 'node test view']);
47 $this->noAccessUser = $this->drupalCreateUser(['access content overview', 'access content']);
48 $this->noAccessUser2 = $this->drupalCreateUser(['access content overview', 'access content']);
50 $this->userMapping = [
52 2 => $this->accessUser,
53 3 => $this->noAccessUser,
58 * Asserts that for each given user, the expected cache context is returned.
60 * @param array $expected
61 * Expected values, keyed by user ID, expected cache contexts as values.
63 protected function assertUserCacheContext(array $expected) {
64 foreach ($expected as $uid => $context) {
66 $this->drupalLogin($this->userMapping[$uid]);
68 $this->pass('Asserting cache context for user ' . $uid . '.');
69 $this->assertIdentical($context, $this->container->get('cache_context.user.node_grants')->getContext('view'));
71 $this->drupalLogout();
75 * Tests NodeAccessGrantsCacheContext::getContext().
77 public function testCacheContext() {
78 $this->assertUserCacheContext([
79 0 => 'view.all:0;node_access_test_author:0;node_access_all:0',
81 2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
82 3 => 'view.all:0;node_access_test_author:3',
85 // Grant view to all nodes (because nid = 0) for users in the
86 // 'node_access_all' realm.
90 'realm' => 'node_access_all',
95 db_insert('node_access')->fields($record)->execute();
97 // Put user accessUser (uid 0) in the realm.
98 \Drupal::state()->set('node_access_test.no_access_uid', 0);
99 drupal_static_reset('node_access_view_all_nodes');
100 $this->assertUserCacheContext([
103 2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
104 3 => 'view.all:0;node_access_test_author:3',
107 // Put user accessUser (uid 2) in the realm.
108 \Drupal::state()->set('node_access_test.no_access_uid', $this->accessUser->id());
109 drupal_static_reset('node_access_view_all_nodes');
110 $this->assertUserCacheContext([
111 0 => 'view.all:0;node_access_test_author:0',
114 3 => 'view.all:0;node_access_test_author:3',
117 // Put user noAccessUser (uid 3) in the realm.
118 \Drupal::state()->set('node_access_test.no_access_uid', $this->noAccessUser->id());
119 drupal_static_reset('node_access_view_all_nodes');
120 $this->assertUserCacheContext([
121 0 => 'view.all:0;node_access_test_author:0',
123 2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
127 // Uninstall the node_access_test module
128 $this->container->get('module_installer')->uninstall(['node_access_test']);
129 drupal_static_reset('node_access_view_all_nodes');
130 $this->assertUserCacheContext([