View source
<?php
namespace Drupal\node\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\Entity\User;
class NodeAccessLanguageAwareTest extends NodeTestBase {
public static $modules = array(
'language',
'node_access_test_language',
);
protected $nodes = array();
protected $adminUser;
protected $webUser;
protected function setUp() {
parent::setUp();
$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();
$this->webUser = $this
->drupalCreateUser(array(
'access content',
));
$this->adminUser = User::load(1);
ConfigurableLanguage::createFromLangcode('hu')
->save();
ConfigurableLanguage::createFromLangcode('ca')
->save();
$this->nodes['both_public'] = $node = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'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(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'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(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'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(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'field_private' => array(
array(
'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(array(
'field_private' => array(
array(
'value' => 0,
),
),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this->nodes['no_language_private'] = $this
->drupalCreateNode(array(
'field_private' => array(
array(
'value' => 1,
),
),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
}
function testNodeAccessLanguageAware() {
$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['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);
$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), 3, 'db_select() returns 3 nodes when no langcode is specified.');
$this
->assertTrue(array_key_exists($this->nodes['both_public']
->id(), $nids), 'The node with both translations public is returned.');
$this
->assertTrue(array_key_exists($this->nodes['ca_private']
->id(), $nids), 'The node with only the Catalan translation private is returned.');
$this
->assertTrue(array_key_exists($this->nodes['no_language_public']
->id(), $nids), 'The node with no language is returned.');
$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), 2, 'db_select() returns 2 nodes when the hu langcode is specified.');
$this
->assertTrue(array_key_exists($this->nodes['both_public']
->id(), $nids), 'The node with both translations public is returned.');
$this
->assertTrue(array_key_exists($this->nodes['ca_private']
->id(), $nids), 'The node with only the Catalan translation private is returned.');
$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), 2, 'db_select() returns 2 nodes when the hu langcode is specified.');
$this
->assertTrue(array_key_exists($this->nodes['both_public']
->id(), $nids), 'The node with both translations public is returned.');
$this
->assertTrue(array_key_exists($this->nodes['hu_private']
->id(), $nids), 'The node with only the Hungarian translation private is returned.');
$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 when the de langcode is specified.');
$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), 6, '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), 6, 'db_select() returns all nodes.');
}
}