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\External;
use Drupal\Tests\UnitTestCase;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
class ExternalTest extends UnitTestCase {
use ProphecyTrait;
protected $container;
protected $translationInterfaceMock;
protected function setUp() {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to visit an external URL.');
$this->translationInterfaceMock = $tProphet
->reveal();
$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);
}
public function testGetTip() {
$plugin = $this
->getPlugin(FALSE);
$this
->assertEquals('Click to visit an external URL.', $plugin
->getTip()
->render());
}
public function testGetIndicator($indicator, $expected) {
$plugin = $this
->getPlugin(FALSE);
$this
->assertEquals($expected, preg_match($plugin
->getIndicator(), $indicator));
}
public function testDefaultConfiguration() {
$plugin = $this
->getPlugin();
$this
->assertEquals([
'settings' => [
'scrape' => TRUE,
],
], $plugin
->defaultConfiguration());
}
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),
],
];
$this
->assertEquals($expected, $plugin
->buildLink($target));
$expected['#title'] = 'http://www.example.com';
$this
->assertEquals($expected, $plugin
->buildLink($target));
$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));
$target['text'] = 'Custom Title';
$expected['#title'] = 'Custom Title';
$this
->assertEquals($expected, $plugin
->buildLink($target));
}
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);
}
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,
]);
}
public function indicatorProvider() {
return [
[
'nomatch',
0,
],
[
'http',
1,
],
[
'https',
1,
],
[
'ext',
1,
],
[
'external',
1,
],
];
}
}