module.test 18 KB

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