NodeAccessRebuildNodeGrantsTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\node\Entity\NodeType;
  4. /**
  5. * Ensures that node access rebuild functions work correctly even
  6. * when other modules implements hook_node_grants().
  7. *
  8. * @group node
  9. */
  10. class NodeAccessRebuildNodeGrantsTest extends NodeTestBase {
  11. /**
  12. * A user to create nodes that only it has access to.
  13. *
  14. * @var \Drupal\user\UserInterface
  15. */
  16. protected $webUser;
  17. /**
  18. * A user to test the rebuild nodes feature which can't access the nodes.
  19. *
  20. * @var \Drupal\user\UserInterface
  21. */
  22. protected $adminUser;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. parent::setUp();
  28. $this->adminUser = $this->drupalCreateUser(['administer site configuration', 'access administration pages', 'access site reports']);
  29. $this->drupalLogin($this->adminUser);
  30. $this->webUser = $this->drupalCreateUser();
  31. }
  32. /**
  33. * Tests rebuilding the node access permissions table with content.
  34. */
  35. public function testNodeAccessRebuildNodeGrants() {
  36. \Drupal::service('module_installer')->install(['node_access_test']);
  37. \Drupal::state()->set('node_access_test.private', TRUE);
  38. node_access_test_add_field(NodeType::load('page'));
  39. $this->resetAll();
  40. // Create 30 nodes so that _node_access_rebuild_batch_operation() has to run
  41. // more than once.
  42. for ($i = 0; $i < 30; $i++) {
  43. $nodes[] = $this->drupalCreateNode([
  44. 'uid' => $this->webUser->id(),
  45. 'private' => [['value' => 1]],
  46. ]);
  47. }
  48. /** @var \Drupal\node\NodeGrantDatabaseStorageInterface $grant_storage */
  49. $grant_storage = \Drupal::service('node.grant_storage');
  50. // Default realm access and node records are present.
  51. foreach ($nodes as $node) {
  52. $this->assertTrue($node->private->value);
  53. $this->assertTrue($grant_storage->access($node, 'view', $this->webUser)->isAllowed(), 'Prior to rebuilding node access the grant storage returns allowed for the node author.');
  54. $this->assertTrue($grant_storage->access($node, 'view', $this->adminUser)->isAllowed(), 'Prior to rebuilding node access the grant storage returns allowed for the admin user.');
  55. }
  56. $this->assertEqual(1, \Drupal::service('node.grant_storage')->checkAll($this->webUser), 'There is an all realm access record');
  57. $this->assertTrue(\Drupal::state()->get('node.node_access_needs_rebuild'), 'Node access permissions need to be rebuilt');
  58. // Rebuild permissions.
  59. $this->drupalGet('admin/reports/status');
  60. $this->clickLink(t('Rebuild permissions'));
  61. $this->drupalPostForm(NULL, [], t('Rebuild permissions'));
  62. $this->assertText(t('The content access permissions have been rebuilt.'));
  63. // Test if the rebuild by user that cannot bypass node access and does not
  64. // have access to the nodes has been successful.
  65. $this->assertFalse($this->adminUser->hasPermission('bypass node access'));
  66. $this->assertNull(\Drupal::state()->get('node.node_access_needs_rebuild'), 'Node access permissions have been rebuilt');
  67. foreach ($nodes as $node) {
  68. $this->assertTrue($grant_storage->access($node, 'view', $this->webUser)->isAllowed(), 'After rebuilding node access the grant storage returns allowed for the node author.');
  69. $this->assertFalse($grant_storage->access($node, 'view', $this->adminUser)->isForbidden(), 'After rebuilding node access the grant storage returns forbidden for the admin user.');
  70. }
  71. $this->assertFalse(\Drupal::service('node.grant_storage')->checkAll($this->webUser), 'There is no all realm access record');
  72. // Test an anonymous node access rebuild from code.
  73. $this->drupalLogout();
  74. node_access_rebuild();
  75. foreach ($nodes as $node) {
  76. $this->assertTrue($grant_storage->access($node, 'view', $this->webUser)->isAllowed(), 'After rebuilding node access the grant storage returns allowed for the node author.');
  77. $this->assertFalse($grant_storage->access($node, 'view', $this->adminUser)->isForbidden(), 'After rebuilding node access the grant storage returns forbidden for the admin user.');
  78. }
  79. $this->assertFalse(\Drupal::service('node.grant_storage')->checkAll($this->webUser), 'There is no all realm access record');
  80. }
  81. /**
  82. * Tests rebuilding the node access permissions table with no content.
  83. */
  84. public function testNodeAccessRebuildNoAccessModules() {
  85. // Default realm access is present.
  86. $this->assertEqual(1, \Drupal::service('node.grant_storage')->count(), 'There is an all realm access record');
  87. // No need to rebuild permissions.
  88. $this->assertFalse(\Drupal::state()->get('node.node_access_needs_rebuild'), 'Node access permissions need to be rebuilt');
  89. // Rebuild permissions.
  90. $this->drupalGet('admin/reports/status');
  91. $this->clickLink(t('Rebuild permissions'));
  92. $this->drupalPostForm(NULL, [], t('Rebuild permissions'));
  93. $this->assertText(t('Content permissions have been rebuilt.'));
  94. $this->assertNull(\Drupal::state()->get('node.node_access_needs_rebuild'), 'Node access permissions have been rebuilt');
  95. // Default realm access is still present.
  96. $this->assertEqual(1, \Drupal::service('node.grant_storage')->count(), 'There is an all realm access record');
  97. }
  98. }