Merge branch 'w18_MDL-32589_m23_proxytest' of git://github.com/skodak/moodle
[moodle.git] / lib / phpunit / bootstrap.php
CommitLineData
5bd40408
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
6e2cff2d
PS
18 * Prepares PHPUnit environment, the phpunit.xml configuration
19 * must specify this file as bootstrap.
5bd40408 20 *
0d8e51a6 21 * Exit codes: {@see phpunit_bootstrap_error()}
5bd40408 22 *
7aea08e1 23 * @package core
5bd40408
PS
24 * @category phpunit
25 * @copyright 2012 Petr Skoda {@link http://skodak.org}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29// we want to know about all problems
30error_reporting(E_ALL | E_STRICT);
31ini_set('display_errors', '1');
32ini_set('log_errors', '1');
33
7b0ff213
PS
34require_once(__DIR__.'/bootstraplib.php');
35
5bd40408 36if (isset($_SERVER['REMOTE_ADDR'])) {
7b0ff213 37 phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
5bd40408
PS
38}
39
a3d5830a 40if (defined('PHPUNIT_TEST')) {
7b0ff213 41 phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
a3d5830a
PS
42}
43/** PHPUnit testing framework active */
44define('PHPUNIT_TEST', true);
45
a3d5830a 46if (!defined('PHPUNIT_UTIL')) {
6e2cff2d 47 /** Identifies utility scripts - the database does not need to be initialised */
a3d5830a 48 define('PHPUNIT_UTIL', false);
5bd40408 49}
5bd40408
PS
50
51if (defined('CLI_SCRIPT')) {
7b0ff213 52 phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
5bd40408
PS
53}
54define('CLI_SCRIPT', true);
55
b5b10866
PS
56$phpunitversion = PHPUnit_Runner_Version::id();
57if ($phpunitversion === '@package_version@') {
58 // library checked out from git, let's hope dev knows that 3.6.0 is required
59} else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
0d8e51a6 60 phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
b5b10866
PS
61}
62unset($phpunitversion);
63
0684ea99 64if (!include_once('PHPUnit/Extensions/Database/Autoload.php')) {
0d8e51a6
PS
65 phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
66}
67
5bd40408
PS
68define('NO_OUTPUT_BUFFERING', true);
69
6e2cff2d 70// only load CFG from config.php, stop ASAP in lib/setup.php
5bd40408
PS
71define('ABORT_AFTER_CONFIG', true);
72require(__DIR__ . '/../../config.php');
73
7514f9c2
PS
74if (!defined('PHPUNIT_LONGTEST')) {
75 /** Execute longer version of tests */
76 define('PHPUNIT_LONGTEST', false);
77}
78
5bd40408 79// remove error handling overrides done in config.php
a3d5830a 80error_reporting(E_ALL | E_STRICT);
5bd40408
PS
81ini_set('display_errors', '1');
82ini_set('log_errors', '1');
a3d5830a 83set_time_limit(0); // no time limit in CLI scripts, user may cancel execution
5bd40408
PS
84
85// prepare dataroot
86umask(0);
87if (isset($CFG->phpunit_directorypermissions)) {
88 $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
89} else {
90 $CFG->directorypermissions = 02777;
91}
92$CFG->filepermissions = ($CFG->directorypermissions & 0666);
93if (!isset($CFG->phpunit_dataroot)) {
0d8e51a6 94 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
5bd40408
PS
95}
96if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
0d8e51a6 97 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
5bd40408
PS
98}
99if (!file_exists($CFG->phpunit_dataroot)) {
100 mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
101}
102if (!is_dir($CFG->phpunit_dataroot)) {
0d8e51a6 103 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
5bd40408 104}
7b0ff213 105
5bd40408 106if (!is_writable($CFG->phpunit_dataroot)) {
6e2cff2d 107 // try to fix permissions if possible
5bd40408
PS
108 if (function_exists('posix_getuid')) {
109 $chmod = fileperms($CFG->phpunit_dataroot);
7b0ff213 110 if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
5bd40408
PS
111 $chmod = $chmod | 0700;
112 chmod($CFG->phpunit_dataroot, $chmod);
113 }
114 }
115 if (!is_writable($CFG->phpunit_dataroot)) {
0d8e51a6 116 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
5bd40408
PS
117 }
118}
119if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
120 if ($dh = opendir($CFG->phpunit_dataroot)) {
121 while (($file = readdir($dh)) !== false) {
7b0ff213 122 if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
5bd40408
PS
123 continue;
124 }
0d8e51a6 125 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
5bd40408
PS
126 }
127 closedir($dh);
128 unset($dh);
129 unset($file);
130 }
131
132 // now we are 100% sure this dir is used only for phpunit tests
133 phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot);
134}
135
5bd40408
PS
136// verify db prefix
137if (!isset($CFG->phpunit_prefix)) {
0d8e51a6 138 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
a3d5830a
PS
139}
140if ($CFG->phpunit_prefix === '') {
0d8e51a6 141 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
5bd40408
PS
142}
143if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
0d8e51a6 144 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
5bd40408
PS
145}
146
920f4efe 147// override CFG settings if necessary and throw away extra CFG settings
fb8e3fab 148$CFG->wwwroot = 'http://www.example.com/moodle';
6e2cff2d
PS
149$CFG->dataroot = $CFG->phpunit_dataroot;
150$CFG->prefix = $CFG->phpunit_prefix;
151$CFG->dbtype = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
152$CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
153$CFG->dbhost = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
154$CFG->dbname = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
155$CFG->dbuser = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
156$CFG->dbpass = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
157$CFG->prefix = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
158$CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
5bd40408
PS
159
160$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
366c6adb 161 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
46cb5d9a 162 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
366c6adb 163 );
5bd40408
PS
164$productioncfg = (array)$CFG;
165$CFG = new stdClass();
166foreach ($productioncfg as $key=>$value) {
167 if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
168 // ignore
169 continue;
170 }
171 $CFG->{$key} = $value;
172}
173unset($key);
174unset($value);
175unset($allowed);
176unset($productioncfg);
177
178// force the same CFG settings in all sites
a3d5830a 179$CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
5bd40408
PS
180$CFG->debugdisplay = 1;
181error_reporting($CFG->debug);
182ini_set('display_errors', '1');
6e2cff2d 183ini_set('log_errors', '1');
5bd40408 184
a3d5830a
PS
185$CFG->passwordsaltmain = 'phpunit'; // makes login via normal UI impossible
186
5bd40408
PS
187$CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
188$CFG->cachetext = 0; // disable this very nasty setting
189
190// some ugly hacks
191$CFG->themerev = 1;
192$CFG->jsrev = 1;
193
194// load test case stub classes and other stuff
195require_once("$CFG->dirroot/lib/phpunit/lib.php");
196
197// finish moodle init
198define('ABORT_AFTER_CONFIG_CANCEL', true);
199require("$CFG->dirroot/lib/setup.php");
200
32962961 201raise_memory_limit(MEMORY_HUGE);
5bd40408 202
a3d5830a 203if (PHPUNIT_UTIL) {
6e2cff2d 204 // we are not going to do testing, this is 'true' in utility scripts that only init database
5bd40408
PS
205 return;
206}
207
a3d5830a 208// is database and dataroot ready for testing?
7b0ff213
PS
209list($errorcode, $message) = phpunit_util::testing_ready_problem();
210if ($errorcode) {
211 phpunit_bootstrap_error($errorcode, $message);
a3d5830a 212}
5bd40408 213
6e2cff2d 214// prepare for the first test run - store fresh globals, reset database and dataroot, etc.
a3d5830a 215phpunit_util::bootstrap_init();