fa2116ab875690935955fdf75f27bbd59715eb47
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeAccessGrantsCacheContextTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 /**
6  * Tests the node access grants cache context service.
7  *
8  * @group node
9  * @group Cache
10  */
11 class NodeAccessGrantsCacheContextTest extends NodeTestBase {
12
13   /**
14    * Modules to enable.
15    *
16    * @var array
17    */
18   public static $modules = ['node_access_test'];
19
20   /**
21    * User with permission to view content.
22    */
23   protected $accessUser;
24
25   /**
26    * User without permission to view content.
27    */
28   protected $noAccessUser;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     node_access_rebuild();
37
38     // Create some content.
39     $this->drupalCreateNode();
40     $this->drupalCreateNode();
41     $this->drupalCreateNode();
42     $this->drupalCreateNode();
43
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']);
49
50     $this->userMapping = [
51       1 => $this->rootUser,
52       2 => $this->accessUser,
53       3 => $this->noAccessUser,
54     ];
55   }
56
57   /**
58    * Asserts that for each given user, the expected cache context is returned.
59    *
60    * @param array $expected
61    *   Expected values, keyed by user ID, expected cache contexts as values.
62    */
63   protected function assertUserCacheContext(array $expected) {
64     foreach ($expected as $uid => $context) {
65       if ($uid > 0) {
66         $this->drupalLogin($this->userMapping[$uid]);
67       }
68       $this->pass('Asserting cache context for user ' . $uid . '.');
69       $this->assertIdentical($context, $this->container->get('cache_context.user.node_grants')->getContext('view'));
70     }
71     $this->drupalLogout();
72   }
73
74   /**
75    * Tests NodeAccessGrantsCacheContext::getContext().
76    */
77   public function testCacheContext() {
78     $this->assertUserCacheContext([
79       0 => 'view.all:0;node_access_test_author:0;node_access_all:0',
80       1 => 'all',
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',
83     ]);
84
85     // Grant view to all nodes (because nid = 0) for users in the
86     // 'node_access_all' realm.
87     $record = [
88       'nid' => 0,
89       'gid' => 0,
90       'realm' => 'node_access_all',
91       'grant_view' => 1,
92       'grant_update' => 0,
93       'grant_delete' => 0,
94     ];
95     db_insert('node_access')->fields($record)->execute();
96
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([
101       0 => 'view.all',
102       1 => 'all',
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',
105     ]);
106
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',
112       1 => 'all',
113       2 => 'view.all',
114       3 => 'view.all:0;node_access_test_author:3',
115     ]);
116
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',
122       1 => 'all',
123       2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
124       3 => 'view.all',
125     ]);
126
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([
131       0 => 'view.all',
132       1 => 'all',
133       2 => 'view.all',
134       3 => 'view.all',
135     ]);
136   }
137
138 }