Updated: Comment #N

Problem/Motivation

Discovered in #2211949: Support keeping new revision id for migrate.

The form controller for nodes and custom blocks now initially call setIsNewRevision() to set the default value for the checkbox.

Later on, it is called again in prepareEntity() but only if the checkbox is checked. Which made sense before the initial call existed, but now we need to explicitly disable it instead when not checked.

Fairly easy to reproduce:

1. Make sure the save new revision checkbox in the node type settings is enabled.
2. Create a node
3. Edit now, disable checkbox, save.

Expected result: No revision has been saved, no revisions tab visible.
Actual result: Two revisions exist now.

Proposed resolution

Change the calls to pass the value of the checkbox to the function.

Remaining tasks

Fix needs to be written and a test needs to be created, that test do the steps above. Maybe a new method in NodeRevisionsTest?

User interface changes

API changes

Comments

sidharthap’s picture

Status: Active » Needs review
StatusFileSize
new601 bytes

I checked the node module and found this.
// Always use the default revision setting.
$node->setNewRevision(!empty($this->settings['options']['revision']));

Here i am attaching the patch to remove this line. I tested it on my local removing this line and it resolves the issue.

berdir’s picture

Status: Needs review » Needs work

Thanks, but this is not the right fix. that call is required for the new revision checkbox to be enabled or not based on the node tape settings.

sidharthap’s picture

Thanks @Berdir,
how about to place a else condition here if check box value is null ?

public function submit(array $form, array &$form_state) {
// Build the node object from the submitted values.
$node = parent::submit($form, $form_state);
// Save as a new revision if requested to do so.
if (!empty($form_state['values']['revision'])) {
$node->setNewRevision();
.........
}
else {
$node->setNewRevision(FALSE);
}

berdir’s picture

Yes for example, or just $node->setNewRevision(!empty($form_state['values']['revision']));

marthinal’s picture

Status: Needs work » Needs review
StatusFileSize
new1.01 KB

We need to verify if the checkbox is not checked (FALSE). Let's try this.

Schoonzie’s picture

Patch #5 works fixes the issue for me. I'll have a look at writing a test now.

Schoonzie’s picture

StatusFileSize
new2.99 KB

Test attached. This test sets the 'revision' settings for page content types to true and creates a new node. It then loads the node edit form and asserts the 'create new revision' checkbox is checked. Then it submits the node edit form with the 'Create new revision' checkbox unchecked asserts that a revision is not created.

This fails without the patch in #5.

Schoonzie’s picture

StatusFileSize
new4 KB

Attached patch with fix by marthinal and test by me.

The last submitted patch, 7: 2221789-6-tests.patch, failed testing.

berdir’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

Tests and fix looks good, thanks!

Just a bunch of coding style/naming things to fix and then this is ready to go :)

  1. +++ b/core/modules/node/lib/Drupal/node/Tests/NodeCreateNewRevisionTest.php
    @@ -0,0 +1,78 @@
    + * Definition of Drupal\node\Tests\NodeRevisionsTest.
    

    Should be Contains of \Drupal\... instead of Definition of...

  2. +++ b/core/modules/node/lib/Drupal/node/Tests/NodeCreateNewRevisionTest.php
    @@ -0,0 +1,78 @@
    +/**
    + * Tests the node revision functionality.
    + */
    +class NodeCreateNewRevisionTest extends NodeTestBase {
    

    Should we name this NodeRevisionUiTest or so, to clarify that we're testing the UI unlike the other revision tests? I think it's better to not make this too specific about creating new revisions, as we're actually testing that we're not creating one and might add more tests later on.

  3. +++ b/core/modules/node/lib/Drupal/node/Tests/NodeCreateNewRevisionTest.php
    @@ -0,0 +1,78 @@
    +  protected $nodes;
    +  protected $logs;
    

    Seem unused, so can be removed.

  4. +++ b/core/modules/node/lib/Drupal/node/Tests/NodeCreateNewRevisionTest.php
    @@ -0,0 +1,78 @@
    +    return array(
    +      'name' => 'Node create new revision',
    +      'description' => "Edit a node with revisions turned on by default and check that revisions are not created if 'Create new revision' option is unchecked.",
    +      'group' => 'Node',
    +    );
    

    Same here, then I'd say name is something "Node revision UI", or maybe Node revision form? class should in that case also be Form instead of Ui.

    The description can be shorter, we might also add more test method to this later for other examples, so make it a bit more generic. "Checks the UI for controlling node revision behavior." or so?

    @inheritdoc should also be added to setUp() and getInfo() now.

  5. +++ b/core/modules/node/lib/Drupal/node/Tests/NodeCreateNewRevisionTest.php
    @@ -0,0 +1,78 @@
    +    // Set page revision setting 'create new revision'. This will mean new
    +    // revisions are created by default when the node is edited.
    +    \Drupal::config('node.type.page')->set('settings.node.options.revision', TRUE)->save();
    

    I think this on by default but it makes sense to ensure that anyway. However, we should use the entity API to change a config entity:

    $type = entity_load('node_type', 'page');
    $type->settings['node']['options']['revision'] = TRUE;
    $type->save();
    

    I think...

  6. +++ b/core/modules/node/lib/Drupal/node/Tests/NodeCreateNewRevisionTest.php
    @@ -0,0 +1,78 @@
    +    $this->assertFieldChecked('edit-revision', 'create new revision on node edit form checked');
    

    Create... instead of create...

Schoonzie’s picture

Thanks for the feedback! I will update the test class and redo the patch.

Schoonzie’s picture

Status: Needs work » Needs review
StatusFileSize
new3.93 KB

I have updated the test class based on those suggestions and attached a patch which includes marthinal's fix.

There are some UI tests in NodeRevisionsTest but that is testing that the permissions around revisions work so I think this is OK to be in it's own class. Let me know if you think otherwise.

berdir’s picture

Thanks, looks good. Only nitpick that I was able to find is the missing leading \ in the @file docblock, it should be "Contains \Drupal\node\Tests\NodeRevisionsUiTest".

Also, When updating a patch based on a review, it is very helpful to provide an interdiff, see https://drupal.org/documentation/git/interdiff. That makes it easier for me to verify the changes you made since the last patch.

marthinal’s picture

@Schoonzie Normally we add 2 patches.

One with only the test (that should fail).

And the other that fixes this fail.

I didn't try this test but I tell you for the future :)

Many thanks for your help.

berdir’s picture

@marthinal: Yep. There's a patch with only the test in #7, but it would probably make sense to have the final patch as a test-only patch too, to proof that it still fails, but we didn't make any functional changes on the test since then I think.

Schoonzie’s picture

StatusFileSize
new2.92 KB

Thanks for the feedback again. Hopefully this is the last round.

Attached is a patch with just the test. This should fail.

I have also attached an interdiff of this patch and the patch in #7, however because the file was renamed it is not really helpful.

Schoonzie’s picture

StatusFileSize
new3.93 KB

And here is the complete patch with the fix and the tests. This should pass.

Is this type of patch needed / useful?

Schoonzie’s picture

StatusFileSize
new5.56 KB

Oops, here is the interdiff I mentioned in #16

The last submitted patch, 16: 2221789-16-tests.patch, failed testing.

heathergaye’s picture

Status: Needs review » Reviewed & tested by the community

Tested patch at #17, applies, works as described.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Nice catch, and great to have some expanded test coverage for this!

Committed and pushed to 8.x. Thanks!

  • Commit bd28b21 on 8.x by webchick:
    Issue #2221789 by Schoonzie, marthinal, sidharthap | Berdir: Not...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.