summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxjm2016-07-27 16:28:35 (GMT)
committerxjm2016-07-27 16:28:35 (GMT)
commit4668a2605ba718eca6c21dcd72ae428179e3aeb6 (patch)
tree78cd47d45c450687741137045dc5e45993eebdc2
parent21c135fc9123e3c1f099e10877507c4d9bb9766b (diff)
Issue #2362435 by hampercm, markdorison, Alienpruts, lokapujya, Wim Leers, deepakaryan1988, amateescu, martin107, alvar0hurtad0, sdelbosc, tadityar, nlisgo, timmillwood, Palashvijay4O, googletorp, kerby70, adamzimmermann, tstoeckler, dawehner, tkoleary, Crell, legolasbo: When viewing a revision, the Quick Edit, Edit, and Delete contextual link operations are available, but should not be
-rw-r--r--core/modules/node/src/NodeViewBuilder.php19
-rw-r--r--core/modules/node/src/Tests/NodeRevisionsTest.php47
2 files changed, 61 insertions, 5 deletions
diff --git a/core/modules/node/src/NodeViewBuilder.php b/core/modules/node/src/NodeViewBuilder.php
index f4243b2..c65250e 100644
--- a/core/modules/node/src/NodeViewBuilder.php
+++ b/core/modules/node/src/NodeViewBuilder.php
@@ -146,10 +146,21 @@ class NodeViewBuilder extends EntityViewBuilder {
/** @var \Drupal\node\NodeInterface $entity */
parent::alterBuild($build, $entity, $display, $view_mode);
if ($entity->id()) {
- $build['#contextual_links']['node'] = array(
- 'route_parameters' => array('node' => $entity->id()),
- 'metadata' => array('changed' => $entity->getChangedTime()),
- );
+ if ($entity->isDefaultRevision()) {
+ $build['#contextual_links']['node'] = [
+ 'route_parameters' => ['node' => $entity->id()],
+ 'metadata' => ['changed' => $entity->getChangedTime()],
+ ];
+ }
+ else {
+ $build['#contextual_links']['node_revision'] = [
+ 'route_parameters' => [
+ 'node' => $entity->id(),
+ 'node_revision' => $entity->getRevisionId(),
+ ],
+ 'metadata' => ['changed' => $entity->getChangedTime()],
+ ];
+ }
}
}
diff --git a/core/modules/node/src/Tests/NodeRevisionsTest.php b/core/modules/node/src/Tests/NodeRevisionsTest.php
index 40f2a40..bfdd213 100644
--- a/core/modules/node/src/Tests/NodeRevisionsTest.php
+++ b/core/modules/node/src/Tests/NodeRevisionsTest.php
@@ -8,6 +8,7 @@ use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
+use Drupal\Component\Serialization\Json;
/**
* Create a node with revisions and test viewing, saving, reverting, and
@@ -34,7 +35,7 @@ class NodeRevisionsTest extends NodeTestBase {
/**
* {@inheritdoc}
*/
- public static $modules = array('node', 'datetime', 'language', 'content_translation');
+ public static $modules = ['node', 'contextual', 'datetime', 'language', 'content_translation'];
/**
* {@inheritdoc}
@@ -71,6 +72,7 @@ class NodeRevisionsTest extends NodeTestBase {
'delete page revisions',
'edit any page content',
'delete any page content',
+ 'access contextual links',
'translate any entity',
'administer content types',
)
@@ -152,6 +154,18 @@ class NodeRevisionsTest extends NodeTestBase {
// Confirm that this is the default revision.
$this->assertTrue($node->isDefaultRevision(), 'Third node revision is the default one.');
+ // Confirm that the "Edit" and "Delete" contextual links appear for the
+ // default revision.
+ $ids = ['node:node=' . $node->id() . ':changed=' . $node->getChangedTime()];
+ $json = $this->renderContextualLinks($ids, 'node/' . $node->id());
+ $this->verbose($json[$ids[0]]);
+
+ $expected = '<li class="entitynodeedit-form"><a href="' . base_path() . 'node/' . $node->id() . '/edit">Edit</a></li>';
+ $this->assertTrue(strstr($json[$ids[0]], $expected), 'The "Edit" contextual link is shown for the default revision.');
+ $expected = '<li class="entitynodedelete-form"><a href="' . base_path() . 'node/' . $node->id() . '/delete">Delete</a></li>';
+ $this->assertTrue(strstr($json[$ids[0]], $expected), 'The "Delete" contextual link is shown for the default revision.');
+
+
// Confirm that revisions revert properly.
$this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionid() . "/revert", array(), t('Revert'));
$this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.',
@@ -165,6 +179,16 @@ class NodeRevisionsTest extends NodeTestBase {
$node = node_revision_load($node->getRevisionId());
$this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the default one.');
+ // Confirm that "Edit" and "Delete" contextual links don't appear for
+ // non-default revision.
+ $ids = ['node_revision::node=' . $node->id() . '&node_revision=' . $node->getRevisionId() . ':'];
+ $json = $this->renderContextualLinks($ids, 'node/' . $node->id() . '/revisions/' . $node->getRevisionId() . '/view');
+ $this->verbose($json[$ids[0]]);
+
+ $this->assertFalse(strstr($json[$ids[0]], '<li class="entitynodeedit-form">'), 'The "Edit" contextual link is not shown for a non-default revision.');
+ $this->assertFalse(strstr($json[$ids[0]], '<li class="entitynodedelete-form">'), 'The "Delete" contextual link is not shown for a non-default revision.');
+
+
// Confirm revisions delete properly.
$this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", array(), t('Delete'));
$this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
@@ -318,6 +342,27 @@ class NodeRevisionsTest extends NodeTestBase {
}
/**
+ * Gets server-rendered contextual links for the given contextual links IDs.
+ *
+ * @param string[] $ids
+ * An array of contextual link IDs.
+ * @param string $current_path
+ * The Drupal path for the page for which the contextual links are rendered.
+ *
+ * @return string
+ * The decoded JSON response body.
+ */
+ protected function renderContextualLinks(array $ids, $current_path) {
+ $post = array();
+ for ($i = 0; $i < count($ids); $i++) {
+ $post['ids[' . $i . ']'] = $ids[$i];
+ }
+ $response = $this->drupalPost('contextual/render', 'application/json', $post, ['query' => ['destination' => $current_path]]);
+
+ return Json::decode($response);
+ }
+
+ /**
* Tests the revision translations are correctly reverted.
*/
public function testRevisionTranslationRevert() {