Further modules included.
[yaffs-website] / web / modules / contrib / libraries / tests / modules / libraries_test / libraries_test.module
1 <?php
2
3 /**
4  * @file
5  * Tests the library detection and loading.
6  */
7
8 use Drupal\Component\Utility\SafeMarkup;
9
10 /**
11  * Implements hook_libraries_info().
12  */
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',
18   );
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),
24   );
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'),
30     'versions' => array(
31       '2' => array(),
32     ),
33   );
34
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'),
40     'versions' => array(
41       '1' => array(),
42     ),
43   );
44
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',
51       // Version 1
52       'pattern' => '/Version (\d+)/',
53       'lines' => 5,
54     ),
55   );
56
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',
61     // Version 1
62     'version callback' => '_libraries_test_get_version',
63     'version arguments' => array('README.txt', '/Version (\d+)/', 5),
64   );
65
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',
70     'version' => '1',
71     'files' => array(
72       'js' => array('example_1.js'),
73       'css' => array('example_1.css'),
74       'php' => array('example_1.php'),
75     ),
76   );
77
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',
84     'version' => '1',
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'),
90       ),
91     ),
92   );
93
94   // Test version overloading.
95   $libraries['example_versions'] = array(
96     'name' => 'Example versions',
97     'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
98     'version' => '2',
99     'versions' => array(
100       '1' => array(
101         'files' => array(
102           'js' => array('example_1.js'),
103           'css' => array('example_1.css'),
104           'php' => array('example_1.php'),
105         ),
106       ),
107       '2' => array(
108         'files' => array(
109           'js' => array('example_2.js'),
110           'css' => array('example_2.css'),
111           'php' => array('example_2.php'),
112         ),
113       ),
114     ),
115   );
116
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',
121     'version' => '1',
122     'variants' => array(
123       'example_variant' => array(
124         'files' => array(
125           'js' => array('example_3.js'),
126           'css' => array('example_3.css'),
127           'php' => array('example_3.php'),
128         ),
129         'variant callback' => '_libraries_test_return_installed',
130         'variant arguments' => array(FALSE),
131       ),
132     ),
133   );
134
135   $libraries['example_variant'] = array(
136     'name' => 'Example variant',
137     'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
138     'version' => '1',
139     'variants' => array(
140       'example_variant' => array(
141         'files' => array(
142           'js' => array('example_3.js'),
143           'css' => array('example_3.css'),
144           'php' => array('example_3.php'),
145         ),
146         'variant callback' => '_libraries_test_return_installed',
147         'variant arguments' => array(TRUE),
148       ),
149     ),
150   );
151
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',
156     'version' => '2',
157     'versions' => array(
158       '1' => array(
159         'variants' => array(
160           'example_variant_1' => array(
161             'files' => array(
162               'js' => array('example_1.js'),
163               'css' => array('example_1.css'),
164               'php' => array('example_1.php'),
165             ),
166             'variant callback' => '_libraries_test_return_installed',
167             'variant arguments' => array(TRUE),
168           ),
169           'example_variant_2' => array(
170             'files' => array(
171               'js' => array('example_2.js'),
172               'css' => array('example_2.css'),
173               'php' => array('example_2.php'),
174             ),
175             'variant callback' => '_libraries_test_return_installed',
176             'variant arguments' => array(TRUE),
177           ),
178         ),
179       ),
180       '2' => array(
181         'variants' => array(
182           'example_variant_1' => array(
183             'files' => array(
184               'js' => array('example_3.js'),
185               'css' => array('example_3.css'),
186               'php' => array('example_3.php'),
187             ),
188             'variant callback' => '_libraries_test_return_installed',
189             'variant arguments' => array(TRUE),
190           ),
191           'example_variant_2' => array(
192             'files' => array(
193               'js' => array('example_4.js'),
194               'css' => array('example_4.css'),
195               'php' => array('example_4.php'),
196             ),
197             'variant callback' => '_libraries_test_return_installed',
198             'variant arguments' => array(TRUE),
199           ),
200         ),
201       ),
202     ),
203   );
204
205   // Test dependency loading.
206   // We add one file to each library to be able to verify if it was loaded with
207   // libraries_load().
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',
212     'version' => '1.1',
213     'files' => array('js' => array('example_1.js')),
214   );
215   $libraries['example_dependency_missing'] = array(
216     'name' => 'Example dependency missing',
217     'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
218     'version' => '1',
219     'dependencies' => array('example_missing'),
220     'files' => array('js' => array('example_1.js')),
221   );
222   $libraries['example_dependency_incompatible'] = array(
223     'name' => 'Example dependency incompatible',
224     'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
225     'version' => '1',
226     'dependencies' => array('example_dependency (>1.1)'),
227     'files' => array('js' => array('example_1.js')),
228   );
229   $libraries['example_dependency_compatible'] = array(
230     'name' => 'Example dependency compatible',
231     'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
232     'version' => '1',
233     'dependencies' => array('example_dependency (>=1.1)'),
234     'files' => array('js' => array('example_1.js')),
235   );
236
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',
241     'version' => '1',
242     'versions' => array(
243       '1' => array(
244         'variants' => array(
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',
252           ),
253         ),
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',
260       ),
261     ),
262     'variants' => array(
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',
270       ),
271     ),
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'),
278     ),
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',
285   );
286
287   return $libraries;
288 }
289
290 /**
291  * Implements hook_libraries_info_file_paths()
292  */
293 function libraries_test_libraries_info_file_paths() {
294   return array(drupal_get_path('module', 'libraries') . '/tests/example');
295 }
296
297 /**
298  * Gets the version of an example library.
299  *
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.
303  */
304 function _libraries_test_return_version($library, $version) {
305   return $version;
306 }
307
308 /**
309  * Gets the version information from an arbitrary library.
310  *
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
317  * parameters.
318  *
319  * @param $library
320  *   An associative array containing all information about the library.
321  * @param $file
322  *   The filename to parse for the version, relative to the library path. For
323  *   example: 'docs/changelog.txt'.
324  * @param pattern
325  *   A string containing a regular expression (PCRE) to match the library
326  *   version. For example: '/@version (\d+)\.(\d+)/'.
327  * @param lines
328  *   (optional) The maximum number of lines to search the pattern in. Defaults
329  *   to 20.
330  * @param cols
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.
334  *
335  * @return
336  *   A string containing the version of the library.
337  *
338  * @see libraries_get_version()
339  */
340 function _libraries_test_get_version($library, $file, $pattern, $lines = 20, $cols = 200) {
341
342   $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $file;
343   if (!file_exists($file)) {
344     return;
345   }
346   $file = fopen($file, 'r');
347   while ($lines && $line = fgets($file, $cols)) {
348     if (preg_match($pattern, $line, $version)) {
349       fclose($file);
350       return $version[1];
351     }
352     $lines--;
353   }
354   fclose($file);
355 }
356
357 /**
358  * Detects the variant of an example library.
359  *
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.
363  */
364 function _libraries_test_return_installed($library, $name, $installed) {
365   return $installed;
366 }
367
368 /**
369  * Sets the 'info callback' key.
370  *
371  * This function is used as a test callback for the 'info' callback group.
372  *
373  * @see _libraries_test_callback()
374  */
375 function _libraries_test_info_callback(&$library, $version, $variant) {
376   _libraries_test_callback($library, $version, $variant, 'info');
377 }
378
379 /**
380  * Sets the 'pre-detect callback' key.
381  *
382  * This function is used as a test callback for the 'pre-detect' callback group.
383  *
384  * @see _libraries_test_callback()
385  */
386 function _libraries_test_pre_detect_callback(&$library, $version, $variant) {
387   _libraries_test_callback($library, $version, $variant, 'pre-detect');
388 }
389
390 /**
391  * Sets the 'post-detect callback' key.
392  *
393  * This function is used as a test callback for the 'post-detect callback group.
394  *
395  * @see _libraries_test_callback()
396  */
397 function _libraries_test_post_detect_callback(&$library, $version, $variant) {
398   _libraries_test_callback($library, $version, $variant, 'post-detect');
399 }
400
401 /**
402  * Sets the 'pre-load callback' key.
403  *
404  * This function is used as a test callback for the 'pre-load' callback group.
405  *
406  * @see _libraries_test_callback()
407  */
408 function _libraries_test_pre_load_callback(&$library, $version, $variant) {
409   _libraries_test_callback($library, $version, $variant, 'pre-load');
410 }
411
412 /**
413  * Sets the 'post-load callback' key.
414  *
415  * This function is used as a test callback for the 'post-load' callback group.
416  *
417  * @see _libraries_test_callback()
418  */
419 function _libraries_test_post_load_callback(&$library, $version, $variant) {
420   _libraries_test_callback($library, $version, $variant, 'post-load');
421 }
422
423 /**
424  * Sets the '[group] callback' key, where [group] is prepare, detect, or load.
425  *
426  * This function is used as a test callback for the all callback groups.
427  *
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.
433  *
434  * @param $library
435  *   An array of library information, which may be version- or variant-specific.
436  *   Passed by reference.
437  * @param $version
438  *   The version the library information passed in $library belongs to, or NULL
439  *   if the passed library information is not version-specific.
440  * @param $variant
441  *   The variant the library information passed in $library belongs to, or NULL
442  *   if the passed library information is not variant-specific.
443  */
444 function _libraries_test_callback(&$library, $version, $variant, $group) {
445   $string = 'applied';
446   if (isset($version) && isset($variant)) {
447     $string .= " (version $version, variant $variant)";
448   }
449   elseif (isset($version)) {
450     $string .= " (version $version)";
451   }
452   elseif (isset($variant)) {
453     $string .= " (variant $variant)";
454   }
455   else {
456     $string .= ' (top-level)';
457   }
458   $library["$group callback"] = $string;
459
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."));
465   }
466 }
467
468 /**
469  * Implements hook_menu().
470  */
471 function libraries_test_menu() {
472   $items['libraries_test/files'] = array(
473     'title' => 'Test files',
474     'route_name' => 'libraries_test_files',
475   );
476   $items['libraries_test/integration_files'] = array(
477     'title' => 'Test integration files',
478     'route_name' => 'libraries_test_integration_files',
479   );
480   $items['libraries_test/versions'] = array(
481     'title' => 'Test version loading',
482     'route_name' => 'libraries_test_versions',
483   );
484   $items['libraries_test/variant'] = array(
485     'title' => 'Test variant loading',
486     'route_name' => 'libraries_test_variant',
487   );
488   $items['libraries_test/versions_and_variants'] = array(
489     'title' => 'Test concurrent version and variant loading',
490     'route_name' => 'libraries_test_versions_and_variants',
491   );
492   $items['libraries_test/cache'] = array(
493     'title' => 'Test caching of library information',
494     'route_name' => 'libraries_test_cache',
495   );
496   return $items;
497 }