module.test

Tests for the module API.

File

drupal-7.x/modules/simpletest/tests/module.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the module API.
  5. */
  6. /**
  7. * Unit tests for the module API.
  8. */
  9. class ModuleUnitTest extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Module API',
  13. 'description' => 'Test low-level module functions.',
  14. 'group' => 'Module',
  15. );
  16. }
  17. /**
  18. * The basic functionality of module_list().
  19. */
  20. function testModuleList() {
  21. // Build a list of modules, sorted alphabetically.
  22. $profile_info = install_profile_info('standard', 'en');
  23. $module_list = $profile_info['dependencies'];
  24. // Installation profile is a module that is expected to be loaded.
  25. $module_list[] = 'standard';
  26. sort($module_list);
  27. // Compare this list to the one returned by module_list(). We expect them
  28. // to match, since all default profile modules have a weight equal to 0
  29. // (except for block.module, which has a lower weight but comes first in
  30. // the alphabet anyway).
  31. $this->assertModuleList($module_list, t('Standard profile'));
  32. // Try to install a new module.
  33. module_enable(array('contact'));
  34. $module_list[] = 'contact';
  35. sort($module_list);
  36. $this->assertModuleList($module_list, t('After adding a module'));
  37. // Try to mess with the module weights.
  38. db_update('system')
  39. ->fields(array('weight' => 20))
  40. ->condition('name', 'contact')
  41. ->condition('type', 'module')
  42. ->execute();
  43. // Reset the module list.
  44. module_list(TRUE);
  45. // Move contact to the end of the array.
  46. unset($module_list[array_search('contact', $module_list)]);
  47. $module_list[] = 'contact';
  48. $this->assertModuleList($module_list, t('After changing weights'));
  49. // Test the fixed list feature.
  50. $fixed_list = array(
  51. 'system' => array('filename' => drupal_get_path('module', 'system')),
  52. 'menu' => array('filename' => drupal_get_path('module', 'menu')),
  53. );
  54. module_list(FALSE, FALSE, FALSE, $fixed_list);
  55. $new_module_list = array_combine(array_keys($fixed_list), array_keys($fixed_list));
  56. $this->assertModuleList($new_module_list, t('When using a fixed list'));
  57. // Reset the module list.
  58. module_list(TRUE);
  59. $this->assertModuleList($module_list, t('After reset'));
  60. }
  61. /**
  62. * Assert that module_list() return the expected values.
  63. *
  64. * @param $expected_values
  65. * The expected values, sorted by weight and module name.
  66. */
  67. protected function assertModuleList(Array $expected_values, $condition) {
  68. $expected_values = array_combine($expected_values, $expected_values);
  69. $this->assertEqual($expected_values, module_list(), format_string('@condition: module_list() returns correct results', array('@condition' => $condition)));
  70. ksort($expected_values);
  71. $this->assertIdentical($expected_values, module_list(FALSE, FALSE, TRUE), format_string('@condition: module_list() returns correctly sorted results', array('@condition' => $condition)));
  72. }
  73. /**
  74. * Test module_implements() caching.
  75. */
  76. function testModuleImplements() {
  77. // Clear the cache.
  78. cache_clear_all('module_implements', 'cache_bootstrap');
  79. $this->assertFalse(cache_get('module_implements', 'cache_bootstrap'), 'The module implements cache is empty.');
  80. $this->drupalGet('');
  81. $this->assertTrue(cache_get('module_implements', 'cache_bootstrap'), 'The module implements cache is populated after requesting a page.');
  82. // Test again with an authenticated user.
  83. $this->user = $this->drupalCreateUser();
  84. $this->drupalLogin($this->user);
  85. cache_clear_all('module_implements', 'cache_bootstrap');
  86. $this->drupalGet('');
  87. $this->assertTrue(cache_get('module_implements', 'cache_bootstrap'), 'The module implements cache is populated after requesting a page.');
  88. // Make sure group include files are detected properly even when the file is
  89. // already loaded when the cache is rebuilt.
  90. // For that activate the module_test which provides the file to load.
  91. module_enable(array('module_test'));
  92. module_load_include('inc', 'module_test', 'module_test.file');
  93. $modules = module_implements('test_hook');
  94. $static = drupal_static('module_implements');
  95. $this->assertTrue(in_array('module_test', $modules), 'Hook found.');
  96. $this->assertEqual($static['test_hook']['module_test'], 'file', 'Include file detected.');
  97. }
  98. /**
  99. * Test that module_invoke() can load a hook defined in hook_hook_info().
  100. */
  101. function testModuleInvoke() {
  102. module_enable(array('module_test'), FALSE);
  103. $this->resetAll();
  104. $this->drupalGet('module-test/hook-dynamic-loading-invoke');
  105. $this->assertText('success!', 'module_invoke() dynamically loads a hook defined in hook_hook_info().');
  106. }
  107. /**
  108. * Test that module_invoke_all() can load a hook defined in hook_hook_info().
  109. */
  110. function testModuleInvokeAll() {
  111. module_enable(array('module_test'), FALSE);
  112. $this->resetAll();
  113. $this->drupalGet('module-test/hook-dynamic-loading-invoke-all');
  114. $this->assertText('success!', 'module_invoke_all() dynamically loads a hook defined in hook_hook_info().');
  115. }
  116. /**
  117. * Test dependency resolution.
  118. */
  119. function testDependencyResolution() {
  120. // Enable the test module, and make sure that other modules we are testing
  121. // are not already enabled. (If they were, the tests below would not work
  122. // correctly.)
  123. module_enable(array('module_test'), FALSE);
  124. $this->assertTrue(module_exists('module_test'), 'Test module is enabled.');
  125. $this->assertFalse(module_exists('forum'), 'Forum module is disabled.');
  126. $this->assertFalse(module_exists('poll'), 'Poll module is disabled.');
  127. $this->assertFalse(module_exists('php'), 'PHP module is disabled.');
  128. // First, create a fake missing dependency. Forum depends on poll, which
  129. // depends on a made-up module, foo. Nothing should be installed.
  130. variable_set('dependency_test', 'missing dependency');
  131. drupal_static_reset('system_rebuild_module_data');
  132. $result = module_enable(array('forum'));
  133. $this->assertFalse($result, 'module_enable() returns FALSE if dependencies are missing.');
  134. $this->assertFalse(module_exists('forum'), 'module_enable() aborts if dependencies are missing.');
  135. // Now, fix the missing dependency. Forum module depends on poll, but poll
  136. // depends on the PHP module. module_enable() should work.
  137. variable_set('dependency_test', 'dependency');
  138. drupal_static_reset('system_rebuild_module_data');
  139. $result = module_enable(array('forum'));
  140. $this->assertTrue($result, 'module_enable() returns the correct value.');
  141. // Verify that the fake dependency chain was installed.
  142. $this->assertTrue(module_exists('poll') && module_exists('php'), 'Dependency chain was installed by module_enable().');
  143. // Verify that the original module was installed.
  144. $this->assertTrue(module_exists('forum'), 'Module installation with unlisted dependencies succeeded.');
  145. // Finally, verify that the modules were enabled in the correct order.
  146. $this->assertEqual(variable_get('test_module_enable_order', array()), array('php', 'poll', 'forum'), 'Modules were enabled in the correct order by module_enable().');
  147. // Now, disable the PHP module. Both forum and poll should be disabled as
  148. // well, in the correct order.
  149. module_disable(array('php'));
  150. $this->assertTrue(!module_exists('forum') && !module_exists('poll'), 'Depedency chain was disabled by module_disable().');
  151. $this->assertFalse(module_exists('php'), 'Disabling a module with unlisted dependents succeeded.');
  152. $this->assertEqual(variable_get('test_module_disable_order', array()), array('forum', 'poll', 'php'), 'Modules were disabled in the correct order by module_disable().');
  153. // Disable a module that is listed as a dependency by the installation
  154. // profile. Make sure that the profile itself is not on the list of
  155. // dependent modules to be disabled.
  156. $profile = drupal_get_profile();
  157. $info = install_profile_info($profile);
  158. $this->assertTrue(in_array('comment', $info['dependencies']), 'Comment module is listed as a dependency of the installation profile.');
  159. $this->assertTrue(module_exists('comment'), 'Comment module is enabled.');
  160. module_disable(array('comment'));
  161. $this->assertFalse(module_exists('comment'), 'Comment module was disabled.');
  162. $disabled_modules = variable_get('test_module_disable_order', array());
  163. $this->assertTrue(in_array('comment', $disabled_modules), 'Comment module is in the list of disabled modules.');
  164. $this->assertFalse(in_array($profile, $disabled_modules), 'The installation profile is not in the list of disabled modules.');
  165. // Try to uninstall the PHP module by itself. This should be rejected,
  166. // since the modules which it depends on need to be uninstalled first, and
  167. // that is too destructive to perform automatically.
  168. $result = drupal_uninstall_modules(array('php'));
  169. $this->assertFalse($result, 'Calling drupal_uninstall_modules() on a module whose dependents are not uninstalled fails.');
  170. foreach (array('forum', 'poll', 'php') as $module) {
  171. $this->assertNotEqual(drupal_get_installed_schema_version($module), SCHEMA_UNINSTALLED, format_string('The @module module was not uninstalled.', array('@module' => $module)));
  172. }
  173. // Now uninstall all three modules explicitly, but in the incorrect order,
  174. // and make sure that drupal_uninstal_modules() uninstalled them in the
  175. // correct sequence.
  176. $result = drupal_uninstall_modules(array('poll', 'php', 'forum'));
  177. $this->assertTrue($result, 'drupal_uninstall_modules() returns the correct value.');
  178. foreach (array('forum', 'poll', 'php') as $module) {
  179. $this->assertEqual(drupal_get_installed_schema_version($module), SCHEMA_UNINSTALLED, format_string('The @module module was uninstalled.', array('@module' => $module)));
  180. }
  181. $this->assertEqual(variable_get('test_module_uninstall_order', array()), array('forum', 'poll', 'php'), 'Modules were uninstalled in the correct order by drupal_uninstall_modules().');
  182. // Uninstall the profile module from above, and make sure that the profile
  183. // itself is not on the list of dependent modules to be uninstalled.
  184. $result = drupal_uninstall_modules(array('comment'));
  185. $this->assertTrue($result, 'drupal_uninstall_modules() returns the correct value.');
  186. $this->assertEqual(drupal_get_installed_schema_version('comment'), SCHEMA_UNINSTALLED, 'Comment module was uninstalled.');
  187. $uninstalled_modules = variable_get('test_module_uninstall_order', array());
  188. $this->assertTrue(in_array('comment', $uninstalled_modules), 'Comment module is in the list of uninstalled modules.');
  189. $this->assertFalse(in_array($profile, $uninstalled_modules), 'The installation profile is not in the list of uninstalled modules.');
  190. // Enable forum module again, which should enable both the poll module and
  191. // php module. But, this time do it with poll module declaring a dependency
  192. // on a specific version of php module in its info file. Make sure that
  193. // module_enable() still works.
  194. variable_set('dependency_test', 'version dependency');
  195. drupal_static_reset('system_rebuild_module_data');
  196. $result = module_enable(array('forum'));
  197. $this->assertTrue($result, 'module_enable() returns the correct value.');
  198. // Verify that the fake dependency chain was installed.
  199. $this->assertTrue(module_exists('poll') && module_exists('php'), 'Dependency chain was installed by module_enable().');
  200. // Verify that the original module was installed.
  201. $this->assertTrue(module_exists('forum'), 'Module installation with version dependencies succeeded.');
  202. // Finally, verify that the modules were enabled in the correct order.
  203. $enable_order = variable_get('test_module_enable_order', array());
  204. $php_position = array_search('php', $enable_order);
  205. $poll_position = array_search('poll', $enable_order);
  206. $forum_position = array_search('forum', $enable_order);
  207. $php_before_poll = $php_position !== FALSE && $poll_position !== FALSE && $php_position < $poll_position;
  208. $poll_before_forum = $poll_position !== FALSE && $forum_position !== FALSE && $poll_position < $forum_position;
  209. $this->assertTrue($php_before_poll && $poll_before_forum, 'Modules were enabled in the correct order by module_enable().');
  210. }
  211. }
  212. /**
  213. * Unit tests for module installation.
  214. */
  215. class ModuleInstallTestCase extends DrupalWebTestCase {
  216. public static function getInfo() {
  217. return array(
  218. 'name' => 'Module installation',
  219. 'description' => 'Tests the installation of modules.',
  220. 'group' => 'Module',
  221. );
  222. }
  223. function setUp() {
  224. parent::setUp('module_test');
  225. }
  226. /**
  227. * Test that calls to drupal_write_record() work during module installation.
  228. *
  229. * This is a useful function to test because modules often use it to insert
  230. * initial data in their database tables when they are being installed or
  231. * enabled. Furthermore, drupal_write_record() relies on the module schema
  232. * information being available, so this also checks that the data from one of
  233. * the module's hook implementations, in particular hook_schema(), is
  234. * properly available during this time. Therefore, this test helps ensure
  235. * that modules are fully functional while Drupal is installing and enabling
  236. * them.
  237. */
  238. function testDrupalWriteRecord() {
  239. // Check for data that was inserted using drupal_write_record() while the
  240. // 'module_test' module was being installed and enabled.
  241. $data = db_query("SELECT data FROM {module_test}")->fetchCol();
  242. $this->assertTrue(in_array('Data inserted in hook_install()', $data), 'Data inserted using drupal_write_record() in hook_install() is correctly saved.');
  243. $this->assertTrue(in_array('Data inserted in hook_enable()', $data), 'Data inserted using drupal_write_record() in hook_enable() is correctly saved.');
  244. }
  245. }
  246. /**
  247. * Unit tests for module uninstallation and related hooks.
  248. */
  249. class ModuleUninstallTestCase extends DrupalWebTestCase {
  250. public static function getInfo() {
  251. return array(
  252. 'name' => 'Module uninstallation',
  253. 'description' => 'Tests the uninstallation of modules.',
  254. 'group' => 'Module',
  255. );
  256. }
  257. function setUp() {
  258. parent::setUp('module_test', 'user');
  259. }
  260. /**
  261. * Tests the hook_modules_uninstalled() of the user module.
  262. */
  263. function testUserPermsUninstalled() {
  264. // Uninstalls the module_test module, so hook_modules_uninstalled()
  265. // is executed.
  266. module_disable(array('module_test'));
  267. drupal_uninstall_modules(array('module_test'));
  268. // Are the perms defined by module_test removed from {role_permission}.
  269. $count = db_query("SELECT COUNT(rid) FROM {role_permission} WHERE permission = :perm", array(':perm' => 'module_test perm'))->fetchField();
  270. $this->assertEqual(0, $count, 'Permissions were all removed.');
  271. }
  272. }