View source
<?php
namespace Drupal\node\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\User;
class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
public static $modules = array(
'language',
'node_access_test_language',
'node_access_test',
);
protected $nodes = array();
protected $webUser;
protected $adminUser;
protected function setUp() {
parent::setUp();
node_access_test_add_field(NodeType::load('page'));
$field_storage = entity_create('field_storage_config', array(
'field_name' => 'field_private',
'entity_type' => 'node',
'type' => 'boolean',
'cardinality' => 1,
));
$field_storage
->save();
entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => 'page',
'widget' => array(
'type' => 'options_buttons',
),
'settings' => array(
'on_label' => 'Private',
'off_label' => 'Not private',
),
))
->save();
node_access_rebuild();
ConfigurableLanguage::createFromLangcode('hu')
->save();
ConfigurableLanguage::createFromLangcode('ca')
->save();
$this->webUser = $this
->drupalCreateUser(array(
'access content',
));
$this->adminUser = User::load(1);
$this->nodes['public_both_public'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'value' => 0,
),
),
'private' => FALSE,
));
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 0;
$node
->save();
$this->nodes['private_both_public'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'value' => 0,
),
),
'private' => TRUE,
));
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 0;
$node
->save();
$this->nodes['public_hu_private'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'value' => 1,
),
),
'private' => FALSE,
));
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 0;
$node
->save();
$this->nodes['public_ca_private'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'value' => 0,
),
),
'private' => FALSE,
));
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 1;
$node
->save();
$this->nodes['public_both_private'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'value' => 1,
),
),
'private' => FALSE,
));
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 1;
$node
->save();
$this->nodes['private_both_private'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'value' => 1,
),
),
'private' => TRUE,
));
$translation = $node
->addTranslation('ca');
$translation->title->value = $this
->randomString();
$translation->field_private->value = 1;
$node
->save();
$this->nodes['public_no_language_private'] = $this
->drupalCreateNode(array(
'field_private' => array(
array(
'value' => 1,
),
),
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this->nodes['public_no_language_public'] = $this
->drupalCreateNode(array(
'field_private' => array(
array(
'value' => 0,
),
),
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this->nodes['private_no_language_private'] = $this
->drupalCreateNode(array(
'field_private' => array(
array(
'value' => 1,
),
),
'private' => TRUE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this->nodes['private_no_language_public'] = $this
->drupalCreateNode(array(
'field_private' => array(
array(
'value' => 1,
),
),
'private' => TRUE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
}
function testNodeAccessLanguageAwareCombination() {
$expected_node_access = array(
'view' => TRUE,
'update' => FALSE,
'delete' => FALSE,
);
$expected_node_access_no_access = array(
'view' => FALSE,
'update' => FALSE,
'delete' => FALSE,
);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_both_public'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_both_public']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_both_public']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['private_both_public'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['private_both_public']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['private_both_public']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_hu_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_hu_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_hu_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_ca_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_ca_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_ca_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_both_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_both_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_both_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_both_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_both_private']
->getTranslation('hu'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_both_private']
->getTranslation('ca'), $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_no_language_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access, $this->nodes['public_no_language_public'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_no_language_private'], $this->webUser);
$this
->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_no_language_public'], $this->webUser);
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $this->webUser)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 4, 'db_select() returns 4 nodes when no langcode is specified.');
$this
->assertTrue(array_key_exists($this->nodes['public_both_public']
->id(), $nids), 'Returned node ID is full public node.');
$this
->assertTrue(array_key_exists($this->nodes['public_ca_private']
->id(), $nids), 'Returned node ID is Hungarian public only node.');
$this
->assertTrue(array_key_exists($this->nodes['private_both_public']
->id(), $nids), 'Returned node ID is both public non-language-aware private only node.');
$this
->assertTrue(array_key_exists($this->nodes['public_no_language_public']
->id(), $nids), 'Returned node ID is no language public node.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $this->webUser)
->addMetaData('langcode', 'hu')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 3, 'db_select() returns 3 nodes.');
$this
->assertTrue(array_key_exists($this->nodes['public_both_public']
->id(), $nids), 'Returned node ID is both public node.');
$this
->assertTrue(array_key_exists($this->nodes['public_ca_private']
->id(), $nids), 'Returned node ID is Hungarian public only node.');
$this
->assertTrue(array_key_exists($this->nodes['private_both_public']
->id(), $nids), 'Returned node ID is both public non-language-aware private only node.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $this->webUser)
->addMetaData('langcode', 'ca')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 3, 'db_select() returns 3 nodes.');
$this
->assertTrue(array_key_exists($this->nodes['public_both_public']
->id(), $nids), 'Returned node ID is both public node.');
$this
->assertTrue(array_key_exists($this->nodes['public_hu_private']
->id(), $nids), 'Returned node ID is Catalan public only node.');
$this
->assertTrue(array_key_exists($this->nodes['private_both_public']
->id(), $nids), 'Returned node ID is both public non-language-aware private only node.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $this->webUser)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertTrue(empty($nids), 'db_select() returns an empty result.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $this->adminUser)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 10, 'db_select() returns all nodes.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $this->adminUser)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 10, 'db_select() returns all nodes.');
}
}