You are here

protected function NodeEditFormTest::checkVariousAuthoredByValues in Drupal 8

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

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
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertRaw(t('There are no entities matching "%name".', [
    '%name' => '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
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, t('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
    ->assertFieldByName($form_element_name, $expected, 'Authored by field displays the correct value for the anonymous user.');

  // Change the authored by field to another user's name (that is not
  // logged in).
  $edit[$form_element_name] = $this->webUser
    ->getAccountName();
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = $this->nodeStorage
    ->load($node
    ->id());
  $this
    ->assertIdentical($node
    ->getOwnerId(), $this->webUser
    ->id(), 'Node authored by normal user.');
}