Commit 54e4d1b3 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

Merge branch 'w16_MDL-32400_m23_phpunit4' of git://github.com/skodak/moodle

parents aa4ebbe5 6b04fdc0
@ECHO OFF
ECHO Initialising Moodle PHPUnit test environment...
CALL php %~dp0\util.php --diag > NUL 2>&1
IF ERRORLEVEL 133 GOTO drop
IF ERRORLEVEL 132 GOTO install
IF ERRORLEVEL 1 GOTO unknown
GOTO done
:drop
CALL php %~dp0\util.php --drop
IF ERRORLEVEL 1 GOTO done
:install
CALL php %~dp0\util.php --install
GOTO done
:unknown
CALL php %~dp0\util.php --diag
:done
......@@ -43,13 +43,13 @@ exec("php util.php --diag", $output, $code);
if ($code == 0) {
// everything is ready
} else if ($code == 132) {
} else if ($code == PHPUNIT_EXITCODE_INSTALL) {
passthru("php util.php --install", $code);
if ($code != 0) {
exit($code);
}
} else if ($code == 133) {
} else if ($code == PHPUNIT_EXITCODE_REINSTALL) {
passthru("php util.php --drop", $code);
passthru("php util.php --install", $code);
if ($code != 0) {
......
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
CLIDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
UTIL="$CLIDIR/util.php"
echo "Initialising Moodle PHPUnit test environment..."
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
if [ $DIAG -gt 0 ] ; then
echo $DIGERROR
exit $DIAG
fi
fi
fi
php $UTIL --buildconfig
......@@ -17,14 +17,7 @@
/**
* PHPUnit related utilities.
*
* Exit codes:
* 0 - success
* 1 - general error
* 130 - missing PHPUnit library error
* 131 - configuration problem
* 132 - install new test database
* 133 - drop existing data before installing
* 134 - can not create main phpunit.xml
* Exit codes: {@see phpunit_bootstrap_error()}
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
......@@ -72,12 +65,8 @@ if ($options['phpunitdir']) {
}
// verify PHPUnit libs are loaded
if (!@include_once('PHPUnit/Autoload.php')) {
phpunit_bootstrap_error(130);
}
if (!@include_once('PHPUnit/Extensions/Database/Autoload.php')) {
phpunit_bootstrap_error(130);
if (!include_once('PHPUnit/Autoload.php')) {
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
}
if ($options['run']) {
......@@ -147,7 +136,7 @@ if ($diag) {
if (phpunit_util::build_config_file()) {
exit(0);
} else {
phpunit_bootstrap_error(134);
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, 'Can not create phpunit.xml configuration file, verify dirroot permissions');
}
} else if ($drop) {
......
......@@ -67,7 +67,7 @@ if ($execute) {
if ($code == 0) {
// everything is ready
} else if ($code == 132) {
} else if ($code == PHPUNIT_EXITCODE_INSTALL) {
tool_phpunit_header();
echo $OUTPUT->box_start('generalbox');
echo '<pre>';
......@@ -87,7 +87,7 @@ if ($execute) {
echo $OUTPUT->footer();
die();
} else if ($code == 133) {
} else if ($code == PHPUNIT_EXITCODE_REINSTALL) {
tool_phpunit_header();
echo $OUTPUT->box_start('generalbox');
echo '<pre>';
......
......@@ -262,6 +262,9 @@ class grade_base_testcase extends advanced_testcase {
protected function load_grade_items() {
global $DB;
// purge all items created by module generators
$DB->delete_records('grade_items', array('itemtype'=>'mod'));
$course_category = grade_category::fetch_course_category($this->course->id);
// id = 0
......
......@@ -18,13 +18,7 @@
* Prepares PHPUnit environment, the phpunit.xml configuration
* must specify this file as bootstrap.
*
* Exit codes:
* 0 - success
* 1 - general error
* 130 - missing PHPUnit library error
* 131 - configuration problem
* 132 - install new test database
* 133 - drop existing data before installing
* Exit codes: {@see phpunit_bootstrap_error()}
*
* @package core
* @category phpunit
......@@ -63,10 +57,14 @@ $phpunitversion = PHPUnit_Runner_Version::id();
if ($phpunitversion === '@package_version@') {
// library checked out from git, let's hope dev knows that 3.6.0 is required
} else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
phpunit_bootstrap_error(129, $phpunitversion);
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
}
unset($phpunitversion);
if (!include_once('PHPUnit/Extensions/Database/Autoload.php')) {
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
}
define('NO_OUTPUT_BUFFERING', true);
// only load CFG from config.php, stop ASAP in lib/setup.php
......@@ -93,16 +91,16 @@ if (isset($CFG->phpunit_directorypermissions)) {
}
$CFG->filepermissions = ($CFG->directorypermissions & 0666);
if (!isset($CFG->phpunit_dataroot)) {
phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
}
if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
phpunit_bootstrap_error(131, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$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(131, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
}
if (!is_writable($CFG->phpunit_dataroot)) {
......@@ -115,7 +113,7 @@ if (!is_writable($CFG->phpunit_dataroot)) {
}
}
if (!is_writable($CFG->phpunit_dataroot)) {
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
}
}
if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
......@@ -124,7 +122,7 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
continue;
}
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
}
closedir($dh);
unset($dh);
......@@ -137,13 +135,13 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
// verify db prefix
if (!isset($CFG->phpunit_prefix)) {
phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
}
if ($CFG->phpunit_prefix === '') {
phpunit_bootstrap_error(131, '$CFG->phpunit_prefix can not be empty, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
}
if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
phpunit_bootstrap_error(131, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
}
// override CFG settings if necessary and throw away extra CFG settings
......
......@@ -25,6 +25,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('PHPUNIT_EXITCODE_PHPUNITMISSING', 129);
define('PHPUNIT_EXITCODE_PHPUNITWRONG', 130);
define('PHPUNIT_EXITCODE_PHPUNITEXTMISSING', 131);
define('PHPUNIT_EXITCODE_CONFIGERROR', 135);
define('PHPUNIT_EXITCODE_CONFIGWARNING', 136);
define('PHPUNIT_EXITCODE_INSTALL', 140);
define('PHPUNIT_EXITCODE_REINSTALL', 141);
/**
* Print error and stop execution
* @param int $errorcode The exit error code
......@@ -39,35 +47,35 @@ function phpunit_bootstrap_error($errorcode, $text = '') {
case 1:
$text = 'Error: '.$text;
break;
case 129:
case PHPUNIT_EXITCODE_PHPUNITMISSING:
$text = "Moodle can not find PHPUnit PEAR library";
break;
case PHPUNIT_EXITCODE_PHPUNITWRONG:
$text = 'Moodle requires PHPUnit 3.6.x, '.$text.' is not compatible';
break;
case 130:
$text = 'Moodle can not find PHPUnit PEAR library or necessary PHPUnit extension';
case PHPUNIT_EXITCODE_PHPUNITEXTMISSING:
$text = 'Moodle can not find required PHPUnit extension '.$text;
break;
case 131:
$text = 'Moodle configuration problem: '.$text;
case PHPUNIT_EXITCODE_CONFIGERROR:
$text = "Moodle PHPUnit environment configuration error:\n".$text;
break;
case 132:
$text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/util.php --install";
case PHPUNIT_EXITCODE_CONFIGWARNING:
$text = "Moodle PHPUnit environment configuration warning:\n".$text;
break;
case 133:
$text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/util.php --drop\n php admin/tool/phpunit/cli/util.php --install";
case PHPUNIT_EXITCODE_INSTALL:
$text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/init.php";
break;
case 134:
$text = 'Moodle can not create PHPUnit configuration file, please verify dirroot permissions';
case PHPUNIT_EXITCODE_REINSTALL:
$text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/init.php";
break;
default:
$text = empty($text) ? '' : ': '.$text;
$text = 'Unknown error '.$errorcode.$text;
break;
}
if (defined('PHPUNIT_UTIL') and PHPUNIT_UTIL) {
// do not write to error stream because we need the error message in PHP exec result from web ui
echo($text."\n");
} else {
fwrite(STDERR, $text."\n");
}
// do not write to error stream because we need the error message in PHP exec result from web ui
echo($text."\n");
exit($errorcode);
}
......
......@@ -593,20 +593,20 @@ abstract class phpunit_module_generator {
/**
* Create course module and link it to course
* @param stdClass $instance
* @param int $courseid
* @param array $options: section, visible
* @return stdClass $cm instance
* @return int $cm instance id
*/
protected function create_course_module(stdClass $instance, array $options) {
protected function precreate_course_module($courseid, array $options) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
$modulename = $this->get_modulename();
$cm = new stdClass();
$cm->course = $instance->course;
$cm->course = $courseid;
$cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
$cm->instance = $instance->id;
$cm->instance = 0;
$cm->section = isset($options['section']) ? $options['section'] : 0;
$cm->idnumber = isset($options['idnumber']) ? $options['idnumber'] : 0;
$cm->added = time();
......@@ -627,11 +627,28 @@ abstract class phpunit_module_generator {
add_mod_to_section($cm);
$cm = get_coursemodule_from_id($modulename, $cm->id, $cm->course, true, MUST_EXIST);
return $cm->id;
}
/**
* Called after *_add_instance()
* @param int $id
* @param int $cmid
* @return stdClass module instance
*/
protected function post_add_instance($id, $cmid) {
global $DB;
$DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
$instance = $DB->get_record($this->get_modulename(), array('id'=>$id), '*', MUST_EXIST);
$cm = get_coursemodule_from_id($this->get_modulename(), $cmid, $instance->course, true, MUST_EXIST);
context_module::instance($cm->id);
return $cm;
$instance->cmid = $cm->id;
return $instance;
}
/**
......
......@@ -36,34 +36,25 @@ require_once 'PHPUnit/Extensions/Database/Autoload.php';
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class phpunit_util {
/**
* @var array original content of all database tables
*/
/** @var string current version hash from php files */
protected static $versionhash = null;
/** @var array original content of all database tables*/
protected static $tabledata = null;
/**
* @var array original structure of all database tables
*/
/** @var array original structure of all database tables */
protected static $tablestructure = null;
/**
* @var array An array of original globals, restored after each test
*/
/** @var array An array of original globals, restored after each test */
protected static $globals = array();
/**
* @var int last value of db writes counter, used for db resetting
*/
/** @var int last value of db writes counter, used for db resetting */
public static $lastdbwrites = null;
/**
* @var phpunit_data_generator
*/
/** @var phpunit_data_generator */
protected static $generator = null;
/**
* @var resource used for prevention of parallel test execution
*/
/** @var resource used for prevention of parallel test execution */
protected static $lockhandle = null;
/**
......@@ -116,6 +107,30 @@ class phpunit_util {
}
}
/**
* Load global $CFG;
* @internal
* @static
* @return void
*/
public static function initialise_cfg() {
global $DB;
$dbhash = false;
try {
$dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'));
} catch (Exception $e) {
// not installed yet
initialise_cfg();
return;
}
if ($dbhash !== phpunit_util::get_version_hash()) {
// do not set CFG - the only way forward is to drop and reinstall
return;
}
// standard CFG init
initialise_cfg();
}
/**
* Get data generator
* @static
......@@ -625,26 +640,31 @@ class phpunit_util {
if (!self::is_test_site()) {
// dataroot was verified in bootstrap, so it must be DB
return array(131, 'Can not use test database, try changing prefix');
return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
}
if (empty($tables)) {
return array(132, '');
return array(PHPUNIT_EXITCODE_INSTALL, '');
}
if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser") or !file_exists("$CFG->dataroot/phpunit/tablestructure.ser")) {
return array(133, '');
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
if (!file_exists("$CFG->dataroot/phpunit/versionshash.txt")) {
return array(133, '');
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
$hash = phpunit_util::get_version_hash();
$oldhash = file_get_contents("$CFG->dataroot/phpunit/versionshash.txt");
if ($hash !== $oldhash) {
return array(133, '');
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
$dbhash = get_config('core', 'phpunittest');
if ($hash !== $dbhash) {
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
return array(0, '');
......@@ -662,7 +682,7 @@ class phpunit_util {
global $DB, $CFG;
if (!self::is_test_site()) {
phpunit_bootstrap_error(131, 'Can not drop non-test site!!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
}
// purge dataroot
......@@ -707,13 +727,13 @@ class phpunit_util {
global $DB, $CFG;
if (!self::is_test_site()) {
phpunit_bootstrap_error(131, 'Can not install on non-test site!!');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
}
if ($DB->get_tables()) {
list($errorcode, $message) = phpunit_util::testing_ready_problem();
if ($errorcode) {
phpunit_bootstrap_error(133, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
} else {
phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
}
......@@ -731,7 +751,8 @@ class phpunit_util {
update_timezone_records($timezones);
// add test db flag
set_config('phpunittest', 'phpunittest');
$hash = phpunit_util::get_version_hash();
set_config('phpunittest', $hash);
// store data for all tables
$data = array();
......@@ -756,7 +777,6 @@ class phpunit_util {
phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/tablestructure.ser");
// hash all plugin versions - helps with very fast detection of db structure changes
$hash = phpunit_util::get_version_hash();
file_put_contents("$CFG->dataroot/phpunit/versionshash.txt", $hash);
phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/versionshash.txt", $hash);
}
......@@ -769,6 +789,10 @@ class phpunit_util {
public static function get_version_hash() {
global $CFG;
if (self::$versionhash) {
return self::$versionhash;
}
$versions = array();
// main version first
......@@ -801,9 +825,9 @@ class phpunit_util {
}
}
$hash = sha1(serialize($versions));
self::$versionhash = sha1(serialize($versions));
return $hash;
return self::$versionhash;
}
/**
......@@ -831,7 +855,7 @@ class phpunit_util {
if (!file_exists("$fullplug/tests/")) {
continue;
}
$dir = preg_replace("|$CFG->dirroot/|", '', $fullplug, 1);
$dir = substr($fullplug, strlen($CFG->dirroot)+1);
$dir .= '/tests';
$component = $type.'_'.$plug;
......@@ -850,9 +874,12 @@ class phpunit_util {
phpunit_boostrap_fix_file_permissions("$CFG->dirroot/phpunit.xml");
}
}
// relink - it seems that xml:base does not work in phpunit xml files, remove this nasty hack if you find a way to set xml base for relative refs
$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);
$data = str_replace('lib/phpunit/', $CFG->dirroot.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'phpunit'.DIRECTORY_SEPARATOR, $data);
$data = preg_replace('|<directory suffix="_test.php">([^<]+)</directory>|',
'<directory suffix="_test.php">'.$CFG->dirroot.(DIRECTORY_SEPARATOR === '\\' ? '\\\\' : DIRECTORY_SEPARATOR).'$1</directory>',
$data);
file_put_contents("$CFG->dataroot/phpunit/webrunner.xml", $data);
phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/webrunner.xml");
......
......@@ -13,7 +13,7 @@ Installation
1. install PEAR package manager - see [PEAR Manual](http://pear.php.net/manual/en/installation.php)
2. install PHPUnit package and phpunit/DbUnit extension - see [PHPUnit installation documentation](http://www.phpunit.de/manual/current/en/installation.html)
3. edit main config.php - add `$CFG->phpunit_prefix` and `$CFG->phpunit_dataroot` - see config-dist.php
4. execute `admin/tool/phpunit/cli/init.sh` to initialise the test environemnt, repeat it after every upgrade or installation of plugins
4. execute `php admin/tool/phpunit/cli/init.php` to initialise the test environemnt, repeat it after every upgrade or installation of plugins
Test execution
......@@ -29,7 +29,7 @@ How to add more tests?
2. add `local/mytest/tests/my_test.php` file with `local_my_testcase` class that extends `basic_testcase` or `advanced_testcase`
3. add some test_*() methods
4. execute your new test case `phpunit local_my_testcase local/mytest/tests/my_test.php`
5. execute `admin/tool/phpunit/cli/init.sh` to get the plugin tests included in main phpunit.xml configuration file
5. execute `php admin/tool/phpunit/cli/init.php` to get the plugin tests included in main phpunit.xml configuration file
How to convert existing tests?
......
......@@ -479,7 +479,12 @@ if (isset($CFG->debug)) {
}
// Load up any configuration from the config table
initialise_cfg();
if (PHPUNIT_TEST) {
phpunit_util::initialise_cfg();
} else {
initialise_cfg();
}
// Verify upgrade is not running unless we are in a script that needs to execute in any case
if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
......
This diff is collapsed.
......@@ -2708,8 +2708,12 @@ function assignment_update_instance($assignment){
* Adds an assignment instance
*
* This is done by calling the add_instance() method of the assignment type class
*
* @param stdClass $assignment
* @param mod_assignment_mod_form $mform
* @return int intance id
*/
function assignment_add_instance($assignment) {
function assignment_add_instance($assignment, $mform = null) {
global $CFG;
$assignment->assignmenttype = clean_param($assignment->assignmenttype, PARAM_PLUGIN);
......
......@@ -39,11 +39,11 @@ class mod_assignment_generator extends phpunit_module_generator {
/**
* Create new assignment module instance
* @param array|stdClass $record
* @param array $options
* @param array $options (mostly course_module properties)
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $DB, $CFG;
global $CFG;
require_once("$CFG->dirroot/mod/assignment/locallib.php");
$this->instancecount++;
......@@ -52,6 +52,9 @@ class mod_assignment_generator extends phpunit_module_generator {
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'assignment').' '.$i;
}
......@@ -67,15 +70,17 @@ class mod_assignment_generator extends phpunit_module_generator {
</