Skip to content
Snippets Groups Projects
Commit 6432c929 authored by Tobias Zimmermann's avatar Tobias Zimmermann
Browse files

by tstoeckler: Add tests for local asset libraries

parent 18a89449
No related branches found
No related tags found
No related merge requests found
Showing with 168 additions and 58 deletions
......@@ -26,8 +26,8 @@ services:
class: Drupal\libraries\StreamWrapper\AssetLibrariesStream
tags:
- { name: stream_wrapper, scheme: 'asset' }
stream_wrapper.php_library_files:
class: Drupal\libraries\StreamWrapper\PhpLibraryFilesStream
stream_wrapper.php_file_libraries:
class: Drupal\libraries\StreamWrapper\PhpFileLibrariesStream
tags:
- { name: stream_wrapper, scheme: 'php-file' }
......
......@@ -81,7 +81,11 @@ trait LocalRemoteAssetTrait {
protected function getPathPrefix() {
/** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface|\Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface $this */
if ($this->isInstalled()) {
return $this->getLocalPath();
// LocalLibraryInterface::getLocalPath() returns the path relative to the
// app root. In order for the core core asset system to register the path
// as relative to the app root, a leading slash is required.
/** @see \Drupal\Core\Asset\LibraryDiscoveryParser::buildByExtension() */
return '/' . $this->getLocalPath();
}
elseif ($this->hasRemoteUrl()) {
return $this->getRemoteUrl();
......
......@@ -32,11 +32,11 @@ trait LocalLibraryTrait {
protected $installed = FALSE;
/**
* The absolute path to the library on the filesystem.
* The local path to the library relative to the app root.
*
* @var string
*/
protected $libraryPath;
protected $localPath;
/**
* Checks whether the library is installed.
......@@ -70,7 +70,7 @@ trait LocalLibraryTrait {
* Gets the path to the library.
*
* @return string
* The absolute path to the library on the filesystem.
* The path to the library relative to the app root.
*
* @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException
*
......@@ -82,7 +82,7 @@ trait LocalLibraryTrait {
throw new LibraryNotInstalledException($this);
}
return $this->libraryPath;
return $this->localPath;
}
/**
......@@ -95,9 +95,10 @@ trait LocalLibraryTrait {
*/
public function setLocalPath($path) {
$this->installed = TRUE;
$this->libraryPath = (string) $path;
$this->localPath = (string) $path;
assert('$this->libraryPath !== ""');
assert('$this->localPath !== ""');
assert('$this->localPath[0] !== "/"');
}
}
......@@ -38,6 +38,13 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
*/
protected $fileSystemHelper;
/**
* The app root.
*
* @var string
*/
protected $appRoot;
/**
* The scheme of the stream wrapper.
*
......@@ -50,11 +57,14 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
*
* @param \Drupal\Core\File\FileSystemInterface $file_system_helper
* The file system helper.
* @param string $app_root
* The app root.
* @param string $scheme
* The scheme of the stream wrapper.
*/
public function __construct(FileSystemInterface $file_system_helper, $scheme) {
public function __construct(FileSystemInterface $file_system_helper, $app_root, $scheme) {
$this->fileSystemHelper = $file_system_helper;
$this->appRoot = (string) $app_root;
$this->scheme = (string) $scheme;
}
......@@ -65,7 +75,7 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
if (!isset($configuration['scheme'])) {
throw new MissingPluginConfigurationException($plugin_id, $plugin_definition, $configuration, 'scheme');
}
return new static($container->get('file_system'), $configuration['scheme']);
return new static($container->get('file_system'), $container->get('app.root'), $configuration['scheme']);
}
......@@ -79,7 +89,13 @@ class StreamLocator implements LocatorInterface, ContainerFactoryPluginInterface
*/
public function locate(LocalLibraryInterface $library) {
$path = $this->fileSystemHelper->realpath($this->getUri($library));
if (is_dir($path) && is_readable($path)) {
if ($path && is_dir($path) && is_readable($path)) {
// Set a path relative to the app root.
assert('strpos($path, $this->appRoot . "/") === 0', "Path: $path");
$path = str_replace($this->appRoot . '/', '', $path);
assert('$path[0] !== "/"');
$library->setLocalPath($path);
}
else {
......
......@@ -39,7 +39,7 @@ class AssetLibrariesStream extends LocalStream {
*/
public function getDirectoryPath() {
// @todo Provide support for site-specific directories, etc.
return 'sites/all/assets';
return 'sites/all/assets/vendor';
}
}
......@@ -15,7 +15,7 @@ use Drupal\Core\StreamWrapper\LocalStream;
* Can be used with the 'php-file://' scheme, for example
* 'php-file-library://guzzle/src/functions_include.php'.
*/
class PhpLibraryFilesStream extends LocalStream {
class PhpFileLibrariesStream extends LocalStream {
use LocalHiddenStreamTrait;
use PrivateStreamTrait;
......
......@@ -10,6 +10,7 @@ namespace Drupal\Tests\libraries\Kernel\ExternalLibrary\Asset;
use Drupal\libraries\ExternalLibrary\Asset\AssetLibrary;
use Drupal\libraries\ExternalLibrary\Exception\LibraryClassNotFoundException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
use Drupal\Tests\libraries\Kernel\ExternalLibrary\TestLibraryFilesStream;
use Drupal\Tests\libraries\Kernel\ExternalLibraryKernelTestBase;
/**
......@@ -70,7 +71,7 @@ class AssetLibraryTest extends ExternalLibraryKernelTestBase {
}
/**
* Tests that an external asset library is registered as a core asset library.
* Tests that a remote asset library is registered as a core asset library.
*
* @see \Drupal\libraries\Extension\Extension
* @see \Drupal\libraries\Extension\ExtensionHandler
......@@ -80,7 +81,7 @@ class AssetLibraryTest extends ExternalLibraryKernelTestBase {
* @see \Drupal\libraries\ExternalLibrary\ExternalLibraryTrait
* @see \Drupal\libraries\ExternalLibrary\Registry\ExternalLibraryRegistry
*/
public function testAssetLibrary() {
public function testAssetLibraryRemote() {
$library = $this->libraryDiscovery->getLibraryByName('libraries', 'test_asset_library');
$expected = [
'version' => '1.0',
......@@ -107,4 +108,41 @@ class AssetLibraryTest extends ExternalLibraryKernelTestBase {
$this->assertEquals($expected, $library);
}
/**
* Tests that a local asset library is registered as a core asset library.
*/
public function testAssetLibraryLocal() {
$this->container->set('stream_wrapper.asset_libraries', new TestLibraryFilesStream(
$this->container->get('module_handler'),
$this->container->get('string_translation'),
'assets/vendor'
));
$this->libraryDiscovery->clearCachedDefinitions();
$library = $this->libraryDiscovery->getLibraryByName('libraries', 'test_asset_library');
$expected = [
'version' => '1.0',
'css' => [[
'weight' => -200,
'group' => 0,
'type' => 'file',
'data' => $this->modulePath . '/tests/assets/vendor/test_asset_library/example.css',
'version' => '1.0',
]],
'js' => [[
'group' => -100,
'type' => 'file',
'data' => $this->modulePath . '/tests/assets/vendor/test_asset_library/example.js',
'version' => '1.0',
'minified' => FALSE,
]],
'dependencies' => [],
'license' => [
'name' => 'GNU-GPL-2.0-or-later',
'url' => 'https://www.drupal.org/licensing/faq',
'gpl-compatible' => TRUE,
]
];
$this->assertEquals($expected, $library);
}
}
......@@ -10,6 +10,7 @@ namespace Drupal\Tests\libraries\Kernel\ExternalLibrary\PhpFile;
use Drupal\libraries\ExternalLibrary\Exception\LibraryClassNotFoundException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary;
use Drupal\Tests\libraries\Kernel\ExternalLibrary\TestLibraryFilesStream;
use Drupal\Tests\libraries\Kernel\ExternalLibraryKernelTestBase;
/**
......@@ -39,7 +40,11 @@ class PhpFileLibraryTest extends ExternalLibraryKernelTestBase {
$this->externalLibraryManager = $this->container->get('libraries.manager');
$this->container->set('stream_wrapper.php_library_files', new TestPhpLibraryFilesStream());
$this->container->set('stream_wrapper.php_file_libraries', new TestLibraryFilesStream(
$this->container->get('module_handler'),
$this->container->get('string_translation'),
'libraries'
));
}
/**
......
<?php
/**
* @file
* Contains \Drupal\Tests\libraries\Kernel\ExternalLibrary\PhpFile\TestPhpLibraryFilesStream.
*/
namespace Drupal\Tests\libraries\Kernel\ExternalLibrary\PhpFile;
use Drupal\libraries\StreamWrapper\PhpLibraryFilesStream;
/**
*
*/
class TestPhpLibraryFilesStream extends PhpLibraryFilesStream {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
/**
* Constructs a test PHP library files stream.
*/
public function __construct() {
$this->moduleHandler = \Drupal::moduleHandler();
}
/**
* {@inheritdoc}
*/
public function getDirectoryPath() {
return $this->moduleHandler->getModule('libraries')->getPath() . '/tests/libraries';
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\libraries\Kernel\ExternalLibrary\TestPhpLibraryFilesStream.
*/
namespace Drupal\Tests\libraries\Kernel\ExternalLibrary;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\libraries\StreamWrapper\LocalHiddenStreamTrait;
use Drupal\libraries\StreamWrapper\PrivateStreamTrait;
/**
* Provides a stream wrapper for accessing test library files.
*/
class TestLibraryFilesStream extends LocalStream {
use LocalHiddenStreamTrait;
use PrivateStreamTrait;
use StringTranslationTrait;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The test directory.
*
* @var string
*/
protected $directory;
/**
* Constructs a stream wrapper for test library files.
*
* Dependency injection is generally not possible to implement for stream
* wrappers, because stream wrappers are initialized before the container is
* booted, but this stream wrapper is only registered explicitly from tests
* so it is possible here.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation handler.
* @param string $directory
* The directory within the Libraries API's tests directory that is to be
* searched for test library files.
*
*/
public function __construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, $directory) {
$this->moduleHandler = $module_handler;
$this->directory = (string) $directory;
$this->setStringTranslation($string_translation);
}
/**
* {@inheritdoc}
*/
public function getName() {
$this->t('Test library files');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
$this->t('Provides access to test library files.');
}
/**
* {@inheritdoc}
*/
public function getDirectoryPath() {
$module_path = $this->moduleHandler->getModule('libraries')->getPath();
return $module_path . '/tests/' . $this->directory;
}
}
......@@ -37,9 +37,8 @@ abstract class ExternalLibraryKernelTestBase extends KernelTestBase {
$this->externalLibraryRegistry = $this->container->get('libraries.registry');
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$root = $this->container->get('app.root');
$module_handler = $this->container->get('module_handler');
$this->modulePath = "$root/" . $module_handler->getModule('libraries')->getPath();
$this->modulePath = $module_handler->getModule('libraries')->getPath();
$this->installConfig('libraries');
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment