View source
<?php
namespace Drupal\Tests\linkit\Kernel\Matchers;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\media\Entity\MediaType;
use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase;
class MediaMatcherTest extends LinkitKernelTestBase {
public static $modules = [
'file_test',
'file',
'media',
'image',
'field',
];
protected $manager;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installEntitySchema('media');
$this
->installConfig([
'media',
]);
$this
->installSchema('system', [
'key_value_expire',
]);
$this
->installSchema('file', [
'file_usage',
]);
$this->manager = $this->container
->get('plugin.manager.linkit.matcher');
$media_type = MediaType::create([
'label' => 'test',
'id' => 'test',
'description' => 'Test type.',
'source' => 'file',
]);
$media_type
->save();
$source_field = $media_type
->getSource()
->createSourceField($media_type);
$source_field
->getFieldStorageDefinition()
->save();
$source_field
->save();
$media_type
->set('source_configuration', [
'source_field' => $source_field
->getName(),
])
->save();
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();
$media = Media::create([
'bundle' => 'test',
$source_field
->getName() => [
'target_id' => $file
->id(),
],
]);
$media
->save();
}
\Drupal::currentUser()
->setAccount($this
->createUser([
'uid' => 1,
]));
}
public function testMediaMatcherWithDefaultConfiguration() {
$plugin = $this->manager
->createInstance('entity:media', []);
$suggestions = $plugin
->execute('image-test');
$this
->assertEquals(3, count($suggestions
->getSuggestions()), 'Correct number of suggestions.');
foreach ($suggestions
->getSuggestions() as $key => $suggestion) {
$this
->assertEquals('/media/' . ($key + 1), $suggestion
->getPath());
}
$config = \Drupal::service('config.factory')
->getEditable('media.settings');
$config
->set('standalone_url', TRUE)
->save();
drupal_flush_all_caches();
$suggestions = $plugin
->execute('image-test');
foreach ($suggestions
->getSuggestions() as $key => $suggestion) {
$this
->assertEquals('/media/' . ($key + 1), $suggestion
->getPath());
}
}
}