View source
<?php
namespace Drupal\Tests\freelinking\Unit\Plugin\freelinking;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\freelinking\Plugin\freelinking\NodeTitle;
use Prophecy\Argument;
class NodeTitleFailoverTest extends NodeTestBase {
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();
$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([]);
$nodeStorageProphet = $this
->prophesize('\\Drupal\\node\\NodeStorageInterface');
$nodeStorageProphet
->getQuery('AND')
->willReturn($entityQuery);
$entityManagerProphet
->getStorage('node')
->wilLReturn($nodeStorageProphet
->reveal());
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$moduleHandlerProphet
->moduleExists('search')
->willReturn(TRUE);
$moduleHandlerProphet
->moduleExists('freelinking_prepopulate')
->willReturn(TRUE);
$pathValidatorProphet = $this
->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
$container = new ContainerBuilder();
$container
->set('string_translation', $this->translationInterfaceMock);
$container
->set('entity_type.manager', $entityManagerProphet
->reveal());
$container
->set('path.validator', $pathValidatorProphet
->reveal());
$container
->set('module_handler', $moduleHandlerProphet
->reveal());
\Drupal::setContainer($container);
$this->container = $container;
}
public function testFailover($failoverOption, array $expected) {
$plugin = NodeTitle::create($this->container, [
'settings' => [
'nodetypes' => [],
'failover' => $failoverOption,
],
], 'nodetitle', []);
$target = [
'dest' => 'Test Node',
'language' => $this
->getDefaultLanguage(),
'target' => 'Test Node|Test Node',
];
if ($failoverOption === 'error') {
$expected['#message'] = new TranslatableMarkup('Node title %target does not exist', [
'%target' => 'Test Node',
], [], $this->translationInterfaceMock);
}
$link = $plugin
->buildLink($target);
$this
->assertEquals($expected, $link);
}
public function failoverProvider() {
$noneExpected = [
'#markup' => '[[nodetitle:Test Node|Test Node]]',
];
$showTextExpected = [
'error' => 'showtext',
];
$searchExpected = [
'error' => 'search',
];
$prepopulateExpected = [
'error' => 'prepopulate',
];
$errorExpected = [
'#theme' => 'freelink_error',
'#plugin' => 'nodetitle',
];
return [
[
'_none',
$noneExpected,
],
[
'showtext',
$showTextExpected,
],
[
'search',
$searchExpected,
],
[
'prepopulate',
$prepopulateExpected,
],
[
'error',
$errorExpected,
],
];
}
}