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