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