Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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, the phpunit.xml configuration
19
 * must specify this file as bootstrap.
20
 *
21
 * Exit codes: {@see phpunit_bootstrap_error()}
22
 *
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
27
 */
28
 
29
// phpcs:disable moodle.Files.MoodleInternal.MoodleInternalGlobalState
30
 
31
if (isset($_SERVER['REMOTE_ADDR'])) {
32
    die; // No access from web!
33
}
34
 
35
// We want to know about all problems.
36
error_reporting(E_ALL | E_STRICT);
37
ini_set('display_errors', '1');
38
ini_set('log_errors', '1');
39
 
40
// Make sure OPcache does not strip comments, we need them in phpunit!
41
if (ini_get('opcache.enable') && strtolower(ini_get('opcache.enable')) !== 'off') {
42
    if (!ini_get('opcache.save_comments') || strtolower(ini_get('opcache.save_comments')) === 'off') {
43
        ini_set('opcache.enable', 0);
44
    }
45
}
46
 
47
if (!defined('IGNORE_COMPONENT_CACHE')) {
48
    define('IGNORE_COMPONENT_CACHE', true);
49
}
50
 
51
require_once(__DIR__ . '/bootstraplib.php');
52
require_once(__DIR__ . '/../testing/lib.php');
53
 
54
if (isset($_SERVER['REMOTE_ADDR'])) {
55
    phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
56
}
57
 
58
if (defined('PHPUNIT_TEST')) {
59
    phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
60
}
61
 
62
/** PHPUnit testing framework active */
63
define('PHPUNIT_TEST', true);
64
 
65
if (!defined('PHPUNIT_UTIL')) {
66
    /** Identifies utility scripts - the database does not need to be initialised */
67
    define('PHPUNIT_UTIL', false);
68
}
69
 
70
if (defined('CLI_SCRIPT')) {
71
    phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
72
}
73
define('CLI_SCRIPT', true);
74
 
75
$phpunitversion = PHPUnit\Runner\Version::id();
76
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
77
if ($phpunitversion === '@package_version@') {
78
    // Library checked out from git, let's hope dev knows that 3.6.0 is required.
79
} else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
80
    phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
81
}
82
unset($phpunitversion);
83
 
84
// Only load CFG from config.php, stop ASAP in lib/setup.php.
85
define('ABORT_AFTER_CONFIG', true);
86
require(__DIR__ . '/../../config.php');
87
 
88
if (!defined('PHPUNIT_LONGTEST')) {
89
    /** Execute longer version of tests */
90
    define('PHPUNIT_LONGTEST', false);
91
}
92
 
93
// Remove error handling overrides done in config.php.
94
error_reporting(E_ALL | E_STRICT);
95
ini_set('display_errors', '1');
96
ini_set('log_errors', '1');
97
set_time_limit(0); // No time limit in CLI scripts, user may cancel execution.
98
 
99
// Prepare dataroot.
100
umask(0);
101
if (isset($CFG->phpunit_directorypermissions)) {
102
    $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
103
} else {
104
    $CFG->directorypermissions = 02777;
105
}
106
$CFG->filepermissions = ($CFG->directorypermissions & 0666);
107
if (!isset($CFG->phpunit_dataroot)) {
108
    phpunit_bootstrap_error(
109
        PHPUNIT_EXITCODE_CONFIGERROR,
110
        'Missing $CFG->phpunit_dataroot in config.php, can not run tests!',
111
    );
112
}
113
 
114
// Create test dir if does not exists yet.
115
if (!file_exists($CFG->phpunit_dataroot)) {
116
    mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
117
}
118
if (!is_dir($CFG->phpunit_dataroot)) {
119
    phpunit_bootstrap_error(
120
        PHPUNIT_EXITCODE_CONFIGERROR,
121
        '$CFG->phpunit_dataroot directory can not be created, can not run tests!',
122
    );
123
}
124
 
125
// Ensure we access to phpunit_dataroot realpath always.
126
$CFG->phpunit_dataroot = realpath($CFG->phpunit_dataroot);
127
 
128
if (isset($CFG->dataroot) && $CFG->phpunit_dataroot === $CFG->dataroot) {
129
    phpunit_bootstrap_error(
130
        PHPUNIT_EXITCODE_CONFIGERROR,
131
        '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!',
132
    );
133
}
134
 
135
if (!is_writable($CFG->phpunit_dataroot)) {
136
    // Try to fix permissions if possible.
137
    if (function_exists('posix_getuid')) {
138
        $chmod = fileperms($CFG->phpunit_dataroot);
139
        if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
140
            $chmod = $chmod | 0700;
141
            chmod($CFG->phpunit_dataroot, $chmod);
142
        }
143
    }
144
    if (!is_writable($CFG->phpunit_dataroot)) {
145
        phpunit_bootstrap_error(
146
            PHPUNIT_EXITCODE_CONFIGERROR,
147
            '$CFG->phpunit_dataroot directory is not writable, can not run tests!',
148
        );
149
    }
