Newer
Older
Daniel Kudwien
committed
<?php
/**
* @file
* Tests for Libraries API.
Daniel Kudwien
committed
*/
/**
* Tests basic detection and loading of libraries.
*/
Daniel Kudwien
committed
class LibrariesTestCase extends DrupalWebTestCase {
protected $profile = 'testing';
Daniel Kudwien
committed
public static function getInfo() {
return array(
'name' => 'Libraries detection and loading',
Daniel Kudwien
committed
'description' => 'Tests detection and loading of libraries.',
'group' => 'Libraries API',
);
}
function setUp() {
Daniel Kudwien
committed
parent::setUp('libraries', 'libraries_test');
}
/**
* Tests libraries detection and loading.
*
* @todo Better method name(s); split into detection/loading/overloading/etc.
*/
function testLibraries() {
Daniel Kudwien
committed
// Test libraries_get_path().
$this->assertEqual(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.');
Tobias Stoeckler
committed
// Test that library information is found correctly.
Tobias Zimmermann
committed
$expected = array(
Tobias Stoeckler
committed
'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
Tobias Stoeckler
committed
'files' => array(
'js' => array('example_1.js'),
'css' => array('example_1.css'),
'php' => array('example_1.php'),
),
Tobias Zimmermann
committed
'module' => 'libraries_test',
);
libraries_info_defaults($expected, 'example_files');
$library = libraries_info('example_files');
Tobias Zimmermann
committed
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$this->assertEqual($library, $expected, 'Library information is correctly gathered.');
Tobias Stoeckler
committed
// Test a library specified with an .info file gets detected.
Tobias Zimmermann
committed
$expected = array(
Tobias Stoeckler
committed
'name' => 'Example info file',
'info file' => drupal_get_path('module', 'libraries_test') . '/example/example_info_file.libraries.info',
Tobias Zimmermann
committed
);
libraries_info_defaults($expected, 'example_info_file');
$library = libraries_info('example_info_file');
Tobias Zimmermann
committed
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$this->assertEqual($library, $expected, 'Library specified with an .info file found');
Daniel Kudwien
committed
// Test missing library.
Tobias Zimmermann
committed
$library = libraries_detect('example_missing');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$this->assertEqual($library['error'], 'not found', 'Missing library not found.');
$error_message = t('The %library library could not be found.', array(
Tobias Stoeckler
committed
'%library' => $library['name'],
Daniel Kudwien
committed
));
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.');
Daniel Kudwien
committed
// Test unknown library version.
Tobias Zimmermann
committed
$library = libraries_detect('example_undetected_version');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$this->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.');
$error_message = t('The version of the %library library could not be detected.', array(
Tobias Stoeckler
committed
'%library' => $library['name'],
Daniel Kudwien
committed
));
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.');
Daniel Kudwien
committed
// Test unsupported library version.
Tobias Zimmermann
committed
$library = libraries_detect('example_unsupported_version');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$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.', array(
Daniel Kudwien
committed
'%version' => $library['version'],
Tobias Stoeckler
committed
'%library' => $library['name'],
Daniel Kudwien
committed
));
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.');
Daniel Kudwien
committed
Tobias Stoeckler
committed
// Test supported library version.
Tobias Zimmermann
committed
$library = libraries_detect('example_supported_version');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$this->assertEqual($library['installed'], TRUE, 'Supported library version found.');
Daniel Kudwien
committed
// Test libraries_get_version().
Tobias Zimmermann
committed
$library = libraries_detect('example_default_version_callback');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.');
Daniel Kudwien
committed
Tobias Stoeckler
committed
// Test a multiple-parameter version callback.
Tobias Zimmermann
committed
$library = libraries_detect('example_multiple_parameter_version_callback');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.');
Tobias Stoeckler
committed
// Test a top-level files property.
Tobias Zimmermann
committed
$library = libraries_detect('example_files');
Tobias Stoeckler
committed
$files = array(
'js' => array('example_1.js'),
'css' => array('example_1.css'),
'php' => array('example_1.php'),
Tobias Stoeckler
committed
);
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
Tobias Stoeckler
committed
$this->assertEqual($library['files'], $files, 'Top-level files property works.');
Daniel Kudwien
committed
// Test version-specific library files.
Tobias Zimmermann
committed
$library = libraries_detect('example_versions');
Daniel Kudwien
committed
$files = array(
'js' => array('example_2.js'),
'css' => array('example_2.css'),
'php' => array('example_2.php'),
Daniel Kudwien
committed
);
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
Daniel Kudwien
committed
$this->assertEqual($library['files'], $files, 'Version-specific library files found.');
Tobias Stoeckler
committed
// Test missing variant.
Tobias Zimmermann
committed
$library = libraries_detect('example_variant_missing');
Tobias Zimmermann
committed
$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.', array(
Tobias Zimmermann
committed
'%variant' => 'example_variant',
'%library' => 'Example variant missing',
Tobias Stoeckler
committed
));
$this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.');
Tobias Stoeckler
committed
// Test existing variant.
Tobias Zimmermann
committed
$library = libraries_detect('example_variant');
Tobias Zimmermann
committed
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.');
Tobias Stoeckler
committed
Daniel Kudwien
committed
// Test the applying of callbacks.
Tobias Zimmermann
committed
$expected = array(
Daniel Kudwien
committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'name' => 'Example callback',
'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
'version' => '1',
'versions' => array(
'1' => array(
'variants' => array(
'example_variant' => array(
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'variants' => array(
'example_variant' => array(
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'callbacks' => array(
'prepare' => array('_libraries_test_prepare_callback'),
'detect' => array('_libraries_test_detect_callback'),
'load' => array('_libraries_test_load_callback'),
),
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'load callback' => 'not applied',
Tobias Zimmermann
committed
'module' => 'libraries_test',
);
libraries_info_defaults($expected, 'example_callback');
Daniel Kudwien
committed
// Test a callback in the 'prepare' phase.
$expected['prepare callback'] = 'applied (top-level)';
$expected['versions']['1']['prepare callback'] = 'applied (version 1)';
$expected['versions']['1']['variants']['example_variant']['prepare callback'] = 'applied (version 1, variant example_variant)';
$expected['variants']['example_variant']['prepare callback'] = 'applied (variant example_variant)';
$library = libraries_info('example_callback');
Tobias Zimmermann
committed
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
Daniel Kudwien
committed
$this->assertEqual($library, $expected, 'Prepare callback was applied correctly.');
Tobias Zimmermann
committed
Daniel Kudwien
committed
// Test a callback in the 'detect' phase.
Tobias Zimmermann
committed
// Successfully detected libraries should only contain version information
// for the detected version and thus, be marked as installed.
Daniel Kudwien
committed
unset($expected['versions']);
$expected['installed'] = TRUE;
Tobias Zimmermann
committed
// Additionally, version-specific properties of the detected version are
// supposed to override the corresponding top-level properties.
Daniel Kudwien
committed
$expected['prepare callback'] = 'applied (version 1)';
$expected['variants']['example_variant']['installed'] = TRUE;
$expected['variants']['example_variant']['prepare callback'] = 'applied (version 1, variant example_variant)';
$expected['detect callback'] = 'applied (top-level)';
$expected['variants']['example_variant']['detect callback'] = 'applied (variant example_variant)';
$library = libraries_detect('example_callback');
Tobias Zimmermann
committed
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
Daniel Kudwien
committed
$this->assertEqual($library, $expected, 'Detect callback was applied correctly.');
// Test a callback in the 'load' phase.
Tobias Zimmermann
committed
// Successfully loaded libraries should only contain information about the
// already loaded variant.
Daniel Kudwien
committed
unset($expected['variants']);
$expected['loaded'] = 0;
$expected['load callback'] = 'applied (top-level)';
$library = libraries_load('example_callback');
Tobias Zimmermann
committed
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
Daniel Kudwien
committed
$this->assertEqual($library, $expected, 'Load callback was applied correctly.');
// This is not recommended usually and is only used for testing purposes.
drupal_static_reset('libraries_load');
Tobias Zimmermann
committed
// Successfully loaded library variants are supposed to contain the specific
// variant information only.
Daniel Kudwien
committed
$expected['prepare callback'] = 'applied (version 1, variant example_variant)';
$expected['detect callback'] = 'applied (variant example_variant)';
$library = libraries_load('example_callback', 'example_variant');
Tobias Zimmermann
committed
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
Daniel Kudwien
committed
$this->assertEqual($library, $expected, 'Load callback was applied correctly to a variant.');
Tobias Stoeckler
committed
// Test loading of a simple library with a top-level files property.
$this->drupalGet('libraries_test/files');
$this->assertLibraryFiles('example_1', 'File loading');
Daniel Kudwien
committed
Tobias Stoeckler
committed
// Test loading of integration files.
$this->drupalGet('libraries_test/integration_files');
Tobias Stoeckler
committed
$this->assertRaw('libraries_test.js', 'Integration file loading: libraries_test.js found');
$this->assertRaw('libraries_test.css', 'Integration file loading: libraries_test.css found');
$this->assertRaw('libraries_test.inc', 'Integration file loading: libraries_test.inc found');
Daniel Kudwien
committed
Tobias Stoeckler
committed
// Test version overloading.
$this->drupalGet('libraries_test/versions');
Tobias Stoeckler
committed
$this->assertLibraryFiles('example_2', 'Version overloading');
Tobias Stoeckler
committed
Daniel Kudwien
committed
// Test variant loading.
Tobias Stoeckler
committed
$this->drupalGet('libraries_test/variant');
Tobias Stoeckler
committed
$this->assertLibraryFiles('example_3', 'Variant loading');
Tobias Stoeckler
committed
// Test version overloading and variant loading.
$this->drupalGet('libraries_test/versions_and_variants');
Tobias Stoeckler
committed
$this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading');
Daniel Kudwien
committed
/**
* 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.
*
* @param $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.
Tobias Stoeckler
committed
* @param $label
* (optional) A label to prepend to the assertion messages, to make them
* less ambiguous.
* @param $extensions
* (optional) The expected file extensions of $name. Defaults to
* array('js', 'css', 'php').
*/
Tobias Stoeckler
committed
function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) {
$names = drupal_map_assoc(array('example_1', 'example_2', 'example_3', 'example_4'));
unset($names[$name]);
// Test that the wrong files are not loaded.
foreach ($names as $filename) {
Tobias Stoeckler
committed
foreach ($extensions as $extension) {
$message = "$filename.$extension not found";
$message = ($label !== '' ? "$label: $message" : $message);
$this->assertNoRaw("$filename.$extension", $message);
}
}
// Test that the correct files are loaded.
Tobias Stoeckler
committed
foreach ($extensions as $extension) {
Tobias Stoeckler
committed
$message = "$name.$extension found";
Tobias Stoeckler
committed
$message = ($label !== '' ? "$label: $message" : $message);
$this->assertRaw("$name.$extension", $message);
}
Daniel Kudwien
committed
}