[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Utils for behat-related stuff 19 * 20 * @package core 21 * @category test 22 * @copyright 2012 David Monllaó 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once (__DIR__ . '/../lib.php'); 29 require_once (__DIR__ . '/../../testing/classes/util.php'); 30 require_once (__DIR__ . '/behat_command.php'); 31 require_once (__DIR__ . '/behat_config_manager.php'); 32 33 require_once (__DIR__ . '/../../filelib.php'); 34 35 /** 36 * Init/reset utilities for Behat database and dataroot 37 * 38 * @package core 39 * @category test 40 * @copyright 2013 David Monllaó 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 42 */ 43 class behat_util extends testing_util { 44 45 /** 46 * The behat test site fullname and shortname. 47 */ 48 const BEHATSITENAME = "Acceptance test site"; 49 50 /** 51 * @var array Files to skip when resetting dataroot folder 52 */ 53 protected static $datarootskiponreset = array('.', '..', 'behat', 'behattestdir.txt'); 54 55 /** 56 * @var array Files to skip when dropping dataroot folder 57 */ 58 protected static $datarootskipondrop = array('.', '..', 'lock'); 59 60 /** 61 * Installs a site using $CFG->dataroot and $CFG->prefix 62 * @throws coding_exception 63 * @return void 64 */ 65 public static function install_site() { 66 global $DB, $CFG; 67 require_once($CFG->dirroot.'/user/lib.php'); 68 if (!defined('BEHAT_UTIL')) { 69 throw new coding_exception('This method can be only used by Behat CLI tool'); 70 } 71 72 $tables = $DB->get_tables(false); 73 if (!empty($tables)) { 74 behat_error(BEHAT_EXITCODE_INSTALLED); 75 } 76 77 // New dataroot. 78 self::reset_dataroot(); 79 80 $options = array(); 81 $options['adminuser'] = 'admin'; 82 $options['adminpass'] = 'admin'; 83 $options['fullname'] = self::BEHATSITENAME; 84 $options['shortname'] = self::BEHATSITENAME; 85 86 install_cli_database($options, false); 87 88 // We need to keep the installed dataroot filedir files. 89 // So each time we reset the dataroot before running a test, the default files are still installed. 90 self::save_original_data_files(); 91 92 $frontpagesummary = new admin_setting_special_frontpagedesc(); 93 $frontpagesummary->write_setting(self::BEHATSITENAME); 94 95 // Update admin user info. 96 $user = $DB->get_record('user', array('username' => 'admin')); 97 $user->email = '[email protected]'; 98 $user->firstname = 'Admin'; 99 $user->lastname = 'User'; 100 $user->city = 'Perth'; 101 $user->country = 'AU'; 102 user_update_user($user, false); 103 104 // Disable email message processor. 105 $DB->set_field('message_processors', 'enabled', '0', array('name' => 'email')); 106 107 // Sets maximum debug level. 108 set_config('debug', DEBUG_DEVELOPER); 109 set_config('debugdisplay', 1); 110 111 // Disable some settings that are not wanted on test sites. 112 set_config('noemailever', 1); 113 114 // Keeps the current version of database and dataroot. 115 self::store_versions_hash(); 116 117 // Stores the database contents for fast reset. 118 self::store_database_state(); 119 } 120 121 /** 122 * Drops dataroot and remove test database tables 123 * @throws coding_exception 124 * @return void 125 */ 126 public static function drop_site() { 127 128 if (!defined('BEHAT_UTIL')) { 129 throw new coding_exception('This method can be only used by Behat CLI tool'); 130 } 131 132 self::reset_dataroot(); 133 self::drop_dataroot(); 134 self::drop_database(true); 135 } 136 137 /** 138 * Checks if $CFG->behat_wwwroot is available 139 * 140 * @return bool 141 */ 142 public static function is_server_running() { 143 global $CFG; 144 145 $request = new curl(); 146 $request->get($CFG->behat_wwwroot); 147 148 if ($request->get_errno() === 0) { 149 return true; 150 } 151 return false; 152 } 153 154 /** 155 * Checks whether the test database and dataroot is ready 156 * Stops execution if something went wrong 157 * @throws coding_exception 158 * @return void 159 */ 160 protected static function test_environment_problem() { 161 global $CFG, $DB; 162 163 if (!defined('BEHAT_UTIL')) { 164 throw new coding_exception('This method can be only used by Behat CLI tool'); 165 } 166 167 if (!self::is_test_site()) { 168 behat_error(1, 'This is not a behat test site!'); 169 } 170 171 $tables = $DB->get_tables(false); 172 if (empty($tables)) { 173 behat_error(BEHAT_EXITCODE_INSTALL, ''); 174 } 175 176 if (!self::is_test_data_updated()) { 177 behat_error(BEHAT_EXITCODE_REINSTALL, 'The test environment was initialised for a different version'); 178 } 179 } 180 181 /** 182 * Enables test mode 183 * 184 * It uses CFG->behat_dataroot 185 * 186 * Starts the test mode checking the composer installation and 187 * the test environment and updating the available 188 * features and steps definitions. 189 * 190 * Stores a file in dataroot/behat to allow Moodle to switch 191 * to the test environment when using cli-server. 192 * @throws coding_exception 193 * @return void 194 */ 195 public static function start_test_mode() { 196 global $CFG; 197 198 if (!defined('BEHAT_UTIL')) { 199 throw new coding_exception('This method can be only used by Behat CLI tool'); 200 } 201 202 // Checks the behat set up and the PHP version. 203 if ($errorcode = behat_command::behat_setup_problem()) { 204 exit($errorcode); 205 } 206 207 // Check that test environment is correctly set up. 208 self::test_environment_problem(); 209 210 // Updates all the Moodle features and steps definitions. 211 behat_config_manager::update_config_file(); 212 213 if (self::is_test_mode_enabled()) { 214 return; 215 } 216 217 $contents = '$CFG->behat_wwwroot, $CFG->behat_prefix and $CFG->behat_dataroot' . 218 ' are currently used as $CFG->wwwroot, $CFG->prefix and $CFG->dataroot'; 219 $filepath = self::get_test_file_path(); 220 if (!file_put_contents($filepath, $contents)) { 221 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created'); 222 } 223 } 224 225 /** 226 * Returns the status of the behat test environment 227 * 228 * @return int Error code 229 */ 230 public static function get_behat_status() { 231 232 if (!defined('BEHAT_UTIL')) { 233 throw new coding_exception('This method can be only used by Behat CLI tool'); 234 } 235 236 // Checks the behat set up and the PHP version, returning an error code if something went wrong. 237 if ($errorcode = behat_command::behat_setup_problem()) { 238 return $errorcode; 239 } 240 241 // Check that test environment is correctly set up, stops execution. 242 self::test_environment_problem(); 243 } 244 245 /** 246 * Disables test mode 247 * @throws coding_exception 248 * @return void 249 */ 250 public static function stop_test_mode() { 251 252 if (!defined('BEHAT_UTIL')) { 253 throw new coding_exception('This method can be only used by Behat CLI tool'); 254 } 255 256 $testenvfile = self::get_test_file_path(); 257 258 if (!self::is_test_mode_enabled()) { 259 echo "Test environment was already disabled\n"; 260 } else { 261 if (!unlink($testenvfile)) { 262 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete test environment file'); 263 } 264 } 265 } 266 267 /** 268 * Checks whether test environment is enabled or disabled 269 * 270 * To check is the current script is running in the test 271 * environment 272 * 273 * @return bool 274 */ 275 public static function is_test_mode_enabled() { 276 277 $testenvfile = self::get_test_file_path(); 278 if (file_exists($testenvfile)) { 279 return true; 280 } 281 282 return false; 283 } 284 285 /** 286 * Returns the path to the file which specifies if test environment is enabled 287 * @return string 288 */ 289 protected final static function get_test_file_path() { 290 return behat_command::get_behat_dir() . '/test_environment_enabled.txt'; 291 } 292 293 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |