[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 if (substr($cwd, -1) !== DIRECTORY_SEPARATOR) { 56 $cwd .= DIRECTORY_SEPARATOR; 57 } 58 $path = realpath($CFG->dirroot.$moodlepath); 59 60 if (strpos($path, $cwd) === 0) { 61 $path = substr($path, strlen($cwd)); 62 } 63 64 if (testing_is_cygwin()) { 65 $path = str_replace('\\', '/', $path); 66 } 67 68 return $path; 69 } 70 71 /** 72 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible 73 * @param string $file 74 * @return bool success 75 */ 76 function testing_fix_file_permissions($file) { 77 global $CFG; 78 79 $permissions = fileperms($file); 80 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) { 81 $permissions = $permissions | $CFG->filepermissions; 82 return chmod($file, $permissions); 83 } 84 85 return true; 86 } 87 88 /** 89 * Find out if running under Cygwin on Windows. 90 * @return bool 91 */ 92 function testing_is_cygwin() { 93 if (empty($_SERVER['OS']) or $_SERVER['OS'] !== 'Windows_NT') { 94 return false; 95 96 } else if (!empty($_SERVER['SHELL']) and $_SERVER['SHELL'] === '/bin/bash') { 97 return true; 98 99 } else if (!empty($_SERVER['TERM']) and $_SERVER['TERM'] === 'cygwin') { 100 return true; 101 102 } else { 103 return false; 104 } 105 } 106 107 /** 108 * Returns whether a mingw CLI is running. 109 * 110 * MinGW sets $_SERVER['TERM'] to cygwin, but it 111 * can not run .bat files; this function may be useful 112 * when we need to output proposed commands to users 113 * using Windows CLI interfaces. 114 * 115 * @link http://sourceforge.net/p/mingw/bugs/1902 116 * @return bool 117 */ 118 function testing_is_mingw() { 119 120 if (!testing_is_cygwin()) { 121 return false; 122 } 123 124 if (!empty($_SERVER['MSYSTEM'])) { 125 return true; 126 } 127 128 return false; 129 } 130 131 /** 132 * Mark empty dataroot to be used for testing. 133 * @param string $dataroot The dataroot directory 134 * @param string $framework The test framework 135 * @return void 136 */ 137 function testing_initdataroot($dataroot, $framework) { 138 global $CFG; 139 140 $filename = $dataroot . '/' . $framework . 'testdir.txt'; 141 142 umask(0); 143 if (!file_exists($filename)) { 144 file_put_contents($filename, 'Contents of this directory are used during tests only, do not delete this file!'); 145 } 146 testing_fix_file_permissions($filename); 147 148 $varname = $framework . '_dataroot'; 149 $datarootdir = $CFG->{$varname} . '/' . $framework; 150 if (!file_exists($datarootdir)) { 151 mkdir($datarootdir, $CFG->directorypermissions); 152 } 153 } 154 155 /** 156 * Prints an error and stops execution 157 * 158 * @param integer $errorcode 159 * @param string $text 160 * @return void exits 161 */ 162 function testing_error($errorcode, $text = '') { 163 164 // do not write to error stream because we need the error message in PHP exec result from web ui 165 echo($text."\n"); 166 if (isset($_SERVER['REMOTE_ADDR'])) { 167 header('HTTP/1.1 500 Internal Server Error'); 168 } 169 exit($errorcode); 170 } 171 172 /** 173 * Updates the composer installer and the dependencies. 174 * 175 * @return void exit() if something goes wrong 176 */ 177 function testing_update_composer_dependencies() { 178 // To restore the value after finishing. 179 $cwd = getcwd(); 180 181 // Set some paths. 182 $dirroot = dirname(dirname(__DIR__)); 183 $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar'; 184 $composerurl = 'https://getcomposer.org/composer.phar'; 185 186 // Switch to Moodle's dirroot for easier path handling. 187 chdir($dirroot); 188 189 // Download or update composer.phar. Unfortunately we can't use the curl 190 // class in filelib.php as we're running within one of the test platforms. 191 if (!file_exists($composerpath)) { 192 $file = @fopen($composerpath, 'w'); 193 if ($file === false) { 194 $errordetails = error_get_last(); 195 $error = sprintf("Unable to create composer.phar\nPHP error: %s", 196 $errordetails['message']); 197 testing_error(TESTING_EXITCODE_COMPOSER, $error); 198 } 199 $curl = curl_init(); 200 201 curl_setopt($curl, CURLOPT_URL, $composerurl); 202 curl_setopt($curl, CURLOPT_FILE, $file); 203 $result = curl_exec($curl); 204 205 $curlerrno = curl_errno($curl); 206 $curlerror = curl_error($curl); 207 $curlinfo = curl_getinfo($curl); 208 209 curl_close($curl); 210 fclose($file); 211 212 if (!$result) { 213 $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s", 214 $curlerrno, $curlerror); 215 testing_error(TESTING_EXITCODE_COMPOSER, $error); 216 } else if ($curlinfo['http_code'] === 404) { 217 if (file_exists($composerpath)) { 218 // Deleting the resource as it would contain HTML. 219 unlink($composerpath); 220 } 221 $error = sprintf("Unable to download composer.phar\n" . 222 "404 http status code fetching $composerurl"); 223 testing_error(TESTING_EXITCODE_COMPOSER, $error); 224 } 225 } else { 226 passthru("php composer.phar self-update", $code); 227 if ($code != 0) { 228 exit($code); 229 } 230 } 231 232 // Update composer dependencies. 233 passthru("php composer.phar install", $code); 234 if ($code != 0) { 235 exit($code); 236 } 237 238 // Return to our original location. 239 chdir($cwd); 240 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |