View source
<?php
namespace Drupal\Tests\linkit\Kernel\Matchers;
use Drupal\file\Entity\File;
use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase;
class FileMatcherTest extends LinkitKernelTestBase {
public static $modules = [
'file_test',
'file',
];
protected $manager;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installSchema('system', [
'key_value_expire',
]);
$this
->installSchema('file', [
'file_usage',
]);
$this->manager = $this->container
->get('plugin.manager.linkit.matcher');
foreach ([
'gif',
'jpg',
'png',
] as $ext) {
$file = File::create([
'uid' => 1,
'filename' => 'image-test.' . $ext,
'uri' => 'public://image-test.' . $ext,
'filemime' => 'text/plain',
'status' => FILE_STATUS_PERMANENT,
]);
$file
->save();
}
\Drupal::currentUser()
->setAccount($this
->createUser([
'uid' => 1,
]));
}
public function testFileMatcherWithDefaultConfiguration() {
$plugin = $this->manager
->createInstance('entity:file', []);
$suggestions = $plugin
->execute('image-test');
$this
->assertEquals(3, count($suggestions
->getSuggestions()), 'Correct number of suggestions.');
}
public function testFileMatcherWithExtensionFiler() {
$plugin = $this->manager
->createInstance('entity:file', [
'settings' => [
'file_extensions' => 'png',
],
]);
$suggestions = $plugin
->execute('image-test');
$this
->assertEquals(1, count($suggestions
->getSuggestions()), 'Correct number of suggestions with single file extension filter.');
$plugin = $this->manager
->createInstance('entity:file', [
'settings' => [
'file_extensions' => 'png jpg',
],
]);
$suggestions = $plugin
->execute('image-test');
$this
->assertEquals(2, count($suggestions
->getSuggestions()), 'Correct number of suggestions with multiple file extension filter.');
}
public function testTermMatcherWidthMetadataTokens() {
$plugin = $this->manager
->createInstance('entity:file', [
'settings' => [
'metadata' => '[file:fid] [file:field_with_no_value]',
],
]);
$suggestionCollection = $plugin
->execute('Lorem');
$suggestions = $suggestionCollection
->getSuggestions();
foreach ($suggestions as $suggestion) {
$this
->assertStringNotContainsString('[file:fid]', $suggestion
->getDescription(), 'Raw token "[file:fid]" is not present in the description');
$this
->assertStringNotContainsString('[file:field_with_no_value]', $suggestion
->getDescription(), 'Raw token "[file:field_with_no_value]" is not present in the description');
}
}
}