function NodeAccessLanguageTest::testNodeAccessPrivate

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

Tests node access with multiple node languages and private nodes.

File

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

Class

NodeAccessLanguageTest
Tests multilingual node access with a module that is not language-aware.

Namespace

Drupal\Tests\node\Kernel

Code

public function testNodeAccessPrivate() : void {
    $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 private node with langcode Hungarian, will be saved as the
    // fallback in node access table.
    $node_private_hu = $this->drupalCreateNode([
        'body' => [
            [],
        ],
        'langcode' => 'hu',
        'private' => TRUE,
    ]);
    $this->assertSame('hu', $node_private_hu->language()
        ->getId(), '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([
        'private' => TRUE,
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $this->assertSame(LanguageInterface::LANGCODE_NOT_SPECIFIED, $node_private_no_language->language()
        ->getId(), '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::entityTypeManager()->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([
        'access content',
        'node test view',
    ]);
    $node_private_ca = $this->drupalCreateNode([
        'body' => [
            [],
        ],
        'langcode' => 'ca',
        'private' => TRUE,
    ]);
    $this->assertSame('ca', $node_private_ca->language()
        ->getId(), '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::entityTypeManager()->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);
}

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