/** * Drop all test site data. * * Note: To be used from CLI scripts only. * * @static * @return void may terminate execution with exit code */ public static function drop_site() { global $DB, $CFG; if (!self::is_test_site()) { phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!'); } // purge dataroot self::reset_dataroot(); phpunit_bootstrap_initdataroot($CFG->dataroot); $keep = array('.', '..', 'lock', 'webrunner.xml'); $files = scandir("{$CFG->dataroot}/phpunit"); foreach ($files as $file) { if (in_array($file, $keep)) { continue; } $path = "{$CFG->dataroot}/phpunit/{$file}"; if (is_dir($path)) { remove_dir($path, false); } else { unlink($path); } } // drop all tables $tables = $DB->get_tables(false); if (isset($tables['config'])) { // config always last to prevent problems with interrupted drops! unset($tables['config']); $tables['config'] = 'config'; } foreach ($tables as $tablename) { $table = new xmldb_table($tablename); $DB->get_manager()->drop_table($table); } }
} } if (!file_exists("{$CFG->phpunit_dataroot}/phpunittestdir.txt")) { if ($dh = opendir($CFG->phpunit_dataroot)) { while (($file = readdir($dh)) !== false) { if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') { continue; } phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?'); } closedir($dh); unset($dh); unset($file); } // now we are 100% sure this dir is used only for phpunit tests phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot); } // verify db prefix if (!isset($CFG->phpunit_prefix)) { phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!'); } if ($CFG->phpunit_prefix === '') { phpunit_bootstrap_error(131, '$CFG->phpunit_prefix can not be empty, can not run tests!'); } if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) { phpunit_bootstrap_error(131, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!'); } // override CFG settings if necessary and throw away extra CFG settings $CFG->dataroot = $CFG->phpunit_dataroot; $CFG->prefix = $CFG->phpunit_prefix; $CFG->dbtype = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
/** * Drop all test site data. * * Note: To be used from CLI scripts only. * * @static * @return void may terminate execution with exit code */ public static function drop_site() { global $DB, $CFG; if (!self::is_test_site()) { cli_error('Can not drop non-test sites!!', 131); } // drop dataroot remove_dir($CFG->dataroot, true); phpunit_bootstrap_initdataroot($CFG->dataroot); // drop all tables $trans = $DB->start_delegated_transaction(); $tables = $DB->get_tables(false); foreach ($tables as $tablename) { $DB->delete_records($tablename, array()); } $trans->allow_commit(); // now drop them foreach ($tables as $tablename) { $table = new xmldb_table($tablename); $DB->get_manager()->drop_table($table); } }