View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Database\Database;
class NodeQueryAlterTest extends NodeTestBase {
protected static $modules = [
'node_access_test',
];
protected $defaultTheme = 'stark';
protected $accessUser;
protected $noAccessUser;
protected function setUp() : void {
parent::setUp();
node_access_rebuild();
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this->accessUser = $this
->drupalCreateUser([
'access content overview',
'access content',
'node test view',
]);
$this->noAccessUser = $this
->drupalCreateUser([
'access content overview',
'access content',
]);
$this->noAccessUser2 = $this
->drupalCreateUser([
'access content overview',
'access content',
]);
}
public function testNodeQueryAlterLowLevelWithAccess() {
try {
$query = Database::getConnection()
->select('node', 'n')
->fields('n');
$query
->addTag('node_access');
$query
->addMetaData('op', 'view');
$query
->addMetaData('account', $this->accessUser);
$result = $query
->execute()
->fetchAll();
$this
->assertCount(4, $result, 'User with access can see correct nodes');
} catch (\Exception $e) {
$this
->fail('Altered query is malformed');
}
}
public function testNodeQueryAlterWithRevisions() {
try {
$query = \Drupal::entityTypeManager()
->getStorage('node')
->getQuery();
$result = $query
->accessCheck(TRUE)
->allRevisions()
->execute();
$this
->assertCount(4, $result, 'User with access can see correct nodes');
} catch (\Exception $e) {
$this
->fail('Altered query is malformed');
}
}
public function testNodeQueryAlterLowLevelNoAccess() {
try {
$query = Database::getConnection()
->select('node', 'n')
->fields('n');
$query
->addTag('node_access');
$query
->addMetaData('op', 'view');
$query
->addMetaData('account', $this->noAccessUser);
$result = $query
->execute()
->fetchAll();
$this
->assertCount(0, $result, 'User with no access cannot see nodes');
} catch (\Exception $e) {
$this
->fail('Altered query is malformed');
}
}
public function testNodeQueryAlterLowLevelEditAccess() {
try {
$query = Database::getConnection()
->select('node', 'n')
->fields('n');
$query
->addTag('node_access');
$query
->addMetaData('op', 'update');
$query
->addMetaData('account', $this->accessUser);
$result = $query
->execute()
->fetchAll();
$this
->assertCount(0, $result, 'User with view-only access cannot edit nodes');
} catch (\Exception $e) {
$this
->fail($e
->getMessage());
$this
->fail((string) $query);
$this
->fail('Altered query is malformed');
}
}
public function testNodeQueryAlterOverride() {
$record = [
'nid' => 0,
'gid' => 0,
'realm' => 'node_access_all',
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
];
$connection = Database::getConnection();
$connection
->insert('node_access')
->fields($record)
->execute();
drupal_static_reset('node_access_view_all_nodes');
try {
$query = $connection
->select('node', 'n')
->fields('n');
$query
->addTag('node_access');
$query
->addMetaData('op', 'view');
$query
->addMetaData('account', $this->noAccessUser);
$result = $query
->execute()
->fetchAll();
$this
->assertCount(0, $result, 'User view privileges are not overridden');
} catch (\Exception $e) {
$this
->fail('Altered query is malformed');
}
$this
->drupalLogin($this->noAccessUser2);
\Drupal::state()
->set('node_access_test.no_access_uid', $this->noAccessUser
->id());
drupal_static_reset('node_access_view_all_nodes');
try {
$query = $connection
->select('node', 'n')
->fields('n');
$query
->addTag('node_access');
$query
->addMetaData('op', 'view');
$query
->addMetaData('account', $this->noAccessUser);
$result = $query
->execute()
->fetchAll();
$this
->assertCount(4, $result, 'User view privileges are overridden');
} catch (\Exception $e) {
$this
->fail('Altered query is malformed');
}
\Drupal::state()
->delete('node_access_test.no_access_uid');
}
}