/** * Perform a fresh test site installation * * Note: To be used from CLI scripts only. * * @static * @return void may terminate execution with exit code */ public static function install_site() { global $DB, $CFG; if (!self::is_test_site()) { phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!'); } if ($DB->get_tables()) { list($errorcode, $message) = phpunit_util::testing_ready_problem(); if ($errorcode) { phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised'); } else { phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised'); } } $options = array(); $options['adminpass'] = '******'; $options['shortname'] = 'phpunit'; $options['fullname'] = 'PHPUnit test site'; install_cli_database($options, false); // install timezone info $timezones = get_records_csv($CFG->libdir . '/timezone.txt', 'timezone'); update_timezone_records($timezones); // add test db flag $hash = phpunit_util::get_version_hash(); set_config('phpunittest', $hash); // store data for all tables $data = array(); $structure = array(); $tables = $DB->get_tables(); foreach ($tables as $table) { $columns = $DB->get_columns($table); $structure[$table] = $columns; if (isset($columns['id']) and $columns['id']->auto_increment) { $data[$table] = $DB->get_records($table, array(), 'id ASC'); } else { // there should not be many of these $data[$table] = $DB->get_records($table, array()); } } $data = serialize($data); file_put_contents("{$CFG->dataroot}/phpunit/tabledata.ser", $data); phpunit_boostrap_fix_file_permissions("{$CFG->dataroot}/phpunit/tabledata.ser"); $structure = serialize($structure); file_put_contents("{$CFG->dataroot}/phpunit/tablestructure.ser", $structure); phpunit_boostrap_fix_file_permissions("{$CFG->dataroot}/phpunit/tablestructure.ser"); // hash all plugin versions - helps with very fast detection of db structure changes file_put_contents("{$CFG->dataroot}/phpunit/versionshash.txt", $hash); phpunit_boostrap_fix_file_permissions("{$CFG->dataroot}/phpunit/versionshash.txt", $hash); }
/** * Perform a fresh test site installation * * Note: To be used from CLI scripts only. * * @static * @return void may terminate execution with exit code */ public static function install_site() { global $DB, $CFG; if (!self::is_test_site()) { cli_error('Can not install non-test sites!!', 131); } if ($DB->get_tables()) { cli_error('Database tables already installed, drop the site first.', 133); } $options = array(); $options['adminpass'] = '******'; // removed later $options['shortname'] = 'phpunit'; $options['fullname'] = 'PHPUnit test site'; install_cli_database($options, false); // just in case remove admin password so that normal login is not possible $DB->set_field('user', 'password', 'not cached', array('username' => 'admin')); // add test db flag set_config('phpunittest', 'phpunittest'); // store data for all tables $data = array(); $tables = $DB->get_tables(); foreach ($tables as $table) { $data[$table] = $DB->get_records($table, array()); } $data = serialize($data); @unlink("{$CFG->dataroot}/phpunit/tabledata.ser"); file_put_contents("{$CFG->dataroot}/phpunit/tabledata.ser", $data); // hash all plugin versions - helps with very fast detection of db structure changes $hash = phpunit_util::get_version_hash(); @unlink("{$CFG->dataroot}/phpunit/versionshash.txt"); file_put_contents("{$CFG->dataroot}/phpunit/versionshash.txt", $hash); }