class SearchTest in Freelinking 8.3
Same name and namespace in other branches
- 4.0.x tests/src/Unit/Plugin/freelinking/SearchTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\SearchTest
Tests the search plugin.
@group freelinking
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\SearchTest
Expanded class hierarchy of SearchTest
File
- tests/
src/ Unit/ Plugin/ freelinking/ SearchTest.php, line 17
Namespace
Drupal\Tests\freelinking\Unit\Plugin\freelinkingView source
class SearchTest extends UnitTestCase {
/**
* Search plugin.
*
* @var \Drupal\freelinking\Plugin\freelinking\Search
*/
protected $plugin;
/**
* Translation interface mock.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationInterfaceMock;
/**
* {@inheritdoc}
*/
protected function setUp() {
// Mock the string translation.
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Search this site for content like “%dest”.');
$this->translationInterfaceMock = $tProphet
->reveal();
// Mock path validator.
$pathValidatorProphet = $this
->prophesize('\\Drupal\\Core\\Path\\PathValidatorInterface');
// Mock module handler.
$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);
}
/**
* Assert that indicator functions for both google and core search.
*
* @param string $indicator
* The indicator string.
* @param int $expected
* The expected output from preg_match.
*
* @dataProvider indicatorProvider
*/
public function testGetIndicator($indicator, $expected) {
$this
->assertEquals($expected, preg_match($this->plugin
->getIndicator(), $indicator));
}
/**
* Asserts that the appropriate tip is returned.
*/
public function testGetTip() {
$this
->assertEquals('Search this site for content like “%dest”.', $this->plugin
->getTip());
}
/**
* Asserts that buildLink is functional when search enabled.
*/
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));
}
/**
* Provides test parameters for ::testGetIndicator.
*
* @return array
* An array of test parameters.
*/
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'search',
1,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SearchTest:: |
protected | property | Search plugin. | |
SearchTest:: |
protected | property | Translation interface mock. | |
SearchTest:: |
public | function | Provides test parameters for ::testGetIndicator. | |
SearchTest:: |
protected | function |
Overrides UnitTestCase:: |
|
SearchTest:: |
public | function | Asserts that buildLink is functional when search enabled. | |
SearchTest:: |
public | function | Assert that indicator functions for both google and core search. | |
SearchTest:: |
public | function | Asserts that the appropriate tip is returned. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |