function NodeAccessLanguageFallbackTest::testNodeAccessLanguageFallback

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

Tests node access fallback handling with multiple node languages.

File

core/modules/node/tests/src/Functional/NodeAccessLanguageFallbackTest.php, line 54

Class

NodeAccessLanguageFallbackTest
Tests that the node_access system stores the proper fallback marker.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeAccessLanguageFallback() : void {
    // The node_access_test module allows nodes to be marked private. We need to
    // ensure that system honors the fallback system of node access properly.
    // Note that node_access_test_language is language-sensitive and does not
    // apply to the fallback test.
    // Create one node in Hungarian and marked as private.
    $node = $this->drupalCreateNode([
        'body' => [
            [],
        ],
        'langcode' => 'hu',
        'private' => [
            [
                'value' => 1,
            ],
        ],
        'status' => 1,
    ]);
    // There should be one entry in node_access, with fallback set to hu.
    $this->checkRecords(1, 'hu');
    // Create a translation user.
    $admin = $this->drupalCreateUser([
        'bypass node access',
        'administer nodes',
        'translate any entity',
        'administer content translation',
    ]);
    $this->drupalLogin($admin);
    $this->drupalGet('node/' . $node->id() . '/translations');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Create a Catalan translation through the UI.
    $url_options = [
        'language' => \Drupal::languageManager()->getLanguage('ca'),
    ];
    $this->drupalGet('node/' . $node->id() . '/translations/add/hu/ca', $url_options);
    $this->assertSession()
        ->statusCodeEquals(200);
    // Save the form.
    $this->getSession()
        ->getPage()
        ->pressButton('Save (this translation)');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Check the node access table.
    $this->checkRecords(2, 'hu');
    // Programmatically create a translation. This process lets us check that
    // both forms and code behave in the same way.
    $storage = \Drupal::entityTypeManager()->getStorage('node');
    // Reload the node.
    $node = $storage->load(1);
    // Create an Afrikaans translation.
    $translation = $node->addTranslation('af');
    $translation->title->value = $this->randomString();
    $translation->status = 1;
    $node->save();
    // Check the node access table.
    $this->checkRecords(3, 'hu');
    // For completeness, edit the Catalan version again.
    $this->drupalGet('node/' . $node->id() . '/edit', $url_options);
    $this->assertSession()
        ->statusCodeEquals(200);
    // Save the form.
    $this->getSession()
        ->getPage()
        ->pressButton('Save (this translation)');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Check the node access table.
    $this->checkRecords(3, 'hu');
}

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