|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 define('NO_OUTPUT_BUFFERING', true); 00027 00028 require(dirname(__FILE__) . '/../../../config.php'); 00029 require_once($CFG->libdir.'/adminlib.php'); 00030 require_once('simpletestlib.php'); 00031 require_once('simpletestcoveragelib.php'); 00032 require_once('ex_simple_test.php'); 00033 require_once('ex_reporter.php'); 00034 00035 $showpasses = optional_param('showpasses', false, PARAM_BOOL); 00036 $codecoverage = optional_param('codecoverage', false, PARAM_BOOL); 00037 $selected = optional_param_array('selected', array(), PARAM_INT); 00038 00039 // Print the header and check access. 00040 admin_externalpage_setup('tooldbtest'); 00041 echo $OUTPUT->header(); 00042 00043 global $UNITTEST; 00044 $UNITTEST = new stdClass(); 00045 00046 if (!data_submitted()) { 00047 $selected = array(); 00048 for ($i=0; $i<=10; $i++) { 00049 $selected[$i] = 1; 00050 } 00051 } 00052 00053 00054 $dbinfos = array(); 00055 $tests = array(); 00056 00057 $dbinfos[0] = array('name'=>"Current database ($CFG->dblibrary/$CFG->dbtype)", 'installed'=>true, 'configured'=>true); // TODO: localise 00058 if (data_submitted() and !empty($selected[0])) { 00059 $tests[0] = $DB; 00060 } 00061 00062 for ($i=1; $i<=10; $i++) { 00063 $name = 'func_test_db_'.$i; 00064 if (!isset($CFG->$name)) { 00065 continue; 00066 } 00067 list($library, $driver, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) = $CFG->$name; 00068 $dbinfos[$i] = array('name'=>"External database $i ($library/$driver/$dbhost/$dbname/$prefix)", 'installed'=>false, 'configured'=>false); 00069 00070 $classname = "{$driver}_{$library}_moodle_database"; 00071 require_once("$CFG->libdir/dml/$classname.php"); 00072 $d = new $classname(); 00073 if (!$d->driver_installed()) { 00074 continue; 00075 } 00076 $dbinfos[$i]['installed'] = true; 00077 00078 try { 00079 $d->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions); 00080 $dbinfos[$i]['configured'] = true; 00081 if (data_submitted() and !empty($selected[$i])) { 00082 $tests[$i] = $d; 00083 } else { 00084 $d->dispose(); 00085 } 00086 } catch (dml_connection_exception $e) { 00087 $dbinfos[$i]['configured'] = false; 00088 } 00089 } 00090 00091 if (!empty($tests)) { 00092 $covreporter = new moodle_coverage_reporter('Functional DB Tests Code Coverage Report', 'dbtest'); 00093 $covrecorder = new moodle_coverage_recorder($covreporter); 00094 00095 foreach ($tests as $i=>$database) { 00096 $dbinfo = $dbinfos[$i]; 00097 00098 $UNITTEST->func_test_db = $database; // pass the db to the tests through global 00099 00100 echo $OUTPUT->heading('Running tests on: '.$dbinfo['name'], 3); // TODO: localise 00101 00102 // Create the group of tests. 00103 $test = new autogroup_test_coverage(false, true, $codecoverage); 00104 00105 00106 $test->addTestFile($CFG->libdir.'/dml/simpletest/testdml.php'); 00107 $test->addTestFile($CFG->libdir.'/ddl/simpletest/testddl.php'); 00108 00109 // Make the reporter, which is what displays the results. 00110 $reporter = new ExHtmlReporter($showpasses); 00111 00112 set_time_limit(300); // 5 mins per DB should be enough 00113 $test->run_with_external_coverage($reporter, $covrecorder); 00114 00115 unset($UNITTEST->func_test_db); 00116 00117 echo '<hr />'; 00118 } 00119 if ($codecoverage) { 00120 $covrecorder->generate_report(); 00121 moodle_coverage_reporter::print_summary_info('dbtest'); 00122 } 00123 00124 } 00125 00126 // Print the form for adjusting options. 00127 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter'); 00128 echo '<form method="post" action="dbtest.php">'; 00129 echo '<div>'; 00130 echo $OUTPUT->heading("Run functional database tests"); // TODO: localise 00131 echo '<p>'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'tool_unittest')).'</p>'; 00132 if (moodle_coverage_recorder::can_run_codecoverage()) { 00133 echo '<p>'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'tool_unittest')).'</p>'; 00134 } else { 00135 echo '<p>'; print_string('codecoveragedisabled', 'tool_unittest'); echo '<input type="hidden" name="codecoverage" value="0" /></p>'; 00136 } 00137 echo '<p><strong>'."Databases:".'</strong></p>'; 00138 echo '<ul>'; 00139 foreach ($dbinfos as $i=>$dbinfo) { 00140 $name = $dbinfo['name']; 00141 if ($dbinfo['installed']) { 00142 if (!$dbinfo['configured']) { 00143 $name = "$name (misconfigured)"; // TODO: localise 00144 } 00145 echo '<li>'.html_writer::checkbox('selected['.$i.']', 1, intval(!empty($selected[$i])), $name).'</li>'; 00146 } else { 00147 echo '<li>'."$name: driver not installed".'</li>'; // TODO: localise 00148 } 00149 } 00150 echo '</ul>'; 00151 echo '<p>External databases are configured in config.php, add lines:</p> 00152 <pre> 00153 $CFG->func_test_db_1 = array("native", "pgsql", "localhost", "moodleuser", "password", "moodle", "test", null); 00154 $CFG->func_test_db_2 = array("native", "mssql", "localhost", "moodleuser", "password", "moodle", "test", null); 00155 </pre> 00156 <p>where order of parameters is: dblibrary, dbtype, dbhost, dbuser, dbpass, dbname, prefix, dboptions</p>'; 00157 echo '<p><input type="submit" value="' . get_string('runtests', 'tool_unittest') . '" /></p>'; 00158 echo '</div>'; 00159 echo '</form>'; 00160 echo $OUTPUT->box_end(); 00161 00162 // Print link to latest code coverage for this report type 00163 if (!data_submitted() || !$codecoverage) { 00164 moodle_coverage_reporter::print_link_to_latest('dbtest'); 00165 } 00166 00167 // Footer. 00168 echo $OUTPUT->footer();