function NodeAccessLanguageTest::testNodeAccessQueryTag in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/node/src/Tests/NodeAccessLanguageTest.php \Drupal\node\Tests\NodeAccessLanguageTest::testNodeAccessQueryTag()
Tests db_select() with a 'node_access' tag and langcode metadata.
File
- core/
modules/ node/ src/ Tests/ NodeAccessLanguageTest.php, line 183 - Contains \Drupal\node\Tests\NodeAccessLanguageTest.
Class
- NodeAccessLanguageTest
- Tests node_access and db_select() with node_access tag functionality with multiple languages with a test node access module that is not language-aware.
Namespace
Drupal\node\TestsCode
function testNodeAccessQueryTag() {
// Create a normal authenticated user.
$web_user = $this
->drupalCreateUser(array(
'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(array(
'body' => array(
array(),
),
'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(array(
'body' => array(
array(),
),
'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(array(
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this
->assertTrue($node_no_language
->language()
->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.');
// Query the nodes table as the web user with the node access tag and no
// specific langcode.
$select = db_select('node', 'n')
->fields('n', array(
'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
->assertEqual(count($nids), 2, 'db_select() returns 2 node');
$this
->assertTrue(array_key_exists($node_public
->id(), $nids), 'Returned node ID is public node.');
$this
->assertTrue(array_key_exists($node_no_language
->id(), $nids), 'Returned node ID is no language node.');
// Query the nodes table as the web user with the node access tag and
// langcode de.
$select = db_select('node', 'n')
->fields('n', array(
'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), 'db_select() returns an empty result.');
// Query the nodes table as admin user (full access) with the node access
// tag and no specific langcode.
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $admin_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
// All nodes are returned.
$this
->assertEqual(count($nids), 3, 'db_select() returns all three nodes.');
// Query the nodes table as admin user (full access) with the node access
// tag and langcode de.
$select = db_select('node', 'n')
->fields('n', array(
'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
->assertEqual(count($nids), 3, 'db_select() returns all three nodes.');
}