public function LibrariesWebTest::assertLibraryFiles in Libraries API 8.3
Helper function to assert that a library was correctly loaded.
Asserts that all the correct files were loaded and all the incorrect ones were not.
Parameters
$name: The name of the files that should be loaded. The current testing system knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name has an associated JavaScript, CSS and PHP file that will be asserted. All other files will be asserted to not be loaded. See tests/example/README.txt for more information on how the loading of the files is tested.
$label: (optional) A label to prepend to the assertion messages, to make them less ambiguous.
$extensions: (optional) The expected file extensions of $name. Defaults to array('js', 'css', 'php').
1 call to LibrariesWebTest::assertLibraryFiles()
- LibrariesWebTest::_testLibrariesOutput in src/
Tests/ LibrariesWebTest.php - Tests that library files are properly added to the page output.
File
- src/
Tests/ LibrariesWebTest.php, line 475
Class
- LibrariesWebTest
- Tests basic detection and loading of libraries.
Namespace
Drupal\libraries\TestsCode
public function assertLibraryFiles($name, $label = '', $extensions = [
'js',
'css',
'php',
]) {
$label = $label !== '' ? "{$label}: " : '';
// Test that the wrong files are not loaded...
$names = [
'example_1' => FALSE,
'example_2' => FALSE,
'example_3' => FALSE,
'example_4' => FALSE,
];
// ...and the correct ones are.
$names[$name] = TRUE;
// Test for the specific HTML that the different file types appear as in the
// DOM.
$html = [
'js' => [
'<script src="',
'"></script>',
],
'css' => [
'<link rel="stylesheet" href="',
'" media="all" />',
],
// PHP files do not get added to the DOM directly.
// @see _libraries_test_load()
'php' => [
'<li>',
'</li>',
],
];
$html_expected = [];
$html_not_expected = [];
foreach ($names as $name => $expected) {
foreach ($extensions as $extension) {
$filepath = drupal_get_path('module', 'libraries') . "/tests/example/{$name}.{$extension}";
// JavaScript and CSS files appear as full URLs and with an appended
// query string.
if (in_array($extension, [
'js',
'css',
])) {
$filepath = $this->urlAssembler
->assemble("base://{$filepath}", [
'query' => [
$this->state
->get('system.css_js_query_string') ?: '0' => NULL,
],
'absolute' => TRUE,
]);
// If index.php is part of the generated URLs, we need to strip it.
// $filepath = str_replace('index.php/', '', $filepath);
}
list($prefix, $suffix) = $html[$extension];
$raw = $prefix . $filepath . $suffix;
if ($expected) {
$html_expected[] = Html::escape($raw);
$this
->assertRaw($raw, "{$label}{$name}.{$extension} found.");
}
else {
$html_not_expected[] = Html::escape($raw);
$this
->assertNoRaw($raw, "{$label}{$name}.{$extension} not found.");
}
}
}
$html_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_expected) . '</pre></li></ul>';
$html_not_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_not_expected) . '</pre></li></ul>';
$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}");
}