View source
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\User;
class NodeAccessLanguageTest extends NodeAccessTestBase {
protected static $modules = [
'language',
'node_access_test',
];
protected function setUp() : void {
parent::setUp();
node_access_test_add_field(NodeType::load('page'));
node_access_rebuild();
\Drupal::state()
->set('node_access_test.private', TRUE);
ConfigurableLanguage::createFromLangcode('hu')
->save();
ConfigurableLanguage::createFromLangcode('ca')
->save();
ConfigurableLanguage::createFromLangcode('hr')
->save();
}
public function testNodeAccess() {
$web_user = $this
->drupalCreateUser([
'access content',
]);
$expected_node_access = [
'view' => TRUE,
'update' => FALSE,
'delete' => FALSE,
];
$expected_node_access_no_access = [
'view' => FALSE,
'update' => FALSE,
'delete' => FALSE,
];
$node_public_hu = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'private' => FALSE,
]);
$this
->assertSame('hu', $node_public_hu
->language()
->getId(), 'Node created as Hungarian.');
$this
->assertNodeAccess($expected_node_access, $node_public_hu, $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_hu
->getTranslation('hu'), $web_user);
$node_public_no_language = $this
->drupalCreateNode([
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->assertSame(LanguageInterface::LANGCODE_NOT_SPECIFIED, $node_public_no_language
->language()
->getId(), 'Node created with not specified language.');
$this
->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user);
\Drupal::entityTypeManager()
->getAccessControlHandler('node')
->resetCache();
\Drupal::state()
->set('node_access_test_secret_catalan', 1);
$node_public_ca = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'ca',
'private' => FALSE,
]);
$this
->assertSame('ca', $node_public_ca
->language()
->getId(), 'Node created as Catalan.');
$this
->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_public_ca, $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_hu, $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_hu
->getTranslation('hu'), $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_public_ca
->getTranslation('ca'), $web_user);
\Drupal::state()
->set('node_access_test_secret_catalan', 0);
$this
->assertNodeAccess($expected_node_access_no_access, $node_public_ca, $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_public_ca
->getTranslation('ca'), $web_user);
\Drupal::entityTypeManager()
->getAccessControlHandler('node')
->resetCache();
$this
->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_ca, $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_hu, $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_hu
->getTranslation('hu'), $web_user);
$this
->assertNodeAccess($expected_node_access, $node_public_ca
->getTranslation('ca'), $web_user);
}
public function testNodeAccessPrivate() {
$web_user = $this
->drupalCreateUser([
'access content',
]);
$expected_node_access = [
'view' => TRUE,
'update' => FALSE,
'delete' => FALSE,
];
$expected_node_access_no_access = [
'view' => FALSE,
'update' => FALSE,
'delete' => FALSE,
];
$node_private_hu = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'private' => TRUE,
]);
$this
->assertSame('hu', $node_private_hu
->language()
->getId(), 'Node created as Hungarian.');
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_hu, $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_hu
->getTranslation('hu'), $web_user);
$node_private_no_language = $this
->drupalCreateNode([
'private' => TRUE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->assertSame(LanguageInterface::LANGCODE_NOT_SPECIFIED, $node_private_no_language
->language()
->getId(), 'Node created with not specified language.');
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user);
\Drupal::entityTypeManager()
->getAccessControlHandler('node')
->resetCache();
\Drupal::state()
->set('node_access_test_secret_catalan', 1);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user);
$private_ca_user = $this
->drupalCreateUser([
'access content',
'node test view',
]);
$node_private_ca = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'ca',
'private' => TRUE,
]);
$this
->assertSame('ca', $node_private_ca
->language()
->getId(), 'Node created as Catalan.');
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_ca, $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_ca
->getTranslation('ca'), $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_ca, $private_ca_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_ca
->getTranslation('ca'), $private_ca_user);
\Drupal::entityTypeManager()
->getAccessControlHandler('node')
->resetCache();
\Drupal::state()
->set('node_access_test_secret_catalan', 0);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_ca, $web_user);
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_ca
->getTranslation('ca'), $web_user);
$this
->assertNodeAccess($expected_node_access, $node_private_ca, $private_ca_user);
$this
->assertNodeAccess($expected_node_access, $node_private_ca
->getTranslation('ca'), $private_ca_user);
}
public function testNodeAccessQueryTag() {
$web_user = $this
->drupalCreateUser([
'access content',
]);
$admin_user = User::load(1);
$node_private = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'private' => TRUE,
]);
$this
->assertSame('hu', $node_private
->language()
->getId(), 'Node created as Hungarian.');
$node_public = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'private' => FALSE,
]);
$this
->assertSame('hu', $node_public
->language()
->getId(), 'Node created as Hungarian.');
$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();
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $web_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(2, $nids, 'Query returns 2 node');
$this
->assertArrayHasKey($node_public
->id(), $nids);
$this
->assertArrayHasKey($node_no_language
->id(), $nids);
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $web_user)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertTrue(empty($nids), 'Query returns an empty result.');
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $admin_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(3, $nids, 'Query returns all three nodes.');
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $admin_user)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(3, $nids, 'Query returns all three nodes.');
}
}