NOBUG standardising prevention of output buffering
[moodle.git] / admin / report / unittest / dbtest.php
CommitLineData
244bb396 1<?php
e6b4f00e 2/**
3 * Run database functional tests.
e6b4f00e 4 * @package SimpleTestEx
5 */
6
7/** */
8require_once(dirname(__FILE__).'/../../../config.php');
9require_once($CFG->libdir.'/adminlib.php');
e2074b8b 10require_once($CFG->libdir.'/simpletestcoveragelib.php');
e6b4f00e 11require_once('ex_simple_test.php');
12require_once('ex_reporter.php');
13
e98a3926
PS
14// disable compression, it would prevent closing of buffers
15if (ini_get('zlib.output_compression')) {
16 ini_set('zlib.output_compression', 'Off');
17}
18
19// try to flush everything all the time
20ob_implicit_flush(true);
21while(ob_get_level()) {
22 if (!ob_end_clean()) {
23 // prevent infinite loop
24 break;
25 }
26}
27
e2074b8b 28$showpasses = optional_param('showpasses', false, PARAM_BOOL);
29$codecoverage = optional_param('codecoverage', false, PARAM_BOOL);
30$selected = optional_param('selected', array(), PARAM_INT);
e6b4f00e 31
d994ced2 32// Print the header and check access.
33admin_externalpage_setup('reportdbtest');
61ef8f9f 34echo $OUTPUT->header();
d994ced2 35
a230012c 36global $UNITTEST;
a226a972 37$UNITTEST = new stdClass();
a230012c 38
b936956e 39if (!data_submitted()) {
40 $selected = array();
41 for ($i=0; $i<=10; $i++) {
42 $selected[$i] = 1;
43 }
44}
e6b4f00e 45
e6b4f00e 46
b936956e 47$dbinfos = array();
48$tests = array();
49
06813454 50$dbinfos[0] = array('name'=>"Current database ($CFG->dblibrary/$CFG->dbtype)", 'installed'=>true, 'configured'=>true); // TODO: localise
b936956e 51if (data_submitted() and !empty($selected[0])) {
52 $tests[0] = $DB;
53}
e6b4f00e 54
55for ($i=1; $i<=10; $i++) {
b936956e 56 $name = 'func_test_db_'.$i;
e6b4f00e 57 if (!isset($CFG->$name)) {
58 continue;
59 }
beaa43db 60 list($library, $driver, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) = $CFG->$name;
d19c1ab6 61 $dbinfos[$i] = array('name'=>"External database $i ($library/$driver/$dbhost/$dbname/$prefix)", 'installed'=>false, 'configured'=>false);
e6b4f00e 62
63 $classname = "{$driver}_{$library}_moodle_database";
64 require_once("$CFG->libdir/dml/$classname.php");
65 $d = new $classname();
66 if (!$d->driver_installed()) {
67 continue;
68 }
b936956e 69 $dbinfos[$i]['installed'] = true;
e6b4f00e 70
c51ca835 71 try {
72 $d->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
b936956e 73 $dbinfos[$i]['configured'] = true;
74 if (data_submitted() and !empty($selected[$i])) {
75 $tests[$i] = $d;
76 } else {
77 $d->dispose();
78 }
c51ca835 79 } catch (dml_connection_exception $e) {
80 $dbinfos[$i]['configured'] = false;
e6b4f00e 81 }
82}
83
b936956e 84if (!empty($tests)) {
e2074b8b 85 $covreporter = new moodle_coverage_reporter('Functional DB Tests Code Coverage Report', 'dbtest');
86 $covrecorder = new moodle_coverage_recorder($covreporter);
87
b936956e 88 foreach ($tests as $i=>$database) {
89 $dbinfo = $dbinfos[$i];
1ddfd4ec 90
a230012c 91 $UNITTEST->func_test_db = $database; // pass the db to the tests through global
e6b4f00e 92
2fff8846 93 echo $OUTPUT->heading('Running tests on: '.$dbinfo['name'], 3); // TODO: localise
e6b4f00e 94
b936956e 95 // Create the group of tests.
e2074b8b 96 $test = new autogroup_test_coverage(false, true, $codecoverage);
97
e6b4f00e 98
3dce78e1 99 $test->addTestFile($CFG->libdir.'/dml/simpletest/testdml.php');
100 $test->addTestFile($CFG->libdir.'/ddl/simpletest/testddl.php');
b936956e 101
102 // Make the reporter, which is what displays the results.
103 $reporter = new ExHtmlReporter($showpasses);
e6b4f00e 104
e2074b8b 105 set_time_limit(300); // 5 mins per DB should be enough
106 $test->run_with_external_coverage($reporter, $covrecorder);
b936956e 107
a230012c 108 unset($UNITTEST->func_test_db);
109
b936956e 110 echo '<hr />';
111 }
e2074b8b 112 if ($codecoverage) {
113 $covrecorder->generate_report();
114 moodle_coverage_reporter::print_summary_info('dbtest');
115 }
e6b4f00e 116
e6b4f00e 117}
118
119// Print the form for adjusting options.
5d902b5d 120echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
b936956e 121echo '<form method="post" action="dbtest.php">';
122echo '<div>';
2fff8846 123echo $OUTPUT->heading("Run functional database tests"); // TODO: localise
6bacd339 124echo '<p>'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'simpletest')).'</p>';
e2074b8b 125if (moodle_coverage_recorder::can_run_codecoverage()) {
6bacd339 126 echo '<p>'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'simpletest')).'</p>';
e2074b8b 127} else {
128 echo '<p>'; print_string('codecoveragedisabled', 'simpletest'); echo '<input type="hidden" name="codecoverage" value="0" /></p>';
129}
70013fdc 130echo '<p><strong>'."Databases:".'</strong></p>';
b936956e 131echo '<ul>';
132foreach ($dbinfos as $i=>$dbinfo) {
133 $name = $dbinfo['name'];
134 if ($dbinfo['installed']) {
135 if (!$dbinfo['configured']) {
136 $name = "$name (misconfigured)"; // TODO: localise
137 }
6bacd339 138 echo '<li>'.html_writer::checkbox('selected['.$i.']', 1, intval(!empty($selected[$i])), $name).'</li>';
b936956e 139 } else {
70013fdc 140 echo '<li>'."$name: driver not installed".'</li>'; // TODO: localise
b936956e 141 }
142}
70013fdc 143echo '</ul>';
144echo '<p>External databases are configured in config.php, add lines:</p>
06813454 145<pre>
d6960b0b 146$CFG->func_test_db_1 = array("native", "pgsql", "localhost", "moodleuser", "password", "moodle", "test", null);
147$CFG->func_test_db_2 = array("native", "mssql", "localhost", "moodleuser", "password", "moodle", "test", null);
06813454 148</pre>
70013fdc 149<p>where order of parameters is: dblibrary, dbtype, dbhost, dbuser, dbpass, dbname, prefix, dboptions</p>';
06813454 150echo '<p><input type="submit" value="' . get_string('runtests', 'simpletest') . '" /></p>';
b936956e 151echo '</div>';
e6b4f00e 152echo '</form>';
5d902b5d 153echo $OUTPUT->box_end();
e6b4f00e 154
e5d06af2 155// Print link to latest code coverage for this report type
156if (!data_submitted() || !$codecoverage) {
157 moodle_coverage_reporter::print_link_to_latest('dbtest');
158}
159
e6b4f00e 160// Footer.
73d6f52f 161echo $OUTPUT->footer();