Unit tests MDL-24909 Unit tests used 5 minute time limit per test class; this is...
[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
cf617be6
SM
36// This limit is the time allowed per individual test function. Please do not
37// increase this value. If you get a PHP time limit when running unit tests,
38// find the unit test which is running slowly, and either make it faster,
39// split it into multiple tests, or call set_time_limit within that test.
40define('TIME_ALLOWED_PER_UNIT_TEST', 60);
41
3ef8c936 42// Print the header.
6bacd339 43$strtitle = get_string('unittests', 'simpletest');
bee4702d 44
aa4f6af4 45if (!is_null($path)) {
7cfa8aee
AD
46 //trim so user doesn't get an error if they include a space on the end of the path (ie by pasting path)
47 $path = trim($path);
48
cc320976 49 // Turn off xmlstrictheaders during the unit test run.
50 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
51 $CFG->xmlstrictheaders = false;
61ef8f9f 52 echo $OUTPUT->header();
cc320976 53 $CFG->xmlstrictheaders = $origxmlstrictheaders;
54 unset($origxmlstrictheaders);
55
aa4f6af4 56 // Create the group of tests.
e2074b8b 57 $test = new autogroup_test_coverage($showsearch, true, $codecoverage, 'Moodle Unit Tests Code Coverage Report', 'unittest');
eef868d1 58
59 // OU specific. We use the _nonproject folder for stuff we want to
aa4f6af4 60 // keep in CVS, but which is not really relevant. It does no harm
61 // to leave this here.
62 $test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
eef868d1 63
aa4f6af4 64 // Make the reporter, which is what displays the results.
65 $reporter = new ExHtmlReporter($showpasses);
eef868d1 66
aa4f6af4 67 if ($showsearch) {
2fff8846 68 echo $OUTPUT->heading('Searching for test cases');
aa4f6af4 69 }
70 flush();
eef868d1 71
aa4f6af4 72 // Work out what to test.
73 if (substr($path, 0, 1) == '/') {
74 $path = substr($path, 1);
75 }
76 $path = $CFG->dirroot . '/' . $path;
77 if (substr($path, -1) == '/') {
78 $path = substr($path, 0, -1);
79 }
80 $displaypath = substr($path, strlen($CFG->dirroot) + 1);
81 $ok = true;
82 if (is_file($path)) {
83 $test->addTestFile($path);
84 } else if (is_dir($path)){
85 $test->findTestFiles($path);
3ef8c936 86 } else {
6bacd339 87 echo $OUTPUT->box(get_string('pathdoesnotexist', 'simpletest', $path), 'errorbox');
aa4f6af4 88 $ok = false;
89 }
eef868d1 90
aa4f6af4 91 // If we have something to test, do it.
92 if ($ok) {
93 if ($path == $CFG->dirroot) {
6bacd339 94 $title = get_string('moodleunittests', 'simpletest', get_string('all', 'simpletest'));
aa4f6af4 95 } else {
6bacd339 96 $title = get_string('moodleunittests', 'simpletest', $displaypath);
aa4f6af4 97 }
2fff8846 98 echo $OUTPUT->heading($title);
aa4f6af4 99 $test->run($reporter);
3ef8c936 100 }
3ef8c936 101
6bacd339 102 $formheader = get_string('retest', 'simpletest');
aa4f6af4 103} else {
104 $displaypath = '';
61ef8f9f 105 echo $OUTPUT->header();
6bacd339 106 $formheader = get_string('rununittests', 'simpletest');
aa4f6af4 107}
3ef8c936 108// Print the form for adjusting options.
5d902b5d 109echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
2fff8846 110echo $OUTPUT->heading($formheader);
7203167d 111echo '<form method="get" action="index.php">';
f689ff1a 112echo '<fieldset class="invisiblefieldset">';
6bacd339
PS
113echo '<p>'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'simpletest')).'</p>';
114echo '<p>'.html_writer::checkbox('showsearch', 1, $showsearch, get_string('showsearch', 'simpletest')).'</p>';
e2074b8b 115if (moodle_coverage_recorder::can_run_codecoverage()) {
40be0144 116 echo '<p>'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'simpletest')).'</p>';
e2074b8b 117} else {
118 echo '<p>'; print_string('codecoveragedisabled', 'simpletest'); echo '<input type="hidden" name="codecoverage" value="0" /></p>';
119}
3ef8c936 120echo '<p>';
6bacd339 121 echo '<label for="path">', get_string('onlytest', 'simpletest'), '</label> ';
bae3a75c 122 echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
3ef8c936 123echo '</p>';
6bacd339 124echo '<input type="submit" value="' . get_string('runtests', 'simpletest') . '" />';
f689ff1a 125echo '</fieldset>';
3ef8c936 126echo '</form>';
5d902b5d 127echo $OUTPUT->box_end();
2d5b05c8 128
e5d06af2 129// Print link to latest code coverage for this report type
130if (is_null($path) || !$codecoverage) {
131 moodle_coverage_reporter::print_link_to_latest('unittest');
132}
d27a349b 133
3ef8c936 134// Footer.
73d6f52f 135echo $OUTPUT->footer();