LibrariesLoadWebTest.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * @file
  4. * Contains LibrariesLoadWebTest.
  5. *
  6. * Simpletest automatically discovers test files using PSR-4. We cannot,
  7. * however, declare a namespace for this class, as that would require PHP 5.3.
  8. * To prepare the PHP 5.3 requirement and the usage of PSR-4 in 7.x-3.x, we
  9. * place the test files in the correct directory already, but for now register
  10. * them explicitly in libraries.info
  11. */
  12. /**
  13. * Tests basic detection and loading of libraries.
  14. */
  15. class LibrariesLoadWebTest extends LibrariesWebTestBase {
  16. /**
  17. * Provides metadata about this test.
  18. *
  19. * @return array
  20. * An array of test metadata with the following keys:
  21. * - name: The name of the test.
  22. * - description: The description of the test.
  23. * - group: The group of the test.
  24. */
  25. public static function getInfo() {
  26. return array(
  27. 'name' => 'Libraries detection and loading',
  28. 'description' => 'Tests detection and loading of libraries.',
  29. 'group' => 'Libraries API',
  30. );
  31. }
  32. /**
  33. * Tests libraries_detect_dependencies().
  34. */
  35. public function testLibrariesDetectDependencies() {
  36. $library = array(
  37. 'name' => 'Example',
  38. 'dependencies' => array('example_missing'),
  39. );
  40. libraries_detect_dependencies($library);
  41. $this->assertEqual($library['error'], 'missing dependency', 'libraries_detect_dependencies() detects missing dependency');
  42. $error_message = t('The %dependency library, which the %library library depends on, is not installed.', array(
  43. '%dependency' => 'Example missing',
  44. '%library' => $library['name'],
  45. ));
  46. $this->verbose("Expected:<br>$error_message");
  47. $this->verbose('Actual:<br>' . $library['error message']);
  48. $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing dependency');
  49. // Test versioned dependencies.
  50. $version = '1.1';
  51. $compatible = array(
  52. '1.1',
  53. '<=1.1',
  54. '>=1.1',
  55. '<1.2',
  56. '<2.0',
  57. '>1.0',
  58. '>1.0-rc1',
  59. '>1.0-beta2',
  60. '>1.0-alpha3',
  61. '>0.1',
  62. '<1.2, >1.0',
  63. '>0.1, <=1.1',
  64. );
  65. $incompatible = array(
  66. '1.2',
  67. '2.0',
  68. '<1.1',
  69. '>1.1',
  70. '<=1.0',
  71. '<=1.0-rc1',
  72. '<=1.0-beta2',
  73. '<=1.0-alpha3',
  74. '>=1.2',
  75. '<1.1, >0.9',
  76. '>=0.1, <1.1',
  77. );
  78. $library = array(
  79. 'name' => 'Example',
  80. );
  81. foreach ($compatible as $version_string) {
  82. $library['dependencies'][0] = "example_dependency ($version_string)";
  83. // libraries_detect_dependencies() is a post-detect callback, so
  84. // 'installed' is already set, when it is called. It sets the value to
  85. // FALSE for missing or incompatible dependencies.
  86. $library['installed'] = TRUE;
  87. libraries_detect_dependencies($library);
  88. $this->verbose('Library:<pre>' . var_export($library, TRUE) . '</pre>');
  89. $this->assertTrue($library['installed'], "libraries_detect_dependencies() detects compatible version string: '$version_string' is compatible with '$version'");
  90. }
  91. foreach ($incompatible as $version_string) {
  92. $library['dependencies'][0] = "example_dependency ($version_string)";
  93. $library['installed'] = TRUE;
  94. unset($library['error'], $library['error message']);
  95. libraries_detect_dependencies($library);
  96. $this->verbose('Library:<pre>' . var_export($library, TRUE) . '</pre>');
  97. $this->assertEqual($library['error'], 'incompatible dependency', "libraries_detect_dependencies() detects incompatible version strings: '$version_string' is incompatible with '$version'");
  98. }
  99. // Instead of repeating this assertion for each version string, we just
  100. // re-use the $library variable from the foreach loop.
  101. $error_message = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', array(
  102. '%dependency_version' => $version,
  103. '%dependency' => 'Example dependency',
  104. '%library' => $library['name'],
  105. ));
  106. $this->verbose("Expected:<br>$error_message");
  107. $this->verbose('Actual:<br>' . $library['error message']);
  108. $this->assertEqual($library['error message'], $error_message, 'Correct error message for an incompatible dependency');
  109. }
  110. /**
  111. * Tests libraries_scan_info_files().
  112. */
  113. public function testLibrariesScanInfoFiles() {
  114. $expected = array('example_info_file' => (object) array(
  115. 'uri' => drupal_get_path('module', 'libraries') . '/tests/libraries/example_info_file.libraries.info',
  116. 'filename' => 'example_info_file.libraries.info',
  117. 'name' => 'example_info_file.libraries',
  118. ));
  119. $this->assertEqual(libraries_scan_info_files(), $expected, 'libraries_scan_info_files() correctly finds the example info file.');
  120. $this->verbose('<pre>' . var_export(libraries_scan_info_files(), TRUE) . '</pre>');
  121. }
  122. /**
  123. * Tests libraries_info().
  124. */
  125. public function testLibrariesInfo() {
  126. // Test that modules can provide and alter library information.
  127. $info = libraries_info();
  128. $this->assertTrue(isset($info['example_module']));
  129. $this->verbose('Library:<pre>' . var_export($info['example_module'], TRUE) . '</pre>');
  130. $this->assertEqual($info['example_module']['info type'], 'module');
  131. $this->assertEqual($info['example_module']['module'], 'libraries_test_module');
  132. $this->assertTrue($info['example_module']['module_altered']);
  133. // Test that themes can provide and alter library information.
  134. $this->assertTrue(isset($info['example_theme']));
  135. $this->verbose('Library:<pre>' . var_export($info['example_theme'], TRUE) . '</pre>');
  136. $this->assertEqual($info['example_theme']['info type'], 'theme');
  137. $this->assertEqual($info['example_theme']['theme'], 'libraries_test_theme');
  138. $this->assertTrue($info['example_theme']['theme_altered']);
  139. // Test that library information is found correctly.
  140. $expected = array(
  141. 'name' => 'Example files',
  142. 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
  143. 'version' => '1',
  144. 'files' => array(
  145. 'js' => array('example_1.js' => array()),
  146. 'css' => array('example_1.css' => array()),
  147. 'php' => array('example_1.php' => array()),
  148. ),
  149. 'info type' => 'module',
  150. 'module' => 'libraries_test_module',
  151. );
  152. libraries_info_defaults($expected, 'example_files');
  153. $library = libraries_info('example_files');
  154. $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  155. $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  156. $this->assertEqual($library, $expected, 'Library information is correctly gathered.');
  157. // Test a library specified with an .info file gets detected.
  158. $expected = array(
  159. 'name' => 'Example info file',
  160. 'info type' => 'info file',
  161. 'info file' => drupal_get_path('module', 'libraries') . '/tests/libraries/example_info_file.libraries.info',
  162. );
  163. libraries_info_defaults($expected, 'example_info_file');
  164. $library = libraries_info('example_info_file');
  165. // If this module was downloaded from Drupal.org, the Drupal.org packaging
  166. // system has corrupted the test info file.
  167. // @see http://drupal.org/node/1606606
  168. unset($library['core'], $library['datestamp'], $library['project'], $library['version']);
  169. $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  170. $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  171. $this->assertEqual($library, $expected, 'Library specified with an .info file found');
  172. }
  173. /**
  174. * Tests libraries_detect().
  175. */
  176. public function testLibrariesDetect() {
  177. // Test missing library.
  178. $library = libraries_detect('example_missing');
  179. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  180. $this->assertEqual($library['error'], 'not found', 'Missing library not found.');
  181. $error_message = t('The %library library could not be found.', array(
  182. '%library' => $library['name'],
  183. ));
  184. $this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.');
  185. // Test unknown library version.
  186. $library = libraries_detect('example_undetected_version');
  187. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  188. $this->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.');
  189. $error_message = t('The version of the %library library could not be detected.', array(
  190. '%library' => $library['name'],
  191. ));
  192. $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.');
  193. // Test unsupported library version.
  194. $library = libraries_detect('example_unsupported_version');
  195. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  196. $this->assertEqual($library['error'], 'not supported', 'Unsupported version detected as such.');
  197. $error_message = t('The installed version %version of the %library library is not supported.', array(
  198. '%version' => $library['version'],
  199. '%library' => $library['name'],
  200. ));
  201. $this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.');
  202. // Test supported library version.
  203. $library = libraries_detect('example_supported_version');
  204. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  205. $this->assertEqual($library['installed'], TRUE, 'Supported library version found.');
  206. // Test libraries_get_version().
  207. $library = libraries_detect('example_default_version_callback');
  208. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  209. $this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.');
  210. // Test a multiple-parameter version callback.
  211. $library = libraries_detect('example_multiple_parameter_version_callback');
  212. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  213. $this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.');
  214. // Test a top-level files property.
  215. $library = libraries_detect('example_files');
  216. $files = array(
  217. 'js' => array('example_1.js' => array()),
  218. 'css' => array('example_1.css' => array()),
  219. 'php' => array('example_1.php' => array()),
  220. );
  221. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  222. $this->assertEqual($library['files'], $files, 'Top-level files property works.');
  223. // Test version-specific library files.
  224. $library = libraries_detect('example_versions');
  225. $files = array(
  226. 'js' => array('example_2.js' => array()),
  227. 'css' => array('example_2.css' => array()),
  228. 'php' => array('example_2.php' => array()),
  229. );
  230. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  231. $this->assertEqual($library['files'], $files, 'Version-specific library files found.');
  232. // Test missing variant.
  233. $library = libraries_detect('example_variant_missing');
  234. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  235. $this->assertEqual($library['variants']['example_variant']['error'], 'not found', 'Missing variant not found');
  236. $error_message = t('The %variant variant of the %library library could not be found.', array(
  237. '%variant' => 'example_variant',
  238. '%library' => 'Example variant missing',
  239. ));
  240. $this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.');
  241. // Test existing variant.
  242. $library = libraries_detect('example_variant');
  243. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  244. $this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.');
  245. }
  246. /**
  247. * Tests libraries_detect() without a $name parameter.
  248. */
  249. public function testLibrariesDetectAll() {
  250. // Test that an array with all library information is returned and that the
  251. // libraries are properly detected.
  252. $libraries = libraries_detect();
  253. $this->verbose('<pre>' . var_export($libraries, TRUE) . '</pre>');
  254. $this->assertEqual($libraries['example_missing']['error'], 'not found');
  255. $this->assertEqual($libraries['example_undetected_version']['error'], 'not detected');
  256. $this->assertEqual($libraries['example_unsupported_version']['error'], 'not supported');
  257. $this->assertEqual($libraries['example_supported_version']['installed'], TRUE);
  258. }
  259. /**
  260. * Tests libraries_load().
  261. */
  262. public function testLibrariesLoad() {
  263. // Test dependencies.
  264. $library = libraries_load('example_dependency_missing');
  265. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  266. $this->assertFalse($library['loaded'], 'Library with missing dependency cannot be loaded');
  267. $library = libraries_load('example_dependency_incompatible');
  268. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  269. $this->assertFalse($library['loaded'], 'Library with incompatible dependency cannot be loaded');
  270. $library = libraries_load('example_dependency_compatible');
  271. $this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
  272. $this->assertEqual($library['loaded'], 1, 'Library with compatible dependency is loaded');
  273. $loaded = &drupal_static('libraries_load');
  274. $this->verbose('<pre>' . var_export($loaded, TRUE) . '</pre>');
  275. $this->assertEqual($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded');
  276. // Test that PHP files that have a local $path variable do not break library
  277. // loading.
  278. // @see _libraries_require_once()
  279. $library = libraries_load('example_path_variable_override');
  280. $this->assertEqual($library['loaded'], 2, 'PHP files cannot break library loading.');
  281. }
  282. /**
  283. * Tests the applying of callbacks.
  284. */
  285. public function testCallbacks() {
  286. $expected = array(
  287. 'name' => 'Example callback',
  288. 'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
  289. 'version' => '1',
  290. 'versions' => array(
  291. '1' => array(
  292. 'variants' => array(
  293. 'example_variant' => array(
  294. 'info callback' => 'not applied',
  295. 'pre-detect callback' => 'not applied',
  296. 'post-detect callback' => 'not applied',
  297. 'pre-dependencies-load callback' => 'not applied',
  298. 'pre-load callback' => 'not applied',
  299. 'post-load callback' => 'not applied',
  300. ),
  301. ),
  302. 'info callback' => 'not applied',
  303. 'pre-detect callback' => 'not applied',
  304. 'post-detect callback' => 'not applied',
  305. 'pre-dependencies-load callback' => 'not applied',
  306. 'pre-load callback' => 'not applied',
  307. 'post-load callback' => 'not applied',
  308. ),
  309. ),
  310. 'variants' => array(
  311. 'example_variant' => array(
  312. 'info callback' => 'not applied',
  313. 'pre-detect callback' => 'not applied',
  314. 'post-detect callback' => 'not applied',
  315. 'pre-dependencies-load callback' => 'not applied',
  316. 'pre-load callback' => 'not applied',
  317. 'post-load callback' => 'not applied',
  318. ),
  319. ),
  320. 'callbacks' => array(
  321. 'info' => array('_libraries_test_module_info_callback'),
  322. 'pre-detect' => array('_libraries_test_module_pre_detect_callback'),
  323. 'post-detect' => array('_libraries_test_module_post_detect_callback'),
  324. 'pre-dependencies-load' => array('_libraries_test_module_pre_dependencies_load_callback'),
  325. 'pre-load' => array('_libraries_test_module_pre_load_callback'),
  326. 'post-load' => array('_libraries_test_module_post_load_callback'),
  327. ),
  328. 'info callback' => 'not applied',
  329. 'pre-detect callback' => 'not applied',
  330. 'post-detect callback' => 'not applied',
  331. 'pre-dependencies-load callback' => 'not applied',
  332. 'pre-load callback' => 'not applied',
  333. 'post-load callback' => 'not applied',
  334. 'info type' => 'module',
  335. 'module' => 'libraries_test_module',
  336. );
  337. libraries_info_defaults($expected, 'example_callback');
  338. // Test a callback in the 'info' group.
  339. $expected['info callback'] = 'applied (top-level)';
  340. $expected['versions']['1']['info callback'] = 'applied (version 1)';
  341. $expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
  342. $expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)';
  343. $library = libraries_info('example_callback');
  344. $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  345. $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  346. $this->assertEqual($library, $expected, 'Prepare callback was applied correctly.');
  347. // Test a callback in the 'pre-detect' and 'post-detect' phases.
  348. // Successfully detected libraries should only contain version information
  349. // for the detected version and thus, be marked as installed.
  350. unset($expected['versions']);
  351. $expected['installed'] = TRUE;
  352. // Additionally, version-specific properties of the detected version are
  353. // supposed to override the corresponding top-level properties.
  354. $expected['info callback'] = 'applied (version 1)';
  355. $expected['variants']['example_variant']['installed'] = TRUE;
  356. $expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
  357. // Version-overloading takes place after the 'pre-detect' callbacks have
  358. // been applied.
  359. $expected['pre-detect callback'] = 'applied (version 1)';
  360. $expected['post-detect callback'] = 'applied (top-level)';
  361. $expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)';
  362. $expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)';
  363. $library = libraries_detect('example_callback');
  364. $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  365. $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  366. $this->assertEqual($library, $expected, 'Detect callback was applied correctly.');
  367. // Test a callback in the 'pre-dependencies-load', 'pre-load' and
  368. // 'post-load' phases.
  369. // Successfully loaded libraries should only contain information about the
  370. // already loaded variant.
  371. unset($expected['variants']);
  372. $expected['loaded'] = 0;
  373. $expected['pre-dependencies-load callback'] = 'applied (top-level)';
  374. $expected['pre-load callback'] = 'applied (top-level)';
  375. $expected['post-load callback'] = 'applied (top-level)';
  376. $library = libraries_load('example_callback');
  377. $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  378. $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  379. $this->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.');
  380. // This is not recommended usually and is only used for testing purposes.
  381. drupal_static_reset('libraries_load');
  382. // Successfully loaded library variants are supposed to contain the specific
  383. // variant information only.
  384. $expected['info callback'] = 'applied (version 1, variant example_variant)';
  385. $expected['pre-detect callback'] = 'applied (version 1, variant example_variant)';
  386. $expected['post-detect callback'] = 'applied (variant example_variant)';
  387. $library = libraries_load('example_callback', 'example_variant');
  388. $this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
  389. $this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
  390. $this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.');
  391. }
  392. /**
  393. * Tests that library files are properly added to the page output.
  394. *
  395. * We check for JavaScript and CSS files directly in the DOM and add a list of
  396. * included PHP files manually to the page output.
  397. *
  398. * @see _libraries_test_module_load()
  399. */
  400. public function testLibrariesOutput() {
  401. // Test loading of a simple library with a top-level files property.
  402. $this->drupalGet('libraries-test-module/files');
  403. $this->assertLibraryFiles('example_1', 'File loading');
  404. // Test loading of integration files.
  405. $this->drupalGet('libraries-test-module/module-integration-files');
  406. $this->assertRaw('libraries_test_module.js', 'Integration file loading: libraries_test_module.js found');
  407. $this->assertRaw('libraries_test_module.css', 'Integration file loading: libraries_test_module.css found');
  408. $this->assertRaw('libraries_test_module.inc', 'Integration file loading: libraries_test_module.inc found');
  409. $this->drupalGet('libraries-test-module/theme-integration-files');
  410. $this->assertRaw('libraries_test_theme.js', 'Integration file loading: libraries_test_theme.js found');
  411. $this->assertRaw('libraries_test_theme.css', 'Integration file loading: libraries_test_theme.css found');
  412. $this->assertRaw('libraries_test_theme.inc', 'Integration file loading: libraries_test_theme.inc found');
  413. // Test loading of post-load integration files.
  414. $this->drupalGet('libraries-test-module/module-integration-files-post-load');
  415. // If the files were not loaded correctly, a fatal error occurs.
  416. $this->assertResponse(200, 'Post-load integration files are loaded correctly.');
  417. // Test version overloading.
  418. $this->drupalGet('libraries-test-module/versions');
  419. $this->assertLibraryFiles('example_2', 'Version overloading');
  420. // Test variant loading.
  421. $this->drupalGet('libraries-test-module/variant');
  422. $this->assertLibraryFiles('example_3', 'Variant loading');
  423. // Test version overloading and variant loading.
  424. $this->drupalGet('libraries-test-module/versions-and-variants');
  425. $this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading');
  426. // Test caching.
  427. variable_set('libraries_test_module_cache', TRUE);
  428. cache_clear_all('example_callback', 'cache_libraries');
  429. // When the library information is not cached, all callback groups should be
  430. // invoked.
  431. $this->drupalGet('libraries-test-module/cache');
  432. $this->assertRaw('The <em>info</em> callback group was invoked.', 'Info callback invoked for uncached libraries.');
  433. $this->assertRaw('The <em>pre-detect</em> callback group was invoked.', 'Pre-detect callback invoked for uncached libraries.');
  434. $this->assertRaw('The <em>post-detect</em> callback group was invoked.', 'Post-detect callback invoked for uncached libraries.');
  435. $this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for uncached libraries.');
  436. $this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for uncached libraries.');
  437. // When the library information is cached only the 'pre-load' and
  438. // 'post-load' callback groups should be invoked.
  439. $this->drupalGet('libraries-test-module/cache');
  440. $this->assertNoRaw('The <em>info</em> callback group was not invoked.', 'Info callback not invoked for cached libraries.');
  441. $this->assertNoRaw('The <em>pre-detect</em> callback group was not invoked.', 'Pre-detect callback not invoked for cached libraries.');
  442. $this->assertNoRaw('The <em>post-detect</em> callback group was not invoked.', 'Post-detect callback not invoked for cached libraries.');
  443. $this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for cached libraries.');
  444. $this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for cached libraries.');
  445. variable_set('libraries_test_module_cache', FALSE);
  446. }
  447. /**
  448. * Helper function to assert that a library was correctly loaded.
  449. *
  450. * Asserts that all the correct files were loaded and all the incorrect ones
  451. * were not.
  452. *
  453. * @param $name
  454. * The name of the files that should be loaded. The current testing system
  455. * knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name
  456. * has an associated JavaScript, CSS and PHP file that will be asserted. All
  457. * other files will be asserted to not be loaded. See
  458. * tests/example/README.txt for more information on how the loading of the
  459. * files is tested.
  460. * @param $label
  461. * (optional) A label to prepend to the assertion messages, to make them
  462. * less ambiguous.
  463. * @param $extensions
  464. * (optional) The expected file extensions of $name. Defaults to
  465. * array('js', 'css', 'php').
  466. */
  467. public function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) {
  468. $label = ($label !== '' ? "$label: " : '');
  469. // Test that the wrong files are not loaded...
  470. $names = array(
  471. 'example_1' => FALSE,
  472. 'example_2' => FALSE,
  473. 'example_3' => FALSE,
  474. 'example_4' => FALSE,
  475. );
  476. // ...and the correct ones are.
  477. $names[$name] = TRUE;
  478. // Test for the specific HTML that the different file types appear as in the
  479. // DOM.
  480. $html = array(
  481. 'js' => array('<script type="text/javascript" src="', '"></script>'),
  482. 'css' => array('@import url("', '");'),
  483. // PHP files do not get added to the DOM directly.
  484. // @see _libraries_test_load()
  485. 'php' => array('<li>', '</li>'),
  486. );
  487. foreach ($names as $name => $expected) {
  488. foreach ($extensions as $extension) {
  489. $filepath = drupal_get_path('module', 'libraries') . "/tests/libraries/example/$name.$extension";
  490. // JavaScript and CSS files appear as full URLs and with an appended
  491. // query string.
  492. if (in_array($extension, array('js', 'css'))) {
  493. $filepath = url('', array('absolute' => TRUE)) . $filepath . '?' . variable_get('css_js_query_string');
  494. }
  495. $raw = $html[$extension][0] . $filepath . $html[$extension][1];
  496. if ($expected) {
  497. $this->assertRaw($raw, "$label$name.$extension found.");
  498. }
  499. else {
  500. $this->assertNoRaw($raw, "$label$name.$extension not found.");
  501. }
  502. }
  503. }
  504. }
  505. }