You are here

function NodeAccessLanguageTest::testNodeAccess 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::testNodeAccess()

Tests node access with multiple node languages and no private nodes.

File

core/modules/node/src/Tests/NodeAccessLanguageTest.php, line 50
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 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,
  );

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

  // Tests the default access is provided for the public Hungarian node.
  $this
    ->assertNodeAccess($expected_node_access, $node_public_hu, $web_user);

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

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

  // Tests that access is granted if requested with no language.
  $this
    ->assertNodeAccess($expected_node_access, $node_public_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);
  $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.');

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

  // Tests that Hungarian node is still accessible.
  $this
    ->assertNodeAccess($expected_node_access, $node_public_hu, $web_user);
  $this
    ->assertNodeAccess($expected_node_access, $node_public_hu
    ->getTranslation('hu'), $web_user);

  // Tests that Catalan is still not accessible.
  $this
    ->assertNodeAccess($expected_node_access_no_access, $node_public_ca
    ->getTranslation('ca'), $web_user);

  // Make Catalan accessible.
  \Drupal::state()
    ->set('node_access_test_secret_catalan', 0);

  // Tests that Catalan is accessible on a node with a Catalan version as the
  // static cache has not been reset.
  $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();

  // Tests that access is granted if requested with no language.
  $this
    ->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user);
  $this
    ->assertNodeAccess($expected_node_access, $node_public_ca, $web_user);

  // Tests that Hungarian node is still accessible.
  $this
    ->assertNodeAccess($expected_node_access, $node_public_hu, $web_user);
  $this
    ->assertNodeAccess($expected_node_access, $node_public_hu
    ->getTranslation('hu'), $web_user);

  // Tests that Catalan is accessible on a node with a Catalan version.
  $this
    ->assertNodeAccess($expected_node_access, $node_public_ca
    ->getTranslation('ca'), $web_user);
}