View source
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\Entity\User;
use Drupal\field\Entity\FieldStorageConfig;
class NodeAccessLanguageAwareTest extends NodeAccessTestBase {
protected static $modules = [
'language',
'node_access_test_language',
];
protected $nodes = [];
protected $adminUser;
protected $webUser;
protected function setUp() : void {
parent::setUp();
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_private',
'entity_type' => 'node',
'type' => 'boolean',
'cardinality' => 1,
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'page',
'widget' => [
'type' => 'options_buttons',
],
'settings' => [
'on_label' => 'Private',
'off_label' => 'Not private',
],
])
->save();
node_access_rebuild();
$this->webUser = $this
->drupalCreateUser([
'access content',
]);
$this->adminUser = User::load(1);
ConfigurableLanguage::createFromLangcode('hu')
->save();
ConfigurableLanguage::createFromLangcode('ca')
->save();
$this->nodes['both_public'] = $node = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'field_private' => [
[
'value' => 0,
],
],
]);
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 0;
$node
->save();
$this->nodes['ca_private'] = $node = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'field_private' => [
[
'value' => 0,
],
],
]);
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 1;
$node
->save();
$this->nodes['hu_private'] = $node = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'field_private' => [
[
'value' => 1,
],
],
]);
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 0;
$node
->save();
$this->nodes['both_private'] = $node = $this
->drupalCreateNode([
'body' => [
[],
],
'langcode' => 'hu',
'field_private' => [
[
'value' => 1,
],
],
]);
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 1;
$node
->save();
$this->nodes['no_language_public'] = $this
->drupalCreateNode([
'field_private' => [
[
'value' => 0,
],
],
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this->nodes['no_language_private'] = $this
->drupalCreateNode([
'field_private' => [
[
'value' => 1,
],
],
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
}
public function testNodeAccessLanguageAware() {
$expected_node_access = [
'view' => TRUE,
'update' => FALSE,
'delete' => FALSE,
];
$expected_node_access_no_access = [
'view' => FALSE,
'update' => FALSE,
'delete' => FALSE,
];
$this
->assertNodeAccess($expected_node_access, $this->nodes['both_public'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['both_public']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['both_public']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['hu_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['hu_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['hu_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['ca_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['ca_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['ca_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['no_language_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['no_language_public'], $this->webUser);
$connection = Database::getConnection();
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $this->webUser)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(3, $nids, '$connection->select() returns 3 nodes when no langcode is specified.');
$this
->assertArrayHasKey($this->nodes['both_public']
->id(), $nids);
$this
->assertArrayHasKey($this->nodes['ca_private']
->id(), $nids);
$this
->assertArrayHasKey($this->nodes['no_language_public']
->id(), $nids);
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $this->webUser)
->addMetaData('langcode', 'hu')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(2, $nids, 'Query returns 2 nodes when the hu langcode is specified.');
$this
->assertArrayHasKey($this->nodes['both_public']
->id(), $nids);
$this
->assertArrayHasKey($this->nodes['ca_private']
->id(), $nids);
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $this->webUser)
->addMetaData('langcode', 'ca')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(2, $nids, 'Query returns 2 nodes when the hu langcode is specified.');
$this
->assertArrayHasKey($this->nodes['both_public']
->id(), $nids);
$this
->assertArrayHasKey($this->nodes['hu_private']
->id(), $nids);
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $this->webUser)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertTrue(empty($nids), 'Query returns an empty result when the de langcode is specified.');
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $this->adminUser)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(6, $nids, 'Query returns all nodes.');
$select = $connection
->select('node', 'n')
->fields('n', [
'nid',
])
->addMetaData('account', $this->adminUser)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertCount(6, $nids, 'Query returns all nodes.');
}
}