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

Issue #2090623 by tstoeckler: Fix path prepending for asset libraries

parent cefff381
No related branches found
No related tags found
No related merge requests found
......@@ -69,6 +69,28 @@ trait LocalRemoteAssetTrait {
return ($this->isInstalled() || $this->hasRemoteUrl());
}
/**
* Gets the prefix to prepend to file paths.
*
* For local libraries this is the library path, for remote libraries this is
* the remote URL.
*
* @return string
* The path prefix.
*/
protected function getPathPrefix() {
/** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface|\Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface $this */
if ($this->isInstalled()) {
return $this->getLocalPath();
}
elseif ($this->hasRemoteUrl()) {
return $this->getRemoteUrl();
}
else {
// @todo Throw an exception.
}
}
/**
* Gets the CSS assets attached to this library.
*
......@@ -84,8 +106,15 @@ trait LocalRemoteAssetTrait {
* @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::getCssAssets()
*/
protected function getCssAssets() {
// @todo Process the paths.
return $this->cssAssets;
// @todo Consider somehow caching the processed information.
$processed_assets = [];
foreach ($this->cssAssets as $category => $category_assets) {
// @todo Somehow consolidate this with getJsAssets().
foreach ($category_assets as $filename => $options) {
$processed_assets[$category][$this->getPathPrefix() . '/' . $filename] = $options;
}
}
return $processed_assets;
}
/**
......@@ -99,8 +128,13 @@ trait LocalRemoteAssetTrait {
* @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::getJsAssets()
*/
protected function getJsAssets() {
// @todo Process the paths.
return $this->jsAssets;
// @todo Consider somehow caching the processed information.
$processed_assets = [];
// @todo Somehow consolidate this with getCssAssets().
foreach ($this->jsAssets as $filename => $options) {
$processed_assets[$this->getPathPrefix() . '/' . $filename] = $options;
}
return $processed_assets;
}
}
......
......@@ -3,7 +3,9 @@
"class": "Drupal\\libraries\\ExternalLibrary\\Asset\\AssetLibrary",
"remote_url": "http://example.com",
"css": {
"example.css": []
"base": {
"example.css": []
}
},
"js": {
"example.js": []
......
......@@ -7,6 +7,9 @@
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\ExternalLibraryKernelTestBase;
/**
......@@ -40,6 +43,32 @@ class AssetLibraryTest extends ExternalLibraryKernelTestBase {
$this->libraryDiscovery = $this->container->get('library.discovery');
}
/**
* Tests that library metadata is correctly gathered.
*/
public function testMetadata() {
try {
/** @var \Drupal\libraries\ExternalLibrary\Asset\AssetLibrary $library */
$library = $this->externalLibraryRegistry->getLibrary('test_asset_library');
$this->assertInstanceOf(AssetLibrary::class, $library);
$this->assertEquals('test_asset_library', $library->getId());
$expected = ['test_asset_library' => [
'version' => 1.0,
'css' => ['base' => ['http://example.com/example.css' => []]],
'js' => ['http://example.com/example.js' => []],
'dependencies' => [],
]];
$this->assertEquals($expected, $library->getAttachableAssetLibraries());
}
catch (LibraryClassNotFoundException $exception) {
$this->fail();
}
catch (LibraryDefinitionNotFoundException $exception) {
$this->fail();
}
}
/**
* Tests that an external asset library is registered as a core asset library.
*
......
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\libraries\Kernel\ExternalLibrary\PhpFile;
use Drupal\Core\Asset\Exception\LibraryDefinitionMissingLicenseException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryClassNotFoundException;
use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
use Drupal\libraries\ExternalLibrary\PhpFile\PhpFileLibrary;
......@@ -32,13 +31,6 @@ class PhpFileLibraryTest extends ExternalLibraryKernelTestBase {
*/
protected $externalLibraryManager;
/**
* The external library registry.
*
* @var \Drupal\libraries\ExternalLibrary\Registry\ExternalLibraryRegistryInterface
*/
protected $externalLibraryRegistry;
/**
* {@inheritdoc}
*/
......@@ -46,7 +38,6 @@ class PhpFileLibraryTest extends ExternalLibraryKernelTestBase {
parent::setUp();
$this->externalLibraryManager = $this->container->get('libraries.manager');
$this->externalLibraryRegistry = $this->container->get('libraries.registry');
$this->container->set('stream_wrapper.php_library_files', new TestPhpLibraryFilesStream());
}
......
......@@ -7,16 +7,19 @@
namespace Drupal\Tests\libraries\Kernel;
use Drupal\Component\FileCache\ApcuFileCacheBackend;
use Drupal\Component\FileCache\FileCache;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\KernelTests\KernelTestBase as CoreKernelTestBase;
use Drupal\Core\Site\Settings;
use Drupal\KernelTests\KernelTestBase;
/**
* Provides an improved version of the core kernel test base class.
*/
abstract class ExternalLibraryKernelTestBase extends CoreKernelTestBase {
abstract class ExternalLibraryKernelTestBase extends KernelTestBase {
/**
* The external library registry.
*
* @var \Drupal\libraries\ExternalLibrary\Registry\ExternalLibraryRegistryInterface
*/
protected $externalLibraryRegistry;
/**
* The absolute path to the Libraries API module.
......@@ -31,6 +34,8 @@ abstract class ExternalLibraryKernelTestBase extends CoreKernelTestBase {
protected function setUp() {
parent::setUp();
$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');
......
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