2c9254c45323010c0145c28f58785d39e4e46f81
[yaffs-website] / web / modules / contrib / libraries / src / Tests / LibrariesWebTest.php
1 <?php
2
3 namespace Drupal\libraries\Tests;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests basic detection and loading of libraries.
10  *
11  * @group libraries
12  */
13 class LibrariesWebTest extends WebTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected $profile = 'testing';
19
20   /**
21    * Modules to install.
22    *
23    * @var array
24    */
25   public static $modules = array('libraries', 'libraries_test');
26
27   /**
28    * The URL generator used in this test.
29    *
30    * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
31    */
32   protected $urlAssembler;
33
34   /**
35    * The state service used in this test.
36    *
37    * @var \Drupal\Core\State\StateInterface
38    */
39   protected $state;
40
41   /**
42    * {@inheritdoc}
43    */
44   protected function setUp() {
45     parent::setUp();
46
47     $this->urlAssembler = $this->container->get('unrouted_url_assembler');
48     $this->state = $this->container->get('state');
49   }
50
51   /**
52    * Tests libraries_detect_dependencies().
53    */
54   function testLibrariesDetectDependencies() {
55     $library = array(
56       'name' => 'Example',
57       'dependencies' => array('example_missing'),
58     );
59     libraries_detect_dependencies($library);
60     $this->assertEqual($library['error'], 'missing dependency', 'libraries_detect_dependencies() detects missing dependency');
61     $error_message = t('The %dependency library, which the %library library depends on, is not installed.', array(
62       '%dependency' => 'Example missing',
63       '%library' => $library['name'],
64     ));
65     $this->verbose("Expected:<br>$error_message");
66     $this->verbose('Actual:<br>' . $library['error message']);
67     $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing dependency');
68     // Test versioned dependencies.
69     $version = '1.1';
70     $compatible = array(
71       '1.1',
72       '<=1.1',
73       '>=1.1',
74       '<1.2',
75       '<2.0',
76       '>1.0',
77       '>1.0-rc1',
78       '>1.0-beta2',
79       '>1.0-alpha3',
80       '>0.1',
81       '<1.2, >1.0',
82       '>0.1, <=1.1',
83     );
84     $incompatible = array(
85       '1.2',
86       '2.0',
87       '<1.1',
88       '>1.1',
89       '<=1.0',
90       '<=1.0-rc1',
91       '<=1.0-beta2',
92       '<=1.0-alpha3',
93       '>=1.2',
94       '<1.1, >0.9',
95       '>=0.1, <1.1',
96     );
97     $library = array(
98       'name' => 'Example',
99     );
100     foreach ($compatible as $version_string) {
101       $library['dependencies'][0] = "example_dependency ($version_string)";
102       // libraries_detect_dependencies() is a post-detect callback, so
103       // 'installed' is already set, when it is called. It sets the value to
104       // FALSE for missing or incompatible dependencies.
105       $library['installed'] = TRUE;
106       libraries_detect_dependencies($library);
107       $this->assertTrue($library['installed'], "libraries_detect_dependencies() detects compatible version string: '$version_string' is compatible with '$version'");
108     }
109     foreach ($incompatible as $version_string) {
110       $library['dependencies'][0] = "example_dependency ($version_string)";
111       $library['installed'] = TRUE;
112       unset($library['error'], $library['error message']);
113       libraries_detect_dependencies($library);
114       $this->assertEqual($library['error'], 'incompatible dependency', "libraries_detect_dependencies() detects incompatible version strings: '$version_string' is incompatible with '$version'");
115     }
116     // Instead of repeating this assertion for each version string, we just
117     // re-use the $library variable from the foreach loop.
118     $error_message = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', array(
119       '%dependency_version' => $version,
120       '%dependency' => 'Example dependency',
121       '%library' => $library['name'],
122     ));
123     $this->verbose("Expected:<br>$error_message");
124     $this->verbose('Actual:<br>' . $library['error message']);
125     $this->assertEqual($library['error message'], $error_message, 'Correct error message for an incompatible dependency');
126   }
127
128   /**
129    * Tests libraries_scan_info_files().
130    */
131   function testLibrariesScanInfoFiles() {
132     $expected = array('example_info_file' => (object) array(
133       'uri' => drupal_get_path('module', 'libraries') . '/tests/example/example_info_file.libraries.info.yml',
134       'filename' => 'example_info_file.libraries.info.yml',
135       'name' => 'example_info_file.libraries.info',
136     ));
137     $actual = libraries_scan_info_files();
138     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
139     $this->verbose('Actual:<pre>' . var_export($actual, TRUE) . '</pre>');
140     $this->assertEqual($actual, $expected, 'libraries_scan_info_files() correctly finds the example info file.');
141     $this->verbose('<pre>' . var_export(libraries_scan_info_files(), TRUE) . '</pre>');
142   }
143
144   /**
145    * Tests libraries_info().
146    */
147   function testLibrariesInfo() {
148     // Test that library information is found correctly.
149     $expected = array(
150       'name' => 'Example files',
151       'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
152       'version' => '1',
153       'files' => array(
154         'js' => array('example_1.js' => array()),
155         'css' => array('example_1.css' => array()),
156         'php' => array('example_1.php' => array()),
157       ),
158       'module' => 'libraries_test',
159     );
160     libraries_info_defaults($expected, 'example_files');
161     $library = libraries_info('example_files');
162     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
163     $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
164     $this->assertEqual($library, $expected, 'Library information is correctly gathered.');
165
166     // Test a library specified with an .info file gets detected.
167     $expected = array(
168       'name' => 'Example info file',
169       'info file' => drupal_get_path('module', 'libraries') . '/tests/example/example_info_file.libraries.info.yml',
170     );
171     libraries_info_defaults($expected, 'example_info_file');
172     $library = libraries_info('example_info_file');
173     // If this module was downloaded from Drupal.org, the Drupal.org packaging
174     // system has corrupted the test info file.
175     // @see http://drupal.org/node/1606606
176     unset($library['core'], $library['datestamp'], $library['project'], $library['version']);
177     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
178     $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
179     $this->assertEqual($library, $expected, 'Library specified with an .info file found');
180   }
181
182   /**
183    * Tests libraries_detect().
184    */
185   function testLibrariesDetect() {
186     // Test missing library.
187     $library = libraries_detect('example_missing');
188     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
189     $this->assertEqual($library['error'], 'not found', 'Missing library not found.');
190     $error_message = t('The %library library could not be found.', array(
191       '%library' => $library['name'],
192     ));
193     $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.');
194
195     // Test unknown library version.
196     $library = libraries_detect('example_undetected_version');
197     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
198     $this->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.');
199     $error_message = t('The version of the %library library could not be detected.', array(
200       '%library' => $library['name'],
201     ));
202     $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.');
203
204     // Test unsupported library version.
205     $library = libraries_detect('example_unsupported_version');
206     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
207     $this->assertEqual($library['error'], 'not supported', 'Unsupported version detected as such.');
208     $error_message = t('The installed version %version of the %library library is not supported.', array(
209       '%version' => $library['version'],
210       '%library' => $library['name'],
211     ));
212     $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.');
213
214     // Test supported library version.
215     $library = libraries_detect('example_supported_version');
216     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
217     $this->assertEqual($library['installed'], TRUE, 'Supported library version found.');
218
219     // Test libraries_get_version().
220     $library = libraries_detect('example_default_version_callback');
221     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
222     $this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.');
223
224     // Test a multiple-parameter version callback.
225     $library = libraries_detect('example_multiple_parameter_version_callback');
226     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
227     $this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.');
228
229     // Test a top-level files property.
230     $library = libraries_detect('example_files');
231     $files = array(
232       'js' => array('example_1.js' => array()),
233       'css' => array('example_1.css' => array()),
234       'php' => array('example_1.php' => array()),
235     );
236     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
237     $this->assertEqual($library['files'], $files, 'Top-level files property works.');
238
239     // Test version-specific library files.
240     $library = libraries_detect('example_versions');
241     $files = array(
242       'js' => array('example_2.js' => array()),
243       'css' => array('example_2.css' => array()),
244       'php' => array('example_2.php' => array()),
245     );
246     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
247     $this->assertEqual($library['files'], $files, 'Version-specific library files found.');
248
249     // Test missing variant.
250     $library = libraries_detect('example_variant_missing');
251     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
252     $this->assertEqual($library['variants']['example_variant']['error'], 'not found', 'Missing variant not found');
253     $error_message = t('The %variant variant of the %library library could not be found.', array(
254       '%variant' => 'example_variant',
255       '%library' => 'Example variant missing',
256     ));
257     $this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.');
258
259     // Test existing variant.
260     $library = libraries_detect('example_variant');
261     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
262     $this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.');
263   }
264
265   /**
266    * Tests libraries_load().
267    *
268    * @todo Remove or rewrite to accomodate integration with core Libraries.
269    */
270   function _testLibrariesLoad() {
271     // Test dependencies.
272     $library = libraries_load('example_dependency_missing');
273     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
274     $this->assertFalse($library['loaded'], 'Library with missing dependency cannot be loaded');
275     $library = libraries_load('example_dependency_incompatible');
276     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
277     $this->assertFalse($library['loaded'], 'Library with incompatible dependency cannot be loaded');
278     $library = libraries_load('example_dependency_compatible');
279     $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
280     $this->assertEqual($library['loaded'], 1, 'Library with compatible dependency is loaded');
281     $loaded = &drupal_static('libraries_load');
282     $this->verbose('<pre>' . var_export($loaded, TRUE) . '</pre>');
283     $this->assertEqual($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded');
284   }
285
286   /**
287    * Tests the applying of callbacks.
288    */
289   function testCallbacks() {
290     $expected = array(
291       'name' => 'Example callback',
292       'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
293       'version' => '1',
294       'versions' => array(
295         '1' => array(
296           'variants' => array(
297             'example_variant' => array(
298               'info callback' => 'not applied',
299               'pre-detect callback' => 'not applied',
300               'post-detect callback' => 'not applied',
301               'pre-load callback' => 'not applied',
302               'post-load callback' => 'not applied',
303             ),
304           ),
305           'info callback' => 'not applied',
306           'pre-detect callback' => 'not applied',
307           'post-detect callback' => 'not applied',
308           'pre-load callback' => 'not applied',
309           'post-load callback' => 'not applied',
310         ),
311       ),
312       'variants' => array(
313         'example_variant' => array(
314           'info callback' => 'not applied',
315           'pre-detect callback' => 'not applied',
316           'post-detect callback' => 'not applied',
317           'pre-load callback' => 'not applied',
318           'post-load callback' => 'not applied',
319         ),
320       ),
321       'callbacks' => array(
322         'info' => array('_libraries_test_info_callback'),
323         'pre-detect' => array('_libraries_test_pre_detect_callback'),
324         'post-detect' => array('_libraries_test_post_detect_callback'),
325         'pre-load' => array('_libraries_test_pre_load_callback'),
326         'post-load' => array('_libraries_test_post_load_callback'),
327       ),
328       'info callback' => 'not applied',
329       'pre-detect callback' => 'not applied',
330       'post-detect callback' => 'not applied',
331       'pre-load callback' => 'not applied',
332       'post-load callback' => 'not applied',
333       'module' => 'libraries_test',
334     );
335     libraries_info_defaults($expected, 'example_callback');
336
337     // Test a callback in the 'info' group.
338     $expected['info callback'] = 'applied (top-level)';
339     $expected['versions']['1']['info callback'] = 'applied (version 1)';
340     $expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
341     $expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)';
342     $library = libraries_info('example_callback');
343     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
344     $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
345     $this->assertEqual($library, $expected, 'Prepare callback was applied correctly.');
346
347     // Test a callback in the 'pre-detect' and 'post-detect' phases.
348     // Successfully detected libraries should only contain version information
349     // for the detected version and thus, be marked as installed.
350     unset($expected['versions']);
351     $expected['installed'] = TRUE;
352     // Additionally, version-specific properties of the detected version are
353     // supposed to override the corresponding top-level properties.
354     $expected['info callback'] = 'applied (version 1)';
355     $expected['variants']['example_variant']['installed'] = TRUE;
356     $expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
357     // Version-overloading takes place after the 'pre-detect' callbacks have
358     // been applied.
359     $expected['pre-detect callback'] = 'applied (version 1)';
360     $expected['post-detect callback'] = 'applied (top-level)';
361     $expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)';
362     $expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)';
363     $library = libraries_detect('example_callback');
364     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
365     $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
366     $this->assertEqual($library, $expected, 'Detect callback was applied correctly.');
367
368     // Test a callback in the 'pre-load' and 'post-load' phases.
369     // Successfully loaded libraries should only contain information about the
370     // already loaded variant.
371     unset($expected['variants']);
372     $expected['loaded'] = 0;
373     $expected['pre-load callback'] = 'applied (top-level)';
374     $expected['post-load callback'] = 'applied (top-level)';
375     $library = libraries_load('example_callback');
376     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
377     $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
378     $this->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.');
379     // This is not recommended usually and is only used for testing purposes.
380     drupal_static_reset('libraries_load');
381     // Successfully loaded library variants are supposed to contain the specific
382     // variant information only.
383     $expected['info callback'] = 'applied (version 1, variant example_variant)';
384     $expected['pre-detect callback'] = 'applied (version 1, variant example_variant)';
385     $expected['post-detect callback'] = 'applied (variant example_variant)';
386     $library = libraries_load('example_callback', 'example_variant');
387     $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
388     $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
389     $this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.');
390   }
391
392   /**
393    * Tests that library files are properly added to the page output.
394    *
395    * We check for JavaScript and CSS files directly in the DOM and add a list of
396    * included PHP files manually to the page output.
397    *
398    * @todo Remove or rewrite to accomodate integration with core Libraries.
399    * @see _libraries_test_load()
400    */
401   function _testLibrariesOutput() {
402     // Test loading of a simple library with a top-level files property.
403     $this->drupalGet('libraries_test/files');
404     $this->assertLibraryFiles('example_1', 'File loading');
405
406     // Test loading of integration files.
407     $this->drupalGet('libraries_test/integration_files');
408     $this->assertRaw('libraries_test.js', 'Integration file loading: libraries_test.js found');
409     $this->assertRaw('libraries_test.css', 'Integration file loading: libraries_test.css found');
410     $this->assertRaw('libraries_test.inc', 'Integration file loading: libraries_test.inc found');
411
412     // Test version overloading.
413     $this->drupalGet('libraries_test/versions');
414     $this->assertLibraryFiles('example_2', 'Version overloading');
415
416     // Test variant loading.
417     $this->drupalGet('libraries_test/variant');
418     $this->assertLibraryFiles('example_3', 'Variant loading');
419
420     // Test version overloading and variant loading.
421     $this->drupalGet('libraries_test/versions_and_variants');
422     $this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading');
423
424     // Test caching.
425     \Drupal::state()->set('libraries_test.cache', TRUE);
426     \Drupal::cache('libraries')->delete('example_callback');
427     // When the library information is not cached, all callback groups should be
428     // invoked.
429     $this->drupalGet('libraries_test/cache');
430     $this->assertRaw('The <em>info</em> callback group was invoked.', 'Info callback invoked for uncached libraries.');
431     $this->assertRaw('The <em>pre-detect</em> callback group was invoked.', 'Pre-detect callback invoked for uncached libraries.');
432     $this->assertRaw('The <em>post-detect</em> callback group was invoked.', 'Post-detect callback invoked for uncached libraries.');
433     $this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for uncached libraries.');
434     $this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for uncached libraries.');
435     // When the library information is cached only the 'pre-load' and
436     // 'post-load' callback groups should be invoked.
437     $this->drupalGet('libraries_test/cache');
438     $this->assertNoRaw('The <em>info</em> callback group was not invoked.', 'Info callback not invoked for cached libraries.');
439     $this->assertNoRaw('The <em>pre-detect</em> callback group was not invoked.', 'Pre-detect callback not invoked for cached libraries.');
440     $this->assertNoRaw('The <em>post-detect</em> callback group was not invoked.', 'Post-detect callback not invoked for cached libraries.');
441     $this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for cached libraries.');
442     $this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for cached libraries.');
443     \Drupal::state()->set('libraries_test.cache', FALSE);
444   }
445
446   /**
447    * Helper function to assert that a library was correctly loaded.
448    *
449    * Asserts that all the correct files were loaded and all the incorrect ones
450    * were not.
451    *
452    * @param $name
453    *   The name of the files that should be loaded. The current testing system
454    *   knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name
455    *   has an associated JavaScript, CSS and PHP file that will be asserted. All
456    *   other files will be asserted to not be loaded. See
457    *   tests/example/README.txt for more information on how the loading of the
458    *   files is tested.
459    * @param $label
460    *   (optional) A label to prepend to the assertion messages, to make them
461    *   less ambiguous.
462    * @param $extensions
463    *   (optional) The expected file extensions of $name. Defaults to
464    *   array('js', 'css', 'php').
465    */
466   function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) {
467     $label = ($label !== '' ? "$label: " : '');
468
469     // Test that the wrong files are not loaded...
470     $names = array(
471       'example_1' => FALSE,
472       'example_2' => FALSE,
473       'example_3' => FALSE,
474       'example_4' => FALSE,
475     );
476     // ...and the correct ones are.
477     $names[$name] = TRUE;
478
479     // Test for the specific HTML that the different file types appear as in the
480     // DOM.
481     $html = array(
482       'js' => array('<script src="', '"></script>'),
483       'css' => array('<link rel="stylesheet" href="', '" media="all" />'),
484       // PHP files do not get added to the DOM directly.
485       // @see _libraries_test_load()
486       'php' => array('<li>', '</li>'),
487     );
488
489     $html_expected = array();
490     $html_not_expected = array();
491
492     foreach ($names as $name => $expected) {
493       foreach ($extensions as $extension) {
494         $filepath = drupal_get_path('module', 'libraries') . "/tests/example/$name.$extension";
495         // JavaScript and CSS files appear as full URLs and with an appended
496         // query string.
497         if (in_array($extension, array('js', 'css'))) {
498           $filepath = $this->urlAssembler->assemble("base://$filepath", [
499             'query' => [
500               $this->state->get('system.css_js_query_string') ?: '0' => NULL,
501             ],
502             'absolute' => TRUE,
503           ]);
504           // If index.php is part of the generated URLs, we need to strip it.
505           //$filepath = str_replace('index.php/', '', $filepath);
506         }
507         list($prefix, $suffix) = $html[$extension];
508         $raw = $prefix . $filepath . $suffix;
509         if ($expected) {
510           $html_expected[] = Html::escape($raw);
511           $this->assertRaw($raw, "$label$name.$extension found.");
512         }
513         else {
514           $html_not_expected[] = Html::escape($raw);
515           $this->assertNoRaw($raw, "$label$name.$extension not found.");
516         }
517       }
518     }
519
520     $html_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_expected) . '</pre></li></ul>';
521     $html_not_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_not_expected) . '</pre></li></ul>';
522     $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}");
523   }
524
525 }