function NodeRevisionsUiTest::testNodeFormSaveWithoutRevision

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

Checks that unchecking 'Create new revision' works when editing a node.

File

core/modules/node/tests/src/Functional/NodeRevisionsUiTest.php, line 45

Class

NodeRevisionsUiTest
Tests the UI for controlling node revision behavior.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeFormSaveWithoutRevision() {
    $this->drupalLogin($this->editor);
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    // Set page revision setting 'create new revision'. This will mean new
    // revisions are created by default when the node is edited.
    $type = NodeType::load('page');
    $type->setNewRevision(TRUE);
    $type->save();
    // Create the node.
    $node = $this->drupalCreateNode();
    // Verify the checkbox is checked on the node edit form.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked");
    // Uncheck the create new revision checkbox and save the node.
    $edit = [
        'revision' => FALSE,
    ];
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
    // Load the node again and check the revision is the same as before.
    $node_storage->resetCache([
        $node->id(),
    ]);
    $node_revision = $node_storage->load($node->id(), TRUE);
    $this->assertEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' unchecked, a new revision is not created.");
    // Verify the checkbox is checked on the node edit form.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked");
    // Submit the form without changing the checkbox.
    $edit = [];
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
    // Load the node again and check the revision is different from before.
    $node_storage->resetCache([
        $node->id(),
    ]);
    $node_revision = $node_storage->load($node->id());
    $this->assertNotEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created.");
}

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