function NodeAccessLanguageTest::testNodeAccessQueryTag

Same name and namespace in other branches
  1. 11.x core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccessQueryTag()
  2. 9 core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccessQueryTag()
  3. 8.9.x core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccessQueryTag()
  4. main 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 192

Class

NodeAccessLanguageTest
Tests multilingual node access with a module that is not language-aware.

Namespace

Drupal\Tests\node\Kernel

Code

public function testNodeAccessQueryTag() : void {
  // 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->assertSame('hu', $node_private->language()
    ->getId(), '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->assertSame('hu', $node_public->language()
    ->getId(), '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->assertSame(LanguageInterface::LANGCODE_NOT_SPECIFIED, $node_no_language->language()
    ->getId(), '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->assertEmpty($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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.