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:
Petr Skoda
2012-04-10 15:27:01 +02:00
parent 4b17369567
commit 7b0ff21385
10 changed files with 418 additions and 132 deletions
+21 -56
View File
@@ -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);
}
}
+84
View File
@@ -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
View File
@@ -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;
-1
View File
@@ -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)