Backup MDL-24906 Fixed unit test failures
[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
f68cb08b 18// Always run the unit tests in developer debug mode.
19$CFG->debug = DEBUG_DEVELOPER;
20error_reporting($CFG->debug);
346c5887 21raise_memory_limit(MEMORY_EXTRA);
f68cb08b 22
84ebf08d 23// page parameters
e2074b8b 24$path = optional_param('path', null, PARAM_PATH);
25$showpasses = optional_param('showpasses', false, PARAM_BOOL);
26$codecoverage = optional_param('codecoverage', false, PARAM_BOOL);
27$showsearch = optional_param('showsearch', false, PARAM_BOOL);
db5af934 28
84ebf08d 29admin_externalpage_setup('reportsimpletest', '', array('showpasses'=>$showpasses, 'showsearch'=>$showsearch));
d994ced2 30
d994ced2 31$unittest = true;
32
a230012c 33global $UNITTEST;
a226a972 34$UNITTEST = new stdClass();
a230012c 35
3ef8c936 36// Print the header.
6bacd339 37$strtitle = get_string('unittests', 'simpletest');
bee4702d 38
aa4f6af4 39if (!is_null($path)) {
7cfa8aee
AD
40 //trim so user doesn't get an error if they include a space on the end of the path (ie by pasting path)
41 $path = trim($path);
42
cc320976 43 // Turn off xmlstrictheaders during the unit test run.
44 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
45 $CFG->xmlstrictheaders = false;
61ef8f9f 46 echo $OUTPUT->header();
cc320976 47 $CFG->xmlstrictheaders = $origxmlstrictheaders;
48 unset($origxmlstrictheaders);
49
aa4f6af4 50 // Create the group of tests.
e2074b8b 51 $test = new autogroup_test_coverage($showsearch, true, $codecoverage, 'Moodle Unit Tests Code Coverage Report', 'unittest');
eef868d1 52
53 // OU specific. We use the _nonproject folder for stuff we want to
aa4f6af4 54 // keep in CVS, but which is not really relevant. It does no harm
55 // to leave this here.
56 $test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
eef868d1 57
aa4f6af4 58 // Make the reporter, which is what displays the results.
59 $reporter = new ExHtmlReporter($showpasses);
eef868d1 60
aa4f6af4 61 if ($showsearch) {
2fff8846 62 echo $OUTPUT->heading('Searching for test cases');
aa4f6af4 63 }
64 flush();
eef868d1 65
aa4f6af4 66 // Work out what to test.
67 if (substr($path, 0, 1) == '/') {
68 $path = substr($path, 1);
69 }
70 $path = $CFG->dirroot . '/' . $path;
71 if (substr($path, -1) == '/') {
72 $path = substr($path, 0, -1);
73 }
74 $displaypath = substr($path, strlen($CFG->dirroot) + 1);
75 $ok = true;
76 if (is_file($path)) {
77 $test->addTestFile($path);
78 } else if (is_dir($path)){
79 $test->findTestFiles($path);
3ef8c936 80 } else {
6bacd339 81 echo $OUTPUT->box(get_string('pathdoesnotexist', 'simpletest', $path), 'errorbox');
aa4f6af4 82 $ok = false;
83 }
eef868d1 84
aa4f6af4 85 // If we have something to test, do it.
86 if ($ok) {
87 if ($path == $CFG->dirroot) {
6bacd339 88 $title = get_string('moodleunittests', 'simpletest', get_string('all', 'simpletest'));
aa4f6af4 89 } else {
6bacd339 90 $title = get_string('moodleunittests', 'simpletest', $displaypath);
aa4f6af4 91 }
2fff8846 92 echo $OUTPUT->heading($title);
e2074b8b 93 set_time_limit(300); // 5 mins
aa4f6af4 94 $test->run($reporter);
3ef8c936 95 }
3ef8c936 96
6bacd339 97 $formheader = get_string('retest', 'simpletest');
aa4f6af4 98} else {
99 $displaypath = '';
61ef8f9f 100 echo $OUTPUT->header();
6bacd339 101 $formheader = get_string('rununittests', 'simpletest');
aa4f6af4 102}
3ef8c936 103// Print the form for adjusting options.
5d902b5d 104echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
2fff8846 105echo $OUTPUT->heading($formheader);
7203167d 106echo '<form method="get" action="index.php">';
f689ff1a 107echo '<fieldset class="invisiblefieldset">';
6bacd339
PS
108echo '<p>'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'simpletest')).'</p>';
109echo '<p>'.html_writer::checkbox('showsearch', 1, $showsearch, get_string('showsearch', 'simpletest')).'</p>';
e2074b8b 110if (moodle_coverage_recorder::can_run_codecoverage()) {
40be0144 111 echo '<p>'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'simpletest')).'</p>';
e2074b8b 112} else {
113 echo '<p>'; print_string('codecoveragedisabled', 'simpletest'); echo '<input type="hidden" name="codecoverage" value="0" /></p>';
114}
3ef8c936 115echo '<p>';
6bacd339 116 echo '<label for="path">', get_string('onlytest', 'simpletest'), '</label> ';
bae3a75c 117 echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
3ef8c936 118echo '</p>';
6bacd339 119echo '<input type="submit" value="' . get_string('runtests', 'simpletest') . '" />';
f689ff1a 120echo '</fieldset>';
3ef8c936 121echo '</form>';
5d902b5d 122echo $OUTPUT->box_end();
2d5b05c8 123
e5d06af2 124// Print link to latest code coverage for this report type
125if (is_null($path) || !$codecoverage) {
126 moodle_coverage_reporter::print_link_to_latest('unittest');
127}
d27a349b 128
3ef8c936 129// Footer.
73d6f52f 130echo $OUTPUT->footer();