Moodle 2.3.1 release
[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
0d8e51a6
PS
28define('PHPUNIT_EXITCODE_PHPUNITMISSING', 129);
29define('PHPUNIT_EXITCODE_PHPUNITWRONG', 130);
30define('PHPUNIT_EXITCODE_PHPUNITEXTMISSING', 131);
31define('PHPUNIT_EXITCODE_CONFIGERROR', 135);
32define('PHPUNIT_EXITCODE_CONFIGWARNING', 136);
33define('PHPUNIT_EXITCODE_INSTALL', 140);
34define('PHPUNIT_EXITCODE_REINSTALL', 141);
35
7b0ff213
PS
36/**
37 * Print error and stop execution
38 * @param int $errorcode The exit error code
39 * @param string $text An error message to display
40 * @return void stops code execution with error code
41 */
42function phpunit_bootstrap_error($errorcode, $text = '') {
43 switch ($errorcode) {
219d1a4e
PS
44 case 0:
45 // this is not an error, just print information and exit
46 break;
7b0ff213
PS
47 case 1:
48 $text = 'Error: '.$text;
49 break;
0d8e51a6
PS
50 case PHPUNIT_EXITCODE_PHPUNITMISSING:
51 $text = "Moodle can not find PHPUnit PEAR library";
52 break;
53 case PHPUNIT_EXITCODE_PHPUNITWRONG:
b5b10866
PS
54 $text = 'Moodle requires PHPUnit 3.6.x, '.$text.' is not compatible';
55 break;
0d8e51a6
PS
56 case PHPUNIT_EXITCODE_PHPUNITEXTMISSING:
57 $text = 'Moodle can not find required PHPUnit extension '.$text;
b5b10866 58 break;
0d8e51a6
PS
59 case PHPUNIT_EXITCODE_CONFIGERROR:
60 $text = "Moodle PHPUnit environment configuration error:\n".$text;
7b0ff213 61 break;
0d8e51a6
PS
62 case PHPUNIT_EXITCODE_CONFIGWARNING:
63 $text = "Moodle PHPUnit environment configuration warning:\n".$text;
7b0ff213 64 break;
0d8e51a6 65 case PHPUNIT_EXITCODE_INSTALL:
5f9ba165
PS
66 $path = phpunit_bootstrap_cli_argument_path('/admin/tool/phpunit/cli/init.php');
67 $text = "Moodle PHPUnit environment is not initialised, please use:\n php $path";
7b0ff213 68 break;
0d8e51a6 69 case PHPUNIT_EXITCODE_REINSTALL:
5f9ba165
PS
70 $path = phpunit_bootstrap_cli_argument_path('/admin/tool/phpunit/cli/init.php');
71 $text = "Moodle PHPUnit environment was initialised for different version, please use:\n php $path";
7b0ff213
PS
72 break;
73 default:
74 $text = empty($text) ? '' : ': '.$text;
75 $text = 'Unknown error '.$errorcode.$text;
76 break;
77 }
0d8e51a6
PS
78
79 // do not write to error stream because we need the error message in PHP exec result from web ui
80 echo($text."\n");
7b0ff213
PS
81 exit($errorcode);
82}
83
5f9ba165
PS
84/**
85 * Returns relative path against current working directory,
86 * to be used for shell execution hints.
87 * @param string $moodlepath starting with "/", ex: "/admin/tool/cli/init.php"
88 * @return string path relative to current directory or absolute path
89 */
90function phpunit_bootstrap_cli_argument_path($moodlepath) {
91 global $CFG;
92
93 if (isset($CFG->admin) and $CFG->admin !== 'admin') {
94 $moodlepath = preg_replace('|^/admin/|', "/$CFG->admin/", $moodlepath);
95 }
96
97 $cwd = getcwd();
98 if (substr($cwd, -1) !== DIRECTORY_SEPARATOR) {
99 $cwd .= DIRECTORY_SEPARATOR;
100 }
101 $path = realpath($CFG->dirroot.$moodlepath);
102
103 if (strpos($path, $cwd) === 0) {
104 return substr($path, strlen($cwd));
105 }
106
107 return $path;
108}
109
7b0ff213
PS
110/**
111 * Mark empty dataroot to be used for testing.
112 * @param string $dataroot The dataroot directory
113 * @return void
114 */
115function phpunit_bootstrap_initdataroot($dataroot) {
116 global $CFG;
117 umask(0);
118 if (!file_exists("$dataroot/phpunittestdir.txt")) {
119 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
120 }
6e2cff2d 121 phpunit_boostrap_fix_file_permissions("$dataroot/phpunittestdir.txt");
7b0ff213
PS
122 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
123 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
124 }
125}
6e2cff2d
PS
126
127/**
128 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
129 * @param string $file
130 * @return bool success
131 */
132function phpunit_boostrap_fix_file_permissions($file) {
133 global $CFG;
134
135 $permissions = fileperms($file);
136 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
137 $permissions = $permissions | $CFG->filepermissions;
138 return chmod($file, $permissions);
139 }
140
141 return true;
920f4efe 142}