comparison core/modules/node/tests/src/Functional/NodeAccessCacheabilityTest.php @ 5:12f9dff5fda9 tip

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