3 namespace Drupal\Tests\node\Functional;
5 use Drupal\node\Entity\Node;
8 * Tests hook_node_access_records when acquiring grants.
12 class NodeAccessRecordsTest extends NodeTestBase {
15 * Enable a module that implements node access API hooks and alter hook.
19 public static $modules = ['node_test'];
22 * Creates a node and tests the creation of node access rules.
24 public function testNodeAccessRecords() {
25 // Create an article node.
26 $node1 = $this->drupalCreateNode(['type' => 'article']);
27 $this->assertTrue(Node::load($node1->id()), 'Article node created.');
29 // Check to see if grants added by node_test_node_access_records made it in.
30 $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', [':nid' => $node1->id()])->fetchAll();
31 $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
32 $this->assertEqual($records[0]->realm, 'test_article_realm', 'Grant with article_realm acquired for node without alteration.');
33 $this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');
35 // Create an unpromoted "Basic page" node.
36 $node2 = $this->drupalCreateNode(['type' => 'page', 'promote' => 0]);
37 $this->assertTrue(Node::load($node2->id()), 'Unpromoted basic page node created.');
39 // Check to see if grants added by node_test_node_access_records made it in.
40 $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', [':nid' => $node2->id()])->fetchAll();
41 $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
42 $this->assertEqual($records[0]->realm, 'test_page_realm', 'Grant with page_realm acquired for node without alteration.');
43 $this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');
45 // Create an unpromoted, unpublished "Basic page" node.
46 $node3 = $this->drupalCreateNode(['type' => 'page', 'promote' => 0, 'status' => 0]);
47 $this->assertTrue(Node::load($node3->id()), 'Unpromoted, unpublished basic page node created.');
49 // Check to see if grants added by node_test_node_access_records made it in.
50 $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', [':nid' => $node3->id()])->fetchAll();
51 $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
52 $this->assertEqual($records[0]->realm, 'test_page_realm', 'Grant with page_realm acquired for node without alteration.');
53 $this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');
55 // Create a promoted "Basic page" node.
56 $node4 = $this->drupalCreateNode(['type' => 'page', 'promote' => 1]);
57 $this->assertTrue(Node::load($node4->id()), 'Promoted basic page node created.');
59 // Check to see if grant added by node_test_node_access_records was altered
60 // by node_test_node_access_records_alter.
61 $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', [':nid' => $node4->id()])->fetchAll();
62 $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
63 $this->assertEqual($records[0]->realm, 'test_alter_realm', 'Altered grant with alter_realm acquired for node.');
64 $this->assertEqual($records[0]->gid, 2, 'Altered grant with gid = 2 acquired for node.');
66 // Check to see if we can alter grants with hook_node_grants_alter().
67 $operations = ['view', 'update', 'delete'];
68 // Create a user that is allowed to access content.
69 $web_user = $this->drupalCreateUser(['access content']);
70 foreach ($operations as $op) {
71 $grants = node_test_node_grants($web_user, $op);
72 $altered_grants = $grants;
73 \Drupal::moduleHandler()->alter('node_grants', $altered_grants, $web_user, $op);
74 $this->assertNotEqual($grants, $altered_grants, format_string('Altered the %op grant for a user.', ['%op' => $op]));
77 // Check that core does not grant access to an unpublished node when an
78 // empty $grants array is returned.
79 $node6 = $this->drupalCreateNode(['status' => 0, 'disable_node_access' => TRUE]);
80 $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', [':nid' => $node6->id()])->fetchAll();
81 $this->assertEqual(count($records), 0, 'Returned no records for unpublished node.');