5 * Tests the library detection and loading.
8 use Drupal\Component\Utility\SafeMarkup;
11 * Implements hook_libraries_info().
13 function libraries_test_libraries_info() {
14 // Test library detection.
15 $libraries['example_missing'] = array(
16 'name' => 'Example missing',
17 'library path' => drupal_get_path('module', 'libraries') . '/tests/missing',
19 $libraries['example_undetected_version'] = array(
20 'name' => 'Example undetected version',
21 'library path' => drupal_get_path('module', 'libraries') . '/tests',
22 'version callback' => '_libraries_test_return_version',
23 'version arguments' => array(FALSE),
25 $libraries['example_unsupported_version'] = array(
26 'name' => 'Example unsupported version',
27 'library path' => drupal_get_path('module', 'libraries') . '/tests',
28 'version callback' => '_libraries_test_return_version',
29 'version arguments' => array('1'),
35 $libraries['example_supported_version'] = array(
36 'name' => 'Example supported version',
37 'library path' => drupal_get_path('module', 'libraries') . '/tests',
38 'version callback' => '_libraries_test_return_version',
39 'version arguments' => array('1'),
45 // Test the default version callback.
46 $libraries['example_default_version_callback'] = array(
47 'name' => 'Example default version callback',
48 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
49 'version arguments' => array(
50 'file' => 'README.txt',
52 'pattern' => '/Version (\d+)/',
57 // Test a multiple-parameter version callback.
58 $libraries['example_multiple_parameter_version_callback'] = array(
59 'name' => 'Example multiple parameter version callback',
60 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
62 'version callback' => '_libraries_test_get_version',
63 'version arguments' => array('README.txt', '/Version (\d+)/', 5),
66 // Test a top-level files property.
67 $libraries['example_files'] = array(
68 'name' => 'Example files',
69 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
72 'js' => array('example_1.js'),
73 'css' => array('example_1.css'),
74 'php' => array('example_1.php'),
78 // Test loading of integration files.
79 // Normally added by the corresponding module via hook_libraries_info_alter(),
80 // these files should be automatically loaded when the library is loaded.
81 $libraries['example_integration_files'] = array(
82 'name' => 'Example integration files',
83 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
85 'integration files' => array(
86 'libraries_test' => array(
87 'js' => array('libraries_test.js'),
88 'css' => array('libraries_test.css'),
89 'php' => array('libraries_test.inc'),
94 // Test version overloading.
95 $libraries['example_versions'] = array(
96 'name' => 'Example versions',
97 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
102 'js' => array('example_1.js'),
103 'css' => array('example_1.css'),
104 'php' => array('example_1.php'),
109 'js' => array('example_2.js'),
110 'css' => array('example_2.css'),
111 'php' => array('example_2.php'),
117 // Test variant detection.
118 $libraries['example_variant_missing'] = array(
119 'name' => 'Example variant missing',
120 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
123 'example_variant' => array(
125 'js' => array('example_3.js'),
126 'css' => array('example_3.css'),
127 'php' => array('example_3.php'),
129 'variant callback' => '_libraries_test_return_installed',
130 'variant arguments' => array(FALSE),
135 $libraries['example_variant'] = array(
136 'name' => 'Example variant',
137 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
140 'example_variant' => array(
142 'js' => array('example_3.js'),
143 'css' => array('example_3.css'),
144 'php' => array('example_3.php'),
146 'variant callback' => '_libraries_test_return_installed',
147 'variant arguments' => array(TRUE),
152 // Test correct behaviour with multiple versions and multiple variants.
153 $libraries['example_versions_and_variants'] = array(
154 'name' => 'Example versions and variants',
155 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
160 'example_variant_1' => array(
162 'js' => array('example_1.js'),
163 'css' => array('example_1.css'),
164 'php' => array('example_1.php'),
166 'variant callback' => '_libraries_test_return_installed',
167 'variant arguments' => array(TRUE),
169 'example_variant_2' => array(
171 'js' => array('example_2.js'),
172 'css' => array('example_2.css'),
173 'php' => array('example_2.php'),
175 'variant callback' => '_libraries_test_return_installed',
176 'variant arguments' => array(TRUE),
182 'example_variant_1' => array(
184 'js' => array('example_3.js'),
185 'css' => array('example_3.css'),
186 'php' => array('example_3.php'),
188 'variant callback' => '_libraries_test_return_installed',
189 'variant arguments' => array(TRUE),
191 'example_variant_2' => array(
193 'js' => array('example_4.js'),
194 'css' => array('example_4.css'),
195 'php' => array('example_4.php'),
197 'variant callback' => '_libraries_test_return_installed',
198 'variant arguments' => array(TRUE),
205 // Test dependency loading.
206 // We add one file to each library to be able to verify if it was loaded with
208 // This library acts as a dependency for the libraries below.
209 $libraries['example_dependency'] = array(
210 'name' => 'Example dependency',
211 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
213 'files' => array('js' => array('example_1.js')),
215 $libraries['example_dependency_missing'] = array(
216 'name' => 'Example dependency missing',
217 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
219 'dependencies' => array('example_missing'),
220 'files' => array('js' => array('example_1.js')),
222 $libraries['example_dependency_incompatible'] = array(
223 'name' => 'Example dependency incompatible',
224 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
226 'dependencies' => array('example_dependency (>1.1)'),
227 'files' => array('js' => array('example_1.js')),
229 $libraries['example_dependency_compatible'] = array(
230 'name' => 'Example dependency compatible',
231 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
233 'dependencies' => array('example_dependency (>=1.1)'),
234 'files' => array('js' => array('example_1.js')),
237 // Test the applying of callbacks.
238 $libraries['example_callback'] = array(
239 'name' => 'Example callback',
240 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
245 'example_variant' => array(
246 // These keys are for testing purposes only.
247 'info callback' => 'not applied',
248 'pre-detect callback' => 'not applied',
249 'post-detect callback' => 'not applied',
250 'pre-load callback' => 'not applied',
251 'post-load callback' => 'not applied',
254 // These keys are for testing purposes only.
255 'info callback' => 'not applied',
256 'pre-detect callback' => 'not applied',
257 'post-detect callback' => 'not applied',
258 'pre-load callback' => 'not applied',
259 'post-load callback' => 'not applied',
263 'example_variant' => array(
264 // These keys are for testing purposes only.
265 'info callback' => 'not applied',
266 'pre-detect callback' => 'not applied',
267 'post-detect callback' => 'not applied',
268 'pre-load callback' => 'not applied',
269 'post-load callback' => 'not applied',
272 'callbacks' => array(
273 'info' => array('_libraries_test_info_callback'),
274 'pre-detect' => array('_libraries_test_pre_detect_callback'),
275 'post-detect' => array('_libraries_test_post_detect_callback'),
276 'pre-load' => array('_libraries_test_pre_load_callback'),
277 'post-load' => array('_libraries_test_post_load_callback'),
279 // These keys are for testing purposes only.
280 'info callback' => 'not applied',
281 'pre-detect callback' => 'not applied',
282 'post-detect callback' => 'not applied',
283 'pre-load callback' => 'not applied',
284 'post-load callback' => 'not applied',
291 * Implements hook_libraries_info_file_paths()
293 function libraries_test_libraries_info_file_paths() {
294 return array(drupal_get_path('module', 'libraries') . '/tests/example');
298 * Gets the version of an example library.
300 * Returns exactly the version string entered as the $version parameter. This
301 * function cannot be collapsed with _libraries_test_return_installed(), because
302 * of the different arguments that are passed automatically.
304 function _libraries_test_return_version($library, $version) {
309 * Gets the version information from an arbitrary library.
311 * Test function for a version callback with multiple arguments. This is an
312 * exact copy of libraries_get_version(), which uses a single $option argument,
313 * except for the fact that it uses multiple arguments. Since we support both
314 * type of version callbacks, detecting the version of a test library with this
315 * function ensures that the arguments are passed correctly. This function might
316 * be a useful reference for a custom version callback that uses multiple
320 * An associative array containing all information about the library.
322 * The filename to parse for the version, relative to the library path. For
323 * example: 'docs/changelog.txt'.
325 * A string containing a regular expression (PCRE) to match the library
326 * version. For example: '/@version (\d+)\.(\d+)/'.
328 * (optional) The maximum number of lines to search the pattern in. Defaults
331 * (optional) The maximum number of characters per line to take into account.
332 * Defaults to 200. In case of minified or compressed files, this prevents
333 * reading the entire file into memory.
336 * A string containing the version of the library.
338 * @see libraries_get_version()
340 function _libraries_test_get_version($library, $file, $pattern, $lines = 20, $cols = 200) {
342 $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $file;
343 if (!file_exists($file)) {
346 $file = fopen($file, 'r');
347 while ($lines && $line = fgets($file, $cols)) {
348 if (preg_match($pattern, $line, $version)) {
358 * Detects the variant of an example library.
360 * Returns exactly the value of $installed, either TRUE or FALSE. This function
361 * cannot be collapsed with _libraries_test_return_version(), because of the
362 * different arguments that are passed automatically.
364 function _libraries_test_return_installed($library, $name, $installed) {
369 * Sets the 'info callback' key.
371 * This function is used as a test callback for the 'info' callback group.
373 * @see _libraries_test_callback()
375 function _libraries_test_info_callback(&$library, $version, $variant) {
376 _libraries_test_callback($library, $version, $variant, 'info');
380 * Sets the 'pre-detect callback' key.
382 * This function is used as a test callback for the 'pre-detect' callback group.
384 * @see _libraries_test_callback()
386 function _libraries_test_pre_detect_callback(&$library, $version, $variant) {
387 _libraries_test_callback($library, $version, $variant, 'pre-detect');
391 * Sets the 'post-detect callback' key.
393 * This function is used as a test callback for the 'post-detect callback group.
395 * @see _libraries_test_callback()
397 function _libraries_test_post_detect_callback(&$library, $version, $variant) {
398 _libraries_test_callback($library, $version, $variant, 'post-detect');
402 * Sets the 'pre-load callback' key.
404 * This function is used as a test callback for the 'pre-load' callback group.
406 * @see _libraries_test_callback()
408 function _libraries_test_pre_load_callback(&$library, $version, $variant) {
409 _libraries_test_callback($library, $version, $variant, 'pre-load');
413 * Sets the 'post-load callback' key.
415 * This function is used as a test callback for the 'post-load' callback group.
417 * @see _libraries_test_callback()
419 function _libraries_test_post_load_callback(&$library, $version, $variant) {
420 _libraries_test_callback($library, $version, $variant, 'post-load');
424 * Sets the '[group] callback' key, where [group] is prepare, detect, or load.
426 * This function is used as a test callback for the all callback groups.
428 * It sets the '[group] callback' (see above) key to 'applied ([part])' where
429 * [part] is either 'top-level', 'version x.y' (where x.y is the passed-in
430 * version string), 'variant example' (where example is the passed-in variant
431 * name), or 'version x.y, variant example' (see above), depending on the part
432 * of the library the passed-in library information belongs to.
435 * An array of library information, which may be version- or variant-specific.
436 * Passed by reference.
438 * The version the library information passed in $library belongs to, or NULL
439 * if the passed library information is not version-specific.
441 * The variant the library information passed in $library belongs to, or NULL
442 * if the passed library information is not variant-specific.
444 function _libraries_test_callback(&$library, $version, $variant, $group) {
446 if (isset($version) && isset($variant)) {
447 $string .= " (version $version, variant $variant)";
449 elseif (isset($version)) {
450 $string .= " (version $version)";
452 elseif (isset($variant)) {
453 $string .= " (variant $variant)";
456 $string .= ' (top-level)';
458 $library["$group callback"] = $string;
460 // The following is used to test caching of library information.
461 // Only set the message for the top-level library to prevent confusing,
462 // duplicate messages.
463 if (!isset($version) && !isset($variant) && \Drupal::state()->get('libraries_test.cache', FALSE)) {
464 drupal_set_message(SafeMarkup::set("The <em>$group</em> callback group was invoked."));
469 * Implements hook_menu().
471 function libraries_test_menu() {
472 $items['libraries_test/files'] = array(
473 'title' => 'Test files',
474 'route_name' => 'libraries_test_files',
476 $items['libraries_test/integration_files'] = array(
477 'title' => 'Test integration files',
478 'route_name' => 'libraries_test_integration_files',
480 $items['libraries_test/versions'] = array(
481 'title' => 'Test version loading',
482 'route_name' => 'libraries_test_versions',
484 $items['libraries_test/variant'] = array(
485 'title' => 'Test variant loading',
486 'route_name' => 'libraries_test_variant',
488 $items['libraries_test/versions_and_variants'] = array(
489 'title' => 'Test concurrent version and variant loading',
490 'route_name' => 'libraries_test_versions_and_variants',
492 $items['libraries_test/cache'] = array(
493 'title' => 'Test caching of library information',
494 'route_name' => 'libraries_test_cache',