You are here

function NodeAccessLanguageTest::testNodeAccessPrivate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Tests/NodeAccessLanguageTest.php \Drupal\node\Tests\NodeAccessLanguageTest::testNodeAccessPrivate()

Tests node access with multiple node languages and private nodes.

File

core/modules/node/src/Tests/NodeAccessLanguageTest.php, line 120
Contains \Drupal\node\Tests\NodeAccessLanguageTest.

Class

NodeAccessLanguageTest
Tests node_access and db_select() with node_access tag functionality with multiple languages with a test node access module that is not language-aware.

Namespace

Drupal\node\Tests

Code

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,
  );

  // Creating a private node with langcode Hungarian, will be saved as the
  // fallback in node access table.
  $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.');

  // Tests the default access is not provided for the private Hungarian node.
  $this
    ->assertNodeAccess($expected_node_access_no_access, $node_private_hu, $web_user);

  // Tests that Hungarian provided specifically results in the same.
  $this
    ->assertNodeAccess($expected_node_access_no_access, $node_private_hu
    ->getTranslation('hu'), $web_user);

  // Creating a private node with no special langcode, like when no language
  // module enabled.
  $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.');

  // Tests that access is not granted if requested with no language.
  $this
    ->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user);

  // Reset the node access cache and turn on our test node access code.
  \Drupal::entityManager()
    ->getAccessControlHandler('node')
    ->resetCache();
  \Drupal::state()
    ->set('node_access_test_secret_catalan', 1);

  // Tests that access is not granted if requested with no language.
  $this
    ->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user);

  // Creating a private node with langcode Catalan to test that the
  // node_access_test_secret_catalan flag works.
  $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.');

  // Tests that Catalan is still not accessible to either user.
  $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);

  // Tests that Catalan is still not accessible for a user with no access to
  // private nodes.
  $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);

  // Tests that Catalan is accessible by a user with the permission to see
  // private nodes.
  $this
    ->assertNodeAccess($expected_node_access, $node_private_ca, $private_ca_user);
  $this
    ->assertNodeAccess($expected_node_access, $node_private_ca
    ->getTranslation('ca'), $private_ca_user);
}