Unit tests MDL-24842: Fixed unit tests for accesslib, added ability to switch CFG in test baseclass

This commit is contained in:
Sam Marshall
2010-10-25 14:43:59 +00:00
parent 9c670df6db
commit 2cc9bae531
2 changed files with 78 additions and 18 deletions
+44
View File
@@ -427,6 +427,9 @@ class UnitTestCaseUsingDatabase extends UnitTestCase {
private $realuserid = null;
private $tables = array();
private $realcfg;
protected $testcfg;
public function __construct($label = false) {
global $DB, $CFG;
@@ -442,6 +445,19 @@ class UnitTestCaseUsingDatabase extends UnitTestCase {
$this->realdb = $DB;
$this->testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
$this->testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix);
// Set up test config
$this->testcfg = (object)array(
'testcfg' => true, // Marker that this is a test config
'libdir' => $CFG->libdir, // Must use real one so require_once works
'dirroot' => $CFG->dirroot, // Must use real one
'dataroot' => $CFG->dataroot, // Use real one for now (maybe this should change?)
'ostype' => $CFG->ostype, // Real one
'wwwroot' => 'http://www.example.org', // Use fixed url
'siteadmins' => '0', // No admins
'siteguest' => '0' // No guest
);
$this->realcfg = $CFG;
}
/**
@@ -466,6 +482,28 @@ class UnitTestCaseUsingDatabase extends UnitTestCase {
$DB = $this->realdb;
}
/**
* Switch to using the test $CFG for all queries until further notice.
*/
protected function switch_to_test_cfg() {
global $CFG;
if (isset($CFG->testcfg)) {
debugging('switch_to_test_cfg called when the test CFG was already selected. This suggest you are doing something wrong and dangerous. Please review your code immediately.', DEBUG_DEVELOPER);
}
$CFG = $this->testcfg;
}
/**
* Revert to using the real $CFG for all future queries.
*/
protected function revert_to_real_cfg() {
global $CFG;
if (!isset($CFG->testcfg)) {
debugging('revert_to_real_cfg called when the test CFG was not already selected. This suggest you are doing something wrong and dangerous. Please review your code immediately.', DEBUG_DEVELOPER);
}
$CFG = $this->realcfg;
}
/**
* Switch $USER->id to a test value.
*
@@ -514,6 +552,12 @@ class UnitTestCaseUsingDatabase extends UnitTestCase {
$cleanmore = true;
}
// Switch back to the real CFG if necessary.
if (isset($CFG->testcfg)) {
$this->revert_to_real_cfg();
$cleanmore = true;
}
// revert_global_user_id if necessary.
if (!is_null($this->realuserid)) {
$this->revert_global_user_id();