libraries.test 26 KB

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