View source
<?php
namespace Drupal\Tests\Core\Asset;
use Drupal\Core\Asset\LibraryDiscovery;
use Drupal\Tests\UnitTestCase;
class LibraryDiscoveryTest extends UnitTestCase {
protected $libraryDiscovery;
protected $libraryDiscoveryCollector;
protected $cacheTagsInvalidator;
protected $libraryData = [
'test_1' => [
'js' => [],
'css' => [
'foo.css' => [],
],
],
'test_2' => [
'js' => [
'bar.js' => [],
],
'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',
],
];
protected function setUp() : void {
parent::setUp();
$this->cacheTagsInvalidator = $this
->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$this->libraryDiscoveryCollector = $this
->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscoveryCollector')
->disableOriginalConstructor()
->getMock();
$this->libraryDiscovery = new LibraryDiscovery($this->libraryDiscoveryCollector, $this->cacheTagsInvalidator);
$this->libraryDiscoveryCollector
->expects($this
->once())
->method('get')
->with('test')
->willReturn($this->libraryData);
}
public function testGetLibrariesByExtension() {
$this->libraryDiscovery
->getLibrariesByExtension('test');
$this->libraryDiscovery
->getLibraryByName('test', 'test_1');
$this->libraryDiscovery
->getLibrariesByExtension('test');
}
public function testGetLibraryByName() {
$this
->assertSame($this->libraryData['test_1'], $this->libraryDiscovery
->getLibraryByName('test', 'test_1'));
}
public function testAssetLibraryDeprecation() {
$previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {
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);
}
});
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();
}
}