function NodeAccessLanguageTest::testNodeAccess

Same name in other branches
  1. 9 core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccess()
  2. 10 core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccess()
  3. 11.x core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php \Drupal\Tests\node\Kernel\NodeAccessLanguageTest::testNodeAccess()

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

File

core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php, line 47

Class

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

Namespace

Drupal\Tests\node\Kernel

Code

public function testNodeAccess() {
    $web_user = $this->drupalCreateUser([
        'access content',
    ]);
    $expected_node_access = [
        'view' => TRUE,
        'update' => FALSE,
        'delete' => FALSE,
    ];
    $expected_node_access_no_access = [
        '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([
        'body' => [
            [],
        ],
        '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([
        '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::entityTypeManager()->getAccessControlHandler('node')
        ->resetCache();
    \Drupal::state()->set('node_access_test_secret_catalan', 1);
    $node_public_ca = $this->drupalCreateNode([
        'body' => [
            [],
        ],
        '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::entityTypeManager()->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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.