MDL-31857 phpunit: PHPDoc and typo fixes during integration
[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
23 * 130 - coding error
24 * 131 - configuration problem
25 * 132 - drop data, then install new test database
26 *
7aea08e1 27 * @package core
5bd40408
PS
28 * @category phpunit
29 * @copyright 2012 Petr Skoda {@link http://skodak.org}
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32
33// we want to know about all problems
34error_reporting(E_ALL | E_STRICT);
35ini_set('display_errors', '1');
36ini_set('log_errors', '1');
37
38if (isset($_SERVER['REMOTE_ADDR'])) {
39 phpunit_bootstrap_error('Unit tests can be executed only from commandline!', 1);
40}
41
42if (defined('PHPUNITTEST')) {
43 phpunit_bootstrap_error("PHPUNITTEST constant must not be manually defined anywhere!", 130);
44}
45define('PHPUNITTEST', true);
46
47if (defined('CLI_SCRIPT')) {
48 phpunit_bootstrap_error('CLI_SCRIPT must not be manually defined in any PHPUnit test scripts', 130);
49}
50define('CLI_SCRIPT', true);
51
52define('NO_OUTPUT_BUFFERING', true);
53
54// only load CFG from config.php
55define('ABORT_AFTER_CONFIG', true);
56require(__DIR__ . '/../../config.php');
57
58// remove error handling overrides done in config.php
59error_reporting(E_ALL);
60ini_set('display_errors', '1');
61ini_set('log_errors', '1');
62
63// prepare dataroot
64umask(0);
65if (isset($CFG->phpunit_directorypermissions)) {
66 $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
67} else {
68 $CFG->directorypermissions = 02777;
69}
70$CFG->filepermissions = ($CFG->directorypermissions & 0666);
71if (!isset($CFG->phpunit_dataroot)) {
72 phpunit_bootstrap_error('Missing $CFG->phpunit_dataroot in config.php, can not run tests!', 131);
73}
74if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
75 phpunit_bootstrap_error('$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!', 131);
76}
77if (!file_exists($CFG->phpunit_dataroot)) {
78 mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
79}
80if (!is_dir($CFG->phpunit_dataroot)) {
81 phpunit_bootstrap_error('$CFG->phpunit_dataroot directory can not be created, can not run tests!', 131);
82}
83if (!is_writable($CFG->phpunit_dataroot)) {
84 // try to fix premissions if possible
85 if (function_exists('posix_getuid')) {
86 $chmod = fileperms($CFG->phpunit_dataroot);
87 if (fileowner($dir) == posix_getuid()) {
88 $chmod = $chmod | 0700;
89 chmod($CFG->phpunit_dataroot, $chmod);
90 }
91 }
92 if (!is_writable($CFG->phpunit_dataroot)) {
93 phpunit_bootstrap_error('$CFG->phpunit_dataroot directory is not writable, can not run tests!', 131);
94 }
95}
96if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
97 if ($dh = opendir($CFG->phpunit_dataroot)) {
98 while (($file = readdir($dh)) !== false) {
99 if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_store') {
100 continue;
101 }
102 phpunit_bootstrap_error('$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?', 131);
103 }
104 closedir($dh);
105 unset($dh);
106 unset($file);
107 }
108
109 // now we are 100% sure this dir is used only for phpunit tests
110 phpunit_bootstrap_initdataroot($CFG->phpunit_dataroot);
111}
112
113
114// verify db prefix
115if (!isset($CFG->phpunit_prefix)) {
116 phpunit_bootstrap_error('Missing $CFG->phpunit_dataroot in config.php, can not run tests!', 131);
117}
118if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
119 phpunit_bootstrap_error('$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!', 131);
120}
121
122// throw away standard CFG settings
123
124$CFG->dataroot = $CFG->phpunit_dataroot;
125$CFG->prefix = $CFG->phpunit_prefix;
126
127$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
128 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions');
129$productioncfg = (array)$CFG;
130$CFG = new stdClass();
131foreach ($productioncfg as $key=>$value) {
132 if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
133 // ignore
134 continue;
135 }
136 $CFG->{$key} = $value;
137}
138unset($key);
139unset($value);
140unset($allowed);
141unset($productioncfg);
142
143// force the same CFG settings in all sites
144$CFG->debug = (E_ALL | E_STRICT | 38911); // can not use DEBUG_DEVELOPER here
145$CFG->debugdisplay = 1;
146error_reporting($CFG->debug);
147ini_set('display_errors', '1');
148ini_set('log_errors', '0');
149
150$CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
151$CFG->cachetext = 0; // disable this very nasty setting
152
153// some ugly hacks
154$CFG->themerev = 1;
155$CFG->jsrev = 1;
156
157// load test case stub classes and other stuff
158require_once("$CFG->dirroot/lib/phpunit/lib.php");
159
160// finish moodle init
161define('ABORT_AFTER_CONFIG_CANCEL', true);
162require("$CFG->dirroot/lib/setup.php");
163
164raise_memory_limit(MEMORY_EXTRA);
165
166if (defined('PHPUNIT_CLI_UTIL')) {
167 // all other tests are done in the CLI scripts...
168 return;
169}
170
171if (!phpunit_util::is_testing_ready()) {
172 phpunit_bootstrap_error('Database is not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"', 132);
173}
174
175// refresh data in all tables, clear caches, etc.
176phpunit_util::reset_all_data();
177
178// store fresh globals
179phpunit_util::init_globals();
180
181
182//=========================================================
183
184/**
185 * Print error and stop execution
7aea08e1
SH
186 * @param string $text An error message to display
187 * @param int $errorcode The error code (see docblock for detailed list)
188 * @return void stops code execution with error code
5bd40408
PS
189 */
190function phpunit_bootstrap_error($text, $errorcode = 1) {
191 fwrite(STDERR, $text."\n");
192 exit($errorcode);
193}
194
195/**
196 * Mark empty dataroot to be used for testing.
7aea08e1 197 * @param string $dataroot The dataroot directory
5bd40408
PS
198 * @return void
199 */
200function phpunit_bootstrap_initdataroot($dataroot) {
201 global $CFG;
202
203 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
204 chmod("$dataroot/phpunittestdir.txt", $CFG->filepermissions);
205 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
206 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
207 }
208}