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;
class PathAliasTest extends UnitTestCase {
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(FALSE);
$aliasManagerProphet = $this
->prophesize('\\Drupal\\path_alias\\AliasManagerInterface');
$aliasManagerProphet
->getPathByAlias(Argument::any(), Argument::any())
->will(function ($args) {
return $args[0] === '/validalias' ? '/node/1' : '/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 testGetIndicator($indicator, $expected) {
$plugin = $this
->getPlugin();
$this
->assertEquals($expected, preg_match($plugin
->getIndicator(), $indicator));
}
public function testGetTip() {
$plugin = $this
->getPlugin();
$this
->assertEquals('Click to view a local node.', $plugin
->getTip()
->render());
}
public function testDefaultConfiguration() {
$plugin = $this
->getPlugin();
$this
->assertEquals([
'settings' => [
'failover' => 'search',
],
], $plugin
->defaultConfiguration());
}
public function testBuildLink($alias, array $expected) {
$target = [
'text' => 'A valid path alias',
'dest' => $alias,
'target' => 'alias:' . $alias . '|A valid path alias',
'language' => NULL,
];
$plugin = $this
->getPlugin('error');
if ($alias === 'validalias') {
$expected['#url'] = Url::fromUri('base:node/1', [
'language' => NULL,
]);
$expected['#attributes'] = [
'title' => new TranslatableMarkup('Click to view a local node.', [], [], $this->translationInterfaceMock),
];
}
else {
$expected['#message'] = new TranslatableMarkup('path “%path” not found', [
'%path' => '/invalidalias',
], [], $this->translationInterfaceMock);
}
$this
->assertEquals($expected, $plugin
->buildLink($target));
}
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'path',
1,
],
[
'alias',
1,
],
];
}
public function buildLinkProvider() {
$validExpected = [
'#type' => 'link',
'#title' => 'A valid path alias',
];
$invalidExpected = [
'#theme' => 'freelink_error',
'#plugin' => 'path_alias',
];
return [
[
'validalias',
$validExpected,
],
[
'invalidalias',
$invalidExpected,
],
];
}
}