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