5 * Tests the library detection and loading.
9 * Implements hook_libraries_info().
11 function libraries_test_module_libraries_info() {
12 // Test library information gathering.
13 $libraries['example_module'] = array(
14 'name' => 'Example module',
15 'module_altered' => FALSE,
18 // Test library detection.
19 $libraries['example_missing'] = array(
20 'name' => 'Example missing',
21 // Provide a vendor and download URL to test that the UI links to it.
22 'vendor url' => 'http://example.com',
23 'download url' => 'http://example.com/download',
24 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/missing',
26 $libraries['example_undetected_version'] = array(
27 'name' => 'Example undetected version',
28 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
29 'version callback' => '_libraries_test_module_return_version',
30 'version arguments' => array(FALSE),
32 $libraries['example_unsupported_version'] = array(
33 'name' => 'Example unsupported version',
34 // Provide a download URL to test that the UI links to it.
35 'download url' => 'http://example.com/download',
36 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
37 'version callback' => '_libraries_test_module_return_version',
38 'version arguments' => array('1'),
43 $libraries['example_supported_version'] = array(
44 'name' => 'Example supported version',
45 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
46 'version callback' => '_libraries_test_module_return_version',
47 'version arguments' => array('1'),
53 // Test the default version callback.
54 $libraries['example_default_version_callback'] = array(
55 'name' => 'Example default version callback',
56 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
57 'version arguments' => array(
58 'file' => 'README.txt',
60 'pattern' => '/Version (\d+)/',
65 // Test a multiple-parameter version callback.
66 $libraries['example_multiple_parameter_version_callback'] = array(
67 'name' => 'Example multiple parameter version callback',
68 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
70 'version callback' => '_libraries_test_module_get_version',
71 'version arguments' => array('README.txt', '/Version (\d+)/', 5),
74 // Test a top-level files property.
75 $libraries['example_files'] = array(
76 'name' => 'Example files',
77 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
80 'js' => array('example_1.js'),
81 'css' => array('example_1.css'),
82 'php' => array('example_1.php'),
86 // Test loading of integration files.
87 // Normally added by the corresponding module via hook_libraries_info_alter(),
88 // these files should be automatically loaded when the library is loaded.
89 $libraries['example_module_integration_files'] = array(
90 'name' => 'Example module integration files',
91 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
93 'integration files' => array(
94 'libraries_test_module' => array(
95 'js' => array('libraries_test_module.js'),
96 'css' => array('libraries_test_module.css'),
97 'php' => array('libraries_test_module.inc'),
102 // Test loading of integration files after library files.
103 // We test the correct loading order by calling a function that is defined in
104 // example_1.php in libraries_test_module_post_load.inc.
105 $libraries['example_module_integration_files_post_load'] = array(
106 'name' => 'Example module post-load integration files',
107 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
110 'php' => array('example_1.php'),
112 'integration files' => array(
113 'libraries_test_module' => array(
114 'php' => array('libraries_test_module_post_load.inc'),
117 'post-load integration files' => TRUE,
120 // Test version overloading.
121 $libraries['example_versions'] = array(
122 'name' => 'Example versions',
123 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
128 'js' => array('example_1.js'),
129 'css' => array('example_1.css'),
130 'php' => array('example_1.php'),
135 'js' => array('example_2.js'),
136 'css' => array('example_2.css'),
137 'php' => array('example_2.php'),
143 // Test variant detection.
144 $libraries['example_variant_missing'] = array(
145 'name' => 'Example variant missing',
146 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
149 'example_variant' => array(
151 'js' => array('example_3.js'),
152 'css' => array('example_3.css'),
153 'php' => array('example_3.php'),
155 'variant callback' => '_libraries_test_module_return_installed',
156 'variant arguments' => array(FALSE),
161 $libraries['example_variant'] = array(
162 'name' => 'Example variant',
163 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
166 'example_variant' => array(
168 'js' => array('example_3.js'),
169 'css' => array('example_3.css'),
170 'php' => array('example_3.php'),
172 'variant callback' => '_libraries_test_module_return_installed',
173 'variant arguments' => array(TRUE),
178 // Test correct behaviour with multiple versions and multiple variants.
179 $libraries['example_versions_and_variants'] = array(
180 'name' => 'Example versions and variants',
181 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
186 'example_variant_1' => array(
188 'js' => array('example_1.js'),
189 'css' => array('example_1.css'),
190 'php' => array('example_1.php'),
192 'variant callback' => '_libraries_test_module_return_installed',
193 'variant arguments' => array(TRUE),
195 'example_variant_2' => array(
197 'js' => array('example_2.js'),
198 'css' => array('example_2.css'),
199 'php' => array('example_2.php'),
201 'variant callback' => '_libraries_test_module_return_installed',
202 'variant arguments' => array(TRUE),
208 'example_variant_1' => array(
210 'js' => array('example_3.js'),
211 'css' => array('example_3.css'),
212 'php' => array('example_3.php'),
214 'variant callback' => '_libraries_test_module_return_installed',
215 'variant arguments' => array(TRUE),
217 'example_variant_2' => array(
219 'js' => array('example_4.js'),
220 'css' => array('example_4.css'),
221 'php' => array('example_4.php'),
223 'variant callback' => '_libraries_test_module_return_installed',
224 'variant arguments' => array(TRUE),
231 // Test dependency loading.
232 // We add one file to each library to be able to verify if it was loaded with
234 // This library acts as a dependency for the libraries below.
235 $libraries['example_dependency'] = array(
236 'name' => 'Example dependency',
237 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
239 'files' => array('js' => array('example_1.js')),
241 $libraries['example_dependency_missing'] = array(
242 'name' => 'Example dependency missing',
243 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
245 'dependencies' => array('example_missing'),
246 'files' => array('js' => array('example_1.js')),
248 $libraries['example_dependency_incompatible'] = array(
249 'name' => 'Example dependency incompatible',
250 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
252 'dependencies' => array('example_dependency (>1.1)'),
253 'files' => array('js' => array('example_1.js')),
255 $libraries['example_dependency_compatible'] = array(
256 'name' => 'Example dependency compatible',
257 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
259 'dependencies' => array('example_dependency (>=1.1)'),
260 'files' => array('js' => array('example_1.js')),
263 // Test the applying of callbacks.
264 $libraries['example_callback'] = array(
265 'name' => 'Example callback',
266 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
271 'example_variant' => array(
272 // These keys are for testing purposes only.
273 'info callback' => 'not applied',
274 'pre-detect callback' => 'not applied',
275 'post-detect callback' => 'not applied',
276 'pre-dependencies-load callback' => 'not applied',
277 'pre-load callback' => 'not applied',
278 'post-load callback' => 'not applied',
281 // These keys are for testing purposes only.
282 'info callback' => 'not applied',
283 'pre-detect callback' => 'not applied',
284 'post-detect callback' => 'not applied',
285 'pre-dependencies-load callback' => 'not applied',
286 'pre-load callback' => 'not applied',
287 'post-load callback' => 'not applied',
291 'example_variant' => array(
292 // These keys are for testing purposes only.
293 'info callback' => 'not applied',
294 'pre-detect callback' => 'not applied',
295 'post-detect callback' => 'not applied',
296 'pre-dependencies-load callback' => 'not applied',
297 'pre-load callback' => 'not applied',
298 'post-load callback' => 'not applied',
301 'callbacks' => array(
302 'info' => array('_libraries_test_module_info_callback'),
303 'pre-detect' => array('_libraries_test_module_pre_detect_callback'),
304 'post-detect' => array('_libraries_test_module_post_detect_callback'),
305 'pre-dependencies-load' => array('_libraries_test_module_pre_dependencies_load_callback'),
306 'pre-load' => array('_libraries_test_module_pre_load_callback'),
307 'post-load' => array('_libraries_test_module_post_load_callback'),
309 // These keys are for testing purposes only.
310 'info callback' => 'not applied',
311 'pre-detect callback' => 'not applied',
312 'post-detect callback' => 'not applied',
313 'pre-dependencies-load callback' => 'not applied',
314 'pre-load callback' => 'not applied',
315 'post-load callback' => 'not applied',
318 $libraries['example_path_variable_override'] = array(
319 'name' => 'Example path variable override',
320 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
323 'php' => array('example_1.php', 'example_2.php'),
331 * Implements hook_libraries_info_alter().
333 function libraries_test_module_libraries_info_alter(&$libraries) {
334 $libraries['example_module']['module_altered'] = TRUE;
338 * Implements hook_libraries_info_file_paths()
340 function libraries_test_module_libraries_info_file_paths() {
341 return array(drupal_get_path('module', 'libraries') . '/tests/libraries');
345 * Gets the version of an example library.
347 * Returns exactly the version string entered as the $version parameter. This
348 * function cannot be collapsed with _libraries_test_module_return_installed(),
349 * because of the different arguments that are passed automatically.
351 function _libraries_test_module_return_version($library, $version) {
356 * Gets the version information from an arbitrary library.
358 * Test function for a version callback with multiple arguments. This is an
359 * exact copy of libraries_get_version(), which uses a single $option argument,
360 * except for the fact that it uses multiple arguments. Since we support both
361 * type of version callbacks, detecting the version of a test library with this
362 * function ensures that the arguments are passed correctly. This function might
363 * be a useful reference for a custom version callback that uses multiple
367 * An associative array containing all information about the library.
369 * The filename to parse for the version, relative to the library path. For
370 * example: 'docs/changelog.txt'.
372 * A string containing a regular expression (PCRE) to match the library
373 * version. For example: '/@version (\d+)\.(\d+)/'.
375 * (optional) The maximum number of lines to search the pattern in. Defaults
378 * (optional) The maximum number of characters per line to take into account.
379 * Defaults to 200. In case of minified or compressed files, this prevents
380 * reading the entire file into memory.
383 * A string containing the version of the library.
385 * @see libraries_get_version()
387 function _libraries_test_module_get_version($library, $file, $pattern, $lines = 20, $cols = 200) {
389 $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $file;
390 if (!file_exists($file)) {
393 $file = fopen($file, 'r');
394 while ($lines && $line = fgets($file, $cols)) {
395 if (preg_match($pattern, $line, $version)) {
405 * Detects the variant of an example library.
407 * Returns exactly the value of $installed, either TRUE or FALSE. This function
408 * cannot be collapsed with _libraries_test_module_return_version(), because of
409 * the different arguments that are passed automatically.
411 function _libraries_test_module_return_installed($library, $name, $installed) {
416 * Sets the 'info callback' key.
418 * This function is used as a test callback for the 'info' callback group.
420 * @see _libraries_test_module_callback()
422 function _libraries_test_module_info_callback(&$library, $version, $variant) {
423 _libraries_test_module_callback($library, $version, $variant, 'info');
427 * Sets the 'pre-detect callback' key.
429 * This function is used as a test callback for the 'pre-detect' callback group.
431 * @see _libraries_test_module_callback()
433 function _libraries_test_module_pre_detect_callback(&$library, $version, $variant) {
434 _libraries_test_module_callback($library, $version, $variant, 'pre-detect');
438 * Sets the 'post-detect callback' key.
440 * This function is used as a test callback for the 'post-detect callback group.
442 * @see _libraries_test_module_callback()
444 function _libraries_test_module_post_detect_callback(&$library, $version, $variant) {
445 _libraries_test_module_callback($library, $version, $variant, 'post-detect');
449 * Sets the 'pre-dependencies-load callback' key.
451 * This function is used as a test callback for the 'pre-dependencies-load'
454 * @see _libraries_test_module_callback()
456 function _libraries_test_module_pre_dependencies_load_callback(&$library, $version, $variant) {
457 _libraries_test_module_callback($library, $version, $variant, 'pre-dependencies-load');
461 * Sets the 'pre-load callback' key.
463 * This function is used as a test callback for the 'pre-load' callback group.
465 * @see _libraries_test_module_callback()
467 function _libraries_test_module_pre_load_callback(&$library, $version, $variant) {
468 _libraries_test_module_callback($library, $version, $variant, 'pre-load');
472 * Sets the 'post-load callback' key.
474 * This function is used as a test callback for the 'post-load' callback group.
476 * @see _libraries_test_module_callback()
478 function _libraries_test_module_post_load_callback(&$library, $version, $variant) {
479 _libraries_test_module_callback($library, $version, $variant, 'post-load');
483 * Sets the '[group] callback' key, where [group] is prepare, detect, or load.
485 * This function is used as a test callback for the all callback groups.
487 * It sets the '[group] callback' (see above) key to 'applied ([part])' where
488 * [part] is either 'top-level', 'version x.y' (where x.y is the passed-in
489 * version string), 'variant example' (where example is the passed-in variant
490 * name), or 'version x.y, variant example' (see above), depending on the part
491 * of the library the passed-in library information belongs to.
494 * An array of library information, which may be version- or variant-specific.
495 * Passed by reference.
497 * The version the library information passed in $library belongs to, or NULL
498 * if the passed library information is not version-specific.
500 * The variant the library information passed in $library belongs to, or NULL
501 * if the passed library information is not variant-specific.
503 function _libraries_test_module_callback(&$library, $version, $variant, $group) {
505 if (isset($version) && isset($variant)) {
506 $string .= " (version $version, variant $variant)";
508 elseif (isset($version)) {
509 $string .= " (version $version)";
511 elseif (isset($variant)) {
512 $string .= " (variant $variant)";
515 $string .= ' (top-level)';
517 $library["$group callback"] = $string;
519 // The following is used to test caching of library information.
520 // Only set the message for the top-level library to prevent confusing,
521 // duplicate messages.
522 if (!isset($version) && !isset($variant) && variable_get('libraries_test_module_cache', FALSE)) {
523 drupal_set_message("The <em>$group</em> callback group was invoked.");
528 * Implements hook_menu().
530 function libraries_test_module_menu() {
532 'page callback' => '_libraries_test_module_load',
533 'access callback' => TRUE,
535 $items['libraries-test-module/files'] = $base + array(
536 'title' => 'Test files',
537 'page arguments' => array('example_files'),
539 $items['libraries-test-module/module-integration-files'] = $base + array(
540 'title' => 'Test module integration files',
541 'page arguments' => array('example_module_integration_files'),
543 $items['libraries-test-module/module-integration-files-post-load'] = $base + array(
544 'title' => 'Test module post-load integration files',
545 'page arguments' => array('example_module_integration_files_post_load'),
547 $items['libraries-test-module/theme-integration-files'] = $base + array(
548 'title' => 'Test theme integration files',
549 'page arguments' => array('example_theme_integration_files'),
551 $items['libraries-test-module/versions'] = $base + array(
552 'title' => 'Test version loading',
553 'page arguments' => array('example_versions'),
555 $items['libraries-test-module/variant'] = $base + array(
556 'title' => 'Test variant loading',
557 'page arguments' => array('example_variant', 'example_variant'),
559 $items['libraries-test-module/versions-and-variants'] = $base + array(
560 'title' => 'Test concurrent version and variant loading',
561 'page arguments' => array('example_versions_and_variants', 'example_variant_2'),
563 $items['libraries-test-module/cache'] = $base + array(
564 'title' => 'Test caching of library information',
565 'page arguments' => array('example_callback'),
571 * Loads a specified library (variant) for testing.
573 * JavaScript and CSS files can be checked directly by SimpleTest, so we only
574 * need to manually check for PHP files. We provide information about the loaded
575 * JavaScript and CSS files for easier debugging. See example/README.txt for
578 function _libraries_test_module_load($library, $variant = NULL) {
579 libraries_load($library, $variant);
580 // JavaScript and CSS files can be checked directly by SimpleTest, so we only
581 // need to manually check for PHP files.
584 // For easer debugging of JS loading, a text is shown that the JavaScript will
586 $output .= '<h2>JavaScript</h2>';
587 $output .= '<div class="libraries-test-module-js">';
588 $output .= 'If this text shows up, no JavaScript test file was loaded.';
591 // For easier debugging of CSS loading, the loaded CSS files will color the
593 $output .= '<h2>CSS</h2>';
594 $output .= '<div class="libraries-test-module-css">';
595 $output .= 'If one of the CSS test files has been loaded, this text will be colored:';
597 // Do not reference the actual CSS files (i.e. including '.css'), because that
599 $output .= '<li>example_1: red</li>';
600 $output .= '<li>example_2: green</li>';
601 $output .= '<li>example_3: orange</li>';
602 $output .= '<li>example_4: blue</li>';
603 $output .= '<li>libraries_test_module: purple</li>';
604 $output .= '<li>libraries_test_theme: turquoise</li>';
608 $output .= '<h2>PHP</h2>';
609 $output .= '<div class="libraries-test-module-php">';
610 $output .= 'The following is a list of all loaded test PHP files:';
612 $files = get_included_files();
613 foreach ($files as $file) {
614 if (strpos($file, 'libraries/test') && !strpos($file, 'libraries_test_module.module') && !strpos($file, 'template.php')) {
615 $output .= '<li>' . str_replace(DRUPAL_ROOT . '/', '', $file) . '</li>';
625 * Implements hook_system_theme_info().
627 function libraries_test_module_system_theme_info() {
629 $themes['libraries_test_theme'] = drupal_get_path('module', 'libraries') . '/tests/themes/libraries_test_theme/libraries_test_theme.info';