From c863246b516ef9f92f4c3a9a513a0e20f315e46a Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 23 Apr 2008 17:49:50 +0000
Subject: [PATCH] - Patch #249629 by chx, flobruit: made drupal_test_case()
 reusable and improve the SimpleTest documentation.

---
 modules/simpletest/drupal_test_suite.php | 92 +++++++++++++++---------
 1 file changed, 59 insertions(+), 33 deletions(-)

diff --git a/modules/simpletest/drupal_test_suite.php b/modules/simpletest/drupal_test_suite.php
index bbc33e220c4f..6779d882bac3 100644
--- a/modules/simpletest/drupal_test_suite.php
+++ b/modules/simpletest/drupal_test_suite.php
@@ -2,17 +2,18 @@
 // $Id$
 
 /**
- * Implementes getTestInstances to allow access to the test objects from outside
+ * Implements getTestInstances to allow access to the test objects from outside.
  */
 class DrupalTestSuite extends TestSuite {
-  var $_cleanupModules   = array();
+  var $_cleanupModules = array();
 
   function DrupalTestSuite($label) {
     $this->TestSuite($label);
   }
 
   /**
-   * @return array of instantiated tests that this GroupTests holds
+   * @return
+   *   An array of instantiated tests that this GroupTests holds.
    */
   function getTestInstances() {
     for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) {
@@ -27,25 +28,20 @@ function getTestInstances() {
 
 class DrupalTests extends DrupalTestSuite {
   /**
-   * Constructor
-   * @param array   $class_list  list containing the classes of tests to be processed
-   *                             default: NULL - run all tests
+   * Constructor for the DrupalTests class.
+   *
+   * @param array $class_list
+   *   List containing the classes of tests to be processed,
+   *   defaults to process all tests.
    */
   function DrupalTests($class_list = NULL) {
     static $classes;
     $this->DrupalTestSuite('Drupal Unit Tests');
 
-    /* Tricky part to avoid double inclusion */
+    // Tricky part to avoid double inclusion.
     if (!$classes) {
 
-      $files = array();
-      foreach (array_keys(module_rebuild_cache()) as $module) {
-        $module_path = drupal_get_path('module', $module);
-        $test = $module_path . "/$module.test";
-        if (file_exists($test)) {
-          $files[] = $test;
-        }
-      }
+      $files = $this->getFiles();
 
       $existing_classes = get_declared_classes();
       foreach ($files as $file) {
@@ -62,10 +58,9 @@ function DrupalTests($class_list = NULL) {
     }
     $groups = array();
     foreach ($classes as $class) {
-      if (!is_subclass_of($class, 'DrupalWebTestCase') && !is_subclass_of($class, 'DrupalUnitTestCase')) {
-        continue;
+      if ($this->classIsTest($class)) {
+        $this->_addClassToGroups($groups, $class);
       }
-      $this->_addClassToGroups($groups, $class);
     }
     foreach ($groups as $group_name => $group) {
       $group_test = &new DrupalTestSuite($group_name);
@@ -77,9 +72,12 @@ function DrupalTests($class_list = NULL) {
   }
 
   /**
-   * Adds a class to a groups array specified by the getInfo of the group
-   * @param array  $groups Group of categorized tests
-   * @param string $class  Name of a class
+   * Adds a class to a groups array specified by the getInfo() of the group.
+   *
+   * @param array $groups
+   *   Group of categorized tests.
+   * @param string $class
+   *   Name of the class.
    */
   function _addClassToGroups(&$groups, $class) {
     $test = &new $class();
@@ -95,27 +93,55 @@ function _addClassToGroups(&$groups, $class) {
    * The Drupal version uses paintHeader instead of paintGroupStart
    * to avoid collapsing of the very top level.
    *
-   * @param SimpleReporter $reporter    Current test reporter.
+   * @param SimpleReporter $reporter
+   *   Current test reporter.
    * @access public
    */
   function run(&$reporter) {
-    cache_clear_all();
     @set_time_limit(0);
     ignore_user_abort(TRUE);
 
-    // Disable devel output, check simpletest settings page
-    if (!variable_get('simpletest_devel', FALSE)) {
-      $GLOBALS['devel_shutdown'] = FALSE;
-    }
-
+    $this->cleanupBeforeRun();
     $result = parent::run($reporter);
+    return $result;
+  }
 
-    // Restores modules
-    foreach ($this->_cleanupModules as $name => $status) {
-      db_query("UPDATE {system} SET status = %d WHERE name = '%s' AND type = 'module'", $status, $name);
+  /**
+   * Gets the files which contains the tests.
+   *
+   * @return
+   *   A list of files that contains the tests.
+   */
+  function getFiles() {
+    $files = array();
+    foreach (array_keys(module_rebuild_cache()) as $module) {
+      $module_path = drupal_get_path('module', $module);
+      $test = $module_path . "/$module.test";
+      if (file_exists($test)) {
+        $files[] = $test;
+      }
     }
-    $this->_cleanupModules = array();
+    return $files;
+  }
 
-    return $result;
+  /**
+   * Determines whether the class is a test.
+   *
+   * @return
+   *   TRUE / FALSE depending on whether the class is a test.
+   */
+  function classIsTest($class) {
+    return is_subclass_of($class, 'DrupalWebTestCase');
+  }
+
+  /**
+   * Called before the tests are run.
+   */
+  function cleanupBeforeRun() {
+    cache_clear_all();
+    // Disable devel output, check simpletest settings page.
+    if (!variable_get('simpletest_devel', FALSE)) {
+      $GLOBALS['devel_shutdown'] = FALSE;
+    }
   }
 }
-- 
GitLab