PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/unittest/index.php

http://github.com/moodle/moodle
PHP | 173 lines | 105 code | 24 blank | 44 comment | 21 complexity | 2780f82d45f6aa0ee0d2bce174233884 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Run the unit tests.
  18. *
  19. * @package tool
  20. * @subpackage unittest
  21. * @copyright &copy; 2006 The Open University
  22. * @author N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. define('NO_OUTPUT_BUFFERING', true);
  26. require(dirname(__FILE__) . '/../../../config.php');
  27. require_once($CFG->libdir.'/adminlib.php');
  28. require_once('simpletestlib.php');
  29. require_once('simpletestcoveragelib.php');
  30. require_once('ex_simple_test.php');
  31. require_once('ex_reporter.php');
  32. // Always run the unit tests in developer debug mode.
  33. $CFG->debug = DEBUG_DEVELOPER;
  34. error_reporting($CFG->debug);
  35. raise_memory_limit(MEMORY_EXTRA);
  36. // page parameters
  37. $path = optional_param('path', null, PARAM_PATH);
  38. $showpasses = optional_param('showpasses', false, PARAM_BOOL);
  39. $codecoverage = optional_param('codecoverage', false, PARAM_BOOL);
  40. $showsearch = optional_param('showsearch', false, PARAM_BOOL);
  41. admin_externalpage_setup('toolsimpletest', '', array('showpasses'=>$showpasses, 'showsearch'=>$showsearch));
  42. $unittest = true;
  43. global $UNITTEST;
  44. $UNITTEST = new stdClass();
  45. // This limit is the time allowed per individual test function. Please do not
  46. // increase this value. If you get a PHP time limit when running unit tests,
  47. // find the unit test which is running slowly, and either make it faster,
  48. // split it into multiple tests, or call set_time_limit within that test.
  49. define('TIME_ALLOWED_PER_UNIT_TEST', 60);
  50. // Print the header.
  51. $strtitle = get_string('unittests', 'tool_unittest');
  52. if (!is_null($path)) {
  53. //trim so user doesn't get an error if they include a space on the end of the path (ie by pasting path)
  54. $path = trim($path);
  55. // Turn off xmlstrictheaders during the unit test run.
  56. $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
  57. $CFG->xmlstrictheaders = false;
  58. echo $OUTPUT->header();
  59. $CFG->xmlstrictheaders = $origxmlstrictheaders;
  60. unset($origxmlstrictheaders);
  61. // Create the group of tests.
  62. $test = new autogroup_test_coverage($showsearch, true, $codecoverage, 'Moodle Unit Tests Code Coverage Report', 'unittest');
  63. // OU specific. We use the _nonproject folder for stuff we want to
  64. // keep in CVS, but which is not really relevant. It does no harm
  65. // to leave this here.
  66. $test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
  67. // Make the reporter, which is what displays the results.
  68. $reporter = new ExHtmlReporter($showpasses);
  69. if ($showsearch) {
  70. echo $OUTPUT->heading('Searching for test cases');
  71. }
  72. flush();
  73. // Work out what to test.
  74. if (substr($path, 0, 1) == '/') {
  75. $path = substr($path, 1);
  76. }
  77. $path = $CFG->dirroot . '/' . $path;
  78. if (substr($path, -1) == '/') {
  79. $path = substr($path, 0, -1);
  80. }
  81. $displaypath = substr($path, strlen($CFG->dirroot) + 1);
  82. $ok = true;
  83. if (is_file($path)) {
  84. $test->addTestFile($path);
  85. } else if (is_dir($path)){
  86. $test->findTestFiles($path);
  87. } else {
  88. echo $OUTPUT->box(get_string('pathdoesnotexist', 'tool_unittest', $path), 'errorbox');
  89. $ok = false;
  90. }
  91. // If we have something to test, do it.
  92. if ($ok) {
  93. if ($path == $CFG->dirroot) {
  94. $title = get_string('moodleunittests', 'tool_unittest', get_string('all', 'tool_unittest'));
  95. } else {
  96. $title = get_string('moodleunittests', 'tool_unittest', $displaypath);
  97. }
  98. echo $OUTPUT->heading($title);
  99. $test->run($reporter);
  100. }
  101. $formheader = get_string('retest', 'tool_unittest');
  102. } else {
  103. $displaypath = '';
  104. echo $OUTPUT->header();
  105. $formheader = get_string('rununittests', 'tool_unittest');
  106. }
  107. // Print the form for adjusting options.
  108. echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
  109. echo $OUTPUT->heading($formheader);
  110. echo '<form method="get" action="index.php">';
  111. echo '<fieldset class="invisiblefieldset">';
  112. echo '<p>'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'tool_unittest')).'</p>';
  113. echo '<p>'.html_writer::checkbox('showsearch', 1, $showsearch, get_string('showsearch', 'tool_unittest')).'</p>';
  114. if (moodle_coverage_recorder::can_run_codecoverage()) {
  115. echo '<p>'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'tool_unittest')).'</p>';
  116. } else {
  117. echo '<p>'; print_string('codecoveragedisabled', 'tool_unittest'); echo '<input type="hidden" name="codecoverage" value="0" /></p>';
  118. }
  119. echo '<p>';
  120. echo '<label for="path">', get_string('onlytest', 'tool_unittest'), '</label> ';
  121. echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
  122. echo '</p>';
  123. echo '<input type="submit" value="' . get_string('runtests', 'tool_unittest') . '" />';
  124. echo '</fieldset>';
  125. echo '</form>';
  126. echo $OUTPUT->box_end();
  127. $otherpages = array();
  128. $otherpages['PDF lib test'] = new moodle_url('/admin/tool/unittest/other/pdflibtestpage.php');
  129. if (debugging('', DEBUG_DEVELOPER)) {
  130. $otherpages['TODO checker'] = new moodle_url('/admin/tool/unittest/other/todochecker.php');
  131. }
  132. // print list of extra test pages that are not simpletests,
  133. // not everything there is good enough to show to our users
  134. if ($otherpages) {
  135. echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
  136. echo $OUTPUT->heading(get_string('othertestpages', 'tool_unittest'));
  137. echo '<ul>';
  138. foreach ($otherpages as $name=>$url) {
  139. echo '<li>'.html_writer::link($url, $name).'</li>';
  140. }
  141. echo '</ul>';
  142. echo $OUTPUT->box_end();
  143. }
  144. // Print link to latest code coverage for this report type
  145. if (is_null($path) || !$codecoverage) {
  146. moodle_coverage_reporter::print_link_to_latest('unittest');
  147. }
  148. // Footer.
  149. echo $OUTPUT->footer();