Commit b36841be authored by anon's avatar anon Committed by anon

Issue #2753265 by anon: Change how the file matcher handles URL:s

parent 9b39660e
......@@ -31,6 +31,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface {
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a LinkitFilter object.
*
......@@ -81,11 +88,23 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface
if ($entity) {
$entity = $this->entityRepository->getTranslationFromContext($entity, $langcode);
// Set the appropriate href and title attributes.
$url = $entity->toUrl()->toString(TRUE);
$element->setAttribute('href', $url->getGeneratedUrl());
// Set the appropriate href attribute.
// The file entity has not declared any "links" in its entity
// definition. We therefor have to use the file entity specific
// getFileUri() instead.
if ($entity_type === 'file') {
/** @var \Drupal\file\Entity\File $entity */
$url = file_create_url($entity->getFileUri());
$element->setAttribute('href', $url);
}
else {
$url = $entity->toUrl()->toString(TRUE);
$element->setAttribute('href', $url->getGeneratedUrl());
}
$access = $entity->access('view', NULL, TRUE);
// Set the appropriate title attribute.
if ($this->settings['title'] && !$access->isForbidden()) {
$element->setAttribute('title', $entity->label());
}
......
......@@ -204,14 +204,4 @@ class FileMatcher extends EntityMatcher {
return LinkitXss::descriptionFilter($description);
}
/**
* {@inheritdoc}
*
* The file entity still uses url() even though it's deprecated in the
* entity interface.
*/
protected function buildPath(EntityInterface $entity) {
return $entity->url();
}
}
......@@ -20,13 +20,6 @@ class LinkitFilterEntityTest extends LinkitFilterTestBase {
*/
public static $modules = ['entity_test', 'path', 'language'];
/**
* An user with permissions to administer content types.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* {@inheritdoc}
*/
......
<?php
namespace Drupal\linkit\Tests;
use Drupal\file\Entity\File;
/**
* Tests the Linkit filter for file entities.
*
* @group linkit
*/
class LinkitFilterFileEntityTest extends LinkitFilterTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['file'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
}
/**
* Tests the linkit filter for file entities.
*/
public function testFilterFileEntity() {
$file = File::create(array(
'uid' => 1,
'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt',
'filemime' => 'text/plain',
'status' => FILE_STATUS_PERMANENT,
));
$file->save();
// Disable the automatic title attribute.
$this->filter->setConfiguration(['settings' => ['title' => 0]]);
$this->assertLinkitFilter($file);
// Automatically set the title.
$this->filter->setConfiguration(['settings' => ['title' => 1]]);
$this->assertLinkitFilterWithTitle($file);
}
}
......@@ -38,8 +38,16 @@ abstract class LinkitFilterTestBase extends LinkitTestBase {
* The entity object to check.
*/
protected function assertLinkitFilter(EntityInterface $entity, $langcode = LanguageInterface::LANGCODE_SITE_DEFAULT) {
if ($entity->getEntityTypeId() === "file") {
/** @var \Drupal\file\Entity\File $entity */
$href = $url = file_create_url($entity->getFileUri());
}
else {
$href = $entity->toUrl()->toString();
}
$input = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '">Link text</a>';
$expected = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '" href="' . $entity->toUrl()->toString() . '">Link text</a>';
$expected = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '" href="' . $href . '">Link text</a>';
$this->assertIdentical($expected, $this->process($input, $langcode)->getProcessedText());
}
......@@ -50,8 +58,16 @@ abstract class LinkitFilterTestBase extends LinkitTestBase {
* The entity object to check.
*/
protected function assertLinkitFilterWithTitle(EntityInterface $entity, $langcode = LanguageInterface::LANGCODE_SITE_DEFAULT) {
if ($entity->getEntityTypeId() === "file") {
/** @var \Drupal\file\Entity\File $entity */
$href = $url = file_create_url($entity->getFileUri());
}
else {
$href = $entity->toUrl()->toString();
}
$input = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '">Link text</a>';
$expected = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '" href="' . $entity->toUrl()->toString() . '" title="' . Html::decodeEntities($entity->label()) . '">Link text</a>';
$expected = '<a data-entity-type="' . $entity->getEntityTypeId() . '" data-entity-uuid="' . $entity->uuid() . '" href="' . $href . '" title="' . Html::decodeEntities($entity->label()) . '">Link text</a>';
$this->assertIdentical($expected, $this->process($input, $langcode)->getProcessedText());
}
......
<?php
namespace Drupal\linkit\Tests\Matchers;
use Drupal\linkit\Tests\LinkitTestBase;
/**
* Base class for matcher tests.
*
* Adds some additional matcher specific assertions and helper functions.
*/
abstract class EntityMatcherTestBase extends LinkitTestBase {
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
}
/**
* Assert that paths are formatted as an URI with the entity: scheme.
*
* @param string $entity_type
* The entity_type.
* @param array $matches
* An array of matches.
*/
public function assertResultUri($entity_type, $matches) {
foreach ($matches as $match) {
$this->assertTrue(preg_match("/^entity:" . $entity_type . "\\/\\w+$/i", $match['path']), 'Result URI correct formatted.');
}
}
}
......@@ -3,14 +3,13 @@
namespace Drupal\linkit\Tests\Matchers;
use Drupal\file\Entity\File;
use Drupal\linkit\Tests\LinkitTestBase;
/**
* Tests file matcher.
*
* @group linkit
*/
class FileMatcherTest extends LinkitTestBase {
class FileMatcherTest extends EntityMatcherTestBase {
/**
* Modules to enable.
......@@ -19,20 +18,11 @@ class FileMatcherTest extends LinkitTestBase {
*/
public static $modules = ['file_test', 'file'];
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
$image_files = $this->drupalGetTestFiles('image');
......@@ -50,6 +40,21 @@ class FileMatcherTest extends LinkitTestBase {
}
}
/**
* Tests the paths for results on a file matcher.
*/
public function testMatcherResultsPath() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:file', [
'settings' => [
'file_status' => 0,
],
]);
$matches = $plugin->getMatches('image-test');
$this->assertResultUri('file', $matches);
}
/**
* Tests file matcher.
*/
......
......@@ -2,14 +2,12 @@
namespace Drupal\linkit\Tests\Matchers;
use Drupal\linkit\Tests\LinkitTestBase;
/**
* Tests node matcher.
*
* @group linkit
*/
class NodeMatcherTest extends LinkitTestBase {
class NodeMatcherTest extends EntityMatcherTestBase {
/**
* Modules to enable.
......@@ -18,20 +16,11 @@ class NodeMatcherTest extends LinkitTestBase {
*/
public static $modules = ['node'];
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
$type1 = $this->drupalCreateContentType([
'type' => 'test1',
......@@ -66,6 +55,17 @@ class NodeMatcherTest extends LinkitTestBase {
]);
}
/**
* Tests the paths for results on a node matcher.
*/
public function testMatcherResultsPath() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:node', []);
$matches = $plugin->getMatches('Lorem');
$this->assertResultUri('node', $matches);
}
/**
* Tests node matcher.
*/
......
......@@ -4,7 +4,6 @@ namespace Drupal\linkit\Tests\Matchers;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\linkit\Tests\LinkitTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
/**
......@@ -12,7 +11,7 @@ use Drupal\taxonomy\Entity\Vocabulary;
*
* @group linkit
*/
class TermMatcherTest extends LinkitTestBase {
class TermMatcherTest extends EntityMatcherTestBase {
/**
* Modules to enable.
......@@ -21,13 +20,6 @@ class TermMatcherTest extends LinkitTestBase {
*/
public static $modules = ['taxonomy'];
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* Creates and saves a vocabulary.
*
......@@ -75,7 +67,7 @@ class TermMatcherTest extends LinkitTestBase {
],
'vid' => $vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
]);
$term->save();
return $term;
}
......@@ -85,8 +77,6 @@ class TermMatcherTest extends LinkitTestBase {
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
$testing_vocabulary_1 = $this->createVocabulary('testing_vocabulary_1');
$testing_vocabulary_2 = $this->createVocabulary('testing_vocabulary_2');
......@@ -99,6 +89,21 @@ class TermMatcherTest extends LinkitTestBase {
$this->createTerm($testing_vocabulary_2, ['name' => 'foo_baz']);
}
/**
* Tests the paths for results on a term matcher.
*/
public function testMatcherResultsPath() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:taxonomy_term', [
'settings' => [
'file_status' => 0,
],
]);
$matches = $plugin->getMatches('foo');
$this->assertResultUri('taxonomy_term', $matches);
}
/**
* Tests term matcher with default configuration.
*/
......
......@@ -2,14 +2,12 @@
namespace Drupal\linkit\Tests\Matchers;
use Drupal\linkit\Tests\LinkitTestBase;
/**
* Tests user matcher.
*
* @group linkit
*/
class UserMatcherTest extends LinkitTestBase {
class UserMatcherTest extends EntityMatcherTestBase {
/**
* Modules to enable.
......@@ -18,20 +16,11 @@ class UserMatcherTest extends LinkitTestBase {
*/
public static $modules = ['user'];
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
$custom_role = $this->drupalCreateRole([], 'custom_role', 'custom_role');
$custom_role_admin = $this->drupalCreateRole([], 'custom_role_admin', 'custom_role_admin');
......@@ -56,6 +45,17 @@ class UserMatcherTest extends LinkitTestBase {
$account->save();
}
/**
* Tests the paths for results on a user matcher.
*/
public function testMatcherResultsPath() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:user', []);
$matches = $plugin->getMatches('Lorem');
$this->assertResultUri('user', $matches);
}
/**
* Tests user matcher.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment