commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / libraries / tests / modules / libraries_test_module / libraries_test_module.module
1 <?php
2
3 /**
4 * @file
5 * Tests the library detection and loading.
6 */
7
8 /**
9 * Implements hook_libraries_info().
10 */
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,
16 );
17
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',
25 );
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),
31 );
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'),
39 'versions' => array(
40 '2' => array(),
41 ),
42 );
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'),
48 'versions' => array(
49 '1' => array(),
50 ),
51 );
52
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',
59 // Version 1
60 'pattern' => '/Version (\d+)/',
61 'lines' => 5,
62 ),
63 );
64
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',
69 // Version 1
70 'version callback' => '_libraries_test_module_get_version',
71 'version arguments' => array('README.txt', '/Version (\d+)/', 5),
72 );
73
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',
78 'version' => '1',
79 'files' => array(
80 'js' => array('example_1.js'),
81 'css' => array('example_1.css'),
82 'php' => array('example_1.php'),
83 ),
84 );
85
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',
92 'version' => '1',
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'),
98 ),
99 ),
100 );
101
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',
108 'version' => '1',
109 'files' => array(
110 'php' => array('example_1.php'),
111 ),
112 'integration files' => array(
113 'libraries_test_module' => array(
114 'php' => array('libraries_test_module_post_load.inc'),
115 ),
116 ),
117 'post-load integration files' => TRUE,
118 );
119
120 // Test version overloading.
121 $libraries['example_versions'] = array(
122 'name' => 'Example versions',
123 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
124 'version' => '2',
125 'versions' => array(
126 '1' => array(
127 'files' => array(
128 'js' => array('example_1.js'),
129 'css' => array('example_1.css'),
130 'php' => array('example_1.php'),
131 ),
132 ),
133 '2' => array(
134 'files' => array(
135 'js' => array('example_2.js'),
136 'css' => array('example_2.css'),
137 'php' => array('example_2.php'),
138 ),
139 ),
140 ),
141 );
142
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',
147 'version' => '1',
148 'variants' => array(
149 'example_variant' => array(
150 'files' => array(
151 'js' => array('example_3.js'),
152 'css' => array('example_3.css'),
153 'php' => array('example_3.php'),
154 ),
155 'variant callback' => '_libraries_test_module_return_installed',
156 'variant arguments' => array(FALSE),
157 ),
158 ),
159 );
160
161 $libraries['example_variant'] = array(
162 'name' => 'Example variant',
163 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
164 'version' => '1',
165 'variants' => array(
166 'example_variant' => array(
167 'files' => array(
168 'js' => array('example_3.js'),
169 'css' => array('example_3.css'),
170 'php' => array('example_3.php'),
171 ),
172 'variant callback' => '_libraries_test_module_return_installed',
173 'variant arguments' => array(TRUE),
174 ),
175 ),
176 );
177
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',
182 'version' => '2',
183 'versions' => array(
184 '1' => array(
185 'variants' => array(
186 'example_variant_1' => array(
187 'files' => array(
188 'js' => array('example_1.js'),
189 'css' => array('example_1.css'),
190 'php' => array('example_1.php'),
191 ),
192 'variant callback' => '_libraries_test_module_return_installed',
193 'variant arguments' => array(TRUE),
194 ),
195 'example_variant_2' => array(
196 'files' => array(
197 'js' => array('example_2.js'),
198 'css' => array('example_2.css'),
199 'php' => array('example_2.php'),
200 ),
201 'variant callback' => '_libraries_test_module_return_installed',
202 'variant arguments' => array(TRUE),
203 ),
204 ),
205 ),
206 '2' => array(
207 'variants' => array(
208 'example_variant_1' => array(
209 'files' => array(
210 'js' => array('example_3.js'),
211 'css' => array('example_3.css'),
212 'php' => array('example_3.php'),
213 ),
214 'variant callback' => '_libraries_test_module_return_installed',
215 'variant arguments' => array(TRUE),
216 ),
217 'example_variant_2' => array(
218 'files' => array(
219 'js' => array('example_4.js'),
220 'css' => array('example_4.css'),
221 'php' => array('example_4.php'),
222 ),
223 'variant callback' => '_libraries_test_module_return_installed',
224 'variant arguments' => array(TRUE),
225 ),
226 ),
227 ),
228 ),
229 );
230
231 // Test dependency loading.
232 // We add one file to each library to be able to verify if it was loaded with
233 // libraries_load().
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',
238 'version' => '1.1',
239 'files' => array('js' => array('example_1.js')),
240 );
241 $libraries['example_dependency_missing'] = array(
242 'name' => 'Example dependency missing',
243 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
244 'version' => '1',
245 'dependencies' => array('example_missing'),
246 'files' => array('js' => array('example_1.js')),
247 );
248 $libraries['example_dependency_incompatible'] = array(
249 'name' => 'Example dependency incompatible',
250 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
251 'version' => '1',
252 'dependencies' => array('example_dependency (>1.1)'),
253 'files' => array('js' => array('example_1.js')),
254 );
255 $libraries['example_dependency_compatible'] = array(
256 'name' => 'Example dependency compatible',
257 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
258 'version' => '1',
259 'dependencies' => array('example_dependency (>=1.1)'),
260 'files' => array('js' => array('example_1.js')),
261 );
262
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',
267 'version' => '1',
268 'versions' => array(
269 '1' => array(
270 'variants' => array(
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',
279 ),
280 ),
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',
288 ),
289 ),
290 'variants' => array(
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',
299 ),
300 ),
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'),
308 ),
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',
316 );
317
318 $libraries['example_path_variable_override'] = array(
319 'name' => 'Example path variable override',
320 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
321 'version' => '1',
322 'files' => array(
323 'php' => array('example_1.php', 'example_2.php'),
324 ),
325 );
326
327 return $libraries;
328 }
329
330 /**
331 * Implements hook_libraries_info_alter().
332 */
333 function libraries_test_module_libraries_info_alter(&$libraries) {
334 $libraries['example_module']['module_altered'] = TRUE;
335 }
336
337 /**
338 * Implements hook_libraries_info_file_paths()
339 */
340 function libraries_test_module_libraries_info_file_paths() {
341 return array(drupal_get_path('module', 'libraries') . '/tests/libraries');
342 }
343
344 /**
345 * Gets the version of an example library.
346 *
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.
350 */
351 function _libraries_test_module_return_version($library, $version) {
352 return $version;
353 }
354
355 /**
356 * Gets the version information from an arbitrary library.
357 *
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
364 * parameters.
365 *
366 * @param $library
367 * An associative array containing all information about the library.
368 * @param $file
369 * The filename to parse for the version, relative to the library path. For
370 * example: 'docs/changelog.txt'.
371 * @param pattern
372 * A string containing a regular expression (PCRE) to match the library
373 * version. For example: '/@version (\d+)\.(\d+)/'.
374 * @param lines
375 * (optional) The maximum number of lines to search the pattern in. Defaults
376 * to 20.
377 * @param cols
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.
381 *
382 * @return
383 * A string containing the version of the library.
384 *
385 * @see libraries_get_version()
386 */
387 function _libraries_test_module_get_version($library, $file, $pattern, $lines = 20, $cols = 200) {
388
389 $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $file;
390 if (!file_exists($file)) {
391 return;
392 }
393 $file = fopen($file, 'r');
394 while ($lines && $line = fgets($file, $cols)) {
395 if (preg_match($pattern, $line, $version)) {
396 fclose($file);
397 return $version[1];
398 }
399 $lines--;
400 }
401 fclose($file);
402 }
403
404 /**
405 * Detects the variant of an example library.
406 *
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.
410 */
411 function _libraries_test_module_return_installed($library, $name, $installed) {
412 return $installed;
413 }
414
415 /**
416 * Sets the 'info callback' key.
417 *
418 * This function is used as a test callback for the 'info' callback group.
419 *
420 * @see _libraries_test_module_callback()
421 */
422 function _libraries_test_module_info_callback(&$library, $version, $variant) {
423 _libraries_test_module_callback($library, $version, $variant, 'info');
424 }
425
426 /**
427 * Sets the 'pre-detect callback' key.
428 *
429 * This function is used as a test callback for the 'pre-detect' callback group.
430 *
431 * @see _libraries_test_module_callback()
432 */
433 function _libraries_test_module_pre_detect_callback(&$library, $version, $variant) {
434 _libraries_test_module_callback($library, $version, $variant, 'pre-detect');
435 }
436
437 /**
438 * Sets the 'post-detect callback' key.
439 *
440 * This function is used as a test callback for the 'post-detect callback group.
441 *
442 * @see _libraries_test_module_callback()
443 */
444 function _libraries_test_module_post_detect_callback(&$library, $version, $variant) {
445 _libraries_test_module_callback($library, $version, $variant, 'post-detect');
446 }
447
448 /**
449 * Sets the 'pre-dependencies-load callback' key.
450 *
451 * This function is used as a test callback for the 'pre-dependencies-load'
452 * callback group.
453 *
454 * @see _libraries_test_module_callback()
455 */
456 function _libraries_test_module_pre_dependencies_load_callback(&$library, $version, $variant) {
457 _libraries_test_module_callback($library, $version, $variant, 'pre-dependencies-load');
458 }
459
460 /**
461 * Sets the 'pre-load callback' key.
462 *
463 * This function is used as a test callback for the 'pre-load' callback group.
464 *
465 * @see _libraries_test_module_callback()
466 */
467 function _libraries_test_module_pre_load_callback(&$library, $version, $variant) {
468 _libraries_test_module_callback($library, $version, $variant, 'pre-load');
469 }
470
471 /**
472 * Sets the 'post-load callback' key.
473 *
474 * This function is used as a test callback for the 'post-load' callback group.
475 *
476 * @see _libraries_test_module_callback()
477 */
478 function _libraries_test_module_post_load_callback(&$library, $version, $variant) {
479 _libraries_test_module_callback($library, $version, $variant, 'post-load');
480 }
481
482 /**
483 * Sets the '[group] callback' key, where [group] is prepare, detect, or load.
484 *
485 * This function is used as a test callback for the all callback groups.
486 *
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.
492 *
493 * @param $library
494 * An array of library information, which may be version- or variant-specific.
495 * Passed by reference.
496 * @param $version
497 * The version the library information passed in $library belongs to, or NULL
498 * if the passed library information is not version-specific.
499 * @param $variant
500 * The variant the library information passed in $library belongs to, or NULL
501 * if the passed library information is not variant-specific.
502 */
503 function _libraries_test_module_callback(&$library, $version, $variant, $group) {
504 $string = 'applied';
505 if (isset($version) && isset($variant)) {
506 $string .= " (version $version, variant $variant)";
507 }
508 elseif (isset($version)) {
509 $string .= " (version $version)";
510 }
511 elseif (isset($variant)) {
512 $string .= " (variant $variant)";
513 }
514 else {
515 $string .= ' (top-level)';
516 }
517 $library["$group callback"] = $string;
518
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.");
524 }
525 }
526
527 /**
528 * Implements hook_menu().
529 */
530 function libraries_test_module_menu() {
531 $base = array(
532 'page callback' => '_libraries_test_module_load',
533 'access callback' => TRUE,
534 );
535 $items['libraries-test-module/files'] = $base + array(
536 'title' => 'Test files',
537 'page arguments' => array('example_files'),
538 );
539 $items['libraries-test-module/module-integration-files'] = $base + array(
540 'title' => 'Test module integration files',
541 'page arguments' => array('example_module_integration_files'),
542 );
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'),
546 );
547 $items['libraries-test-module/theme-integration-files'] = $base + array(
548 'title' => 'Test theme integration files',
549 'page arguments' => array('example_theme_integration_files'),
550 );
551 $items['libraries-test-module/versions'] = $base + array(
552 'title' => 'Test version loading',
553 'page arguments' => array('example_versions'),
554 );
555 $items['libraries-test-module/variant'] = $base + array(
556 'title' => 'Test variant loading',
557 'page arguments' => array('example_variant', 'example_variant'),
558 );
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'),
562 );
563 $items['libraries-test-module/cache'] = $base + array(
564 'title' => 'Test caching of library information',
565 'page arguments' => array('example_callback'),
566 );
567 return $items;
568 }
569
570 /**
571 * Loads a specified library (variant) for testing.
572 *
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
576 * more information.
577 */
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.
582 $output = '';
583
584 // For easer debugging of JS loading, a text is shown that the JavaScript will
585 // replace.
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.';
589 $output .= '</div>';
590
591 // For easier debugging of CSS loading, the loaded CSS files will color the
592 // following text.
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:';
596 $output .= '<ul>';
597 // Do not reference the actual CSS files (i.e. including '.css'), because that
598 // breaks testing.
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>';
605 $output .= '</ul>';
606 $output .= '</div>';
607
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:';
611 $output .= '<ul>';
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>';
616 }
617 }
618 $output .= '</ul>';
619 $output .= '</div>';
620
621 return $output;
622 }
623
624 /**
625 * Implements hook_system_theme_info().
626 */
627 function libraries_test_module_system_theme_info() {
628 $themes = array();
629 $themes['libraries_test_theme'] = drupal_get_path('module', 'libraries') . '/tests/themes/libraries_test_theme/libraries_test_theme.info';
630 return $themes;
631 }