class ExternalTest in Freelinking 8.3
Same name and namespace in other branches
- 4.0.x tests/src/Unit/Plugin/freelinking/ExternalTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\ExternalTest
Tests the external plugin.
@group freelinking
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\freelinking\Unit\Plugin\freelinking\ExternalTest
Expanded class hierarchy of ExternalTest
File
- tests/
src/ Unit/ Plugin/ freelinking/ ExternalTest.php, line 23
Namespace
Drupal\Tests\freelinking\Unit\Plugin\freelinkingView source
class ExternalTest extends UnitTestCase {
/**
* The container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* String translation mock.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationInterfaceMock;
/**
* {@inheritdoc}
*/
protected function setUp() {
// Mock string translation service.
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to visit an external URL.');
$this->translationInterfaceMock = $tProphet
->reveal();
// Mock module handler service.
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$moduleHandlerProphet
->moduleExists('search')
->willReturn(FALSE);
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this->translationInterfaceMock);
$this->container
->set('module_handler', $moduleHandlerProphet
->reveal());
$this->container
->set('http_client', $this
->getGuzzleMock());
\Drupal::setContainer($this->container);
}
/**
* Assert that getTip() is functional.
*/
public function testGetTip() {
$plugin = $this
->getPlugin(FALSE);
$this
->assertEquals('Click to visit an external URL.', $plugin
->getTip()
->render());
}
/**
* Assert that the indicator is functional.
*
* @param string $indicator
* The indicator string.
* @param int $expected
* The expected value from preg_match.
*
* @dataProvider indicatorProvider
*/
public function testGetIndicator($indicator, $expected) {
$plugin = $this
->getPlugin(FALSE);
$this
->assertEquals($expected, preg_match($plugin
->getIndicator(), $indicator));
}
/**
* Asserts that default configuration is expected.
*/
public function testDefaultConfiguration() {
$plugin = $this
->getPlugin();
$this
->assertEquals([
'settings' => [
'scrape' => TRUE,
],
], $plugin
->defaultConfiguration());
}
/**
* Asserts that buildLink returns appropriate render array.
*
* A data provider is not used for this test because Guzzle mocking is a bit
* weird and unorthodox.
*/
public function testBuildLink() {
$plugin = $this
->getPlugin();
$target = [
'dest' => '//www.example.com',
'indicator' => 'http',
'language' => NULL,
'text' => '',
];
$expected = [
'#type' => 'link',
'#title' => new TranslatableMarkup('Ext. link: “@title”', [
'@title' => 'Test Page',
], [], $this->translationInterfaceMock),
'#url' => Url::fromUri('http://www.example.com', [
'absolute' => TRUE,
'language' => NULL,
]),
'#attributes' => [
'title' => new TranslatableMarkup('Click to visit an external URL.', [], [], $this->translationInterfaceMock),
],
];
// Assert that 200 Response with title.
$this
->assertEquals($expected, $plugin
->buildLink($target));
// Assert that 200 Response without title has URL as title.
$expected['#title'] = 'http://www.example.com';
$this
->assertEquals($expected, $plugin
->buildLink($target));
// Assert that RequestException is handled correctly.
$error_expected = [
'#theme' => 'freelink_error',
'#plugin' => 'external',
'#message' => new TranslatableMarkup('External target “@url” not found', [
'@url' => 'http://www.example.com',
], [], $this->translationInterfaceMock),
];
$this
->assertEquals($error_expected, $plugin
->buildLink($target));
// Assert that 200 Response with title is not displayed when text is set.
$target['text'] = 'Custom Title';
$expected['#title'] = 'Custom Title';
$this
->assertEquals($expected, $plugin
->buildLink($target));
}
/**
* Get plugin instance.
*
* @param bool $scrapeOption
* The scrape option.
*
* @return \Drupal\freelinking\Plugin\freelinking\External
* A plugin instance.
*/
protected function getPlugin($scrapeOption = TRUE) {
$configuration = [
'settings' => [
'scrape' => $scrapeOption,
],
];
$plugin_definition = [
'id' => 'external',
'title' => 'External links',
'hidden' => FALSE,
'weight' => 0,
] + $configuration;
return External::create($this->container, $configuration, 'external', $plugin_definition);
}
/**
* Create Guzzle client instance with mock handlers.
*
* @return \GuzzleHttp\Client
* The Guzzle HTTP Client.
*/
protected function getGuzzleMock() {
$errorResponse = new Response(404, [
'Content-Type' => 'text/plain, charset=UTF-8',
], '404 Error: Page not found');
$mock = new MockHandler([
new Response(200, [
'Content-Type' => 'text/html',
], '<body><h1 class="page-title">Test Page</h1><div>Test Page Content.</div></body>'),
new Response(200, [
'Content-Type' => 'text/html',
], '<body>A bunch of text without a page title</body>'),
$errorResponse,
new RequestException('Server Error', new Request('GET', 'test'), $errorResponse),
]);
$handler = HandlerStack::create($mock);
return new Client([
'handler' => $handler,
]);
}
/**
* Provide test parameters for ::testGetIndicator.
*
* @return array
* An array of test parameters.
*/
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'http',
1,
],
[
'https',
1,
],
[
'ext',
1,
],
[
'external',
1,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExternalTest:: |
protected | property | The container. | |
ExternalTest:: |
protected | property | String translation mock. | |
ExternalTest:: |
protected | function | Create Guzzle client instance with mock handlers. | |
ExternalTest:: |
protected | function | Get plugin instance. | |
ExternalTest:: |
public | function | Provide test parameters for ::testGetIndicator. | |
ExternalTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ExternalTest:: |
public | function | Asserts that buildLink returns appropriate render array. | |
ExternalTest:: |
public | function | Asserts that default configuration is expected. | |
ExternalTest:: |
public | function | Assert that the indicator is functional. | |
ExternalTest:: |
public | function | Assert that getTip() is functional. | |
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. | |
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. |