MDL-51077 Availability: Date restriction unit test static not cleared
[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) {
e17dbeeb 104 global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
7e7cfe7a 105
4c9e03f0
PS
106 // Stop any message redirection.
107 phpunit_util::stop_message_redirection();
108
62401e8f
PS
109 // Stop any message redirection.
110 phpunit_util::stop_event_redirection();
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.
116 phpunit_util::start_phpmailer_redirection();
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();
134 $warnings = array();
135
71fc5003 136 if ($detectchanges === true) {
7e7cfe7a
PS
137 if ($resetdb) {
138 $warnings[] = 'Warning: unexpected database modification, resetting DB state';
139 }
140
141 $oldcfg = self::get_global_backup('CFG');
142 $oldsite = self::get_global_backup('SITE');
143 foreach($CFG as $k=>$v) {
144 if (!property_exists($oldcfg, $k)) {
145 $warnings[] = 'Warning: unexpected new $CFG->'.$k.' value';
146 } else if ($oldcfg->$k !== $CFG->$k) {
147 $warnings[] = 'Warning: unexpected change of $CFG->'.$k.' value';
148 }
149 unset($oldcfg->$k);
150
151 }
152 if ($oldcfg) {
153 foreach($oldcfg as $k=>$v) {
154 $warnings[] = 'Warning: unexpected removal of $CFG->'.$k;
155 }
156 }
157
158 if ($USER->id != 0) {
159 $warnings[] = 'Warning: unexpected change of $USER';
160 }
161
162 if ($COURSE->id != $oldsite->id) {
163 $warnings[] = 'Warning: unexpected change of $COURSE';
164 }
82081c1f 165
d6e7a63d
PS
166 if ($CFG->ostype === 'WINDOWS') {
167 if (setlocale(LC_TIME, 0) !== 'English_Australia.1252') {
168 $warnings[] = 'Warning: unexpected change of locale';
169 }
170 } else {
171 if (setlocale(LC_TIME, 0) !== 'en_AU.UTF-8') {
172 $warnings[] = 'Warning: unexpected change of locale';
173 }
174 }
70faad65
PS
175 }
176
177 if (ini_get('max_execution_time') != 0) {
178 // This is special warning for all resets because we do not want any
179 // libraries to mess with timeouts unintentionally.
180 // Our PHPUnit integration is not supposed to change it either.
181
71fc5003
PS
182 if ($detectchanges !== false) {
183 $warnings[] = 'Warning: max_execution_time was changed to '.ini_get('max_execution_time');
184 }
70faad65 185 set_time_limit(0);
7e7cfe7a
PS
186 }
187
188 // restore original globals
189 $_SERVER = self::get_global_backup('_SERVER');
190 $CFG = self::get_global_backup('CFG');
191 $SITE = self::get_global_backup('SITE');
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);
7e7cfe7a 213 events_get_handlers('reset');
2f1e464a 214 core_text::reset_caches();
ed23ad31 215 get_message_processors(false, true);
73a0f3ba 216 filter_manager::reset_caches();
91fed57a 217 core_filetypes::reset_caches();
218
89a73b08 219 // Reset static unit test options.
220 if (class_exists('\availability_date\condition', false)) {
221 \availability_date\condition::set_current_time_for_test(0);
222 }
223
6b8e46ad
RT
224 // Reset internal users.
225 core_user::reset_internal_users();
226
6fd1cf05 227 //TODO MDL-25290: add more resets here and probably refactor them to new core function
7e7cfe7a 228
2beba297 229 // Reset course and module caches.
13d5c938 230 if (class_exists('format_base')) {
2beba297 231 // If file containing class is not loaded, there is no cache there anyway.
13d5c938
PS
232 format_base::reset_course_cache(0);
233 }
b46be6ad 234 get_fast_modinfo(0, 0, true);
13d5c938 235
98547432 236 // Reset other singletons.
e87214bd
PS
237 if (class_exists('core_plugin_manager')) {
238 core_plugin_manager::reset_caches(true);
98547432 239 }
e87214bd
PS
240 if (class_exists('\core\update\checker')) {
241 \core\update\checker::reset_caches(true);
98547432 242 }
e87214bd
PS
243 if (class_exists('\core\update\deployer')) {
244 \core\update\deployer::reset_caches(true);
dc11af19 245 }
98547432 246
27479b5c 247 // Clear static cache within restore.
248 if (class_exists('restore_section_structure_step')) {
249 restore_section_structure_step::reset_caches();
250 }
251
7e7cfe7a
PS
252 // purge dataroot directory
253 self::reset_dataroot();
254
255 // restore original config once more in case resetting of caches changed CFG
256 $CFG = self::get_global_backup('CFG');
257
258 // inform data generator
259 self::get_data_generator()->reset();
260
261 // fix PHP settings
262 error_reporting($CFG->debug);
263
d6e7a63d
PS
264 // Reset the date/time class.
265 core_date::phpunit_reset();
266
267 // Make sure the time locale is consistent - that is Australian English.
268 if ($CFG->ostype === 'WINDOWS') {
269 setlocale(LC_TIME, 'English_Australia.1252');
270 } else {
271 setlocale(LC_TIME, 'en_AU.UTF-8');
272 }
273
7e7cfe7a
PS
274 // verify db writes just in case something goes wrong in reset
275 if (self::$lastdbwrites != $DB->perf_get_writes()) {
276 error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
277 self::$lastdbwrites = $DB->perf_get_writes();
278 }
279
280 if ($warnings) {
281 $warnings = implode("\n", $warnings);
282 trigger_error($warnings, E_USER_WARNING);
283 }
284 }
285
a9236a2c
PS
286 /**
287 * Reset all database tables to default values.
288 * @static
289 * @return bool true if reset done, false if skipped
290 */
291 public static function reset_database() {
292 global $DB;
293
294 if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) {
295 return false;
296 }
297
298 if (!parent::reset_database()) {
299 return false;
300 }
301
302 self::$lastdbwrites = $DB->perf_get_writes();
303
304 return true;
305 }
306
7e7cfe7a
PS
307 /**
308 * Called during bootstrap only!
309 * @internal
310 * @static
311 * @return void
312 */
313 public static function bootstrap_init() {
314 global $CFG, $SITE, $DB;
315
316 // backup the globals
317 self::$globals['_SERVER'] = $_SERVER;
318 self::$globals['CFG'] = clone($CFG);
319 self::$globals['SITE'] = clone($SITE);
320 self::$globals['DB'] = $DB;
321
322 // refresh data in all tables, clear caches, etc.
323 phpunit_util::reset_all_data();
324 }
325
15bac12e
PS
326 /**
327 * Print some Moodle related info to console.
328 * @internal
329 * @static
330 * @return void
331 */
332 public static function bootstrap_moodle_info() {
7514c2f2 333 echo self::get_site_info();
15bac12e
PS
334 }
335
7e7cfe7a
PS
336 /**
337 * Returns original state of global variable.
338 * @static
339 * @param string $name
340 * @return mixed
341 */
342 public static function get_global_backup($name) {
343 if ($name === 'DB') {
344 // no cloning of database object,
345 // we just need the original reference, not original state
346 return self::$globals['DB'];
347 }
348 if (isset(self::$globals[$name])) {
349 if (is_object(self::$globals[$name])) {
350 $return = clone(self::$globals[$name]);
351 return $return;
352 } else {
353 return self::$globals[$name];
354 }
355 }
356 return null;
357 }
358
7e7cfe7a
PS
359 /**
360 * Is this site initialised to run unit tests?
361 *
362 * @static
363 * @return int array errorcode=>message, 0 means ok
364 */
365 public static function testing_ready_problem() {
0ea35584 366 global $DB;
7e7cfe7a
PS
367
368 if (!self::is_test_site()) {
369 // dataroot was verified in bootstrap, so it must be DB
370 return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
371 }
372
0ea35584 373 $tables = $DB->get_tables(false);
7e7cfe7a
PS
374 if (empty($tables)) {
375 return array(PHPUNIT_EXITCODE_INSTALL, '');
376 }
377
0ea35584 378 if (!self::is_test_data_updated()) {
7e7cfe7a
PS
379 return array(PHPUNIT_EXITCODE_REINSTALL, '');
380 }
381
382 return array(0, '');
383 }
384
385 /**
386 * Drop all test site data.
387 *
388 * Note: To be used from CLI scripts only.
389 *
390 * @static
85b72a75 391 * @param bool $displayprogress if true, this method will echo progress information.
7e7cfe7a
PS
392 * @return void may terminate execution with exit code
393 */
85b72a75 394 public static function drop_site($displayprogress = false) {
7e7cfe7a
PS
395 global $DB, $CFG;
396
397 if (!self::is_test_site()) {
398 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
399 }
400
85b72a75
TH
401 // Purge dataroot
402 if ($displayprogress) {
403 echo "Purging dataroot:\n";
404 }
0ea35584 405
7e7cfe7a 406 self::reset_dataroot();
0ea35584
DM
407 testing_initdataroot($CFG->dataroot, 'phpunit');
408 self::drop_dataroot();
7e7cfe7a
PS
409
410 // drop all tables
0ea35584 411 self::drop_database($displayprogress);
7e7cfe7a
PS
412 }
413
414 /**
415 * Perform a fresh test site installation
416 *
417 * Note: To be used from CLI scripts only.
418 *
419 * @static
420 * @return void may terminate execution with exit code
421 */
422 public static function install_site() {
423 global $DB, $CFG;
424
425 if (!self::is_test_site()) {
426 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
427 }
428
429 if ($DB->get_tables()) {
430 list($errorcode, $message) = phpunit_util::testing_ready_problem();
431 if ($errorcode) {
432 phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
433 } else {
434 phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
435 }
436 }
437
438 $options = array();
439 $options['adminpass'] = 'admin';
440 $options['shortname'] = 'phpunit';
441 $options['fullname'] = 'PHPUnit test site';
442
443 install_cli_database($options, false);
444
14ecbb2e
AN
445 // Set the admin email address.
446 $DB->set_field('user', 'email', 'admin@example.com', array('username' => 'admin'));
447
770eac98
448 // Disable all logging for performance and sanity reasons.
449 set_config('enabled_stores', '', 'tool_log');
450
c78f19d1
JM
451 // We need to keep the installed dataroot filedir files.
452 // So each time we reset the dataroot before running a test, the default files are still installed.
453 self::save_original_data_files();
454
0ea35584
DM
455 // Store version hash in the database and in a file.
456 self::store_versions_hash();
7e7cfe7a 457
0ea35584
DM
458 // Store database data and structure.
459 self::store_database_state();
7e7cfe7a
PS
460 }
461
462 /**
463 * Builds dirroot/phpunit.xml and dataroot/phpunit/webrunner.xml files using defaults from /phpunit.xml.dist
464 * @static
465 * @return bool true means main config file created, false means only dataroot file created
466 */
467 public static function build_config_file() {
468 global $CFG;
469
470 $template = '
a9edd8a6 471 <testsuite name="@component@_testsuite">
7e7cfe7a
PS
472 <directory suffix="_test.php">@dir@</directory>
473 </testsuite>';
474 $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
475
476 $suites = '';
477
46f6f7f2 478 $plugintypes = core_component::get_plugin_types();
7e7cfe7a
PS
479 ksort($plugintypes);
480 foreach ($plugintypes as $type=>$unused) {
bd3b3bba 481 $plugs = core_component::get_plugin_list($type);
7e7cfe7a
PS
482 ksort($plugs);
483 foreach ($plugs as $plug=>$fullplug) {
484 if (!file_exists("$fullplug/tests/")) {
485 continue;
486 }
487 $dir = substr($fullplug, strlen($CFG->dirroot)+1);
488 $dir .= '/tests';
489 $component = $type.'_'.$plug;
490
491 $suite = str_replace('@component@', $component, $template);
492 $suite = str_replace('@dir@', $dir, $suite);
493
494 $suites .= $suite;
495 }
496 }
529495f7
RS
497 // Start a sequence between 100000 and 199000 to ensure each call to init produces
498 // different ids in the database. This reduces the risk that hard coded values will
499 // end up being placed in phpunit or behat test code.
500 $sequencestart = 100000 + mt_rand(0, 99) * 1000;
7e7cfe7a
PS
501
502 $data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
ddffa9d6
PS
503 $data = str_replace(
504 '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
505 '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
506 $data);
7e7cfe7a
PS
507
508 $result = false;
509 if (is_writable($CFG->dirroot)) {
510 if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
0ea35584 511 testing_fix_file_permissions("$CFG->dirroot/phpunit.xml");
7e7cfe7a
PS
512 }
513 }
514
515 // 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
516 $data = str_replace('lib/phpunit/', $CFG->dirroot.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'phpunit'.DIRECTORY_SEPARATOR, $data);
517 $data = preg_replace('|<directory suffix="_test.php">([^<]+)</directory>|',
518 '<directory suffix="_test.php">'.$CFG->dirroot.(DIRECTORY_SEPARATOR === '\\' ? '\\\\' : DIRECTORY_SEPARATOR).'$1</directory>',
519 $data);
520 file_put_contents("$CFG->dataroot/phpunit/webrunner.xml", $data);
0ea35584 521 testing_fix_file_permissions("$CFG->dataroot/phpunit/webrunner.xml");
7e7cfe7a
PS
522
523 return (bool)$result;
524 }
525
526 /**
527 * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
528 *
529 * @static
530 * @return void, stops if can not write files
531 */
532 public static function build_component_config_files() {
533 global $CFG;
534
535 $template = '
536 <testsuites>
537 <testsuite name="@component@">
538 <directory suffix="_test.php">.</directory>
539 </testsuite>
540 </testsuites>';
541
9462323b
RS
542 // Start a sequence between 100000 and 199000 to ensure each call to init produces
543 // different ids in the database. This reduces the risk that hard coded values will
544 // end up being placed in phpunit or behat test code.
545 $sequencestart = 100000 + mt_rand(0, 99) * 1000;
546
7e7cfe7a
PS
547 // Use the upstream file as source for the distributed configurations
548 $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
549 $ftemplate = preg_replace('|<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
550
f8228402
DM
551 // Gets all the components with tests
552 $components = tests_finder::get_components_with_tests('phpunit');
7e7cfe7a
PS
553
554 // Create the corresponding phpunit.xml file for each component
555 foreach ($components as $cname => $cpath) {
556 // Calculate the component suite
557 $ctemplate = $template;
558 $ctemplate = str_replace('@component@', $cname, $ctemplate);
559
560 // Apply it to the file template
561 $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
ddffa9d6
PS
562 $fcontents = str_replace(
563 '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
564 '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
565 $fcontents);
7e7cfe7a
PS
566
567 // fix link to schema
568 $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/');
ed7259d1 569 $fcontents = str_replace('lib/phpunit/', str_repeat('../', $level).'lib/phpunit/', $fcontents);
7e7cfe7a
PS
570
571 // Write the file
572 $result = false;
573 if (is_writable($cpath)) {
574 if ($result = (bool)file_put_contents("$cpath/phpunit.xml", $fcontents)) {
0ea35584 575 testing_fix_file_permissions("$cpath/phpunit.xml");
7e7cfe7a
PS
576 }
577 }
578 // Problems writing file, throw error
579 if (!$result) {
580 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, "Can not create $cpath/phpunit.xml configuration file, verify dir permissions");
581 }
582 }
583 }
584
ef5b5e05
PS
585 /**
586 * To be called from debugging() only.
587 * @param string $message
588 * @param int $level
589 * @param string $from
590 */
591 public static function debugging_triggered($message, $level, $from) {
592 // Store only if debugging triggered from actual test,
593 // we need normal debugging outside of tests to find problems in our phpunit integration.
594 $backtrace = debug_backtrace();
595
596 foreach ($backtrace as $bt) {
597 $intest = false;
598 if (isset($bt['object']) and is_object($bt['object'])) {
599 if ($bt['object'] instanceof PHPUnit_Framework_TestCase) {
600 if (strpos($bt['function'], 'test') === 0) {
601 $intest = true;
602 break;
603 }
604 }
605 }
606 }
607 if (!$intest) {
608 return false;
609 }
610
611 $debug = new stdClass();
612 $debug->message = $message;
613 $debug->level = $level;
614 $debug->from = $from;
615
616 self::$debuggings[] = $debug;
617
618 return true;
619 }
620
621 /**
622 * Resets the list of debugging messages.
623 */
624 public static function reset_debugging() {
625 self::$debuggings = array();
96f81ea3 626 set_debugging(DEBUG_DEVELOPER);
ef5b5e05
PS
627 }
628
629 /**
630 * Returns all debugging messages triggered during test.
631 * @return array with instances having message, level and stacktrace property.
632 */
633 public static function get_debugging_messages() {
634 return self::$debuggings;
635 }
636
637 /**
638 * Prints out any debug messages accumulated during test execution.
639 * @return bool false if no debug messages, true if debug triggered
640 */
641 public static function display_debugging_messages() {
642 if (empty(self::$debuggings)) {
643 return false;
644 }
645 foreach(self::$debuggings as $debug) {
646 echo 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
647 }
648
649 return true;
650 }
4c9e03f0
PS
651
652 /**
653 * Start message redirection.
654 *
655 * Note: Do not call directly from tests,
656 * use $sink = $this->redirectMessages() instead.
657 *
658 * @return phpunit_message_sink
659 */
660 public static function start_message_redirection() {
661 if (self::$messagesink) {
662 self::stop_message_redirection();
663 }
664 self::$messagesink = new phpunit_message_sink();
665 return self::$messagesink;
666 }
667
668 /**
669 * End message redirection.
670 *
671 * Note: Do not call directly from tests,
672 * use $sink->close() instead.
673 */
674 public static function stop_message_redirection() {
675 self::$messagesink = null;
676 }
677
678 /**
679 * Are messages redirected to some sink?
680 *
681 * Note: to be called from messagelib.php only!
682 *
683 * @return bool
684 */
685 public static function is_redirecting_messages() {
686 return !empty(self::$messagesink);
687 }
688
689 /**
690 * To be called from messagelib.php only!
691 *
692 * @param stdClass $message record from message_read table
693 * @return bool true means send message, false means message "sent" to sink.
694 */
695 public static function message_sent($message) {
696 if (self::$messagesink) {
697 self::$messagesink->add_message($message);
698 }
699 }
62401e8f 700
1aba6b2b
AN
701 /**
702 * Start phpmailer redirection.
703 *
704 * Note: Do not call directly from tests,
705 * use $sink = $this->redirectEmails() instead.
706 *
707 * @return phpunit_phpmailer_sink
708 */
709 public static function start_phpmailer_redirection() {
710 if (self::$phpmailersink) {
556e3a9e
AN
711 // If an existing mailer sink is active, just clear it.
712 self::$phpmailersink->clear();
713 } else {
714 self::$phpmailersink = new phpunit_phpmailer_sink();
1aba6b2b 715 }
1aba6b2b
AN
716 return self::$phpmailersink;
717 }
718
719 /**
720 * End phpmailer redirection.
721 *
722 * Note: Do not call directly from tests,
723 * use $sink->close() instead.
724 */
725 public static function stop_phpmailer_redirection() {
726 self::$phpmailersink = null;
727 }
728
729 /**
730 * Are messages for phpmailer redirected to some sink?
731 *
732 * Note: to be called from moodle_phpmailer.php only!
733 *
734 * @return bool
735 */
736 public static function is_redirecting_phpmailer() {
737 return !empty(self::$phpmailersink);
738 }
739
740 /**
741 * To be called from messagelib.php only!
742 *
743 * @param stdClass $message record from message_read table
744 * @return bool true means send message, false means message "sent" to sink.
745 */
746 public static function phpmailer_sent($message) {
747 if (self::$phpmailersink) {
748 self::$phpmailersink->add_message($message);
749 }
750 }
751
62401e8f
PS
752 /**
753 * Start event redirection.
754 *
755 * @private
756 * Note: Do not call directly from tests,
757 * use $sink = $this->redirectEvents() instead.
758 *
759 * @return phpunit_event_sink
760 */
761 public static function start_event_redirection() {
762 if (self::$eventsink) {
763 self::stop_event_redirection();
764 }
765 self::$eventsink = new phpunit_event_sink();
766 return self::$eventsink;
767 }
768
769 /**
770 * End event redirection.
771 *
772 * @private
773 * Note: Do not call directly from tests,
774 * use $sink->close() instead.
775 */
776 public static function stop_event_redirection() {
777 self::$eventsink = null;
778 }
779
780 /**
781 * Are events redirected to some sink?
782 *
783 * Note: to be called from \core\event\base only!
784 *
785 * @private
786 * @return bool
787 */
788 public static function is_redirecting_events() {
789 return !empty(self::$eventsink);
790 }
791
792 /**
793 * To be called from \core\event\base only!
794 *
795 * @private
796 * @param \core\event\base $event record from event_read table
797 * @return bool true means send event, false means event "sent" to sink.
798 */
799 public static function event_triggered(\core\event\base $event) {
800 if (self::$eventsink) {
801 self::$eventsink->add_event($event);
802 }
803 }
7e7cfe7a 804}