PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/simpletest/drupal_unit_tests.php

https://github.com/sdboyer/sdboyer-test
PHP | 180 lines | 122 code | 17 blank | 41 comment | 22 complexity | 66a8e9d724cce38e0b2800c7e2c52326 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Implementes getTestInstances to allow access to the test objects from outside
  4. */
  5. class DrupalTestSuite extends TestSuite {
  6. var $_cleanupModules = array();
  7. function DrupalTestSuite($label) {
  8. $this->TestSuite($label);
  9. }
  10. /**
  11. * @return array of instantiated tests that this GroupTests holds
  12. */
  13. function getTestInstances() {
  14. for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) {
  15. if (is_string($this->_test_cases[$i])) {
  16. $class = $this->_test_cases[$i];
  17. $this->_test_cases[$i] = &new $class();
  18. }
  19. }
  20. return $this->_test_cases;
  21. }
  22. }
  23. class DrupalUnitTests extends DrupalTestSuite {
  24. /**
  25. * Constructor
  26. * @param array $class_list list containing the classes of tests to be processed
  27. * default: NULL - run all tests
  28. */
  29. function DrupalUnitTests($class_list = NULL) {
  30. static $classes;
  31. $this->DrupalTestSuite('Drupal Unit Tests');
  32. /* Tricky part to avoid double inclusion */
  33. if (!$classes) {
  34. $files = array();
  35. foreach (module_list() as $module) {
  36. $module_path = drupal_get_path('module', $module);
  37. if (file_exists($module_path .'/tests/')) {
  38. $dir = $module_path .'/tests';
  39. $tests = file_scan_directory($dir, '\.test$');
  40. $files = array_merge($files, $tests);
  41. }
  42. }
  43. $files = array_keys($files);
  44. $existing_classes = get_declared_classes();
  45. foreach ($files as $file) {
  46. include_once($file);
  47. }
  48. $classes = array_diff(get_declared_classes(), $existing_classes);
  49. }
  50. if (!is_null($class_list)) {
  51. $classes = $class_list;
  52. }
  53. if (count($classes) == 0) {
  54. $this->addTestCase(new BadGroupTest($test_file, 'No new test cases'));
  55. return;
  56. }
  57. $groups = array();
  58. foreach ($classes as $class) {
  59. if (!is_subclass_of($class, 'DrupalTestCase')) {
  60. if (!is_subclass_of($class, 'DrupalWebTestCase')) {
  61. continue;
  62. }
  63. }
  64. $this->_addClassToGroups($groups, $class);
  65. }
  66. foreach ($groups as $group_name => $group) {
  67. $group_test = &new DrupalTestSuite($group_name);
  68. foreach ($group as $key => $v) {
  69. $group_test->addTestCase($group[$key]);
  70. }
  71. $this->addTestCase($group_test);
  72. }
  73. }
  74. /**
  75. * Adds a class to a groups array specified by the get_info of the group
  76. * @param array $groups Group of categorized tests
  77. * @param string $class Name of a class
  78. */
  79. function _addClassToGroups(&$groups, $class) {
  80. $test = &new $class();
  81. if (method_exists($test, 'get_info')) {
  82. $info = $test->get_info();
  83. $groups[$info['group']][] = $test;
  84. }
  85. elseif (method_exists($test, 'getInfo')) {
  86. $info = $test->getInfo();
  87. $groups[$info['group']][] = $test;
  88. }
  89. }
  90. /**
  91. * Invokes run() on all of the held test cases, instantiating
  92. * them if necessary.
  93. * The Drupal version uses paintHeader instead of paintGroupStart
  94. * to avoid collapsing of the very top level.
  95. *
  96. * @param SimpleReporter $reporter Current test reporter.
  97. * @access public
  98. */
  99. function run(&$reporter) {
  100. cache_clear_all();
  101. @set_time_limit(0);
  102. ignore_user_abort(true);
  103. // Disable devel output, check simpletest settings page
  104. if (!variable_get('simpletest_devel', false)) {
  105. $GLOBALS['devel_shutdown'] = FALSE;
  106. }
  107. parent::run($reporter);
  108. // Restores modules
  109. foreach ($this->_cleanupModules as $name => $status) {
  110. db_query("UPDATE {system} SET status = %d WHERE name = '%s' AND type = 'module'", $status, $name);
  111. }
  112. $this->_cleanupModules = array();
  113. }
  114. /**
  115. * Enables a drupal module
  116. * @param string $name name of the module
  117. * @return boolean success
  118. */
  119. function drupalModuleEnable($name) {
  120. if (module_exists($name)) {
  121. return TRUE;
  122. }
  123. include_once './includes/install.inc';
  124. module_rebuild_cache(); // Rebuild the module cache
  125. if (drupal_get_installed_schema_version($name, TRUE) == SCHEMA_UNINSTALLED) {
  126. drupal_install_modules(array($name));
  127. }
  128. else {
  129. $try = module_enable(array($name));
  130. }
  131. if(module_exists($name)) {
  132. if (!isset($this->_cleanupModules[$name])) {
  133. $this->_cleanupModules[$name] = 0;
  134. return TRUE;
  135. }
  136. }
  137. else {
  138. die("required module $name could not be enabled (probably file does not exist)");
  139. }
  140. }
  141. /**
  142. * Disables a drupal module
  143. * @param string $name name of the module
  144. * @return boolean success
  145. */
  146. function drupalModuleDisable($name) {
  147. if (!module_exists($name)) {
  148. return TRUE;
  149. }
  150. /* Update table */
  151. db_query("UPDATE {system} SET status = 0 WHERE name = '%s' AND type = 'module'", $name);
  152. if (db_affected_rows()) {
  153. /* Make sure not overwriting when double switching */
  154. if (!isset($this->_cleanupModules[$name])) {
  155. $this->_cleanupModules[$name] = 1;
  156. }
  157. /* refresh module_list */
  158. module_list(TRUE, FALSE);
  159. return TRUE;
  160. }
  161. die("incompatible module $name could not be disabled for unknown reason");
  162. }
  163. }