function NodeEditFormTest::testNodeEdit

Same name and namespace in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::testNodeEdit()
  2. 10 core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::testNodeEdit()
  3. 11.x core/modules/node/tests/src/Functional/NodeEditFormTest.php \Drupal\Tests\node\Functional\NodeEditFormTest::testNodeEdit()

Checks node edit functionality.

File

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

Class

NodeEditFormTest
Create a node and test node edit functionality.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeEdit() {
    $this->drupalLogin($this->webUser);
    $title_key = 'title[0][value]';
    $body_key = 'body[0][value]';
    // Create node to edit.
    $edit = [];
    $edit[$title_key] = $this->randomMachineName(8);
    $edit[$body_key] = $this->randomMachineName(16);
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    // Check that the node exists in the database.
    $node = $this->drupalGetNodeByTitle($edit[$title_key]);
    $this->assertNotEmpty($node, 'Node found in database.');
    // Check that "edit" link points to correct page.
    $this->clickLink('Edit');
    $this->assertSession()
        ->addressEquals($node->toUrl('edit-form'));
    // Check that the title and body fields are displayed with the correct values.
    // @todo Ideally assertLink would support HTML, but it doesn't.
    $this->assertSession()
        ->responseContains('Edit<span class="visually-hidden">(active tab)</span>');
    $this->assertSession()
        ->fieldValueEquals($title_key, $edit[$title_key]);
    $this->assertSession()
        ->fieldValueEquals($body_key, $edit[$body_key]);
    // Edit the content of the node.
    $edit = [];
    $edit[$title_key] = $this->randomMachineName(8);
    $edit[$body_key] = $this->randomMachineName(16);
    // Stay on the current page, without reloading.
    $this->submitForm($edit, 'Save');
    // Check that the title and body fields are displayed with the updated values.
    $this->assertSession()
        ->pageTextContains($edit[$title_key]);
    $this->assertSession()
        ->pageTextContains($edit[$body_key]);
    // Log in as a second administrator user.
    $second_web_user = $this->drupalCreateUser([
        '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 = [];
    $edit['title[0][value]'] = $this->randomMachineName(8);
    $edit[$body_key] = $this->randomMachineName(16);
    $edit['revision'] = TRUE;
    $this->submitForm($edit, 'Save');
    // Ensure that the node revision has been created.
    $revised_node = $this->drupalGetNodeByTitle($edit['title[0][value]'], TRUE);
    $this->assertNotSame($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->assertSame($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->assertNotSame($first_node_version->getRevisionUser()
        ->id(), $second_node_version->getRevisionUser()
        ->id(), 'Each revision has a distinct user.');
    // Check if the node revision checkbox is rendered on node edit form.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()
        ->fieldExists('edit-revision', NULL);
    // Check that details form element opens when there are errors on child
    // elements.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $edit = [];
    // This invalid date will trigger an error.
    $edit['created[0][value][date]'] = $this->randomMachineName(8);
    // Get the current amount of open details elements.
    $open_details_elements = count($this->cssSelect('details[open="open"]'));
    $this->submitForm($edit, 'Save');
    // The node author details must be open.
    $this->assertSession()
        ->responseContains('<details class="node-form-author js-form-wrapper form-wrapper" data-drupal-selector="edit-author" id="edit-author" open="open">');
    // Only one extra details element should now be open.
    $open_details_elements++;
    $this->assertCount($open_details_elements, $this->cssSelect('details[open="open"]'), 'Exactly one extra open &lt;details&gt; element found.');
    // Edit the same node, save it and verify it's unpublished after unchecking
    // the 'Published' boolean_checkbox and clicking 'Save'.
    $this->drupalGet("node/" . $node->id() . "/edit");
    $edit = [
        'status[value]' => FALSE,
    ];
    $this->submitForm($edit, 'Save');
    $this->nodeStorage
        ->resetCache([
        $node->id(),
    ]);
    $node = $this->nodeStorage
        ->load($node->id());
    $this->assertFalse($node->isPublished(), 'Node is unpublished');
}

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