public function NodeEditFormTest::testNodeEditAuthoredBy in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/node/src/Tests/NodeEditFormTest.php \Drupal\node\Tests\NodeEditFormTest::testNodeEditAuthoredBy()
Tests changing a node's "authored by" field.
File
- core/
modules/ node/ src/ Tests/ NodeEditFormTest.php, line 125 - Contains \Drupal\node\Tests\NodeEditFormTest.
Class
- NodeEditFormTest
- Create a node and test node edit functionality.
Namespace
Drupal\node\TestsCode
public function testNodeEditAuthoredBy() {
$this
->drupalLogin($this->adminUser);
// Create node to edit.
$body_key = 'body[0][value]';
$edit = array();
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$this
->drupalPostForm('node/add/page', $edit, t('Save and publish'));
// Check that the node was authored by the currently logged in user.
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertIdentical($node
->getOwnerId(), $this->adminUser
->id(), '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
->assertNoFieldByName('uid[0][target_id]');
// Now test with the Autcomplete (Tags) field widget.
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
$form_display = \Drupal::entityManager()
->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
->drupalPostForm('node/' . $node
->id() . '/edit', [], t('Save and keep published'));
$this->nodeStorage
->resetCache(array(
$node
->id(),
));
$node = $this->nodeStorage
->load($node
->id());
$this
->assertIdentical($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
->drupalPostForm('node/' . $node
->id() . '/edit', [], t('Save and keep published'));
$this->nodeStorage
->resetCache(array(
$node
->id(),
));
$node = $this->nodeStorage
->load($node
->id());
$this
->assertIdentical($this->webUser
->id(), $node
->getOwner()
->id());
}