PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/phpunit/bootstrap.php

https://github.com/epsd/moodle
PHP | 222 lines | 143 code | 32 blank | 47 comment | 30 complexity | b0b43b00a92c5f3567868a999136f7e4 MD5 | raw file
  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. * Prepares PHPUnit environment, the phpunit.xml configuration
  18. * must specify this file as bootstrap.
  19. *
  20. * Exit codes: {@see phpunit_bootstrap_error()}
  21. *
  22. * @package core
  23. * @category phpunit
  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. // we want to know about all problems
  28. error_reporting(E_ALL | E_STRICT);
  29. ini_set('display_errors', '1');
  30. ini_set('log_errors', '1');
  31. require_once(__DIR__.'/bootstraplib.php');
  32. require_once(__DIR__.'/../testing/lib.php');
  33. if (isset($_SERVER['REMOTE_ADDR'])) {
  34. phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
  35. }
  36. if (defined('PHPUNIT_TEST')) {
  37. phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
  38. }
  39. /** PHPUnit testing framework active */
  40. define('PHPUNIT_TEST', true);
  41. if (!defined('PHPUNIT_UTIL')) {
  42. /** Identifies utility scripts - the database does not need to be initialised */
  43. define('PHPUNIT_UTIL', false);
  44. }
  45. if (defined('CLI_SCRIPT')) {
  46. phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
  47. }
  48. define('CLI_SCRIPT', true);
  49. $phpunitversion = PHPUnit_Runner_Version::id();
  50. if ($phpunitversion === '@package_version@') {
  51. // library checked out from git, let's hope dev knows that 3.6.0 is required
  52. } else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
  53. phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
  54. }
  55. unset($phpunitversion);
  56. if (!include_once('PHPUnit/Extensions/Database/Autoload.php')) {
  57. phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
  58. }
  59. define('NO_OUTPUT_BUFFERING', true);
  60. // only load CFG from config.php, stop ASAP in lib/setup.php
  61. define('ABORT_AFTER_CONFIG', true);
  62. require(__DIR__ . '/../../config.php');
  63. if (!defined('PHPUNIT_LONGTEST')) {
  64. /** Execute longer version of tests */
  65. define('PHPUNIT_LONGTEST', false);
  66. }
  67. // remove error handling overrides done in config.php
  68. error_reporting(E_ALL | E_STRICT);
  69. ini_set('display_errors', '1');
  70. ini_set('log_errors', '1');
  71. set_time_limit(0); // no time limit in CLI scripts, user may cancel execution
  72. // prepare dataroot
  73. umask(0);
  74. if (isset($CFG->phpunit_directorypermissions)) {
  75. $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
  76. } else {
  77. $CFG->directorypermissions = 02777;
  78. }
  79. $CFG->filepermissions = ($CFG->directorypermissions & 0666);
  80. if (!isset($CFG->phpunit_dataroot)) {
  81. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
  82. }
  83. // Create test dir if does not exists yet.
  84. if (!file_exists($CFG->phpunit_dataroot)) {
  85. mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
  86. }
  87. if (!is_dir($CFG->phpunit_dataroot)) {
  88. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
  89. }
  90. // Ensure we access to phpunit_dataroot realpath always.
  91. $CFG->phpunit_dataroot = realpath($CFG->phpunit_dataroot);
  92. if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
  93. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
  94. }
  95. if (!is_writable($CFG->phpunit_dataroot)) {
  96. // try to fix permissions if possible
  97. if (function_exists('posix_getuid')) {
  98. $chmod = fileperms($CFG->phpunit_dataroot);
  99. if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
  100. $chmod = $chmod | 0700;
  101. chmod($CFG->phpunit_dataroot, $chmod);
  102. }
  103. }
  104. if (!is_writable($CFG->phpunit_dataroot)) {
  105. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
  106. }
  107. }
  108. if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
  109. if ($dh = opendir($CFG->phpunit_dataroot)) {
  110. while (($file = readdir($dh)) !== false) {
  111. if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
  112. continue;
  113. }
  114. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
  115. }
  116. closedir($dh);
  117. unset($dh);
  118. unset($file);
  119. }
  120. // now we are 100% sure this dir is used only for phpunit tests
  121. testing_initdataroot($CFG->phpunit_dataroot, 'phpunit');
  122. }
  123. // verify db prefix
  124. if (!isset($CFG->phpunit_prefix)) {
  125. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
  126. }
  127. if ($CFG->phpunit_prefix === '') {
  128. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
  129. }
  130. if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
  131. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
  132. }
  133. // override CFG settings if necessary and throw away extra CFG settings
  134. $CFG->wwwroot = 'http://www.example.com/moodle';
  135. $CFG->dataroot = $CFG->phpunit_dataroot;
  136. $CFG->prefix = $CFG->phpunit_prefix;
  137. $CFG->dbtype = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
  138. $CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
  139. $CFG->dbhost = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
  140. $CFG->dbname = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
  141. $CFG->dbuser = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
  142. $CFG->dbpass = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
  143. $CFG->prefix = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
  144. $CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
  145. $allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
  146. 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
  147. 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
  148. );
  149. $productioncfg = (array)$CFG;
  150. $CFG = new stdClass();
  151. foreach ($productioncfg as $key=>$value) {
  152. if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
  153. // ignore
  154. continue;
  155. }
  156. $CFG->{$key} = $value;
  157. }
  158. unset($key);
  159. unset($value);
  160. unset($allowed);
  161. unset($productioncfg);
  162. // force the same CFG settings in all sites
  163. $CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
  164. $CFG->debugdisplay = 1;
  165. error_reporting($CFG->debug);
  166. ini_set('display_errors', '1');
  167. ini_set('log_errors', '1');
  168. $CFG->passwordsaltmain = 'phpunit'; // makes login via normal UI impossible
  169. $CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
  170. $CFG->cachetext = 0; // disable this very nasty setting
  171. // some ugly hacks
  172. $CFG->themerev = 1;
  173. $CFG->jsrev = 1;
  174. // load test case stub classes and other stuff
  175. require_once("$CFG->dirroot/lib/phpunit/lib.php");
  176. // finish moodle init
  177. define('ABORT_AFTER_CONFIG_CANCEL', true);
  178. require("$CFG->dirroot/lib/setup.php");
  179. raise_memory_limit(MEMORY_HUGE);
  180. if (PHPUNIT_UTIL) {
  181. // we are not going to do testing, this is 'true' in utility scripts that only init database
  182. return;
  183. }
  184. // is database and dataroot ready for testing?
  185. list($errorcode, $message) = phpunit_util::testing_ready_problem();
  186. if ($errorcode) {
  187. phpunit_bootstrap_error($errorcode, $message);
  188. }
  189. // prepare for the first test run - store fresh globals, reset database and dataroot, etc.
  190. phpunit_util::bootstrap_init();