]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14905 towards functional DB tests
authorskodak <skodak>
Fri, 13 Jun 2008 08:35:29 +0000 (08:35 +0000)
committerskodak <skodak>
Fri, 13 Jun 2008 08:35:29 +0000 (08:35 +0000)
admin/report/simpletest/dbtest.php
admin/report/simpletest/ex_simple_test.php
admin/report/simpletest/index.php
lib/ddl/simpletest/testddllib.php
lib/deprecatedlib.php
lib/dml/simpletest/testdmllib.php
lib/weblib.php

index 730731dd5e57a5faa463bb8d6fdba692eee8dccf..86ded3028b68d3c581190860c2db53d2d9ec0666 100644 (file)
@@ -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 '<hr />';
     }
 
index c6732e9d4f04c8054640a638fe96cfc20f88ba3a..49093e6b29ee04573c90c09589ee87f1aaf421f5 100644 (file)
@@ -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;
index bdcd7481c6145460b06c58519fadbc58f3349ed4..a03628b8e4fde03fb9a540dd4270400e60d02ecd 100644 (file)
@@ -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);
index 0311d35169d0835e36bcc94ed6c7300ad4706690..f56f21077baa8ee3378019ea23babb85dafebc74 100755 (executable)
@@ -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;
         }
index 36c07033e450060c46fc2f0d14060ec10c5be829..d5294b8f043cfee27069f69ec09e1fee7864348c 100644 (file)
@@ -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.']' : ''));
index ca8a849812a5974280142b498741afc9c0791935..d94f571c2769b8c6ebee440a9a1b473a03fa457b 100755 (executable)
@@ -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));
 
index e54e0f0e43b1e0bfcc07bf77c7b16f2f5e5f6979..4d905eb1878177872c9b1c83de11a6970d934b5c 100644 (file)
@@ -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.']' : ''));