protected function NodeEditFormTest::checkVariousAuthoredByValues in Drupal 10
Same name and namespace in other branches
- 8 core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::checkVariousAuthoredByValues()
- 9 core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::checkVariousAuthoredByValues()
Checks that the "authored by" works correctly with various values.
Parameters
\Drupal\node\NodeInterface $node: A node object.
string $form_element_name: The name of the form element to populate.
File
- core/
modules/ node/ tests/ src/ Functional/ NodeEditFormTest.php, line 265
Class
- NodeEditFormTest
- Create a node and test node edit functionality.
Namespace
Drupal\Tests\node\FunctionalCode
protected function checkVariousAuthoredByValues(NodeInterface $node, $form_element_name) {
// Try to change the 'authored by' field to an invalid user name.
$edit = [
$form_element_name => 'invalid-name',
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('There are no users matching "invalid-name".');
// Change the authored by field to an empty string, which should assign
// authorship to the anonymous user (uid 0).
$edit[$form_element_name] = '';
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$uid = $node
->getOwnerId();
// Most SQL database drivers stringify fetches but entities are not
// necessarily stored in a SQL database. At the same time, NULL/FALSE/""
// won't do.
$this
->assertTrue($uid === 0 || $uid === '0', 'Node authored by anonymous user.');
// Go back to the edit form and check that the correct value is displayed
// in the author widget.
$this
->drupalGet('node/' . $node
->id() . '/edit');
$anonymous_user = User::getAnonymousUser();
$expected = $anonymous_user
->label() . ' (' . $anonymous_user
->id() . ')';
$this
->assertSession()
->fieldValueEquals($form_element_name, $expected);
// Change the authored by field to another user's name (that is not
// logged in).
$edit[$form_element_name] = $this->webUser
->getAccountName();
$this
->submitForm($edit, 'Save');
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertSame($this->webUser
->id(), $node
->getOwnerId(), 'Node authored by normal user.');
}