You are here

public function LibrariesWebTest::testLibrariesDetect in Libraries API 8.3

Tests libraries_detect().

File

src/Tests/LibrariesWebTest.php, line 194

Class

LibrariesWebTest
Tests basic detection and loading of libraries.

Namespace

Drupal\libraries\Tests

Code

public function testLibrariesDetect() {

  // Test missing library.
  $library = libraries_detect('example_missing');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['error'], 'not found', 'Missing library not found.');
  $error_message = t('The %library library could not be found.', [
    '%library' => $library['name'],
  ]);
  $this
    ->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.');

  // Test unknown library version.
  $library = libraries_detect('example_undetected_version');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.');
  $error_message = t('The version of the %library library could not be detected.', [
    '%library' => $library['name'],
  ]);
  $this
    ->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.');

  // Test unsupported library version.
  $library = libraries_detect('example_unsupported_version');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['error'], 'not supported', 'Unsupported version detected as such.');
  $error_message = t('The installed version %version of the %library library is not supported.', [
    '%version' => $library['version'],
    '%library' => $library['name'],
  ]);
  $this
    ->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.');

  // Test supported library version.
  $library = libraries_detect('example_supported_version');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['installed'], TRUE, 'Supported library version found.');

  // Test libraries_get_version().
  $library = libraries_detect('example_default_version_callback');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['version'], '1', 'Expected version returned by default version callback.');

  // Test a multiple-parameter version callback.
  $library = libraries_detect('example_multiple_parameter_version_callback');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.');

  // Test a top-level files property.
  $library = libraries_detect('example_files');
  $files = [
    'js' => [
      'example_1.js' => [],
    ],
    'css' => [
      'example_1.css' => [],
    ],
    'php' => [
      'example_1.php' => [],
    ],
  ];
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['files'], $files, 'Top-level files property works.');

  // Test version-specific library files.
  $library = libraries_detect('example_versions');
  $files = [
    'js' => [
      'example_2.js' => [],
    ],
    'css' => [
      'example_2.css' => [],
    ],
    'php' => [
      'example_2.php' => [],
    ],
  ];
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['files'], $files, 'Version-specific library files found.');

  // Test missing variant.
  $library = libraries_detect('example_variant_missing');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['variants']['example_variant']['error'], 'not found', 'Missing variant not found');
  $error_message = t('The %variant variant of the %library library could not be found.', [
    '%variant' => 'example_variant',
    '%library' => 'Example variant missing',
  ]);
  $this
    ->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.');

  // Test existing variant.
  $library = libraries_detect('example_variant');
  $this
    ->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.');
}