Commit 5bd40408 authored by Petr Skoda's avatar Petr Skoda
Browse files

MDL-31857basic phpunit support

Thanks Eloy Lafuente, Tim Hunt and Sam Hemelryk for valuable feedback and ideas.
parent a2b30aa8
......@@ -25,3 +25,4 @@ CVS
/.project
/.buildpath
/.cache
/phpunit.xml
\ No newline at end of file
<?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 related utilities.
*
* Exit codes:
* 0 - success
* 1 - general error
* 130 - coding error
* 131 - configuration problem
* 133 - drop existing data before installing
*
* @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('PHPUNIT_CLI_UTIL', true);
require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
require_once($CFG->libdir.'/phpunit/lib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/upgradelib.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/pluginlib.php');
require_once($CFG->libdir.'/installlib.php');
// now get cli options
list($options, $unrecognized) = cli_get_params(
array(
'drop' => false,
'install' => false,
'buildconfig' => false,
'help' => false,
),
array(
'h' => 'help'
)
);
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
$drop = $options['drop'];
$install = $options['install'];
$buildconfig = $options['buildconfig'];
if ($options['help'] or (!$drop and !$install and !$buildconfig)) {
$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
-h, --help Print out this help
Example:
\$/usr/bin/php lib/phpunit/tool.php
";
echo $help;
die;
}
if ($buildconfig) {
phpunit_util::build_config_file();
exit(0);
} else if ($drop) {
phpunit_util::drop_site();
// note: we must stop here because $CFG is messed up and we can not reinstall, sorry
exit(0);
} else if ($install) {
phpunit_util::install_site();
exit(0);
}
<?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 info
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
*/
define('NO_OUTPUT_BUFFERING', true);
require(dirname(__FILE__) . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup('toolphpunit');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'tool_phpunit'));
echo $OUTPUT->box_start();
$info = file_get_contents("$CFG->libdir/phpunit/readme.md");
echo markdown_to_html($info);
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
\ No newline at end of file
<?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/>.
/**
* Strings for component 'tool_phpunit'
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'PHPUnit tests';
<?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 integration
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$ADMIN->add('development', new admin_externalpage('toolphpunit', get_string('pluginname', 'tool_phpunit'), "$CFG->wwwroot/$CFG->admin/tool/phpunit/index.php"));
<?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/>.
/**
* Plugin version info
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2012030800; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012030100; // Requires this Moodle version
$plugin->component = 'tool_phpunit'; // Full name of the plugin (used for diagnostics)
......@@ -479,7 +479,13 @@ $CFG->admin = 'admin';
// Example:
// $CFG->forced_plugin_settings = array('pluginname' => array('settingname' => 'value', 'secondsetting' => 'othervalue'),
// 'otherplugin' => array('mysetting' => 'myvalue', 'thesetting' => 'thevalue'));
//
//=========================================================================
// 9. PHPUNIT SUPPORT
//=========================================================================
// $CFG->phpunit_prefix = 'phpu_';
// $CFG->phpunit_dataroot = '/home/example/phpu_moodledata';
// $CFG->phpunit_directorypermissions = 02777; // optional
//=========================================================================
// ALL DONE! To continue installation, visit your main page with a browser
......
......@@ -217,7 +217,7 @@ $ACCESSLIB_PRIVATE->capabilities = null; // detailed information about th
*/
function accesslib_clear_all_caches_for_unit_testing() {
global $UNITTEST, $USER;
if (empty($UNITTEST->running)) {
if (empty($UNITTEST->running) and !PHPUNITTEST) {
throw new coding_exception('You must not call clear_all_caches outside of unit tests.');
}
......
......@@ -112,18 +112,15 @@ class database_manager {
/**
* Reset a sequence to the id field of a table.
* @param string $table Name of table.
* @throws ddl_exception|ddl_table_missing_exception Exception thrown upon reset errors.
* @param string|xmldb_table $table Name of table.
* @throws ddl_exception thrown upon reset errors.
*/
public function reset_sequence($table) {
if (!is_string($table) and !($table instanceof xmldb_table)) {
throw new ddl_exception('ddlunknownerror', NULL, 'incorrect table parameter!');
}
/// Check the table exists
if (!$this->table_exists($table)) {
throw new ddl_table_missing_exception($table);
}
// Do not test if table exists because it is slow
if (!$sqlarr = $this->generator->getResetSequenceSQL($table)) {
throw new ddl_exception('ddlunknownerror', null, 'table reset sequence sql not generated');
......
......@@ -8,6 +8,8 @@ if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
global $CFG;
require_once($CFG->libdir . '/adminlib.php');
class ddl_test extends UnitTestCase {
......
......@@ -7763,7 +7763,7 @@ function get_plugin_types($fullpaths=true) {
function get_plugin_list($plugintype) {
global $CFG;
$ignored = array('CVS', '_vti_cnf', 'simpletest', 'db', 'yui', 'phpunit');
$ignored = array('CVS', '_vti_cnf', 'simpletest', 'db', 'yui', 'tests');
if ($plugintype == 'auth') {
// Historically we have had an auth plugin called 'db', so allow a special case.
$key = array_search('db', $ignored);
......
<?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/>.
/**
* Prepares PHPUnit environment, it is called automatically.
*
* Exit codes:
* 0 - success
* 1 - general error
* 130 - coding error
* 131 - configuration problem
* 132 - drop data, then install new test database
*
* @package core_core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// we want to know about all problems
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');
ini_set('log_errors', '1');
if (isset($_SERVER['REMOTE_ADDR'])) {
phpunit_bootstrap_error('Unit tests can be executed only from commandline!', 1);
}
if (defined('PHPUNITTEST')) {
phpunit_bootstrap_error("PHPUNITTEST constant must not be manually defined anywhere!", 130);
}
define('PHPUNITTEST', true);
if (defined('CLI_SCRIPT')) {
phpunit_bootstrap_error('CLI_SCRIPT must not be manually defined in any PHPUnit test scripts', 130);
}
define('CLI_SCRIPT', true);
define('NO_OUTPUT_BUFFERING', true);
// only load CFG from config.php
define('ABORT_AFTER_CONFIG', true);
require(__DIR__ . '/../../config.php');
// remove error handling overrides done in config.php
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('log_errors', '1');
// prepare dataroot
umask(0);
if (isset($CFG->phpunit_directorypermissions)) {
$CFG->directorypermissions = $CFG->phpunit_directorypermissions;
} else {
$CFG->directorypermissions = 02777;
}
$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);
}
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);
}
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);
}
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()) {
$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);
}
}
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') {
continue;
}
phpunit_bootstrap_error('$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?', 131);
}
closedir($dh);
unset($dh);
unset($file);
}
// now we are 100% sure this dir is used only for phpunit tests
phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot);
}
// verify db prefix
if (!isset($CFG->phpunit_prefix)) {
phpunit_bootstrap_error('Missing $CFG->phpunit_dataroot in config.php, can not run tests!', 131);
}
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);
}
// throw away standard CFG settings
$CFG->dataroot = $CFG->phpunit_dataroot;
$CFG->prefix = $CFG->phpunit_prefix;
$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions');
$productioncfg = (array)$CFG;
$CFG = new stdClass();
foreach ($productioncfg as $key=>$value) {
if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
// ignore
continue;
}
$CFG->{$key} = $value;
}
unset($key);
unset($value);
unset($allowed);
unset($productioncfg);
// force the same CFG settings in all sites
$CFG->debug = (E_ALL | E_STRICT | 38911); // can not use DEBUG_DEVELOPER here
$CFG->debugdisplay = 1;
error_reporting($CFG->debug);
ini_set('display_errors', '1');
ini_set('log_errors', '0');
$CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
$CFG->cachetext = 0; // disable this very nasty setting
// some ugly hacks
$CFG->themerev = 1;
$CFG->jsrev = 1;
// load test case stub classes and other stuff
require_once("$CFG->dirroot/lib/phpunit/lib.php");
// finish moodle init
define('ABORT_AFTER_CONFIG_CANCEL', true);
require("$CFG->dirroot/lib/setup.php");
raise_memory_limit(MEMORY_EXTRA);
if (defined('PHPUNIT_CLI_UTIL')) {
// all other tests are done in the CLI scripts...
return;
}
if (!phpunit_util::is_testing_ready()) {
phpunit_bootstrap_error('Database is not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"', 132);
}
// refresh data in all tables, clear caches, etc.
phpunit_util::reset_all_data();
// store fresh globals
phpunit_util::init_globals();
//=========================================================
/**
* Print error and stop execution
* @param $text
* @param int $errorcode
* @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 $dataroot
* @return void
*/
function phpunit_bootstrap_initdataroot($dataroot) {
global $CFG;
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);
}
}
<?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/>.
/**
* Various PHPUnit classes and functions
*
* @package core_core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once 'PHPUnit/Autoload.php'; // necessary when loaded from cli/util.php script
/**
* Collection of utility methods.
*
* @package core_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class phpunit_util {
/**
* @var array original content of all database tables
*/
protected static $tabledata = null;
protected static $globals = array();
/**
* Returns contents of all tables right after installation.
* @static
* @return array $table=>$records
*/
protected static function get_tabledata() {
global $CFG;
if (!isset(self::$tabledata)) {
$data = file_get_contents("$CFG->dataroot/phpunit/tabledata.ser");
self::$tabledata = unserialize($data);
}
if (!is_array(self::$tabledata)) {
phpunit_bootstrap_error('Can not read dataroot/phpunit/tabledata.ser or invalid format!');
}
return self::$tabledata;
}
/**
* Initialise CFG using data from fresh new install.
* @static
*/
public static function initialise_cfg() {
global $CFG, $DB;
if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser")) {
// most probably PHPUnit CLI installer
return;
}
if (!$DB->get_manager()->table_exists('config') or !$DB->count_records('config')) {
@unlink("$CFG->dataroot/phpunit/tabledata.ser");
@unlink("$CFG->dataroot/phpunit/versionshash.txt");
self::$tabledata = null;
return;
}
$data = self::get_tabledata();
foreach($data['config'] as $record) {
$name = $record->name;
$value = $record->value;
if (property_exists($CFG, $name)) {
// config.php settings always take precedence
continue;
}
$CFG->{$name} = $value;
}
}
/**
* Reset contents of all database tables to initial values, reset caches, etc.
*
* Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
*
* @static
*/
public static function reset_all_data() {
global $DB, $CFG;
$data = self::get_tabledata();
$trans = $DB->start_delegated_transaction(); // faster and safer
foreach ($data as $table=>$records) {
$DB->delete_records($table, array());
$resetseq = null;
foreach ($records as $record) {
if (is_null($resetseq)) {
$resetseq = property_exists($record, 'id');
}
$DB->import_record($table, $record, false, true);
}
if ($resetseq === true) {
$DB->get_manager()->reset_sequence($table, true);