function NodeRevisionsUiTest::testNodeFormSaveWithoutRevision

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeRevisionsUiTest.php \Drupal\Tests\node\Functional\NodeRevisionsUiTest::testNodeFormSaveWithoutRevision()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeRevisionsUiTest.php \Drupal\Tests\node\Functional\NodeRevisionsUiTest::testNodeFormSaveWithoutRevision()
  3. 10 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 52

Class

NodeRevisionsUiTest
Tests the UI for controlling node revision behavior.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeFormSaveWithoutRevision() : void {
    $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->assertSession()
        ->checkboxChecked('edit-revision');
    // Uncheck the create new revision checkbox and save the node.
    $edit = [
        'revision' => FALSE,
    ];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, '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->assertEquals($node->getRevisionId(), $node_revision->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->assertSession()
        ->checkboxChecked('edit-revision');
    // Submit the form without changing the checkbox.
    $edit = [];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, '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->assertNotEquals($node->getRevisionId(), $node_revision->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.