$showpasses = optional_param('showpasses', 0, PARAM_BOOL);
$selected = optional_param('selected', array(), PARAM_INT);
+global $UNITTEST;
+$UNITTEST = new object();
+
if (!data_submitted()) {
$selected = array();
for ($i=0; $i<=10; $i++) {
}
if (!empty($tests)) {
- /* The UNITTEST constant can be checked elsewhere if you need to know
- * when your code is being run as part of a unit test. */
- define('UNITTEST', true);
-
@ob_implicit_flush(true);
while(@ob_end_flush());
- global $FUNCT_TEST_DB; // hack - we pass the connected db to functional database tests through this global
-
foreach ($tests as $i=>$database) {
$dbinfo = $dbinfos[$i];
- $FUNCT_TEST_DB = $database;
+ $UNITTEST->func_test_db = $database; // pass the db to the tests through global
print_heading('Running tests on: '.$dbinfo['name'], '', 3); // TODO: localise
// Create the group of tests.
$test = new AutoGroupTest(false, true);
- $test->addTestFile($CFG->libdir . '/dml/simpletest/testdmllib.php');
- $test->addTestFile($CFG->libdir . '/ddl/simpletest/testddllib.php');
+ $test->addTestFile($CFG->libdir.'/dml/simpletest/testdmllib.php');
+ $test->addTestFile($CFG->libdir.'/ddl/simpletest/testddllib.php');
// Make the reporter, which is what displays the results.
$reporter = new ExHtmlReporter($showpasses);
$test->run($reporter);
+ unset($UNITTEST->func_test_db);
+
echo '<hr />';
}
$this->thorough = $thorough;
}
+ function run(&$reporter) {
+ global $UNITTEST;
+
+ $UNITTEST->running = true;
+ $return = parent::run($reporter);
+ unset($UNITTEST->running);
+ return $return;
+ }
+
function setLabel($test_name) {
//:HACK: there is no GroupTest::setLabel, so access parent::_label.
$this->_label = $test_name;
$rundbtests = optional_param('rundbtests', false, PARAM_BOOL);
$thorough = optional_param('thorough', false, PARAM_BOOL);
+global $UNITTEST;
+$UNITTEST = new object();
+
// Print the header.
admin_externalpage_setup('reportsimpletest');
$strtitle = get_string('unittests', $langfile);
private $db;
public function setUp() {
- global $CFG, $DB, $FUNCT_TEST_DB;
+ global $CFG, $DB, $UNITTEST;
- if (isset($FUNCT_TEST_DB)) {
- $this->db = $FUNCT_TEST_DB;
+ if (isset($UNITTEST->func_test_db)) {
+ $this->db = $UNITTEST->func_test_db;
} else {
$this->db = $DB;
}
* @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page.
*/
function error($message, $link='') {
+ global $CFG, $SESSION, $THEME, $UNITTEST;
- global $CFG, $SESSION, $THEME;
$message = clean_text($message); // In case nasties are in here
/**
* TODO VERY DIRTY HACK USED FOR UNIT TESTING UNTIL PROPER EXCEPTION HANDLING IS IMPLEMENTED
*/
- if (defined('UNITTEST')) {
+ if (!empty($UNITTEST->running)) {
// Errors in unit test become exceptions, so you can unit test
// code that might call error().
throw new Exception('error() call: '. $message.($link!=='' ? ' ['.$link.']' : ''));
private $db;
function setUp() {
- global $CFG, $DB, $FUNCT_TEST_DB;
+ global $CFG, $DB, $UNITTEST;
- if (isset($FUNCT_TEST_DB)) {
- $this->db = $FUNCT_TEST_DB;
+ if (isset($UNITTEST->func_test_db)) {
+ $this->db = $UNITTEST->func_test_db;
} else {
$this->db = $DB;
}
function test_fix_sql_params() {
$DB = $this->db; // do not use global $DB!
+ $dbmanager = $this->db->get_manager();
// Malformed table placeholder
$sql = "SELECT * FROM [testtable]";
}
public function testGetTables() {
- global $DB;
+ $DB = $this->db; // do not use global $DB!
+ $dbmanager = $this->db->get_manager();
+
// Need to test with multiple DBs
$this->assertTrue($DB->get_tables() > 2);
}
public function testGetIndexes() {
- global $DB;
- // Need to test with multiple DBs
+ $DB = $this->db; // do not use global $DB!
+ $dbmanager = $this->db->get_manager();
+
$this->assertTrue($indices = $DB->get_indexes('testtable'));
$this->assertTrue(count($indices) == 1);
}
public function testGetColumns() {
- global $DB;
+ $DB = $this->db; // do not use global $DB!
+ $dbmanager = $this->db->get_manager();
$this->assertTrue($columns = $DB->get_columns('testtable'));
$fields = $this->tables['testtable']->getFields();
}
public function testExecute() {
- global $DB;
+ $DB = $this->db; // do not use global $DB!
+ $dbmanager = $this->db->get_manager();
+
$sql = "SELECT * FROM {testtable}";
$this->assertTrue($DB->execute($sql));
* @return terminates script, does not return!
*/
function print_error ($errorcode, $module='', $link='', $a=NULL) {
-
- global $CFG, $SESSION, $THEME;
+ global $CFG, $SESSION, $THEME, $UNITTEST;
if (empty($module) || $module == 'moodle' || $module == 'core') {
$module = 'error';
/**
* TODO VERY DIRTY HACK USED FOR UNIT TESTING UNTIL PROPER EXCEPTION HANDLING IS IMPLEMENTED
*/
- if (defined('UNITTEST')) {
+ if (!empty($UNITTEST->running)) {
// Errors in unit test become exceptions, so you can unit test
// code that might call error().
throw new Exception('error() call: '. $message.($link!=='' ? ' ['.$link.']' : ''));