You are here

function LibrariesTestCase::testCallbacks in Libraries API 7.3

Tests the applying of callbacks.

File

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

Class

LibrariesTestCase
Tests basic detection and loading of libraries.

Code

function testCallbacks() {
  $expected = array(
    'name' => 'Example callback',
    'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
    'version' => '1',
    'versions' => array(
      '1' => array(
        'variants' => array(
          'example_variant' => array(
            'info callback' => 'not applied',
            'pre-detect callback' => 'not applied',
            'post-detect callback' => 'not applied',
            'pre-dependencies-load callback' => 'not applied',
            'pre-load callback' => 'not applied',
            'post-load callback' => 'not applied',
          ),
        ),
        'info callback' => 'not applied',
        'pre-detect callback' => 'not applied',
        'post-detect callback' => 'not applied',
        'pre-dependencies-load callback' => 'not applied',
        'pre-load callback' => 'not applied',
        'post-load callback' => 'not applied',
      ),
    ),
    'variants' => array(
      'example_variant' => array(
        'info callback' => 'not applied',
        'pre-detect callback' => 'not applied',
        'post-detect callback' => 'not applied',
        'pre-dependencies-load callback' => 'not applied',
        'pre-load callback' => 'not applied',
        'post-load callback' => 'not applied',
      ),
    ),
    'callbacks' => array(
      'info' => array(
        '_libraries_test_module_info_callback',
      ),
      'pre-detect' => array(
        '_libraries_test_module_pre_detect_callback',
      ),
      'post-detect' => array(
        '_libraries_test_module_post_detect_callback',
      ),
      'pre-dependencies-load' => array(
        '_libraries_test_module_pre_dependencies_load_callback',
      ),
      'pre-load' => array(
        '_libraries_test_module_pre_load_callback',
      ),
      'post-load' => array(
        '_libraries_test_module_post_load_callback',
      ),
    ),
    'info callback' => 'not applied',
    'pre-detect callback' => 'not applied',
    'post-detect callback' => 'not applied',
    'pre-dependencies-load callback' => 'not applied',
    'pre-load callback' => 'not applied',
    'post-load callback' => 'not applied',
    'info type' => 'module',
    'module' => 'libraries_test_module',
  );
  libraries_info_defaults($expected, 'example_callback');

  // Test a callback in the 'info' group.
  $expected['info callback'] = 'applied (top-level)';
  $expected['versions']['1']['info callback'] = 'applied (version 1)';
  $expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
  $expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)';
  $library = libraries_info('example_callback');
  $this
    ->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  $this
    ->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library, $expected, 'Prepare callback was applied correctly.');

  // Test a callback in the 'pre-detect' and 'post-detect' phases.
  // Successfully detected libraries should only contain version information
  // for the detected version and thus, be marked as installed.
  unset($expected['versions']);
  $expected['installed'] = TRUE;

  // Additionally, version-specific properties of the detected version are
  // supposed to override the corresponding top-level properties.
  $expected['info callback'] = 'applied (version 1)';
  $expected['variants']['example_variant']['installed'] = TRUE;
  $expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';

  // Version-overloading takes place after the 'pre-detect' callbacks have
  // been applied.
  $expected['pre-detect callback'] = 'applied (version 1)';
  $expected['post-detect callback'] = 'applied (top-level)';
  $expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)';
  $expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)';
  $library = libraries_detect('example_callback');
  $this
    ->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  $this
    ->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library, $expected, 'Detect callback was applied correctly.');

  // Test a callback in the 'pre-dependencies-load', 'pre-load' and
  // 'post-load' phases.
  // Successfully loaded libraries should only contain information about the
  // already loaded variant.
  unset($expected['variants']);
  $expected['loaded'] = 0;
  $expected['pre-dependencies-load callback'] = 'applied (top-level)';
  $expected['pre-load callback'] = 'applied (top-level)';
  $expected['post-load callback'] = 'applied (top-level)';
  $library = libraries_load('example_callback');
  $this
    ->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  $this
    ->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.');

  // This is not recommended usually and is only used for testing purposes.
  drupal_static_reset('libraries_load');

  // Successfully loaded library variants are supposed to contain the specific
  // variant information only.
  $expected['info callback'] = 'applied (version 1, variant example_variant)';
  $expected['pre-detect callback'] = 'applied (version 1, variant example_variant)';
  $expected['post-detect callback'] = 'applied (variant example_variant)';
  $library = libraries_load('example_callback', 'example_variant');
  $this
    ->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  $this
    ->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  $this
    ->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.');
}