150
}
151
if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
152
    if ($dh = opendir($CFG->phpunit_dataroot)) {
153
        while (($file = readdir($dh)) !== false) {
154
            if ($file === 'phpunit' || $file === '.' || $file === '..' || $file === '.DS_Store') {
155
                continue;
156
            }
157
            phpunit_bootstrap_error(
158
                PHPUNIT_EXITCODE_CONFIGERROR,
159
                '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?',
160
            );
161
        }
162
        closedir($dh);
163
        unset($dh);
164
        unset($file);
165
    }
166
 
167
    // Now we are 100% sure this dir is used only for phpunit tests.
168
    testing_initdataroot($CFG->phpunit_dataroot, 'phpunit');
169
}
170
 
171
// Verify db prefix.
172
if (!isset($CFG->phpunit_prefix)) {
173
    phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
174
}
175
if ($CFG->phpunit_prefix === '') {
176
    phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
177
}
178
if (isset($CFG->prefix) && $CFG->prefix === $CFG->phpunit_prefix) {
179
    phpunit_bootstrap_error(
180
        PHPUNIT_EXITCODE_CONFIGERROR,
181
        '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!',
182
    );
183
}
184
 
185
// Override CFG settings if necessary and throw away extra CFG settings.
186
$CFG->wwwroot   = 'https://www.example.com/moodle';
187
$CFG->dataroot  = $CFG->phpunit_dataroot;
188
$CFG->prefix    = $CFG->phpunit_prefix;
189
$CFG->dbtype    = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
190
$CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
191
$CFG->dbhost    = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
192
$CFG->dbname    = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
193
$CFG->dbuser    = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
194
$CFG->dbpass    = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
195
$CFG->prefix    = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
196
$CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
197
 
198
$allowed = ['wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
199
    'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
200
    // Keep proxy settings from config.php.
201
    'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass',
202
    'altcacheconfigpath', 'pathtogs', 'pathtophp', 'pathtodu', 'aspellpath', 'pathtodot',
203
    'pathtounoconv', 'alternative_file_system_class', 'pathtopython',
204
];
205
$productioncfg = (array) $CFG;
206
$CFG = new stdClass();
207
foreach ($productioncfg as $key => $value) {
208
    if (!in_array($key, $allowed) && strpos($key, 'phpunit_') !== 0 && strpos($key, 'behat_') !== 0) {
209
        // Ignore.
210
        continue;
211
    }
212
    $CFG->{$key} = $value;
213
}
214
unset($key);
215
unset($value);
216
unset($allowed);
217
unset($productioncfg);
218
 
219
// Force the same CFG settings in all sites.
220
$CFG->debug = (E_ALL | E_STRICT); // Can not use DEBUG_DEVELOPER yet.
221
$CFG->debugdeveloper = true;
222
$CFG->debugdisplay = 1;
223
error_reporting($CFG->debug);
224
ini_set('display_errors', '1');
225
ini_set('log_errors', '1');
226
 
227
// Some ugly hacks.
228
$CFG->themerev = 1;
229
$CFG->jsrev = 1;
230
 
231
(function () {
232
    // Determine if this test is being run with isolation.
233
    // This is tricky because neither PHPUnit, nor PHP provide an official way to work this out.
234
    // PHPUnit does set a value, but not until later on and we need this earlier.
235
    // PHPUnit runs isolated tests by creating a class on the fly and running it through proc_open as standard input.
236
    // There is no other legitimate reason to run PHPUnit this way that I'm aware of.
237
    // When run in this way, PHP sets the value of $_SERVER['PHP_SELF'] to "Standard input code".
238
    // It has done this since 2016, and it is unlikely to change.
239
    define(
240
        'PHPUNIT_ISOLATED_TEST',
241
        $_SERVER['PHP_SELF'] === 'Standard input code',
242
    );
243
})();
244
 
245
// Load test case stub classes and other stuff.
246
require_once("$CFG->dirroot/lib/phpunit/lib.php");
247
 
248
// Finish moodle init.
249
define('ABORT_AFTER_CONFIG_CANCEL', true);
250
if (isset($CFG->phpunit_profilingenabled) && $CFG->phpunit_profilingenabled) {
251
    $CFG->profilingenabled = true;
252
    $CFG->profilingincluded = '*';
253
}
254
require("$CFG->dirroot/lib/setup.php");
255
 
256
raise_memory_limit(MEMORY_HUGE);
257
 
258
if (PHPUNIT_UTIL) {
259
    // We are not going to do testing, this is 'true' in utility scripts that only init database.
260
    return;
261
}
262
 
263
// Make sure the hook manager gets initialised before anybody tries to override callbacks,
264
// this is not using caches intentionally to help with development.
265
// Note: We cannot use DI at this point in the bootstrap either.
266
\core\hook\manager::get_instance();
267
 
268
// Is database and dataroot ready for testing?
269
[$errorcode, $message] = phpunit_util::testing_ready_problem();
270
// Print some version info.
271
phpunit_util::bootstrap_moodle_info();
272
if ($errorcode) {
273
    phpunit_bootstrap_error($errorcode, $message);
274
}
275
 
276
// Prepare for the first test run - store fresh globals, reset database and dataroot, etc.
277
phpunit_util::bootstrap_init();