Commit 0d82715b authored by catch's avatar catch

Issue #3064017 by mikelutz, Wim Leers, dww, catch: Create a means to mark an...

Issue #3064017 by mikelutz, Wim Leers, dww, catch: Create a means to mark an asset library as deprecated in a *.libraries.yml file
parent 6033d397
......@@ -55,8 +55,14 @@ public function getLibrariesByExtension($extension) {
* {@inheritdoc}
*/
public function getLibraryByName($extension, $name) {
$extension = $this->getLibrariesByExtension($extension);
return isset($extension[$name]) ? $extension[$name] : FALSE;
$libraries = $this->getLibrariesByExtension($extension);
if (!isset($libraries[$name])) {
return FALSE;
}
if (isset($libraries[$name]['deprecated'])) {
@trigger_error(str_replace('%library_id%', "$extension/$name", $libraries[$name]['deprecated']), E_USER_DEPRECATED);
}
return $libraries[$name];
}
/**
......
......@@ -50,6 +50,13 @@ class LibraryDiscoveryTest extends UnitTestCase {
],
'css' => [],
],
'test_3' => [
'js' => [
'baz.js' => [],
],
'css' => [],
'deprecated' => 'The "%library_id%" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the test_2 library instead. See https://www.example.com',
],
];
/**
......@@ -63,17 +70,16 @@ protected function setUp() {
->disableOriginalConstructor()
->getMock();
$this->libraryDiscovery = new LibraryDiscovery($this->libraryDiscoveryCollector, $this->cacheTagsInvalidator);
$this->libraryDiscoveryCollector->expects($this->once())
->method('get')
->with('test')
->willReturn($this->libraryData);
}
/**
* @covers ::getLibrariesByExtension
*/
public function testGetLibrariesByExtension() {
$this->libraryDiscoveryCollector->expects($this->once())
->method('get')
->with('test')
->willReturn($this->libraryData);
$this->libraryDiscovery->getLibrariesbyExtension('test');
// Verify that subsequent calls don't trigger hook_library_info_alter()
// and hook_js_settings_alter() invocations, nor do they talk to the
......@@ -84,4 +90,38 @@ public function testGetLibrariesByExtension() {
$this->libraryDiscovery->getLibrariesbyExtension('test');
}
/**
* Tests getting a library by name.
*
* @covers ::getLibraryByName
*/
public function testGetLibraryByName() {
$this->assertSame($this->libraryData['test_1'], $this->libraryDiscovery->getLibraryByName('test', 'test_1'));
}
/**
* Tests getting a deprecated library.
*/
public function testAssetLibraryDeprecation() {
$previous_error_handler = set_error_handler(function ($severity, $message, $file, $line, $context) use (&$previous_error_handler) {
// Convert deprecation error into a catchable exception.
if ($severity === E_USER_DEPRECATED) {
throw new \ErrorException($message, 0, $severity, $file, $line);
}
if ($previous_error_handler) {
return $previous_error_handler($severity, $message, $file, $line, $context);
}
});
try {
$this->libraryDiscovery->getLibraryByName('test', 'test_3');
$this->fail('No deprecation error triggered.');
}
catch (\ErrorException $e) {
$this->assertSame('The "test/test_3" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the test_2 library instead. See https://www.example.com', $e->getMessage());
}
restore_error_handler();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment