summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorarpad.rozsa2018-12-28 02:40:08 (GMT)
committerEmil Stjerneman2018-12-28 02:40:08 (GMT)
commitd5e99ddbeb65058723acbb6788907c1273221ae0 (patch)
treea21a4cef16415c70df7e3ad71357bff0490679cf
parent3b5b286c510d77a7c2d05165253a1563ae464dff (diff)
Issue #2903220 by keithdoyle9, marcoscano, arpad.rozsa, Berdir, anon: Unable to link to unpublished nodes
-rw-r--r--src/Plugin/Linkit/Matcher/NodeMatcher.php25
-rw-r--r--tests/src/Kernel/Matchers/NodeMatcherTest.php27
2 files changed, 47 insertions, 5 deletions
diff --git a/src/Plugin/Linkit/Matcher/NodeMatcher.php b/src/Plugin/Linkit/Matcher/NodeMatcher.php
index 3a1424c..f40c7c0 100644
--- a/src/Plugin/Linkit/Matcher/NodeMatcher.php
+++ b/src/Plugin/Linkit/Matcher/NodeMatcher.php
@@ -3,6 +3,7 @@
namespace Drupal\linkit\Plugin\Linkit\Matcher;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\node\NodeInterface;
/**
* Provides specific linkit matchers for the node entity type.
@@ -84,9 +85,27 @@ class NodeMatcher extends EntityMatcher {
protected function buildEntityQuery($search_string) {
$query = parent::buildEntityQuery($search_string);
- $no_access = !$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'));
- if ($this->configuration['include_unpublished'] !== TRUE || $no_access) {
- $query->condition('status', NODE_PUBLISHED);
+ if ($this->configuration['include_unpublished'] == FALSE) {
+ $query->condition('status', NodeInterface::PUBLISHED);
+ }
+ elseif (count($this->moduleHandler->getImplementations('node_grants')) === 0) {
+ if (($this->currentUser->hasPermission('bypass node access') || $this->currentUser->hasPermission('view any unpublished content'))) {
+ // User can see all content, no check necessary.
+ }
+ elseif ($this->currentUser->hasPermission('view own unpublished content')) {
+ // Users with "view own unpublished content" can see only their own.
+ if ($this->configuration['include_unpublished'] == TRUE) {
+ $or_condition = $query
+ ->orConditionGroup()
+ ->condition('status', NodeInterface::PUBLISHED)
+ ->condition('uid', $this->currentUser->id());
+ $query->condition($or_condition);
+ }
+ }
+ }
+ else {
+ // All other users should only get published results.
+ $query->condition('status', NodeInterface::PUBLISHED);
}
return $query;
diff --git a/tests/src/Kernel/Matchers/NodeMatcherTest.php b/tests/src/Kernel/Matchers/NodeMatcherTest.php
index 8783a4f..0ab5550 100644
--- a/tests/src/Kernel/Matchers/NodeMatcherTest.php
+++ b/tests/src/Kernel/Matchers/NodeMatcherTest.php
@@ -18,7 +18,7 @@ class NodeMatcherTest extends LinkitKernelTestBase {
*
* @var array
*/
- public static $modules = ['field', 'node'];
+ public static $modules = ['field', 'node', 'content_moderation', 'workflows'];
/**
* The matcher manager.
@@ -34,6 +34,7 @@ class NodeMatcherTest extends LinkitKernelTestBase {
parent::setUp();
$this->installEntitySchema('node');
+ $this->installSchema('node', ['node_access']);
$this->installConfig(['field', 'node']);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
@@ -74,7 +75,7 @@ class NodeMatcherTest extends LinkitKernelTestBase {
]);
$node->save();
- // Unpublished node.
+ // Unpublished nodes.
$node = Node::create([
'title' => 'Lorem unpublishd',
'type' => $type1->id(),
@@ -82,6 +83,13 @@ class NodeMatcherTest extends LinkitKernelTestBase {
]);
$node->save();
+ $node = Node::create([
+ 'title' => 'Lorem unpublishd 2',
+ 'type' => $type2->id(),
+ 'status' => FALSE,
+ ]);
+ $node->save();
+
// Set the current user to someone that is not the node owner.
\Drupal::currentUser()->setAccount($this->createUser([], ['access content']));
}
@@ -133,7 +141,22 @@ class NodeMatcherTest extends LinkitKernelTestBase {
// Test with permissions to see unpublished nodes.
$suggestions = $plugin->execute('Lorem');
+ $this->assertEquals(5, count($suggestions->getSuggestions()), 'Correct number of suggestions');
+
+ // Test with permissions to see own unpublished nodes.
+ \Drupal::currentUser()->setAccount($this->createUser([], ['access content', 'view own unpublished content']));
+ $nodes = $this->container->get('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Lorem unpublishd']);
+ $node4 = reset($nodes);
+ /** @var \Drupal\node\NodeInterface $node4 */
+ $node4->setOwnerId(\Drupal::currentUser()->id());
+ $node4->save();
+ $suggestions = $plugin->execute('Lorem');
$this->assertEquals(4, count($suggestions->getSuggestions()), 'Correct number of suggestions');
+
+ // Test with permissions to see any unpublished nodes.
+ \Drupal::currentUser()->setAccount($this->createUser([], ['access content', 'view any unpublished content']));
+ $suggestions = $plugin->execute('Lorem');
+ $this->assertEquals(5, count($suggestions->getSuggestions()), 'Correct number of suggestions');
}
/**