View source
<?php
namespace Drupal\Tests\Core\Asset;
use Drupal\Core\Asset\LibraryDiscoveryParser;
use Drupal\Tests\UnitTestCase;
class LibraryDiscoveryParserTest extends UnitTestCase {
protected $libraryDiscoveryParser;
protected $cache;
protected $moduleHandler;
protected $themeManager;
protected $lock;
protected function setUp() {
parent::setUp();
$this->moduleHandler = $this
->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->themeManager = $this
->getMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
$mock_active_theme = $this
->getMockBuilder('Drupal\\Core\\Theme\\ActiveTheme')
->disableOriginalConstructor()
->getMock();
$mock_active_theme
->expects($this
->any())
->method('getLibrariesOverride')
->willReturn([]);
$this->themeManager
->expects($this
->any())
->method('getActiveTheme')
->willReturn($mock_active_theme);
$this->libraryDiscoveryParser = new TestLibraryDiscoveryParser($this->root, $this->moduleHandler, $this->themeManager);
}
public function testBuildByExtensionSimple() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('example_module')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'example_module', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('example_module', 'example');
$library = $libraries['example'];
$this
->assertCount(0, $library['js']);
$this
->assertCount(1, $library['css']);
$this
->assertCount(0, $library['dependencies']);
$this
->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
$this
->assertEquals(\Drupal::VERSION, $library['version']);
}
public function testBuildByExtensionWithTheme() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('example_theme')
->will($this
->returnValue(FALSE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('theme', 'example_theme', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('example_theme');
$library = $libraries['example'];
$this
->assertCount(0, $library['js']);
$this
->assertCount(1, $library['css']);
$this
->assertCount(0, $library['dependencies']);
$this
->assertEquals($path . '/css/example.css', $library['css'][0]['data']);
}
public function testBuildByExtensionWithMissingLibraryFile() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('example_module')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files_not_existing';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'example_module', $path);
$this
->assertSame($this->libraryDiscoveryParser
->buildByExtension('example_module'), array());
}
public function testInvalidLibrariesFile() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('invalid_file')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'invalid_file', $path);
$this->libraryDiscoveryParser
->buildByExtension('invalid_file');
}
public function testBuildByExtensionWithMissingInformation() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('example_module_missing_information')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'example_module_missing_information', $path);
$this->libraryDiscoveryParser
->buildByExtension('example_module_missing_information');
}
public function testVersion() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('versions')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'versions', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('versions');
$this
->assertFalse(array_key_exists('version', $libraries['versionless']));
$this
->assertEquals(-1, $libraries['versionless']['css'][0]['version']);
$this
->assertEquals(-1, $libraries['versionless']['js'][0]['version']);
$this
->assertEquals('9.8.7.6', $libraries['versioned']['version']);
$this
->assertEquals('9.8.7.6', $libraries['versioned']['css'][0]['version']);
$this
->assertEquals('9.8.7.6', $libraries['versioned']['js'][0]['version']);
$this
->assertEquals(\Drupal::VERSION, $libraries['core-versioned']['version']);
$this
->assertEquals(\Drupal::VERSION, $libraries['core-versioned']['css'][0]['version']);
$this
->assertEquals(\Drupal::VERSION, $libraries['core-versioned']['js'][0]['version']);
}
public function testNonStringVersion() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('versions')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'versions', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('versions');
$this
->assertNotEquals('2014-12-13', $libraries['invalid-version']['version']);
$this
->assertEquals('2014-12-13', $libraries['valid-version']['version']);
}
public function testExternalLibraries() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('external')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'external', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('external');
$library = $libraries['example_external'];
$this
->assertEquals('http://example.com/css/example_external.css', $library['css'][0]['data']);
$this
->assertEquals('http://example.com/example_external.js', $library['js'][0]['data']);
$this
->assertEquals('3.14', $library['version']);
}
public function testDefaultCssWeights() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('css_weights')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'css_weights', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('css_weights');
$library = $libraries['example'];
$css = $library['css'];
$this
->assertCount(10, $css);
$this
->assertEquals(200, $css[0]['weight']);
$this
->assertEquals(200 + 29, $css[1]['weight']);
$this
->assertEquals(-200, $css[2]['weight']);
$this
->assertEquals(-200 + 97, $css[3]['weight']);
$this
->assertEquals(-100, $css[4]['weight']);
$this
->assertEquals(-100 + 92, $css[5]['weight']);
$this
->assertEquals(0, $css[6]['weight']);
$this
->assertEquals(45, $css[7]['weight']);
$this
->assertEquals(100, $css[8]['weight']);
$this
->assertEquals(100 + 8, $css[9]['weight']);
}
public function testJsWithPositiveWeight() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('js_positive_weight')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'js_positive_weight', $path);
$this->libraryDiscoveryParser
->buildByExtension('js_positive_weight');
}
public function testLibraryWithCssJsSetting() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('css_js_settings')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'css_js_settings', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('css_js_settings');
$library = $libraries['example'];
$this
->assertEquals(-100, $library['js'][0]['group']);
$this
->assertEquals('file', $library['js'][0]['type']);
$this
->assertEquals($path . '/js/example.js', $library['js'][0]['data']);
$this
->assertEquals(0, $library['css'][0]['group']);
$this
->assertEquals('file', $library['css'][0]['type']);
$this
->assertEquals($path . '/css/base.css', $library['css'][0]['data']);
$this
->assertEquals(array(
'key' => 'value',
), $library['drupalSettings']);
}
public function testLibraryWithDependencies() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('dependencies')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'dependencies', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('dependencies');
$library = $libraries['example'];
$this
->assertCount(2, $library['dependencies']);
$this
->assertEquals('external/example_external', $library['dependencies'][0]);
$this
->assertEquals('example_module/example', $library['dependencies'][1]);
}
public function testLibraryWithDataTypes() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('data_types')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'data_types', $path);
$this->libraryDiscoveryParser
->setFileValidUri('public://test.css', TRUE);
$this->libraryDiscoveryParser
->setFileValidUri('public://test2.css', FALSE);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('data_types');
$library = $libraries['example'];
$this
->assertCount(5, $library['css']);
$this
->assertEquals('external', $library['css'][0]['type']);
$this
->assertEquals('http://example.com/test.css', $library['css'][0]['data']);
$this
->assertEquals('file', $library['css'][1]['type']);
$this
->assertEquals('tmp/test.css', $library['css'][1]['data']);
$this
->assertEquals('external', $library['css'][2]['type']);
$this
->assertEquals('//cdn.com/test.css', $library['css'][2]['data']);
$this
->assertEquals('file', $library['css'][3]['type']);
$this
->assertEquals('public://test.css', $library['css'][3]['data']);
}
public function testLibraryWithJavaScript() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('js')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'js', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('js');
$library = $libraries['example'];
$this
->assertCount(2, $library['js']);
$this
->assertEquals(FALSE, $library['js'][0]['minified']);
$this
->assertEquals(TRUE, $library['js'][1]['minified']);
}
public function testLibraryThirdPartyWithMissingLicense() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('licenses_missing_information')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'licenses_missing_information', $path);
$this->libraryDiscoveryParser
->buildByExtension('licenses_missing_information');
}
public function testLibraryWithLicenses() {
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('moduleExists')
->with('licenses')
->will($this
->returnValue(TRUE));
$path = __DIR__ . '/library_test_files';
$path = substr($path, strlen($this->root) + 1);
$this->libraryDiscoveryParser
->setPaths('module', 'licenses', $path);
$libraries = $this->libraryDiscoveryParser
->buildByExtension('licenses');
$library = $libraries['no-license-info'];
$this
->assertCount(1, $library['css']);
$this
->assertCount(1, $library['js']);
$this
->assertTrue(isset($library['license']));
$default_license = array(
'name' => 'GNU-GPL-2.0-or-later',
'url' => 'https://www.drupal.org/licensing/faq',
'gpl-compatible' => TRUE,
);
$this
->assertEquals($library['license'], $default_license);
$library = $libraries['gpl2'];
$this
->assertCount(1, $library['css']);
$this
->assertCount(1, $library['js']);
$expected_license = array(
'name' => 'gpl2',
'url' => 'https://url-to-gpl2-license',
'gpl-compatible' => TRUE,
);
$this
->assertEquals($library['license'], $expected_license);
$library = $libraries['mit'];
$this
->assertCount(1, $library['css']);
$this
->assertCount(1, $library['js']);
$expected_license = array(
'name' => 'MIT',
'url' => 'https://url-to-mit-license',
'gpl-compatible' => TRUE,
);
$this
->assertEquals($library['license'], $expected_license);
$library = $libraries['public-domain'];
$this
->assertCount(1, $library['css']);
$this
->assertCount(1, $library['js']);
$expected_license = array(
'name' => 'Public Domain',
'url' => 'https://url-to-public-domain-license',
'gpl-compatible' => TRUE,
);
$this
->assertEquals($library['license'], $expected_license);
$library = $libraries['apache'];
$this
->assertCount(1, $library['css']);
$this
->assertCount(1, $library['js']);
$expected_license = array(
'name' => 'apache',
'url' => 'https://url-to-apache-license',
'gpl-compatible' => FALSE,
);
$this
->assertEquals($library['license'], $expected_license);
$library = $libraries['copyright'];
$this
->assertCount(1, $library['css']);
$this
->assertCount(1, $library['js']);
$expected_license = array(
'name' => '© Some company',
'gpl-compatible' => FALSE,
);
$this
->assertEquals($library['license'], $expected_license);
}
}
class TestLibraryDiscoveryParser extends LibraryDiscoveryParser {
protected $paths;
protected $validUris;
protected function drupalGetPath($type, $name) {
return isset($this->paths[$type][$name]) ? $this->paths[$type][$name] : NULL;
}
public function setPaths($type, $name, $path) {
$this->paths[$type][$name] = $path;
}
protected function fileValidUri($source) {
return isset($this->validUris[$source]) ? $this->validUris[$source] : FALSE;
}
public function setFileValidUri($source, $valid) {
$this->validUris[$source] = $valid;
}
}
if (!defined('CSS_AGGREGATE_DEFAULT')) {
define('CSS_AGGREGATE_DEFAULT', 0);
}
if (!defined('CSS_AGGREGATE_THEME')) {
define('CSS_AGGREGATE_THEME', 100);
}
if (!defined('CSS_BASE')) {
define('CSS_BASE', -200);
}
if (!defined('CSS_LAYOUT')) {
define('CSS_LAYOUT', -100);
}
if (!defined('CSS_COMPONENT')) {
define('CSS_COMPONENT', 0);
}
if (!defined('CSS_STATE')) {
define('CSS_STATE', 100);
}
if (!defined('CSS_THEME')) {
define('CSS_THEME', 200);
}
if (!defined('JS_SETTING')) {
define('JS_SETTING', -200);
}
if (!defined('JS_LIBRARY')) {
define('JS_LIBRARY', -100);
}
if (!defined('JS_DEFAULT')) {
define('JS_DEFAULT', 0);
}
if (!defined('JS_THEME')) {
define('JS_THEME', 100);
}