MDL-32400 ignore outdated config settings in future upgrades
[moodle.git] / lib / phpunit / bootstraplib.php
CommitLineData
7b0ff213
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 * PHPUnit bootstrap function
19 *
20 * Note: these functions must be self contained and must not rely on any library or include
21 *
22 * @package core
23 * @category phpunit
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28/**
29 * Print error and stop execution
30 * @param int $errorcode The exit error code
31 * @param string $text An error message to display
32 * @return void stops code execution with error code
33 */
34function phpunit_bootstrap_error($errorcode, $text = '') {
35 switch ($errorcode) {
219d1a4e
PS
36 case 0:
37 // this is not an error, just print information and exit
38 break;
7b0ff213
PS
39 case 1:
40 $text = 'Error: '.$text;
41 break;
b5b10866
PS
42 case 129:
43 $text = 'Moodle requires PHPUnit 3.6.x, '.$text.' is not compatible';
44 break;
7b0ff213 45 case 130:
4e5ff72e 46 $text = 'Moodle can not find PHPUnit PEAR library or necessary PHPUnit extension';
b5b10866 47 break;
7b0ff213 48 case 131:
b5b10866 49 $text = 'Moodle configuration problem: '.$text;
7b0ff213
PS
50 break;
51 case 132:
b6b8a193 52 $text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/util.php --install";
7b0ff213
PS
53 break;
54 case 133:
b6b8a193 55 $text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/util.php --drop\n php admin/tool/phpunit/cli/util.php --install";
7b0ff213
PS
56 break;
57 case 134:
b5b10866 58 $text = 'Moodle can not create PHPUnit configuration file, please verify dirroot permissions';
7b0ff213
PS
59 break;
60 default:
61 $text = empty($text) ? '' : ': '.$text;
62 $text = 'Unknown error '.$errorcode.$text;
63 break;
64 }
2fde5392 65 if (defined('PHPUNIT_UTIL') and PHPUNIT_UTIL) {
920f4efe 66 // do not write to error stream because we need the error message in PHP exec result from web ui
7b0ff213
PS
67 echo($text."\n");
68 } else {
69 fwrite(STDERR, $text."\n");
70 }
71 exit($errorcode);
72}
73
74/**
75 * Mark empty dataroot to be used for testing.
76 * @param string $dataroot The dataroot directory
77 * @return void
78 */
79function phpunit_bootstrap_initdataroot($dataroot) {
80 global $CFG;
81 umask(0);
82 if (!file_exists("$dataroot/phpunittestdir.txt")) {
83 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
84 }
6e2cff2d 85 phpunit_boostrap_fix_file_permissions("$dataroot/phpunittestdir.txt");
7b0ff213
PS
86 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
87 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
88 }
89}
6e2cff2d
PS
90
91/**
92 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
93 * @param string $file
94 * @return bool success
95 */
96function phpunit_boostrap_fix_file_permissions($file) {
97 global $CFG;
98
99 $permissions = fileperms($file);
100 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
101 $permissions = $permissions | $CFG->filepermissions;
102 return chmod($file, $permissions);
103 }
104
105 return true;
920f4efe 106}