2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Testing general functions
20 * Note: these functions must be self contained and must not rely on any library or include
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 * Returns relative path against current working directory,
30 * to be used for shell execution hints.
31 * @param string $moodlepath starting with "/", ex: "/admin/tool/cli/init.php"
32 * @return string path relative to current directory or absolute path
34 function testing_cli_argument_path($moodlepath) {
37 if (isset($CFG->admin
) and $CFG->admin
!== 'admin') {
38 $moodlepath = preg_replace('|^/admin/|', "/$CFG->admin/", $moodlepath);
42 if (substr($cwd, -1) !== DIRECTORY_SEPARATOR
) {
43 $cwd .= DIRECTORY_SEPARATOR
;
45 $path = realpath($CFG->dirroot
.$moodlepath);
47 if (strpos($path, $cwd) === 0) {
48 $path = substr($path, strlen($cwd));
51 if (testing_is_cygwin()) {
52 $path = str_replace('\\', '/', $path);
59 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
61 * @return bool success
63 function testing_fix_file_permissions($file) {
66 $permissions = fileperms($file);
67 if ($permissions & $CFG->filepermissions
!= $CFG->filepermissions
) {
68 $permissions = $permissions |
$CFG->filepermissions
;
69 return chmod($file, $permissions);
76 * Find out if running under Cygwin on Windows.
79 function testing_is_cygwin() {
80 if (empty($_SERVER['OS']) or $_SERVER['OS'] !== 'Windows_NT') {
83 } else if (!empty($_SERVER['SHELL']) and $_SERVER['SHELL'] === '/bin/bash') {
86 } else if (!empty($_SERVER['TERM']) and $_SERVER['TERM'] === 'cygwin') {
95 * Mark empty dataroot to be used for testing.
96 * @param string $dataroot The dataroot directory
97 * @param string $framework The test framework
100 function testing_initdataroot($dataroot, $framework) {
103 $filename = $dataroot . '/' . $framework . 'testdir.txt';
106 if (!file_exists($filename)) {
107 file_put_contents($filename, 'Contents of this directory are used during tests only, do not delete this file!');
109 testing_fix_file_permissions($filename);
111 $varname = $framework . '_dataroot';
112 $datarootdir = $CFG->{$varname} . '/' . $framework;
113 if (!file_exists($datarootdir)) {
114 mkdir($datarootdir, $CFG->directorypermissions
);
119 * Prints an error and stops execution
121 * @param integer $errorcode
122 * @param string $text
125 function testing_error($errorcode, $text = '') {
127 // do not write to error stream because we need the error message in PHP exec result from web ui
133 * Updates the composer installer and the dependencies.
135 * Includes --dev dependencies.
137 * @return void exit() if something goes wrong
139 function testing_update_composer_dependencies() {
141 // To restore the value after finishing.
145 chdir(__DIR__
. '/../..');
147 // Download composer.phar if we can.
148 if (!file_exists(__DIR__
. '/../../composer.phar')) {
149 passthru("curl http://getcomposer.org/installer | php", $code);
155 // If it is already there update the installer.
156 passthru("php composer.phar self-update", $code);
162 // Update composer dependencies.
163 passthru("php composer.phar update --dev", $code);