on-demand release 2.3dev
[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
PS
65 case PHPUNIT_EXITCODE_INSTALL:
66 $text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/init.php";
7b0ff213 67 break;
0d8e51a6
PS
68 case PHPUNIT_EXITCODE_REINSTALL:
69 $text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/init.php";
7b0ff213
PS
70 break;
71 default:
72 $text = empty($text) ? '' : ': '.$text;
73 $text = 'Unknown error '.$errorcode.$text;
74 break;
75 }
0d8e51a6
PS
76
77 // do not write to error stream because we need the error message in PHP exec result from web ui
78 echo($text."\n");
7b0ff213
PS
79 exit($errorcode);
80}
81
82/**
83 * Mark empty dataroot to be used for testing.
84 * @param string $dataroot The dataroot directory
85 * @return void
86 */
87function phpunit_bootstrap_initdataroot($dataroot) {
88 global $CFG;
89 umask(0);
90 if (!file_exists("$dataroot/phpunittestdir.txt")) {
91 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
92 }
6e2cff2d 93 phpunit_boostrap_fix_file_permissions("$dataroot/phpunittestdir.txt");
7b0ff213
PS
94 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
95 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
96 }
97}
6e2cff2d
PS
98
99/**
100 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
101 * @param string $file
102 * @return bool success
103 */
104function phpunit_boostrap_fix_file_permissions($file) {
105 global $CFG;
106
107 $permissions = fileperms($file);
108 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
109 $permissions = $permissions | $CFG->filepermissions;
110 return chmod($file, $permissions);
111 }
112
113 return true;
920f4efe 114}