40baeb53edede3092bd29f2ec815ee8f5d45b5b6
[yaffs-website] / web / core / modules / node / tests / modules / node_access_test / node_access_test.module
1 <?php
2
3 /**
4  * @file
5  * Test module for testing the node access system.
6  *
7  * This module's functionality depends on the following state variables:
8  * - node_access_test.no_access_uid: Used in NodeQueryAlterTest to enable the
9  *   node_access_all grant realm.
10  * - node_access_test.private: When TRUE, the module controls access for nodes
11  *   with a 'private' property set, and inherits the default core access for
12  *   nodes without this flag. When FALSE, the module controls access for all
13  *   nodes.
14  * - node_access_test_secret_catalan: When set to TRUE and using the Catalan
15  *   'ca' language code, makes all Catalan content secret.
16  *
17  * @see node_access_test_node_grants()
18  * @see \Drupal\node\Tests\NodeQueryAlterTest
19  * @see \Drupal\node\Tests\NodeAccessBaseTableTest
20  */
21
22 use Drupal\Core\Session\AccountInterface;
23 use Drupal\Core\Access\AccessResult;
24 use Drupal\field\Entity\FieldStorageConfig;
25 use Drupal\field\Entity\FieldConfig;
26 use Drupal\node\NodeTypeInterface;
27 use Drupal\node\NodeInterface;
28
29 /**
30  * Implements hook_node_grants().
31  *
32  * Provides three grant realms:
33  * - node_access_test_author: Grants users view, update, and delete privileges
34  *   on nodes they have authored. Users receive a group ID matching their user
35  *   ID on this realm.
36  * - node_access_test: Grants users view privileges when they have the
37  *   'node test view' permission. Users with this permission receive two group
38  *   IDs for the realm, 8888 and 8889. Access for both realms is identical;
39  *   the second group is added so that the interaction of multiple groups on
40  *   a given grant realm can be tested in NodeAccessPagerTest.
41  * - node_access_all: Provides grants for the user whose user ID matches the
42  *   'node_access_test.no_access_uid' state variable. Access control on this
43  *   realm is not provided in this module; instead,
44  *   NodeQueryAlterTest::testNodeQueryAlterOverride() manually writes a node
45  *   access record defining the access control for this realm.
46  *
47  * @see \Drupal\node\Tests\NodeQueryAlterTest::testNodeQueryAlterOverride()
48  * @see \Drupal\node\Tests\NodeAccessPagerTest
49  * @see node_access_test.permissions.yml
50  * @see node_access_test_node_access_records()
51  */
52 function node_access_test_node_grants($account, $op) {
53   $grants = [];
54   $grants['node_access_test_author'] = [$account->id()];
55   if ($op == 'view' && $account->hasPermission('node test view', $account)) {
56     $grants['node_access_test'] = [8888, 8889];
57   }
58
59   $no_access_uid = \Drupal::state()->get('node_access_test.no_access_uid') ?: 0;
60   if ($op == 'view' && $account->id() == $no_access_uid) {
61     $grants['node_access_all'] = [0];
62   }
63   return $grants;
64 }
65
66 /**
67  * Implements hook_node_access_records().
68  *
69  * By default, records are written for all nodes. When the
70  * 'node_access_test.private' state variable is set to TRUE, records
71  * are only written for nodes with a "private" property set, which causes the
72  * Node module to write the default global view grant for nodes that are not
73  * marked private.
74  *
75  * @see \Drupal\node\Tests\NodeAccessBaseTableTest::setUp()
76  * @see node_access_test_node_grants()
77  * @see node_access_test.permissions.yml
78  */
79 function node_access_test_node_access_records(NodeInterface $node) {
80   $grants = [];
81   // For NodeAccessBaseTableTestCase, only set records for private nodes.
82   if (!\Drupal::state()->get('node_access_test.private') || $node->private->value) {
83     // Groups 8888 and 8889 for the node_access_test realm both receive a view
84     // grant for all controlled nodes. See node_access_test_node_grants().
85     $grants[] = [
86       'realm' => 'node_access_test',
87       'gid' => 8888,
88       'grant_view' => 1,
89       'grant_update' => 0,
90       'grant_delete' => 0,
91       'priority' => 0,
92     ];
93     $grants[] = [
94       'realm' => 'node_access_test',
95       'gid' => 8889,
96       'grant_view' => 1,
97       'grant_update' => 0,
98       'grant_delete' => 0,
99       'priority' => 0,
100     ];
101     // For the author realm, the group ID is equivalent to a user ID, which
102     // means there are many many groups of just 1 user.
103     $grants[] = [
104       'realm' => 'node_access_test_author',
105       'gid' => $node->getOwnerId(),
106       'grant_view' => 1,
107       'grant_update' => 1,
108       'grant_delete' => 1,
109       'priority' => 0,
110     ];
111   }
112
113   return $grants;
114 }
115
116 /**
117  * Adds the private field to a node type.
118  *
119  * @param \Drupal\node\NodeTypeInterface $type
120  *   A node type entity.
121  */
122 function node_access_test_add_field(NodeTypeInterface $type) {
123   $field_storage = FieldStorageConfig::create([
124     'field_name' => 'private',
125     'entity_type' => 'node',
126     'type' => 'integer',
127   ]);
128   $field_storage->save();
129   $field = FieldConfig::create([
130     'field_name' => 'private',
131     'entity_type' => 'node',
132     'bundle' => $type->id(),
133     'label' => 'Private',
134   ]);
135   $field->save();
136
137   // Assign widget settings for the 'default' form mode.
138   entity_get_form_display('node', $type->id(), 'default')
139     ->setComponent('private', [
140       'type' => 'number',
141     ])
142     ->save();
143 }
144
145 /**
146  * Implements hook_node_access().
147  */
148 function node_access_test_node_access(NodeInterface $node, $op, AccountInterface $account) {
149   $secret_catalan = \Drupal::state()
150     ->get('node_access_test_secret_catalan') ?: 0;
151   if ($secret_catalan && $node->language()->getId() == 'ca') {
152     // Make all Catalan content secret.
153     return AccessResult::forbidden()->setCacheMaxAge(0);
154   }
155   // No opinion.
156   return AccessResult::neutral()->setCacheMaxAge(0);
157 }