You are here

public function NodeEditFormTest::testNodeEdit in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Tests/NodeEditFormTest.php \Drupal\node\Tests\NodeEditFormTest::testNodeEdit()

Checks node edit functionality.

File

core/modules/node/src/Tests/NodeEditFormTest.php, line 60
Contains \Drupal\node\Tests\NodeEditFormTest.

Class

NodeEditFormTest
Create a node and test node edit functionality.

Namespace

Drupal\node\Tests

Code

public function testNodeEdit() {
  $this
    ->drupalLogin($this->webUser);
  $title_key = 'title[0][value]';
  $body_key = 'body[0][value]';

  // Create node to edit.
  $edit = array();
  $edit[$title_key] = $this
    ->randomMachineName(8);
  $edit[$body_key] = $this
    ->randomMachineName(16);
  $this
    ->drupalPostForm('node/add/page', $edit, t('Save'));

  // Check that the node exists in the database.
  $node = $this
    ->drupalGetNodeByTitle($edit[$title_key]);
  $this
    ->assertTrue($node, 'Node found in database.');

  // Check that "edit" link points to correct page.
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertUrl($node
    ->url('edit-form', [
    'absolute' => TRUE,
  ]));

  // Check that the title and body fields are displayed with the correct values.
  // As you see the expected link text has no HTML, but we are using
  $link_text = 'Edit<span class="visually-hidden">(active tab)</span>';

  // @todo Ideally assertLink would support HTML, but it doesn't.
  $this
    ->assertRaw($link_text, 'Edit tab found and marked active.');
  $this
    ->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
  $this
    ->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');

  // Edit the content of the node.
  $edit = array();
  $edit[$title_key] = $this
    ->randomMachineName(8);
  $edit[$body_key] = $this
    ->randomMachineName(16);

  // Stay on the current page, without reloading.
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Check that the title and body fields are displayed with the updated values.
  $this
    ->assertText($edit[$title_key], 'Title displayed.');
  $this
    ->assertText($edit[$body_key], 'Body displayed.');

  // Login as a second administrator user.
  $second_web_user = $this
    ->drupalCreateUser(array(
    'administer nodes',
    'edit any page content',
  ));
  $this
    ->drupalLogin($second_web_user);

  // Edit the same node, creating a new revision.
  $this
    ->drupalGet("node/" . $node
    ->id() . "/edit");
  $edit = array();
  $edit['title[0][value]'] = $this
    ->randomMachineName(8);
  $edit[$body_key] = $this
    ->randomMachineName(16);
  $edit['revision'] = TRUE;
  $this
    ->drupalPostForm(NULL, $edit, t('Save and keep published'));

  // Ensure that the node revision has been created.
  $revised_node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]'], TRUE);
  $this
    ->assertNotIdentical($node
    ->getRevisionId(), $revised_node
    ->getRevisionId(), 'A new revision has been created.');

  // Ensure that the node author is preserved when it was not changed in the
  // edit form.
  $this
    ->assertIdentical($node
    ->getOwnerId(), $revised_node
    ->getOwnerId(), 'The node author has been preserved.');

  // Ensure that the revision authors are different since the revisions were
  // made by different users.
  $first_node_version = node_revision_load($node
    ->getRevisionId());
  $second_node_version = node_revision_load($revised_node
    ->getRevisionId());
  $this
    ->assertNotIdentical($first_node_version
    ->getRevisionAuthor()
    ->id(), $second_node_version
    ->getRevisionAuthor()
    ->id(), 'Each revision has a distinct user.');
}