diff --git a/tests/libraries.test b/tests/libraries.test index 28fb0fb..51198eb 100644 --- a/tests/libraries.test +++ b/tests/libraries.test @@ -6,33 +6,38 @@ */ /** - * Tests basic detection and loading of libraries. + * Tests basic Libraries API functions. */ -class LibrariesTestCase extends DrupalWebTestCase { - protected $profile = 'testing'; - +class LibrariesUnitTestCase extends DrupalUnitTestCase { public static function getInfo() { return array( - 'name' => 'Libraries detection and loading', - 'description' => 'Tests detection and loading of libraries.', + 'name' => 'Libraries API unit tests', + 'description' => 'Tests basic functions provided by Libraries API.', 'group' => 'Libraries API', ); } function setUp() { - parent::setUp('libraries', 'libraries_test'); + drupal_load('module', 'libraries'); + parent::setUp(); } /** - * Tests libraries detection and loading. - * - * @todo Better method name(s); split into detection/loading/overloading/etc. + * Tests libraries_get_path(). */ - function testLibraries() { - // Test libraries_get_path(). + function testLibrariesGetPath() { + // Note that, even though libraries_get_path() doesn't find the 'example' + // library, we are able to make it 'installed' by specifying the 'library + // path' up-front. This is only used for testing purposed and is strongly + // discouraged as it defeats the purpose of Libraries API in the first + // place. $this->assertEqual(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.'); + } - // Test libraries_prepare_files(). + /** + * Tests libraries_prepare_files(). + */ + function testLibrariesPrepareFiles() { $expected = array( 'files' => array( 'js' => array('example.js' => array()), @@ -49,8 +54,31 @@ class LibrariesTestCase extends DrupalWebTestCase { ); libraries_prepare_files($library, NULL, NULL); $this->assertEqual($expected, $library, 'libraries_prepare_files() works correctly.'); + } +} + +/** + * Tests basic detection and loading of libraries. + */ +class LibrariesTestCase extends DrupalWebTestCase { + protected $profile = 'testing'; + + public static function getInfo() { + return array( + 'name' => 'Libraries detection and loading', + 'description' => 'Tests detection and loading of libraries.', + 'group' => 'Libraries API', + ); + } - // Test libraries_detect_dependencies(). + function setUp() { + parent::setUp('libraries', 'libraries_test'); + } + + /** + * Tests libraries_detect_dependencies(). + */ + function testLibrariesDetectDependencies() { $library = array( 'name' => 'Example', 'dependencies' => array('example_missing'), @@ -64,6 +92,7 @@ class LibrariesTestCase extends DrupalWebTestCase { $this->verbose("Expected:
$error_message"); $this->verbose('Actual:
' . $library['error message']); $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing dependency'); + // Test versioned dependencies. $version = '1.1'; $compatible = array( '1.1', @@ -121,7 +150,25 @@ class LibrariesTestCase extends DrupalWebTestCase { $this->verbose("Expected:
$error_message"); $this->verbose('Actual:
' . $library['error message']); $this->assertEqual($library['error message'], $error_message, 'Correct error message for an incompatible dependency'); + } + + /** + * Tests libraries_scan_info_files(). + */ + function testLibrariesScanInfoFiles() { + $expected = array('example_info_file' => (object) array( + 'uri' => drupal_get_path('module', 'libraries') . '/tests/example/example_info_file.libraries.info', + 'filename' => 'example_info_file.libraries.info', + 'name' => 'example_info_file.libraries', + )); + $this->assertEqual(libraries_scan_info_files(), $expected, 'libraries_scan_info_files() correctly finds the example info file.'); + $this->verbose('
' . var_export(libraries_scan_info_files(), TRUE) . '
'); + } + /** + * Tests libraries_info(). + */ + function testLibrariesInfo() { // Test that library information is found correctly. $expected = array( 'name' => 'Example files', @@ -150,7 +197,12 @@ class LibrariesTestCase extends DrupalWebTestCase { $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); $this->assertEqual($library, $expected, 'Library specified with an .info file found'); + } + /** + * Tests libraries_detect(). + */ + function testLibrariesDetect() { // Test missing library. $library = libraries_detect('example_missing'); $this->verbose('
' . var_export($library, TRUE) . '
'); @@ -228,7 +280,12 @@ class LibrariesTestCase extends DrupalWebTestCase { $library = libraries_detect('example_variant'); $this->verbose('
' . var_export($library, TRUE) . '
'); $this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.'); + } + /** + * Tests libraries_load(). + */ + function testLibrariesLoad() { // Test dependencies. $library = libraries_load('example_dependency_missing'); $this->verbose('
' . var_export($library, TRUE) . '
'); @@ -242,8 +299,12 @@ class LibrariesTestCase extends DrupalWebTestCase { $loaded = &drupal_static('libraries_load'); $this->verbose('
' . var_export($loaded, TRUE) . '
'); $this->assertEqual($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded'); + } - // Test the applying of callbacks. + /** + * Tests the applying of callbacks. + */ + function testCallbacks() { $expected = array( 'name' => 'Example callback', 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', @@ -344,7 +405,17 @@ class LibrariesTestCase extends DrupalWebTestCase { $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); $this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.'); + } + /** + * Tests that library files are properly added to the page output. + * + * We check for JavaScript and CSS files directly in the DOM and add a list of + * included PHP files manually to the page output. + * + * @see _libraries_test_load() + */ + function testLibrariesOutput() { // Test loading of a simple library with a top-level files property. $this->drupalGet('libraries_test/files'); $this->assertLibraryFiles('example_1', 'File loading');