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;
use Prophecy\PhpUnit\ProphecyTrait;
class SearchTest extends UnitTestCase {
use ProphecyTrait;
protected $plugin;
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(TRUE);
$container = new ContainerBuilder();
$container
->set('string_translation', $this->translationInterfaceMock);
$container
->set('module_handler', $moduleHandlerProphet
->reveal());
$container
->set('path.validator', $pathValidatorProphet
->reveal());
\Drupal::setContainer($container);
$plugin_definition = [
'id' => 'search',
'title' => 'Search',
'hidden' => FALSE,
'weight' => 0,
'settings' => [
'failover' => 'error',
],
];
$configuration = [
'settings' => [
'failover' => 'error',
],
];
$this->plugin = Search::create($container, $configuration, 'search', $plugin_definition);
}
public function testGetIndicator($indicator, $expected) {
$this
->assertEquals($expected, preg_match($this->plugin
->getIndicator(), $indicator));
}
public function testGetTip() {
$this
->assertEquals('Search this site for content like “%dest”.', $this->plugin
->getTip());
}
public function testBuildLink() {
$expected = [
'#type' => 'link',
'#title' => 'Search Test',
'#url' => Url::fromUri('base:search/node', [
'language' => NULL,
'query' => [
'keys' => 'A+search+string',
],
]),
'#attributes' => [
'title' => new TranslatableMarkup('Search this site for content.', [], [], $this->translationInterfaceMock),
],
];
$target = [
'target' => 'search:A search string|Search Test',
'dest' => 'A search string',
'text' => 'Search Test',
'language' => NULL,
];
$this
->assertEquals($expected, $this->plugin
->buildLink($target));
}
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'search',
1,
],
];
}
}