See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 * Testing general functions 19 * 20 * Note: these functions must be self contained and must not rely on any library or include 21 * 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 26 */ 27 28 /** 29 * Composer error exit status. 30 * 31 * @var int 32 */ 33 define('TESTING_EXITCODE_COMPOSER', 255); 34 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 40 */ 41 function testing_cli_argument_path($moodlepath) { 42 global $CFG; 43 44 if (isset($CFG->admin) and $CFG->admin !== 'admin') { 45 $moodlepath = preg_replace('|^/admin/|', "/$CFG->admin/", $moodlepath); 46 } 47 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(); 54 } 55 56 // Remove last directory separator as $path will not contain one. 57 if ((substr($cwd, -1) === '/') || (substr($cwd, -1) === '\\')) { 58 $cwd = substr($cwd, -1); 59 } 60 61 $path = realpath($CFG->dirroot.$moodlepath); 62 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); 66 67 if (strpos($path, $cwd) === 0) { 68 // Remove current working directory and directory separator. 69 $path = substr($path, strlen($cwd) + 1); 70 } 71 72 return $path; 73 } 74 75 /** 76 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible 77 * @param string $file 78 * @return bool success 79 */ 80 function testing_fix_file_permissions($file) { 81 global $CFG; 82 83 $permissions = fileperms($file); 84 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) { 85 $permissions = $permissions | $CFG->filepermissions; 86 return chmod($file, $permissions); 87 } 88 89 return true; 90 } 91 92 /** 93 * Find out if running under Cygwin on Windows. 94 * @return bool 95 */ 96 function testing_is_cygwin() { 97 if (empty($_SERVER['OS']) or $_SERVER['OS'] !== 'Windows_NT') { 98 return false; 99 100 } else if (!empty($_SERVER['SHELL']) and $_SERVER['SHELL'] === '/bin/bash') { 101 return true; 102 103 } else if (!empty($_SERVER['TERM']) and $_SERVER['TERM'] === 'cygwin') { 104 return true; 105 106 } else { 107 return false; 108 } 109 } 110 111 /** 112 * Returns whether a mingw CLI is running. 113 * 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. 118 * 119 * @link http://sourceforge.net/p/mingw/bugs/1902 120 * @return bool 121 */ 122 function testing_is_mingw() { 123 124 if (!testing_is_cygwin()) { 125 return false; 126 } 127 128 if (!empty($_SERVER['MSYSTEM'])) { 129 return true; 130 } 131 132 return false; 133 } 134 135 /** 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 140 */ 141 function testing_initdataroot($dataroot, $framework) { 142 global $CFG; 143 144 $filename = $dataroot . '/' . $framework . 'testdir.txt'; 145 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!'); 149 } 150 testing_fix_file_permissions($filename); 151 152 $varname = $framework . '_dataroot'; 153 $datarootdir = $CFG->{$varname} . '/' . $framework; 154 if (!file_exists($datarootdir)) { 155 mkdir($datarootdir, $CFG->directorypermissions); 156 } 157 } 158 159 /** 160 * Prints an error and stops execution 161 * 162 * @param integer $errorcode 163 * @param string $text 164 * @return void exits 165 */ 166 function testing_error($errorcode, $text = '') { 167 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'); 172 } 173 exit($errorcode); 174 } 175 176 /** 177 * Updates the composer installer and the dependencies. 178 * 179 * @return void exit() if something goes wrong 180 */ 181 function testing_update_composer_dependencies() { 182 // To restore the value after finishing. 183 $cwd = getcwd(); 184 185 // Set some paths. 186 $dirroot = dirname(dirname(__DIR__)); 187 $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar'; 188 $composerurl = 'https://getcomposer.org/composer.phar'; 189 190 // Switch to Moodle's dirroot for easier path handling. 191 chdir($dirroot); 192 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); 202 } 203 $curl = curl_init(); 204 205 curl_setopt($curl, CURLOPT_URL, $composerurl); 206 curl_setopt($curl, CURLOPT_FILE, $file); 207 $result = curl_exec($curl); 208 209 $curlerrno = curl_errno($curl); 210 $curlerror = curl_error($curl); 211 $curlinfo = curl_getinfo($curl); 212 213 curl_close($curl); 214 fclose($file); 215 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); 224 } 225 $error = sprintf("Unable to download composer.phar\n" . 226 "404 http status code fetching $composerurl"); 227 testing_error(TESTING_EXITCODE_COMPOSER, $error); 228 } 229 } else { 230 passthru("php composer.phar self-update", $code); 231 if ($code != 0) { 232 exit($code); 233 } 234 } 235 236 // Update composer dependencies. 237 passthru("php composer.phar install", $code); 238 if ($code != 0) { 239 exit($code); 240 } 241 242 // Return to our original location. 243 chdir($cwd); 244 } 245 246 /** 247 * Fix DIRECTORY_SEPARATOR for windows. 248 * 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. 252 * 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. 257 * 258 * @param string $path 259 * @return string. 260 */ 261 function testing_cli_fix_directory_separator($path) { 262 global $CFG; 263 264 static $dirseparator = null; 265 266 if (!$dirseparator) { 267 // Default directory separator. 268 $dirseparator = DIRECTORY_SEPARATOR; 269 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 = '\\'; 277 } 278 } else if (testing_is_cygwin()) { 279 $dirseparator = '/'; 280 } 281 } 282 } 283 284 // Normalize \ and / to directory separator. 285 $path = str_replace('\\', $dirseparator, $path); 286 $path = str_replace('/', $dirseparator, $path); 287 288 return $path; 289 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body