function NodeAccessFieldTest::testNodeAccessAdministerField

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

Tests administering fields when node access is restricted.

File

core/modules/node/tests/src/Functional/NodeAccessFieldTest.php, line 92

Class

NodeAccessFieldTest
Tests the interaction of the node access system with fields.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeAccessAdministerField() : void {
    // Create a page node.
    $fieldData = [];
    $value = $fieldData[0]['value'] = $this->randomMachineName();
    $node = $this->drupalCreateNode([
        $this->fieldName => $fieldData,
    ]);
    // Log in as the administrator and confirm that the field value is present.
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextContains($value);
    // Log in as the content admin and try to view the node.
    $this->drupalLogin($this->contentAdminUser);
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextContains('Access denied');
    // Modify the field default as the content admin.
    $edit = [
        'set_default_value' => '1',
    ];
    $default = 'Sometimes words have two meanings';
    $edit["default_value_input[{$this->fieldName}][0][value]"] = $default;
    $this->drupalGet("admin/structure/types/manage/page/fields/node.page.{$this->fieldName}");
    $this->submitForm($edit, 'Save settings');
    // Log in as the administrator.
    $this->drupalLogin($this->adminUser);
    // Confirm that the existing node still has the correct field value.
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextContains($value);
    // Confirm that the new default value appears when creating a new node.
    $this->drupalGet('node/add/page');
    $this->assertSession()
        ->responseContains($default);
}

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