From bd28b217a7f6ac25f4b17a8f2c6768be7b1d2912 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Mon, 24 Mar 2014 08:26:44 -0700
Subject: [PATCH] Issue #2221789 by Schoonzie, marthinal, sidharthap | Berdir:
 Not possible to disable 'Save new revision' when enabled by default.

---
 .../lib/Drupal/node/NodeFormController.php    |  5 +-
 .../Drupal/node/Tests/NodeRevisionsUiTest.php | 82 +++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/node/lib/Drupal/node/Tests/NodeRevisionsUiTest.php

diff --git a/core/modules/node/lib/Drupal/node/NodeFormController.php b/core/modules/node/lib/Drupal/node/NodeFormController.php
index d562e51f6052..672c8efdccdb 100644
--- a/core/modules/node/lib/Drupal/node/NodeFormController.php
+++ b/core/modules/node/lib/Drupal/node/NodeFormController.php
@@ -349,12 +349,15 @@ public function submit(array $form, array &$form_state) {
     $node = parent::submit($form, $form_state);
 
     // Save as a new revision if requested to do so.
-    if (!empty($form_state['values']['revision'])) {
+    if (!empty($form_state['values']['revision']) && $form_state['values']['revision'] != FALSE) {
       $node->setNewRevision();
       // If a new revision is created, save the current user as revision author.
       $node->setRevisionCreationTime(REQUEST_TIME);
       $node->setRevisionAuthorId(\Drupal::currentUser()->id());
     }
+    else {
+      $node->setNewRevision(FALSE);
+    }
 
     $node->validated = TRUE;
     foreach (\Drupal::moduleHandler()->getImplementations('node_submit') as $module) {
diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsUiTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsUiTest.php
new file mode 100644
index 000000000000..a0bb54d91a91
--- /dev/null
+++ b/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsUiTest.php
@@ -0,0 +1,82 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\node\Tests\NodeRevisionsUiTest.
+ */
+
+namespace Drupal\node\Tests;
+
+/**
+ * Tests the node revision functionality.
+ */
+class NodeRevisionsUiTest extends NodeTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Node revisions UI test',
+      'description' => 'Checks the UI for controlling node revision behavior.',
+      'group' => 'Node',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function setUp() {
+    parent::setUp();
+
+    // Create and log in user.
+    $web_user = $this->drupalCreateUser(
+      array(
+        'administer nodes',
+        'edit any page content'
+      )
+    );
+
+    $this->drupalLogin($web_user);
+  }
+
+  /**
+   * Checks that unchecking 'Create new revision' works when editing a node.
+   */
+  function testNodeFormSaveWithoutRevision() {
+
+    // Set page revision setting 'create new revision'. This will mean new
+    // revisions are created by default when the node is edited.
+    $type = entity_load('node_type', 'page');
+    $type->settings['node']['options']['revision'] = 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 = array('revision' => FALSE);
+    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
+
+    // Load the node again and check the revision is the same as before.
+    $node_revision = node_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 = array();
+    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
+
+    // Load the node again and check the revision is different from before.
+    $node_revision = node_load($node->id(), TRUE);
+    $this->assertNotEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created.");
+
+  }
+}
-- 
GitLab