webrunner.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * PHPUnit shell execution wrapper
  18. *
  19. * @package tool_phpunit
  20. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define('NO_OUTPUT_BUFFERING', true);
  24. require(__DIR__ . '/../../../config.php');
  25. require_once($CFG->libdir.'/adminlib.php');
  26. $testpath = optional_param('testpath', '', PARAM_PATH);
  27. $testclass = optional_param('testclass', '', PARAM_ALPHANUMEXT);
  28. $execute = optional_param('execute', 0, PARAM_BOOL);
  29. navigation_node::override_active_url(new moodle_url('/admin/tool/phpunit/index.php'));
  30. admin_externalpage_setup('toolphpunitwebrunner');
  31. if (!$CFG->debugdeveloper) {
  32. print_error('notlocalisederrormessage', 'error', '', null, 'Not available on production sites, sorry.');
  33. }
  34. core_php_time_limit::raise(60*30);
  35. $oldcwd = getcwd();
  36. $code = 0;
  37. if (!isset($CFG->phpunit_dataroot) or !isset($CFG->phpunit_prefix)) {
  38. tool_phpunit_problem('Missing $CFG->phpunit_dataroot or $CFG->phpunit_prefix, can not execute tests.');
  39. }
  40. if (!file_exists($CFG->phpunit_dataroot)) {
  41. mkdir($CFG->phpunit_dataroot, 02777, true);
  42. }
  43. if (!is_writable($CFG->phpunit_dataroot)) {
  44. tool_phpunit_problem('$CFG->phpunit_dataroot in not writable, can not execute tests.');
  45. }
  46. $output = null;
  47. exec('php --version', $output, $code);
  48. if ($code != 0) {
  49. tool_phpunit_problem('Can not execute \'php\' binary.');
  50. }
  51. if ($execute) {
  52. require_sesskey();
  53. chdir($CFG->dirroot);
  54. $output = null;
  55. exec("php $CFG->admin/tool/phpunit/cli/util.php --diag", $output, $code);
  56. if ($code == 0) {
  57. // everything is ready
  58. } else if ($code == PHPUNIT_EXITCODE_INSTALL) {
  59. tool_phpunit_header();
  60. echo $OUTPUT->box_start('generalbox');
  61. echo '<pre>';
  62. echo "Initialising test database:\n\n";
  63. chdir($CFG->dirroot);
  64. ignore_user_abort(true);
  65. passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
  66. passthru("php $CFG->admin/tool/phpunit/cli/util.php --install", $code);
  67. chdir($oldcwd);
  68. echo '</pre>';
  69. echo $OUTPUT->box_end();
  70. if ($code != 0) {
  71. tool_phpunit_problem('Can not initialize database');
  72. }
  73. set_debugging(DEBUG_NONE, false); // Hack: no redirect warning, we really want to redirect.
  74. redirect(new moodle_url($PAGE->url, array('execute'=>1, 'tespath'=>$testpath, 'testclass'=>$testclass, 'sesskey'=>sesskey())), 'Reloading page');
  75. echo $OUTPUT->footer();
  76. die();
  77. } else if ($code == PHPUNIT_EXITCODE_REINSTALL) {
  78. tool_phpunit_header();
  79. echo $OUTPUT->box_start('generalbox');
  80. echo '<pre>';
  81. echo "Reinitialising test database:\n\n";
  82. chdir($CFG->dirroot);
  83. ignore_user_abort(true);
  84. passthru("php $CFG->admin/tool/phpunit/cli/util.php --drop", $code);
  85. passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
  86. passthru("php $CFG->admin/tool/phpunit/cli/util.php --install", $code);
  87. chdir($oldcwd);
  88. echo '</pre>';
  89. echo $OUTPUT->box_end();
  90. if ($code != 0) {
  91. tool_phpunit_problem('Can not initialize database');
  92. }
  93. set_debugging(DEBUG_NONE, false); // Hack: no redirect warning, we really want to redirect.
  94. redirect(new moodle_url($PAGE->url, array('execute'=>1, 'tespath'=>$testpath, 'testclass'=>$testclass, 'sesskey'=>sesskey())), 'Reloading page');
  95. die();
  96. } else {
  97. tool_phpunit_header();
  98. echo $OUTPUT->box_start('generalbox');
  99. echo '<pre>';
  100. echo "Error: $code\n\n";
  101. echo implode("\n", $output);
  102. echo '</pre>';
  103. echo $OUTPUT->box_end();
  104. tool_phpunit_problem('Can not execute tests');
  105. die();
  106. }
  107. tool_phpunit_header();
  108. echo $OUTPUT->box_start('generalbox');
  109. echo '<pre>';
  110. // use the dataroot file
  111. $configdir = "$CFG->phpunit_dataroot/phpunit/webrunner.xml";
  112. if (!file_exists($configdir)) {
  113. passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
  114. if ($code != 0) {
  115. tool_phpunit_problem('Can not create configuration file');
  116. }
  117. }
  118. $configdir = escapeshellarg($configdir);
  119. // no cleanup of path - this is tricky because we can not use escapeshellarg and friends for escaping,
  120. // this is from admin user so PARAM_PATH must be enough
  121. chdir($CFG->dirroot);
  122. passthru("php $CFG->admin/tool/phpunit/cli/util.php --run -c $configdir $testclass $testpath", $code);
  123. chdir($oldcwd);
  124. echo '</pre>';
  125. echo $OUTPUT->box_end();
  126. } else {
  127. tool_phpunit_header();
  128. }
  129. echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
  130. echo '<form method="get" action="webrunner.php">';
  131. echo '<fieldset class="invisiblefieldset">';
  132. echo '<label for="testpath">Test one file</label> ';
  133. echo '<input type="text" id="testpath" name="testpath" value="'.s($testpath).'" size="50" /> (all test cases from webrunner.xml if empty)';
  134. echo '</p>';
  135. echo '<label for="testclass">Class name</label> ';
  136. echo '<input type="text" id="testclass" name="testclass" value="'.s($testclass).'" size="50" /> (first class in file if empty)';
  137. echo '</p>';
  138. echo '<input type="submit" value="Run" />';
  139. echo '<input type="hidden" name="execute" value="1" />';
  140. echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
  141. echo '</fieldset>';
  142. echo '</form>';
  143. echo $OUTPUT->box_end();
  144. echo $OUTPUT->footer();
  145. die;
  146. //========================================
  147. /**
  148. * Print headers and experimental warning
  149. * @return void
  150. */
  151. function tool_phpunit_header() {
  152. global $OUTPUT;
  153. echo $OUTPUT->header();
  154. echo $OUTPUT->heading(get_string('pluginname', 'tool_phpunit'));
  155. echo $OUTPUT->box('EXPERIMENTAL: it is recommended to execute PHPUnit tests and init scripts only from command line.', array('generalbox'));
  156. }
  157. /**
  158. * Called when PHPUnit can not execute.
  159. * @param string $message
  160. * @return void
  161. */
  162. function tool_phpunit_problem($message) {
  163. global $PAGE;
  164. if (!$PAGE->headerprinted) {
  165. tool_phpunit_header();
  166. }
  167. notice($message, new moodle_url('/admin/tool/phpunit/'));
  168. }