Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\Tests\node\Functional;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Core\Url;
|
Chris@18
|
6 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
|
Chris@18
|
7
|
Chris@18
|
8 /**
|
Chris@18
|
9 * Tests the node access automatic cacheability bubbling logic.
|
Chris@18
|
10 *
|
Chris@18
|
11 * @group node
|
Chris@18
|
12 * @group Cache
|
Chris@18
|
13 * @group cacheability_safeguards
|
Chris@18
|
14 */
|
Chris@18
|
15 class NodeAccessCacheabilityTest extends NodeTestBase {
|
Chris@18
|
16
|
Chris@18
|
17 use AssertPageCacheContextsAndTagsTrait;
|
Chris@18
|
18
|
Chris@18
|
19 /**
|
Chris@18
|
20 * Modules to enable.
|
Chris@18
|
21 *
|
Chris@18
|
22 * @var array
|
Chris@18
|
23 */
|
Chris@18
|
24 public static $modules = ['node_access_test', 'node_access_test_auto_bubbling'];
|
Chris@18
|
25
|
Chris@18
|
26 /**
|
Chris@18
|
27 * {@inheritdoc}
|
Chris@18
|
28 */
|
Chris@18
|
29 protected function setUp() {
|
Chris@18
|
30 parent::setUp();
|
Chris@18
|
31
|
Chris@18
|
32 node_access_rebuild();
|
Chris@18
|
33
|
Chris@18
|
34 // Create some content.
|
Chris@18
|
35 $this->drupalCreateNode();
|
Chris@18
|
36 $this->drupalCreateNode();
|
Chris@18
|
37 $this->drupalCreateNode();
|
Chris@18
|
38 $this->drupalCreateNode();
|
Chris@18
|
39 }
|
Chris@18
|
40
|
Chris@18
|
41 /**
|
Chris@18
|
42 * Tests that the node grants cache context is auto-added, only when needed.
|
Chris@18
|
43 *
|
Chris@18
|
44 * @see node_query_node_access_alter()
|
Chris@18
|
45 */
|
Chris@18
|
46 public function testNodeAccessCacheabilitySafeguard() {
|
Chris@18
|
47 $this->dumpHeaders = TRUE;
|
Chris@18
|
48
|
Chris@18
|
49 // The node grants cache context should be added automatically.
|
Chris@18
|
50 $this->drupalGet(new Url('node_access_test_auto_bubbling'));
|
Chris@18
|
51 $this->assertCacheContext('user.node_grants:view');
|
Chris@18
|
52
|
Chris@18
|
53 // The root user has the 'bypass node access' permission, which means the
|
Chris@18
|
54 // node grants cache context is not necessary.
|
Chris@18
|
55 $this->drupalLogin($this->rootUser);
|
Chris@18
|
56 $this->drupalGet(new Url('node_access_test_auto_bubbling'));
|
Chris@18
|
57 $this->assertNoCacheContext('user.node_grants:view');
|
Chris@18
|
58 $this->drupalLogout();
|
Chris@18
|
59
|
Chris@18
|
60 // Uninstall the module with the only hook_node_grants() implementation.
|
Chris@18
|
61 $this->container->get('module_installer')->uninstall(['node_access_test']);
|
Chris@18
|
62 $this->rebuildContainer();
|
Chris@18
|
63
|
Chris@18
|
64 // Because there are no node grants defined, there also is no need for the
|
Chris@18
|
65 // node grants cache context to be bubbled.
|
Chris@18
|
66 $this->drupalGet(new Url('node_access_test_auto_bubbling'));
|
Chris@18
|
67 $this->assertNoCacheContext('user.node_grants:view');
|
Chris@18
|
68 }
|
Chris@18
|
69
|
Chris@18
|
70 /**
|
Chris@18
|
71 * Tests that the user cache contexts are correctly set.
|
Chris@18
|
72 */
|
Chris@18
|
73 public function testNodeAccessCacheContext() {
|
Chris@18
|
74 // Create a user, with edit/delete own content permission.
|
Chris@18
|
75 $test_user1 = $this->drupalCreateUser([
|
Chris@18
|
76 'access content',
|
Chris@18
|
77 'edit own page content',
|
Chris@18
|
78 'delete own page content',
|
Chris@18
|
79 ]);
|
Chris@18
|
80
|
Chris@18
|
81 $this->drupalLogin($test_user1);
|
Chris@18
|
82
|
Chris@18
|
83 $node1 = $this->createNode(['type' => 'page']);
|
Chris@18
|
84
|
Chris@18
|
85 // User should be able to edit/delete their own content.
|
Chris@18
|
86 // Therefore after the access check in node_node_access the user cache
|
Chris@18
|
87 // context should be added.
|
Chris@18
|
88 $this->drupalGet('node/' . $node1->id() . '/edit');
|
Chris@18
|
89 $this->assertCacheContext('user');
|
Chris@18
|
90 $this->drupalGet('node/' . $node1->id() . '/delete');
|
Chris@18
|
91 $this->assertCacheContext('user');
|
Chris@18
|
92
|
Chris@18
|
93 // Create a user without edit/delete permission.
|
Chris@18
|
94 $test_user2 = $this->drupalCreateUser([
|
Chris@18
|
95 'access content',
|
Chris@18
|
96 ]);
|
Chris@18
|
97
|
Chris@18
|
98 $this->drupalLogin($test_user2);
|
Chris@18
|
99
|
Chris@18
|
100 $node2 = $this->createNode(['type' => 'page']);
|
Chris@18
|
101
|
Chris@18
|
102 // The user shouldn't have access to the node edit/delete pages.
|
Chris@18
|
103 // Therefore after the access check in node_node_access the user permissions
|
Chris@18
|
104 // cache context should be added.
|
Chris@18
|
105 $this->drupalGet('node/' . $node2->id() . '/edit');
|
Chris@18
|
106 $this->assertCacheContext('user.permissions');
|
Chris@18
|
107 $this->drupalGet('node/' . $node2->id() . '/delete');
|
Chris@18
|
108 $this->assertCacheContext('user.permissions');
|
Chris@18
|
109 }
|
Chris@18
|
110
|
Chris@18
|
111 }
|