MDL-50611 testing: Extra http status code checking
[moodle.git] / lib / testing / lib.php
CommitLineData
0ea35584
DM
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
0897e190
LC
28/**
29 * Composer error exit status.
30 *
54a1ef26 31 * @var int
0897e190
LC
32 */
33define('TESTING_EXITCODE_COMPOSER', 255);
34
0ea35584
DM
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 */
41function 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
9bb80d20
PS
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 }
0ea35584
DM
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 */
76function 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 */
92function 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
ff7139d6
DM
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 */
118function 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
0ea35584
DM
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 */
137function 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 */
162function 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");
08e7f97e
TL
166 if (isset($_SERVER['REMOTE_ADDR'])) {
167 header('HTTP/1.1 500 Internal Server Error');
168 }
0ea35584
DM
169 exit($errorcode);
170}
7b333271
DM
171
172/**
173 * Updates the composer installer and the dependencies.
174 *
175 * Includes --dev dependencies.
176 *
177 * @return void exit() if something goes wrong
178 */
179function testing_update_composer_dependencies() {
7b333271
DM
180 // To restore the value after finishing.
181 $cwd = getcwd();
182
0897e190
LC
183 // Set some paths.
184 $dirroot = dirname(dirname(__DIR__));
185 $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar';
186 $composerurl = 'https://getcomposer.org/composer.phar';
187
188 // Switch to Moodle's dirroot for easier path handling.
189 chdir($dirroot);
190
191 // Download or update composer.phar. Unfortunately we can't use the curl
192 // class in filelib.php as we're running within one of the test platforms.
193 if (!file_exists($composerpath)) {
54a1ef26 194 $file = @fopen($composerpath, 'w');
0897e190
LC
195 if ($file === false) {
196 $errordetails = error_get_last();
54a1ef26 197 $error = sprintf("Unable to create composer.phar\nPHP error: %s",
0897e190
LC
198 $errordetails['message']);
199 testing_error(TESTING_EXITCODE_COMPOSER, $error);
200 }
201 $curl = curl_init();
202
203 curl_setopt($curl, CURLOPT_URL, $composerurl);
204 curl_setopt($curl, CURLOPT_FILE, $file);
205 $result = curl_exec($curl);
7b333271 206
0897e190
LC
207 $curlerrno = curl_errno($curl);
208 $curlerror = curl_error($curl);
54a1ef26 209 $curlinfo = curl_getinfo($curl);
0897e190
LC
210
211 curl_close($curl);
212 fclose($file);
213
214 if (!$result) {
215 $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s",
216 $curlerrno, $curlerror);
217 testing_error(TESTING_EXITCODE_COMPOSER, $error);
54a1ef26
DM
218 } else if ($curlinfo['http_code'] === 404) {
219 if (file_exists($composerpath)) {
220 // Deleting the resource as it would contain HTML.
221 unlink($composerpath);
222 }
223 $error = sprintf("Unable to download composer.phar\n" .
224 "404 http status code fetching $composerurl");
225 testing_error(TESTING_EXITCODE_COMPOSER, $error);
7b333271
DM
226 }
227 } else {
7b333271
DM
228 passthru("php composer.phar self-update", $code);
229 if ($code != 0) {
230 exit($code);
231 }
232 }
233
234 // Update composer dependencies.
69c150cb 235 passthru("php composer.phar install", $code);
7b333271
DM
236 if ($code != 0) {
237 exit($code);
238 }
239
0897e190 240 // Return to our original location.
7b333271
DM
241 chdir($cwd);
242}