'Libraries detection and loading', 'description' => 'Tests detection and loading of libraries.', 'group' => 'Libraries API', ); } function setUp() { parent::setUp('libraries', 'libraries_test'); } /** * Tests libraries detection and loading. * * @todo Better method name(s); split into detection/loading/overloading/etc. */ function testLibraries() { // Test that library information is found correctly. $expected = array_merge(libraries_info('example_empty'), array( 'machine name' => 'example_files', 'name' => 'Example files', 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', 'version' => '1', 'files' => array( 'js' => array('example_1.js'), 'css' => array('example_1.css'), 'php' => array('example_1.php'), ), )); $library = libraries_info('example_files'); $this->verbose(var_export($expected, TRUE)); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library, $expected, 'Library information is correctly gathered.'); // Test a library specified with an .info file gets detected. $expected = array_merge(libraries_info('example_empty'), array( 'machine name' => 'example_info_file', 'name' => 'Example info file', 'info file' => drupal_get_path('module', 'libraries_test') . '/example/example_info_file.libraries.info', )); unset($expected['module']); $library = libraries_info('example_info_file'); $this->verbose(var_export($expected, TRUE)); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library, $expected, 'Library specified with an .info file found'); // Test missing library. $library = libraries_info('example_missing'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['error'], 'not found', 'Missing library not found.'); $error_message = t('The %library library could not be found.', array( '%library' => $library['name'], )); $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.'); // Test unknown library version. $library = libraries_info('example_undetected_version'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $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( '%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_info('example_unsupported_version'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $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( '%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_info('example_supported_version'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['installed'], TRUE, 'Supported library version found.'); // Test libraries_get_version(). $library = libraries_info('example_default_version_callback'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.'); // Test a multiple-parameter version callback. $library = libraries_info('example_multiple_parameter_version_callback'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.'); // Test a top-level files property. $library = libraries_info('example_files'); libraries_detect_library($library); $files = array( 'js' => array('example_1.js'), 'css' => array('example_1.css'), 'php' => array('example_1.php'), ); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['files'], $files, 'Top-level files property works.'); // Test version-specific library files. $library = libraries_info('example_versions'); libraries_detect_library($library); $files = array( 'js' => array('example_2.js'), 'css' => array('example_2.css'), 'php' => array('example_2.php'), ); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['files'], $files, 'Version-specific library files found.'); // Test missing variant. $library = libraries_info('example_variant_missing'); libraries_detect_library($library); $variants = array_keys($library['variants']); $this->verbose(var_export($library, TRUE)); $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( '%variant' => $variants[0], '%library' => $library['name'], )); $this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.'); // Test existing variant. $library = libraries_info('example_variant'); libraries_detect_library($library); $this->verbose(var_export($library, TRUE)); $this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.'); // Test loading of a simple library with a top-level files property. $this->drupalGet('libraries_test/files'); $this->assertLibraryFiles('example_1', 'File loading'); // Test loading of integration files. $this->drupalGet('libraries_test/integration_files'); $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'); // Test version overloading. $this->drupalGet('libraries_test/versions'); $this->assertLibraryFiles('example_2', 'Version overloading'); // Test variant loading. $this->drupalGet('libraries_test/variant'); $this->assertLibraryFiles('example_3', 'Variant loading'); // Test version overloading and variant loading. $this->drupalGet('libraries_test/versions_and_variants'); $this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading'); } /** * 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. * @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'). */ 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) { 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. foreach ($extensions as $extension) { $message = "$name.$extension found"; $message = ($label !== '' ? "$label: $message" : $message); $this->assertRaw("$name.$extension", $message); } } }