function NodeEditFormTest::checkVariousAuthoredByValues

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::checkVariousAuthoredByValues()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::checkVariousAuthoredByValues()
  3. 11.x 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.

1 call to NodeEditFormTest::checkVariousAuthoredByValues()
NodeEditFormTest::testNodeEditAuthoredBy in core/modules/node/tests/src/Functional/NodeEditFormTest.php
Tests changing a node's "authored by" field.

File

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

Class

NodeEditFormTest
Create a node and test node edit functionality.

Namespace

Drupal\Tests\node\Functional

Code

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.');
}

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