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 NodeAccessLanguageTest extends NodeTestBase {
public static $modules = array(
'language',
'node_access_test',
);
protected function setUp() {
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();
}
function testNodeAccess() {
$web_user = $this
->drupalCreateUser(array(
'access content',
));
$expected_node_access = array(
'view' => TRUE,
'update' => FALSE,
'delete' => FALSE,
);
$expected_node_access_no_access = array(
'view' => FALSE,
'update' => FALSE,
'delete' => FALSE,
);
$node_public_hu = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'private' => FALSE,
));
$this
->assertTrue($node_public_hu
->language()
->getId() == 'hu', '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(array(
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this
->assertTrue($node_public_no_language
->language()
->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.');
$this
->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user);
\Drupal::entityManager()
->getAccessControlHandler('node')
->resetCache();
\Drupal::state()
->set('node_access_test_secret_catalan', 1);
$node_public_ca = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'ca',
'private' => FALSE,
));
$this
->assertTrue($node_public_ca
->language()
->getId() == 'ca', '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::entityManager()
->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);
}
function testNodeAccessPrivate() {
$web_user = $this
->drupalCreateUser(array(
'access content',
));
$expected_node_access = array(
'view' => TRUE,
'update' => FALSE,
'delete' => FALSE,
);
$expected_node_access_no_access = array(
'view' => FALSE,
'update' => FALSE,
'delete' => FALSE,
);
$node_private_hu = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'private' => TRUE,
));
$this
->assertTrue($node_private_hu
->language()
->getId() == 'hu', '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(array(
'private' => TRUE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this
->assertTrue($node_private_no_language
->language()
->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.');
$this
->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user);
\Drupal::entityManager()
->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(array(
'access content',
'node test view',
));
$node_private_ca = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'ca',
'private' => TRUE,
));
$this
->assertTrue($node_private_ca
->language()
->getId() == 'ca', '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::entityManager()
->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);
}
function testNodeAccessQueryTag() {
$web_user = $this
->drupalCreateUser(array(
'access content',
));
$admin_user = User::load(1);
$node_private = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'private' => TRUE,
));
$this
->assertTrue($node_private
->language()
->getId() == 'hu', 'Node created as Hungarian.');
$node_public = $this
->drupalCreateNode(array(
'body' => array(
array(),
),
'langcode' => 'hu',
'private' => FALSE,
));
$this
->assertTrue($node_public
->language()
->getId() == 'hu', 'Node created as Hungarian.');
$node_no_language = $this
->drupalCreateNode(array(
'private' => FALSE,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this
->assertTrue($node_no_language
->language()
->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $web_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 2, 'db_select() returns 2 node');
$this
->assertTrue(array_key_exists($node_public
->id(), $nids), 'Returned node ID is public node.');
$this
->assertTrue(array_key_exists($node_no_language
->id(), $nids), 'Returned node ID is no language node.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $web_user)
->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', $admin_user)
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 3, 'db_select() returns all three nodes.');
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->addMetaData('account', $admin_user)
->addMetaData('langcode', 'de')
->addTag('node_access');
$nids = $select
->execute()
->fetchAllAssoc('nid');
$this
->assertEqual(count($nids), 3, 'db_select() returns all three nodes.');
}
}