View source
<?php
namespace Drupal\Tests\freelinking\Unit\Plugin\freelinking;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\freelinking\NodeTitle;
use Prophecy\Argument;
class NodeTitleTest extends NodeTestBase {
protected $plugin;
protected function setUp() {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to view a local node');
$entityManagerProphet = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entityQuery = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')
->disableOriginalConstructor()
->getMock();
$entityQuery
->expects($this
->any())
->method('condition')
->willReturnSelf();
$entityQuery
->expects($this
->any())
->method('accessCheck')
->willReturnSelf();
$entityQuery
->expects($this
->any())
->method('execute')
->willReturn([
1 => 1,
2 => 2,
]);
$nodeStorageProphet = $this
->prophesize('\\Drupal\\node\\NodeStorageInterface');
$nodeStorageProphet
->getQuery('AND')
->willReturn($entityQuery);
$entityManagerProphet
->getStorage('node')
->wilLReturn($nodeStorageProphet
->reveal());
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$container = new ContainerBuilder();
$container
->set('string_translation', $tProphet
->reveal());
$container
->set('entity_type.manager', $entityManagerProphet
->reveal());
$container
->set('module_handler', $moduleHandlerProphet
->reveal());
\Drupal::setContainer($container);
$this->plugin = NodeTitle::create($container, [
'settings' => [
'nodetypes' => [],
'failover' => '',
],
], 'nodetitle', []);
}
public function testGetTip() {
$this
->assertEquals('Click to view a local node', $this->plugin
->getTip()
->render());
}
public function testGetIndicator($test, $expected) {
$this
->assertEquals($expected, preg_match($this->plugin
->getIndicator(), $test));
}
public function testDefaultConfiguration() {
$this
->assertEquals([
'settings' => [
'nodetypes' => [],
'failover' => '',
],
], $this->plugin
->defaultConfiguration());
}
public function testBuildLink() {
$language = self::getDefaultLanguage();
$expected = [
'#type' => 'link',
'#title' => 'Test Node',
'#url' => Url::fromRoute('entity.node.canonical', [
'node' => 1,
], [
'language' => $language,
]),
'#attributes' => [
'title' => $this->plugin
->getTip(),
],
];
$target = [
'dest' => 'Test Node',
'language' => $language,
];
$this
->assertEquals($expected, $this->plugin
->buildLink($target));
}
public function indicatorProvider() {
return [
[
'ntnomatch',
0,
],
[
'nt',
1,
],
[
'nodetitle',
1,
],
[
'title',
1,
],
];
}
}