View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\node\Entity\NodeType;
class NodeAccessRebuildNodeGrantsTest extends NodeTestBase {
protected $defaultTheme = 'stark';
protected $webUser;
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
'access administration pages',
'access site reports',
]);
$this
->drupalLogin($this->adminUser);
$this->webUser = $this
->drupalCreateUser();
}
public function testNodeAccessRebuildNodeGrants() {
\Drupal::service('module_installer')
->install([
'node_access_test',
]);
\Drupal::state()
->set('node_access_test.private', TRUE);
node_access_test_add_field(NodeType::load('page'));
$this
->resetAll();
for ($i = 0; $i < 30; $i++) {
$nodes[] = $this
->drupalCreateNode([
'uid' => $this->webUser
->id(),
'private' => [
[
'value' => 1,
],
],
]);
}
$grant_storage = \Drupal::service('node.grant_storage');
foreach ($nodes as $node) {
$this
->assertNotEmpty($node->private->value);
$this
->assertTrue($grant_storage
->access($node, 'view', $this->webUser)
->isAllowed(), 'Prior to rebuilding node access the grant storage returns allowed for the node author.');
$this
->assertTrue($grant_storage
->access($node, 'view', $this->adminUser)
->isAllowed(), 'Prior to rebuilding node access the grant storage returns allowed for the admin user.');
}
$this
->assertEquals(1, \Drupal::service('node.grant_storage')
->checkAll($this->webUser), 'There is an all realm access record');
$this
->assertTrue(\Drupal::state()
->get('node.node_access_needs_rebuild'), 'Node access permissions need to be rebuilt');
$this
->drupalGet('admin/reports/status');
$this
->clickLink('Rebuild permissions');
$this
->submitForm([], 'Rebuild permissions');
$this
->assertSession()
->pageTextContains('The content access permissions have been rebuilt.');
$this
->assertFalse($this->adminUser
->hasPermission('bypass node access'));
$this
->assertNull(\Drupal::state()
->get('node.node_access_needs_rebuild'), 'Node access permissions have been rebuilt');
foreach ($nodes as $node) {
$this
->assertTrue($grant_storage
->access($node, 'view', $this->webUser)
->isAllowed(), 'After rebuilding node access the grant storage returns allowed for the node author.');
$this
->assertFalse($grant_storage
->access($node, 'view', $this->adminUser)
->isForbidden(), 'After rebuilding node access the grant storage returns forbidden for the admin user.');
}
$this
->assertEmpty(\Drupal::service('node.grant_storage')
->checkAll($this->webUser), 'There is no all realm access record');
$this
->drupalLogout();
node_access_rebuild();
foreach ($nodes as $node) {
$this
->assertTrue($grant_storage
->access($node, 'view', $this->webUser)
->isAllowed(), 'After rebuilding node access the grant storage returns allowed for the node author.');
$this
->assertFalse($grant_storage
->access($node, 'view', $this->adminUser)
->isForbidden(), 'After rebuilding node access the grant storage returns forbidden for the admin user.');
}
$this
->assertEmpty(\Drupal::service('node.grant_storage')
->checkAll($this->webUser), 'There is no all realm access record');
}
public function testNodeAccessRebuildNoAccessModules() {
$this
->assertEquals(1, \Drupal::service('node.grant_storage')
->count(), 'There is an all realm access record');
$this
->assertNull(\Drupal::state()
->get('node.node_access_needs_rebuild'), 'Node access permissions need to be rebuilt');
$this
->drupalGet('admin/reports/status');
$this
->clickLink('Rebuild permissions');
$this
->submitForm([], 'Rebuild permissions');
$this
->assertSession()
->pageTextContains('Content permissions have been rebuilt.');
$this
->assertNull(\Drupal::state()
->get('node.node_access_needs_rebuild'), 'Node access permissions have been rebuilt');
$this
->assertEquals(1, \Drupal::service('node.grant_storage')
->count(), 'There is an all realm access record');
}
}