From e9d97f1eef493de9b9cb3ae456175ce33b5cb796 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 8 Jun 2009 09:23:55 +0000 Subject: - Patch #449198 by boombatower: cealn up test loading and related API. --- modules/simpletest/simpletest.info | 23 ++++++ modules/simpletest/simpletest.module | 120 +++++++++++++++++++------------- modules/simpletest/simpletest.pages.inc | 17 ++--- modules/simpletest/tests/graph.test | 2 +- 4 files changed, 102 insertions(+), 60 deletions(-) (limited to 'modules/simpletest') diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info index 0fefa967d..a90e5dd2c 100644 --- a/modules/simpletest/simpletest.info +++ b/modules/simpletest/simpletest.info @@ -7,3 +7,26 @@ core = 7.x files[] = simpletest.module files[] = simpletest.pages.inc files[] = simpletest.install +files[] = simpletest.test +files[] = drupal_web_test_case.php + +; Tests in tests directory. +files[] = tests/actions.test +files[] = tests/batch.test +files[] = tests/bootstrap.test +files[] = tests/cache.test +files[] = tests/common.test +files[] = tests/database_test.test +files[] = tests/error.test +files[] = tests/file.test +files[] = tests/form.test +files[] = tests/graph.test +files[] = tests/image.test +files[] = tests/menu.test +files[] = tests/module.test +files[] = tests/registry.test +files[] = tests/schema.test +files[] = tests/session.test +files[] = tests/theme.test +files[] = tests/unicode.test +files[] = tests/xmlrpc.test diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module index 1300c2416..630ddb3f3 100644 --- a/modules/simpletest/simpletest.module +++ b/modules/simpletest/simpletest.module @@ -139,9 +139,6 @@ function simpletest_run_tests($test_list, $reporter = 'drupal') { * Batch operation callback. */ function _simpletest_batch_operation($test_list_init, $test_id, &$context) { - // Ensure that all classes are loaded before we unserialize some instances. - simpletest_get_all_tests(); - // Get working values. if (!isset($context['sandbox']['max'])) { // First iteration: initialize working values. @@ -200,65 +197,94 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) { } /** - * Get a list of all of the tests. + * Get a list of all of the tests provided by the system. + * + * The list of test classes is loaded from the registry where it looks for + * files ending in ".test". Once loaded the test list is cached and stored in + * a static variable. In order to list tests provided by disabled modules + * hook_registry_files_alter() is used to forcefully add them to the registry. * * @return - * An array of tests, with the class name as the keys and the instantiated - * versions of the classes as the values. + * An array of tests keyed with the groups specified in each of the tests + * getInfo() method and then keyed by the test class. An example of the array + * structure is provided below. + * + * @code + * $groups['Blog'] => array( + * 'BlogTestCase' => array( + * 'name' => 'Blog functionality', + * 'description' => 'Create, view, edit, delete, ...', + * 'group' => 'Blog', + * ), + * ); + * @endcode + * @see simpletest_registry_files_alter() */ -function simpletest_get_all_tests() { - static $classes; - if (!isset($classes)) { - require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php'; - $files = array(); - foreach (array_keys(system_get_module_data()) as $module) { - $module_path = drupal_get_path('module', $module); - $test = $module_path . "/$module.test"; - if (file_exists($test)) { - $files[] = $test; - } +function simpletest_test_get_all() { + $groups = &drupal_static(__FUNCTION__); + + if (!$groups) { + // Load test information from cache if available, otherwise retrieve the + // information from each tests getInfo() method. + if ($cache = cache_get('simpletest', 'cache')) { + $groups = $cache->data; + } + else { + // Select all clases in files ending with .test. + $classes = db_select('registry') + ->fields('registry', array('name')) + ->condition('type', 'class') + ->condition('filename', '%.test', 'LIKE') + ->execute(); - $tests_directory = $module_path . '/tests'; - if (is_dir($tests_directory)) { - foreach (file_scan_directory($tests_directory, '/\.test$/') as $file) { - $files[] = $file->filepath; + $groups = array(); + + // Check that each class has a getInfo() method and store the information + // in an array keyed with the group specified in the test information. + foreach ($classes as $class) { + $class = $class->name; + if (class_exists($class) && method_exists($class, 'getInfo')) { + // Valid test class, retrieve test information. + $info = call_user_func(array($class, 'getInfo')); + + // Initialize test groups. + if (!isset($groups[$info['group']])) { + $groups[$info['group']] = array(); + } + $groups[$info['group']][$class] = $info; } } - } - - $existing_classes = get_declared_classes(); - foreach ($files as $file) { - include_once DRUPAL_ROOT . '/' . $file; - } - $classes = array_values(array_diff(get_declared_classes(), $existing_classes)); - foreach ($classes as $key => $class) { - if (!is_subclass_of($class, 'DrupalTestCase') || !method_exists($class, 'getInfo')) { - unset($classes[$key]); + // Sort the groups and tests within the groups by name. + uksort($groups, 'strnatcasecmp'); + foreach ($groups as $group => &$tests) { + uksort($tests, 'strnatcasecmp'); } + + cache_set('simpletest', $groups); } } - if (count($classes) == 0) { - drupal_set_message('No test cases found.', 'error'); - return FALSE; - } - return $classes; + return $groups; } /** - * Categorize the tests into groups. + * Implementation of hook_registry_files_alter(). * - * @param $tests - * A list of tests from simpletest_get_all_tests. - * @see simpletest_get_all_tests. + * Add the test files for disabled modules so that we get a list containing + * all the avialable tests. */ -function simpletest_categorize_tests($tests) { - $groups = array(); - foreach ($tests as $test) { - $info = call_user_func(array($test, 'getInfo')); - $groups[$info['group']][$test] = $info; +function simpletest_registry_files_alter(&$files, $modules) { + foreach ($modules as $module) { + // Only add test files for disabled modules, as enabled modules should + // already include any test files they provide. + if (!$module->status) { + $dir = $module->dir; + foreach ($module->info['files'] as $file) { + if (substr($file, -5) == '.test') { + $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight); + } + } + } } - uksort($groups, 'strnatcasecmp'); - return $groups; } /** diff --git a/modules/simpletest/simpletest.pages.inc b/modules/simpletest/simpletest.pages.inc index 24a779348..2df1765fa 100644 --- a/modules/simpletest/simpletest.pages.inc +++ b/modules/simpletest/simpletest.pages.inc @@ -12,10 +12,6 @@ function simpletest_test_form() { $form = array(); - // Categorize the tests for display. - $uncategorized_tests = simpletest_get_all_tests(); - $tests = simpletest_categorize_tests($uncategorized_tests); - $form['tests'] = array( '#type' => 'fieldset', '#title' => t('Tests'), @@ -27,13 +23,14 @@ function simpletest_test_form() { ); // Generate the list of tests arranged by group. - foreach ($tests as $group_name => $test_group) { - $form['tests']['table'][$group_name] = array( + $groups = simpletest_test_get_all(); + foreach ($groups as $group => $tests) { + $form['tests']['table'][$group] = array( '#collapsed' => TRUE, ); - foreach ($test_group as $class => $info) { - $form['tests']['table'][$group_name][$class] = array( + foreach ($tests as $class => $info) { + $form['tests']['table'][$group][$class] = array( '#type' => 'checkbox', '#title' => $info['name'], '#description' => $info['description'], @@ -166,9 +163,6 @@ function theme_simpletest_test_table($table) { * Run selected tests. */ function simpletest_test_form_submit($form, &$form_state) { - // Ensure that all classes are loaded before we create instances to get test information and run. - simpletest_get_all_tests(); - // Get list of tests. $tests_list = array(); foreach ($form_state['values'] as $class_name => $value) { @@ -200,7 +194,6 @@ function simpletest_result_form(&$form_state, $test_id) { // Load all classes and include CSS. drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css'); - simpletest_get_all_tests(); // Keep track of which test cases passed or failed. $filter = array( diff --git a/modules/simpletest/tests/graph.test b/modules/simpletest/tests/graph.test index a5ed9314b..b3270f843 100644 --- a/modules/simpletest/tests/graph.test +++ b/modules/simpletest/tests/graph.test @@ -23,7 +23,7 @@ class GraphUnitTest extends DrupalUnitTestCase { */ function testDepthFirstSearch() { // Provoke the inclusion of graph.inc. - drupal_function_exists('drupal_depth_first_search'); + require_once 'includes/graph.inc'; // The sample graph used is: // 1 --> 2 --> 3 5 ---> 6 -- cgit v1.2.3