MDL-32323 add basic mod_data data generator
[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
62define('NO_OUTPUT_BUFFERING', true);
63
6e2cff2d 64// only load CFG from config.php, stop ASAP in lib/setup.php
5bd40408
PS
65define('ABORT_AFTER_CONFIG', true);
66require(__DIR__ . '/../../config.php');
67
7514f9c2
PS
68if (!defined('PHPUNIT_LONGTEST')) {
69 /** Execute longer version of tests */
70 define('PHPUNIT_LONGTEST', false);
71}
72
5bd40408 73// remove error handling overrides done in config.php
a3d5830a 74error_reporting(E_ALL | E_STRICT);
5bd40408
PS
75ini_set('display_errors', '1');
76ini_set('log_errors', '1');
a3d5830a 77set_time_limit(0); // no time limit in CLI scripts, user may cancel execution
5bd40408
PS
78
79// prepare dataroot
80umask(0);
81if (isset($CFG->phpunit_directorypermissions)) {
82 $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
83} else {
84 $CFG->directorypermissions = 02777;
85}
86$CFG->filepermissions = ($CFG->directorypermissions & 0666);
87if (!isset($CFG->phpunit_dataroot)) {
7b0ff213 88 phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
5bd40408
PS
89}
90if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
7b0ff213 91 phpunit_bootstrap_error(131, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
5bd40408
PS
92}
93if (!file_exists($CFG->phpunit_dataroot)) {
94 mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
95}
96if (!is_dir($CFG->phpunit_dataroot)) {
7b0ff213 97 phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
5bd40408 98}
7b0ff213 99
5bd40408 100if (!is_writable($CFG->phpunit_dataroot)) {
6e2cff2d 101 // try to fix permissions if possible
5bd40408
PS
102 if (function_exists('posix_getuid')) {
103 $chmod = fileperms($CFG->phpunit_dataroot);
7b0ff213 104 if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
5bd40408
PS
105 $chmod = $chmod | 0700;
106 chmod($CFG->phpunit_dataroot, $chmod);
107 }
108 }
109 if (!is_writable($CFG->phpunit_dataroot)) {
7b0ff213 110 phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
5bd40408
PS
111 }
112}
113if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
114 if ($dh = opendir($CFG->phpunit_dataroot)) {
115 while (($file = readdir($dh)) !== false) {
7b0ff213 116 if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
5bd40408
PS
117 continue;
118 }
7b0ff213 119 phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
5bd40408
PS
120 }
121 closedir($dh);
122 unset($dh);
123 unset($file);
124 }
125
126 // now we are 100% sure this dir is used only for phpunit tests
127 phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot);
128}
129
5bd40408
PS
130// verify db prefix
131if (!isset($CFG->phpunit_prefix)) {
7b0ff213 132 phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
a3d5830a
PS
133}
134if ($CFG->phpunit_prefix === '') {
7b0ff213 135 phpunit_bootstrap_error(131, '$CFG->phpunit_prefix can not be empty, can not run tests!');
5bd40408
PS
136}
137if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
7b0ff213 138 phpunit_bootstrap_error(131, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
5bd40408
PS
139}
140
6e2cff2d
PS
141// override CFG settings if necessary nad throw away extra CFG settings
142$CFG->dataroot = $CFG->phpunit_dataroot;
143$CFG->prefix = $CFG->phpunit_prefix;
144$CFG->dbtype = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
145$CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
146$CFG->dbhost = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
147$CFG->dbname = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
148$CFG->dbuser = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
149$CFG->dbpass = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
150$CFG->prefix = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
151$CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
5bd40408
PS
152
153$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
154 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions');
155$productioncfg = (array)$CFG;
156$CFG = new stdClass();
157foreach ($productioncfg as $key=>$value) {
158 if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
159 // ignore
160 continue;
161 }
162 $CFG->{$key} = $value;
163}
164unset($key);
165unset($value);
166unset($allowed);
167unset($productioncfg);
168
169// force the same CFG settings in all sites
a3d5830a 170$CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
5bd40408
PS
171$CFG->debugdisplay = 1;
172error_reporting($CFG->debug);
173ini_set('display_errors', '1');
6e2cff2d 174ini_set('log_errors', '1');
5bd40408 175
a3d5830a
PS
176$CFG->passwordsaltmain = 'phpunit'; // makes login via normal UI impossible
177
5bd40408
PS
178$CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
179$CFG->cachetext = 0; // disable this very nasty setting
180
181// some ugly hacks
182$CFG->themerev = 1;
183$CFG->jsrev = 1;
184
185// load test case stub classes and other stuff
186require_once("$CFG->dirroot/lib/phpunit/lib.php");
187
188// finish moodle init
189define('ABORT_AFTER_CONFIG_CANCEL', true);
190require("$CFG->dirroot/lib/setup.php");
191
192raise_memory_limit(MEMORY_EXTRA);
193
a3d5830a 194if (PHPUNIT_UTIL) {
6e2cff2d 195 // we are not going to do testing, this is 'true' in utility scripts that only init database
5bd40408
PS
196 return;
197}
198
a3d5830a 199// is database and dataroot ready for testing?
7b0ff213
PS
200list($errorcode, $message) = phpunit_util::testing_ready_problem();
201if ($errorcode) {
202 phpunit_bootstrap_error($errorcode, $message);
a3d5830a 203}
5bd40408 204
6e2cff2d 205// prepare for the first test run - store fresh globals, reset database and dataroot, etc.
a3d5830a 206phpunit_util::bootstrap_init();