PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jiva-technology/smcuk
PHP | 162 lines | 106 code | 15 blank | 41 comment | 19 complexity | 91b83dded1a7d9fbf81afd928e49f544 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 instanciated 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 = module_invoke_all('simpletest');
  35. $existing_classes = get_declared_classes();
  36. foreach ($files as $file) {
  37. include_once($file);
  38. }
  39. $classes = array_diff(get_declared_classes(), $existing_classes);
  40. }
  41. if (!is_null($class_list)) {
  42. $classes = $class_list;
  43. }
  44. if (count($classes) == 0) {
  45. $this->addTestCase(new BadGroupTest($test_file, 'No new test cases'));
  46. return;
  47. }
  48. $groups = array();
  49. foreach ($classes as $class) {
  50. $this->_addClassToGroups($groups, $class);
  51. }
  52. foreach ($groups as $group_name => $group) {
  53. $group_test = &new DrupalTestSuite($group_name);
  54. foreach ($group as $key => $v) {
  55. $group_test->addTestCase($group[$key]);
  56. }
  57. $this->addTestCase($group_test);
  58. }
  59. }
  60. /**
  61. * Adds a class to a groups array specified by the get_info of the group
  62. * @param array $groups Group of categorizesd tests
  63. * @param string $class Name of a class
  64. */
  65. function _addClassToGroups(&$groups, $class) {
  66. $test = &new $class();
  67. if (method_exists($test, 'get_info')) {
  68. $info = $test->get_info();
  69. $groups[$info['group']][] = $test;
  70. }
  71. else {
  72. $groups[$class][] = $test;
  73. }
  74. }
  75. /**
  76. * Invokes run() on all of the held test cases, instantiating
  77. * them if necessary.
  78. * The Druapl version uses paintHeader instead of paintGroupStart
  79. * to avoid collapsing of the very top level.
  80. *
  81. * @param SimpleReporter $reporter Current test reporter.
  82. * @access public
  83. */
  84. function run(&$reporter) {
  85. cache_clear_all();
  86. @set_time_limit(0);
  87. ignore_user_abort(true);
  88. // Disable known problematic modules
  89. $this->drupalModuleDisable('devel');
  90. parent::run($reporter);
  91. // Restores modules
  92. foreach ($this->_cleanupModules as $name => $status) {
  93. db_query("UPDATE {system} SET status = %d WHERE name = '%s' AND type = 'module'", $status, $name);
  94. }
  95. $this->_cleanupModules = array();
  96. }
  97. /**
  98. * Enables a drupal module
  99. * @param string $name name of the module
  100. * @return boolean success
  101. */
  102. function drupalModuleEnable($name) {
  103. if (module_exists($name)) {
  104. return TRUE;
  105. }
  106. include_once './includes/install.inc';
  107. module_rebuild_cache(); // Rebuild the module cache
  108. if (drupal_get_installed_schema_version($name, TRUE) == SCHEMA_UNINSTALLED) {
  109. drupal_install_modules(array($name));
  110. }
  111. else {
  112. $try = module_enable(array($name));
  113. }
  114. if(module_exists($name)) {
  115. if (!isset($this->_cleanupModules[$name])) {
  116. $this->_cleanupModules[$name] = 0;
  117. return TRUE;
  118. }
  119. }
  120. else {
  121. die("required module $name could not be enbled, probably file not exists");
  122. }
  123. }
  124. /**
  125. * Disables a drupal module
  126. * @param string $name name of the module
  127. * @return boolean success
  128. */
  129. function drupalModuleDisable($name) {
  130. if (!module_exists($name)) {
  131. return TRUE;
  132. }
  133. /* Update table */
  134. db_query("UPDATE {system} SET status = 0 WHERE name = '%s' AND type = 'module'", $name);
  135. if (db_affected_rows()) {
  136. /* Make sure not overwriting when double switching */
  137. if (!isset($this->_cleanupModules[$name])) {
  138. $this->_cleanupModules[$name] = 1;
  139. }
  140. /* refresh module_list */
  141. module_list(TRUE, FALSE);
  142. return TRUE;
  143. }
  144. die("incompatible module $name could not be disabled for unknown reason");
  145. }
  146. }
  147. ?>