View source
<?php
namespace Drupal\Tests\freelinking\Unit\Plugin\freelinking;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\freelinking\PathAlias;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
class PathAliasSearchTest extends UnitTestCase {
use ProphecyTrait;
protected $container;
protected $translationInterfaceMock;
protected function setUp() {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to view a local node.');
$this->translationInterfaceMock = $tProphet
->reveal();
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$moduleHandlerProphet
->moduleExists('search')
->willReturn(TRUE);
$aliasManagerProphet = $this
->prophesize('\\Drupal\\path_alias\\AliasManagerInterface');
$aliasManagerProphet
->getPathByAlias('/invalidalias', Argument::any())
->willReturn('/invalidalias');
$pathValidatorProphet = $this
->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this->translationInterfaceMock);
$this->container
->set('module_handler', $moduleHandlerProphet
->reveal());
$this->container
->set('path_alias.manager', $aliasManagerProphet
->reveal());
$this->container
->set('path.validator', $pathValidatorProphet
->reveal());
\Drupal::setContainer($this->container);
}
protected function getPlugin($failoverOption = 'search') {
$configuration = [
'settings' => [
'failover' => $failoverOption,
],
];
$plugin_definition = [
'id' => 'path_alias',
'title' => 'Path Alias',
'hidden' => FALSE,
'weight' => 0,
] + $configuration;
return PathAlias::create($this->container, $configuration, 'path_alias', $plugin_definition);
}
public function testBuildLink() {
$target = [
'text' => 'A valid path alias',
'dest' => 'invalidalias',
'target' => 'alias:invalidalias|A valid path alias',
'language' => NULL,
];
$plugin = $this
->getPlugin('search');
$expected = [
'#type' => 'link',
'#title' => 'A valid path alias',
'#url' => Url::fromUri('base:search', [
'query' => [
'keys' => '/invalidalias',
],
'language' => NULL,
]),
'#attributes' => [
'title' => new TranslatableMarkup('Click to view a local node.', [], [], $this->translationInterfaceMock),
],
];
$this
->assertEquals($expected, $plugin
->buildLink($target));
}
}