From 37c7dfa47c715d16f20194fa1683742e02b0fd1e Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Mon, 8 Oct 2018 19:57:52 +1000 Subject: [PATCH] Issue #808730 by awm, Anushka-mp, dpagini, Jo Fitzgerald, catch, johnv, venugopp, kim.pepper, kscheirer, miro_dietiker, Berdir, alexpott: Show the Revisions tab/page even when only one revision exists --- .../src/Access/NodeRevisionAccessCheck.php | 35 +++++++++++------- .../src/Functional/NodeRevisionsTest.php | 16 +++++++- .../NodeRevisionsUiBypassAccessTest.php | 37 ++++++++++++++++++- 3 files changed, 73 insertions(+), 15 deletions(-) diff --git a/core/modules/node/src/Access/NodeRevisionAccessCheck.php b/core/modules/node/src/Access/NodeRevisionAccessCheck.php index b85d783a1d1..07f760f3738 100644 --- a/core/modules/node/src/Access/NodeRevisionAccessCheck.php +++ b/core/modules/node/src/Access/NodeRevisionAccessCheck.php @@ -119,22 +119,31 @@ class NodeRevisionAccessCheck implements AccessInterface { $this->access[$cid] = FALSE; return FALSE; } - - // There should be at least two revisions. If the vid of the given node - // and the vid of the default revision differ, then we already have two - // different revisions so there is no need for a separate database check. - // Also, if you try to revert to or delete the default revision, that's - // not good. - if ($node->isDefaultRevision() && ($this->nodeStorage->countDefaultLanguageRevisions($node) == 1 || $op == 'update' || $op == 'delete')) { - $this->access[$cid] = FALSE; - } - elseif ($account->hasPermission('administer nodes')) { + // If the revisions checkbox is selected for the content type, display the + // revisions tab. + $bundle_entity_type = $node->getEntityType()->getBundleEntityType(); + $bundle_entity = \Drupal::entityTypeManager()->getStorage($bundle_entity_type)->load($bundle); + if ($bundle_entity->shouldCreateNewRevision() && $op === 'view') { $this->access[$cid] = TRUE; } else { - // First check the access to the default revision and finally, if the - // node passed in is not the default revision then access to that, too. - $this->access[$cid] = $this->nodeAccess->access($this->nodeStorage->load($node->id()), $op, $account) && ($node->isDefaultRevision() || $this->nodeAccess->access($node, $op, $account)); + // There should be at least two revisions. If the vid of the given node + // and the vid of the default revision differ, then we already have two + // different revisions so there is no need for a separate database + // check. Also, if you try to revert to or delete the default revision, + // that's not good. + if ($node->isDefaultRevision() && ($this->nodeStorage->countDefaultLanguageRevisions($node) == 1 || $op === 'update' || $op === 'delete')) { + $this->access[$cid] = FALSE; + } + elseif ($account->hasPermission('administer nodes')) { + $this->access[$cid] = TRUE; + } + else { + // First check the access to the default revision and finally, if the + // node passed in is not the default revision then check access to + // that, too. + $this->access[$cid] = $this->nodeAccess->access($this->nodeStorage->load($node->id()), $op, $account) && ($node->isDefaultRevision() || $this->nodeAccess->access($node, $op, $account)); + } } } diff --git a/core/modules/node/tests/src/Functional/NodeRevisionsTest.php b/core/modules/node/tests/src/Functional/NodeRevisionsTest.php index 3dc0aeacd2a..9e97012feb8 100644 --- a/core/modules/node/tests/src/Functional/NodeRevisionsTest.php +++ b/core/modules/node/tests/src/Functional/NodeRevisionsTest.php @@ -239,6 +239,17 @@ class NodeRevisionsTest extends NodeTestBase { $node->save(); $this->drupalGet("node/" . $node->id() . "/revisions"); + // Verify revisions is accessible since the type has revisions enabled. + $this->assertResponse(200); + // Check initial revision is shown on the node revisions overview page. + $this->assertText('Simple revision message (EN)'); + + // Verify that delete operation is inaccessible for the default revision. + $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/delete"); + $this->assertResponse(403); + + // Verify that revert operation is inaccessible for the default revision. + $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/revert"); $this->assertResponse(403); // Create a new revision and new log message. @@ -261,7 +272,10 @@ class NodeRevisionsTest extends NodeTestBase { $node->save(); $this->drupalGet("node/" . $node->id() . "/revisions"); - $this->assertResponse(403); + // Verify revisions is accessible since the type has revisions enabled. + $this->assertResponse(200); + // Check initial revision is shown on the node revisions overview page. + $this->assertText('Simple revision message (EN)'); // Add a translation in 'DE' and create a new revision and new log message. $translation = $node->addTranslation('de'); diff --git a/core/modules/node/tests/src/Functional/NodeRevisionsUiBypassAccessTest.php b/core/modules/node/tests/src/Functional/NodeRevisionsUiBypassAccessTest.php index 186b694774e..c7a84f86d9c 100644 --- a/core/modules/node/tests/src/Functional/NodeRevisionsUiBypassAccessTest.php +++ b/core/modules/node/tests/src/Functional/NodeRevisionsUiBypassAccessTest.php @@ -70,7 +70,8 @@ class NodeRevisionsUiBypassAccessTest extends NodeTestBase { $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save'); $this->assertUrl($node->toUrl()); - $this->assertNoLink(t('Revisions')); + // Verify revisions exist since the content type has revisions enabled. + $this->assertLink(t('Revisions')); // Verify the checkbox is checked on the node edit form. $this->drupalGet('node/' . $node->id() . '/edit'); @@ -82,6 +83,40 @@ class NodeRevisionsUiBypassAccessTest extends NodeTestBase { $this->assertUrl($node->toUrl()); $this->assertLink(t('Revisions')); + + // Unset page revision setting 'create new revision'. This will mean new + // revisions are not created by default when the node is edited. + $type = NodeType::load('page'); + $type->setNewRevision(FALSE); + $type->save(); + + // Create the node. + $node = $this->drupalCreateNode(); + + // Verify the checkbox is unchecked on the node edit form. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->assertNoFieldChecked('edit-revision', "'Create new revision' checkbox is unchecked"); + // Submit the form without changing the checkbox. + $edit = []; + $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save'); + + $this->assertUrl($node->toUrl()); + // Verify that no link to revisions is displayed since the type + // has the 'create new revision' setting unset. + $this->assertNoLink(t('Revisions')); + + // Verify the checkbox is unchecked on the node edit form. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->assertNoFieldChecked('edit-revision', "'Create new revision' checkbox is unchecked"); + + // Check the 'create new revision' checkbox and save the node. + $edit = ['revision' => TRUE]; + $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save'); + + $this->assertUrl($node->toUrl()); + // Verify that the link is displayed since a new revision is created and + // the 'create new revision' checkbox on the node is checked. + $this->assertLink(t('Revisions')); } }