View source
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
class NodeAccessRecordsTest extends NodeAccessTestBase {
protected static $modules = [
'node_test',
];
public function testNodeAccessRecords() {
$node1 = $this
->drupalCreateNode([
'type' => 'article',
]);
$this
->assertNotEmpty(Node::load($node1
->id()), 'Article node created.');
$connection = Database::getConnection();
$records = $connection
->select('node_access', 'na')
->fields('na', [
'realm',
'gid',
])
->condition('nid', $node1
->id())
->execute()
->fetchAll();
$this
->assertCount(1, $records, 'Returned the correct number of rows.');
$this
->assertEquals('test_article_realm', $records[0]->realm, 'Grant with article_realm acquired for node without alteration.');
$this
->assertEquals(1, $records[0]->gid, 'Grant with gid = 1 acquired for node without alteration.');
$node2 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 0,
]);
$this
->assertNotEmpty(Node::load($node2
->id()), 'Unpromoted basic page node created.');
$records = $connection
->select('node_access', 'na')
->fields('na', [
'realm',
'gid',
])
->condition('nid', $node2
->id())
->execute()
->fetchAll();
$this
->assertCount(1, $records, 'Returned the correct number of rows.');
$this
->assertEquals('test_page_realm', $records[0]->realm, 'Grant with page_realm acquired for node without alteration.');
$this
->assertEquals(1, $records[0]->gid, 'Grant with gid = 1 acquired for node without alteration.');
$node3 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 0,
'status' => 0,
]);
$this
->assertNotEmpty(Node::load($node3
->id()), 'Unpromoted, unpublished basic page node created.');
$records = $connection
->select('node_access', 'na')
->fields('na', [
'realm',
'gid',
])
->condition('nid', $node3
->id())
->execute()
->fetchAll();
$this
->assertCount(1, $records, 'Returned the correct number of rows.');
$this
->assertEquals('test_page_realm', $records[0]->realm, 'Grant with page_realm acquired for node without alteration.');
$this
->assertEquals(1, $records[0]->gid, 'Grant with gid = 1 acquired for node without alteration.');
$node4 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 1,
]);
$this
->assertNotEmpty(Node::load($node4
->id()), 'Promoted basic page node created.');
$records = $connection
->select('node_access', 'na')
->fields('na', [
'realm',
'gid',
])
->condition('nid', $node4
->id())
->execute()
->fetchAll();
$this
->assertCount(1, $records, 'Returned the correct number of rows.');
$this
->assertEquals('test_alter_realm', $records[0]->realm, 'Altered grant with alter_realm acquired for node.');
$this
->assertEquals(2, $records[0]->gid, 'Altered grant with gid = 2 acquired for node.');
$operations = [
'view',
'update',
'delete',
];
$web_user = $this
->drupalCreateUser([
'access content',
]);
foreach ($operations as $op) {
$grants = node_test_node_grants($web_user, $op);
$altered_grants = $grants;
\Drupal::moduleHandler()
->alter('node_grants', $altered_grants, $web_user, $op);
$this
->assertNotEquals($grants, $altered_grants, new FormattableMarkup('Altered the %op grant for a user.', [
'%op' => $op,
]));
}
$node6 = $this
->drupalCreateNode([
'status' => 0,
'disable_node_access' => TRUE,
]);
$records = $connection
->select('node_access', 'na')
->fields('na', [
'realm',
'gid',
])
->condition('nid', $node6
->id())
->execute()
->fetchAll();
$this
->assertCount(0, $records, 'Returned no records for unpublished node.');
}
}