MDL-37555 Apply realpath() 2 CFG->phpunit_dataroot
[moodle.git] / lib / phpunit / bootstrap.php
blobf94c2950d6f2f07802572505b8c1674b11020715
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.
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/>.
17 /**
18 * Prepares PHPUnit environment, the phpunit.xml configuration
19 * must specify this file as bootstrap.
21 * Exit codes: {@see phpunit_bootstrap_error()}
23 * @package core
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
29 // we want to know about all problems
30 error_reporting(E_ALL | E_STRICT);
31 ini_set('display_errors', '1');
32 ini_set('log_errors', '1');
34 require_once(__DIR__.'/bootstraplib.php');
36 if (isset($_SERVER['REMOTE_ADDR'])) {
37 phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
40 if (defined('PHPUNIT_TEST')) {
41 phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
43 /** PHPUnit testing framework active */
44 define('PHPUNIT_TEST', true);
46 if (!defined('PHPUNIT_UTIL')) {
47 /** Identifies utility scripts - the database does not need to be initialised */
48 define('PHPUNIT_UTIL', false);
51 if (defined('CLI_SCRIPT')) {
52 phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
54 define('CLI_SCRIPT', true);
56 $phpunitversion = PHPUnit_Runner_Version::id();
57 if ($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')) {
60 phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
62 unset($phpunitversion);
64 if (!include_once('PHPUnit/Extensions/Database/Autoload.php')) {
65 phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
68 define('NO_OUTPUT_BUFFERING', true);
70 // only load CFG from config.php, stop ASAP in lib/setup.php
71 define('ABORT_AFTER_CONFIG', true);
72 require(__DIR__ . '/../../config.php');
74 if (!defined('PHPUNIT_LONGTEST')) {
75 /** Execute longer version of tests */
76 define('PHPUNIT_LONGTEST', false);
79 // remove error handling overrides done in config.php
80 error_reporting(E_ALL | E_STRICT);
81 ini_set('display_errors', '1');
82 ini_set('log_errors', '1');
83 set_time_limit(0); // no time limit in CLI scripts, user may cancel execution
85 // prepare dataroot
86 umask(0);
87 if (isset($CFG->phpunit_directorypermissions)) {
88 $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
89 } else {
90 $CFG->directorypermissions = 02777;
92 $CFG->filepermissions = ($CFG->directorypermissions & 0666);
93 if (!isset($CFG->phpunit_dataroot)) {
94 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
96 // Ensure we access to phpunit_dataroot realpath always.
97 $CFG->phpunit_dataroot = realpath($CFG->phpunit_dataroot);
99 if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
100 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
102 if (!file_exists($CFG->phpunit_dataroot)) {
103 mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
105 if (!is_dir($CFG->phpunit_dataroot)) {
106 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
109 if (!is_writable($CFG->phpunit_dataroot)) {
110 // try to fix permissions if possible
111 if (function_exists('posix_getuid')) {
112 $chmod = fileperms($CFG->phpunit_dataroot);
113 if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
114 $chmod = $chmod | 0700;
115 chmod($CFG->phpunit_dataroot, $chmod);
118 if (!is_writable($CFG->phpunit_dataroot)) {
119 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
122 if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
123 if ($dh = opendir($CFG->phpunit_dataroot)) {
124 while (($file = readdir($dh)) !== false) {
125 if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
126 continue;
128 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
130 closedir($dh);
131 unset($dh);
132 unset($file);
135 // now we are 100% sure this dir is used only for phpunit tests
136 phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot);
139 // verify db prefix
140 if (!isset($CFG->phpunit_prefix)) {
141 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
143 if ($CFG->phpunit_prefix === '') {
144 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
146 if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
147 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
150 // override CFG settings if necessary and throw away extra CFG settings
151 $CFG->wwwroot = 'http://www.example.com/moodle';
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;
163 $allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
164 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
165 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
167 $productioncfg = (array)$CFG;
168 $CFG = new stdClass();
169 foreach ($productioncfg as $key=>$value) {
170 if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
171 // ignore
172 continue;
174 $CFG->{$key} = $value;
176 unset($key);
177 unset($value);
178 unset($allowed);
179 unset($productioncfg);
181 // force the same CFG settings in all sites
182 $CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
183 $CFG->debugdisplay = 1;
184 error_reporting($CFG->debug);
185 ini_set('display_errors', '1');
186 ini_set('log_errors', '1');
188 $CFG->passwordsaltmain = 'phpunit'; // makes login via normal UI impossible
190 $CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
191 $CFG->cachetext = 0; // disable this very nasty setting
193 // some ugly hacks
194 $CFG->themerev = 1;
195 $CFG->jsrev = 1;
197 // load test case stub classes and other stuff
198 require_once("$CFG->dirroot/lib/phpunit/lib.php");
200 // finish moodle init
201 define('ABORT_AFTER_CONFIG_CANCEL', true);
202 require("$CFG->dirroot/lib/setup.php");
204 raise_memory_limit(MEMORY_HUGE);
206 if (PHPUNIT_UTIL) {
207 // we are not going to do testing, this is 'true' in utility scripts that only init database
208 return;
211 // is database and dataroot ready for testing?
212 list($errorcode, $message) = phpunit_util::testing_ready_problem();
213 if ($errorcode) {
214 phpunit_bootstrap_error($errorcode, $message);
217 // prepare for the first test run - store fresh globals, reset database and dataroot, etc.
218 phpunit_util::bootstrap_init();