MDL-55188 events: First deprecation of eventslib.php
[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 {
a9236a2c
PS
37 /**
38 * @var int last value of db writes counter, used for db resetting
39 */
40 public static $lastdbwrites = null;
41
7e7cfe7a
PS
42 /** @var array An array of original globals, restored after each test */
43 protected static $globals = array();
44
ef5b5e05
PS
45 /** @var array list of debugging messages triggered during the last test execution */
46 protected static $debuggings = array();
47
4c9e03f0
PS
48 /** @var phpunit_message_sink alternative target for moodle messaging */
49 protected static $messagesink = null;
50
1aba6b2b
AN
51 /** @var phpunit_phpmailer_sink alternative target for phpmailer messaging */
52 protected static $phpmailersink = null;
53
62401e8f
PS
54 /** @var phpunit_message_sink alternative target for moodle messaging */
55 protected static $eventsink = null;
56
7e7cfe7a 57 /**
0ea35584 58 * @var array Files to skip when resetting dataroot folder
7e7cfe7a 59 */
0ea35584 60 protected static $datarootskiponreset = array('.', '..', 'phpunittestdir.txt', 'phpunit', '.htaccess');
7e7cfe7a
PS
61
62 /**
0ea35584 63 * @var array Files to skip when dropping dataroot folder
7e7cfe7a 64 */
0ea35584 65 protected static $datarootskipondrop = array('.', '..', 'lock', 'webrunner.xml');
7e7cfe7a
PS
66
67 /**
68 * Load global $CFG;
69 * @internal
70 * @static
71 * @return void
72 */
73 public static function initialise_cfg() {
74 global $DB;
75 $dbhash = false;
76 try {
77 $dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'));
78 } catch (Exception $e) {
79 // not installed yet
80 initialise_cfg();
81 return;
82 }
c5701ce7 83 if ($dbhash !== core_component::get_all_versions_hash()) {
7e7cfe7a
PS
84 // do not set CFG - the only way forward is to drop and reinstall
85 return;
86 }
87 // standard CFG init
88 initialise_cfg();
89 }
90
7e7cfe7a
PS
91 /**
92 * Reset contents of all database tables to initial values, reset caches, etc.
93 *
94 * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
95 *
96 * @static
71fc5003
PS
97 * @param bool $detectchanges
98 * true - changes in global state and database are reported as errors
99 * false - no errors reported
100 * null - only critical problems are reported as errors
7e7cfe7a
PS
101 * @return void
102 */
71fc5003 103 public static function reset_all_data($detectchanges = false) {
6bd871d3 104 global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $FULLME;
7e7cfe7a 105
4c9e03f0 106 // Stop any message redirection.
5ecf8e8f 107 self::stop_message_redirection();
4c9e03f0 108
62401e8f 109 // Stop any message redirection.
5ecf8e8f 110 self::stop_event_redirection();
62401e8f 111
556e3a9e
AN
112 // Start a new email redirection.
113 // This will clear any existing phpmailer redirection.
114 // We redirect all phpmailer output to this message sink which is
115 // called instead of phpmailer actually sending the message.
5ecf8e8f 116 self::start_phpmailer_redirection();
556e3a9e 117
84f08cfa
DW
118 // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
119 // This accounted for 25% of the total time running phpunit - so we removed it.
120
ef5b5e05
PS
121 // Show any unhandled debugging messages, the runbare() could already reset it.
122 self::display_debugging_messages();
123 self::reset_debugging();
124
7e7cfe7a
PS
125 // reset global $DB in case somebody mocked it
126 $DB = self::get_global_backup('DB');
127
128 if ($DB->is_transaction_started()) {
129 // we can not reset inside transaction
130 $DB->force_transaction_rollback();
131 }
132
133 $resetdb = self::reset_database();
96805290 134 $localename = self::get_locale_name();
7e7cfe7a
PS
135 $warnings = array();
136
71fc5003 137 if ($detectchanges === true) {
7e7cfe7a
PS
138 if ($resetdb) {
139 $warnings[] = 'Warning: unexpected database modification, resetting DB state';
140 }
141
142 $oldcfg = self::get_global_backup('CFG');
143 $oldsite = self::get_global_backup('SITE');
144 foreach($CFG as $k=>$v) {
145 if (!property_exists($oldcfg, $k)) {
146 $warnings[] = 'Warning: unexpected new $CFG->'.$k.' value';
147 } else if ($oldcfg->$k !== $CFG->$k) {
148 $warnings[] = 'Warning: unexpected change of $CFG->'.$k.' value';
149 }
150 unset($oldcfg->$k);
151
152 }
153 if ($oldcfg) {
154 foreach($oldcfg as $k=>$v) {
155 $warnings[] = 'Warning: unexpected removal of $CFG->'.$k;
156 }
157 }
158
159 if ($USER->id != 0) {
160 $warnings[] = 'Warning: unexpected change of $USER';
161 }
162
163 if ($COURSE->id != $oldsite->id) {
164 $warnings[] = 'Warning: unexpected change of $COURSE';
165 }
82081c1f 166
6bd871d3 167 if ($FULLME !== self::get_global_backup('FULLME')) {
168 $warnings[] = 'Warning: unexpected change of $FULLME';
169 }
170
96805290
DP
171 if (setlocale(LC_TIME, 0) !== $localename) {
172 $warnings[] = 'Warning: unexpected change of locale';
d6e7a63d 173 }
70faad65
PS
174 }
175
176 if (ini_get('max_execution_time') != 0) {
177 // This is special warning for all resets because we do not want any
178 // libraries to mess with timeouts unintentionally.
179 // Our PHPUnit integration is not supposed to change it either.
180
71fc5003
PS
181 if ($detectchanges !== false) {
182 $warnings[] = 'Warning: max_execution_time was changed to '.ini_get('max_execution_time');
183 }
70faad65 184 set_time_limit(0);
7e7cfe7a
PS
185 }
186
187 // restore original globals
188 $_SERVER = self::get_global_backup('_SERVER');
189 $CFG = self::get_global_backup('CFG');
190 $SITE = self::get_global_backup('SITE');
6bd871d3 191 $FULLME = self::get_global_backup('FULLME');
596ea56f
JP
192 $_GET = array();
193 $_POST = array();
194 $_FILES = array();
195 $_REQUEST = array();
7e7cfe7a
PS
196 $COURSE = $SITE;
197
198 // reinitialise following globals
199 $OUTPUT = new bootstrap_renderer();
200 $PAGE = new moodle_page();
201 $FULLME = null;
202 $ME = null;
203 $SCRIPT = null;
2e00d01d
PS
204
205 // Empty sessison and set fresh new not-logged-in user.
206 \core\session\manager::init_empty_session();
7e7cfe7a
PS
207
208 // reset all static caches
d8a1f426 209 \core\event\manager::phpunit_reset();
7e7cfe7a 210 accesslib_clear_all_caches(true);
a46e11b5
PS
211 get_string_manager()->reset_caches(true);
212 reset_text_filters_cache(true);
2f1e464a 213 core_text::reset_caches();
e61a9638 214 get_message_processors(false, true, true);
73a0f3ba 215 filter_manager::reset_caches();
91fed57a 216 core_filetypes::reset_caches();
db48207e 217 \core_search\manager::clear_static();
dccf9ca3 218 core_user::reset_caches();
a9ca3fa1 219 \core\output\icon_system::reset_caches();
fab11235
MG
220 if (class_exists('core_media_manager', false)) {
221 core_media_manager::reset_caches();
222 }
91fed57a 223
89a73b08 224 // Reset static unit test options.
225 if (class_exists('\availability_date\condition', false)) {
226 \availability_date\condition::set_current_time_for_test(0);
227 }
228
6b8e46ad
RT
229 // Reset internal users.
230 core_user::reset_internal_users();
231
63e9e382
MG
232 // Clear static caches in calendar container.
233 if (class_exists('\core_calendar\local\event\container', false)) {
234 core_calendar\local\event\container::reset_caches();
235 }
236
6fd1cf05 237 //TODO MDL-25290: add more resets here and probably refactor them to new core function
7e7cfe7a 238
2beba297 239 // Reset course and module caches.
13d5c938 240 if (class_exists('format_base')) {
2beba297 241 // If file containing class is not loaded, there is no cache there anyway.
13d5c938
PS
242 format_base::reset_course_cache(0);
243 }
b46be6ad 244 get_fast_modinfo(0, 0, true);
13d5c938 245
98547432 246 // Reset other singletons.
e87214bd
PS
247 if (class_exists('core_plugin_manager')) {
248 core_plugin_manager::reset_caches(true);
98547432 249 }
e87214bd
PS
250 if (class_exists('\core\update\checker')) {
251 \core\update\checker::reset_caches(true);
98547432
252 }
253
27479b5c 254 // Clear static cache within restore.
255 if (class_exists('restore_section_structure_step')) {
256 restore_section_structure_step::reset_caches();
257 }
258
7e7cfe7a
PS
259 // purge dataroot directory
260 self::reset_dataroot();
261
262 // restore original config once more in case resetting of caches changed CFG
263 $CFG = self::get_global_backup('CFG');
264
265 // inform data generator
266 self::get_data_generator()->reset();
267
268 // fix PHP settings
269 error_reporting($CFG->debug);
270
d6e7a63d
PS
271 // Reset the date/time class.
272 core_date::phpunit_reset();
273
274 // Make sure the time locale is consistent - that is Australian English.
96805290 275 setlocale(LC_TIME, $localename);
d6e7a63d 276
972a367b
TH
277 // Reset the log manager cache.
278 get_log_manager(true);
279
5a97f26b
PK
280 // Reset user agent.
281 core_useragent::instance(true, null);
282
7e7cfe7a
PS
283 // verify db writes just in case something goes wrong in reset
284 if (self::$lastdbwrites != $DB->perf_get_writes()) {
285 error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
286 self::$lastdbwrites = $DB->perf_get_writes();
287 }
288
289 if ($warnings) {
290 $warnings = implode("\n", $warnings);
291 trigger_error($warnings, E_USER_WARNING);
292 }
293 }
294
a9236a2c
PS
295 /**
296 * Reset all database tables to default values.
297 * @static
298 * @return bool true if reset done, false if skipped
299 */
300 public static function reset_database() {
301 global $DB;
302
303 if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) {
304 return false;
305 }
306
307 if (!parent::reset_database()) {
308 return false;
309 }
310
311 self::$lastdbwrites = $DB->perf_get_writes();
312
313 return true;
314 }
315
7e7cfe7a
PS
316 /**
317 * Called during bootstrap only!
318 * @internal
319 * @static
320 * @return void
321 */
322 public static function bootstrap_init() {
6bd871d3 323 global $CFG, $SITE, $DB, $FULLME;
7e7cfe7a
PS
324
325 // backup the globals
326 self::$globals['_SERVER'] = $_SERVER;
327 self::$globals['CFG'] = clone($CFG);
328 self::$globals['SITE'] = clone($SITE);
329 self::$globals['DB'] = $DB;
6bd871d3 330 self::$globals['FULLME'] = $FULLME;
7e7cfe7a
PS
331
332 // refresh data in all tables, clear caches, etc.
5ecf8e8f 333 self::reset_all_data();
7e7cfe7a
PS
334 }
335
15bac12e
PS
336 /**
337 * Print some Moodle related info to console.
338 * @internal
339 * @static
340 * @return void
341 */
342 public static function bootstrap_moodle_info() {
7514c2f2 343 echo self::get_site_info();
15bac12e
PS
344 }
345
7e7cfe7a
PS
346 /**
347 * Returns original state of global variable.
348 * @static
349 * @param string $name
350 * @return mixed
351 */
352 public static function get_global_backup($name) {
353 if ($name === 'DB') {
354 // no cloning of database object,
355 // we just need the original reference, not original state
356 return self::$globals['DB'];
357 }
358 if (isset(self::$globals[$name])) {
359 if (is_object(self::$globals[$name])) {
360 $return = clone(self::$globals[$name]);
361 return $return;
362 } else {
363 return self::$globals[$name];
364 }
365 }
366 return null;
367 }
368
7e7cfe7a
PS
369 /**
370 * Is this site initialised to run unit tests?
371 *
372 * @static
373 * @return int array errorcode=>message, 0 means ok
374 */
375 public static function testing_ready_problem() {
0ea35584 376 global $DB;
7e7cfe7a 377
96805290
DP
378 $localename = self::get_locale_name();
379 if (setlocale(LC_TIME, $localename) === false) {
380 return array(PHPUNIT_EXITCODE_CONFIGERROR, "Required locale '$localename' is not installed.");
381 }
382
7e7cfe7a
PS
383 if (!self::is_test_site()) {
384 // dataroot was verified in bootstrap, so it must be DB
385 return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
386 }
387
0ea35584 388 $tables = $DB->get_tables(false);
7e7cfe7a
PS
389 if (empty($tables)) {
390 return array(PHPUNIT_EXITCODE_INSTALL, '');
391 }
392
0ea35584 393 if (!self::is_test_data_updated()) {
7e7cfe7a
PS
394 return array(PHPUNIT_EXITCODE_REINSTALL, '');
395 }
396
397 return array(0, '');
398 }
399
400 /**
401 * Drop all test site data.
402 *
403 * Note: To be used from CLI scripts only.
404 *
405 * @static
85b72a75 406 * @param bool $displayprogress if true, this method will echo progress information.
7e7cfe7a
PS
407 * @return void may terminate execution with exit code
408 */
85b72a75 409 public static function drop_site($displayprogress = false) {
7e7cfe7a
PS
410 global $DB, $CFG;
411
412 if (!self::is_test_site()) {
413 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
414 }
415
85b72a75
TH
416 // Purge dataroot
417 if ($displayprogress) {
418 echo "Purging dataroot:\n";
419 }
0ea35584 420
7e7cfe7a 421 self::reset_dataroot();
0ea35584 422 testing_initdataroot($CFG->dataroot, 'phpunit');
7e7cfe7a 423
36cc9f8e 424 // Drop all tables.
0ea35584 425 self::drop_database($displayprogress);
36cc9f8e
RT
426
427 // Drop dataroot.
428 self::drop_dataroot();
7e7cfe7a
PS
429 }
430
431 /**
432 * Perform a fresh test site installation
433 *
434 * Note: To be used from CLI scripts only.
435 *
436 * @static
437 * @return void may terminate execution with exit code
438 */
439 public static function install_site() {
440 global $DB, $CFG;
441
442 if (!self::is_test_site()) {
443 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
444 }
445
446 if ($DB->get_tables()) {
5ecf8e8f 447 list($errorcode, $message) = self::testing_ready_problem();
7e7cfe7a
PS
448 if ($errorcode) {
449 phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
450 } else {
451 phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
452 }
453 }
454
455 $options = array();
456 $options['adminpass'] = 'admin';
457 $options['shortname'] = 'phpunit';
458 $options['fullname'] = 'PHPUnit test site';
459
460 install_cli_database($options, false);
461
14ecbb2e
AN
462 // Set the admin email address.
463 $DB->set_field('user', 'email', 'admin@example.com', array('username' => 'admin'));
464
770eac98
465 // Disable all logging for performance and sanity reasons.
466 set_config('enabled_stores', '', 'tool_log');
467
c78f19d1
JM
468 // We need to keep the installed dataroot filedir files.
469 // So each time we reset the dataroot before running a test, the default files are still installed.
470 self::save_original_data_files();
471
0ea35584
DM
472 // Store version hash in the database and in a file.
473 self::store_versions_hash();
7e7cfe7a 474
0ea35584
DM
475 // Store database data and structure.
476 self::store_database_state();
7e7cfe7a
PS
477 }
478
479 /**
480 * Builds dirroot/phpunit.xml and dataroot/phpunit/webrunner.xml files using defaults from /phpunit.xml.dist
481 * @static
482 * @return bool true means main config file created, false means only dataroot file created
483 */
484 public static function build_config_file() {
485 global $CFG;
486
487 $template = '
a9edd8a6 488 <testsuite name="@component@_testsuite">
7e7cfe7a
PS
489 <directory suffix="_test.php">@dir@</directory>
490 </testsuite>';
491 $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
492
493 $suites = '';
494
46f6f7f2 495 $plugintypes = core_component::get_plugin_types();
7e7cfe7a
PS
496 ksort($plugintypes);
497 foreach ($plugintypes as $type=>$unused) {
bd3b3bba 498 $plugs = core_component::get_plugin_list($type);
7e7cfe7a
PS
499 ksort($plugs);
500 foreach ($plugs as $plug=>$fullplug) {
501 if (!file_exists("$fullplug/tests/")) {
502 continue;
503 }
504 $dir = substr($fullplug, strlen($CFG->dirroot)+1);
505 $dir .= '/tests';
506 $component = $type.'_'.$plug;
507
508 $suite = str_replace('@component@', $component, $template);
509 $suite = str_replace('@dir@', $dir, $suite);
510
511 $suites .= $suite;
512 }
513 }
529495f7
RS
514 // Start a sequence between 100000 and 199000 to ensure each call to init produces
515 // different ids in the database. This reduces the risk that hard coded values will
516 // end up being placed in phpunit or behat test code.
517 $sequencestart = 100000 + mt_rand(0, 99) * 1000;
7e7cfe7a
PS
518
519 $data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
ddffa9d6
PS
520 $data = str_replace(
521 '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
522 '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
523 $data);
7e7cfe7a
PS
524
525 $result = false;
526 if (is_writable($CFG->dirroot)) {
527 if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
0ea35584 528 testing_fix_file_permissions("$CFG->dirroot/phpunit.xml");
7e7cfe7a
PS
529 }
530 }
531
532 // 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
533 $data = str_replace('lib/phpunit/', $CFG->dirroot.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'phpunit'.DIRECTORY_SEPARATOR, $data);
534 $data = preg_replace('|<directory suffix="_test.php">([^<]+)</directory>|',
535 '<directory suffix="_test.php">'.$CFG->dirroot.(DIRECTORY_SEPARATOR === '\\' ? '\\\\' : DIRECTORY_SEPARATOR).'$1</directory>',
536 $data);
537 file_put_contents("$CFG->dataroot/phpunit/webrunner.xml", $data);
0ea35584 538 testing_fix_file_permissions("$CFG->dataroot/phpunit/webrunner.xml");
7e7cfe7a
PS
539
540 return (bool)$result;
541 }
542
543 /**
544 * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
545 *
546 * @static
547 * @return void, stops if can not write files
548 */
549 public static function build_component_config_files() {
550 global $CFG;
551
552 $template = '
553 <testsuites>
4463f996 554 <testsuite name="@component@_testsuite">
7e7cfe7a
PS
555 <directory suffix="_test.php">.</directory>
556 </testsuite>
9123439e
EL
557 </testsuites>
558 <filter>
559 <whitelist processUncoveredFilesFromWhitelist="false">
560 <directory suffix=".php">.</directory>
561 <exclude>
562 <directory suffix="_test.php">.</directory>
563 </exclude>
564 </whitelist>
565 </filter>';
7e7cfe7a 566
9462323b
RS
567 // Start a sequence between 100000 and 199000 to ensure each call to init produces
568 // different ids in the database. This reduces the risk that hard coded values will
569 // end up being placed in phpunit or behat test code.
570 $sequencestart = 100000 + mt_rand(0, 99) * 1000;
571
7e7cfe7a
PS
572 // Use the upstream file as source for the distributed configurations
573 $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
574 $ftemplate = preg_replace('|<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
575
f8228402
DM
576 // Gets all the components with tests
577 $components = tests_finder::get_components_with_tests('phpunit');
7e7cfe7a
PS
578
579 // Create the corresponding phpunit.xml file for each component
580 foreach ($components as $cname => $cpath) {
581 // Calculate the component suite
582 $ctemplate = $template;
583 $ctemplate = str_replace('@component@', $cname, $ctemplate);
584
585 // Apply it to the file template
586 $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
ddffa9d6
PS
587 $fcontents = str_replace(
588 '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
589 '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
590 $fcontents);
7e7cfe7a
PS
591
592 // fix link to schema
593 $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/');
ed7259d1 594 $fcontents = str_replace('lib/phpunit/', str_repeat('../', $level).'lib/phpunit/', $fcontents);
7e7cfe7a
PS
595
596 // Write the file
597 $result = false;
598 if (is_writable($cpath)) {
599 if ($result = (bool)file_put_contents("$cpath/phpunit.xml", $fcontents)) {
0ea35584 600 testing_fix_file_permissions("$cpath/phpunit.xml");
7e7cfe7a
PS
601 }
602 }
603 // Problems writing file, throw error
604 if (!$result) {
605 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, "Can not create $cpath/phpunit.xml configuration file, verify dir permissions");
606 }
607 }
608 }
609
ef5b5e05
PS
610 /**
611 * To be called from debugging() only.
612 * @param string $message
613 * @param int $level
614 * @param string $from
615 */
616 public static function debugging_triggered($message, $level, $from) {
617 // Store only if debugging triggered from actual test,
618 // we need normal debugging outside of tests to find problems in our phpunit integration.
619 $backtrace = debug_backtrace();
620
621 foreach ($backtrace as $bt) {
ef151737 622 if (isset($bt['object']) and is_object($bt['object'])
801a372d 623 && $bt['object'] instanceof PHPUnit\Framework\TestCase) {
ef151737
RS
624 $debug = new stdClass();
625 $debug->message = $message;
626 $debug->level = $level;
627 $debug->from = $from;
ef5b5e05 628
ef151737 629 self::$debuggings[] = $debug;
ef5b5e05 630
ef151737
RS
631 return true;
632 }
633 }
634 return false;
ef5b5e05
PS
635 }
636
637 /**
638 * Resets the list of debugging messages.
639 */
640 public static function reset_debugging() {
641 self::$debuggings = array();
96f81ea3 642 set_debugging(DEBUG_DEVELOPER);
ef5b5e05
PS
643 }
644
645 /**
646 * Returns all debugging messages triggered during test.
647 * @return array with instances having message, level and stacktrace property.
648 */
649 public static function get_debugging_messages() {
650 return self::$debuggings;
651 }
652
653 /**
654 * Prints out any debug messages accumulated during test execution.
ef151737
RS
655 *
656 * @param bool $return true to return the messages or false to print them directly. Default false.
657 * @return bool|string false if no debug messages, true if debug triggered or string of messages
ef5b5e05 658 */
ef151737 659 public static function display_debugging_messages($return = false) {
ef5b5e05
PS
660 if (empty(self::$debuggings)) {
661 return false;
662 }
ef151737
RS
663
664 $debugstring = '';
ef5b5e05 665 foreach(self::$debuggings as $debug) {
ef151737 666 $debugstring .= 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
ef5b5e05
PS
667 }
668
ef151737
RS
669 if ($return) {
670 return $debugstring;
671 }
672 echo $debugstring;
ef5b5e05
PS
673 return true;
674 }
4c9e03f0
PS
675
676 /**
677 * Start message redirection.
678 *
679 * Note: Do not call directly from tests,
680 * use $sink = $this->redirectMessages() instead.
681 *
682 * @return phpunit_message_sink
683 */
684 public static function start_message_redirection() {
685 if (self::$messagesink) {
686 self::stop_message_redirection();
687 }
688 self::$messagesink = new phpunit_message_sink();
689 return self::$messagesink;
690 }
691
692 /**
693 * End message redirection.
694 *
695 * Note: Do not call directly from tests,
696 * use $sink->close() instead.
697 */
698 public static function stop_message_redirection() {
699 self::$messagesink = null;
700 }
701
702 /**
703 * Are messages redirected to some sink?
704 *
705 * Note: to be called from messagelib.php only!
706 *
707 * @return bool
708 */
709 public static function is_redirecting_messages() {
710 return !empty(self::$messagesink);
711 }
712
713 /**
714 * To be called from messagelib.php only!
715 *
883ce421 716 * @param stdClass $message record from messages table
4c9e03f0
PS
717 * @return bool true means send message, false means message "sent" to sink.
718 */
719 public static function message_sent($message) {
720 if (self::$messagesink) {
721 self::$messagesink->add_message($message);
722 }
723 }
62401e8f 724
1aba6b2b
AN
725 /**
726 * Start phpmailer redirection.
727 *
728 * Note: Do not call directly from tests,
729 * use $sink = $this->redirectEmails() instead.
730 *
731 * @return phpunit_phpmailer_sink
732 */
733 public static function start_phpmailer_redirection() {
734 if (self::$phpmailersink) {
556e3a9e
AN
735 // If an existing mailer sink is active, just clear it.
736 self::$phpmailersink->clear();
737 } else {
738 self::$phpmailersink = new phpunit_phpmailer_sink();
1aba6b2b 739 }
1aba6b2b
AN
740 return self::$phpmailersink;
741 }
742
743 /**
744 * End phpmailer redirection.
745 *
746 * Note: Do not call directly from tests,
747 * use $sink->close() instead.
748 */
749 public static function stop_phpmailer_redirection() {
750 self::$phpmailersink = null;
751 }
752
753 /**
754 * Are messages for phpmailer redirected to some sink?
755 *
756 * Note: to be called from moodle_phpmailer.php only!
757 *
758 * @return bool
759 */
760 public static function is_redirecting_phpmailer() {
761 return !empty(self::$phpmailersink);
762 }
763
764 /**
765 * To be called from messagelib.php only!
766 *
883ce421 767 * @param stdClass $message record from messages table
1aba6b2b
AN
768 * @return bool true means send message, false means message "sent" to sink.
769 */
770 public static function phpmailer_sent($message) {
771 if (self::$phpmailersink) {
772 self::$phpmailersink->add_message($message);
773 }
774 }
775
62401e8f
PS
776 /**
777 * Start event redirection.
778 *
779 * @private
780 * Note: Do not call directly from tests,
781 * use $sink = $this->redirectEvents() instead.
782 *
783 * @return phpunit_event_sink
784 */
785 public static function start_event_redirection() {
786 if (self::$eventsink) {
787 self::stop_event_redirection();
788 }
789 self::$eventsink = new phpunit_event_sink();
790 return self::$eventsink;
791 }
792
793 /**
794 * End event redirection.
795 *
796 * @private
797 * Note: Do not call directly from tests,
798 * use $sink->close() instead.
799 */
800 public static function stop_event_redirection() {
801 self::$eventsink = null;
802 }
803
804 /**
805 * Are events redirected to some sink?
806 *
807 * Note: to be called from \core\event\base only!
808 *
809 * @private
810 * @return bool
811 */
812 public static function is_redirecting_events() {
813 return !empty(self::$eventsink);
814 }
815
816 /**
817 * To be called from \core\event\base only!
818 *
819 * @private
820 * @param \core\event\base $event record from event_read table
821 * @return bool true means send event, false means event "sent" to sink.
822 */
823 public static function event_triggered(\core\event\base $event) {
824 if (self::$eventsink) {
825 self::$eventsink->add_event($event);
826 }
827 }
96805290
DP
828
829 /**
830 * Gets the name of the locale for testing environment (Australian English)
831 * depending on platform environment.
832 *
833 * @return string the locale name.
834 */
835 protected static function get_locale_name() {
836 global $CFG;
837 if ($CFG->ostype === 'WINDOWS') {
838 return 'English_Australia.1252';
839 } else {
840 return 'en_AU.UTF-8';
841 }
842 }
3704ff8c
JD
843
844 /**
845 * Executes all adhoc tasks in the queue. Useful for testing asynchronous behaviour.
846 *
847 * @return void
848 */
849 public static function run_all_adhoc_tasks() {
850 $now = time();
851 while (($task = \core\task\manager::get_next_adhoc_task($now)) !== null) {
852 try {
853 $task->execute();
854 \core\task\manager::adhoc_task_complete($task);
855 } catch (Exception $e) {
856 \core\task\manager::adhoc_task_failed($task);
857 }
858 }
859 }
f6d9efef
JD
860
861 /**
862 * Helper function to call a protected/private method of an object using reflection.
863 *
864 * Example 1. Calling a protected object method:
865 * $result = call_internal_method($myobject, 'method_name', [$param1, $param2], '\my\namespace\myobjectclassname');
866 *
867 * Example 2. Calling a protected static method:
868 * $result = call_internal_method(null, 'method_name', [$param1, $param2], '\my\namespace\myclassname');
869 *
870 * @param object|null $object the object on which to call the method, or null if calling a static method.
871 * @param string $methodname the name of the protected/private method.
872 * @param array $params the array of function params to pass to the method.
873 * @param string $classname the fully namespaced name of the class the object was created from (base in the case of mocks),
874 * or the name of the static class when calling a static method.
875 * @return mixed the respective return value of the method.
876 */
877 public static function call_internal_method($object, $methodname, array $params = array(), $classname) {
878 $reflection = new \ReflectionClass($classname);
879 $method = $reflection->getMethod($methodname);
880 $method->setAccessible(true);
881 return $method->invokeArgs($object, $params);
882 }
7e7cfe7a 883}