View source
<?php
namespace Drupal\Tests\linkit\Kernel\Matchers;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase;
class NodeMatcherTest extends LinkitKernelTestBase {
public static $modules = [
'field',
'node',
'content_moderation',
'workflows',
];
protected $manager;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig([
'field',
'node',
]);
$this->manager = $this->container
->get('plugin.manager.linkit.matcher');
\Drupal::currentUser()
->setAccount($this
->createUser());
$type1 = NodeType::create([
'type' => 'test1',
'name' => 'Test1',
]);
$type1
->save();
$type2 = NodeType::create([
'type' => 'test2',
'name' => 'Test2',
]);
$type2
->save();
$node = Node::create([
'title' => 'Lorem Ipsum 1',
'type' => $type1
->id(),
]);
$node
->save();
$node = Node::create([
'title' => 'Lorem Ipsum 2',
'type' => $type1
->id(),
]);
$node
->save();
$node = Node::create([
'title' => 'Lorem Ipsum 3',
'type' => $type2
->id(),
]);
$node
->save();
$node = Node::create([
'title' => 'Lorem unpublishd',
'type' => $type1
->id(),
'status' => FALSE,
]);
$node
->save();
$node = Node::create([
'title' => 'Lorem unpublishd 2',
'type' => $type2
->id(),
'status' => FALSE,
]);
$node
->save();
\Drupal::currentUser()
->setAccount($this
->createUser([], [
'access content',
]));
}
public function testNodeMatcherWidthDefaultConfiguration() {
$plugin = $this->manager
->createInstance('entity:node', []);
$suggestions = $plugin
->execute('Lorem');
$this
->assertEquals(3, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
}
public function testNodeMatcherWidthBundleFiler() {
$plugin = $this->manager
->createInstance('entity:node', [
'settings' => [
'bundles' => [
'test1' => 'test1',
],
],
]);
$suggestions = $plugin
->execute('Lorem');
$this
->assertEquals(2, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
}
public function testNodeMatcherWidthIncludeUnpublished() {
$plugin = $this->manager
->createInstance('entity:node', [
'settings' => [
'include_unpublished' => TRUE,
],
]);
$suggestions = $plugin
->execute('Lorem');
$this
->assertEquals(3, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
\Drupal::currentUser()
->setAccount($this
->createUser([], [
'bypass node access',
]));
$suggestions = $plugin
->execute('Lorem');
$this
->assertEquals(5, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
\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);
$node4
->setOwnerId(\Drupal::currentUser()
->id());
$node4
->save();
$suggestions = $plugin
->execute('Lorem');
$this
->assertEquals(4, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
\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');
}
public function testNodeMatcherWidthMetadataTokens() {
$plugin = $this->manager
->createInstance('entity:node', [
'settings' => [
'metadata' => '[node:nid] [node:field_with_no_value]',
],
]);
$suggestionCollection = $plugin
->execute('Lorem');
$suggestions = $suggestionCollection
->getSuggestions();
foreach ($suggestions as $suggestion) {
$this
->assertStringNotContainsString('[node:nid]', $suggestion
->getDescription(), 'Raw token "[node:nid]" is not present in the description');
$this
->assertStringNotContainsString('[node:field_with_no_value]', $suggestion
->getDescription(), 'Raw token "[node:field_with_no_value]" is not present in the description');
}
}
}