MDL-37555 Apply realpath() 2 CFG->phpunit_dataroot
[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 95}
c6523d2f
EL
96// Ensure we access to phpunit_dataroot realpath always.
97$CFG->phpunit_dataroot = realpath($CFG->phpunit_dataroot);
98
5bd40408 99if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
0d8e51a6 100 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
5bd40408
PS
101}
102if (!file_exists($CFG->phpunit_dataroot)) {
103 mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
104}
105if (!is_dir($CFG->phpunit_dataroot)) {
0d8e51a6 106 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
5bd40408 107}
7b0ff213 108
5bd40408 109if (!is_writable($CFG->phpunit_dataroot)) {
6e2cff2d 110 // try to fix permissions if possible
5bd40408
PS
111 if (function_exists('posix_getuid')) {
112 $chmod = fileperms($CFG->phpunit_dataroot);
7b0ff213 113 if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
5bd40408
PS
114 $chmod = $chmod | 0700;
115 chmod($CFG->phpunit_dataroot, $chmod);
116 }
117 }
118 if (!is_writable($CFG->phpunit_dataroot)) {
0d8e51a6 119 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
5bd40408
PS
120 }
121}
122if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
123 if ($dh = opendir($CFG->phpunit_dataroot)) {
124 while (($file = readdir($dh)) !== false) {
7b0ff213 125 if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
5bd40408
PS
126 continue;
127 }
0d8e51a6 128 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
129 }
130 closedir($dh);
131 unset($dh);
132 unset($file);
133 }
134
135 // now we are 100% sure this dir is used only for phpunit tests
136 phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot);
137}
138
5bd40408
PS
139// verify db prefix
140if (!isset($CFG->phpunit_prefix)) {
0d8e51a6 141 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
a3d5830a
PS
142}
143if ($CFG->phpunit_prefix === '') {
0d8e51a6 144 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
5bd40408
PS
145}
146if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
0d8e51a6 147 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
5bd40408
PS
148}
149
920f4efe 150// override CFG settings if necessary and throw away extra CFG settings
fb8e3fab 151$CFG->wwwroot = 'http://www.example.com/moodle';
6e2cff2d
PS
152$CFG->dataroot = $CFG->phpunit_dataroot;
153$CFG->prefix = $CFG->phpunit_prefix;
154$CFG->dbtype = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
155$CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
156$CFG->dbhost = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
157$CFG->dbname = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
158$CFG->dbuser = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
159$CFG->dbpass = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
160$CFG->prefix = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
161$CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
5bd40408
PS
162
163$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
366c6adb 164 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
46cb5d9a 165 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
366c6adb 166 );
5bd40408
PS
167$productioncfg = (array)$CFG;
168$CFG = new stdClass();
169foreach ($productioncfg as $key=>$value) {
170 if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
171 // ignore
172 continue;
173 }
174 $CFG->{$key} = $value;
175}
176unset($key);
177unset($value);
178unset($allowed);
179unset($productioncfg);
180
181// force the same CFG settings in all sites
a3d5830a 182$CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
5bd40408
PS
183$CFG->debugdisplay = 1;
184error_reporting($CFG->debug);
185ini_set('display_errors', '1');
6e2cff2d 186ini_set('log_errors', '1');
5bd40408 187
a3d5830a
PS
188$CFG->passwordsaltmain = 'phpunit'; // makes login via normal UI impossible
189
5bd40408
PS
190$CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
191$CFG->cachetext = 0; // disable this very nasty setting
192
193// some ugly hacks
194$CFG->themerev = 1;
195$CFG->jsrev = 1;
196
197// load test case stub classes and other stuff
198require_once("$CFG->dirroot/lib/phpunit/lib.php");
199
200// finish moodle init
201define('ABORT_AFTER_CONFIG_CANCEL', true);
202require("$CFG->dirroot/lib/setup.php");
203
32962961 204raise_memory_limit(MEMORY_HUGE);
5bd40408 205
a3d5830a 206if (PHPUNIT_UTIL) {
6e2cff2d 207 // we are not going to do testing, this is 'true' in utility scripts that only init database
5bd40408
PS
208 return;
209}
210
a3d5830a 211// is database and dataroot ready for testing?
7b0ff213
PS
212list($errorcode, $message) = phpunit_util::testing_ready_problem();
213if ($errorcode) {
214 phpunit_bootstrap_error($errorcode, $message);
a3d5830a 215}
5bd40408 216
6e2cff2d 217// prepare for the first test run - store fresh globals, reset database and dataroot, etc.
a3d5830a 218phpunit_util::bootstrap_init();