public function NodeAccessLanguageTest::testNodeAccessQueryTag in Drupal 8
Same name and namespace in other branches
- 9 core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccessQueryTag()
- 10 core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccessQueryTag()
Tests select queries with a 'node_access' tag and langcode metadata.
File
- core/
modules/ node/ tests/ src/ Kernel/ NodeAccessLanguageTest.php, line 183
Class
- NodeAccessLanguageTest
- Tests node_access and select queries with node_access tag functionality with multiple languages with a test node access module that is not language-aware.
Namespace
Drupal\Tests\node\KernelCode
public function testNodeAccessQueryTag() {
// Create a normal authenticated user.
$web_user = $this
->drupalCreateUser([
'access content',
]);
// Load the user 1 user for later use as an admin user with permission to
// see everything.
$admin_user = User::load(1);
// Creating a private node with langcode Hungarian, will be saved as
// the fallback in node access table.
$node_private = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'private' => TRUE,
]);
$this
->assertTrue($node_private
->language()
->getId() == 'hu', 'Node created as Hungarian.');
// Creating a public node with langcode Hungarian, will be saved as
// the fallback in node access table.
$node_public = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'private' => FALSE,
]);
$this
->assertTrue($node_public
->language()
->getId() == 'hu', 'Node created as Hungarian.');
// Creating a public node with no special langcode, like when no language
// module enabled.
$node_no_language = $this
->drupalCreateNode([
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->assertTrue($node_no_language
->language()
->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.');
$connection = Database::getConnection();
// Query the nodes table as the web user with the node access tag and no
// specific langcode.
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $web_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
// The public node and no language node should be returned. Because no
// langcode is given it will use the fallback node.
$this
->assertCount(2, $nids, 'Query returns 2 node');
$this
->assertArrayHasKey($node_public
->id(), $nids);
$this
->assertArrayHasKey($node_no_language
->id(), $nids);
// Query the nodes table as the web user with the node access tag and
// langcode de.
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $web_user)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
// Because no nodes are created in German, no nodes are returned.
$this
->assertTrue(empty($nids), 'Query returns an empty result.');
// Query the nodes table as admin user (full access) with the node access
// tag and no specific langcode.
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $admin_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
// All nodes are returned.
$this
->assertCount(3, $nids, 'Query returns all three nodes.');
// Query the nodes table as admin user (full access) with the node access
// tag and langcode de.
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $admin_user)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
// All nodes are returned because node access tag is not invoked when the
// user is user 1.
$this
->assertCount(3, $nids, 'Query returns all three nodes.');
}