Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.55 diff -u -r1.55 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 5 Nov 2008 17:06:18 -0000 1.55 +++ modules/simpletest/drupal_web_test_case.php 8 Nov 2008 09:04:44 -0000 @@ -684,19 +684,75 @@ function setUp() { global $db_prefix; + $modules = func_get_args(); + if (!variable_get('simpletest_install_cached', FALSE)) { + // Delete temporary files directory and reset files directory path. + if (is_dir(file_directory_path() . '/simpletest_base')) { + simpletest_clean_temporary_directory(file_directory_path() . '/simpletest_base'); + } + + // Clear previous cache. + $tables = db_find_tables(Database::getActiveConnection()->prefixTables('{simpletest_base}') . '%'); + $ret = array(); + foreach ($tables as $table) { + db_drop_table($ret, $table); + } + + // Create base installation to be re-used. + $this->installDrupal('_base'); + + // Revert variables in preparation for testing instance installation. + variable_set('file_directory_path', $this->original_file_directory); + $db_prefix = $this->db_prefix_original; + + $simpletest_install_prefix = mt_rand(1000, 1000000); + variable_set('simpletest_install_cached', TRUE); + variable_set('simpletest_install_prefix', $simpletest_install_prefix); + + $this->installDrupal($simpletest_install_prefix, $modules); + return; + } + + $this->installDrupal(variable_get('simpletest_install_prefix', ''), $modules, TRUE); + } + + private function installDrupal($prefix, $modules = array(), $from_cache = FALSE) { + global $db_prefix; + // Store necessary current values before switching to prefixed database. $this->db_prefix_original = $db_prefix; $clean_url_original = variable_get('clean_url', 0); // Generate temporary prefixed database to ensure that tests have a clean starting point. - $db_prefix = Database::getActiveConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}'); + $db_prefix = Database::getActiveConnection()->prefixTables('{simpletest' . $prefix . '}'); - include_once DRUPAL_ROOT . '/includes/install.inc'; - drupal_install_system(); + require_once DRUPAL_ROOT . '/includes/install.inc'; + if ($from_cache) { + // TODO Copy files?. + // Copy tables from cache. + $db_prefix = Database::getActiveConnection()->prefixTables($this->db_prefix_original); + $tables = db_find_tables(Database::getActiveConnection()->prefixTables('{simpletest_base}') . '%'); + $db_prefix = Database::getActiveConnection()->prefixTables('{simpletest' . $prefix . '}'); + foreach ($tables as $table) { + $base_name = str_replace('simpletest_base', '', $table); + // Garbage to deal with uid = 0...bad. + if ($base_name == 'users') { + db_query('INSERT INTO {users} SELECT * FROM ' . $table . ' WHERE uid = 0'); + db_query('UPDATE {users} SET uid = uid - 1'); + db_query('INSERT INTO {users} SELECT * FROM ' . $table . ' WHERE uid > 0'); + continue; + } + db_query('INSERT INTO {' . $base_name . '} SELECT * FROM ' . $table); + } + } + else { + drupal_install_system(); + } // Add the specified modules to the list of modules in the default profile. - $args = func_get_args(); - $modules = array_unique(array_merge(drupal_get_profile_modules('default', 'en'), $args)); + if (!$from_cache) { + $modules = array_unique(array_merge(drupal_get_profile_modules('default', 'en'), $modules)); + } drupal_install_modules($modules); // Because the schema is static cached, we need to flush @@ -706,8 +762,10 @@ drupal_get_schema(NULL, TRUE); // Run default profile tasks. - $task = 'profile'; - default_profile_tasks($task, ''); + if (!$from_cache) { + $task = 'profile'; + default_profile_tasks($task, ''); + } // Rebuild caches. menu_rebuild(); @@ -761,7 +819,7 @@ $schema = drupal_get_schema(NULL, TRUE); $ret = array(); foreach ($schema as $name => $table) { - db_drop_table($ret, $name); + db_query('TRUNCATE {' . $name . '}'); } // Return the database prefix to the original. Index: modules/simpletest/simpletest.module =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.module,v retrieving revision 1.26 diff -u -r1.26 simpletest.module --- modules/simpletest/simpletest.module 1 Nov 2008 21:21:35 -0000 1.26 +++ modules/simpletest/simpletest.module 8 Nov 2008 09:04:44 -0000 @@ -342,6 +342,10 @@ * drupal being the default. */ function simpletest_run_tests($test_list, $reporter = 'drupal') { + // Force SimpleTest to create new cache during first test run. + variable_del('simpletest_install_cached'); + variable_del('simpletest_install_prefix'); + global $db_prefix, $db_prefix_original; cache_clear_all(); $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute(); @@ -521,7 +525,7 @@ foreach (array_diff_key($tables, $schema) as $table) { // Strip $db_prefix and skip tables without digits following "simpletest", // e.g. {simpletest_tets_id}. - if (preg_match('/simpletest\d+.*/', $table, $matches)) { + if (preg_match('/simpletest\d+.*/', $table, $matches) || preg_match('/simpletest_base.*/', $table, $matches)) { db_drop_table($ret, $matches[0]); } }