source: moodle/trunk/fuentes/admin/tool/phpunit/webrunner.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

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