From c7d306e168c644f7bccddf622c343547d2bc07d7 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Wed, 17 Sep 2008 14:31:30 +0000 Subject: [PATCH] MDL-15666 MDL-16486 --- admin/index.php | 10 - admin/report/simpletest/index.php | 47 ++++- lib/adminlib.php | 181 ++++++++++-------- lib/ddl/simpletest/testddl.php | 2 +- lib/dml/simpletest/dbspecific.php | 2 +- lib/dml/simpletest/testdml.php | 2 +- lib/grade/simpletest/testgradegrades.php | 4 +- lib/simpletest/fixtures/gradetest.php | 31 +-- lib/simpletest/portfolio_testclass.php | 4 + lib/simpletest/testportfolioaddbutton.php | 15 +- lib/simpletestlib.php | 2 + mod/data/simpletest/testpreset.php | 2 +- .../test_glossary_portfolio_callers.php | 1 - 13 files changed, 165 insertions(+), 138 deletions(-) diff --git a/admin/index.php b/admin/index.php index db460af23d..a6c2b604ad 100644 --- a/admin/index.php +++ b/admin/index.php @@ -23,16 +23,6 @@ require_once($CFG->libdir.'/adminlib.php'); // Contains various admin-only functions $id = optional_param('id', '', PARAM_TEXT); - $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); - $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); - $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); - $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); - $autopilot = optional_param('autopilot', 0, PARAM_BOOL); - -/// set install/upgrade autocontinue session flag - if ($autopilot) { - $SESSION->installautopilot = $autopilot; - } /// Check some PHP server settings diff --git a/admin/report/simpletest/index.php b/admin/report/simpletest/index.php index e6a365ca88..a6f7e257ea 100644 --- a/admin/report/simpletest/index.php +++ b/admin/report/simpletest/index.php @@ -20,6 +20,7 @@ require_login(); require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); $langfile = 'simpletest'; +$unittest = true; // CGI arguments $path = optional_param('path', null, PARAM_PATH); @@ -29,6 +30,7 @@ $rundbtests = optional_param('rundbtests', false, PARAM_BOOL); $thorough = optional_param('thorough', false, PARAM_BOOL); $addconfigprefix = optional_param('addconfigprefix', false, PARAM_RAW); $setuptesttables = optional_param('setuptesttables', false, PARAM_BOOL); +$continuesetuptesttables = optional_param('continuesetuptesttables', false, PARAM_BOOL); $droptesttables = optional_param('droptesttables', false, PARAM_BOOL); global $UNITTEST; @@ -85,21 +87,52 @@ if (empty($CFG->unittest_prefix)) { exit(); } +// Temporarily override $DB and $CFG for a fresh install on the unit test prefix $real_db = clone($DB); +$real_cfg = clone($CFG); +$CFG = new stdClass(); +$CFG->dbhost = $real_cfg->dbhost; +$CFG->dbtype = $real_cfg->dbtype; +$CFG->dblibrary = $real_cfg->dblibrary; +$CFG->dbuser = $real_cfg->dbuser; +$CFG->dbpass = $real_cfg->dbpass; +$CFG->dbname = $real_cfg->dbname; +$CFG->dbpersist = $real_cfg->dbpersist; +$CFG->unittest_prefix = $real_cfg->unittest_prefix; +$CFG->wwwroot = $real_cfg->wwwroot; +$CFG->dirroot = $real_cfg->dirroot; +$CFG->libdir = $real_cfg->libdir; +$CFG->dataroot = $real_cfg->dataroot; +$CFG->admin = $real_cfg->admin; +$CFG->release = $real_cfg->release; +$CFG->config_php_settings = $real_cfg->config_php_settings; +$CFG->frametarget = $real_cfg->frametarget; +$CFG->footer = $real_cfg->footer; + $DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary); $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->unittest_prefix); + +if ($config = $DB->get_records('config')) { + foreach ($config as $conf) { + $CFG->{$conf->name} = $conf->value; + } +} + $test_tables = $DB->get_tables(); // Build test tables if requested and needed -if ($setuptesttables) { +if ($setuptesttables || $continuesetuptesttables) { $version = null; $release = null; include("$CFG->dirroot/version.php"); // defines $version and $release - // Drop all tables first if they exist - $manager = $DB->get_manager(); - foreach ($test_tables as $table) { - $manager->drop_table($table); + if (!$continuesetuptesttables) { + // Drop all tables first if they exist + $manager = $DB->get_manager(); + foreach ($test_tables as $table) { + $xmldbtable = new xmldb_table($table); + $manager->drop_table($xmldbtable); + } } upgrade_db($version, $release, true); @@ -108,7 +141,8 @@ if ($setuptesttables) { if ($droptesttables) { $manager = $DB->get_manager(); foreach ($test_tables as $table) { - $manager->drop_table($table); + $xmldbtable = new xmldb_table($table); + $manager->drop_table($xmldbtable); } $test_tables = $DB->get_tables(); } @@ -122,6 +156,7 @@ if (empty($test_tables['config'])) { } $DB = $real_db; +$CFG = $real_cfg; if (!is_null($path)) { // Create the group of tests. diff --git a/lib/adminlib.php b/lib/adminlib.php index a2d13859e2..ad6ef31142 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -29,8 +29,12 @@ define('INSECURE_DATAROOT_ERROR', 2); * @param string $release * @param bool $unittest If true, bypasses a bunch of confirmation screens */ -function upgrade_db($version, $release, $unittest=false) { - global $CFG, $DB, $SESSION; +function upgrade_db($version, $release) { + global $CFG, $DB, $SESSION, $unittest; + + if (empty($unittest)) { + $unittest = false; + } $confirmupgrade = optional_param('confirmupgrade', $unittest, PARAM_BOOL); $confirmrelease = optional_param('confirmrelease', $unittest, PARAM_BOOL); @@ -38,10 +42,16 @@ function upgrade_db($version, $release, $unittest=false) { $agreelicense = optional_param('agreelicense', $unittest, PARAM_BOOL); $autopilot = optional_param('autopilot', $unittest, PARAM_BOOL); $setuptesttables= optional_param('setuptesttables', $unittest, PARAM_BOOL); + $continuesetuptesttables= optional_param('continuesetuptesttables', $unittest, PARAM_BOOL); $return_url = "$CFG->wwwroot/$CFG->admin/index.php"; if ($unittest) { - $return_url = "$CFG->wwwroot/$CFG->admin/report/simpletest/index.php"; + $return_url = "$CFG->wwwroot/$CFG->admin/report/simpletest/index.php?continuesetuptesttables=".$continuesetuptesttables; + } + + /// set install/upgrade autocontinue session flag + if ($autopilot) { + $SESSION->installautopilot = $autopilot; } /// Check if the main tables have been installed yet or not. @@ -144,6 +154,7 @@ function upgrade_db($version, $release, $unittest=false) { print_error('cannotupdateversion', 'debug'); } + // Write default settings unconditionally (i.e. even if a setting is already set, overwrite it) // (this should only have any effect during initial install). admin_apply_default_settings(NULL, true); @@ -156,7 +167,7 @@ function upgrade_db($version, $release, $unittest=false) { // hack - set up mnet require_once $CFG->dirroot.'/mnet/lib.php'; - print_continue('index.php'); + print_continue('index.php?continuesetuptesttables='.$setuptesttables); print_footer('none'); die; @@ -405,86 +416,100 @@ function upgrade_db($version, $release, $unittest=false) { // Turn xmlstrictheaders back on now. $CFG->xmlstrictheaders = $origxmlstrictheaders; -/// Set up the blank site - to be customized later at the end of install. - if (! $site = get_site()) { - // We are about to create the site "course" - require_once($CFG->libdir.'/blocklib.php'); - - $newsite = new object(); - $newsite->fullname = ""; - $newsite->shortname = ""; - $newsite->summary = NULL; - $newsite->newsitems = 3; - $newsite->numsections = 0; - $newsite->category = 0; - $newsite->format = 'site'; // Only for this course - $newsite->teacher = get_string("defaultcourseteacher"); - $newsite->teachers = get_string("defaultcourseteachers"); - $newsite->student = get_string("defaultcoursestudent"); - $newsite->students = get_string("defaultcoursestudents"); - $newsite->timemodified = time(); - - if (!$newid = $DB->insert_record('course', $newsite)) { - print_error('cannotsetupsite', 'error'); - } - // make sure course context exists - get_context_instance(CONTEXT_COURSE, $newid); - - // Site created, add blocks for it - $page = page_create_object(PAGE_COURSE_VIEW, $newid); - blocks_repopulate_page($page); // Return value not checked because you can always edit later - - // create default course category - $cat = get_course_category(); - - redirect('index.php'); - } - - // initialise default blocks on admin and site page if needed - if (empty($CFG->adminblocks_initialised)) { - require_once("$CFG->dirroot/$CFG->admin/pagelib.php"); - require_once($CFG->libdir.'/blocklib.php'); - page_map_class(PAGE_ADMIN, 'page_admin'); - $page = page_create_object(PAGE_ADMIN, 0); // there must be some id number - blocks_repopulate_page($page); - - //add admin_tree block to site if not already present - if ($admintree = $DB->get_record('block', array('name'=>'admin_tree'))) { - $page = page_create_object(PAGE_COURSE_VIEW, SITEID); - $pageblocks=blocks_get_by_page($page); - blocks_execute_action($page, $pageblocks, 'add', (int)$admintree->id, false, false); - if ($admintreeinstance = $DB->get_record('block_instance', array('pagetype'=>$page->type, 'pageid'=>SITEID, 'blockid'=>$admintree->id))) { + if (!$unittest) { + /// Set up the blank site - to be customized later at the end of install. + if (! $site = get_site()) { + build_course_site(); + redirect('index.php?continuesetuptesttables='.$continuesetuptesttables); + } + + // initialise default blocks on admin and site page if needed + if (empty($CFG->adminblocks_initialised)) { + require_once("$CFG->dirroot/$CFG->admin/pagelib.php"); + require_once($CFG->libdir.'/blocklib.php'); + page_map_class(PAGE_ADMIN, 'page_admin'); + $page = page_create_object(PAGE_ADMIN, 0); // there must be some id number + blocks_repopulate_page($page); + + //add admin_tree block to site if not already present + if ($admintree = $DB->get_record('block', array('name'=>'admin_tree'))) { + $page = page_create_object(PAGE_COURSE_VIEW, SITEID); $pageblocks=blocks_get_by_page($page); - blocks_execute_action($page, $pageblocks, 'moveleft', $admintreeinstance, false, false); + blocks_execute_action($page, $pageblocks, 'add', (int)$admintree->id, false, false); + if ($admintreeinstance = $DB->get_record('block_instance', array('pagetype'=>$page->type, 'pageid'=>SITEID, 'blockid'=>$admintree->id))) { + $pageblocks=blocks_get_by_page($page); + blocks_execute_action($page, $pageblocks, 'moveleft', $admintreeinstance, false, false); + } } - } - set_config('adminblocks_initialised', 1); - } + set_config('adminblocks_initialised', 1); + } -/// Define the unique site ID code if it isn't already - if (empty($CFG->siteidentifier)) { // Unique site identification code - set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']); - } + /// Define the unique site ID code if it isn't already + if (empty($CFG->siteidentifier)) { // Unique site identification code + set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']); + } -/// ugly hack - if mnet is not initialised include the mnet lib, it adds needed mnet records and configures config options -/// we should not do such crazy stuff in lib functions!!! - if (empty($CFG->mnet_localhost_id)) { - require_once $CFG->dirroot.'/mnet/lib.php'; - } + /// ugly hack - if mnet is not initialised include the mnet lib, it adds needed mnet records and configures config options + /// we should not do such crazy stuff in lib functions!!! + if (empty($CFG->mnet_localhost_id)) { + require_once $CFG->dirroot.'/mnet/lib.php'; + } -/// Check if the guest user exists. If not, create one. - if (!$DB->record_exists('user', array('username'=>'guest'))) { - if (! $guest = create_guest_record()) { - notify("Could not create guest user record !!!"); + /// Check if the guest user exists. If not, create one. + if (!$DB->record_exists('user', array('username'=>'guest'))) { + if (! $guest = create_guest_record()) { + notify("Could not create guest user record !!!"); + } } - } -/// Set up the admin user - if (empty($CFG->rolesactive)) { - build_context_path(); // just in case - should not be needed + /// Set up the admin user + if (empty($CFG->rolesactive)) { + build_context_path(); // just in case - should not be needed + create_admin_user(); + } + } else { + build_site_course(); + create_guest_record(); create_admin_user(); + redirect($return_url); } +} + +function build_site_course() { + global $CFG, $DB; + + $continuesetuptesttables= optional_param('continuesetuptesttables', $unittest, PARAM_BOOL); + + // We are about to create the site "course" + require_once($CFG->libdir.'/blocklib.php'); + + $newsite = new object(); + $newsite->fullname = ""; + $newsite->shortname = ""; + $newsite->summary = NULL; + $newsite->newsitems = 3; + $newsite->numsections = 0; + $newsite->category = 0; + $newsite->format = 'site'; // Only for this course + $newsite->teacher = get_string("defaultcourseteacher"); + $newsite->teachers = get_string("defaultcourseteachers"); + $newsite->student = get_string("defaultcoursestudent"); + $newsite->students = get_string("defaultcoursestudents"); + $newsite->timemodified = time(); + + if (!$newid = $DB->insert_record('course', $newsite)) { + print_error('cannotsetupsite', 'error'); + } + // make sure course context exists + get_context_instance(CONTEXT_COURSE, $newid); + + // Site created, add blocks for it + $page = page_create_object(PAGE_COURSE_VIEW, $newid); + blocks_repopulate_page($page); // Return value not checked because you can always edit later + + // create default course category + $cat = get_course_category(); } @@ -965,7 +990,7 @@ function upgrade_plugins($type, $dir, $return) { */ function upgrade_activity_modules($return) { - global $CFG, $interactive, $DB; + global $CFG, $interactive, $DB, $unittest; if (!$mods = get_list_of_plugins('mod') ) { print_error('nomodules', 'debug'); @@ -1095,7 +1120,7 @@ function upgrade_activity_modules($return) { } else { // module not installed yet, so install it if (!$updated_modules) { - if (!defined('CLI_UPGRADE') || !CLI_UPGRADE ) { + if ((!defined('CLI_UPGRADE') || !CLI_UPGRADE) && !$unittest) { print_header($strmodulesetup, $strmodulesetup, build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '', upgrade_get_javascript(), false, ' ', ' '); @@ -1386,7 +1411,7 @@ function create_admin_user($user_input=NULL) { * The upgrade is finished at the end of script or after timeout. */ function start_upgrade() { - global $CFG; + global $CFG, $DB; static $started = false; @@ -1396,7 +1421,7 @@ function start_upgrade() { } else { ignore_user_abort(true); register_shutdown_function('upgrade_finished_handler'); - if ($CFG->version === '') { + if ($CFG->version === '' || !$DB->get_manager()->table_exists(new xmldb_table('config'))) { // db not installed yet $CFG->upgraderunning = time()+300; } else { diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index da8f28caf6..462379c81f 100755 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -11,7 +11,7 @@ if (!defined('MOODLE_INTERNAL')) { require_once($CFG->libdir . '/adminlib.php'); -class ddl_test extends MoodleUnitTestCase { +class ddl_test extends UnitTestCase { private $tables = array(); private $tdb; diff --git a/lib/dml/simpletest/dbspecific.php b/lib/dml/simpletest/dbspecific.php index a3fe7d2e9b..e403d44705 100644 --- a/lib/dml/simpletest/dbspecific.php +++ b/lib/dml/simpletest/dbspecific.php @@ -1,6 +1,6 @@ assertTrue(method_exists($grade_grade, 'insert')); $grade_grade->itemid = $this->grade_items[0]->id; - $grade_grade->userid = 1; + $grade_grade->userid = 10; $grade_grade->rawgrade = 88; $grade_grade->rawgrademax = 110; $grade_grade->rawgrademin = 18; @@ -117,7 +117,7 @@ class grade_grade_test extends grade_test { $this->assertEqual(40, grade_grade::standardise_score(50, 30, 80, 0, 100)); } - + /* * Disabling this test: the set_locked() arguments have been modified, rendering these tests useless until they are re-written diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php index 06f75d3476..eee7c51533 100644 --- a/lib/simpletest/fixtures/gradetest.php +++ b/lib/simpletest/fixtures/gradetest.php @@ -51,18 +51,13 @@ Mock::generate('grade_outcome', 'mock_grade_outcome'); */ class grade_test extends MoodleUnitTestCase { - /** - * Each database table receives a number of test entries. These are saved as - * arrays of stcClass objects available to this class. This means that - * every test has access to these test data. The order of the following array is - * crucial, because of the interrelationships between objects. - */ - public $tables = array('grade_categories', + public $grade_tables = array('grade_categories', 'scale', 'grade_items', 'grade_grades', 'grade_outcomes'); + public $grade_items = array(); public $grade_categories = array(); public $grade_grades = array(); @@ -72,9 +67,6 @@ class grade_test extends MoodleUnitTestCase { public $activities = array(); public $courseid = 1; public $userid = 1; - - public static $db = null; - public $realdb; public $dbmanager; /** @@ -82,18 +74,10 @@ class grade_test extends MoodleUnitTestCase { * These tests have to work on a brand new site. */ function setUp() { - // Set global category settings to -1 (not force) global $CFG, $DB; - - if (is_null(grade_test::$db)) { - $this->realdb = $DB; - grade_test::$db = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary); - grade_test::$db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, "tst_", $CFG->dboptions); - } - - $DB = grade_test::$db; $this->dbmanager = $DB->get_manager(); + parent::setup(); $CFG->grade_droplow = -1; $CFG->grade_keephigh = -1; $CFG->grade_aggregation = -1; @@ -109,7 +93,7 @@ class grade_test extends MoodleUnitTestCase { die("Could not create all the test tables!"); } - foreach ($this->tables as $table) { + foreach ($this->grade_tables as $table) { $function = "load_$table"; $this->$function(); } @@ -632,11 +616,10 @@ class grade_test extends MoodleUnitTestCase { */ function tearDown() { // delete the contents of tables before the test run - the unit test might fail on fatal error and the data would not be deleted! - foreach ($this->tables as $table) { + foreach ($this->grade_tables as $table) { unset($this->$table); } - global $DB; - $DB = $this->realdb; + parent::tearDown(); } /** @@ -1377,7 +1360,7 @@ class grade_test extends MoodleUnitTestCase { } $grade = new stdClass(); - $grade->itemid = $this->grade_items[7]->id; + $grade->itemid = $this->grade_items[8]->id; $grade->userid = 3; $grade->rawgrade = 100; $grade->finalgrade = 100; diff --git a/lib/simpletest/portfolio_testclass.php b/lib/simpletest/portfolio_testclass.php index 62dc5af835..a6133a31ec 100644 --- a/lib/simpletest/portfolio_testclass.php +++ b/lib/simpletest/portfolio_testclass.php @@ -189,6 +189,10 @@ require_once($CFG->dirroot . '/admin/generator.php'); class portfoliolib_test extends MoodleUnitTestCase { + function setup() { + parent::setup(); + } + function test_construct_dupe_instance() { $gotexception = false; try { diff --git a/lib/simpletest/testportfolioaddbutton.php b/lib/simpletest/testportfolioaddbutton.php index 4a46de96cd..f5d73fdf5a 100644 --- a/lib/simpletest/testportfolioaddbutton.php +++ b/lib/simpletest/testportfolioaddbutton.php @@ -37,21 +37,10 @@ if (!defined('MOODLE_INTERNAL')) { require_once($CFG->libdir . '/portfoliolib.php'); -class portfoliolibaddbutton_test extends MoodleMoodleUnitTestCase { - - function setUp() { - global $DB, $CFG; - } - - function tearDown() { - global $DB; - } - - function test_addbutton() { - - } +class portfoliolibaddbutton_test extends MoodleUnitTestCase { function test_set_formats() { + $button = new portfolio_add_button(); $button->set_callback_options('assignment_portfolio_caller', array('id' => 6), '/mod/assignment/lib.php'); $formats = array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_IMAGE); diff --git a/lib/simpletestlib.php b/lib/simpletestlib.php index 4cc713d463..7af88779d2 100644 --- a/lib/simpletestlib.php +++ b/lib/simpletestlib.php @@ -172,6 +172,8 @@ class MoodleUnitTestCase extends UnitTestCase { $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->unittest_prefix); $manager = $DB->get_manager(); + $tables = $DB->get_tables(); + if (!$manager->table_exists('user')) { print_error('tablesnotsetup', 'simpletest'); } diff --git a/mod/data/simpletest/testpreset.php b/mod/data/simpletest/testpreset.php index dd6cb95838..263ccf9d63 100644 --- a/mod/data/simpletest/testpreset.php +++ b/mod/data/simpletest/testpreset.php @@ -35,7 +35,7 @@ if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page } -require_once($CFG->dirroot . '/mod/data/preset_class.php'); +require_once($CFG->dirroot . '/mod/data/lib.php'); class data_preset_test extends MoodleUnitTestCase { diff --git a/mod/glossary/simpletest/test_glossary_portfolio_callers.php b/mod/glossary/simpletest/test_glossary_portfolio_callers.php index 01e4423085..c8f255eecb 100644 --- a/mod/glossary/simpletest/test_glossary_portfolio_callers.php +++ b/mod/glossary/simpletest/test_glossary_portfolio_callers.php @@ -18,7 +18,6 @@ class testGlossaryPortfolioCallers extends portfoliolib_test { global $DB; parent::setUp(); - $settings = array('tiny' => 1, 'quiet' => 1, 'pre_cleanup' => 1, 'modules_list' => array('glossary'), 'entries_per_glossary' => 20, 'number_of_students' => 5, 'students_per_course' => 5, 'number_of_sections' => 1, -- 2.39.5