You are here

function LibrariesTestCase::assertLibraryFiles in Libraries API 7.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 LibrariesTestCase::assertLibraryFiles()
LibrariesTestCase::testLibrariesOutput in tests/libraries.test
Tests that library files are properly added to the page output.

File

tests/libraries.test, line 525
Tests for Libraries API.

Class

LibrariesTestCase
Tests basic detection and loading of libraries.

Code

function assertLibraryFiles($name, $label = '', $extensions = array(
  'js',
  'css',
  'php',
)) {
  $label = $label !== '' ? "{$label}: " : '';

  // Test that the wrong files are not loaded...
  $names = array(
    '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 = array(
    'js' => array(
      '<script type="text/javascript" src="',
      '"></script>',
    ),
    'css' => array(
      '@import url("',
      '");',
    ),
    // PHP files do not get added to the DOM directly.
    // @see _libraries_test_load()
    'php' => array(
      '<li>',
      '</li>',
    ),
  );
  foreach ($names as $name => $expected) {
    foreach ($extensions as $extension) {
      $filepath = drupal_get_path('module', 'libraries') . "/tests/libraries/example/{$name}.{$extension}";

      // JavaScript and CSS files appear as full URLs and with an appended
      // query string.
      if (in_array($extension, array(
        'js',
        'css',
      ))) {
        $filepath = url('', array(
          'absolute' => TRUE,
        )) . $filepath . '?' . variable_get('css_js_query_string');
      }
      $raw = $html[$extension][0] . $filepath . $html[$extension][1];
      if ($expected) {
        $this
          ->assertRaw($raw, "{$label}{$name}.{$extension} found.");
      }
      else {
        $this
          ->assertNoRaw($raw, "{$label}{$name}.{$extension} not found.");
      }
    }
  }
}