NOBUG standardising prevention of output buffering
[moodle.git] / admin / report / unittest / index.php
CommitLineData
3ef8c936 1<?php
2/**
3 * Run the unit tests.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
3ef8c936 8 * @package SimpleTestEx
9 */
10
11/** */
52f81103 12require_once(dirname(__FILE__).'/../../../config.php');
bee4702d 13require_once($CFG->libdir.'/adminlib.php');
e2074b8b 14require_once($CFG->libdir.'/simpletestcoveragelib.php');
3ef8c936 15require_once('ex_simple_test.php');
16require_once('ex_reporter.php');
17
e98a3926
PS
18// disable compression, it would prevent closing of buffers
19if (ini_get('zlib.output_compression')) {
20 ini_set('zlib.output_compression', 'Off');
21}
22
23// try to flush everything all the time
24ob_implicit_flush(true);
25while(ob_get_level()) {
26 if (!ob_end_clean()) {
27 // prevent infinite loop
28 break;
29 }
30}
31
f68cb08b 32// Always run the unit tests in developer debug mode.
33$CFG->debug = DEBUG_DEVELOPER;
34error_reporting($CFG->debug);
346c5887 35raise_memory_limit(MEMORY_EXTRA);
f68cb08b 36
84ebf08d 37// page parameters
e2074b8b 38$path = optional_param('path', null, PARAM_PATH);
39$showpasses = optional_param('showpasses', false, PARAM_BOOL);
40$codecoverage = optional_param('codecoverage', false, PARAM_BOOL);
41$showsearch = optional_param('showsearch', false, PARAM_BOOL);
db5af934 42
84ebf08d 43admin_externalpage_setup('reportsimpletest', '', array('showpasses'=>$showpasses, 'showsearch'=>$showsearch));
d994ced2 44
d994ced2 45$unittest = true;
46
a230012c 47global $UNITTEST;
a226a972 48$UNITTEST = new stdClass();
a230012c 49
cf617be6
SM
50// This limit is the time allowed per individual test function. Please do not
51// increase this value. If you get a PHP time limit when running unit tests,
52// find the unit test which is running slowly, and either make it faster,
53// split it into multiple tests, or call set_time_limit within that test.
54define('TIME_ALLOWED_PER_UNIT_TEST', 60);
55
3ef8c936 56// Print the header.
6bacd339 57$strtitle = get_string('unittests', 'simpletest');
bee4702d 58
aa4f6af4 59if (!is_null($path)) {
7cfa8aee
AD
60 //trim so user doesn't get an error if they include a space on the end of the path (ie by pasting path)
61 $path = trim($path);
62
cc320976 63 // Turn off xmlstrictheaders during the unit test run.
64 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
65 $CFG->xmlstrictheaders = false;
61ef8f9f 66 echo $OUTPUT->header();
cc320976 67 $CFG->xmlstrictheaders = $origxmlstrictheaders;
68 unset($origxmlstrictheaders);
69
aa4f6af4 70 // Create the group of tests.
e2074b8b 71 $test = new autogroup_test_coverage($showsearch, true, $codecoverage, 'Moodle Unit Tests Code Coverage Report', 'unittest');
eef868d1 72
73 // OU specific. We use the _nonproject folder for stuff we want to
aa4f6af4 74 // keep in CVS, but which is not really relevant. It does no harm
75 // to leave this here.
76 $test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
eef868d1 77
aa4f6af4 78 // Make the reporter, which is what displays the results.
79 $reporter = new ExHtmlReporter($showpasses);
eef868d1 80
aa4f6af4 81 if ($showsearch) {
2fff8846 82 echo $OUTPUT->heading('Searching for test cases');
aa4f6af4 83 }
84 flush();
eef868d1 85
aa4f6af4 86 // Work out what to test.
87 if (substr($path, 0, 1) == '/') {
88 $path = substr($path, 1);
89 }
90 $path = $CFG->dirroot . '/' . $path;
91 if (substr($path, -1) == '/') {
92 $path = substr($path, 0, -1);
93 }
94 $displaypath = substr($path, strlen($CFG->dirroot) + 1);
95 $ok = true;
96 if (is_file($path)) {
97 $test->addTestFile($path);
98 } else if (is_dir($path)){
99 $test->findTestFiles($path);
3ef8c936 100 } else {
6bacd339 101 echo $OUTPUT->box(get_string('pathdoesnotexist', 'simpletest', $path), 'errorbox');
aa4f6af4 102 $ok = false;
103 }
eef868d1 104
aa4f6af4 105 // If we have something to test, do it.
106 if ($ok) {
107 if ($path == $CFG->dirroot) {
6bacd339 108 $title = get_string('moodleunittests', 'simpletest', get_string('all', 'simpletest'));
aa4f6af4 109 } else {
6bacd339 110 $title = get_string('moodleunittests', 'simpletest', $displaypath);
aa4f6af4 111 }
2fff8846 112 echo $OUTPUT->heading($title);
aa4f6af4 113 $test->run($reporter);
3ef8c936 114 }
3ef8c936 115
6bacd339 116 $formheader = get_string('retest', 'simpletest');
aa4f6af4 117} else {
118 $displaypath = '';
61ef8f9f 119 echo $OUTPUT->header();
6bacd339 120 $formheader = get_string('rununittests', 'simpletest');
aa4f6af4 121}
3ef8c936 122// Print the form for adjusting options.
5d902b5d 123echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
2fff8846 124echo $OUTPUT->heading($formheader);
7203167d 125echo '<form method="get" action="index.php">';
f689ff1a 126echo '<fieldset class="invisiblefieldset">';
6bacd339
PS
127echo '<p>'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'simpletest')).'</p>';
128echo '<p>'.html_writer::checkbox('showsearch', 1, $showsearch, get_string('showsearch', 'simpletest')).'</p>';
e2074b8b 129if (moodle_coverage_recorder::can_run_codecoverage()) {
40be0144 130 echo '<p>'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'simpletest')).'</p>';
e2074b8b 131} else {
132 echo '<p>'; print_string('codecoveragedisabled', 'simpletest'); echo '<input type="hidden" name="codecoverage" value="0" /></p>';
133}
3ef8c936 134echo '<p>';
6bacd339 135 echo '<label for="path">', get_string('onlytest', 'simpletest'), '</label> ';
bae3a75c 136 echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
3ef8c936 137echo '</p>';
6bacd339 138echo '<input type="submit" value="' . get_string('runtests', 'simpletest') . '" />';
f689ff1a 139echo '</fieldset>';
3ef8c936 140echo '</form>';
5d902b5d 141echo $OUTPUT->box_end();
2d5b05c8 142
e5d06af2 143// Print link to latest code coverage for this report type
144if (is_null($path) || !$codecoverage) {
145 moodle_coverage_reporter::print_link_to_latest('unittest');
146}
d27a349b 147
3ef8c936 148// Footer.
73d6f52f 149echo $OUTPUT->footer();