function NodeAccessLanguageTest::testNodeAccessQueryTag

Tests db_select() with a 'node_access' tag and langcode metadata.

File

drupal/core/modules/node/lib/Drupal/node/Tests/NodeAccessLanguageTest.php, line 171
Definition of Drupal\node\Tests\NodeAccessLanguageTest.

Class

NodeAccessLanguageTest
Verifies node_access() functionality for multiple languages.

Namespace

Drupal\node\Tests

Code

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->langcode == '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->langcode == '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,
  ));
  $this
    ->assertTrue($node_no_language->langcode == Language::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->nid, $nids), 'Returned node ID is public node.');
  $this
    ->assertTrue(array_key_exists($node_no_language->nid, $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.');
}