.../LibraryDefinitionMissingLicenseException.php | 15 +++++ core/lib/Drupal/Core/Asset/LibraryDiscovery.php | 6 ++ core/modules/system/system.routing.yml | 8 --- .../Tests/Core/Asset/LibraryDiscoveryTest.php | 46 ++++++++++++-- .../library_test_files/licenses.libraries.yml | 72 ++++++++++++++++++++++ .../licenses_missing_information.libraries.yml | 9 +++ 6 files changed, 142 insertions(+), 14 deletions(-) diff --git a/core/lib/Drupal/Core/Asset/Exception/LibraryDefinitionMissingLicenseException.php b/core/lib/Drupal/Core/Asset/Exception/LibraryDefinitionMissingLicenseException.php new file mode 100644 index 0000000..2ed2c70 --- /dev/null +++ b/core/lib/Drupal/Core/Asset/Exception/LibraryDefinitionMissingLicenseException.php @@ -0,0 +1,15 @@ +moduleHandler->expects($this->atLeastOnce()) + ->method('moduleExists') + ->with('licenses_missing_information') + ->will($this->returnValue(TRUE)); + + $path = __DIR__ . '/library_test_files'; + $path = substr($path, strlen(DRUPAL_ROOT) + 1); + $this->libraryDiscovery->setPaths('module', 'licenses_missing_information', $path); + + // For libraries without license info, the default license is applied. + $library = $this->libraryDiscovery->getLibraryByName('licenses_missing_information', 'no-license-info-but-remote'); + $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://drupal.org/licensing/faq', + 'gpl-compatible' => TRUE, + ); + $this->assertEquals($library['license'], $default_license); + } + + /** * Tests a library with various licenses, some GPL-compatible, some not. * * @covers ::buildLibrariesByExtension() @@ -400,7 +434,7 @@ public function testLibraryWithLicenses() { 'url' => 'https://drupal.org/licensing/faq', 'gpl-compatible' => TRUE, ); - $this->assertEquals($library['license'], $default_license, 'Default license is applied.'); + $this->assertEquals($library['license'], $default_license); // GPL2-licensed libraries. $library = $this->libraryDiscovery->getLibraryByName('licenses', 'gpl2'); @@ -411,7 +445,7 @@ public function testLibraryWithLicenses() { 'url' => 'https://url-to-gpl2-license', 'gpl-compatible' => TRUE, ); - $this->assertEquals($library['license'], $expected_license, 'Expected license metadata found.'); + $this->assertEquals($library['license'], $expected_license); // MIT-licensed libraries. $library = $this->libraryDiscovery->getLibraryByName('licenses', 'mit'); @@ -422,7 +456,7 @@ public function testLibraryWithLicenses() { 'url' => 'https://url-to-mit-license', 'gpl-compatible' => TRUE, ); - $this->assertEquals($library['license'], $expected_license, 'Expected license metadata found.'); + $this->assertEquals($library['license'], $expected_license); // Libraries in the Public Domain. $library = $this->libraryDiscovery->getLibraryByName('licenses', 'public-domain'); @@ -433,7 +467,7 @@ public function testLibraryWithLicenses() { 'url' => 'https://url-to-public-domain-license', 'gpl-compatible' => TRUE, ); - $this->assertEquals($library['license'], $expected_license, 'Expected license metadata found.'); + $this->assertEquals($library['license'], $expected_license); // Apache-licensed libraries. $library = $this->libraryDiscovery->getLibraryByName('licenses', 'apache'); @@ -444,7 +478,7 @@ public function testLibraryWithLicenses() { 'url' => 'https://url-to-apache-license', 'gpl-compatible' => FALSE, ); - $this->assertEquals($library['license'], $expected_license, 'Expected license metadata found.'); + $this->assertEquals($library['license'], $expected_license); // Copyrighted libraries. $library = $this->libraryDiscovery->getLibraryByName('licenses', 'copyright'); @@ -454,7 +488,7 @@ public function testLibraryWithLicenses() { 'name' => '© Some company', 'gpl-compatible' => FALSE, ); - $this->assertEquals($library['license'], $expected_license, 'Expected license metadata found.'); + $this->assertEquals($library['license'], $expected_license); } /** diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses.libraries.yml new file mode 100644 index 0000000..455b4c5 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses.libraries.yml @@ -0,0 +1,72 @@ +# No license information: should default to GPL 2.0 or later.. +no-license-info: + version: 1.0 + js: + no-license-info.js: {} + css: + base: + no-license-info.css: {} + +# A library with all assets licensed under the GPL-compatible GPL 2 license. +gpl2: + version: 1.0 + license: + name: gpl2 + url: https://url-to-gpl2-license + gpl-compatible: true + js: + gpl2.js: {} + css: + base: + gpl2.css: {} + +# A library with all assets licensed under the GPL-compatible MIT license. +mit: + version: 1.0 + license: + name: MIT + url: https://url-to-mit-license + gpl-compatible: true + js: + mit.js: {} + css: + base: + mit.css: {} + +# A library with all assets licensed under the GPL-compatible Public Domain. +public-domain: + version: 1.0 + license: + name: Public Domain + url: https://url-to-public-domain-license + gpl-compatible: true + js: + public-domain.js: {} + css: + base: + public-domain.css: {} + +# A library with all assets licensed under the GPL-incompatible Apache license. +apache: + version: 1.0 + license: + name: apache + url: https://url-to-apache-license + gpl-compatible: false + js: + apache.js: {} + css: + base: + apache.css: {} + +# A library with all assets licensed under the GPL-incompatible copyright. +copyright: + version: 1.0 + license: + name: © Some company + gpl-compatible: false + js: + copyright.js: {} + css: + base: + copyright.css: {} diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses_missing_information.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses_missing_information.libraries.yml new file mode 100644 index 0000000..f36d061 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/licenses_missing_information.libraries.yml @@ -0,0 +1,9 @@ +# No license information for a 3rd party library (it has the 'remote' key). +no-license-info-but-remote: + version: 1.0 + remote: https://url-to-remote + js: + no-license-info-but-remote.js: {} + css: + base: + no-license-info-but-remote.css: {}