function NodeEditFormTest::testNodeEditAuthoredBy

Same name and namespace in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::testNodeEditAuthoredBy()
  2. 10 core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::testNodeEditAuthoredBy()
  3. 11.x core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::testNodeEditAuthoredBy()

Tests changing a node's "authored by" field.

File

core/modules/node/tests/src/Functional/NodeEditFormTest.php, line 165

Class

NodeEditFormTest
Create a node and test node edit functionality.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeEditAuthoredBy() {
    $this->drupalLogin($this->adminUser);
    // Create node to edit.
    $body_key = 'body[0][value]';
    $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName(8);
    $edit[$body_key] = $this->randomMachineName(16);
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    // Check that the node was authored by the currently logged in user.
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertSame($this->adminUser
        ->id(), $node->getOwnerId(), 'Node authored by admin user.');
    $this->checkVariousAuthoredByValues($node, 'uid[0][target_id]');
    // Check that normal users cannot change the authored by information.
    $this->drupalLogin($this->webUser);
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()
        ->fieldNotExists('uid[0][target_id]');
    // Now test with the Autocomplete (Tags) field widget.
    
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
    $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')
        ->load('node.page.default');
    $widget = $form_display->getComponent('uid');
    $widget['type'] = 'entity_reference_autocomplete_tags';
    $widget['settings'] = [
        'match_operator' => 'CONTAINS',
        'size' => 60,
        'placeholder' => '',
    ];
    $form_display->setComponent('uid', $widget);
    $form_display->save();
    $this->drupalLogin($this->adminUser);
    // Save the node without making any changes.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm([], 'Save');
    $this->nodeStorage
        ->resetCache([
        $node->id(),
    ]);
    $node = $this->nodeStorage
        ->load($node->id());
    $this->assertSame($this->webUser
        ->id(), $node->getOwner()
        ->id());
    $this->checkVariousAuthoredByValues($node, 'uid[target_id]');
    // Hide the 'authored by' field from the form.
    $form_display->removeComponent('uid')
        ->save();
    // Check that saving the node without making any changes keeps the proper
    // author ID.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm([], 'Save');
    $this->nodeStorage
        ->resetCache([
        $node->id(),
    ]);
    $node = $this->nodeStorage
        ->load($node->id());
    $this->assertSame($this->webUser
        ->id(), $node->getOwner()
        ->id());
}

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