diff --git a/src/Plugin/Linkit/Matcher/NodeMatcher.php b/src/Plugin/Linkit/Matcher/NodeMatcher.php index 3a1424cc8a78cd8ab85b400db980e4e1358c1443..f40c7c006e049a7fbaeb92b579ccdbbbd004f9e1 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 8783a4f9762ea601eb826dffd1eeb9dc08c674c7..0ab55506edb60546da7e1672e204e020c322fa80 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'); } /**