MDL-39846 introduce new objecttable property
[moodle.git] / lib / phpunit / classes / util.php
CommitLineData
7e7cfe7a
PS
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 * Utility class.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
0ea35584 26require_once(__DIR__.'/../../testing/classes/util.php');
7e7cfe7a
PS
27
28/**
29 * Collection of utility methods.
30 *
31 * @package core
32 * @category phpunit
33 * @copyright 2012 Petr Skoda {@link http://skodak.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
0ea35584 36class phpunit_util extends testing_util {
7e7cfe7a
PS
37 /** @var array An array of original globals, restored after each test */
38 protected static $globals = array();
39
ef5b5e05
PS
40 /** @var array list of debugging messages triggered during the last test execution */
41 protected static $debuggings = array();
42
4c9e03f0
PS
43 /** @var phpunit_message_sink alternative target for moodle messaging */
44 protected static $messagesink = null;
45
7e7cfe7a 46 /**
0ea35584 47 * @var array Files to skip when resetting dataroot folder
7e7cfe7a 48 */
0ea35584 49 protected static $datarootskiponreset = array('.', '..', 'phpunittestdir.txt', 'phpunit', '.htaccess');
7e7cfe7a
PS
50
51 /**
0ea35584 52 * @var array Files to skip when dropping dataroot folder
7e7cfe7a 53 */
0ea35584 54 protected static $datarootskipondrop = array('.', '..', 'lock', 'webrunner.xml');
7e7cfe7a
PS
55
56 /**
57 * Load global $CFG;
58 * @internal
59 * @static
60 * @return void
61 */
62 public static function initialise_cfg() {
63 global $DB;
64 $dbhash = false;
65 try {
66 $dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'));
67 } catch (Exception $e) {
68 // not installed yet
69 initialise_cfg();
70 return;
71 }
0ea35584 72 if ($dbhash !== self::get_version_hash()) {
7e7cfe7a
PS
73 // do not set CFG - the only way forward is to drop and reinstall
74 return;
75 }
76 // standard CFG init
77 initialise_cfg();
78 }
79
7e7cfe7a
PS
80 /**
81 * Reset contents of all database tables to initial values, reset caches, etc.
82 *
83 * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
84 *
85 * @static
71fc5003
PS
86 * @param bool $detectchanges
87 * true - changes in global state and database are reported as errors
88 * false - no errors reported
89 * null - only critical problems are reported as errors
7e7cfe7a
PS
90 * @return void
91 */
71fc5003 92 public static function reset_all_data($detectchanges = false) {
e17dbeeb 93 global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
7e7cfe7a 94
4c9e03f0
PS
95 // Stop any message redirection.
96 phpunit_util::stop_message_redirection();
97
d03d5113
PS
98 // Release memory and indirectly call destroy() methods to release resource handles, etc.
99 gc_collect_cycles();
100
ef5b5e05
PS
101 // Show any unhandled debugging messages, the runbare() could already reset it.
102 self::display_debugging_messages();
103 self::reset_debugging();
104
7e7cfe7a
PS
105 // reset global $DB in case somebody mocked it
106 $DB = self::get_global_backup('DB');
107
108 if ($DB->is_transaction_started()) {
109 // we can not reset inside transaction
110 $DB->force_transaction_rollback();
111 }
112
113 $resetdb = self::reset_database();
114 $warnings = array();
115
71fc5003 116 if ($detectchanges === true) {
7e7cfe7a
PS
117 if ($resetdb) {
118 $warnings[] = 'Warning: unexpected database modification, resetting DB state';
119 }
120
121 $oldcfg = self::get_global_backup('CFG');
122 $oldsite = self::get_global_backup('SITE');
123 foreach($CFG as $k=>$v) {
124 if (!property_exists($oldcfg, $k)) {
125 $warnings[] = 'Warning: unexpected new $CFG->'.$k.' value';
126 } else if ($oldcfg->$k !== $CFG->$k) {
127 $warnings[] = 'Warning: unexpected change of $CFG->'.$k.' value';
128 }
129 unset($oldcfg->$k);
130
131 }
132 if ($oldcfg) {
133 foreach($oldcfg as $k=>$v) {
134 $warnings[] = 'Warning: unexpected removal of $CFG->'.$k;
135 }
136 }
137
138 if ($USER->id != 0) {
139 $warnings[] = 'Warning: unexpected change of $USER';
140 }
141
142 if ($COURSE->id != $oldsite->id) {
143 $warnings[] = 'Warning: unexpected change of $COURSE';
144 }
82081c1f 145
70faad65
PS
146 }
147
148 if (ini_get('max_execution_time') != 0) {
149 // This is special warning for all resets because we do not want any
150 // libraries to mess with timeouts unintentionally.
151 // Our PHPUnit integration is not supposed to change it either.
152
71fc5003
PS
153 if ($detectchanges !== false) {
154 $warnings[] = 'Warning: max_execution_time was changed to '.ini_get('max_execution_time');
155 }
70faad65 156 set_time_limit(0);
7e7cfe7a
PS
157 }
158
159 // restore original globals
160 $_SERVER = self::get_global_backup('_SERVER');
161 $CFG = self::get_global_backup('CFG');
162 $SITE = self::get_global_backup('SITE');
596ea56f
JP
163 $_GET = array();
164 $_POST = array();
165 $_FILES = array();
166 $_REQUEST = array();
7e7cfe7a
PS
167 $COURSE = $SITE;
168
169 // reinitialise following globals
170 $OUTPUT = new bootstrap_renderer();
171 $PAGE = new moodle_page();
172 $FULLME = null;
173 $ME = null;
174 $SCRIPT = null;
175 $SESSION = new stdClass();
176 $_SESSION['SESSION'] =& $SESSION;
177
178 // set fresh new not-logged-in user
179 $user = new stdClass();
180 $user->id = 0;
181 $user->mnethostid = $CFG->mnet_localhost_id;
182 session_set_user($user);
183
184 // reset all static caches
d8a1f426 185 \core\event\manager::phpunit_reset();
7e7cfe7a 186 accesslib_clear_all_caches(true);
a46e11b5
PS
187 get_string_manager()->reset_caches(true);
188 reset_text_filters_cache(true);
7e7cfe7a
PS
189 events_get_handlers('reset');
190 textlib::reset_caches();
6fd1cf05
MG
191 if (class_exists('repository')) {
192 repository::reset_caches();
193 }
73a0f3ba 194 filter_manager::reset_caches();
6fd1cf05 195 //TODO MDL-25290: add more resets here and probably refactor them to new core function
7e7cfe7a 196
2beba297 197 // Reset course and module caches.
13d5c938 198 if (class_exists('format_base')) {
2beba297 199 // If file containing class is not loaded, there is no cache there anyway.
13d5c938
PS
200 format_base::reset_course_cache(0);
201 }
b46be6ad 202 get_fast_modinfo(0, 0, true);
13d5c938 203
98547432
204 // Reset other singletons.
205 if (class_exists('plugin_manager')) {
206 plugin_manager::reset_caches(true);
207 }
208 if (class_exists('available_update_checker')) {
209 available_update_checker::reset_caches(true);
210 }
dc11af19
DM
211 if (class_exists('available_update_deployer')) {
212 available_update_deployer::reset_caches(true);
213 }
98547432 214
7e7cfe7a
PS
215 // purge dataroot directory
216 self::reset_dataroot();
217
218 // restore original config once more in case resetting of caches changed CFG
219 $CFG = self::get_global_backup('CFG');
220
221 // inform data generator
222 self::get_data_generator()->reset();
223
224 // fix PHP settings
225 error_reporting($CFG->debug);
226
227 // verify db writes just in case something goes wrong in reset
228 if (self::$lastdbwrites != $DB->perf_get_writes()) {
229 error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
230 self::$lastdbwrites = $DB->perf_get_writes();
231 }
232
233 if ($warnings) {
234 $warnings = implode("\n", $warnings);
235 trigger_error($warnings, E_USER_WARNING);
236 }
237 }
238
239 /**
240 * Called during bootstrap only!
241 * @internal
242 * @static
243 * @return void
244 */
245 public static function bootstrap_init() {
246 global $CFG, $SITE, $DB;
247
248 // backup the globals
249 self::$globals['_SERVER'] = $_SERVER;
250 self::$globals['CFG'] = clone($CFG);
251 self::$globals['SITE'] = clone($SITE);
252 self::$globals['DB'] = $DB;
253
254 // refresh data in all tables, clear caches, etc.
255 phpunit_util::reset_all_data();
256 }
257
15bac12e
PS
258 /**
259 * Print some Moodle related info to console.
260 * @internal
261 * @static
262 * @return void
263 */
264 public static function bootstrap_moodle_info() {
265 global $CFG;
266
267 // All developers have to understand English, do not localise!
268
269 $release = null;
270 require("$CFG->dirroot/version.php");
271
272 echo "Moodle $release, $CFG->dbtype";
273 if ($hash = self::get_git_hash()) {
274 echo ", $hash";
275 }
276 echo "\n";
277 }
278
279 /**
280 * Try to get current git hash of the Moodle in $CFG->dirroot.
281 * @return string null if unknown, sha1 hash if known
282 */
283 public static function get_git_hash() {
284 global $CFG;
285
286 // This is a bit naive, but it should mostly work for all platforms.
287
288 if (!file_exists("$CFG->dirroot/.git/HEAD")) {
289 return null;
290 }
291
292 $ref = file_get_contents("$CFG->dirroot/.git/HEAD");
293 if ($ref === false) {
294 return null;
295 }
296
297 $ref = trim($ref);
298
299 if (strpos($ref, 'ref: ') !== 0) {
300 return null;
301 }
302
303 $ref = substr($ref, 5);
304
305 if (!file_exists("$CFG->dirroot/.git/$ref")) {
306 return null;
307 }
308
309 $hash = file_get_contents("$CFG->dirroot/.git/$ref");
310
311 if ($hash === false) {
312 return null;
313 }
314
315 $hash = trim($hash);
316
317 if (strlen($hash) != 40) {
318 return null;
319 }
320
321 return $hash;
322 }
323
7e7cfe7a
PS
324 /**
325 * Returns original state of global variable.
326 * @static
327 * @param string $name
328 * @return mixed
329 */
330 public static function get_global_backup($name) {
331 if ($name === 'DB') {
332 // no cloning of database object,
333 // we just need the original reference, not original state
334 return self::$globals['DB'];
335 }
336 if (isset(self::$globals[$name])) {
337 if (is_object(self::$globals[$name])) {
338 $return = clone(self::$globals[$name]);
339 return $return;
340 } else {
341 return self::$globals[$name];
342 }
343 }
344 return null;
345 }
346
7e7cfe7a
PS
347 /**
348 * Is this site initialised to run unit tests?
349 *
350 * @static
351 * @return int array errorcode=>message, 0 means ok
352 */
353 public static function testing_ready_problem() {
0ea35584 354 global $DB;
7e7cfe7a
PS
355
356 if (!self::is_test_site()) {
357 // dataroot was verified in bootstrap, so it must be DB
358 return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
359 }
360
0ea35584 361 $tables = $DB->get_tables(false);
7e7cfe7a
PS
362 if (empty($tables)) {
363 return array(PHPUNIT_EXITCODE_INSTALL, '');
364 }
365
0ea35584 366 if (!self::is_test_data_updated()) {
7e7cfe7a
PS
367 return array(PHPUNIT_EXITCODE_REINSTALL, '');
368 }
369
370 return array(0, '');
371 }
372
373 /**
374 * Drop all test site data.
375 *
376 * Note: To be used from CLI scripts only.
377 *
378 * @static
85b72a75 379 * @param bool $displayprogress if true, this method will echo progress information.
7e7cfe7a
PS
380 * @return void may terminate execution with exit code
381 */
85b72a75 382 public static function drop_site($displayprogress = false) {
7e7cfe7a
PS
383 global $DB, $CFG;
384
385 if (!self::is_test_site()) {
386 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
387 }
388
85b72a75
TH
389 // Purge dataroot
390 if ($displayprogress) {
391 echo "Purging dataroot:\n";
392 }
0ea35584 393
7e7cfe7a 394 self::reset_dataroot();
0ea35584
DM
395 testing_initdataroot($CFG->dataroot, 'phpunit');
396 self::drop_dataroot();
7e7cfe7a
PS
397
398 // drop all tables
0ea35584 399 self::drop_database($displayprogress);
7e7cfe7a
PS
400 }
401
402 /**
403 * Perform a fresh test site installation
404 *
405 * Note: To be used from CLI scripts only.
406 *
407 * @static
408 * @return void may terminate execution with exit code
409 */
410 public static function install_site() {
411 global $DB, $CFG;
412
413 if (!self::is_test_site()) {
414 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
415 }
416
417 if ($DB->get_tables()) {
418 list($errorcode, $message) = phpunit_util::testing_ready_problem();
419 if ($errorcode) {
420 phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
421 } else {
422 phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
423 }
424 }
425
426 $options = array();
427 $options['adminpass'] = 'admin';
428 $options['shortname'] = 'phpunit';
429 $options['fullname'] = 'PHPUnit test site';
430
431 install_cli_database($options, false);
432
433 // install timezone info
434 $timezones = get_records_csv($CFG->libdir.'/timezone.txt', 'timezone');
435 update_timezone_records($timezones);
436
0ea35584
DM
437 // Store version hash in the database and in a file.
438 self::store_versions_hash();
7e7cfe7a 439
0ea35584
DM
440 // Store database data and structure.
441 self::store_database_state();
7e7cfe7a
PS
442 }
443
444 /**
445 * Builds dirroot/phpunit.xml and dataroot/phpunit/webrunner.xml files using defaults from /phpunit.xml.dist
446 * @static
447 * @return bool true means main config file created, false means only dataroot file created
448 */
449 public static function build_config_file() {
450 global $CFG;
451
452 $template = '
564fcb3b 453 <testsuite name="@component@ test suite">
7e7cfe7a
PS
454 <directory suffix="_test.php">@dir@</directory>
455 </testsuite>';
456 $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
457
458 $suites = '';
459
46f6f7f2 460 $plugintypes = core_component::get_plugin_types();
7e7cfe7a
PS
461 ksort($plugintypes);
462 foreach ($plugintypes as $type=>$unused) {
bd3b3bba 463 $plugs = core_component::get_plugin_list($type);
7e7cfe7a
PS
464 ksort($plugs);
465 foreach ($plugs as $plug=>$fullplug) {
466 if (!file_exists("$fullplug/tests/")) {
467 continue;
468 }
469 $dir = substr($fullplug, strlen($CFG->dirroot)+1);
470 $dir .= '/tests';
471 $component = $type.'_'.$plug;
472
473 $suite = str_replace('@component@', $component, $template);
474 $suite = str_replace('@dir@', $dir, $suite);
475
476 $suites .= $suite;
477 }
478 }
479
480 $data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
481
482 $result = false;
483 if (is_writable($CFG->dirroot)) {
484 if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
0ea35584 485 testing_fix_file_permissions("$CFG->dirroot/phpunit.xml");
7e7cfe7a
PS
486 }
487 }
488
489 // relink - it seems that xml:base does not work in phpunit xml files, remove this nasty hack if you find a way to set xml base for relative refs
490 $data = str_replace('lib/phpunit/', $CFG->dirroot.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'phpunit'.DIRECTORY_SEPARATOR, $data);
491 $data = preg_replace('|<directory suffix="_test.php">([^<]+)</directory>|',
492 '<directory suffix="_test.php">'.$CFG->dirroot.(DIRECTORY_SEPARATOR === '\\' ? '\\\\' : DIRECTORY_SEPARATOR).'$1</directory>',
493 $data);
494 file_put_contents("$CFG->dataroot/phpunit/webrunner.xml", $data);
0ea35584 495 testing_fix_file_permissions("$CFG->dataroot/phpunit/webrunner.xml");
7e7cfe7a
PS
496
497 return (bool)$result;
498 }
499
500 /**
501 * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
502 *
503 * @static
504 * @return void, stops if can not write files
505 */
506 public static function build_component_config_files() {
507 global $CFG;
508
509 $template = '
510 <testsuites>
511 <testsuite name="@component@">
512 <directory suffix="_test.php">.</directory>
513 </testsuite>
514 </testsuites>';
515
516 // Use the upstream file as source for the distributed configurations
517 $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
518 $ftemplate = preg_replace('|<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
519
f8228402
DM
520 // Gets all the components with tests
521 $components = tests_finder::get_components_with_tests('phpunit');
7e7cfe7a
PS
522
523 // Create the corresponding phpunit.xml file for each component
524 foreach ($components as $cname => $cpath) {
525 // Calculate the component suite
526 $ctemplate = $template;
527 $ctemplate = str_replace('@component@', $cname, $ctemplate);
528
529 // Apply it to the file template
530 $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
531
532 // fix link to schema
533 $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/');
ed7259d1 534 $fcontents = str_replace('lib/phpunit/', str_repeat('../', $level).'lib/phpunit/', $fcontents);
7e7cfe7a
PS
535
536 // Write the file
537 $result = false;
538 if (is_writable($cpath)) {
539 if ($result = (bool)file_put_contents("$cpath/phpunit.xml", $fcontents)) {
0ea35584 540 testing_fix_file_permissions("$cpath/phpunit.xml");
7e7cfe7a
PS
541 }
542 }
543 // Problems writing file, throw error
544 if (!$result) {
545 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, "Can not create $cpath/phpunit.xml configuration file, verify dir permissions");
546 }
547 }
548 }
549
ef5b5e05
PS
550 /**
551 * To be called from debugging() only.
552 * @param string $message
553 * @param int $level
554 * @param string $from
555 */
556 public static function debugging_triggered($message, $level, $from) {
557 // Store only if debugging triggered from actual test,
558 // we need normal debugging outside of tests to find problems in our phpunit integration.
559 $backtrace = debug_backtrace();
560
561 foreach ($backtrace as $bt) {
562 $intest = false;
563 if (isset($bt['object']) and is_object($bt['object'])) {
564 if ($bt['object'] instanceof PHPUnit_Framework_TestCase) {
565 if (strpos($bt['function'], 'test') === 0) {
566 $intest = true;
567 break;
568 }
569 }
570 }
571 }
572 if (!$intest) {
573 return false;
574 }
575
576 $debug = new stdClass();
577 $debug->message = $message;
578 $debug->level = $level;
579 $debug->from = $from;
580
581 self::$debuggings[] = $debug;
582
583 return true;
584 }
585
586 /**
587 * Resets the list of debugging messages.
588 */
589 public static function reset_debugging() {
590 self::$debuggings = array();
591 }
592
593 /**
594 * Returns all debugging messages triggered during test.
595 * @return array with instances having message, level and stacktrace property.
596 */
597 public static function get_debugging_messages() {
598 return self::$debuggings;
599 }
600
601 /**
602 * Prints out any debug messages accumulated during test execution.
603 * @return bool false if no debug messages, true if debug triggered
604 */
605 public static function display_debugging_messages() {
606 if (empty(self::$debuggings)) {
607 return false;
608 }
609 foreach(self::$debuggings as $debug) {
610 echo 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
611 }
612
613 return true;
614 }
4c9e03f0
PS
615
616 /**
617 * Start message redirection.
618 *
619 * Note: Do not call directly from tests,
620 * use $sink = $this->redirectMessages() instead.
621 *
622 * @return phpunit_message_sink
623 */
624 public static function start_message_redirection() {
625 if (self::$messagesink) {
626 self::stop_message_redirection();
627 }
628 self::$messagesink = new phpunit_message_sink();
629 return self::$messagesink;
630 }
631
632 /**
633 * End message redirection.
634 *
635 * Note: Do not call directly from tests,
636 * use $sink->close() instead.
637 */
638 public static function stop_message_redirection() {
639 self::$messagesink = null;
640 }
641
642 /**
643 * Are messages redirected to some sink?
644 *
645 * Note: to be called from messagelib.php only!
646 *
647 * @return bool
648 */
649 public static function is_redirecting_messages() {
650 return !empty(self::$messagesink);
651 }
652
653 /**
654 * To be called from messagelib.php only!
655 *
656 * @param stdClass $message record from message_read table
657 * @return bool true means send message, false means message "sent" to sink.
658 */
659 public static function message_sent($message) {
660 if (self::$messagesink) {
661 self::$messagesink->add_message($message);
662 }
663 }
7e7cfe7a 664}