View source
<?php
namespace Drupal\Tests\freelinking\Unit\Plugin\freelinking;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\freelinking\Node;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class NodeTest extends NodeTestBase {
protected $translationInterfaceMock;
protected $plugin;
protected $container;
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();
$nodeProphet = $this
->prophesize('\\Drupal\\node\\Entity\\Node');
$nodeProphet
->id()
->willReturn(1);
$nodeProphet
->label()
->willReturn('Test Node');
$nodeProphet
->language()
->willReturn(self::getDefaultLanguage());
$nodeStorageProphet = $this
->prophesize('\\Drupal\\node\\NodeStorageInterface');
$nodeStorageProphet
->load(1)
->willReturn($nodeProphet
->reveal());
$nodeStorageProphet
->load(2)
->willReturn(NULL);
$entityManagerProphet = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entityManagerProphet
->getStorage('node')
->willReturn($nodeStorageProphet
->reveal());
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $entityManagerProphet
->reveal());
$container
->set('string_translation', $this->translationInterfaceMock);
\Drupal::setContainer($container);
$this->container = $container;
$plugin_definition = [
'id' => 'nid',
'title' => 'Node ID',
'hidden' => FALSE,
'weight' => 0,
'settings' => [],
];
$this->plugin = Node::create($container, [], 'nid', $plugin_definition);
}
public function testGetIndicator($indicator, $expected) {
$this
->assertEquals($expected, preg_match($this->plugin
->getIndicator(), $indicator));
}
public function testGetTip() {
$this
->assertEquals('Click to view a local node.', $this->plugin
->getTip()
->render());
}
public function testBuildLink(array $target, array $expected, $shouldFailover = FALSE) {
$language = self::getDefaultLanguage();
if ($shouldFailover) {
$expected['#message'] = new TranslatableMarkup($expected['#message'], [
'@nid' => '2',
], [], $this->translationInterfaceMock);
}
else {
$expected['#url'] = Url::fromRoute('entity.node.canonical', [
'node' => 1,
], [
'language' => $language,
]);
$expected['#attributes']['title'] = new TranslatableMarkup('Click to view a local node', [], [], $this->translationInterfaceMock);
}
$this
->assertEquals($expected, $this->plugin
->buildLink($target));
}
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'n',
1,
],
[
'nid',
1,
],
[
'node',
1,
],
];
}
public function buildLinkProvider() {
$language = self::getDefaultLanguage();
$failoverTarget = [
'target' => 'nid:2',
'dest' => '2',
'language' => $language,
];
$failoverExpected = [
'#theme' => 'freelink_error',
'#plugin' => 'nid',
'#message' => 'Invalid node ID @nid',
];
$successTarget = [
'target' => 'nid:1',
'dest' => '1',
'language' => $language,
];
$successExpected = [
'#type' => 'link',
'#title' => 'Test Node',
'#attributes' => [
'title' => 'Click to view a local node',
],
];
return [
[
$failoverTarget,
$failoverExpected,
TRUE,
],
[
$successTarget,
$successExpected,
FALSE,
],
];
}
}