From b039b4d77dac8cf0b2d8b8cd195f6187cefcd2b2 Mon Sep 17 00:00:00 2001 From: "lee.5151" <lee.5151@osu.edu> Date: Mon, 28 Nov 2022 12:57:36 -0500 Subject: [PATCH] Upgrading drupal/libraries (3.0.0-beta1 => 4.0.0) --- composer.json | 2 +- composer.lock | 22 +- vendor/composer/installed.json | 30 +- vendor/composer/installed.php | 10 +- web/modules/libraries/libraries.api.php | 254 ++++----- web/modules/libraries/libraries.drush.inc | 66 ++- web/modules/libraries/libraries.info.yml | 9 +- web/modules/libraries/libraries.module | 119 ++-- .../Plugin/libraries/Locator/UriLocator.php | 3 +- .../libraries/Type/AssetLibraryType.php | 3 +- .../Type/MultipleAssetLibraryType.php | 2 +- .../VersionDetector/LinePatternDetector.php | 2 +- .../libraries/src/Tests/LibrariesUnitTest.php | 53 -- .../libraries/src/Tests/LibrariesWebTest.php | 525 ------------------ .../example_info_file.libraries.info.yml | 6 +- .../libraries_test/libraries_test.info.yml | 8 +- .../libraries_test/libraries_test.module | 384 ++++++------- .../DefinitionDiscoveryFactoryTest.php | 15 +- .../Asset/AssetLibraryTestBase.php | 4 +- .../ExternalLibrary/GlobalLocatorTest.php | 2 +- .../PhpFile/PhpFileLibraryTest.php | 2 +- .../Kernel/LibrariesApi/LibrariesUnitTest.php | 53 ++ .../src/Kernel/LibraryTypeKernelTestBase.php | 6 +- .../LinePatternDetectorTest.php | 9 +- 24 files changed, 542 insertions(+), 1047 deletions(-) delete mode 100644 web/modules/libraries/src/Tests/LibrariesUnitTest.php delete mode 100644 web/modules/libraries/src/Tests/LibrariesWebTest.php create mode 100644 web/modules/libraries/tests/src/Kernel/LibrariesApi/LibrariesUnitTest.php diff --git a/composer.json b/composer.json index fdb70b8e51..cae9b3ec72 100644 --- a/composer.json +++ b/composer.json @@ -128,7 +128,7 @@ "drupal/google_tag": "1.5", "drupal/honeypot": "2.1.2", "drupal/inline_entity_form": "1.0-rc9", - "drupal/libraries": "3.0-beta1", + "drupal/libraries": "4.0", "drupal/link_attributes": "1.11", "drupal/linkit": "5.0-beta13", "drupal/mathjax": "3.0.0", diff --git a/composer.lock b/composer.lock index 9f77354148..bf53b2708f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "34725321c1b77f13acdd41e96c52fb5d", + "content-hash": "4d2c6288539f72a84326f6403602c30c", "packages": [ { "name": "alchemy/zippy", @@ -5119,29 +5119,29 @@ }, { "name": "drupal/libraries", - "version": "3.0.0-beta1", + "version": "4.0.0", "source": { "type": "git", "url": "https://git.drupalcode.org/project/libraries.git", - "reference": "8.x-3.0-beta1" + "reference": "4.0.0" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/libraries-8.x-3.0-beta1.zip", - "reference": "8.x-3.0-beta1", - "shasum": "7843870c52251cc5290e1cdca94524f71edbf016" + "url": "https://ftp.drupal.org/files/projects/libraries-4.0.0.zip", + "reference": "4.0.0", + "shasum": "52c0b4ecb0566d36afdeb5f8cf780d085a41fc64" }, "require": { - "drupal/core": "^8 || ^9" + "drupal/core": "^8.8 || ^9 || ^10" }, "type": "drupal-module", "extra": { "drupal": { - "version": "8.x-3.0-beta1", - "datestamp": "1618261123", + "version": "4.0.0", + "datestamp": "1666346899", "security-coverage": { - "status": "not-covered", - "message": "Beta releases are not covered by Drupal security advisories." + "status": "covered", + "message": "Covered by Drupal's security advisory policy" } } }, diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index ffdd21e445..c077d8c1c2 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -5283,30 +5283,30 @@ }, { "name": "drupal/libraries", - "version": "3.0.0-beta1", - "version_normalized": "3.0.0.0-beta1", + "version": "4.0.0", + "version_normalized": "4.0.0.0", "source": { "type": "git", "url": "https://git.drupalcode.org/project/libraries.git", - "reference": "8.x-3.0-beta1" + "reference": "4.0.0" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/libraries-8.x-3.0-beta1.zip", - "reference": "8.x-3.0-beta1", - "shasum": "7843870c52251cc5290e1cdca94524f71edbf016" + "url": "https://ftp.drupal.org/files/projects/libraries-4.0.0.zip", + "reference": "4.0.0", + "shasum": "52c0b4ecb0566d36afdeb5f8cf780d085a41fc64" }, "require": { - "drupal/core": "^8 || ^9" + "drupal/core": "^8.8 || ^9 || ^10" }, "type": "drupal-module", "extra": { "drupal": { - "version": "8.x-3.0-beta1", - "datestamp": "1609758293", + "version": "4.0.0", + "datestamp": "1666346899", "security-coverage": { - "status": "not-covered", - "message": "Beta releases are not covered by Drupal security advisories." + "status": "covered", + "message": "Covered by Drupal's security advisory policy" } } }, @@ -5316,10 +5316,6 @@ "GPL-2.0-or-later" ], "authors": [ - { - "name": "Pol", - "homepage": "https://www.drupal.org/user/47194" - }, { "name": "joseph.olstad", "homepage": "https://www.drupal.org/user/1321830" @@ -5328,6 +5324,10 @@ "name": "podarok", "homepage": "https://www.drupal.org/user/116002" }, + { + "name": "Pol", + "homepage": "https://www.drupal.org/user/47194" + }, { "name": "rjacobs", "homepage": "https://www.drupal.org/user/422459" diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index bddb3ca2ac..695ee22b7b 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'osu-asc-webservices/d8-upstream', 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => '429f72e15c8ceea994931b8d1abf14e67c0e8c20', + 'reference' => 'b32d6543d3f3958296e85fa3c3e6faf263f8a478', 'type' => 'project', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -902,9 +902,9 @@ 'dev_requirement' => false, ), 'drupal/libraries' => array( - 'pretty_version' => '3.0.0-beta1', - 'version' => '3.0.0.0-beta1', - 'reference' => '8.x-3.0-beta1', + 'pretty_version' => '4.0.0', + 'version' => '4.0.0.0', + 'reference' => '4.0.0', 'type' => 'drupal-module', 'install_path' => __DIR__ . '/../../web/modules/libraries', 'aliases' => array(), @@ -1594,7 +1594,7 @@ 'osu-asc-webservices/d8-upstream' => array( 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => '429f72e15c8ceea994931b8d1abf14e67c0e8c20', + 'reference' => 'b32d6543d3f3958296e85fa3c3e6faf263f8a478', 'type' => 'project', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), diff --git a/web/modules/libraries/libraries.api.php b/web/modules/libraries/libraries.api.php index 859e6023eb..ec4a3e8795 100644 --- a/web/modules/libraries/libraries.api.php +++ b/web/modules/libraries/libraries.api.php @@ -341,7 +341,7 @@ function hook_libraries_info() { // This array key lets Libraries API search for 'sites/all/libraries/example' // directory, which should contain the entire, original extracted library. - $libraries['example'] = array( + $libraries['example'] = [ // Only used in administrative UI of Libraries API. 'name' => 'Example library', 'vendor url' => 'http://example.com', @@ -353,53 +353,53 @@ function hook_libraries_info() { 'version callback' => 'mymodule_get_version', // Specify arguments for the version callback. By default, // libraries_get_version() takes a named argument array: - 'version arguments' => array( + 'version arguments' => [ 'file' => 'docs/CHANGELOG.txt', 'pattern' => '@version\s+([0-9a-zA-Z\.-]+)@', 'lines' => 5, 'cols' => 20, - ), + ], // Default list of files of the library to load. Important: Only specify // third-party files belonging to the library here, not integration files of // your module. - 'files' => array( + 'files' => [ // 'js' and 'css' follow the syntax of hook_library(), but file paths are // relative to the library path. - 'js' => array( + 'js' => [ 'exlib.js', 'gadgets/foo.js', - ), - 'css' => array( + ], + 'css' => [ 'lib_style.css', 'skin/example.css', - ), + ], // For PHP libraries, specify include files here, still relative to the // library path. - 'php' => array( + 'php' => [ 'exlib.php', 'exlib.inc', - ), - ), + ], + ], // Optional: Specify alternative variants of the library, if available. - 'variants' => array( + 'variants' => [ // All properties defined for 'minified' override top-level properties. - 'minified' => array( - 'files' => array( - 'js' => array( + 'minified' => [ + 'files' => [ + 'js' => [ 'exlib.min.js', 'gadgets/foo.min.js', - ), - 'css' => array( + ], + 'css' => [ 'lib_style.css', 'skin/example.css', - ), - ), + ], + ], 'variant callback' => 'mymodule_check_variant', - 'variant arguments' => array( + 'variant arguments' => [ 'variant' => 'minified', - ), - ), - ), + ], + ], + ], // Optional, but usually required: Override top-level properties for later // versions of the library. The properties of the minimum version that is // matched override the top-level properties. Note: @@ -409,85 +409,85 @@ function hook_libraries_info() { // defaults. // - The array keys have to be strings, as PHP does not support floats for // array keys. - 'versions' => array( - '2' => array( - 'files' => array( - 'js' => array('exlib.js'), - 'css' => array('exlib_style.css'), - ), - ), - '3.0' => array( - 'files' => array( - 'js' => array('exlib.js'), - 'css' => array('lib_style.css'), - ), - ), - '3.2' => array( - 'files' => array( - 'js' => array( + 'versions' => [ + '2' => [ + 'files' => [ + 'js' => ['exlib.js'], + 'css' => ['exlib_style.css'], + ], + ], + '3.0' => [ + 'files' => [ + 'js' => ['exlib.js'], + 'css' => ['lib_style.css'], + ], + ], + '3.2' => [ + 'files' => [ + 'js' => [ 'exlib.js', 'gadgets/foo.js', - ), - 'css' => array( + ], + 'css' => [ 'lib_style.css', 'skin/example.css', - ), - ), - ), - ), + ], + ], + ], + ], // Optional: Register files to auto-load for your module. All files must be // keyed by module, and follow the syntax of the 'files' property. - 'integration files' => array( - 'mymodule' => array( - 'js' => array('ex_lib.inc'), - ), - ), + 'integration files' => [ + 'mymodule' => [ + 'js' => ['ex_lib.inc'], + ], + ], // Optionally register callbacks to apply to the library during different // stages of its lifetime ('callback groups'). - 'callbacks' => array( + 'callbacks' => [ // Used to alter the info associated with the library. - 'info' => array( + 'info' => [ 'mymodule_example_libraries_info_callback', - ), + ], // Called before detecting the given library. - 'pre-detect' => array( + 'pre-detect' => [ 'mymodule_example_libraries_predetect_callback', - ), + ], // Called after detecting the library. - 'post-detect' => array( + 'post-detect' => [ 'mymodule_example_libraries_postdetect_callback', - ), + ], // Called before the library is loaded. - 'pre-load' => array( + 'pre-load' => [ 'mymodule_example_libraries_preload_callback', - ), + ], // Called after the library is loaded. - 'post-load' => array( + 'post-load' => [ 'mymodule_example_libraries_postload_callback', - ), - ), - ); + ], + ], + ]; // A very simple library. No changing APIs (hence, no versions), no variants. // Expected to be extracted into 'sites/all/libraries/simple'. - $libraries['simple'] = array( + $libraries['simple'] = [ 'name' => 'Simple library', 'vendor url' => 'http://example.com/simple', 'download url' => 'http://example.com/simple', - 'version arguments' => array( + 'version arguments' => [ 'file' => 'readme.txt', // Best practice: Document the actual version strings for later reference. // 1.x: Version 1.0 'pattern' => '/Version (\d+)/', 'lines' => 5, - ), - 'files' => array( - 'js' => array('simple.js'), - ), - ); + ], + 'files' => [ + 'js' => ['simple.js'], + ], + ]; // A library that (naturally) evolves over time with API changes. - $libraries['tinymce'] = array( + $libraries['tinymce'] = [ 'name' => 'TinyMCE', 'vendor url' => 'http://tinymce.moxiecode.com', 'download url' => 'http://tinymce.moxiecode.com/download.php', @@ -495,7 +495,7 @@ function hook_libraries_info() { // The regular expression catches two parts (the major and the minor // version), which libraries_get_version() doesn't allow. 'version callback' => 'tinymce_get_version', - 'version arguments' => array( + 'version arguments' => [ // It can be easier to parse the first characters of a minified file // instead of doing a multi-line pattern matching in a source file. See // 'lines' and 'cols' below. @@ -506,61 +506,61 @@ function hook_libraries_info() { 'pattern' => '@majorVersion[=:]["\'](\d).+?minorVersion[=:]["\']([\d\.]+)@', 'lines' => 1, 'cols' => 100, - ), - 'versions' => array( - '2.1' => array( - 'files' => array( - 'js' => array('tiny_mce.js'), - ), - 'variants' => array( - 'source' => array( - 'files' => array( - 'js' => array('tiny_mce_src.js'), - ), - ), - ), - 'integration files' => array( - 'wysiwyg' => array( - 'js' => array('editors/js/tinymce-2.js'), - 'css' => array('editors/js/tinymce-2.css'), - ), - ), - ), + ], + 'versions' => [ + '2.1' => [ + 'files' => [ + 'js' => ['tiny_mce.js'], + ], + 'variants' => [ + 'source' => [ + 'files' => [ + 'js' => ['tiny_mce_src.js'], + ], + ], + ], + 'integration files' => [ + 'wysiwyg' => [ + 'js' => ['editors/js/tinymce-2.js'], + 'css' => ['editors/js/tinymce-2.css'], + ], + ], + ], // Definition used if 3.1 or above is detected. - '3.1' => array( + '3.1' => [ // Does not support JS aggregation. - 'files' => array( - 'js' => array( - 'tiny_mce.js' => array('preprocess' => FALSE), - ), - ), - 'variants' => array( + 'files' => [ + 'js' => [ + 'tiny_mce.js' => ['preprocess' => FALSE], + ], + ], + 'variants' => [ // New variant leveraging jQuery. Not stable yet; therefore not the // default variant. - 'jquery' => array( - 'files' => array( - 'js' => array( - 'tiny_mce_jquery.js' => array('preprocess' => FALSE), - ), - ), - ), - 'source' => array( - 'files' => array( - 'js' => array( - 'tiny_mce_src.js' => array('preprocess' => FALSE), - ), - ), - ), - ), - 'integration files' => array( - 'wysiwyg' => array( - 'js' => array('editors/js/tinymce-3.js'), - 'css' => array('editors/js/tinymce-3.css'), - ), - ), - ), - ), - ); + 'jquery' => [ + 'files' => [ + 'js' => [ + 'tiny_mce_jquery.js' => ['preprocess' => FALSE], + ], + ], + ], + 'source' => [ + 'files' => [ + 'js' => [ + 'tiny_mce_src.js' => ['preprocess' => FALSE], + ], + ], + ], + ], + 'integration files' => [ + 'wysiwyg' => [ + 'js' => ['editors/js/tinymce-3.js'], + 'css' => ['editors/js/tinymce-3.css'], + ], + ], + ], + ], + ]; return $libraries; } @@ -577,9 +577,9 @@ function hook_libraries_info() { * @deprecated Will be removed before a stable Drupal 8 release. */ function hook_libraries_info_alter(&$libraries) { - $files = array( - 'php' => array('example_module.php_spellchecker.inc'), - ); + $files = [ + 'php' => ['example_module.php_spellchecker.inc'], + ]; $libraries['php_spellchecker']['integration files']['example_module'] = $files; } @@ -605,5 +605,5 @@ function hook_libraries_info_alter(&$libraries) { function hook_libraries_info_file_paths() { // Taken from the Libraries test module, which needs to specify the path to // the test library. - return array(drupal_get_path('module', 'libraries_test') . '/example'); + return [\Drupal::service('extension.list.module')->getPath('libraries_test') . '/example']; } diff --git a/web/modules/libraries/libraries.drush.inc b/web/modules/libraries/libraries.drush.inc index 3d9f7cd429..fe9003b7ae 100644 --- a/web/modules/libraries/libraries.drush.inc +++ b/web/modules/libraries/libraries.drush.inc @@ -11,18 +11,20 @@ * Implements hook_drush_command(). */ function libraries_drush_command() { - $items['libraries-list'] = array( + $items['libraries-list'] = [ 'callback' => 'libraries_drush_list', 'description' => dt('Lists registered library information.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, - ); - /**$items['libraries-download'] = array( - 'callback' => 'libraries_drush_download', - 'description' => dt('Downloads a registered library into the libraries directory for the active site.'), - 'arguments' => array( - 'name' => dt('The internal name of the registered library.'), - ), - );*/ + ]; + /* + $items['libraries-download'] = array[ + 'callback' => 'libraries_drush_download', + 'description' => dt('Downloads a registered library into the libraries directory for the active site.'), + 'arguments' => [ + 'name' => dt('The internal name of the registered library.'), + ], + ]; + */ return $items; } @@ -61,7 +63,7 @@ function libraries_drush_invalidate_cache() { * Lists registered library information. */ function libraries_drush_list() { - $libraries = array(); + $libraries = []; foreach (\Drupal::service('libraries.manager')->info() as $name => $info) { $libraries[$name] = \Drupal::service('libraries.manager')->getLibrary($name); } @@ -72,16 +74,22 @@ function libraries_drush_list() { } else { - $rows = array(); + $rows = []; // drush_print_table() automatically treats the first row as the header, if // $header is TRUE. - $rows[] = array(dt('Name'), dt('Status'), dt('Version'), dt('Variants'), dt('Dependencies')); + $rows[] = [ + dt('Name'), + dt('Status'), + dt('Version'), + dt('Variants'), + dt('Dependencies'), + ]; foreach ($libraries as $name => $library) { $status = ($library['installed'] ? dt('OK') : Unicode::ucfirst($library['error'])); $version = (($library['installed'] && !empty($library['version'])) ? $library['version'] : '-'); // Only list installed variants. - $variants = array(); + $variants = []; foreach ($library['variants'] as $variant_name => $variant) { if ($variant['installed']) { $variants[] = $variant_name; @@ -91,11 +99,11 @@ function libraries_drush_list() { $dependencies = (!empty($library['dependencies']) ? implode(', ', $library['dependencies']) : '-'); - $rows[] = array($name, $status, $version, $variants, $dependencies); + $rows[] = [$name, $status, $version, $variants, $dependencies]; } // Make the possible values for the 'Status' column and the 'Version' header // wrap nicely. - $widths = array(0, 12, 7, 0, 0); + $widths = [0, 12, 7, 0, 0]; drush_print_table($rows, TRUE, $widths); } } @@ -103,7 +111,7 @@ function libraries_drush_list() { /** * Downloads a library. * - * @param $name + * @param string $name * The internal name of the library to download. */ function libraries_drush_download($name) { @@ -126,7 +134,7 @@ function libraries_drush_download($name) { // Create the path if it does not exist. if (!is_dir($path)) { drush_op('mkdir', $path); - drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice'); + drush_log(dt('Directory @path was created', ['@path' => $path]), 'notice'); } // Set the directory to the download location. @@ -136,34 +144,34 @@ function libraries_drush_download($name) { $filename = basename(COLORBOX_DOWNLOAD_URI); $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip'); - // Remove any existing Colorbox plugin directory + // Remove any existing Colorbox plugin directory. if (is_dir($dirname)) { - drush_log(dt('A existing Colorbox plugin was overwritten at @path', array('@path' => $path)), 'notice'); + drush_log(dt('A existing Colorbox plugin was overwritten at @path', ['@path' => $path]), 'notice'); } - // Remove any existing Colorbox plugin zip archive + // Remove any existing Colorbox plugin zip archive. if (is_file($filename)) { drush_op('unlink', $filename); } - // Download the zip archive - if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) { - drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI); + // Download the zip archive. + if (!drush_shell_exec('wget ' . COLORBOX_DOWNLOAD_URI)) { + drush_shell_exec('curl -O ' . COLORBOX_DOWNLOAD_URI); } if (is_file($filename)) { - // Decompress the zip archive - drush_shell_exec('unzip -qq -o '. $filename); - // Remove the zip archive + // Decompress the zip archive. + drush_shell_exec('unzip -qq -o ' . $filename); + // Remove the zip archive. drush_op('unlink', $filename); } // Set working directory back to the previous working directory. chdir($olddir); - if (is_dir($path .'/'. $dirname)) { - drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success'); + if (is_dir($path . '/' . $dirname)) { + drush_log(dt('Colorbox plugin has been downloaded to @path', ['@path' => $path]), 'success'); } else { - drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error'); + drush_log(dt('Drush was unable to download the Colorbox plugin to @path', ['@path' => $path]), 'error'); } } diff --git a/web/modules/libraries/libraries.info.yml b/web/modules/libraries/libraries.info.yml index d0d9423667..7c18d8f375 100644 --- a/web/modules/libraries/libraries.info.yml +++ b/web/modules/libraries/libraries.info.yml @@ -1,10 +1,9 @@ name: Libraries type: module description: Allows version-dependent and shared usage of external libraries. -core: 8.x -core_version_requirement: ^8 || ^9 +core_version_requirement: ^8.8 || ^9 || ^10 -# Information added by Drupal.org packaging script on 2021-01-04 -version: '8.x-3.0-beta1' +# Information added by Drupal.org packaging script on 2022-10-21 +version: '4.0.0' project: 'libraries' -datestamp: 1609758296 +datestamp: 1666346901 diff --git a/web/modules/libraries/libraries.module b/web/modules/libraries/libraries.module index 7028d2f977..647a1eed02 100644 --- a/web/modules/libraries/libraries.module +++ b/web/modules/libraries/libraries.module @@ -108,7 +108,7 @@ function libraries_get_path($name, $base_path = FALSE) { * https://www.drupal.org/node/2170763 */ function libraries_get_libraries() { - $searchdir = array(); + $searchdir = []; $config = DrupalKernel::findSitePath(\Drupal::request()); // @todo core/libraries @@ -117,7 +117,7 @@ function libraries_get_libraries() { // profile, installation profiles may want to place libraries into a // 'libraries' directory. if ($profile = \Drupal::installProfile()) { - $profile_path = drupal_get_path('profile', $profile); + $profile_path = \Drupal::service('extension.list.profile')->getPath($profile); $searchdir[] = "$profile_path/libraries"; }; @@ -131,8 +131,8 @@ function libraries_get_libraries() { $searchdir[] = "$config/libraries"; // Retrieve list of directories. - $directories = array(); - $nomask = array('CVS'); + $directories = []; + $nomask = ['CVS']; foreach ($searchdir as $dir) { if (is_dir($dir) && $handle = opendir($dir)) { while (FALSE !== ($file = readdir($handle))) { @@ -168,24 +168,24 @@ function libraries_get_libraries() { * https://www.drupal.org/node/2170763 */ function libraries_scan_info_files() { - $profile = drupal_get_path('profile', \Drupal::installProfile()); + $profile = \Drupal::service('extension.list.profile')->getPath(\Drupal::installProfile()); $config = DrupalKernel::findSitePath(\Drupal::request()); // Build a list of directories. - $directories = \Drupal::moduleHandler()->invokeAll('libraries_info_file_paths', $args = array()); + $directories = \Drupal::moduleHandler()->invokeAll('libraries_info_file_paths', $args = []); $directories[] = "$profile/libraries"; $directories[] = 'sites/all/libraries'; $directories[] = 'libraries'; $directories[] = "$config/libraries"; // Scan for info files. - $files = array(); + $files = []; foreach ($directories as $dir) { if (file_exists($dir)) { - $files = array_merge($files, \Drupal::service('file_system')->scanDirectory($dir, '@^[a-z0-9._-]+\.libraries\.info\.yml$@', array( + $files = array_merge($files, \Drupal::service('file_system')->scanDirectory($dir, '@^[a-z0-9._-]+\.libraries\.info\.yml$@', [ 'key' => 'name', 'recurse' => FALSE, - ))); + ])); } } @@ -294,7 +294,7 @@ function libraries_traverse_library(&$library, $callback) { function libraries_prepare_files(&$library, $version = NULL, $variant = NULL) { // Both the 'files' property and the 'integration files' property contain file // declarations, and we want to make both consistent. - $file_types = array(); + $file_types = []; if (isset($library['files'])) { $file_types[] = &$library['files']; } @@ -306,12 +306,12 @@ function libraries_prepare_files(&$library, $version = NULL, $variant = NULL) { } foreach ($file_types as &$files) { // Go through all supported types of files. - foreach (array('js', 'css', 'php') as $type) { + foreach (['js', 'css', 'php'] as $type) { if (isset($files[$type])) { foreach ($files[$type] as $key => $value) { // Unset numeric keys and turn the respective values into keys. if (is_numeric($key)) { - $files[$type][$value] = array(); + $files[$type][$value] = []; unset($files[$type][$key]); } } @@ -348,27 +348,27 @@ function libraries_detect_dependencies(&$library, $version = NULL, $variant = NU foreach ($library['dependencies'] as &$dependency_string) { $dependency = Dependency::createFromString($dependency_string); $info = libraries_detect($dependency->getName()); - if (!$info['installed']) { + if (!isset($info['installed']) || !$info['installed']) { $library['installed'] = FALSE; $library['error'] = 'missing dependency'; - $library['error message'] = t('The %dependency library, which the %library library depends on, is not installed.', array( - '%dependency' => $dependency['name'], + $library['error message'] = t('The %dependency library, which the %library library depends on, is not installed.', [ + '%dependency' => $dependency->getName(), '%library' => $library['name'], - )); + ]); } elseif (!$dependency->isCompatible($info['version'])) { $library['installed'] = FALSE; $library['error'] = 'incompatible dependency'; - $library['error message'] = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', array( + $library['error message'] = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', [ '%dependency_version' => $info['version'], '%dependency' => $info['name'], '%library' => $library['name'], - )); + ]); } // Remove the version string from the dependency, so libraries_load() can // load the libraries directly. - $dependency_string = $info['name']; + $dependency_string = $info['name'] ?? NULL; } } } @@ -401,7 +401,7 @@ function &libraries_info($name = NULL) { $libraries = &drupal_static(__FUNCTION__); if (!isset($libraries)) { - $libraries = array(); + $libraries = []; // Gather information from hook_libraries_info(). $module_handler = \Drupal::moduleHandler(); foreach ($module_handler->getImplementations('libraries_info') as $module) { @@ -414,13 +414,13 @@ function &libraries_info($name = NULL) { // @see drupal_alter() global $theme, $base_theme_info; if (isset($theme)) { - $theme_keys = array(); + $theme_keys = []; foreach ($base_theme_info as $base) { $theme_keys[] = $base->name; } $theme_keys[] = $theme; foreach ($theme_keys as $theme_key) { - $function = $theme_key . '_' . 'libraries_info'; + $function = $theme_key . '_libraries_info'; if (function_exists($function)) { foreach ($function() as $machine_name => $properties) { $properties['theme'] = $theme_key; @@ -440,7 +440,7 @@ function &libraries_info($name = NULL) { // 'type' key in info files. We therefore use Symfony's YAML parser // directly. // @todo Consider creating a dedicating InfoParser for library info files - // similar to \Drupal\Core\Extension\InfoParser + // similar to \Drupal\Core\Extension\InfoParser. $parser = new Parser(); foreach (libraries_scan_info_files() as $machine_name => $file) { $properties = $parser->parse(file_get_contents($file->uri)); @@ -487,7 +487,7 @@ function &libraries_info($name = NULL) { * https://www.drupal.org/node/2170763 */ function libraries_info_defaults(&$library, $name) { - $library += array( + $library += [ 'machine name' => $name, 'name' => $name, 'vendor url' => '', @@ -495,21 +495,21 @@ function libraries_info_defaults(&$library, $name) { 'path' => '', 'library path' => NULL, 'version callback' => 'libraries_get_version', - 'version arguments' => array(), - 'files' => array(), - 'dependencies' => array(), - 'variants' => array(), - 'versions' => array(), - 'integration files' => array(), - 'callbacks' => array(), - ); - $library['callbacks'] += array( - 'info' => array(), - 'pre-detect' => array(), - 'post-detect' => array(), - 'pre-load' => array(), - 'post-load' => array(), - ); + 'version arguments' => [], + 'files' => [], + 'dependencies' => [], + 'variants' => [], + 'versions' => [], + 'integration files' => [], + 'callbacks' => [], + ]; + $library['callbacks'] += [ + 'info' => [], + 'pre-detect' => [], + 'post-detect' => [], + 'pre-load' => [], + 'post-load' => [], + ]; // Add our own callbacks before any others. array_unshift($library['callbacks']['info'], 'libraries_prepare_files'); @@ -564,9 +564,9 @@ function libraries_detect($name) { } if ($library['library path'] === FALSE || !file_exists($library['library path'])) { $library['error'] = 'not found'; - $library['error message'] = t('The %library library could not be found.', array( + $library['error message'] = t('The %library library could not be found.', [ '%library' => $library['name'], - )); + ]); return $library; } @@ -579,16 +579,16 @@ function libraries_detect($name) { // indexed array of multiple parameters. if (isset($library['version arguments'][0])) { // Add the library as the first argument. - $library['version'] = call_user_func_array($library['version callback'], array_merge(array($library), $library['version arguments'])); + $library['version'] = call_user_func_array($library['version callback'], array_merge([$library], $library['version arguments'])); } else { $library['version'] = $library['version callback']($library, $library['version arguments']); } if (empty($library['version'])) { $library['error'] = 'not detected'; - $library['error message'] = t('The version of the %library library could not be detected.', array( + $library['error message'] = t('The version of the %library library could not be detected.', [ '%library' => $library['name'], - )); + ]); return $library; } } @@ -604,10 +604,10 @@ function libraries_detect($name) { } if (!$version) { $library['error'] = 'not supported'; - $library['error message'] = t('The installed version %version of the %library library is not supported.', array( + $library['error message'] = t('The installed version %version of the %library library is not supported.', [ '%version' => $library['version'], '%library' => $library['name'], - )); + ]); return $library; } @@ -628,18 +628,19 @@ function libraries_detect($name) { // We support both a single parameter, which is an associative array, // and an indexed array of multiple parameters. if (isset($variant['variant arguments'][0])) { - // Add the library as the first argument, and the variant name as the second. - $variant['installed'] = call_user_func_array($variant['variant callback'], array_merge(array($library, $variant_name), $variant['variant arguments'])); + // Add the library as the first argument, and the variant name as the + // second. + $variant['installed'] = call_user_func_array($variant['variant callback'], array_merge([$library, $variant_name], $variant['variant arguments'])); } else { $variant['installed'] = $variant['variant callback']($library, $variant_name, $variant['variant arguments']); } if (!$variant['installed']) { $variant['error'] = 'not found'; - $variant['error message'] = t('The %variant variant of the %library library could not be found.', array( + $variant['error message'] = t('The %variant variant of the %library library could not be found.', [ '%variant' => $variant_name, '%library' => $library['name'], - )); + ]); } } } @@ -679,7 +680,7 @@ function libraries_detect($name) { * https://www.drupal.org/node/2170763 */ function libraries_load($name, $variant = NULL) { - $loaded = &drupal_static(__FUNCTION__, array()); + $loaded = &drupal_static(__FUNCTION__, []); if (!isset($loaded[$name])) { $library = \Drupal::cache('libraries')->get($name); @@ -696,7 +697,7 @@ function libraries_load($name, $variant = NULL) { // Ensure that the $variant key exists, and if it does not, set its // 'installed' property to FALSE by default. This will prevent the loading // of the library files below. - $library['variants'] += array($variant => array('installed' => FALSE)); + $library['variants'] += [$variant => ['installed' => FALSE]]; $library = array_merge($library, $library['variants'][$variant]); } // Regardless of whether a specific variant was requested or not, there can @@ -754,7 +755,7 @@ function libraries_load_files($library) { // The parameters for drupal_add_js() and drupal_add_css() require special // handling. // @see drupal_process_attached() - foreach (array('js', 'css') as $type) { + foreach (['js', 'css'] as $type) { // Given the removal of core functions like _drupal_add_js and // _drupal_add_css the logic below cannot safely be run anymore. // @see https://www.drupal.org/node/2702563 @@ -765,7 +766,7 @@ function libraries_load_files($library) { // (and only) argument. if (!is_array($options)) { $data = $options; - $options = array(); + $options = []; } // In some cases, the first parameter ($data) is an array. Arrays can't // be passed as keys in PHP, so we have to get $data from the value @@ -804,11 +805,11 @@ function libraries_load_files($library) { // Load integration files. if (!empty($library['integration files'])) { foreach ($library['integration files'] as $module => $files) { - libraries_load_files(array( + libraries_load_files([ 'files' => $files, 'path' => '', - 'library path' => drupal_get_path('module', $module), - )); + 'library path' => \Drupal::service('extension.list.module')->getPath($module), + ]); } } @@ -846,12 +847,12 @@ function libraries_load_files($library) { */ function libraries_get_version($library, $options) { // Provide defaults. - $options += array( + $options += [ 'file' => '', 'pattern' => '', 'lines' => 20, 'cols' => 200, - ); + ]; $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $options['file']; if (empty($options['file']) || !file_exists($file)) { diff --git a/web/modules/libraries/src/Plugin/libraries/Locator/UriLocator.php b/web/modules/libraries/src/Plugin/libraries/Locator/UriLocator.php index 2732505021..aa5318361a 100644 --- a/web/modules/libraries/src/Plugin/libraries/Locator/UriLocator.php +++ b/web/modules/libraries/src/Plugin/libraries/Locator/UriLocator.php @@ -2,6 +2,7 @@ namespace Drupal\libraries\Plugin\libraries\Locator; +use Drupal\Core\StreamWrapper\LocalStream; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface; @@ -73,7 +74,7 @@ public static function create(ContainerInterface $container, array $configuratio public function locate(LocalLibraryInterface $library) { /** @var \Drupal\Core\StreamWrapper\LocalStream $stream_wrapper */ $stream_wrapper = $this->streamWrapperManager->getViaUri($this->uri); - assert($stream_wrapper instanceof \Drupal\Core\StreamWrapper\LocalStream); + assert($stream_wrapper instanceof LocalStream); // Calling LocalStream::getDirectoryPath() explicitly avoids the realpath() // usage in LocalStream::getLocalPath(), which breaks if Libraries API is // symbolically linked into the Drupal installation. diff --git a/web/modules/libraries/src/Plugin/libraries/Type/AssetLibraryType.php b/web/modules/libraries/src/Plugin/libraries/Type/AssetLibraryType.php index a92f27daa0..9cd5b52519 100644 --- a/web/modules/libraries/src/Plugin/libraries/Type/AssetLibraryType.php +++ b/web/modules/libraries/src/Plugin/libraries/Type/AssetLibraryType.php @@ -2,6 +2,7 @@ namespace Drupal\libraries\Plugin\libraries\Type; +use Drupal\libraries\ExternalLibrary\Asset\AssetLibraryInterface; use Drupal\libraries\ExternalLibrary\Asset\AssetLibrary; use Drupal\libraries\ExternalLibrary\Asset\AttachableAssetLibraryRegistrationInterface; use Drupal\libraries\ExternalLibrary\LibraryInterface; @@ -24,7 +25,7 @@ public function getLibraryClass() { * {@inheritdoc} */ public function getAttachableAssetLibraries(LibraryInterface $library, LibraryManagerInterface $library_manager) { - assert($library instanceof \Drupal\libraries\ExternalLibrary\Asset\AssetLibraryInterface); + assert($library instanceof AssetLibraryInterface); /** @var \Drupal\libraries\ExternalLibrary\Asset\AssetLibraryInterface $library */ return [$library->getId() => $library->getAttachableAssetLibrary($library_manager)]; } diff --git a/web/modules/libraries/src/Plugin/libraries/Type/MultipleAssetLibraryType.php b/web/modules/libraries/src/Plugin/libraries/Type/MultipleAssetLibraryType.php index f73887e917..b1b8984515 100644 --- a/web/modules/libraries/src/Plugin/libraries/Type/MultipleAssetLibraryType.php +++ b/web/modules/libraries/src/Plugin/libraries/Type/MultipleAssetLibraryType.php @@ -25,7 +25,7 @@ public function getLibraryClass() { * {@inheritdoc} */ public function getAttachableAssetLibraries(LibraryInterface $external_library, LibraryManagerInterface $library_manager) { - assert($external_library instanceof \Drupal\libraries\ExternalLibrary\Asset\MultipleAssetLibraryInterface); + assert($external_library instanceof MultipleAssetLibraryInterface); /** @var \Drupal\libraries\ExternalLibrary\Asset\MultipleAssetLibraryInterface $external_library */ $attachable_libraries = []; foreach ($external_library->getAttachableAssetLibraries($library_manager) as $component_name => $attachable_library) { diff --git a/web/modules/libraries/src/Plugin/libraries/VersionDetector/LinePatternDetector.php b/web/modules/libraries/src/Plugin/libraries/VersionDetector/LinePatternDetector.php index 25a799c536..3d187a5bbb 100644 --- a/web/modules/libraries/src/Plugin/libraries/VersionDetector/LinePatternDetector.php +++ b/web/modules/libraries/src/Plugin/libraries/VersionDetector/LinePatternDetector.php @@ -56,7 +56,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('app.root') + $container->getParameter('app.root') ); } diff --git a/web/modules/libraries/src/Tests/LibrariesUnitTest.php b/web/modules/libraries/src/Tests/LibrariesUnitTest.php deleted file mode 100644 index f616a92b8e..0000000000 --- a/web/modules/libraries/src/Tests/LibrariesUnitTest.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php - -namespace Drupal\libraries\Tests; - -use Drupal\simpletest\KernelTestBase; - -/** - * Tests basic Libraries API functions. - * - * @group libraries - */ -class LibrariesUnitTest extends KernelTestBase { - - /** - * {@inheritdoc} - */ - public static $modules = array('libraries'); - - /** - * Tests libraries_get_path(). - */ - function testLibrariesGetPath() { - // Note that, even though libraries_get_path() doesn't find the 'example' - // library, we are able to make it 'installed' by specifying the 'library - // path' up-front. This is only used for testing purposed and is strongly - // discouraged as it defeats the purpose of Libraries API in the first - // place. - $this->assertEqual(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.'); - } - - /** - * Tests libraries_prepare_files(). - */ - function testLibrariesPrepareFiles() { - $expected = array( - 'files' => array( - 'js' => array('example.js' => array()), - 'css' => array('example.css' => array()), - 'php' => array('example.php' => array()), - ), - ); - $library = array( - 'files' => array( - 'js' => array('example.js'), - 'css' => array('example.css'), - 'php' => array('example.php'), - ), - ); - libraries_prepare_files($library, NULL, NULL); - $this->assertEqual($expected, $library, 'libraries_prepare_files() works correctly.'); - } - -} diff --git a/web/modules/libraries/src/Tests/LibrariesWebTest.php b/web/modules/libraries/src/Tests/LibrariesWebTest.php deleted file mode 100644 index f0a0da04f7..0000000000 --- a/web/modules/libraries/src/Tests/LibrariesWebTest.php +++ /dev/null @@ -1,525 +0,0 @@ -<?php - -namespace Drupal\libraries\Tests; - -use Drupal\Component\Utility\Html; -use Drupal\simpletest\WebTestBase; - -/** - * Tests basic detection and loading of libraries. - * - * @group libraries - */ -class LibrariesWebTest extends WebTestBase { - - /** - * {@inheritdoc} - */ - protected $profile = 'testing'; - - /** - * Modules to install. - * - * @var array - */ - public static $modules = array('libraries', 'libraries_test'); - - /** - * The URL generator used in this test. - * - * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface - */ - protected $urlAssembler; - - /** - * The state service used in this test. - * - * @var \Drupal\Core\State\StateInterface - */ - protected $state; - - /** - * {@inheritdoc} - */ - protected function setUp() { - parent::setUp(); - - $this->urlAssembler = $this->container->get('unrouted_url_assembler'); - $this->state = $this->container->get('state'); - } - - /** - * Tests libraries_detect_dependencies(). - */ - function testLibrariesDetectDependencies() { - $library = array( - 'name' => 'Example', - 'dependencies' => array('Example missing'), - ); - libraries_detect_dependencies($library); - $this->assertEqual($library['error'], 'missing dependency', 'libraries_detect_dependencies() detects missing dependency'); - $error_message = t('The %dependency library, which the %library library depends on, is not installed.', array( - '%dependency' => 'Example missing', - '%library' => $library['name'], - )); - $this->verbose("Expected:<br>$error_message"); - $this->verbose('Actual:<br>' . $library['error message']); - $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing dependency'); - // Test versioned dependencies. - $version = '1.1'; - $compatible = array( - '1.1', - '<=1.1', - '>=1.1', - '<1.2', - '<2.0', - '>1.0', - '>1.0-rc1', - '>1.0-beta2', - '>1.0-alpha3', - '>0.1', - '<1.2, >1.0', - '>0.1, <=1.1', - ); - $incompatible = array( - '1.2', - '2.0', - '<1.1', - '>1.1', - '<=1.0', - '<=1.0-rc1', - '<=1.0-beta2', - '<=1.0-alpha3', - '>=1.2', - '<1.1, >0.9', - '>=0.1, <1.1', - ); - $library = array( - 'name' => 'Example', - ); - foreach ($compatible as $version_string) { - $library['dependencies'][0] = "example_dependency ($version_string)"; - // libraries_detect_dependencies() is a post-detect callback, so - // 'installed' is already set, when it is called. It sets the value to - // FALSE for missing or incompatible dependencies. - $library['installed'] = TRUE; - libraries_detect_dependencies($library); - $this->assertTrue($library['installed'], "libraries_detect_dependencies() detects compatible version string: '$version_string' is compatible with '$version'"); - } - foreach ($incompatible as $version_string) { - $library['dependencies'][0] = "example_dependency ($version_string)"; - $library['installed'] = TRUE; - unset($library['error'], $library['error message']); - libraries_detect_dependencies($library); - $this->assertEqual($library['error'], 'incompatible dependency', "libraries_detect_dependencies() detects incompatible version strings: '$version_string' is incompatible with '$version'"); - } - // Instead of repeating this assertion for each version string, we just - // re-use the $library variable from the foreach loop. - $error_message = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', array( - '%dependency_version' => $version, - '%dependency' => 'Example dependency', - '%library' => $library['name'], - )); - $this->verbose("Expected:<br>$error_message"); - $this->verbose('Actual:<br>' . $library['error message']); - $this->assertEqual($library['error message'], $error_message, 'Correct error message for an incompatible dependency'); - } - - /** - * Tests libraries_scan_info_files(). - */ - function testLibrariesScanInfoFiles() { - $expected = array('example_info_file' => (object) array( - 'uri' => drupal_get_path('module', 'libraries') . '/tests/example/example_info_file.libraries.info.yml', - 'filename' => 'example_info_file.libraries.info.yml', - 'name' => 'example_info_file.libraries.info', - )); - $actual = libraries_scan_info_files(); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($actual, TRUE) . '</pre>'); - $this->assertEqual($actual, $expected, 'libraries_scan_info_files() correctly finds the example info file.'); - $this->verbose('<pre>' . var_export(libraries_scan_info_files(), TRUE) . '</pre>'); - } - - /** - * Tests libraries_info(). - */ - function testLibrariesInfo() { - // Test that library information is found correctly. - $expected = array( - 'name' => 'Example files', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', - 'version' => '1', - 'files' => array( - 'js' => array('example_1.js' => array()), - 'css' => array('example_1.css' => array()), - 'php' => array('example_1.php' => array()), - ), - 'module' => 'libraries_test', - ); - libraries_info_defaults($expected, 'example_files'); - $library = libraries_info('example_files'); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library, $expected, 'Library information is correctly gathered.'); - - // Test a library specified with an .info file gets detected. - $expected = array( - 'name' => 'Example info file', - 'info file' => drupal_get_path('module', 'libraries') . '/tests/example/example_info_file.libraries.info.yml', - ); - libraries_info_defaults($expected, 'example_info_file'); - $library = libraries_info('example_info_file'); - // If this module was downloaded from Drupal.org, the Drupal.org packaging - // system has corrupted the test info file. - // @see http://drupal.org/node/1606606 - unset($library['core'], $library['datestamp'], $library['project'], $library['version']); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library, $expected, 'Library specified with an .info file found'); - } - - /** - * Tests libraries_detect(). - */ - function testLibrariesDetect() { - // Test missing library. - $library = libraries_detect('example_missing'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['error'], 'not found', 'Missing library not found.'); - $error_message = t('The %library library could not be found.', array( - '%library' => $library['name'], - )); - $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.'); - - // Test unknown library version. - $library = libraries_detect('example_undetected_version'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.'); - $error_message = t('The version of the %library library could not be detected.', array( - '%library' => $library['name'], - )); - $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.'); - - // Test unsupported library version. - $library = libraries_detect('example_unsupported_version'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['error'], 'not supported', 'Unsupported version detected as such.'); - $error_message = t('The installed version %version of the %library library is not supported.', array( - '%version' => $library['version'], - '%library' => $library['name'], - )); - $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.'); - - // Test supported library version. - $library = libraries_detect('example_supported_version'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['installed'], TRUE, 'Supported library version found.'); - - // Test libraries_get_version(). - $library = libraries_detect('example_default_version_callback'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.'); - - // Test a multiple-parameter version callback. - $library = libraries_detect('example_multiple_parameter_version_callback'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.'); - - // Test a top-level files property. - $library = libraries_detect('example_files'); - $files = array( - 'js' => array('example_1.js' => array()), - 'css' => array('example_1.css' => array()), - 'php' => array('example_1.php' => array()), - ); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['files'], $files, 'Top-level files property works.'); - - // Test version-specific library files. - $library = libraries_detect('example_versions'); - $files = array( - 'js' => array('example_2.js' => array()), - 'css' => array('example_2.css' => array()), - 'php' => array('example_2.php' => array()), - ); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['files'], $files, 'Version-specific library files found.'); - - // Test missing variant. - $library = libraries_detect('example_variant_missing'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['variants']['example_variant']['error'], 'not found', 'Missing variant not found'); - $error_message = t('The %variant variant of the %library library could not be found.', array( - '%variant' => 'example_variant', - '%library' => 'Example variant missing', - )); - $this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.'); - - // Test existing variant. - $library = libraries_detect('example_variant'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.'); - } - - /** - * Tests libraries_load(). - * - * @todo Remove or rewrite to accomodate integration with core Libraries. - */ - function _testLibrariesLoad() { - // Test dependencies. - $library = libraries_load('example_dependency_missing'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertFalse($library['loaded'], 'Library with missing dependency cannot be loaded'); - $library = libraries_load('example_dependency_incompatible'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertFalse($library['loaded'], 'Library with incompatible dependency cannot be loaded'); - $library = libraries_load('example_dependency_compatible'); - $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library['loaded'], 1, 'Library with compatible dependency is loaded'); - $loaded = &drupal_static('libraries_load'); - $this->verbose('<pre>' . var_export($loaded, TRUE) . '</pre>'); - $this->assertEqual($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded'); - } - - /** - * Tests the applying of callbacks. - */ - function testCallbacks() { - $expected = array( - 'name' => 'Example callback', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', - 'version' => '1', - 'versions' => array( - '1' => array( - 'variants' => array( - 'example_variant' => array( - 'info callback' => 'not applied', - 'pre-detect callback' => 'not applied', - 'post-detect callback' => 'not applied', - 'pre-load callback' => 'not applied', - 'post-load callback' => 'not applied', - ), - ), - 'info callback' => 'not applied', - 'pre-detect callback' => 'not applied', - 'post-detect callback' => 'not applied', - 'pre-load callback' => 'not applied', - 'post-load callback' => 'not applied', - ), - ), - 'variants' => array( - 'example_variant' => array( - 'info callback' => 'not applied', - 'pre-detect callback' => 'not applied', - 'post-detect callback' => 'not applied', - 'pre-load callback' => 'not applied', - 'post-load callback' => 'not applied', - ), - ), - 'callbacks' => array( - 'info' => array('_libraries_test_info_callback'), - 'pre-detect' => array('_libraries_test_pre_detect_callback'), - 'post-detect' => array('_libraries_test_post_detect_callback'), - 'pre-load' => array('_libraries_test_pre_load_callback'), - 'post-load' => array('_libraries_test_post_load_callback'), - ), - 'info callback' => 'not applied', - 'pre-detect callback' => 'not applied', - 'post-detect callback' => 'not applied', - 'pre-load callback' => 'not applied', - 'post-load callback' => 'not applied', - 'module' => 'libraries_test', - ); - libraries_info_defaults($expected, 'example_callback'); - - // Test a callback in the 'info' group. - $expected['info callback'] = 'applied (top-level)'; - $expected['versions']['1']['info callback'] = 'applied (version 1)'; - $expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)'; - $expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)'; - $library = libraries_info('example_callback'); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library, $expected, 'Prepare callback was applied correctly.'); - - // Test a callback in the 'pre-detect' and 'post-detect' phases. - // Successfully detected libraries should only contain version information - // for the detected version and thus, be marked as installed. - unset($expected['versions']); - $expected['installed'] = TRUE; - // Additionally, version-specific properties of the detected version are - // supposed to override the corresponding top-level properties. - $expected['info callback'] = 'applied (version 1)'; - $expected['variants']['example_variant']['installed'] = TRUE; - $expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)'; - // Version-overloading takes place after the 'pre-detect' callbacks have - // been applied. - $expected['pre-detect callback'] = 'applied (version 1)'; - $expected['post-detect callback'] = 'applied (top-level)'; - $expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)'; - $expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)'; - $library = libraries_detect('example_callback'); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library, $expected, 'Detect callback was applied correctly.'); - - // Test a callback in the 'pre-load' and 'post-load' phases. - // Successfully loaded libraries should only contain information about the - // already loaded variant. - unset($expected['variants']); - $expected['loaded'] = 0; - $expected['pre-load callback'] = 'applied (top-level)'; - $expected['post-load callback'] = 'applied (top-level)'; - $library = libraries_load('example_callback'); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.'); - // This is not recommended usually and is only used for testing purposes. - drupal_static_reset('libraries_load'); - // Successfully loaded library variants are supposed to contain the specific - // variant information only. - $expected['info callback'] = 'applied (version 1, variant example_variant)'; - $expected['pre-detect callback'] = 'applied (version 1, variant example_variant)'; - $expected['post-detect callback'] = 'applied (variant example_variant)'; - $library = libraries_load('example_callback', 'example_variant'); - $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); - $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); - $this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.'); - } - - /** - * Tests that library files are properly added to the page output. - * - * We check for JavaScript and CSS files directly in the DOM and add a list of - * included PHP files manually to the page output. - * - * @todo Remove or rewrite to accomodate integration with core Libraries. - * @see _libraries_test_load() - */ - function _testLibrariesOutput() { - // Test loading of a simple library with a top-level files property. - $this->drupalGet('libraries_test/files'); - $this->assertLibraryFiles('example_1', 'File loading'); - - // Test loading of integration files. - $this->drupalGet('libraries_test/integration_files'); - $this->assertRaw('libraries_test.js', 'Integration file loading: libraries_test.js found'); - $this->assertRaw('libraries_test.css', 'Integration file loading: libraries_test.css found'); - $this->assertRaw('libraries_test.inc', 'Integration file loading: libraries_test.inc found'); - - // Test version overloading. - $this->drupalGet('libraries_test/versions'); - $this->assertLibraryFiles('example_2', 'Version overloading'); - - // Test variant loading. - $this->drupalGet('libraries_test/variant'); - $this->assertLibraryFiles('example_3', 'Variant loading'); - - // Test version overloading and variant loading. - $this->drupalGet('libraries_test/versions_and_variants'); - $this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading'); - - // Test caching. - \Drupal::state()->set('libraries_test.cache', TRUE); - \Drupal::cache('libraries')->delete('example_callback'); - // When the library information is not cached, all callback groups should be - // invoked. - $this->drupalGet('libraries_test/cache'); - $this->assertRaw('The <em>info</em> callback group was invoked.', 'Info callback invoked for uncached libraries.'); - $this->assertRaw('The <em>pre-detect</em> callback group was invoked.', 'Pre-detect callback invoked for uncached libraries.'); - $this->assertRaw('The <em>post-detect</em> callback group was invoked.', 'Post-detect callback invoked for uncached libraries.'); - $this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for uncached libraries.'); - $this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for uncached libraries.'); - // When the library information is cached only the 'pre-load' and - // 'post-load' callback groups should be invoked. - $this->drupalGet('libraries_test/cache'); - $this->assertNoRaw('The <em>info</em> callback group was not invoked.', 'Info callback not invoked for cached libraries.'); - $this->assertNoRaw('The <em>pre-detect</em> callback group was not invoked.', 'Pre-detect callback not invoked for cached libraries.'); - $this->assertNoRaw('The <em>post-detect</em> callback group was not invoked.', 'Post-detect callback not invoked for cached libraries.'); - $this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for cached libraries.'); - $this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for cached libraries.'); - \Drupal::state()->set('libraries_test.cache', FALSE); - } - - /** - * Helper function to assert that a library was correctly loaded. - * - * Asserts that all the correct files were loaded and all the incorrect ones - * were not. - * - * @param $name - * The name of the files that should be loaded. The current testing system - * knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name - * has an associated JavaScript, CSS and PHP file that will be asserted. All - * other files will be asserted to not be loaded. See - * tests/example/README.txt for more information on how the loading of the - * files is tested. - * @param $label - * (optional) A label to prepend to the assertion messages, to make them - * less ambiguous. - * @param $extensions - * (optional) The expected file extensions of $name. Defaults to - * array('js', 'css', 'php'). - */ - function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) { - $label = ($label !== '' ? "$label: " : ''); - - // Test that the wrong files are not loaded... - $names = array( - 'example_1' => FALSE, - 'example_2' => FALSE, - 'example_3' => FALSE, - 'example_4' => FALSE, - ); - // ...and the correct ones are. - $names[$name] = TRUE; - - // Test for the specific HTML that the different file types appear as in the - // DOM. - $html = array( - 'js' => array('<script src="', '"></script>'), - 'css' => array('<link rel="stylesheet" href="', '" media="all" />'), - // PHP files do not get added to the DOM directly. - // @see _libraries_test_load() - 'php' => array('<li>', '</li>'), - ); - - $html_expected = array(); - $html_not_expected = array(); - - foreach ($names as $name => $expected) { - foreach ($extensions as $extension) { - $filepath = drupal_get_path('module', 'libraries') . "/tests/example/$name.$extension"; - // JavaScript and CSS files appear as full URLs and with an appended - // query string. - if (in_array($extension, array('js', 'css'))) { - $filepath = $this->urlAssembler->assemble("base://$filepath", [ - 'query' => [ - $this->state->get('system.css_js_query_string') ?: '0' => NULL, - ], - 'absolute' => TRUE, - ]); - // If index.php is part of the generated URLs, we need to strip it. - //$filepath = str_replace('index.php/', '', $filepath); - } - list($prefix, $suffix) = $html[$extension]; - $raw = $prefix . $filepath . $suffix; - if ($expected) { - $html_expected[] = Html::escape($raw); - $this->assertRaw($raw, "$label$name.$extension found."); - } - else { - $html_not_expected[] = Html::escape($raw); - $this->assertNoRaw($raw, "$label$name.$extension not found."); - } - } - } - - $html_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_expected) . '</pre></li></ul>'; - $html_not_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_not_expected) . '</pre></li></ul>'; - $this->verbose("Strings of HTML that are expected to be present:{$html_expected}Strings of HTML that are expected to not be present:{$html_not_expected}"); - } - -} diff --git a/web/modules/libraries/tests/example/example_info_file.libraries.info.yml b/web/modules/libraries/tests/example/example_info_file.libraries.info.yml index 90d4246681..ddab70ebb2 100644 --- a/web/modules/libraries/tests/example/example_info_file.libraries.info.yml +++ b/web/modules/libraries/tests/example/example_info_file.libraries.info.yml @@ -1,7 +1,7 @@ # This is an example info file of a library used for testing purposes. name: Example info file -# Information added by Drupal.org packaging script on 2021-01-04 -version: '8.x-3.0-beta1' +# Information added by Drupal.org packaging script on 2022-10-21 +version: '4.0.0' project: 'libraries' -datestamp: 1609758296 +datestamp: 1666346901 diff --git a/web/modules/libraries/tests/modules/libraries_test/libraries_test.info.yml b/web/modules/libraries/tests/modules/libraries_test/libraries_test.info.yml index 6a9118db10..008462107f 100644 --- a/web/modules/libraries/tests/modules/libraries_test/libraries_test.info.yml +++ b/web/modules/libraries/tests/modules/libraries_test/libraries_test.info.yml @@ -1,7 +1,7 @@ name: Libraries test module type: module description: Tests library detection and loading. -core: 8.x +core_version_requirement: ^8 || ^9 || ^10 dependencies: - libraries:libraries hidden: TRUE @@ -9,7 +9,7 @@ library_dependencies: - test_asset_library - test_asset_multiple_library -# Information added by Drupal.org packaging script on 2021-01-04 -version: '8.x-3.0-beta1' +# Information added by Drupal.org packaging script on 2022-10-21 +version: '4.0.0' project: 'libraries' -datestamp: 1609758296 +datestamp: 1666346901 diff --git a/web/modules/libraries/tests/modules/libraries_test/libraries_test.module b/web/modules/libraries/tests/modules/libraries_test/libraries_test.module index af1a4ac39a..89b0ecab6c 100644 --- a/web/modules/libraries/tests/modules/libraries_test/libraries_test.module +++ b/web/modules/libraries/tests/modules/libraries_test/libraries_test.module @@ -13,277 +13,277 @@ */ function libraries_test_libraries_info() { // Test library detection. - $libraries['example_missing'] = array( + $libraries['example_missing'] = [ 'name' => 'Example missing', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/missing', - ); - $libraries['example_undetected_version'] = array( + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/missing', + ]; + $libraries['example_undetected_version'] = [ 'name' => 'Example undetected version', - 'library path' => drupal_get_path('module', 'libraries') . '/tests', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests', 'version callback' => '_libraries_test_return_version', - 'version arguments' => array(FALSE), - ); - $libraries['example_unsupported_version'] = array( + 'version arguments' => [FALSE], + ]; + $libraries['example_unsupported_version'] = [ 'name' => 'Example unsupported version', - 'library path' => drupal_get_path('module', 'libraries') . '/tests', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests', 'version callback' => '_libraries_test_return_version', - 'version arguments' => array('1'), - 'versions' => array( - '2' => array(), - ), - ); + 'version arguments' => ['1'], + 'versions' => [ + '2' => [], + ], + ]; - $libraries['example_supported_version'] = array( + $libraries['example_supported_version'] = [ 'name' => 'Example supported version', - 'library path' => drupal_get_path('module', 'libraries') . '/tests', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests', 'version callback' => '_libraries_test_return_version', - 'version arguments' => array('1'), - 'versions' => array( - '1' => array(), - ), - ); + 'version arguments' => ['1'], + 'versions' => [ + '1' => [], + ], + ]; // Test the default version callback. - $libraries['example_default_version_callback'] = array( + $libraries['example_default_version_callback'] = [ 'name' => 'Example default version callback', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', - 'version arguments' => array( + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', + 'version arguments' => [ 'file' => 'README.txt', // Version 1 'pattern' => '/Version (\d+)/', 'lines' => 5, - ), - ); + ], + ]; // Test a multiple-parameter version callback. - $libraries['example_multiple_parameter_version_callback'] = array( + $libraries['example_multiple_parameter_version_callback'] = [ 'name' => 'Example multiple parameter version callback', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', // Version 1 'version callback' => '_libraries_test_get_version', - 'version arguments' => array('README.txt', '/Version (\d+)/', 5), - ); + 'version arguments' => ['README.txt', '/Version (\d+)/', 5], + ]; // Test a top-level files property. - $libraries['example_files'] = array( + $libraries['example_files'] = [ 'name' => 'Example files', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'files' => array( - 'js' => array('example_1.js'), - 'css' => array('example_1.css'), - 'php' => array('example_1.php'), - ), - ); + 'files' => [ + 'js' => ['example_1.js'], + 'css' => ['example_1.css'], + 'php' => ['example_1.php'], + ], + ]; // Test loading of integration files. // Normally added by the corresponding module via hook_libraries_info_alter(), // these files should be automatically loaded when the library is loaded. - $libraries['example_integration_files'] = array( + $libraries['example_integration_files'] = [ 'name' => 'Example integration files', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'integration files' => array( - 'libraries_test' => array( - 'js' => array('libraries_test.js'), - 'css' => array('libraries_test.css'), - 'php' => array('libraries_test.inc'), - ), - ), - ); + 'integration files' => [ + 'libraries_test' => [ + 'js' => ['libraries_test.js'], + 'css' => ['libraries_test.css'], + 'php' => ['libraries_test.inc'], + ], + ], + ]; // Test version overloading. - $libraries['example_versions'] = array( + $libraries['example_versions'] = [ 'name' => 'Example versions', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '2', - 'versions' => array( - '1' => array( - 'files' => array( - 'js' => array('example_1.js'), - 'css' => array('example_1.css'), - 'php' => array('example_1.php'), - ), - ), - '2' => array( - 'files' => array( - 'js' => array('example_2.js'), - 'css' => array('example_2.css'), - 'php' => array('example_2.php'), - ), - ), - ), - ); + 'versions' => [ + '1' => [ + 'files' => [ + 'js' => ['example_1.js'], + 'css' => ['example_1.css'], + 'php' => ['example_1.php'], + ], + ], + '2' => [ + 'files' => [ + 'js' => ['example_2.js'], + 'css' => ['example_2.css'], + 'php' => ['example_2.php'], + ], + ], + ], + ]; // Test variant detection. - $libraries['example_variant_missing'] = array( + $libraries['example_variant_missing'] = [ 'name' => 'Example variant missing', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'variants' => array( - 'example_variant' => array( - 'files' => array( - 'js' => array('example_3.js'), - 'css' => array('example_3.css'), - 'php' => array('example_3.php'), - ), + 'variants' => [ + 'example_variant' => [ + 'files' => [ + 'js' => ['example_3.js'], + 'css' => ['example_3.css'], + 'php' => ['example_3.php'], + ], 'variant callback' => '_libraries_test_return_installed', - 'variant arguments' => array(FALSE), - ), - ), - ); + 'variant arguments' => [FALSE], + ], + ], + ]; - $libraries['example_variant'] = array( + $libraries['example_variant'] = [ 'name' => 'Example variant', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'variants' => array( - 'example_variant' => array( - 'files' => array( - 'js' => array('example_3.js'), - 'css' => array('example_3.css'), - 'php' => array('example_3.php'), - ), + 'variants' => [ + 'example_variant' => [ + 'files' => [ + 'js' => ['example_3.js'], + 'css' => ['example_3.css'], + 'php' => ['example_3.php'], + ], 'variant callback' => '_libraries_test_return_installed', - 'variant arguments' => array(TRUE), - ), - ), - ); + 'variant arguments' => [TRUE], + ], + ], + ]; // Test correct behaviour with multiple versions and multiple variants. - $libraries['example_versions_and_variants'] = array( + $libraries['example_versions_and_variants'] = [ 'name' => 'Example versions and variants', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '2', - 'versions' => array( - '1' => array( - 'variants' => array( - 'example_variant_1' => array( - 'files' => array( - 'js' => array('example_1.js'), - 'css' => array('example_1.css'), - 'php' => array('example_1.php'), - ), + 'versions' => [ + '1' => [ + 'variants' => [ + 'example_variant_1' => [ + 'files' => [ + 'js' => ['example_1.js'], + 'css' => ['example_1.css'], + 'php' => ['example_1.php'], + ], 'variant callback' => '_libraries_test_return_installed', - 'variant arguments' => array(TRUE), - ), - 'example_variant_2' => array( - 'files' => array( - 'js' => array('example_2.js'), - 'css' => array('example_2.css'), - 'php' => array('example_2.php'), - ), + 'variant arguments' => [TRUE], + ], + 'example_variant_2' => [ + 'files' => [ + 'js' => ['example_2.js'], + 'css' => ['example_2.css'], + 'php' => ['example_2.php'], + ], 'variant callback' => '_libraries_test_return_installed', - 'variant arguments' => array(TRUE), - ), - ), - ), - '2' => array( - 'variants' => array( - 'example_variant_1' => array( - 'files' => array( - 'js' => array('example_3.js'), - 'css' => array('example_3.css'), - 'php' => array('example_3.php'), - ), + 'variant arguments' => [TRUE], + ], + ], + ], + '2' => [ + 'variants' => [ + 'example_variant_1' => [ + 'files' => [ + 'js' => ['example_3.js'], + 'css' => ['example_3.css'], + 'php' => ['example_3.php'], + ], 'variant callback' => '_libraries_test_return_installed', - 'variant arguments' => array(TRUE), - ), - 'example_variant_2' => array( - 'files' => array( - 'js' => array('example_4.js'), - 'css' => array('example_4.css'), - 'php' => array('example_4.php'), - ), + 'variant arguments' => [TRUE], + ], + 'example_variant_2' => [ + 'files' => [ + 'js' => ['example_4.js'], + 'css' => ['example_4.css'], + 'php' => ['example_4.php'], + ], 'variant callback' => '_libraries_test_return_installed', - 'variant arguments' => array(TRUE), - ), - ), - ), - ), - ); + 'variant arguments' => [TRUE], + ], + ], + ], + ], + ]; // Test dependency loading. // We add one file to each library to be able to verify if it was loaded with // libraries_load(). // This library acts as a dependency for the libraries below. - $libraries['example_dependency'] = array( + $libraries['example_dependency'] = [ 'name' => 'Example dependency', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1.1', - 'files' => array('js' => array('example_1.js')), - ); - $libraries['example_dependency_missing'] = array( + 'files' => ['js' => ['example_1.js']], + ]; + $libraries['example_dependency_missing'] = [ 'name' => 'Example dependency missing', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'dependencies' => array('example_missing'), - 'files' => array('js' => array('example_1.js')), - ); - $libraries['example_dependency_incompatible'] = array( + 'dependencies' => ['example_missing'], + 'files' => ['js' => ['example_1.js']], + ]; + $libraries['example_dependency_incompatible'] = [ 'name' => 'Example dependency incompatible', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'dependencies' => array('example_dependency (>1.1)'), - 'files' => array('js' => array('example_1.js')), - ); - $libraries['example_dependency_compatible'] = array( + 'dependencies' => ['example_dependency (>1.1)'], + 'files' => ['js' => ['example_1.js']], + ]; + $libraries['example_dependency_compatible'] = [ 'name' => 'Example dependency compatible', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'dependencies' => array('example_dependency (>=1.1)'), - 'files' => array('js' => array('example_1.js')), - ); + 'dependencies' => ['example_dependency (>=1.1)'], + 'files' => ['js' => ['example_1.js']], + ]; // Test the applying of callbacks. - $libraries['example_callback'] = array( + $libraries['example_callback'] = [ 'name' => 'Example callback', - 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'library path' => \Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example', 'version' => '1', - 'versions' => array( - '1' => array( - 'variants' => array( - 'example_variant' => array( + 'versions' => [ + '1' => [ + 'variants' => [ + 'example_variant' => [ // These keys are for testing purposes only. 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', 'pre-load callback' => 'not applied', 'post-load callback' => 'not applied', - ), - ), + ], + ], // These keys are for testing purposes only. 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', 'pre-load callback' => 'not applied', 'post-load callback' => 'not applied', - ), - ), - 'variants' => array( - 'example_variant' => array( + ], + ], + 'variants' => [ + 'example_variant' => [ // These keys are for testing purposes only. 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', 'pre-load callback' => 'not applied', 'post-load callback' => 'not applied', - ), - ), - 'callbacks' => array( - 'info' => array('_libraries_test_info_callback'), - 'pre-detect' => array('_libraries_test_pre_detect_callback'), - 'post-detect' => array('_libraries_test_post_detect_callback'), - 'pre-load' => array('_libraries_test_pre_load_callback'), - 'post-load' => array('_libraries_test_post_load_callback'), - ), + ], + ], + 'callbacks' => [ + 'info' => ['_libraries_test_info_callback'], + 'pre-detect' => ['_libraries_test_pre_detect_callback'], + 'post-detect' => ['_libraries_test_post_detect_callback'], + 'pre-load' => ['_libraries_test_pre_load_callback'], + 'post-load' => ['_libraries_test_post_load_callback'], + ], // These keys are for testing purposes only. 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', 'pre-load callback' => 'not applied', 'post-load callback' => 'not applied', - ); + ]; return $libraries; } @@ -292,7 +292,7 @@ function libraries_test_libraries_info() { * Implements hook_libraries_info_file_paths() */ function libraries_test_libraries_info_file_paths() { - return array(drupal_get_path('module', 'libraries') . '/tests/example'); + return [\Drupal::service('extension.list.module')->getPath('libraries') . '/tests/example']; } /** @@ -470,29 +470,29 @@ function _libraries_test_callback(&$library, $version, $variant, $group) { * Implements hook_menu(). */ function libraries_test_menu() { - $items['libraries_test/files'] = array( + $items['libraries_test/files'] = [ 'title' => 'Test files', 'route_name' => 'libraries_test_files', - ); - $items['libraries_test/integration_files'] = array( + ]; + $items['libraries_test/integration_files'] = [ 'title' => 'Test integration files', 'route_name' => 'libraries_test_integration_files', - ); - $items['libraries_test/versions'] = array( + ]; + $items['libraries_test/versions'] = [ 'title' => 'Test version loading', 'route_name' => 'libraries_test_versions', - ); - $items['libraries_test/variant'] = array( + ]; + $items['libraries_test/variant'] = [ 'title' => 'Test variant loading', 'route_name' => 'libraries_test_variant', - ); - $items['libraries_test/versions_and_variants'] = array( + ]; + $items['libraries_test/versions_and_variants'] = [ 'title' => 'Test concurrent version and variant loading', 'route_name' => 'libraries_test_versions_and_variants', - ); - $items['libraries_test/cache'] = array( + ]; + $items['libraries_test/cache'] = [ 'title' => 'Test caching of library information', 'route_name' => 'libraries_test_cache', - ); + ]; return $items; } diff --git a/web/modules/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php b/web/modules/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php index 0b049bfdab..71ba6f1da2 100644 --- a/web/modules/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php +++ b/web/modules/libraries/tests/src/Functional/ExternalLibrary/Definition/DefinitionDiscoveryFactoryTest.php @@ -29,15 +29,22 @@ class DefinitionDiscoveryFactoryTest extends BrowserTestBase { */ protected $definitionPath; + /** + * Set default theme to stark. + * + * @var string + */ + protected $defaultTheme = 'stark'; + /** * {@inheritdoc} */ - public static $modules = ['libraries']; + protected static $modules = ['libraries']; /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ @@ -108,9 +115,9 @@ public function testDiscovery() { /** @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface $discovery */ $discovery = $this->container->get($discovery_service_id); $definition_file = "$definitions_directory/$library_id.json"; - $this->assertFileNotExists($definition_file); + $this->assertFileDoesNotExist($definition_file); $this->assertTrue($discovery->hasDefinition($library_id)); - $this->assertFileNotExists($definition_file); + $this->assertFileDoesNotExist($definition_file); $this->assertEquals($discovery->getDefinition($library_id), $expected_definition); $this->assertFileExists($definition_file); } diff --git a/web/modules/libraries/tests/src/Kernel/ExternalLibrary/Asset/AssetLibraryTestBase.php b/web/modules/libraries/tests/src/Kernel/ExternalLibrary/Asset/AssetLibraryTestBase.php index 4f835b7255..1056098b14 100644 --- a/web/modules/libraries/tests/src/Kernel/ExternalLibrary/Asset/AssetLibraryTestBase.php +++ b/web/modules/libraries/tests/src/Kernel/ExternalLibrary/Asset/AssetLibraryTestBase.php @@ -16,7 +16,7 @@ abstract class AssetLibraryTestBase extends LibraryTypeKernelTestBase { * * @see \Drupal\libraries\ExternalLibrary\LibraryManager::getRequiredLibraryIds() */ - public static $modules = ['system']; + protected static $modules = ['system']; /** * The Drupal core library discovery. @@ -28,7 +28,7 @@ abstract class AssetLibraryTestBase extends LibraryTypeKernelTestBase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->coreLibraryDiscovery = $this->container->get('library.discovery'); } diff --git a/web/modules/libraries/tests/src/Kernel/ExternalLibrary/GlobalLocatorTest.php b/web/modules/libraries/tests/src/Kernel/ExternalLibrary/GlobalLocatorTest.php index 84eb86eb1e..39a361da80 100644 --- a/web/modules/libraries/tests/src/Kernel/ExternalLibrary/GlobalLocatorTest.php +++ b/web/modules/libraries/tests/src/Kernel/ExternalLibrary/GlobalLocatorTest.php @@ -15,7 +15,7 @@ class GlobalLocatorTest extends LibraryTypeKernelTestBase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); // Assign our test stream (which points to the test php lib) to the asset // scheme. This gives us a scheme to work with in the test that is not diff --git a/web/modules/libraries/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php b/web/modules/libraries/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php index 10c1d2cfee..75098e7dee 100644 --- a/web/modules/libraries/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php +++ b/web/modules/libraries/tests/src/Kernel/ExternalLibrary/PhpFile/PhpFileLibraryTest.php @@ -15,7 +15,7 @@ class PhpFileLibraryTest extends LibraryTypeKernelTestBase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->container->set('stream_wrapper.php_file_libraries', new TestLibraryFilesStream( diff --git a/web/modules/libraries/tests/src/Kernel/LibrariesApi/LibrariesUnitTest.php b/web/modules/libraries/tests/src/Kernel/LibrariesApi/LibrariesUnitTest.php new file mode 100644 index 0000000000..247c1168bd --- /dev/null +++ b/web/modules/libraries/tests/src/Kernel/LibrariesApi/LibrariesUnitTest.php @@ -0,0 +1,53 @@ +<?php + +namespace Drupal\Tests\libraries\Kernel\LibrariesApi; + +use Drupal\KernelTests\KernelTestBase; + +/** + * Tests basic Libraries API functions. + * + * @group libraries + */ +class LibrariesUnitTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['libraries']; + + /** + * Tests libraries_get_path(). + */ + function testLibrariesGetPath() { + // Note that, even though libraries_get_path() doesn't find the 'example' + // library, we are able to make it 'installed' by specifying the 'library + // path' up-front. This is only used for testing purposed and is strongly + // discouraged as it defeats the purpose of Libraries API in the first + // place. + $this->assertEquals(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.'); + } + + /** + * Tests libraries_prepare_files(). + */ + function testLibrariesPrepareFiles() { + $expected = [ + 'files' => [ + 'js' => ['example.js' => []], + 'css' => ['example.css' => []], + 'php' => ['example.php' => []], + ], + ]; + $library = [ + 'files' => [ + 'js' => ['example.js'], + 'css' => ['example.css'], + 'php' => ['example.php'], + ], + ]; + libraries_prepare_files($library, NULL, NULL); + $this->assertEquals($expected, $library, 'libraries_prepare_files() works correctly.'); + } + +} diff --git a/web/modules/libraries/tests/src/Kernel/LibraryTypeKernelTestBase.php b/web/modules/libraries/tests/src/Kernel/LibraryTypeKernelTestBase.php index 8185794763..b2bdf70d1b 100644 --- a/web/modules/libraries/tests/src/Kernel/LibraryTypeKernelTestBase.php +++ b/web/modules/libraries/tests/src/Kernel/LibraryTypeKernelTestBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\libraries\Kernel; +use Prophecy\PhpUnit\ProphecyTrait; use Drupal\Component\Plugin\Exception\PluginException; use Drupal\KernelTests\KernelTestBase; use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException; @@ -14,6 +15,7 @@ */ abstract class LibraryTypeKernelTestBase extends KernelTestBase { + use ProphecyTrait; /** * The external library manager. * @@ -38,7 +40,7 @@ abstract class LibraryTypeKernelTestBase extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['libraries', 'libraries_test']; + protected static $modules = ['libraries', 'libraries_test']; /** * Gets the ID of the library type that is being tested. @@ -50,7 +52,7 @@ abstract protected function getLibraryTypeId(); /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */ diff --git a/web/modules/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php b/web/modules/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php index 5e4e34b931..ab9ed61aa5 100644 --- a/web/modules/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php +++ b/web/modules/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php @@ -2,9 +2,11 @@ namespace Drupal\Tests\libraries\Unit\Plugin\libraries\VersionDetector; +use Prophecy\PhpUnit\ProphecyTrait; use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface; use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface; use Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector; +use Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException; use Drupal\Tests\UnitTestCase; use org\bovigo\vfs\vfsStream; @@ -17,16 +19,16 @@ */ class LinePatternDetectorTest extends UnitTestCase { + use ProphecyTrait; protected $libraryId = 'test_library'; /** * Tests that version detection fails for a non-local library. * - * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException - * * @covers ::detectVersion */ public function testDetectVersionNonLocal() { + $this->expectException(UnknownLibraryVersionException::class); $library = $this->prophesize(VersionedLibraryInterface::class); $detector = $this->setupDetector(); $detector->detectVersion($library->reveal()); @@ -35,11 +37,10 @@ public function testDetectVersionNonLocal() { /** * Tests that version detection fails for a missing file. * - * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException - * * @covers ::detectVersion */ public function testDetectVersionMissingFile() { + $this->expectException(UnknownLibraryVersionException::class); $library = $this->setupLibrary(); $detector = $this->setupDetector(['file' => 'CHANGELOG.txt']); -- GitLab