Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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   * Prepares PHPUnit environment, the phpunit.xml configuration
  19   * must specify this file as bootstrap.
  20   *
  21   * Exit codes: {@see phpunit_bootstrap_error()}
  22   *
  23   * @package    core
  24   * @category   phpunit
  25   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  if (isset($_SERVER['REMOTE_ADDR'])) {
  30      die; // No access from web!
  31  }
  32  
  33  // we want to know about all problems
  34  error_reporting(E_ALL | E_STRICT);
  35  ini_set('display_errors', '1');
  36  ini_set('log_errors', '1');
  37  
  38  // Make sure OPcache does not strip comments, we need them in phpunit!
  39  if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {
  40      if (!ini_get('opcache.save_comments') or strtolower(ini_get('opcache.save_comments')) === 'off') {
  41          ini_set('opcache.enable', 0);
  42      }
  43  }
  44  
  45  if (!defined('IGNORE_COMPONENT_CACHE')) {
  46      define('IGNORE_COMPONENT_CACHE', true);
  47  }
  48  
  49  require_once (__DIR__.'/bootstraplib.php');
  50  require_once (__DIR__.'/../testing/lib.php');
  51  
  52  if (isset($_SERVER['REMOTE_ADDR'])) {
  53      phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
  54  }
  55  
  56  if (defined('PHPUNIT_TEST')) {
  57      phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
  58  }
  59  /** PHPUnit testing framework active */
  60  define('PHPUNIT_TEST', true);
  61  
  62  if (!defined('PHPUNIT_UTIL')) {
  63      /** Identifies utility scripts - the database does not need to be initialised */
  64      define('PHPUNIT_UTIL', false);
  65  }
  66  
  67  if (defined('CLI_SCRIPT')) {
  68      phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
  69  }
  70  define('CLI_SCRIPT', true);
  71  
  72  $phpunitversion = PHPUnit\Runner\Version::id();
  73  if ($phpunitversion === '@package_version@') {
  74      // library checked out from git, let's hope dev knows that 3.6.0 is required
  75  } else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
  76      phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
  77  }
  78  unset($phpunitversion);
  79  
  80  // only load CFG from config.php, stop ASAP in lib/setup.php
  81  define('ABORT_AFTER_CONFIG', true);
  82  require(__DIR__ . '/../../config.php');
  83  
  84  if (!defined('PHPUNIT_LONGTEST')) {
  85      /** Execute longer version of tests */
  86      define('PHPUNIT_LONGTEST', false);
  87  }
  88  
  89  // remove error handling overrides done in config.php
  90  error_reporting(E_ALL | E_STRICT);
  91  ini_set('display_errors', '1');
  92  ini_set('log_errors', '1');
  93  set_time_limit(0); // no time limit in CLI scripts, user may cancel execution
  94  
  95  // prepare dataroot
  96  umask(0);
  97  if (isset($CFG->phpunit_directorypermissions)) {
  98      $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
  99  } else {
 100      $CFG->directorypermissions = 02777;
 101  }
 102  $CFG->filepermissions = ($CFG->directorypermissions & 0666);
 103  if (!isset($CFG->phpunit_dataroot)) {
 104      phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
 105  }
 106  
 107  // Create test dir if does not exists yet.
 108  if (!file_exists($CFG->phpunit_dataroot)) {
 109      mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
 110  }
 111  if (!is_dir($CFG->phpunit_dataroot)) {
 112      phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
 113  }
 114  
 115  // Ensure we access to phpunit_dataroot realpath always.
 116  $CFG->phpunit_dataroot = realpath($CFG->phpunit_dataroot);
 117  
 118  if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
 119      phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
 120  }
 121  
 122  if (!is_writable($CFG->phpunit_dataroot)) {
 123      // try to fix permissions if possible
 124      if (function_exists('posix_getuid')) {
 125          $chmod = fileperms($CFG->phpunit_dataroot);
 126          if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
 127              $chmod = $chmod | 0700;
 128              chmod($CFG->phpunit_dataroot, $chmod);
 129          }
 130      }
 131      if (!is_writable($CFG->phpunit_dataroot)) {
 132          phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
 133      }
 134  }
 135  if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
 136      if ($dh = opendir($CFG->phpunit_dataroot)) {
 137          while (($file = readdir($dh)) !== false) {
 138              if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
 139                  continue;
 140              }
 141              phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
 142          }
 143          closedir($dh);
 144          unset($dh);
 145          unset($file);
 146      }
 147  
 148      // now we are 100% sure this dir is used only for phpunit tests
 149      testing_initdataroot($CFG->phpunit_dataroot, 'phpunit');
 150  }
 151  
 152  // verify db prefix
 153  if (!isset($CFG->phpunit_prefix)) {
 154      phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
 155  }
 156  if ($CFG->phpunit_prefix === '') {
 157      phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
 158  }
 159  if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
 160      phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
 161  }
 162  
 163  // override CFG settings if necessary and throw away extra CFG settings
 164  $CFG->wwwroot   = 'https://www.example.com/moodle';
 165  $CFG->dataroot  = $CFG->phpunit_dataroot;
 166  $CFG->prefix    = $CFG->phpunit_prefix;
 167  $CFG->dbtype    = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
 168  $CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
 169  $CFG->dbhost    = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
 170  $CFG->dbname    = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
 171  $CFG->dbuser    = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
 172  $CFG->dbpass    = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
 173  $CFG->prefix    = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
 174  $CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
 175  
 176  $allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
 177                   'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
 178                   'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
 179                   'altcacheconfigpath', 'pathtogs', 'pathtophp', 'pathtodu', 'aspellpath', 'pathtodot',
 180                   'pathtounoconv', 'alternative_file_system_class', 'pathtopython'
 181                  );
 182  $productioncfg = (array)$CFG;
 183  $CFG = new stdClass();
 184  foreach ($productioncfg as $key=>$value) {
 185      if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0 and strpos($key, 'behat_') !== 0) {
 186          // ignore
 187          continue;
 188      }
 189      $CFG->{$key} = $value;
 190  }
 191  unset($key);
 192  unset($value);
 193  unset($allowed);
 194  unset($productioncfg);
 195  
 196  // force the same CFG settings in all sites
 197  $CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
 198  $CFG->debugdeveloper = true;
 199  $CFG->debugdisplay = 1;
 200  error_reporting($CFG->debug);
 201  ini_set('display_errors', '1');
 202  ini_set('log_errors', '1');
 203  
 204  // some ugly hacks
 205  $CFG->themerev = 1;
 206  $CFG->jsrev = 1;
 207  
 208  
 209  (function () {
 210      // Determine if this test is being run with isolation.
 211      // This is tricky because neither PHPUnit, nor PHP provide an official way to work this out.
 212      // PHPUnit does set a value, but not until later on and we need this earlier.
 213      // PHPUnit runs isolated tests by creating a class on the fly and running it through proc_open as standard input.
 214      // There is no other legitimate reason to run PHPUnit this way that I'm aware of.
 215      // When run in this way, PHP sets the value of $_SERVER['PHP_SELF'] to "Standard input code".
 216      // It has done this since 2016, and it is unlikely to change.
 217      define(
 218          'PHPUNIT_ISOLATED_TEST',
 219          $_SERVER['PHP_SELF'] === 'Standard input code',
 220      );
 221  })();
 222  
 223  // load test case stub classes and other stuff
 224  require_once("$CFG->dirroot/lib/phpunit/lib.php");
 225  
 226  // finish moodle init
 227  define('ABORT_AFTER_CONFIG_CANCEL', true);
 228  if (isset($CFG->phpunit_profilingenabled) && $CFG->phpunit_profilingenabled) {
 229      $CFG->profilingenabled = true;
 230      $CFG->profilingincluded = '*';
 231  }
 232  require("$CFG->dirroot/lib/setup.php");
 233  
 234  raise_memory_limit(MEMORY_HUGE);
 235  
 236  if (PHPUNIT_UTIL) {
 237      // we are not going to do testing, this is 'true' in utility scripts that only init database
 238      return;
 239  }
 240  
 241  // Make sure the hook manager gets initialised before anybody tries to override callbacks,
 242  // this is not using caches intentionally to help with development.
 243  \core\hook\manager::get_instance();
 244  
 245  // is database and dataroot ready for testing?
 246  list($errorcode, $message) = phpunit_util::testing_ready_problem();
 247  // print some version info
 248  phpunit_util::bootstrap_moodle_info();
 249  if ($errorcode) {
 250      phpunit_bootstrap_error($errorcode, $message);
 251  }
 252  
 253  // prepare for the first test run - store fresh globals, reset database and dataroot, etc.
 254  phpunit_util::bootstrap_init();