MDL-81456 core: Stop injecting test data into real hook manager
[moodle.git] / lib / phpunit / classes / util.php
blob527036ce5be29353b345bfb9cf4c4d8ff675c828
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.
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/>.
17 /**
18 * Utility class.
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
26 use core\{
27 di,
28 hook,
31 require_once(__DIR__.'/../../testing/classes/util.php');
32 require_once(__DIR__ . "/coverage_info.php");
34 /**
35 * Collection of utility methods.
37 * @package core
38 * @category phpunit
39 * @copyright 2012 Petr Skoda {@link http://skodak.org}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class phpunit_util extends testing_util {
43 /**
44 * @var int last value of db writes counter, used for db resetting
46 public static $lastdbwrites = null;
48 /** @var array An array of original globals, restored after each test */
49 protected static $globals = array();
51 /** @var array list of debugging messages triggered during the last test execution */
52 protected static $debuggings = array();
54 /** @var phpunit_message_sink alternative target for moodle messaging */
55 protected static $messagesink = null;
57 /** @var phpunit_phpmailer_sink alternative target for phpmailer messaging */
58 protected static $phpmailersink = null;
60 /** @var phpunit_message_sink alternative target for moodle messaging */
61 protected static $eventsink = null;
63 /**
64 * @var array Files to skip when resetting dataroot folder
66 protected static $datarootskiponreset = array('.', '..', 'phpunittestdir.txt', 'phpunit', '.htaccess');
68 /**
69 * @var array Files to skip when dropping dataroot folder
71 protected static $datarootskipondrop = array('.', '..', 'lock');
73 /**
74 * Load global $CFG;
75 * @internal
76 * @static
77 * @return void
79 public static function initialise_cfg() {
80 global $DB;
81 $dbhash = false;
82 try {
83 $dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'));
84 } catch (Exception $e) {
85 // not installed yet
86 initialise_cfg();
87 return;
89 if ($dbhash !== core_component::get_all_versions_hash()) {
90 // do not set CFG - the only way forward is to drop and reinstall
91 return;
93 // standard CFG init
94 initialise_cfg();
97 /**
98 * Reset contents of all database tables to initial values, reset caches, etc.
100 * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
102 * @static
103 * @param bool $detectchanges
104 * true - changes in global state and database are reported as errors
105 * false - no errors reported
106 * null - only critical problems are reported as errors
107 * @return void
109 public static function reset_all_data($detectchanges = false) {
110 global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $FULLME, $FILTERLIB_PRIVATE;
112 // Stop all hook redirections.
113 di::get(hook\manager::class)->phpunit_stop_redirections();
115 // Stop any message redirection.
116 self::stop_message_redirection();
118 // Stop any message redirection.
119 self::stop_event_redirection();
121 // Start a new email redirection.
122 // This will clear any existing phpmailer redirection.
123 // We redirect all phpmailer output to this message sink which is
124 // called instead of phpmailer actually sending the message.
125 self::start_phpmailer_redirection();
127 // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
128 // This accounted for 25% of the total time running phpunit - so we removed it.
130 // Show any unhandled debugging messages, the runbare() could already reset it.
131 self::display_debugging_messages();
132 self::reset_debugging();
134 // reset global $DB in case somebody mocked it
135 $DB = self::get_global_backup('DB');
137 if ($DB->is_transaction_started()) {
138 // we can not reset inside transaction
139 $DB->force_transaction_rollback();
142 $resetdb = self::reset_database();
143 $localename = self::get_locale_name();
144 $warnings = array();
146 if ($detectchanges === true) {
147 if ($resetdb) {
148 $warnings[] = 'Warning: unexpected database modification, resetting DB state';
151 $oldcfg = self::get_global_backup('CFG');
152 $oldsite = self::get_global_backup('SITE');
153 foreach($CFG as $k=>$v) {
154 if (!property_exists($oldcfg, $k)) {
155 $warnings[] = 'Warning: unexpected new $CFG->'.$k.' value';
156 } else if ($oldcfg->$k !== $CFG->$k) {
157 $warnings[] = 'Warning: unexpected change of $CFG->'.$k.' value';
159 unset($oldcfg->$k);
162 if ($oldcfg) {
163 foreach($oldcfg as $k=>$v) {
164 $warnings[] = 'Warning: unexpected removal of $CFG->'.$k;
168 if ($USER->id != 0) {
169 $warnings[] = 'Warning: unexpected change of $USER';
172 if ($COURSE->id != $oldsite->id) {
173 $warnings[] = 'Warning: unexpected change of $COURSE';
176 if ($FULLME !== self::get_global_backup('FULLME')) {
177 $warnings[] = 'Warning: unexpected change of $FULLME';
180 if (setlocale(LC_TIME, 0) !== $localename) {
181 $warnings[] = 'Warning: unexpected change of locale';
185 if (ini_get('max_execution_time') != 0) {
186 // This is special warning for all resets because we do not want any
187 // libraries to mess with timeouts unintentionally.
188 // Our PHPUnit integration is not supposed to change it either.
190 if ($detectchanges !== false) {
191 $warnings[] = 'Warning: max_execution_time was changed to '.ini_get('max_execution_time');
193 set_time_limit(0);
196 // restore original globals
197 $_SERVER = self::get_global_backup('_SERVER');
198 $CFG = self::get_global_backup('CFG');
199 $SITE = self::get_global_backup('SITE');
200 $FULLME = self::get_global_backup('FULLME');
201 $_GET = array();
202 $_POST = array();
203 $_FILES = array();
204 $_REQUEST = array();
205 $COURSE = $SITE;
207 // reinitialise following globals
208 $OUTPUT = new bootstrap_renderer();
209 $PAGE = new moodle_page();
210 $FULLME = null;
211 $ME = null;
212 $SCRIPT = null;
213 $FILTERLIB_PRIVATE = null;
214 if (!empty($SESSION->notifications)) {
215 $SESSION->notifications = [];
218 // Empty sessison and set fresh new not-logged-in user.
219 \core\session\manager::init_empty_session();
221 // reset all static caches
222 \core\event\manager::phpunit_reset();
223 accesslib_clear_all_caches(true);
224 accesslib_reset_role_cache();
225 get_string_manager()->reset_caches(true);
226 reset_text_filters_cache(true);
227 get_message_processors(false, true, true);
228 filter_manager::reset_caches();
229 core_filetypes::reset_caches();
230 \core_search\manager::clear_static();
231 core_user::reset_caches();
232 \core\output\icon_system::reset_caches();
233 if (class_exists('core_media_manager', false)) {
234 core_media_manager::reset_caches();
237 // Reset static unit test options.
238 if (class_exists('\availability_date\condition', false)) {
239 \availability_date\condition::set_current_time_for_test(0);
242 // Reset internal users.
243 core_user::reset_internal_users();
245 // Clear static caches in calendar container.
246 if (class_exists('\core_calendar\local\event\container', false)) {
247 core_calendar\local\event\container::reset_caches();
250 //TODO MDL-25290: add more resets here and probably refactor them to new core function
252 // Reset course and module caches.
253 core_courseformat\base::reset_course_cache(0);
254 get_fast_modinfo(0, 0, true);
256 // Reset other singletons.
257 if (class_exists('core_plugin_manager')) {
258 core_plugin_manager::reset_caches(true);
260 if (class_exists('\core\update\checker')) {
261 \core\update\checker::reset_caches(true);
263 if (class_exists('\core_course\customfield\course_handler')) {
264 \core_course\customfield\course_handler::reset_caches();
266 if (class_exists('\core_reportbuilder\manager')) {
267 \core_reportbuilder\manager::reset_caches();
269 if (class_exists('\core_cohort\customfield\cohort_handler')) {
270 \core_cohort\customfield\cohort_handler::reset_caches();
272 if (class_exists('\core_group\customfield\group_handler')) {
273 \core_group\customfield\group_handler::reset_caches();
275 if (class_exists('\core_group\customfield\grouping_handler')) {
276 \core_group\customfield\grouping_handler::reset_caches();
279 // Clear static cache within restore.
280 if (class_exists('restore_section_structure_step')) {
281 restore_section_structure_step::reset_caches();
284 // purge dataroot directory
285 self::reset_dataroot();
287 // restore original config once more in case resetting of caches changed CFG
288 $CFG = self::get_global_backup('CFG');
290 // inform data generator
291 self::get_data_generator()->reset();
293 // fix PHP settings
294 error_reporting($CFG->debug);
296 // Reset the date/time class.
297 core_date::phpunit_reset();
299 // Make sure the time locale is consistent - that is Australian English.
300 setlocale(LC_TIME, $localename);
302 // Reset the log manager cache.
303 get_log_manager(true);
305 // Reset user agent.
306 core_useragent::instance(true, null);
308 // Reset the DI container.
309 \core\di::reset_container();
311 // verify db writes just in case something goes wrong in reset
312 if (self::$lastdbwrites != $DB->perf_get_writes()) {
313 error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
314 self::$lastdbwrites = $DB->perf_get_writes();
317 if ($warnings) {
318 $warnings = implode("\n", $warnings);
319 trigger_error($warnings, E_USER_WARNING);
324 * Reset all database tables to default values.
325 * @static
326 * @return bool true if reset done, false if skipped
328 public static function reset_database() {
329 global $DB;
331 if (defined('PHPUNIT_ISOLATED_TEST') && PHPUNIT_ISOLATED_TEST && self::$lastdbwrites === null) {
332 // This is an isolated test and the lastdbwrites has not yet been initialised.
333 // Isolated test runs are reset by the test runner before the run starts.
334 self::$lastdbwrites = $DB->perf_get_writes();
337 if (!is_null(self::$lastdbwrites) && self::$lastdbwrites == $DB->perf_get_writes()) {
338 return false;
341 if (!parent::reset_database()) {
342 return false;
345 self::$lastdbwrites = $DB->perf_get_writes();
347 return true;
351 * Called during bootstrap only!
352 * @internal
353 * @static
354 * @return void
356 public static function bootstrap_init() {
357 global $CFG, $SITE, $DB, $FULLME;
359 // backup the globals
360 self::$globals['_SERVER'] = $_SERVER;
361 self::$globals['CFG'] = clone($CFG);
362 self::$globals['SITE'] = clone($SITE);
363 self::$globals['DB'] = $DB;
364 self::$globals['FULLME'] = $FULLME;
366 // refresh data in all tables, clear caches, etc.
367 self::reset_all_data();
371 * Print some Moodle related info to console.
372 * @internal
373 * @static
374 * @return void
376 public static function bootstrap_moodle_info() {
377 echo self::get_site_info();
381 * Returns original state of global variable.
382 * @static
383 * @param string $name
384 * @return mixed
386 public static function get_global_backup($name) {
387 if ($name === 'DB') {
388 // no cloning of database object,
389 // we just need the original reference, not original state
390 return self::$globals['DB'];
392 if (isset(self::$globals[$name])) {
393 if (is_object(self::$globals[$name])) {
394 $return = clone(self::$globals[$name]);
395 return $return;
396 } else {
397 return self::$globals[$name];
400 return null;
404 * Is this site initialised to run unit tests?
406 * @static
407 * @return int array errorcode=>message, 0 means ok
409 public static function testing_ready_problem() {
410 global $DB;
412 $localename = self::get_locale_name();
413 if (setlocale(LC_TIME, $localename) === false) {
414 return array(PHPUNIT_EXITCODE_CONFIGERROR, "Required locale '$localename' is not installed.");
417 if (!self::is_test_site()) {
418 // dataroot was verified in bootstrap, so it must be DB
419 return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
422 $tables = $DB->get_tables(false);
423 if (empty($tables)) {
424 return array(PHPUNIT_EXITCODE_INSTALL, '');
427 if (!self::is_test_data_updated()) {
428 return array(PHPUNIT_EXITCODE_REINSTALL, '');
431 return array(0, '');
435 * Drop all test site data.
437 * Note: To be used from CLI scripts only.
439 * @static
440 * @param bool $displayprogress if true, this method will echo progress information.
441 * @return void may terminate execution with exit code
443 public static function drop_site($displayprogress = false) {
444 global $DB, $CFG;
446 if (!self::is_test_site()) {
447 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
450 // Purge dataroot
451 if ($displayprogress) {
452 echo "Purging dataroot:\n";
455 self::reset_dataroot();
456 testing_initdataroot($CFG->dataroot, 'phpunit');
458 // Drop all tables.
459 self::drop_database($displayprogress);
461 // Drop dataroot.
462 self::drop_dataroot();
466 * Perform a fresh test site installation
468 * Note: To be used from CLI scripts only.
470 * @static
471 * @return void may terminate execution with exit code
473 public static function install_site() {
474 global $DB, $CFG;
476 if (!self::is_test_site()) {
477 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
480 if ($DB->get_tables()) {
481 list($errorcode, $message) = self::testing_ready_problem();
482 if ($errorcode) {
483 phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
484 } else {
485 phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
489 $options = array();
490 $options['adminpass'] = 'admin';
491 $options['shortname'] = 'phpunit';
492 $options['fullname'] = 'PHPUnit test site';
494 install_cli_database($options, false);
496 // Set the admin email address.
497 $DB->set_field('user', 'email', 'admin@example.com', array('username' => 'admin'));
499 // Disable all logging for performance and sanity reasons.
500 set_config('enabled_stores', '', 'tool_log');
502 // Remove any default blocked hosts and port restrictions, to avoid blocking tests (eg those using local files).
503 set_config('curlsecurityblockedhosts', '');
504 set_config('curlsecurityallowedport', '');
506 // Execute all the adhoc tasks.
507 while ($task = \core\task\manager::get_next_adhoc_task(time())) {
508 $task->execute();
509 \core\task\manager::adhoc_task_complete($task);
512 // We need to keep the installed dataroot filedir files.
513 // So each time we reset the dataroot before running a test, the default files are still installed.
514 self::save_original_data_files();
516 // Store version hash in the database and in a file.
517 self::store_versions_hash();
519 // Store database data and structure.
520 self::store_database_state();
524 * Builds dirroot/phpunit.xml file using defaults from /phpunit.xml.dist
525 * @static
526 * @return bool true means main config file created, false means only dataroot file created
528 public static function build_config_file() {
529 global $CFG;
531 $template = <<<EOF
532 <testsuite name="@component@_testsuite">
533 <directory suffix="_test.php">@dir@</directory>
534 </testsuite>
536 EOF;
537 $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
539 $suites = '';
540 $includelists = [];
541 $excludelists = [];
543 $subsystems = core_component::get_core_subsystems();
544 $subsystems['core'] = $CFG->dirroot . '/lib';
545 foreach ($subsystems as $subsystem => $fulldir) {
546 if (empty($fulldir)) {
547 continue;
549 if (!file_exists("{$fulldir}/tests/")) {
550 // There are no tests - skip this directory.
551 continue;
554 $dir = substr($fulldir, strlen($CFG->dirroot) + 1);
555 if ($coverageinfo = self::get_coverage_info($fulldir)) {
556 $includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
557 $excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
561 $plugintypes = core_component::get_plugin_types();
562 ksort($plugintypes);
563 foreach (array_keys($plugintypes) as $type) {
564 $plugs = core_component::get_plugin_list($type);
565 ksort($plugs);
566 foreach ($plugs as $plug => $plugindir) {
567 if (!file_exists("{$plugindir}/tests/")) {
568 // There are no tests - skip this directory.
569 continue;
572 $dir = substr($plugindir, strlen($CFG->dirroot) + 1);
573 $testdir = "{$dir}/tests";
574 $component = "{$type}_{$plug}";
576 $suite = str_replace('@component@', $component, $template);
577 $suite = str_replace('@dir@', $testdir, $suite);
579 $suites .= $suite;
581 if ($coverageinfo = self::get_coverage_info($plugindir)) {
583 $includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
584 $excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
589 // Start a sequence between 100000 and 199000 to ensure each call to init produces
590 // different ids in the database. This reduces the risk that hard coded values will
591 // end up being placed in phpunit or behat test code.
592 $sequencestart = 100000 + mt_rand(0, 99) * 1000;
594 $data = preg_replace('| *<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', trim($suites, "\n"), $data, 1);
595 $data = str_replace(
596 '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
597 '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
598 $data);
600 $coverages = self::get_coverage_config($includelists, $excludelists);
601 $data = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $data);
603 $result = false;
604 if (is_writable($CFG->dirroot)) {
605 if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
606 testing_fix_file_permissions("$CFG->dirroot/phpunit.xml");
610 return (bool)$result;
614 * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
616 * @static
617 * @return void, stops if can not write files
619 public static function build_component_config_files() {
620 global $CFG;
622 $template = <<<EOT
623 <testsuites>
624 <testsuite name="@component@_testsuite">
625 <directory suffix="_test.php">.</directory>
626 </testsuite>
627 </testsuites>
628 EOT;
629 $coveragedefault = <<<EOT
630 <include>
631 <directory suffix=".php">.</directory>
632 </include>
633 <exclude>
634 <directory suffix="_test.php">.</directory>
635 </exclude>
636 EOT;
638 // Start a sequence between 100000 and 199000 to ensure each call to init produces
639 // different ids in the database. This reduces the risk that hard coded values will
640 // end up being placed in phpunit or behat test code.
641 $sequencestart = 100000 + mt_rand(0, 99) * 1000;
643 // Use the upstream file as source for the distributed configurations
644 $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
645 $ftemplate = preg_replace('| *<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
647 // Gets all the components with tests
648 $components = tests_finder::get_components_with_tests('phpunit');
650 // Create the corresponding phpunit.xml file for each component
651 foreach ($components as $cname => $cpath) {
652 // Calculate the component suite
653 $ctemplate = $template;
654 $ctemplate = str_replace('@component@', $cname, $ctemplate);
656 $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
658 // Check for coverage configurations.
659 if ($coverageinfo = self::get_coverage_info($cpath)) {
660 $coverages = self::get_coverage_config($coverageinfo->get_includelists(''), $coverageinfo->get_excludelists(''));
661 } else {
662 $coverages = $coveragedefault;
664 $fcontents = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $fcontents);
666 // Apply it to the file template.
667 $fcontents = str_replace(
668 '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
669 '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
670 $fcontents);
672 // fix link to schema
673 $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/');
674 $fcontents = str_replace('lib/phpunit/', str_repeat('../', $level).'lib/phpunit/', $fcontents);
676 // Write the file
677 $result = false;
678 if (is_writable($cpath)) {
679 if ($result = (bool)file_put_contents("$cpath/phpunit.xml", $fcontents)) {
680 testing_fix_file_permissions("$cpath/phpunit.xml");
683 // Problems writing file, throw error
684 if (!$result) {
685 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, "Can not create $cpath/phpunit.xml configuration file, verify dir permissions");
691 * To be called from debugging() only.
692 * @param string $message
693 * @param int $level
694 * @param string $from
696 public static function debugging_triggered($message, $level, $from) {
697 // Store only if debugging triggered from actual test,
698 // we need normal debugging outside of tests to find problems in our phpunit integration.
699 $backtrace = debug_backtrace();
701 // Only for advanced_testcase, database_driver_testcase (and descendants). Others aren't
702 // able to manage the debugging sink, so any debugging has to be output normally and, hopefully,
703 // PHPUnit execution will catch that unexpected output properly.
704 $sinksupport = false;
705 foreach ($backtrace as $bt) {
706 if (isset($bt['object']) && is_object($bt['object'])
707 && (
708 $bt['object'] instanceof advanced_testcase ||
709 $bt['object'] instanceof database_driver_testcase)
711 $sinksupport = true;
712 break;
715 if (!$sinksupport) {
716 return false;
719 // Verify that we are inside a PHPUnit test (little bit redundant, because
720 // we already have checked above that this is an advanced/database_driver
721 // testcase, but let's keep things double safe for now).
722 foreach ($backtrace as $bt) {
723 if (isset($bt['object']) && is_object($bt['object'])
724 && $bt['object'] instanceof PHPUnit\Framework\TestCase) {
725 $debug = new stdClass();
726 $debug->message = $message;
727 $debug->level = $level;
728 $debug->from = $from;
730 self::$debuggings[] = $debug;
732 return true;
735 return false;
739 * Resets the list of debugging messages.
741 public static function reset_debugging() {
742 self::$debuggings = array();
743 set_debugging(DEBUG_DEVELOPER);
747 * Returns all debugging messages triggered during test.
748 * @return array with instances having message, level and stacktrace property.
750 public static function get_debugging_messages() {
751 return self::$debuggings;
755 * Prints out any debug messages accumulated during test execution.
757 * @param bool $return true to return the messages or false to print them directly. Default false.
758 * @return bool|string false if no debug messages, true if debug triggered or string of messages
760 public static function display_debugging_messages($return = false) {
761 if (empty(self::$debuggings)) {
762 return false;
765 $debugstring = '';
766 foreach(self::$debuggings as $debug) {
767 $debugstring .= 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
770 if ($return) {
771 return $debugstring;
773 echo $debugstring;
774 return true;
778 * Start message redirection.
780 * Note: Do not call directly from tests,
781 * use $sink = $this->redirectMessages() instead.
783 * @return phpunit_message_sink
785 public static function start_message_redirection() {
786 if (self::$messagesink) {
787 self::stop_message_redirection();
789 self::$messagesink = new phpunit_message_sink();
790 return self::$messagesink;
794 * End message redirection.
796 * Note: Do not call directly from tests,
797 * use $sink->close() instead.
799 public static function stop_message_redirection() {
800 self::$messagesink = null;
804 * Are messages redirected to some sink?
806 * Note: to be called from messagelib.php only!
808 * @return bool
810 public static function is_redirecting_messages() {
811 return !empty(self::$messagesink);
815 * To be called from messagelib.php only!
817 * @param stdClass $message record from messages table
818 * @return bool true means send message, false means message "sent" to sink.
820 public static function message_sent($message) {
821 if (self::$messagesink) {
822 self::$messagesink->add_message($message);
827 * Start phpmailer redirection.
829 * Note: Do not call directly from tests,
830 * use $sink = $this->redirectEmails() instead.
832 * @return phpunit_phpmailer_sink
834 public static function start_phpmailer_redirection() {
835 if (self::$phpmailersink) {
836 // If an existing mailer sink is active, just clear it.
837 self::$phpmailersink->clear();
838 } else {
839 self::$phpmailersink = new phpunit_phpmailer_sink();
841 return self::$phpmailersink;
845 * End phpmailer redirection.
847 * Note: Do not call directly from tests,
848 * use $sink->close() instead.
850 public static function stop_phpmailer_redirection() {
851 self::$phpmailersink = null;
855 * Are messages for phpmailer redirected to some sink?
857 * Note: to be called from moodle_phpmailer.php only!
859 * @return bool
861 public static function is_redirecting_phpmailer() {
862 return !empty(self::$phpmailersink);
866 * To be called from messagelib.php only!
868 * @param stdClass $message record from messages table
869 * @return bool true means send message, false means message "sent" to sink.
871 public static function phpmailer_sent($message) {
872 if (self::$phpmailersink) {
873 self::$phpmailersink->add_message($message);
878 * Start event redirection.
880 * @private
881 * Note: Do not call directly from tests,
882 * use $sink = $this->redirectEvents() instead.
884 * @return phpunit_event_sink
886 public static function start_event_redirection() {
887 if (self::$eventsink) {
888 self::stop_event_redirection();
890 self::$eventsink = new phpunit_event_sink();
891 return self::$eventsink;
895 * End event redirection.
897 * @private
898 * Note: Do not call directly from tests,
899 * use $sink->close() instead.
901 public static function stop_event_redirection() {
902 self::$eventsink = null;
906 * Are events redirected to some sink?
908 * Note: to be called from \core\event\base only!
910 * @private
911 * @return bool
913 public static function is_redirecting_events() {
914 return !empty(self::$eventsink);
918 * To be called from \core\event\base only!
920 * @private
921 * @param \core\event\base $event record from event_read table
922 * @return bool true means send event, false means event "sent" to sink.
924 public static function event_triggered(\core\event\base $event) {
925 if (self::$eventsink) {
926 self::$eventsink->add_event($event);
931 * Gets the name of the locale for testing environment (Australian English)
932 * depending on platform environment.
934 * @return string the locale name.
936 protected static function get_locale_name() {
937 global $CFG;
938 if ($CFG->ostype === 'WINDOWS') {
939 return 'English_Australia.1252';
940 } else {
941 return 'en_AU.UTF-8';
946 * Executes all adhoc tasks in the queue. Useful for testing asynchronous behaviour.
948 * @return void
950 public static function run_all_adhoc_tasks() {
951 $now = time();
952 while (($task = \core\task\manager::get_next_adhoc_task($now)) !== null) {
953 try {
954 $task->execute();
955 \core\task\manager::adhoc_task_complete($task);
956 } catch (Exception $e) {
957 \core\task\manager::adhoc_task_failed($task);
963 * Helper function to call a protected/private method of an object using reflection.
965 * Example 1. Calling a protected object method:
966 * $result = call_internal_method($myobject, 'method_name', [$param1, $param2], '\my\namespace\myobjectclassname');
968 * Example 2. Calling a protected static method:
969 * $result = call_internal_method(null, 'method_name', [$param1, $param2], '\my\namespace\myclassname');
971 * @param object|null $object the object on which to call the method, or null if calling a static method.
972 * @param string $methodname the name of the protected/private method.
973 * @param array $params the array of function params to pass to the method.
974 * @param string $classname the fully namespaced name of the class the object was created from (base in the case of mocks),
975 * or the name of the static class when calling a static method.
976 * @return mixed the respective return value of the method.
978 public static function call_internal_method($object, $methodname, array $params, $classname) {
979 $reflection = new \ReflectionClass($classname);
980 $method = $reflection->getMethod($methodname);
981 return $method->invokeArgs($object, $params);
985 * Pad the supplied string with $level levels of indentation.
987 * @param string $string The string to pad
988 * @param int $level The number of levels of indentation to pad
989 * @return string
991 protected static function pad(string $string, int $level): string {
992 return str_repeat(" ", $level * 2) . "{$string}\n";
996 * Get the coverage config for the supplied includelist and excludelist configuration.
998 * @param string[] $includelists The list of files/folders in the includelist.
999 * @param string[] $excludelists The list of files/folders in the excludelist.
1000 * @return string
1002 protected static function get_coverage_config(array $includelists, array $excludelists): string {
1003 $coverages = '';
1004 if (!empty($includelists)) {
1005 $coverages .= self::pad("<include>", 2);
1006 foreach ($includelists as $line) {
1007 $coverages .= self::pad($line, 3);
1009 $coverages .= self::pad("</include>", 2);
1010 if (!empty($excludelists)) {
1011 $coverages .= self::pad("<exclude>", 2);
1012 foreach ($excludelists as $line) {
1013 $coverages .= self::pad($line, 3);
1015 $coverages .= self::pad("</exclude>", 2);
1019 return $coverages;
1023 * Get the phpunit_coverage_info for the specified plugin or subsystem directory.
1025 * @param string $fulldir The directory to find the coverage info file in.
1026 * @return phpunit_coverage_info
1028 protected static function get_coverage_info(string $fulldir): phpunit_coverage_info {
1029 $coverageconfig = "{$fulldir}/tests/coverage.php";
1030 if (file_exists($coverageconfig)) {
1031 $coverageinfo = require($coverageconfig);
1032 if (!$coverageinfo instanceof phpunit_coverage_info) {
1033 throw new \coding_exception("{$coverageconfig} does not return a phpunit_coverage_info");
1036 return $coverageinfo;
1039 return new phpunit_coverage_info();;
1043 * Whether the current process is an isolated test process.
1045 * @return bool
1047 public static function is_in_isolated_process(): bool {
1048 // Note: There is no function to call, or much to go by in order to tell whether we are in an isolated process
1049 // during Bootstrap, when this function is called.
1050 // We can do so by testing the existence of the wrapper function, but there is nothing set until that point.
1051 return function_exists('__phpunit_run_isolated_test');