Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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      exit($errorcode);
     167  }
     168  
     169  /**
     170   * Updates the composer installer and the dependencies.
     171   *
     172   * @return void exit() if something goes wrong
     173   */
     174  function testing_update_composer_dependencies() {
     175      // To restore the value after finishing.
     176      $cwd = getcwd();
     177  
     178      // Set some paths.
     179      $dirroot = dirname(dirname(__DIR__));
     180      $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar';
     181      $composerurl = 'https://getcomposer.org/composer.phar';
     182  
     183      // Switch to Moodle's dirroot for easier path handling.
     184      chdir($dirroot);
     185  
     186      // Download or update composer.phar. Unfortunately we can't use the curl
     187      // class in filelib.php as we're running within one of the test platforms.
     188      if (!file_exists($composerpath)) {
     189          $file = @fopen($composerpath, 'w');
     190          if ($file === false) {
     191              $errordetails = error_get_last();
     192              $error = sprintf("Unable to create composer.phar\nPHP error: %s",
     193                               $errordetails['message']);
     194              testing_error(TESTING_EXITCODE_COMPOSER, $error);
     195          }
     196          $curl = curl_init();
     197  
     198          curl_setopt($curl, CURLOPT_URL,  $composerurl);
     199          curl_setopt($curl, CURLOPT_FILE, $file);
     200          $result = curl_exec($curl);
     201  
     202          $curlerrno = curl_errno($curl);
     203          $curlerror = curl_error($curl);
     204          $curlinfo = curl_getinfo($curl);
     205  
     206          curl_close($curl);
     207          fclose($file);
     208  
     209          if (!$result) {
     210              $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s",
     211                               $curlerrno, $curlerror);
     212              testing_error(TESTING_EXITCODE_COMPOSER, $error);
     213          } else if ($curlinfo['http_code'] === 404) {
     214              if (file_exists($composerpath)) {
     215                  // Deleting the resource as it would contain HTML.
     216                  unlink($composerpath);
     217              }
     218              $error = sprintf("Unable to download composer.phar\n" .
     219                                  "404 http status code fetching $composerurl");
     220              testing_error(TESTING_EXITCODE_COMPOSER, $error);
     221          }
     222      } else {
     223          passthru("php composer.phar self-update", $code);
     224          if ($code != 0) {
     225              exit($code);
     226          }
     227      }
     228  
     229      // Update composer dependencies.
     230      passthru("php composer.phar update", $code);
     231      if ($code != 0) {
     232          exit($code);
     233      }
     234  
     235      // Return to our original location.
     236      chdir($cwd);
     237  }
    

    Search This Site: