3 namespace Drupal\libraries\Tests;
5 use Drupal\Component\Utility\Html;
6 use Drupal\simpletest\WebTestBase;
9 * Tests basic detection and loading of libraries.
13 class LibrariesWebTest extends WebTestBase {
18 protected $profile = 'testing';
25 public static $modules = array('libraries', 'libraries_test');
28 * The URL generator used in this test.
30 * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
32 protected $urlAssembler;
35 * The state service used in this test.
37 * @var \Drupal\Core\State\StateInterface
44 protected function setUp() {
47 $this->urlAssembler = $this->container->get('unrouted_url_assembler');
48 $this->state = $this->container->get('state');
52 * Tests libraries_detect_dependencies().
54 function testLibrariesDetectDependencies() {
57 'dependencies' => array('example_missing'),
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'],
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.
84 $incompatible = array(
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'");
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'");
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'],
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');
129 * Tests libraries_scan_info_files().
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',
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>');
145 * Tests libraries_info().
147 function testLibrariesInfo() {
148 // Test that library information is found correctly.
150 'name' => 'Example files',
151 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
154 'js' => array('example_1.js' => array()),
155 'css' => array('example_1.css' => array()),
156 'php' => array('example_1.php' => array()),
158 'module' => 'libraries_test',
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.');
166 // Test a library specified with an .info file gets detected.
168 'name' => 'Example info file',
169 'info file' => drupal_get_path('module', 'libraries') . '/tests/example/example_info_file.libraries.info.yml',
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');
183 * Tests libraries_detect().
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'],
193 $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.');
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'],
202 $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.');
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'],
212 $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.');
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.');
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.');
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.');
229 // Test a top-level files property.
230 $library = libraries_detect('example_files');
232 'js' => array('example_1.js' => array()),
233 'css' => array('example_1.css' => array()),
234 'php' => array('example_1.php' => array()),
236 $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
237 $this->assertEqual($library['files'], $files, 'Top-level files property works.');
239 // Test version-specific library files.
240 $library = libraries_detect('example_versions');
242 'js' => array('example_2.js' => array()),
243 'css' => array('example_2.css' => array()),
244 'php' => array('example_2.php' => array()),
246 $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
247 $this->assertEqual($library['files'], $files, 'Version-specific library files found.');
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',
257 $this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.');
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.');
266 * Tests libraries_load().
268 * @todo Remove or rewrite to accomodate integration with core Libraries.
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');
287 * Tests the applying of callbacks.
289 function testCallbacks() {
291 'name' => 'Example callback',
292 'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
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',
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',
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',
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'),
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',
335 libraries_info_defaults($expected, 'example_callback');
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.');
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
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.');
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.');
393 * Tests that library files are properly added to the page output.
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.
398 * @todo Remove or rewrite to accomodate integration with core Libraries.
399 * @see _libraries_test_load()
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');
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');
412 // Test version overloading.
413 $this->drupalGet('libraries_test/versions');
414 $this->assertLibraryFiles('example_2', 'Version overloading');
416 // Test variant loading.
417 $this->drupalGet('libraries_test/variant');
418 $this->assertLibraryFiles('example_3', 'Variant loading');
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');
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
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);
447 * Helper function to assert that a library was correctly loaded.
449 * Asserts that all the correct files were loaded and all the incorrect ones
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
460 * (optional) A label to prepend to the assertion messages, to make them
463 * (optional) The expected file extensions of $name. Defaults to
464 * array('js', 'css', 'php').
466 function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) {
467 $label = ($label !== '' ? "$label: " : '');
469 // Test that the wrong files are not loaded...
471 'example_1' => FALSE,
472 'example_2' => FALSE,
473 'example_3' => FALSE,
474 'example_4' => FALSE,
476 // ...and the correct ones are.
477 $names[$name] = TRUE;
479 // Test for the specific HTML that the different file types appear as in the
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>'),
489 $html_expected = array();
490 $html_not_expected = array();
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
497 if (in_array($extension, array('js', 'css'))) {
498 $filepath = $this->urlAssembler->assemble("base://$filepath", [
500 $this->state->get('system.css_js_query_string') ?: '0' => NULL,
504 // If index.php is part of the generated URLs, we need to strip it.
505 //$filepath = str_replace('index.php/', '', $filepath);
507 list($prefix, $suffix) = $html[$extension];
508 $raw = $prefix . $filepath . $suffix;
510 $html_expected[] = Html::escape($raw);
511 $this->assertRaw($raw, "$label$name.$extension found.");
514 $html_not_expected[] = Html::escape($raw);
515 $this->assertNoRaw($raw, "$label$name.$extension not found.");
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}");