You are here

public function LibrariesWebTest::testCallbacks in Libraries API 8.3

Tests the applying of callbacks.

File

src/Tests/LibrariesWebTest.php, line 298

Class

LibrariesWebTest
Tests basic detection and loading of libraries.

Namespace

Drupal\libraries\Tests

Code

public function testCallbacks() {
  $expected = [
    'name' => 'Example callback',
    'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
    'version' => '1',
    'versions' => [
      '1' => [
        'variants' => [
          'example_variant' => [
            'info callback' => 'not applied',
            'pre-detect callback' => 'not applied',
            'post-detect 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-load callback' => 'not applied',
        'post-load callback' => 'not applied',
      ],
    ],
    'variants' => [
      'example_variant' => [
        'info callback' => 'not applied',
        'pre-detect callback' => 'not applied',
        'post-detect callback' => 'not applied',
        'pre-load callback' => 'not applied',
        'post-load callback' => 'not applied',
      ],
    ],
    'callbacks' => [
      'info' => [
        '_libraries_test_info_callback',
      ],
      'pre-detect' => [
        '_libraries_test_pre_detect_callback',
      ],
      'post-detect' => [
        '_libraries_test_post_detect_callback',
      ],
      'pre-load' => [
        '_libraries_test_pre_load_callback',
      ],
      'post-load' => [
        '_libraries_test_post_load_callback',
      ],
    ],
    'info callback' => 'not applied',
    'pre-detect callback' => 'not applied',
    'post-detect callback' => 'not applied',
    'pre-load callback' => 'not applied',
    'post-load callback' => 'not applied',
    'module' => 'libraries_test',
  ];
  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-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-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.');
}