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\Search;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class SearchFailoverTest extends UnitTestCase {
protected $container;
protected $translationInterfaceMock;
protected function setUp() {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Search this site for content like “%dest”.');
$this->translationInterfaceMock = $tProphet
->reveal();
$pathValidatorProphet = $this
->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$moduleHandlerProphet
->moduleExists('search')
->willReturn(FALSE);
$container = new ContainerBuilder();
$container
->set('string_translation', $this->translationInterfaceMock);
$container
->set('module_handler', $moduleHandlerProphet
->reveal());
$container
->set('path.validator', $pathValidatorProphet
->reveal());
\Drupal::setContainer($container);
$this->container = $container;
}
public function testBuildLink($failoverOption, array $expected) {
$target = [
'target' => 'search:Test Search|Test Search',
'dest' => 'Test Search',
'text' => 'Test Search',
'language' => NULL,
];
if ($failoverOption === 'google') {
$expected['#url'] = Url::fromUri('https://google.com/search', [
'query' => [
'q' => 'Test+Search',
'hl' => 'en',
],
'language' => NULL,
'absolute' => TRUE,
]);
}
elseif ($failoverOption === 'error') {
$expected['#message'] = new TranslatableMarkup('Search unavailable', [], [], $this->translationInterfaceMock);
}
$plugin_definition = [
'id' => 'search',
'title' => 'Search',
'hidden' => FALSE,
'weight' => 0,
'settings' => [
'failover' => $failoverOption,
],
];
$configuration = [
'settings' => [
'failover' => $failoverOption,
],
];
$plugin = Search::create($this->container, $configuration, 'search', $plugin_definition);
$this
->assertEquals($expected, $plugin
->buildLink($target));
}
public function buildLinkProvider() {
$errorExpected = [
'#theme' => 'freelink_error',
'#plugin' => 'search',
'#message' => 'Search unavailable',
];
$googleExpected = [
'#type' => 'link',
'#title' => 'Google Search Test Search',
'#attributes' => [
'title' => 'Search this site for content like “%dest”.',
],
];
return [
[
'error',
$errorExpected,
],
[
'google',
$googleExpected,
],
];
}
}