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\File;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class FileTest extends UnitTestCase {
protected $translationInterfaceMock;
protected $container;
protected function setUp() {
$tProphet = $this
->prophesize('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$tProphet
->translateString(Argument::any())
->willReturn('Click to view a local file.');
$this->translationInterfaceMock = $tProphet
->reveal();
$configProphet = $this
->prophesize('\\Drupal\\Core\\Config\\ImmutableConfig');
$configProphet
->get('default_scheme')
->willReturn('public');
$configFactoryProphet = $this
->prophesize('\\Drupal\\Core\\Config\\ConfigFactoryInterface');
$configFactoryProphet
->get('system.file')
->willReturn($configProphet
->reveal());
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this->translationInterfaceMock);
$this->container
->set('module_handler', $this
->getModuleHandlerMock());
$this->container
->set('config.factory', $configFactoryProphet
->reveal());
$this->container
->set('stream_wrapper_manager', $this
->getStreamWrapperMock());
\Drupal::setContainer($this->container);
}
public function testGetIndicator() {
$plugin = $this
->getPlugin();
$this
->assertEquals(1, preg_match($plugin
->getIndicator(), 'file'));
}
public function testGetTip() {
$plugin = $this
->getPlugin();
$this
->assertEquals('Click to view a local file.', $plugin
->getTip()
->render());
}
public function testDefaultConfiguration() {
$expected = [
'settings' => [
'scheme' => 'public',
],
];
$plugin = $this
->getPlugin();
$this
->assertEquals($expected, $plugin
->defaultConfiguration());
}
public function testBuildLink($path, $availability) {
$this->container
->set('module_handler', $this
->getModuleHandlerMock($availability));
$this->container
->set('stream_wrapper_manager', $this
->getStreamWrapperMock($availability));
\Drupal::setContainer($this->container);
if ($availability > 0) {
$expected = [
'#type' => 'link',
'#title' => 'Test File',
'#url' => Url::fromUri('http://example.com/logo.png', [
'absolute' => TRUE,
'language' => NULL,
]),
'#attributes' => [
'title' => new TranslatableMarkup('Click to view a local file.', [], [], $this->translationInterfaceMock),
],
];
}
else {
$expected = [
'#theme' => 'freelink_error',
'#plugin' => 'file',
'#message' => new TranslatableMarkup('File @name not found', [
'@name' => 'logo.png',
], [], $this->translationInterfaceMock),
];
}
$target = [
'target' => 'file:logo.png|Test File',
'dest' => 'logo.png',
'text' => 'Test File',
'language' => NULL,
];
$plugin = $this
->getPlugin();
$this
->assertEquals($expected, $plugin
->buildLink($target));
}
public function buildLinkProvider() {
return [
[
'logo.png',
0,
],
[
'logo.png',
-1,
],
[
'logo.png',
1,
],
];
}
protected function getPlugin() {
$plugin_definition = [
'id' => 'file',
'title' => 'File',
'hidden' => FALSE,
'weight' => 0,
'settings' => [
'scheme' => 'public',
],
];
return File::create($this->container, [
'settings' => [
'scheme' => 'public',
],
], 'file', $plugin_definition);
}
protected function getStreamWrapperMock($fileAvailability = 0) {
$streamWrapperProphet = $this
->prophesize('\\Drupal\\Core\\StreamWrapper\\StreamWrapperInterface');
$streamWrapperProphet
->setUri(Argument::any())
->will(function () {
return $this;
});
$streamWrapperProphet
->realpath()
->will(function () use ($fileAvailability) {
return $fileAvailability !== 0;
});
$streamWrapperProphet
->getExternalUrl()
->willReturn('http://example.com/logo.png');
$streamWrapperManagerProphet = $this
->prophesize('\\Drupal\\Core\\StreamWrapper\\StreamWrapperManagerInterface');
$streamWrapperManagerProphet
->getViaScheme(Argument::any())
->willReturn($streamWrapperProphet
->reveal());
return $streamWrapperManagerProphet
->reveal();
}
protected function getModuleHandlerMock($fileAvailability = 0) {
$moduleHandlerProphet = $this
->prophesize('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$moduleHandlerProphet
->invokeAll('file_download', Argument::any())
->will(function ($args) use ($fileAvailability) {
$ret = [];
if ($fileAvailability === -1) {
$ret['file'] = -1;
}
return $ret;
});
return $moduleHandlerProphet
->reveal();
}
}