View source
<?php
namespace Drupal\Tests\linkit\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\file\Entity\File;
use Drupal\filter\FilterPluginCollection;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
class LinkitFilterEntityTest extends LinkitKernelTestBase {
use AssertLinkitFilterTrait;
use PathAliasTestTrait;
public static $modules = [
'filter',
'entity_test',
'path',
'path_alias',
'language',
'file',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('entity_test_mul');
$this
->installEntitySchema('file');
ConfigurableLanguage::createFromLangcode('sv')
->save();
ConfigurableLanguage::createFromLangcode('da')
->save();
ConfigurableLanguage::createFromLangcode('fi')
->save();
$manager = $this->container
->get('plugin.manager.filter');
$bag = new FilterPluginCollection($manager, []);
$this->filter = $bag
->get('linkit');
}
public function testFilterEntityAccess() {
$entity_no_access = EntityTest::create([
'name' => 'forbid_access',
]);
$entity_no_access
->save();
$entity_with_access = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity_with_access
->save();
$this->filter
->setConfiguration([
'settings' => [
'title' => 1,
],
]);
$input = '<a data-entity-type="' . $entity_no_access
->getEntityTypeId() . '" data-entity-uuid="' . $entity_no_access
->uuid() . '">Link text</a>';
$this
->assertFalse(strpos($this
->process($input)
->getProcessedText(), 'title'), 'The link does not contain a title attribute.');
$this
->assertLinkitFilterWithTitle($entity_with_access);
}
public function testFilterEntityTranslations() {
$entity = EntityTestMul::create([
'name' => $this
->randomMachineName(),
]);
$entity
->addTranslation('sv', [
'name' => $this
->randomMachineName(),
'langcode' => 'sv',
]);
$entity
->addTranslation('da', [
'name' => $this
->randomMachineName(),
'langcode' => 'da',
]);
$entity
->addTranslation('fi', [
'name' => $this
->randomMachineName(),
'langcode' => 'fi',
]);
$entity
->save();
$url = $entity
->toUrl()
->toString();
$this
->createPathAlias($url, '/' . $this
->randomMachineName(), 'en');
$this
->createPathAlias($url, '/' . $this
->randomMachineName(), 'sv');
$this
->createPathAlias($url, '/' . $this
->randomMachineName(), 'da');
$this
->createPathAlias($url, '/' . $this
->randomMachineName(), 'fi');
$this->filter
->setConfiguration([
'settings' => [
'title' => 0,
],
]);
foreach ($entity
->getTranslationLanguages() as $language) {
$this
->assertLinkitFilter($entity
->getTranslation($language
->getId()), $language
->getId());
}
$this->filter
->setConfiguration([
'settings' => [
'title' => 1,
],
]);
foreach ($entity
->getTranslationLanguages() as $language) {
$this
->assertLinkitFilterWithTitle($entity
->getTranslation($language
->getId()), $language
->getId());
}
}
public function testFilterFileEntity() {
$file = File::create([
'uid' => 1,
'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt',
'filemime' => 'text/plain',
'status' => FILE_STATUS_PERMANENT,
]);
$file
->save();
$this->filter
->setConfiguration([
'settings' => [
'title' => 0,
],
]);
$this
->assertLinkitFilter($file);
$this->filter
->setConfiguration([
'settings' => [
'title' => 1,
],
]);
$this
->assertLinkitFilterWithTitle($file);
}
public function testTitleOverwritten() {
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$this->filter
->setConfiguration([
'settings' => [
'title' => 1,
],
]);
$input = '<a data-entity-type="' . $entity
->getEntityTypeId() . '" data-entity-uuid="' . $entity
->uuid() . '" title="Do not override">Link text</a>';
$this
->assertTrue(strpos($this
->process($input)
->getProcessedText(), 'Do not override') !== FALSE, 'The filer is not overwrite the provided title attribute value.');
}
public function testQueryAndFragments() {
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$input = '<a data-entity-type="' . $entity
->getEntityTypeId() . '" data-entity-uuid="' . $entity
->uuid() . '" href="unimportant/1234?query=string#fragment">Link text</a>';
$this
->assertStringContainsString('?query=string', $this
->process($input)
->getProcessedText());
$this
->assertStringContainsString('#fragment', $this
->process($input)
->getProcessedText());
}
}