From a230012cd82a5d93797940c62321d6ce323c6b39 Mon Sep 17 00:00:00 2001 From: skodak Date: Fri, 13 Jun 2008 08:35:29 +0000 Subject: [PATCH] MDL-14905 towards functional DB tests --- admin/report/simpletest/dbtest.php | 17 ++++++++-------- admin/report/simpletest/ex_simple_test.php | 9 +++++++++ admin/report/simpletest/index.php | 3 +++ lib/ddl/simpletest/testddllib.php | 6 +++--- lib/deprecatedlib.php | 4 ++-- lib/dml/simpletest/testdmllib.php | 23 ++++++++++++++-------- lib/weblib.php | 5 ++--- 7 files changed, 42 insertions(+), 25 deletions(-) diff --git a/admin/report/simpletest/dbtest.php b/admin/report/simpletest/dbtest.php index 730731dd5e..86ded3028b 100644 --- a/admin/report/simpletest/dbtest.php +++ b/admin/report/simpletest/dbtest.php @@ -17,6 +17,9 @@ require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); $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++) { @@ -63,32 +66,28 @@ for ($i=1; $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 '
'; } diff --git a/admin/report/simpletest/ex_simple_test.php b/admin/report/simpletest/ex_simple_test.php index c6732e9d4f..49093e6b29 100644 --- a/admin/report/simpletest/ex_simple_test.php +++ b/admin/report/simpletest/ex_simple_test.php @@ -33,6 +33,15 @@ class AutoGroupTest extends GroupTest { $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; diff --git a/admin/report/simpletest/index.php b/admin/report/simpletest/index.php index bdcd7481c6..a03628b8e4 100644 --- a/admin/report/simpletest/index.php +++ b/admin/report/simpletest/index.php @@ -28,6 +28,9 @@ $showsearch = optional_param('showsearch', false, PARAM_BOOL); $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); diff --git a/lib/ddl/simpletest/testddllib.php b/lib/ddl/simpletest/testddllib.php index 0311d35169..f56f21077b 100755 --- a/lib/ddl/simpletest/testddllib.php +++ b/lib/ddl/simpletest/testddllib.php @@ -16,10 +16,10 @@ class ddllib_test extends UnitTestCase { 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; } diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 36c07033e4..d5294b8f04 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -437,14 +437,14 @@ function get_current_group($courseid, $full = false) { * @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.']' : '')); diff --git a/lib/dml/simpletest/testdmllib.php b/lib/dml/simpletest/testdmllib.php index ca8a849812..d94f571c27 100755 --- a/lib/dml/simpletest/testdmllib.php +++ b/lib/dml/simpletest/testdmllib.php @@ -13,10 +13,10 @@ class dmllib_test extends UnitTestCase { 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; } @@ -71,6 +71,7 @@ class dmllib_test extends UnitTestCase { 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]"; @@ -183,14 +184,17 @@ class dmllib_test extends UnitTestCase { } 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); @@ -211,7 +215,8 @@ class dmllib_test extends UnitTestCase { } 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(); @@ -231,7 +236,9 @@ class dmllib_test extends UnitTestCase { } 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)); diff --git a/lib/weblib.php b/lib/weblib.php index e54e0f0e43..4d905eb187 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -5601,8 +5601,7 @@ function print_scale_menu_helpbutton($courseid, $scale, $return=false) { * @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'; @@ -5617,7 +5616,7 @@ function print_error ($errorcode, $module='', $link='', $a=NULL) { /** * 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.']' : '')); -- 2.39.5