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