| Commit | Line | Data |
|---|---|---|
| 41234e95 | 1 | <?php |
| 41234e95 | 2 | |
| 3 | /** | |
| 4 | * @file | |
| 250552d9 | 5 | * Tests for Libraries API. |
| 41234e95 | 6 | */ |
| 7 | ||
| 250552d9 | 8 | /** |
| 9 | * Tests basic detection and loading of libraries. | |
| 10 | */ | |
| a27f5567 | 11 | class LibrariesTestCase extends DrupalWebTestCase { |
| 250552d9 | 12 | protected $profile = 'testing'; |
| a27f5567 | 13 | |
| 14 | public static function getInfo() { | |
| 15 | return array( | |
| 250552d9 | 16 | 'name' => 'Libraries detection and loading', |
| a27f5567 | 17 | 'description' => 'Tests detection and loading of libraries.', |
| 18 | 'group' => 'Libraries API', | |
| 19 | ); | |
| 20 | } | |
| 21 | ||
| 250552d9 | 22 | function setUp() { |
| a27f5567 | 23 | parent::setUp('libraries', 'libraries_test'); |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 27 | * Tests libraries detection and loading. | |
| 28 | * | |
| 29 | * @todo Better method name(s); split into detection/loading/overloading/etc. | |
| 30 | */ | |
| 250552d9 | 31 | function testLibraries() { |
| 9022ed7d | 32 | // Test libraries_get_path(). |
| 33 | $this->assertEqual(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.'); | |
| 34 | ||
| d8145a2d | 35 | // Test that library information is found correctly. |
| e6f845f4 | 36 | $expected = array( |
| dbb7c2d4 | 37 | 'name' => 'Example files', |
| d8145a2d | 38 | 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', |
| dbb7c2d4 | 39 | 'version' => '1', |
| d8145a2d TS |
40 | 'files' => array( |
| 41 | 'js' => array('example_1.js'), | |
| 42 | 'css' => array('example_1.css'), | |
| 43 | 'php' => array('example_1.php'), | |
| 44 | ), | |
| e6f845f4 TS |
45 | 'module' => 'libraries_test', |
| 46 | ); | |
| 47 | libraries_info_defaults($expected, 'example_files'); | |
| dbb7c2d4 | 48 | $library = libraries_info('example_files'); |
| e6f845f4 TS |
49 | $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); |
| 50 | $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); | |
| d8145a2d TS |
51 | $this->assertEqual($library, $expected, 'Library information is correctly gathered.'); |
| 52 | ||
| c365e82b | 53 | // Test a library specified with an .info file gets detected. |
| e6f845f4 | 54 | $expected = array( |
| d8145a2d TS |
55 | 'name' => 'Example info file', |
| 56 | 'info file' => drupal_get_path('module', 'libraries_test') . '/example/example_info_file.libraries.info', | |
| e6f845f4 TS |
57 | ); |
| 58 | libraries_info_defaults($expected, 'example_info_file'); | |
| dbb7c2d4 | 59 | $library = libraries_info('example_info_file'); |
| e6f845f4 TS |
60 | $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); |
| 61 | $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); | |
| c365e82b TS |
62 | $this->assertEqual($library, $expected, 'Library specified with an .info file found'); |
| 63 | ||
| a27f5567 | 64 | // Test missing library. |
| f08245e1 | 65 | $library = libraries_detect('example_missing'); |
| e6f845f4 | 66 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| d8145a2d | 67 | $this->assertEqual($library['error'], 'not found', 'Missing library not found.'); |
| dbb7c2d4 | 68 | $error_message = t('The %library library could not be found.', array( |
| d8145a2d | 69 | '%library' => $library['name'], |
| a27f5567 | 70 | )); |
| 88e17104 | 71 | $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.'); |
| a27f5567 | 72 | |
| 73 | // Test unknown library version. | |
| f08245e1 | 74 | $library = libraries_detect('example_undetected_version'); |
| e6f845f4 | 75 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| d8145a2d | 76 | $this->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.'); |
| dbb7c2d4 | 77 | $error_message = t('The version of the %library library could not be detected.', array( |
| d8145a2d | 78 | '%library' => $library['name'], |
| a27f5567 | 79 | )); |
| 88e17104 | 80 | $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.'); |
| a27f5567 | 81 | |
| 82 | // Test unsupported library version. | |
| f08245e1 | 83 | $library = libraries_detect('example_unsupported_version'); |
| e6f845f4 | 84 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| d8145a2d | 85 | $this->assertEqual($library['error'], 'not supported', 'Unsupported version detected as such.'); |
| dbb7c2d4 | 86 | $error_message = t('The installed version %version of the %library library is not supported.', array( |
| a27f5567 | 87 | '%version' => $library['version'], |
| d8145a2d | 88 | '%library' => $library['name'], |
| a27f5567 | 89 | )); |
| 88e17104 | 90 | $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.'); |
| a27f5567 | 91 | |
| a483bb17 | 92 | // Test supported library version. |
| f08245e1 | 93 | $library = libraries_detect('example_supported_version'); |
| e6f845f4 | 94 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| a483bb17 TS |
95 | $this->assertEqual($library['installed'], TRUE, 'Supported library version found.'); |
| 96 | ||
| a27f5567 | 97 | // Test libraries_get_version(). |
| f08245e1 | 98 | $library = libraries_detect('example_default_version_callback'); |
| e6f845f4 | 99 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| dbb7c2d4 | 100 | $this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.'); |
| a27f5567 | 101 | |
| a483bb17 | 102 | // Test a multiple-parameter version callback. |
| f08245e1 | 103 | $library = libraries_detect('example_multiple_parameter_version_callback'); |
| e6f845f4 | 104 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| dbb7c2d4 | 105 | $this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.'); |
| a483bb17 TS |
106 | |
| 107 | // Test a top-level files property. | |
| f08245e1 | 108 | $library = libraries_detect('example_files'); |
| a483bb17 | 109 | $files = array( |
| d033c260 TS |
110 | 'js' => array('example_1.js'), |
| 111 | 'css' => array('example_1.css'), | |
| 112 | 'php' => array('example_1.php'), | |
| a483bb17 | 113 | ); |
| e6f845f4 | 114 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| a483bb17 TS |
115 | $this->assertEqual($library['files'], $files, 'Top-level files property works.'); |
| 116 | ||
| a27f5567 | 117 | // Test version-specific library files. |
| f08245e1 | 118 | $library = libraries_detect('example_versions'); |
| a27f5567 | 119 | $files = array( |
| d033c260 TS |
120 | 'js' => array('example_2.js'), |
| 121 | 'css' => array('example_2.css'), | |
| 122 | 'php' => array('example_2.php'), | |
| a27f5567 | 123 | ); |
| e6f845f4 | 124 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| a27f5567 | 125 | $this->assertEqual($library['files'], $files, 'Version-specific library files found.'); |
| a27f5567 | 126 | |
| a483bb17 | 127 | // Test missing variant. |
| f08245e1 | 128 | $library = libraries_detect('example_variant_missing'); |
| e6f845f4 | 129 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| 88e17104 | 130 | $this->assertEqual($library['variants']['example_variant']['error'], 'not found', 'Missing variant not found'); |
| dbb7c2d4 | 131 | $error_message = t('The %variant variant of the %library library could not be found.', array( |
| e6f845f4 TS |
132 | '%variant' => 'example_variant', |
| 133 | '%library' => 'Example variant missing', | |
| a483bb17 | 134 | )); |
| 88e17104 | 135 | $this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.'); |
| a483bb17 TS |
136 | |
| 137 | // Test existing variant. | |
| f08245e1 | 138 | $library = libraries_detect('example_variant'); |
| e6f845f4 | 139 | $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>'); |
| d033c260 | 140 | $this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.'); |
| a483bb17 | 141 | |
| 926327cc | 142 | // Test the applying of callbacks. |
| e6f845f4 | 143 | $expected = array( |
| 926327cc | 144 | 'name' => 'Example callback', |
| 145 | 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', | |
| 146 | 'version' => '1', | |
| 147 | 'versions' => array( | |
| 148 | '1' => array( | |
| 149 | 'variants' => array( | |
| 150 | 'example_variant' => array( | |
| f455d71f TS |
151 | 'info callback' => 'not applied', |
| 152 | 'pre-detect callback' => 'not applied', | |
| 153 | 'post-detect callback' => 'not applied', | |
| 926327cc | 154 | 'load callback' => 'not applied', |
| 155 | ), | |
| 156 | ), | |
| f455d71f TS |
157 | 'info callback' => 'not applied', |
| 158 | 'pre-detect callback' => 'not applied', | |
| 159 | 'post-detect callback' => 'not applied', | |
| 926327cc | 160 | 'load callback' => 'not applied', |
| 161 | ), | |
| 162 | ), | |
| 163 | 'variants' => array( | |
| 164 | 'example_variant' => array( | |
| f455d71f TS |
165 | 'info callback' => 'not applied', |
| 166 | 'pre-detect callback' => 'not applied', | |
| 167 | 'post-detect callback' => 'not applied', | |
| 926327cc | 168 | 'load callback' => 'not applied', |
| 169 | ), | |
| 170 | ), | |
| 171 | 'callbacks' => array( | |
| f455d71f TS |
172 | 'info' => array('_libraries_test_info_callback'), |
| 173 | 'pre-detect' => array('_libraries_test_pre_detect_callback'), | |
| 174 | 'post-detect' => array('_libraries_test_post_detect_callback'), | |
| 926327cc | 175 | 'load' => array('_libraries_test_load_callback'), |
| 176 | ), | |
| f455d71f TS |
177 | 'info callback' => 'not applied', |
| 178 | 'pre-detect callback' => 'not applied', | |
| 179 | 'post-detect callback' => 'not applied', | |
| 926327cc | 180 | 'load callback' => 'not applied', |
| e6f845f4 TS |
181 | 'module' => 'libraries_test', |
| 182 | ); | |
| 183 | libraries_info_defaults($expected, 'example_callback'); | |
| 184 | ||
| f455d71f TS |
185 | // Test a callback in the 'info' group. |
| 186 | $expected['info callback'] = 'applied (top-level)'; | |
| 187 | $expected['versions']['1']['info callback'] = 'applied (version 1)'; | |
| 188 | $expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)'; | |
| 189 | $expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)'; | |
| 926327cc | 190 | $library = libraries_info('example_callback'); |
| e6f845f4 TS |
191 | $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); |
| 192 | $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); | |
| 926327cc | 193 | $this->assertEqual($library, $expected, 'Prepare callback was applied correctly.'); |
| e6f845f4 | 194 | |
| f455d71f | 195 | // Test a callback in the 'pre-detect' and 'post-detect' phase. |
| e6f845f4 TS |
196 | // Successfully detected libraries should only contain version information |
| 197 | // for the detected version and thus, be marked as installed. | |
| 926327cc | 198 | unset($expected['versions']); |
| 199 | $expected['installed'] = TRUE; | |
| e6f845f4 TS |
200 | // Additionally, version-specific properties of the detected version are |
| 201 | // supposed to override the corresponding top-level properties. | |
| f455d71f | 202 | $expected['info callback'] = 'applied (version 1)'; |
| 926327cc | 203 | $expected['variants']['example_variant']['installed'] = TRUE; |
| f455d71f TS |
204 | $expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)'; |
| 205 | // Version-overloading takes place after the 'pre-detect' callbacks have | |
| 206 | // been applied. | |
| 207 | $expected['pre-detect callback'] = 'applied (version 1)'; | |
| 208 | $expected['post-detect callback'] = 'applied (top-level)'; | |
| 209 | $expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)'; | |
| 210 | $expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)'; | |
| 926327cc | 211 | $library = libraries_detect('example_callback'); |
| e6f845f4 TS |
212 | $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); |
| 213 | $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); | |
| 926327cc | 214 | $this->assertEqual($library, $expected, 'Detect callback was applied correctly.'); |
| 215 | ||
| 216 | // Test a callback in the 'load' phase. | |
| e6f845f4 TS |
217 | // Successfully loaded libraries should only contain information about the |
| 218 | // already loaded variant. | |
| 926327cc | 219 | unset($expected['variants']); |
| 220 | $expected['loaded'] = 0; | |
| 221 | $expected['load callback'] = 'applied (top-level)'; | |
| 222 | $library = libraries_load('example_callback'); | |
| e6f845f4 TS |
223 | $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); |
| 224 | $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); | |
| 926327cc | 225 | $this->assertEqual($library, $expected, 'Load callback was applied correctly.'); |
| 226 | // This is not recommended usually and is only used for testing purposes. | |
| 227 | drupal_static_reset('libraries_load'); | |
| e6f845f4 TS |
228 | // Successfully loaded library variants are supposed to contain the specific |
| 229 | // variant information only. | |
| f455d71f TS |
230 | $expected['info callback'] = 'applied (version 1, variant example_variant)'; |
| 231 | $expected['pre-detect callback'] = 'applied (version 1, variant example_variant)'; | |
| 232 | $expected['post-detect callback'] = 'applied (variant example_variant)'; | |
| 926327cc | 233 | $library = libraries_load('example_callback', 'example_variant'); |
| e6f845f4 TS |
234 | $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>'); |
| 235 | $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>'); | |
| 926327cc | 236 | $this->assertEqual($library, $expected, 'Load callback was applied correctly to a variant.'); |
| 237 | ||
| a483bb17 | 238 | // Test loading of a simple library with a top-level files property. |
| dbb7c2d4 | 239 | $this->drupalGet('libraries_test/files'); |
| 240 | $this->assertLibraryFiles('example_1', 'File loading'); | |
| a27f5567 | 241 | |
| a483bb17 TS |
242 | // Test loading of integration files. |
| 243 | $this->drupalGet('libraries_test/integration_files'); | |
| d8145a2d TS |
244 | $this->assertRaw('libraries_test.js', 'Integration file loading: libraries_test.js found'); |
| 245 | $this->assertRaw('libraries_test.css', 'Integration file loading: libraries_test.css found'); | |
| 246 | $this->assertRaw('libraries_test.inc', 'Integration file loading: libraries_test.inc found'); | |
| a27f5567 | 247 | |
| a483bb17 TS |
248 | // Test version overloading. |
| 249 | $this->drupalGet('libraries_test/versions'); | |
| d8145a2d | 250 | $this->assertLibraryFiles('example_2', 'Version overloading'); |
| a483bb17 | 251 | |
| a27f5567 | 252 | // Test variant loading. |
| a483bb17 | 253 | $this->drupalGet('libraries_test/variant'); |
| d8145a2d | 254 | $this->assertLibraryFiles('example_3', 'Variant loading'); |
| a483bb17 TS |
255 | |
| 256 | // Test version overloading and variant loading. | |
| 257 | $this->drupalGet('libraries_test/versions_and_variants'); | |
| d8145a2d | 258 | $this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading'); |
| d033c260 | 259 | } |
| a27f5567 | 260 | |
| d033c260 TS |
261 | /** |
| 262 | * Helper function to assert that a library was correctly loaded. | |
| 263 | * | |
| 264 | * Asserts that all the correct files were loaded and all the incorrect ones | |
| 265 | * were not. | |
| 266 | * | |
| 267 | * @param $name | |
| 268 | * The name of the files that should be loaded. The current testing system | |
| 269 | * knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name | |
| 270 | * has an associated JavaScript, CSS and PHP file that will be asserted. All | |
| 271 | * other files will be asserted to not be loaded. See | |
| 272 | * tests/example/README.txt for more information on how the loading of the | |
| 273 | * files is tested. | |
| d8145a2d TS |
274 | * @param $label |
| 275 | * (optional) A label to prepend to the assertion messages, to make them | |
| 276 | * less ambiguous. | |
| 277 | * @param $extensions | |
| 278 | * (optional) The expected file extensions of $name. Defaults to | |
| 279 | * array('js', 'css', 'php'). | |
| d033c260 | 280 | */ |
| d8145a2d | 281 | function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) { |
| d033c260 TS |
282 | $names = drupal_map_assoc(array('example_1', 'example_2', 'example_3', 'example_4')); |
| 283 | unset($names[$name]); | |
| 284 | ||
| 285 | // Test that the wrong files are not loaded. | |
| 286 | foreach ($names as $filename) { | |
| d8145a2d TS |
287 | foreach ($extensions as $extension) { |
| 288 | $message = "$filename.$extension not found"; | |
| 289 | $message = ($label !== '' ? "$label: $message" : $message); | |
| 290 | $this->assertNoRaw("$filename.$extension", $message); | |
| 291 | } | |
| d033c260 TS |
292 | } |
| 293 | ||
| 294 | // Test that the correct files are loaded. | |
| d8145a2d | 295 | foreach ($extensions as $extension) { |
| e3ff27ca | 296 | $message = "$name.$extension found"; |
| d8145a2d TS |
297 | $message = ($label !== '' ? "$label: $message" : $message); |
| 298 | $this->assertRaw("$name.$extension", $message); | |
| 299 | } | |
| a27f5567 | 300 | } |
| d033c260 | 301 | |
| a27f5567 | 302 | } |
| 303 |