NodeAccessRecordsTest.php 4.3 KB

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