2012-03-21 10:31:37 +01:00
< ? 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/>.
/**
2012-04-05 19:03:31 +02:00
* Prepares PHPUnit environment , the phpunit . xml configuration
* must specify this file as bootstrap .
2012-03-21 10:31:37 +01:00
*
2012-04-13 12:45:18 +02:00
* Exit codes : { @ see phpunit_bootstrap_error ()}
2012-03-21 10:31:37 +01:00
*
2012-03-22 10:45:17 +13:00
* @ package core
2012-03-21 10:31:37 +01:00
* @ 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' );
2013-07-01 21:06:44 +02:00
// Make sure OPcache does not strip comments, we need them in phpunit!
if ( ini_get ( 'opcache.enable' ) and strtolower ( ini_get ( 'opcache.enable' )) !== 'off' ) {
if ( ! ini_get ( 'opcache.save_comments' ) or strtolower ( ini_get ( 'opcache.save_comments' )) === 'off' ) {
ini_set ( 'opcache.enable' , 0 );
} else {
ini_set ( 'opcache.load_comments' , 1 );
}
}
2013-07-04 19:09:21 +02:00
if ( ! defined ( 'IGNORE_COMPONENT_CACHE' )) {
define ( 'IGNORE_COMPONENT_CACHE' , true );
}
2012-04-05 01:25:06 +02:00
require_once ( __DIR__ . '/bootstraplib.php' );
2013-01-11 17:34:24 +08:00
require_once ( __DIR__ . '/../testing/lib.php' );
2013-06-24 16:59:54 +02:00
require_once ( __DIR__ . '/classes/autoloader.php' );
2012-04-05 01:25:06 +02:00
2012-03-21 10:31:37 +01:00
if ( isset ( $_SERVER [ 'REMOTE_ADDR' ])) {
2012-04-05 01:25:06 +02:00
phpunit_bootstrap_error ( 1 , 'Unit tests can be executed only from command line!' );
2012-03-21 10:31:37 +01:00
}
2012-03-31 23:51:02 +02:00
if ( defined ( 'PHPUNIT_TEST' )) {
2012-04-05 01:25:06 +02:00
phpunit_bootstrap_error ( 1 , " PHPUNIT_TEST constant must not be manually defined anywhere! " );
2012-03-31 23:51:02 +02:00
}
/** PHPUnit testing framework active */
define ( 'PHPUNIT_TEST' , true );
if ( ! defined ( 'PHPUNIT_UTIL' )) {
2012-04-05 19:03:31 +02:00
/** Identifies utility scripts - the database does not need to be initialised */
2012-03-31 23:51:02 +02:00
define ( 'PHPUNIT_UTIL' , false );
2012-03-21 10:31:37 +01:00
}
if ( defined ( 'CLI_SCRIPT' )) {
2012-04-05 01:25:06 +02:00
phpunit_bootstrap_error ( 1 , 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts' );
2012-03-21 10:31:37 +01:00
}
define ( 'CLI_SCRIPT' , true );
2012-04-08 17:20:24 +02:00
$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' )) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_PHPUNITWRONG , $phpunitversion );
2012-04-08 17:20:24 +02:00
}
unset ( $phpunitversion );
2012-04-13 12:52:37 +02:00
if ( ! include_once ( 'PHPUnit/Extensions/Database/Autoload.php' )) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_PHPUNITEXTMISSING , 'phpunit/DbUnit' );
}
2012-03-21 10:31:37 +01:00
define ( 'NO_OUTPUT_BUFFERING' , true );
2012-04-05 19:03:31 +02:00
// only load CFG from config.php, stop ASAP in lib/setup.php
2012-03-21 10:31:37 +01:00
define ( 'ABORT_AFTER_CONFIG' , true );
require ( __DIR__ . '/../../config.php' );
2012-04-03 11:12:52 +02:00
if ( ! defined ( 'PHPUNIT_LONGTEST' )) {
/** Execute longer version of tests */
define ( 'PHPUNIT_LONGTEST' , false );
}
2012-03-21 10:31:37 +01:00
// remove error handling overrides done in config.php
2012-03-31 23:51:02 +02:00
error_reporting ( E_ALL | E_STRICT );
2012-03-21 10:31:37 +01:00
ini_set ( 'display_errors' , '1' );
ini_set ( 'log_errors' , '1' );
2012-03-31 23:51:02 +02:00
set_time_limit ( 0 ); // no time limit in CLI scripts, user may cancel execution
2012-03-21 10:31:37 +01:00
// 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 )) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!' );
2012-03-21 10:31:37 +01:00
}
2013-01-16 16:15:56 +01:00
2013-01-25 23:35:46 +01:00
// Create test dir if does not exists yet.
2012-03-21 10:31:37 +01:00
if ( ! file_exists ( $CFG -> phpunit_dataroot )) {
mkdir ( $CFG -> phpunit_dataroot , $CFG -> directorypermissions );
}
if ( ! is_dir ( $CFG -> phpunit_dataroot )) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , '$CFG->phpunit_dataroot directory can not be created, can not run tests!' );
2012-03-21 10:31:37 +01:00
}
2012-04-05 01:25:06 +02:00
2013-01-25 23:35:46 +01:00
// Ensure we access to phpunit_dataroot realpath always.
$CFG -> phpunit_dataroot = realpath ( $CFG -> phpunit_dataroot );
if ( isset ( $CFG -> dataroot ) and $CFG -> phpunit_dataroot === $CFG -> dataroot ) {
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!' );
}
2012-03-21 10:31:37 +01:00
if ( ! is_writable ( $CFG -> phpunit_dataroot )) {
2012-04-05 19:03:31 +02:00
// try to fix permissions if possible
2012-03-21 10:31:37 +01:00
if ( function_exists ( 'posix_getuid' )) {
$chmod = fileperms ( $CFG -> phpunit_dataroot );
2012-04-05 01:25:06 +02:00
if ( fileowner ( $CFG -> phpunit_dataroot ) == posix_getuid ()) {
2012-03-21 10:31:37 +01:00
$chmod = $chmod | 0700 ;
chmod ( $CFG -> phpunit_dataroot , $chmod );
}
}
if ( ! is_writable ( $CFG -> phpunit_dataroot )) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , '$CFG->phpunit_dataroot directory is not writable, can not run tests!' );
2012-03-21 10:31:37 +01:00
}
}
if ( ! file_exists ( " $CFG->phpunit_dataroot /phpunittestdir.txt " )) {
if ( $dh = opendir ( $CFG -> phpunit_dataroot )) {
while (( $file = readdir ( $dh )) !== false ) {
2012-04-05 01:25:06 +02:00
if ( $file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store' ) {
2012-03-21 10:31:37 +01:00
continue ;
}
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?' );
2012-03-21 10:31:37 +01:00
}
closedir ( $dh );
unset ( $dh );
unset ( $file );
}
// now we are 100% sure this dir is used only for phpunit tests
2013-01-11 17:34:24 +08:00
testing_initdataroot ( $CFG -> phpunit_dataroot , 'phpunit' );
2012-03-21 10:31:37 +01:00
}
// verify db prefix
if ( ! isset ( $CFG -> phpunit_prefix )) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , 'Missing $CFG->phpunit_prefix in config.php, can not run tests!' );
2012-03-31 23:51:02 +02:00
}
if ( $CFG -> phpunit_prefix === '' ) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , '$CFG->phpunit_prefix can not be empty, can not run tests!' );
2012-03-21 10:31:37 +01:00
}
if ( isset ( $CFG -> prefix ) and $CFG -> prefix === $CFG -> phpunit_prefix ) {
2012-04-13 12:45:18 +02:00
phpunit_bootstrap_error ( PHPUNIT_EXITCODE_CONFIGERROR , '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!' );
2012-03-21 10:31:37 +01:00
}
2012-04-10 13:50:04 +02:00
// override CFG settings if necessary and throw away extra CFG settings
2012-04-22 17:23:20 +02:00
$CFG -> wwwroot = 'http://www.example.com/moodle' ;
2012-04-05 19:03:31 +02:00
$CFG -> dataroot = $CFG -> phpunit_dataroot ;
$CFG -> prefix = $CFG -> phpunit_prefix ;
$CFG -> dbtype = isset ( $CFG -> phpunit_dbtype ) ? $CFG -> phpunit_dbtype : $CFG -> dbtype ;
$CFG -> dblibrary = isset ( $CFG -> phpunit_dblibrary ) ? $CFG -> phpunit_dblibrary : $CFG -> dblibrary ;
$CFG -> dbhost = isset ( $CFG -> phpunit_dbhost ) ? $CFG -> phpunit_dbhost : $CFG -> dbhost ;
$CFG -> dbname = isset ( $CFG -> phpunit_dbname ) ? $CFG -> phpunit_dbname : $CFG -> dbname ;
$CFG -> dbuser = isset ( $CFG -> phpunit_dbuser ) ? $CFG -> phpunit_dbuser : $CFG -> dbuser ;
$CFG -> dbpass = isset ( $CFG -> phpunit_dbpass ) ? $CFG -> phpunit_dbpass : $CFG -> dbpass ;
$CFG -> prefix = isset ( $CFG -> phpunit_prefix ) ? $CFG -> phpunit_prefix : $CFG -> prefix ;
$CFG -> dboptions = isset ( $CFG -> phpunit_dboptions ) ? $CFG -> phpunit_dboptions : $CFG -> dboptions ;
2012-03-21 10:31:37 +01:00
$allowed = array ( 'wwwroot' , 'dataroot' , 'dirroot' , 'admin' , 'directorypermissions' , 'filepermissions' ,
2012-04-26 20:31:19 +02:00
'dbtype' , 'dblibrary' , 'dbhost' , 'dbname' , 'dbuser' , 'dbpass' , 'prefix' , 'dboptions' ,
2012-04-30 08:35:16 +02:00
'proxyhost' , 'proxyport' , 'proxytype' , 'proxyuser' , 'proxypassword' , 'proxybypass' , // keep proxy settings from config.php
2014-06-04 10:07:03 +08:00
'altcacheconfigpath' , 'pathtogs' , 'pathtoclam' , 'pathtodu' , 'aspellpath' , 'pathtodot'
2012-04-26 20:31:19 +02:00
);
2012-03-21 10:31:37 +01:00
$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
2012-03-31 23:51:02 +02:00
$CFG -> debug = ( E_ALL | E_STRICT ); // can not use DEBUG_DEVELOPER yet
2013-08-10 22:46:49 +02:00
$CFG -> debugdeveloper = true ;
2012-03-21 10:31:37 +01:00
$CFG -> debugdisplay = 1 ;
error_reporting ( $CFG -> debug );
ini_set ( 'display_errors' , '1' );
2012-04-05 19:03:31 +02:00
ini_set ( 'log_errors' , '1' );
2012-03-21 10:31:37 +01:00
$CFG -> noemailever = true ; // better not mail anybody from tests, override temporarily if necessary
// 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 " );
2012-04-26 20:36:37 +02:00
raise_memory_limit ( MEMORY_HUGE );
2012-03-21 10:31:37 +01:00
2012-03-31 23:51:02 +02:00
if ( PHPUNIT_UTIL ) {
2012-04-05 19:03:31 +02:00
// we are not going to do testing, this is 'true' in utility scripts that only init database
2012-03-21 10:31:37 +01:00
return ;
}
2012-03-31 23:51:02 +02:00
// is database and dataroot ready for testing?
2012-04-05 01:25:06 +02:00
list ( $errorcode , $message ) = phpunit_util :: testing_ready_problem ();
2013-03-23 00:06:39 +01:00
// print some version info
phpunit_util :: bootstrap_moodle_info ();
2012-04-05 01:25:06 +02:00
if ( $errorcode ) {
phpunit_bootstrap_error ( $errorcode , $message );
2012-03-31 23:51:02 +02:00
}
2012-03-21 10:31:37 +01:00
2012-04-05 19:03:31 +02:00
// prepare for the first test run - store fresh globals, reset database and dataroot, etc.
2012-03-31 23:51:02 +02:00
phpunit_util :: bootstrap_init ();