MDL-60425 blog: Remove empty line
[moodle.git] / lib / testing / lib.php
blobd796d83846cec5d318e5a1c8bfa9e7e8586f61e3
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.
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/>.
17 /**
18 * Testing general functions
20 * Note: these functions must be self contained and must not rely on any library or include
22 * @package core
23 * @category test
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 /**
29 * Composer error exit status.
31 * @var int
33 define('TESTING_EXITCODE_COMPOSER', 255);
35 /**
36 * Returns relative path against current working directory,
37 * to be used for shell execution hints.
38 * @param string $moodlepath starting with "/", ex: "/admin/tool/cli/init.php"
39 * @return string path relative to current directory or absolute path
41 function testing_cli_argument_path($moodlepath) {
42 global $CFG;
44 if (isset($CFG->admin) and $CFG->admin !== 'admin') {
45 $moodlepath = preg_replace('|^/admin/|', "/$CFG->admin/", $moodlepath);
48 if (isset($_SERVER['REMOTE_ADDR'])) {
49 // Web access, this should not happen often.
50 $cwd = dirname(dirname(__DIR__));
51 } else {
52 // This is the real CLI script, work with relative paths.
53 $cwd = getcwd();
56 // Remove last directory separator as $path will not contain one.
57 if ((substr($cwd, -1) === '/') || (substr($cwd, -1) === '\\')) {
58 $cwd = substr($cwd, -1);
61 $path = realpath($CFG->dirroot.$moodlepath);
63 // We need standrad directory seperator for path and cwd, so it can be compared.
64 $cwd = testing_cli_fix_directory_separator($cwd);
65 $path = testing_cli_fix_directory_separator($path);
67 if (strpos($path, $cwd) === 0) {
68 // Remove current working directory and directory separator.
69 $path = substr($path, strlen($cwd) + 1);
72 return $path;
75 /**
76 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
77 * @param string $file
78 * @return bool success
80 function testing_fix_file_permissions($file) {
81 global $CFG;
83 $permissions = fileperms($file);
84 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
85 $permissions = $permissions | $CFG->filepermissions;
86 return chmod($file, $permissions);
89 return true;
92 /**
93 * Find out if running under Cygwin on Windows.
94 * @return bool
96 function testing_is_cygwin() {
97 if (empty($_SERVER['OS']) or $_SERVER['OS'] !== 'Windows_NT') {
98 return false;
100 } else if (!empty($_SERVER['SHELL']) and $_SERVER['SHELL'] === '/bin/bash') {
101 return true;
103 } else if (!empty($_SERVER['TERM']) and $_SERVER['TERM'] === 'cygwin') {
104 return true;
106 } else {
107 return false;
112 * Returns whether a mingw CLI is running.
114 * MinGW sets $_SERVER['TERM'] to cygwin, but it
115 * can not run .bat files; this function may be useful
116 * when we need to output proposed commands to users
117 * using Windows CLI interfaces.
119 * @link http://sourceforge.net/p/mingw/bugs/1902
120 * @return bool
122 function testing_is_mingw() {
124 if (!testing_is_cygwin()) {
125 return false;
128 if (!empty($_SERVER['MSYSTEM'])) {
129 return true;
132 return false;
136 * Mark empty dataroot to be used for testing.
137 * @param string $dataroot The dataroot directory
138 * @param string $framework The test framework
139 * @return void
141 function testing_initdataroot($dataroot, $framework) {
142 global $CFG;
144 $filename = $dataroot . '/' . $framework . 'testdir.txt';
146 umask(0);
147 if (!file_exists($filename)) {
148 file_put_contents($filename, 'Contents of this directory are used during tests only, do not delete this file!');
150 testing_fix_file_permissions($filename);
152 $varname = $framework . '_dataroot';
153 $datarootdir = $CFG->{$varname} . '/' . $framework;
154 if (!file_exists($datarootdir)) {
155 mkdir($datarootdir, $CFG->directorypermissions);
160 * Prints an error and stops execution
162 * @param integer $errorcode
163 * @param string $text
164 * @return void exits
166 function testing_error($errorcode, $text = '') {
168 // do not write to error stream because we need the error message in PHP exec result from web ui
169 echo($text."\n");
170 if (isset($_SERVER['REMOTE_ADDR'])) {
171 header('HTTP/1.1 500 Internal Server Error');
173 exit($errorcode);
177 * Updates the composer installer and the dependencies.
179 * @return void exit() if something goes wrong
181 function testing_update_composer_dependencies() {
182 // To restore the value after finishing.
183 $cwd = getcwd();
185 // Set some paths.
186 $dirroot = dirname(dirname(__DIR__));
187 $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar';
188 $composerurl = 'https://getcomposer.org/composer.phar';
190 // Switch to Moodle's dirroot for easier path handling.
191 chdir($dirroot);
193 // Download or update composer.phar. Unfortunately we can't use the curl
194 // class in filelib.php as we're running within one of the test platforms.
195 if (!file_exists($composerpath)) {
196 $file = @fopen($composerpath, 'w');
197 if ($file === false) {
198 $errordetails = error_get_last();
199 $error = sprintf("Unable to create composer.phar\nPHP error: %s",
200 $errordetails['message']);
201 testing_error(TESTING_EXITCODE_COMPOSER, $error);
203 $curl = curl_init();
205 curl_setopt($curl, CURLOPT_URL, $composerurl);
206 curl_setopt($curl, CURLOPT_FILE, $file);
207 $result = curl_exec($curl);
209 $curlerrno = curl_errno($curl);
210 $curlerror = curl_error($curl);
211 $curlinfo = curl_getinfo($curl);
213 curl_close($curl);
214 fclose($file);
216 if (!$result) {
217 $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s",
218 $curlerrno, $curlerror);
219 testing_error(TESTING_EXITCODE_COMPOSER, $error);
220 } else if ($curlinfo['http_code'] === 404) {
221 if (file_exists($composerpath)) {
222 // Deleting the resource as it would contain HTML.
223 unlink($composerpath);
225 $error = sprintf("Unable to download composer.phar\n" .
226 "404 http status code fetching $composerurl");
227 testing_error(TESTING_EXITCODE_COMPOSER, $error);
229 } else {
230 passthru("php composer.phar self-update", $code);
231 if ($code != 0) {
232 exit($code);
236 // Update composer dependencies.
237 passthru("php composer.phar install", $code);
238 if ($code != 0) {
239 exit($code);
242 // Return to our original location.
243 chdir($cwd);
247 * Fix DIRECTORY_SEPARATOR for windows.
249 * In PHP on Windows, DIRECTORY_SEPARATOR is set to the backslash (\)
250 * character. However, if you're running a Cygwin/Msys/Git shell
251 * exec() calls will return paths using the forward slash (/) character.
253 * NOTE: Because PHP on Windows will accept either forward or backslashes,
254 * paths should be built using ONLY forward slashes, regardless of
255 * OS. MOODLE_DIRECTORY_SEPARATOR should only be used when parsing
256 * paths returned by the shell.
258 * @param string $path
259 * @return string.
261 function testing_cli_fix_directory_separator($path) {
262 global $CFG;
264 static $dirseparator = null;
266 if (!$dirseparator) {
267 // Default directory separator.
268 $dirseparator = DIRECTORY_SEPARATOR;
270 // On windows we need to find what directory separator is used.
271 if ($CFG->ostype = 'WINDOWS') {
272 if (!empty($_SERVER['argv'][0])) {
273 if (false === strstr($_SERVER['argv'][0], '\\')) {
274 $dirseparator = '/';
275 } else {
276 $dirseparator = '\\';
278 } else if (testing_is_cygwin()) {
279 $dirseparator = '/';
284 // Normalize \ and / to directory separator.
285 $path = str_replace('\\', $dirseparator, $path);
286 $path = str_replace('/', $dirseparator, $path);
288 return $path;