MDL-32323 experimental web UI for execution of PHPUnit tests
It is not localised intentionally because only developers should use it…
This commit is contained in:
@@ -3,22 +3,24 @@
|
||||
CLIDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
UTIL="$CLIDIR/util.php"
|
||||
|
||||
echo "Building phpunit.xml and initialising test database..."
|
||||
echo "Initialising test database and creating phpunit.xml configuration..."
|
||||
|
||||
DIGERROR=`php $UTIL --diag`
|
||||
DIAG=$?
|
||||
if [ $DIAG -eq 132 ] ; then
|
||||
php $UTIL --install
|
||||
else
|
||||
if [ $DIAG -eq 133 ] ; then
|
||||
php $UTIL --drop
|
||||
RESULT=$?
|
||||
if [ $RESULT -gt 0 ] ; then
|
||||
exit $RESULT
|
||||
fi
|
||||
php $UTIL --install
|
||||
else
|
||||
echo $DIGERROR
|
||||
exit $DIAG
|
||||
fi
|
||||
fi
|
||||
|
||||
php $UTIL --buildconfig
|
||||
RESULT=$?
|
||||
if [ $RESULT -gt 0 ] ; then
|
||||
exit $RESULT
|
||||
fi
|
||||
|
||||
php $UTIL --drop
|
||||
RESULT=$?
|
||||
if [ $RESULT -gt 0 ] ; then
|
||||
exit $RESULT
|
||||
fi
|
||||
|
||||
php $UTIL --install
|
||||
RESULT=$?
|
||||
if [ $RESULT -gt 0 ] ; then
|
||||
exit $RESULT
|
||||
fi
|
||||
|
||||
@@ -20,9 +20,11 @@
|
||||
* Exit codes:
|
||||
* 0 - success
|
||||
* 1 - general error
|
||||
* 130 - coding error
|
||||
* 130 - missing PHPUnit error
|
||||
* 131 - configuration problem
|
||||
* 132 - install new test database
|
||||
* 133 - drop existing data before installing
|
||||
* 134 - can not create main phpunit.xml
|
||||
*
|
||||
* @package tool_phpunit
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
@@ -31,10 +33,11 @@
|
||||
|
||||
define('PHPUNIT_UTIL', true);
|
||||
|
||||
require_once(__DIR__ . '/../../../../lib/phpunit/bootstraplib.php');
|
||||
|
||||
// verify PHPUnit installation
|
||||
if (!@include_once('PHPUnit/Autoload.php')) {
|
||||
fwrite(STDERR, "Can not load PHPUnit PEAR library, is it installed?\n");
|
||||
exit(1);
|
||||
phpunit_bootstrap_error(130);
|
||||
}
|
||||
|
||||
require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
|
||||
@@ -51,6 +54,7 @@ list($options, $unrecognized) = cli_get_params(
|
||||
'drop' => false,
|
||||
'install' => false,
|
||||
'buildconfig' => false,
|
||||
'diag' => false,
|
||||
'help' => false,
|
||||
),
|
||||
array(
|
||||
@@ -63,17 +67,19 @@ if ($unrecognized) {
|
||||
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
|
||||
}
|
||||
|
||||
$diag = $options['diag'];
|
||||
$drop = $options['drop'];
|
||||
$install = $options['install'];
|
||||
$buildconfig = $options['buildconfig'];
|
||||
|
||||
if ($options['help'] or (!$drop and !$install and !$buildconfig)) {
|
||||
if ($options['help'] or (!$drop and !$install and !$buildconfig and !$diag)) {
|
||||
$help = "Various PHPUnit utility functions
|
||||
|
||||
Options:
|
||||
--drop Drop database and dataroot
|
||||
--install Install database
|
||||
--buildconfig Build /phpunit.xml from /phpunit.xml.dist that includes suites for all plugins and core
|
||||
--diag Diagnose installation and return error code only
|
||||
|
||||
-h, --help Print out this help
|
||||
|
||||
@@ -81,13 +87,24 @@ Example:
|
||||
\$/usr/bin/php lib/phpunit/tool.php
|
||||
";
|
||||
echo $help;
|
||||
die;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if ($buildconfig) {
|
||||
phpunit_util::build_config_file();
|
||||
if ($diag) {
|
||||
list($errorcode, $message) = phpunit_util::testing_ready_problem();
|
||||
if ($errorcode) {
|
||||
phpunit_bootstrap_error($errorcode, $message);
|
||||
}
|
||||
exit(0);
|
||||
|
||||
} else if ($buildconfig) {
|
||||
if (phpunit_util::build_config_file()) {
|
||||
exit(0);
|
||||
} else {
|
||||
phpunit_bootstrap_error(134);
|
||||
}
|
||||
|
||||
|
||||
} else if ($drop) {
|
||||
phpunit_util::drop_site();
|
||||
// note: we must stop here because $CFG is messed up and we can not reinstall, sorry
|
||||
|
||||
@@ -26,3 +26,5 @@
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$ADMIN->add('development', new admin_externalpage('toolphpunit', get_string('pluginname', 'tool_phpunit'), "$CFG->wwwroot/$CFG->admin/tool/phpunit/index.php"));
|
||||
$ADMIN->add('development', new admin_externalpage('toolphpunitwebrunner', get_string('pluginname', 'tool_phpunit'), "$CFG->wwwroot/$CFG->admin/tool/phpunit/webrunner.php",
|
||||
'moodle/site:config', true));
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* PHPUnit shell execution wrapper
|
||||
*
|
||||
* @package tool_phpunit
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
define('NO_OUTPUT_BUFFERING', true);
|
||||
|
||||
require(dirname(__FILE__) . '/../../../config.php');
|
||||
require_once($CFG->libdir.'/adminlib.php');
|
||||
|
||||
$path = optional_param('path', null, PARAM_PATH);
|
||||
$execute = optional_param('execute', 0, PARAM_BOOL);
|
||||
|
||||
|
||||
navigation_node::override_active_url(new moodle_url('/admin/tool/phpunit/index.php'));
|
||||
admin_externalpage_setup('toolphpunitwebrunner');
|
||||
|
||||
if (!debugging('', DEBUG_DEVELOPER)) {
|
||||
error('Not available on production sites, sorry.');
|
||||
}
|
||||
|
||||
set_time_limit(60*30);
|
||||
|
||||
$oldcwd = getcwd();
|
||||
$code = 0;
|
||||
|
||||
if (!isset($CFG->phpunit_dataroot) or !isset($CFG->phpunit_prefix)) {
|
||||
tool_phpunit_problem('Missing $CFG->phpunit_dataroot or $CFG->phpunit_prefix, can not execute tests.');
|
||||
}
|
||||
if (!file_exists($CFG->phpunit_dataroot)) {
|
||||
mkdir($CFG->phpunit_dataroot, 02777, true);
|
||||
}
|
||||
if (!is_writable($CFG->phpunit_dataroot)) {
|
||||
tool_phpunit_problem('$CFG->phpunit_dataroot in not writable, can not execute tests.');
|
||||
}
|
||||
$output = null;
|
||||
exec('phpunit --version', $output, $code);
|
||||
if ($code != 0) {
|
||||
tool_phpunit_problem('Can not execute \'phpunit\' script.');
|
||||
}
|
||||
$output = null;
|
||||
exec('php --version', $output, $code);
|
||||
if ($code != 0) {
|
||||
tool_phpunit_problem('Can not execute \'php\' binary.');
|
||||
}
|
||||
|
||||
if ($execute) {
|
||||
|
||||
require_sesskey();
|
||||
|
||||
chdir($CFG->dirroot);
|
||||
$output = null;
|
||||
exec("php $CFG->admin/tool/phpunit/cli/util.php --diag", $output, $code);
|
||||
if ($code == 0) {
|
||||
// everything is ready
|
||||
|
||||
} else if ($code == 132) {
|
||||
tool_phpunit_header();
|
||||
echo $OUTPUT->box_start('generalbox');
|
||||
echo '<pre>';
|
||||
echo "Initialising test database:\n\n";
|
||||
chdir($CFG->dirroot);
|
||||
ignore_user_abort(true);
|
||||
passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
|
||||
passthru("php $CFG->admin/tool/phpunit/cli/util.php --install", $code);
|
||||
chdir($oldcwd);
|
||||
echo '</pre>';
|
||||
echo $OUTPUT->box_end();
|
||||
if ($code != 0) {
|
||||
tool_phpunit_problem('Can not initialize database');
|
||||
}
|
||||
$CFG->debug = 0; // no pesky redirect warning, we really want to redirect
|
||||
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'path'=>$path, 'sesskey'=>sesskey())), 'Reloading page');
|
||||
echo $OUTPUT->footer();
|
||||
die();
|
||||
|
||||
} else if ($code == 133) {
|
||||
tool_phpunit_header();
|
||||
ignore_user_abort(true);
|
||||
echo $OUTPUT->box_start('generalbox');
|
||||
echo '<pre>';
|
||||
echo "Reinitialising test database:\n\n";
|
||||
chdir($CFG->dirroot);
|
||||
ignore_user_abort(true);
|
||||
passthru("php $CFG->admin/tool/phpunit/cli/util.php --drop", $code);
|
||||
passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
|
||||
passthru("php $CFG->admin/tool/phpunit/cli/util.php --install", $code);
|
||||
chdir($oldcwd);
|
||||
echo '</pre>';
|
||||
echo $OUTPUT->box_end();
|
||||
if ($code != 0) {
|
||||
tool_phpunit_problem('Can not initialize database');
|
||||
}
|
||||
$CFG->debug = 0; // no pesky redirect warning, we really want to redirect
|
||||
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'path'=>$path, 'sesskey'=>sesskey())), 'Reloading page');
|
||||
die();
|
||||
|
||||
} else {
|
||||
tool_phpunit_header();
|
||||
echo $OUTPUT->box_start('generalbox');
|
||||
echo '<pre>';
|
||||
echo "Error: $code\n\n";
|
||||
echo implode("\n", $output);
|
||||
echo '</pre>';
|
||||
echo $OUTPUT->box_end();
|
||||
tool_phpunit_problem('Can not execute tests');
|
||||
die();
|
||||
}
|
||||
|
||||
tool_phpunit_header();
|
||||
echo $OUTPUT->box_start('generalbox');
|
||||
echo '<pre>';
|
||||
|
||||
chdir($CFG->dirroot);
|
||||
// use the dataroot file
|
||||
$configdir = "$CFG->phpunit_dataroot/phpunit/webrunner.xml";
|
||||
if (!file_exists($configdir)) {
|
||||
passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
|
||||
if ($code != 0) {
|
||||
tool_phpunit_problem('Can not create configuration file');
|
||||
}
|
||||
}
|
||||
$configdir = escapeshellarg($configdir);
|
||||
// cleanup the path - this is tricky because we can not use quotes for escaping
|
||||
$path = escapeshellcmd($path);
|
||||
$path = str_replace('\*', '*', $path);
|
||||
passthru("phpunit -c $configdir $path", $code);
|
||||
chdir($oldcwd);
|
||||
|
||||
echo '</pre>';
|
||||
echo $OUTPUT->box_end();
|
||||
|
||||
} else {
|
||||
tool_phpunit_header();
|
||||
}
|
||||
|
||||
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
|
||||
echo '<form method="get" action="webrunner.php">';
|
||||
echo '<fieldset class="invisiblefieldset">';
|
||||
echo '<label for="path">Test only</label> ';
|
||||
echo '<input type="text" id="path" name="path" value="'.s($path).'" size="40" />';
|
||||
echo '</p>';
|
||||
echo '<input type="submit" value="Run" />';
|
||||
echo '<input type="hidden" name="execute" value="1" />';
|
||||
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
|
||||
echo '</fieldset>';
|
||||
echo '</form>';
|
||||
echo $OUTPUT->box_end();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
|
||||
|
||||
|
||||
//========================================
|
||||
|
||||
/**
|
||||
* Print headers and warning
|
||||
*/
|
||||
function tool_phpunit_header() {
|
||||
global $OUTPUT;
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('pluginname', 'tool_phpunit'));
|
||||
echo $OUTPUT->box('EXPERIMENTAL: it is recommended to execute PHPUnit tests and init scripts only from command line.', array('generalbox'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when PHPUnit can not execute.
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
function tool_phpunit_problem($message) {
|
||||
global $OUTPUT, $PAGE;
|
||||
if (!$PAGE->headerprinted) {
|
||||
tool_phpunit_header();
|
||||
}
|
||||
notice($message, new moodle_url('/admin/tool/phpunit/'));
|
||||
}
|
||||
+21
-56
@@ -20,10 +20,10 @@
|
||||
* Exit codes:
|
||||
* 0 - success
|
||||
* 1 - general error
|
||||
* 130 - coding error
|
||||
* 130 - missing PHPUnit error
|
||||
* 131 - configuration problem
|
||||
* 132 - install new test database
|
||||
* 133 - drop old data, then install new test database
|
||||
* 133 - drop existing data before installing
|
||||
*
|
||||
* @package core
|
||||
* @category phpunit
|
||||
@@ -36,12 +36,14 @@ error_reporting(E_ALL | E_STRICT);
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('log_errors', '1');
|
||||
|
||||
require_once(__DIR__.'/bootstraplib.php');
|
||||
|
||||
if (isset($_SERVER['REMOTE_ADDR'])) {
|
||||
phpunit_bootstrap_error('Unit tests can be executed only from command line!', 1);
|
||||
phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
|
||||
}
|
||||
|
||||
if (defined('PHPUNIT_TEST')) {
|
||||
phpunit_bootstrap_error("PHPUNIT_TEST constant must not be manually defined anywhere!", 130);
|
||||
phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
|
||||
}
|
||||
/** PHPUnit testing framework active */
|
||||
define('PHPUNIT_TEST', true);
|
||||
@@ -52,7 +54,7 @@ if (!defined('PHPUNIT_UTIL')) {
|
||||
}
|
||||
|
||||
if (defined('CLI_SCRIPT')) {
|
||||
phpunit_bootstrap_error('CLI_SCRIPT must not be manually defined in any PHPUnit test scripts', 130);
|
||||
phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
|
||||
}
|
||||
define('CLI_SCRIPT', true);
|
||||
|
||||
@@ -82,37 +84,38 @@ if (isset($CFG->phpunit_directorypermissions)) {
|
||||
}
|
||||
$CFG->filepermissions = ($CFG->directorypermissions & 0666);
|
||||
if (!isset($CFG->phpunit_dataroot)) {
|
||||
phpunit_bootstrap_error('Missing $CFG->phpunit_dataroot in config.php, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
|
||||
}
|
||||
if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
|
||||
phpunit_bootstrap_error('$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
|
||||
}
|
||||
if (!file_exists($CFG->phpunit_dataroot)) {
|
||||
mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
|
||||
}
|
||||
if (!is_dir($CFG->phpunit_dataroot)) {
|
||||
phpunit_bootstrap_error('$CFG->phpunit_dataroot directory can not be created, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
|
||||
}
|
||||
|
||||
if (!is_writable($CFG->phpunit_dataroot)) {
|
||||
// try to fix premissions if possible
|
||||
if (function_exists('posix_getuid')) {
|
||||
$chmod = fileperms($CFG->phpunit_dataroot);
|
||||
if (fileowner($dir) == posix_getuid()) {
|
||||
if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
|
||||
$chmod = $chmod | 0700;
|
||||
chmod($CFG->phpunit_dataroot, $chmod);
|
||||
}
|
||||
}
|
||||
if (!is_writable($CFG->phpunit_dataroot)) {
|
||||
phpunit_bootstrap_error('$CFG->phpunit_dataroot directory is not writable, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
|
||||
}
|
||||
}
|
||||
if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
|
||||
if ($dh = opendir($CFG->phpunit_dataroot)) {
|
||||
while (($file = readdir($dh)) !== false) {
|
||||
if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_store') {
|
||||
if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
|
||||
continue;
|
||||
}
|
||||
phpunit_bootstrap_error('$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?', 131);
|
||||
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
|
||||
}
|
||||
closedir($dh);
|
||||
unset($dh);
|
||||
@@ -126,13 +129,13 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
|
||||
|
||||
// verify db prefix
|
||||
if (!isset($CFG->phpunit_prefix)) {
|
||||
phpunit_bootstrap_error('Missing $CFG->phpunit_prefix in config.php, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
|
||||
}
|
||||
if ($CFG->phpunit_prefix === '') {
|
||||
phpunit_bootstrap_error('$CFG->phpunit_prefix can not be empty, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, '$CFG->phpunit_prefix can not be empty, can not run tests!');
|
||||
}
|
||||
if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
|
||||
phpunit_bootstrap_error('$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!', 131);
|
||||
phpunit_bootstrap_error(131, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
|
||||
}
|
||||
|
||||
// throw away standard CFG settings
|
||||
@@ -187,49 +190,11 @@ if (PHPUNIT_UTIL) {
|
||||
}
|
||||
|
||||
// is database and dataroot ready for testing?
|
||||
$problem = phpunit_util::testing_ready_problem();
|
||||
|
||||
if ($problem) {
|
||||
switch ($problem) {
|
||||
case 132:
|
||||
phpunit_bootstrap_error('Database was not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"', $problem);
|
||||
case 133:
|
||||
phpunit_bootstrap_error('Database was initialised for different version, please use "php admin/tool/phpunit/cli/util.php --drop; php admin/tool/phpunit/cli/util.php --install"', $problem);
|
||||
default:
|
||||
phpunit_bootstrap_error('Unknown problem initialising test database', $problem);
|
||||
}
|
||||
list($errorcode, $message) = phpunit_util::testing_ready_problem();
|
||||
if ($errorcode) {
|
||||
phpunit_bootstrap_error($errorcode, $message);
|
||||
}
|
||||
|
||||
// prepare for the first test run - store fresh globals, reset dataroot, etc.
|
||||
phpunit_util::bootstrap_init();
|
||||
|
||||
|
||||
//=========================================================
|
||||
|
||||
/**
|
||||
* Print error and stop execution
|
||||
* @param string $text An error message to display
|
||||
* @param int $errorcode The error code (see docblock for detailed list)
|
||||
* @return void stops code execution with error code
|
||||
*/
|
||||
function phpunit_bootstrap_error($text, $errorcode = 1) {
|
||||
fwrite(STDERR, $text."\n");
|
||||
exit($errorcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark empty dataroot to be used for testing.
|
||||
* @param string $dataroot The dataroot directory
|
||||
* @return void
|
||||
*/
|
||||
function phpunit_bootstrap_initdataroot($dataroot) {
|
||||
global $CFG;
|
||||
|
||||
if (!file_exists("$dataroot/phpunittestdir.txt")) {
|
||||
file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
|
||||
}
|
||||
chmod("$dataroot/phpunittestdir.txt", $CFG->filepermissions);
|
||||
if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
|
||||
mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* PHPUnit bootstrap function
|
||||
*
|
||||
* Note: these functions must be self contained and must not rely on any library or include
|
||||
*
|
||||
* @package core
|
||||
* @category phpunit
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
/**
|
||||
* Print error and stop execution
|
||||
* @param int $errorcode The exit error code
|
||||
* @param string $text An error message to display
|
||||
* @return void stops code execution with error code
|
||||
*/
|
||||
function phpunit_bootstrap_error($errorcode, $text = '') {
|
||||
switch ($errorcode) {
|
||||
case 1:
|
||||
$text = 'Error: '.$text;
|
||||
break;
|
||||
case 130:
|
||||
$text = 'Can not load PHPUnit PEAR library, is it installed?';
|
||||
case 131:
|
||||
$text = 'Configuration problem: '.$text;
|
||||
break;
|
||||
case 132:
|
||||
$text = 'Database was not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"';
|
||||
break;
|
||||
case 133:
|
||||
$text = 'Database was initialised for different version, please use "php admin/tool/phpunit/cli/util.php --drop; php admin/tool/phpunit/cli/util.php --install"';
|
||||
break;
|
||||
case 134:
|
||||
$text = 'Can not create main configuration file, please verify dirroot permissions."';
|
||||
break;
|
||||
default:
|
||||
$text = empty($text) ? '' : ': '.$text;
|
||||
$text = 'Unknown error '.$errorcode.$text;
|
||||
break;
|
||||
}
|
||||
if (PHPUNIT_UTIL) {
|
||||
// do not write to error stream because we need the error message in PHP exec from web ui
|
||||
echo($text."\n");
|
||||
} else {
|
||||
fwrite(STDERR, $text."\n");
|
||||
}
|
||||
exit($errorcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark empty dataroot to be used for testing.
|
||||
* @param string $dataroot The dataroot directory
|
||||
* @return void
|
||||
*/
|
||||
function phpunit_bootstrap_initdataroot($dataroot) {
|
||||
global $CFG;
|
||||
umask(0);
|
||||
if (!file_exists("$dataroot/phpunittestdir.txt")) {
|
||||
file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
|
||||
}
|
||||
if (fileperms("$dataroot/phpunittestdir.txt") & $CFG->filepermissions != $CFG->filepermissions) {
|
||||
chmod("$dataroot/phpunittestdir.txt", $CFG->filepermissions);
|
||||
}
|
||||
if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
|
||||
mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
|
||||
}
|
||||
}
|
||||
+62
-50
@@ -88,7 +88,7 @@ class phpunit_util {
|
||||
}
|
||||
|
||||
if (!is_array(self::$tabledata)) {
|
||||
phpunit_bootstrap_error('Can not read dataroot/phpunit/tabledata.ser or invalid format!');
|
||||
phpunit_bootstrap_error(1, 'Can not read dataroot/phpunit/tabledata.ser or invalid format, reinitialize test database.');
|
||||
}
|
||||
|
||||
return self::$tabledata;
|
||||
@@ -97,24 +97,11 @@ class phpunit_util {
|
||||
/**
|
||||
* Reset all database tables to default values.
|
||||
* @static
|
||||
* @param bool $logchanges
|
||||
* @param null|PHPUnit_Framework_TestCase $caller
|
||||
* @return bool true if reset done, false if skipped
|
||||
*/
|
||||
public static function reset_database($logchanges = false, PHPUnit_Framework_TestCase $caller = null) {
|
||||
public static function reset_database() {
|
||||
global $DB;
|
||||
|
||||
if ($logchanges) {
|
||||
if (self::$lastdbwrites != $DB->perf_get_writes()) {
|
||||
if ($caller) {
|
||||
$where = ' in testcase: '.get_class($caller).'->'.$caller->getName(true);
|
||||
} else {
|
||||
$where = '';
|
||||
}
|
||||
error_log('warning: unexpected database modification, resetting DB state'.$where);
|
||||
}
|
||||
}
|
||||
|
||||
$tables = $DB->get_tables(false);
|
||||
if (!$tables or empty($tables['config'])) {
|
||||
// not installed yet
|
||||
@@ -230,45 +217,43 @@ class phpunit_util {
|
||||
* Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
|
||||
*
|
||||
* @param bool $logchanges log changes in global state and database in error log
|
||||
* @param PHPUnit_Framework_TestCase $caller caller object, used for logging only
|
||||
* @return void
|
||||
* @static
|
||||
*/
|
||||
public static function reset_all_data($logchanges = false, PHPUnit_Framework_TestCase $caller = null) {
|
||||
public static function reset_all_data($logchanges = false) {
|
||||
global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
|
||||
|
||||
$dbreset = self::reset_database($logchanges, $caller);
|
||||
self::reset_database($logchanges);
|
||||
$warnings = array();
|
||||
|
||||
if ($logchanges) {
|
||||
if ($caller) {
|
||||
$where = ' in testcase: '.get_class($caller).'->'.$caller->getName(true);
|
||||
} else {
|
||||
$where = '';
|
||||
if (self::$lastdbwrites != $DB->perf_get_writes()) {
|
||||
$warnings[] = 'warning: unexpected database modification, resetting DB state';
|
||||
}
|
||||
|
||||
$oldcfg = self::get_global_backup('CFG');
|
||||
$oldsite = self::get_global_backup('SITE');
|
||||
foreach($CFG as $k=>$v) {
|
||||
if (!property_exists($oldcfg, $k)) {
|
||||
error_log('warning: unexpected new $CFG->'.$k.' value'.$where);
|
||||
$warnings[] = 'warning: unexpected new $CFG->'.$k.' value';
|
||||
} else if ($oldcfg->$k !== $CFG->$k) {
|
||||
error_log('warning: unexpected change of $CFG->'.$k.' value'.$where);
|
||||
$warnings[] = 'warning: unexpected change of $CFG->'.$k.' value';
|
||||
}
|
||||
unset($oldcfg->$k);
|
||||
|
||||
}
|
||||
if ($oldcfg) {
|
||||
foreach($oldcfg as $k=>$v) {
|
||||
error_log('warning: unexpected removal of $CFG->'.$k.$where);
|
||||
$warnings[] = 'warning: unexpected removal of $CFG->'.$k;
|
||||
}
|
||||
}
|
||||
|
||||
if ($USER->id != 0) {
|
||||
error_log('warning: unexpected change of $USER'.$where);
|
||||
$warnings[] = 'warning: unexpected change of $USER';
|
||||
}
|
||||
|
||||
if ($COURSE->id != $oldsite->id) {
|
||||
error_log('warning: unexpected change of $COURSE'.$where);
|
||||
$warnings[] = 'warning: unexpected change of $COURSE';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,6 +299,11 @@ class phpunit_util {
|
||||
|
||||
// fix PHP settings
|
||||
error_reporting($CFG->debug);
|
||||
|
||||
if ($warnings) {
|
||||
$warnings = implode("\n", $warnings);
|
||||
trigger_error($warnings, E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -382,41 +372,42 @@ class phpunit_util {
|
||||
* Is this site initialised to run unit tests?
|
||||
*
|
||||
* @static
|
||||
* @return int error code, 0 means ok
|
||||
* @return int array errorcode=>message, 0 means ok
|
||||
*/
|
||||
public static function testing_ready_problem() {
|
||||
global $DB, $CFG;
|
||||
global $CFG, $DB;
|
||||
|
||||
$tables = $DB->get_tables(false);
|
||||
|
||||
if (!self::is_test_site()) {
|
||||
return 131;
|
||||
}
|
||||
|
||||
$tables = $DB->get_tables(true);
|
||||
|
||||
if (!$tables) {
|
||||
return 132;
|
||||
}
|
||||
|
||||
if (!get_config('core', 'phpunittest')) {
|
||||
return 131;
|
||||
// dataroot was verified in bootstrap, so it must be DB
|
||||
return array(131, 'Can not use test database, try changing prefix');
|
||||
}
|
||||
|
||||
if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser")) {
|
||||
return 131;
|
||||
if (empty($table)) {
|
||||
return array(132, '');
|
||||
} else {
|
||||
return array(133, '');
|
||||
}
|
||||
}
|
||||
|
||||
if (!file_exists("$CFG->dataroot/phpunit/versionshash.txt")) {
|
||||
return 131;
|
||||
if (empty($table)) {
|
||||
return array(132, '');
|
||||
} else {
|
||||
return array(133, '');
|
||||
}
|
||||
}
|
||||
|
||||
$hash = phpunit_util::get_version_hash();
|
||||
$oldhash = file_get_contents("$CFG->dataroot/phpunit/versionshash.txt");
|
||||
|
||||
if ($hash !== $oldhash) {
|
||||
return 133;
|
||||
return array(133, '');
|
||||
}
|
||||
|
||||
return 0;
|
||||
return array(0, '');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -500,11 +491,17 @@ class phpunit_util {
|
||||
$data = serialize($data);
|
||||
@unlink("$CFG->dataroot/phpunit/tabledata.ser");
|
||||
file_put_contents("$CFG->dataroot/phpunit/tabledata.ser", $data);
|
||||
if (fileperms("$CFG->dataroot/phpunit/tabledata.ser") & $CFG->filepermissions != $CFG->filepermissions) {
|
||||
chmod("$CFG->dataroot/phpunit/tabledata.ser", $CFG->filepermissions);
|
||||
}
|
||||
|
||||
// hash all plugin versions - helps with very fast detection of db structure changes
|
||||
$hash = phpunit_util::get_version_hash();
|
||||
@unlink("$CFG->dataroot/phpunit/versionshash.txt");
|
||||
file_put_contents("$CFG->dataroot/phpunit/versionshash.txt", $hash);
|
||||
if (fileperms("$CFG->dataroot/phpunit/versionshash.txt") & $CFG->filepermissions != $CFG->filepermissions) {
|
||||
chmod("$CFG->dataroot/phpunit/versionshash.txt", $CFG->filepermissions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -553,9 +550,9 @@ class phpunit_util {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds /phpunit.xml file using defaults from /phpunit.xml.dist
|
||||
* Builds dirroot/phpunit.xml and dataroot/phpunit/webrunner.xml file using defaults from /phpunit.xml.dist
|
||||
* @static
|
||||
* @return void
|
||||
* @return bool true means main config file created, false means only dataroot file created
|
||||
*/
|
||||
public static function build_config_file() {
|
||||
global $CFG;
|
||||
@@ -590,8 +587,23 @@ class phpunit_util {
|
||||
|
||||
$data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
|
||||
|
||||
@unlink("$CFG->dirroot/phpunit.xml");
|
||||
file_put_contents("$CFG->dirroot/phpunit.xml", $data);
|
||||
$result = false;
|
||||
if (is_writable($CFG->dirroot)) {
|
||||
if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
|
||||
if (fileperms("$CFG->dirroot/phpunit.xml") & $CFG->filepermissions != $CFG->filepermissions) {
|
||||
chmod("$CFG->dirroot/phpunit.xml", $CFG->filepermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
// relink - it seems that xml:base does not work in phpunit xml files
|
||||
$data = str_replace('lib/phpunit/', "$CFG->dirroot/lib/phpunit/", $data);
|
||||
$data = preg_replace('|<directory suffix="_test.php">([^<]+)</directory>|', '<directory suffix="_test.php">'.$CFG->dirroot.'/$1</directory>', $data);
|
||||
file_put_contents("$CFG->dataroot/phpunit/webrunner.xml", $data);
|
||||
if (fileperms("$CFG->dirroot/phpunit.xml") & $CFG->filepermissions != $CFG->filepermissions) {
|
||||
chmod("$CFG->dirroot/phpunit.xml", $CFG->filepermissions);
|
||||
}
|
||||
|
||||
return (bool)$result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -757,7 +769,7 @@ class basic_testcase extends PHPUnit_Framework_TestCase {
|
||||
*/
|
||||
public function runBare() {
|
||||
parent::runBare();
|
||||
phpunit_util::reset_all_data(true, $this);
|
||||
phpunit_util::reset_all_data(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -806,7 +818,7 @@ class advanced_testcase extends PHPUnit_Framework_TestCase {
|
||||
// keep all data untouched for other tests
|
||||
} else {
|
||||
// reset but log what changed
|
||||
phpunit_util::reset_all_data(true, $this);
|
||||
phpunit_util::reset_all_data(true);
|
||||
}
|
||||
|
||||
$this->resetAfterTest = null;
|
||||
|
||||
@@ -56,4 +56,3 @@ TODO
|
||||
* convert remaining tests
|
||||
* delete all simpletests
|
||||
* hide old SimpleTests in UI and delete Functional DB tests
|
||||
* optional support for execution of tests and cli/util.php from web UI (to be implemented via shell execution)
|
||||
|
||||
@@ -136,6 +136,16 @@ class core_phpunit_basic_testcase extends basic_testcase {
|
||||
global $COURSE;
|
||||
$COURSE->id = 10;
|
||||
}
|
||||
|
||||
public function test_all_modifications() {
|
||||
global $DB, $CFG, $USER, $COURSE;
|
||||
$DB->set_field('user', 'confirmed', 1, array('id'=>-1));
|
||||
$CFG->xx = 'yy';
|
||||
unset($CFG->admin);
|
||||
$CFG->rolesactive = 0;
|
||||
$USER->id = 10;
|
||||
$COURSE->id = 10;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="lib/phpunit/phpunit.xsd"
|
||||
bootstrap="lib/phpunit/bootstrap.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
|
||||
Reference in New Issue
Block a user