[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/testing/ -> lib.php (source)

   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   * 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
  33   */
  34  function testing_cli_argument_path($moodlepath) {
  35      global $CFG;
  36  
  37      if (isset($CFG->admin) and $CFG->admin !== 'admin') {
  38          $moodlepath = preg_replace('|^/admin/|', "/$CFG->admin/", $moodlepath);
  39      }
  40  
  41      if (isset($_SERVER['REMOTE_ADDR'])) {
  42          // Web access, this should not happen often.
  43          $cwd = dirname(dirname(__DIR__));
  44      } else {
  45          // This is the real CLI script, work with relative paths.
  46          $cwd = getcwd();
  47      }
  48      if (substr($cwd, -1) !== DIRECTORY_SEPARATOR) {
  49          $cwd .= DIRECTORY_SEPARATOR;
  50      }
  51      $path = realpath($CFG->dirroot.$moodlepath);
  52  
  53      if (strpos($path, $cwd) === 0) {
  54          $path = substr($path, strlen($cwd));
  55      }
  56  
  57      if (testing_is_cygwin()) {
  58          $path = str_replace('\\', '/', $path);
  59      }
  60  
  61      return $path;
  62  }
  63  
  64  /**
  65   * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
  66   * @param string $file
  67   * @return bool success
  68   */
  69  function testing_fix_file_permissions($file) {
  70      global $CFG;
  71  
  72      $permissions = fileperms($file);
  73      if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
  74          $permissions = $permissions | $CFG->filepermissions;
  75          return chmod($file, $permissions);
  76      }
  77  
  78      return true;
  79  }
  80  
  81  /**
  82   * Find out if running under Cygwin on Windows.
  83   * @return bool
  84   */
  85  function testing_is_cygwin() {
  86      if (empty($_SERVER['OS']) or $_SERVER['OS'] !== 'Windows_NT') {
  87          return false;
  88  
  89      } else if (!empty($_SERVER['SHELL']) and $_SERVER['SHELL'] === '/bin/bash') {
  90          return true;
  91  
  92      } else if (!empty($_SERVER['TERM']) and $_SERVER['TERM'] === 'cygwin') {
  93          return true;
  94  
  95      } else {
  96          return false;
  97      }
  98  }
  99  
 100  /**
 101   * Returns whether a mingw CLI is running.
 102   *
 103   * MinGW sets $_SERVER['TERM'] to cygwin, but it
 104   * can not run .bat files; this function may be useful
 105   * when we need to output proposed commands to users
 106   * using Windows CLI interfaces.
 107   *
 108   * @link http://sourceforge.net/p/mingw/bugs/1902
 109   * @return bool
 110   */
 111  function testing_is_mingw() {
 112  
 113      if (!testing_is_cygwin()) {
 114          return false;
 115      }
 116  
 117      if (!empty($_SERVER['MSYSTEM'])) {
 118          return true;
 119      }
 120  
 121      return false;
 122  }
 123  
 124  /**
 125   * Mark empty dataroot to be used for testing.
 126   * @param string $dataroot  The dataroot directory
 127   * @param string $framework The test framework
 128   * @return void
 129   */
 130  function testing_initdataroot($dataroot, $framework) {
 131      global $CFG;
 132  
 133      $filename = $dataroot . '/' . $framework . 'testdir.txt';
 134  
 135      umask(0);
 136      if (!file_exists($filename)) {
 137          file_put_contents($filename, 'Contents of this directory are used during tests only, do not delete this file!');
 138      }
 139      testing_fix_file_permissions($filename);
 140  
 141      $varname = $framework . '_dataroot';
 142      $datarootdir = $CFG->{$varname} . '/' . $framework;
 143      if (!file_exists($datarootdir)) {
 144          mkdir($datarootdir, $CFG->directorypermissions);
 145      }
 146  }
 147  
 148  /**
 149   * Prints an error and stops execution
 150   *
 151   * @param integer $errorcode
 152   * @param string $text
 153   * @return void exits
 154   */
 155  function testing_error($errorcode, $text = '') {
 156  
 157      // do not write to error stream because we need the error message in PHP exec result from web ui
 158      echo($text."\n");
 159      exit($errorcode);
 160  }
 161  
 162  /**
 163   * Updates the composer installer and the dependencies.
 164   *
 165   * Includes --dev dependencies.
 166   *
 167   * @return void exit() if something goes wrong
 168   */
 169  function testing_update_composer_dependencies() {
 170  
 171      // To restore the value after finishing.
 172      $cwd = getcwd();
 173  
 174      // Dirroot.
 175      chdir(__DIR__ . '/../..');
 176  
 177      // Download composer.phar if we can.
 178      if (!file_exists(__DIR__ . '/../../composer.phar')) {
 179          passthru("curl http://getcomposer.org/installer | php", $code);
 180          if ($code != 0) {
 181              exit($code);
 182          }
 183      } else {
 184  
 185          // If it is already there update the installer.
 186          passthru("php composer.phar self-update", $code);
 187          if ($code != 0) {
 188              exit($code);
 189          }
 190      }
 191  
 192      // Update composer dependencies.
 193      passthru("php composer.phar update --dev", $code);
 194      if ($code != 0) {
 195          exit($code);
 196      }
 197  
 198      chdir($cwd);
 199  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1