Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
26
use core\di;
27
use core\hook;
28
 
29
require_once(__DIR__.'/../../testing/classes/util.php');
30
require_once(__DIR__ . "/coverage_info.php");
31
 
32
/**
33
 * Collection of utility methods.
34
 *
35
 * @package    core
36
 * @category   phpunit
37
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class phpunit_util extends testing_util {
41
    /**
42
     * @var int last value of db writes counter, used for db resetting
43
     */
44
    public static $lastdbwrites = null;
45
 
46
    /** @var array An array of original globals, restored after each test */
47
    protected static $globals = array();
48
 
49
    /** @var array list of debugging messages triggered during the last test execution */
50
    protected static $debuggings = array();
51
 
52
    /** @var phpunit_message_sink alternative target for moodle messaging */
53
    protected static $messagesink = null;
54
 
55
    /** @var phpunit_phpmailer_sink alternative target for phpmailer messaging */
56
    protected static $phpmailersink = null;
57
 
58
    /** @var phpunit_message_sink alternative target for moodle messaging */
59
    protected static $eventsink = null;
60
 
61
    /**
62
     * @var array Files to skip when resetting dataroot folder
63
     */
64
    protected static $datarootskiponreset = array('.', '..', 'phpunittestdir.txt', 'phpunit', '.htaccess');
65
 
66
    /**
67
     * @var array Files to skip when dropping dataroot folder
68
     */
69
    protected static $datarootskipondrop = array('.', '..', 'lock');
70
 
71
    /**
72
     * Load global $CFG;
73
     * @internal
74
     * @static
75
     * @return void
76
     */
77
    public static function initialise_cfg() {
78
        global $DB;
79
        $dbhash = false;
80
        try {
81
            $dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'));
82
        } catch (Exception $e) {
83
            // not installed yet
84
            initialise_cfg();
85
            return;
86
        }
87
        if ($dbhash !== core_component::get_all_versions_hash()) {
88
            // do not set CFG - the only way forward is to drop and reinstall
89
            return;
90
        }
91
        // standard CFG init
92
        initialise_cfg();
93
    }
94
 
95
    /**
96
     * Reset contents of all database tables to initial values, reset caches, etc.
97
     *
98
     * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
99
     *
100
     * @static
101
     * @param bool $detectchanges
102
     *      true  - changes in global state and database are reported as errors
103
     *      false - no errors reported
104
     *      null  - only critical problems are reported as errors
105
     * @return void
106
     */
107
    public static function reset_all_data($detectchanges = false) {
108
        global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $FULLME, $FILTERLIB_PRIVATE;
109
 
110
        // Stop all hook redirections.
111
        di::get(hook\manager::class)->phpunit_stop_redirections();
112
 
113
        // Stop any message redirection.
114
        self::stop_message_redirection();
115
 
116
        // Stop any message redirection.
117
        self::stop_event_redirection();
118
 
119
        // Start a new email redirection.
120
        // This will clear any existing phpmailer redirection.
121
        // We redirect all phpmailer output to this message sink which is
122
        // called instead of phpmailer actually sending the message.
123
        self::start_phpmailer_redirection();
124
 
125
        // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
126
        // This accounted for 25% of the total time running phpunit - so we removed it.
127
 
128
        // Show any unhandled debugging messages, the runbare() could already reset it.
129
        self::display_debugging_messages();
130
        self::reset_debugging();
131
 
132
        // reset global $DB in case somebody mocked it
133
        $DB = self::get_global_backup('DB');
134
 
135
        if ($DB->is_transaction_started()) {
136
            // we can not reset inside transaction
137
            $DB->force_transaction_rollback();
138
        }
139
 
140
        $resetdb = self::reset_database();
141
        $localename = self::get_locale_name();
142
        $warnings = array();
143
 
144
        if ($detectchanges === true) {
145
            if ($resetdb) {
146
                $warnings[] = 'Warning: unexpected database modification, resetting DB state';
147
            }
148
 
149
            $oldcfg = self::get_global_backup('CFG');
150
            $oldsite = self::get_global_backup('SITE');
151
            foreach($CFG as $k=>$v) {
152
                if (!property_exists($oldcfg, $k)) {
153
                    $warnings[] = 'Warning: unexpected new $CFG->'.$k.' value';
154
                } else if ($oldcfg->$k !== $CFG->$k) {
155
                    $warnings[] = 'Warning: unexpected change of $CFG->'.$k.' value';
156
                }
157
                unset($oldcfg->$k);
158
 
159
            }
160
            if ($oldcfg) {
161
                foreach($oldcfg as $k=>$v) {
162
                    $warnings[] = 'Warning: unexpected removal of $CFG->'.$k;
163
                }
164
            }
165
 
166
            if ($USER->id != 0) {
167
                $warnings[] = 'Warning: unexpected change of $USER';
168
            }
169
 
170
            if ($COURSE->id != $oldsite->id) {
171
                $warnings[] = 'Warning: unexpected change of $COURSE';
172
            }
173
 
174
            if ($FULLME !== self::get_global_backup('FULLME')) {
175
                $warnings[] = 'Warning: unexpected change of $FULLME';
176
            }
177
 
178
            if (setlocale(LC_TIME, 0) !== $localename) {
179
                $warnings[] = 'Warning: unexpected change of locale';
180
            }
181
        }
182
 
183
        if (ini_get('max_execution_time') != 0) {
184
            // This is special warning for all resets because we do not want any
185
            // libraries to mess with timeouts unintentionally.
186
            // Our PHPUnit integration is not supposed to change it either.
187
 
188
            if ($detectchanges !== false) {
189
                $warnings[] = 'Warning: max_execution_time was changed to '.ini_get('max_execution_time');
190
            }
191
            set_time_limit(0);
192
        }
193
 
194
        // restore original globals
195
        $_SERVER = self::get_global_backup('_SERVER');
196
        $CFG = self::get_global_backup('CFG');
197
        $SITE = self::get_global_backup('SITE');
198
        $FULLME = self::get_global_backup('FULLME');
199
        $_GET = array();
200
        $_POST = array();
201
        $_FILES = array();
202
        $_REQUEST = array();
203
        $COURSE = $SITE;
204
 
205
        // reinitialise following globals
206
        $OUTPUT = new bootstrap_renderer();
207
        $PAGE = new moodle_page();
208
        $FULLME = null;
209
        $ME = null;
210
        $SCRIPT = null;
211
        $FILTERLIB_PRIVATE = null;
212
        if (!empty($SESSION->notifications)) {
213
            $SESSION->notifications = [];
214
        }
215
 
216
        // Empty sessison and set fresh new not-logged-in user.
217
        \core\session\manager::init_empty_session();
218
 
219
        // reset all static caches
220
        \core\event\manager::phpunit_reset();
221
        accesslib_clear_all_caches(true);
222
        accesslib_reset_role_cache();
223
        get_string_manager()->reset_caches(true);
224
        reset_text_filters_cache(true);
225
        get_message_processors(false, true, true);
226
        filter_manager::reset_caches();
227
        core_filetypes::reset_caches();
228
        \core_search\manager::clear_static();
229
        core_user::reset_caches();
230
        \core\output\icon_system::reset_caches();
231
        if (class_exists('core_media_manager', false)) {
232
            core_media_manager::reset_caches();
233
        }
234
 
235
        // Reset static unit test options.
236
        if (class_exists('\availability_date\condition', false)) {
237
            \availability_date\condition::set_current_time_for_test(0);
238
        }
239
 
240
        // Reset internal users.
241
        core_user::reset_internal_users();
242
 
243
        // Clear static caches in calendar container.
244
        if (class_exists('\core_calendar\local\event\container', false)) {
245
            core_calendar\local\event\container::reset_caches();
246
        }
247
 
248
        //TODO MDL-25290: add more resets here and probably refactor them to new core function
249
 
250
        // Reset course and module caches.
251
        core_courseformat\base::reset_course_cache(0);
252
        get_fast_modinfo(0, 0, true);
253
 
254
        // Reset other singletons.
255
        if (class_exists('core_plugin_manager')) {
256
            core_plugin_manager::reset_caches(true);
257
        }
258
        if (class_exists('\core\update\checker')) {
259
            \core\update\checker::reset_caches(true);
260
        }
261
        if (class_exists('\core_course\customfield\course_handler')) {
262
            \core_course\customfield\course_handler::reset_caches();
263
        }
264
        if (class_exists('\core_reportbuilder\manager')) {
265
            \core_reportbuilder\manager::reset_caches();
266
        }
267
        if (class_exists('\core_cohort\customfield\cohort_handler')) {
268
            \core_cohort\customfield\cohort_handler::reset_caches();
269
        }
270
        if (class_exists('\core_group\customfield\group_handler')) {
271
            \core_group\customfield\group_handler::reset_caches();
272
        }
273
        if (class_exists('\core_group\customfield\grouping_handler')) {
274
            \core_group\customfield\grouping_handler::reset_caches();
275
        }
276
 
277
        // Clear static cache within restore.
278
        if (class_exists('restore_section_structure_step')) {
279
            restore_section_structure_step::reset_caches();
280
        }
281
 
282
        // purge dataroot directory
283
        self::reset_dataroot();
284
 
285
        // restore original config once more in case resetting of caches changed CFG
286
        $CFG = self::get_global_backup('CFG');
287
 
288
        // inform data generator
289
        self::get_data_generator()->reset();
290
 
291
        // fix PHP settings
292
        error_reporting($CFG->debug);
293
 
294
        // Reset the date/time class.
295
        core_date::phpunit_reset();
296
 
297
        // Make sure the time locale is consistent - that is Australian English.
298
        setlocale(LC_TIME, $localename);
299
 
300
        // Reset the log manager cache.
301
        get_log_manager(true);
302
 
303
        // Reset user agent.
304
        core_useragent::instance(true, null);
305
 
306
        // Reset the DI container.
307
        \core\di::reset_container();
308
 
309
        // verify db writes just in case something goes wrong in reset
310
        if (self::$lastdbwrites != $DB->perf_get_writes()) {
311
            error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
312
            self::$lastdbwrites = $DB->perf_get_writes();
313
        }
314
 
315
        if ($warnings) {
316
            $warnings = implode("\n", $warnings);
317
            trigger_error($warnings, E_USER_WARNING);
318
        }
319
    }
320
 
321
    /**
322
     * Reset all database tables to default values.
323
     * @static
324
     * @return bool true if reset done, false if skipped
325
     */
326
    public static function reset_database() {
327
        global $DB;
328
 
329
        if (defined('PHPUNIT_ISOLATED_TEST') && PHPUNIT_ISOLATED_TEST && self::$lastdbwrites === null) {
330
            // This is an isolated test and the lastdbwrites has not yet been initialised.
331
            // Isolated test runs are reset by the test runner before the run starts.
332
            self::$lastdbwrites = $DB->perf_get_writes();
333
        }
334
 
335
        if (!is_null(self::$lastdbwrites) && self::$lastdbwrites == $DB->perf_get_writes()) {
336
            return false;
337
        }
338
 
339
        if (!parent::reset_database()) {
340
            return false;
341
        }
342
 
343
        self::$lastdbwrites = $DB->perf_get_writes();
344
 
345
        return true;
346
    }
347
 
348
    /**
349
     * Called during bootstrap only!
350
     * @internal
351
     * @static
352
     * @return void
353
     */
354
    public static function bootstrap_init() {
355
        global $CFG, $SITE, $DB, $FULLME;
356
 
357
        // backup the globals
358
        self::$globals['_SERVER'] = $_SERVER;
359
        self::$globals['CFG'] = clone($CFG);
360
        self::$globals['SITE'] = clone($SITE);
361
        self::$globals['DB'] = $DB;
362
        self::$globals['FULLME'] = $FULLME;
363
 
364
        // refresh data in all tables, clear caches, etc.
365
        self::reset_all_data();
366
    }
367
 
368
    /**
369
     * Print some Moodle related info to console.
370
     * @internal
371
     * @static
372
     * @return void
373
     */
374
    public static function bootstrap_moodle_info() {
375
        echo self::get_site_info();
376
    }
377
 
378
    /**
379
     * Returns original state of global variable.
380
     * @static
381
     * @param string $name
382
     * @return mixed
383
     */
384
    public static function get_global_backup($name) {
385
        if ($name === 'DB') {
386
            // no cloning of database object,
387
            // we just need the original reference, not original state
388
            return self::$globals['DB'];
389
        }
390
        if (isset(self::$globals[$name])) {
391
            if (is_object(self::$globals[$name])) {
392
                $return = clone(self::$globals[$name]);
393
                return $return;
394
            } else {
395
                return self::$globals[$name];
396
            }
397
        }
398
        return null;
399
    }
400
 
401
    /**
402
     * Is this site initialised to run unit tests?
403
     *
404
     * @static
405
     * @return int array errorcode=>message, 0 means ok
406
     */
407
    public static function testing_ready_problem() {
408
        global $DB;
409
 
410
        $localename = self::get_locale_name();
411
        if (setlocale(LC_TIME, $localename) === false) {
412
            return array(PHPUNIT_EXITCODE_CONFIGERROR, "Required locale '$localename' is not installed.");
413
        }
414
 
415
        if (!self::is_test_site()) {
416
            // dataroot was verified in bootstrap, so it must be DB
417
            return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
418
        }
419
 
420
        $tables = $DB->get_tables(false);
421
        if (empty($tables)) {
422
            return array(PHPUNIT_EXITCODE_INSTALL, '');
423
        }
424
 
425
        if (!self::is_test_data_updated()) {
426
            return array(PHPUNIT_EXITCODE_REINSTALL, '');
427
        }
428
 
429
        return array(0, '');
430
    }
431
 
432
    /**
433
     * Drop all test site data.
434
     *
435
     * Note: To be used from CLI scripts only.
436
     *
437
     * @static
438
     * @param bool $displayprogress if true, this method will echo progress information.
439
     * @return void may terminate execution with exit code
440
     */
441
    public static function drop_site($displayprogress = false) {
442
        global $DB, $CFG;
443
 
444
        if (!self::is_test_site()) {
445
            phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
446
        }
447
 
448
        // Purge dataroot
449
        if ($displayprogress) {
450
            echo "Purging dataroot:\n";
451
        }
452
 
453
        self::reset_dataroot();
454
        testing_initdataroot($CFG->dataroot, 'phpunit');
455
 
456
        // Drop all tables.
457
        self::drop_database($displayprogress);
458
 
459
        // Drop dataroot.
460
        self::drop_dataroot();
461
    }
462
 
463
    /**
464
     * Perform a fresh test site installation
465
     *
466
     * Note: To be used from CLI scripts only.
467
     *
468
     * @static
469
     * @return void may terminate execution with exit code
470
     */
471
    public static function install_site() {
472
        global $DB, $CFG;
473
 
474
        if (!self::is_test_site()) {
475
            phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
476
        }
477
 
478
        if ($DB->get_tables()) {
479
            list($errorcode, $message) = self::testing_ready_problem();
480
            if ($errorcode) {
481
                phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
482
            } else {
483
                phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
484
            }
485
        }
486
 
487
        $options = array();
488
        $options['adminpass'] = 'admin';
489
        $options['shortname'] = 'phpunit';
490
        $options['fullname'] = 'PHPUnit test site';
491
 
492
        install_cli_database($options, false);
493
 
494
        // Set the admin email address.
495
        $DB->set_field('user', 'email', 'admin@example.com', array('username' => 'admin'));
496
 
497
        // Disable all logging for performance and sanity reasons.
498
        set_config('enabled_stores', '', 'tool_log');
499
 
500
        // Remove any default blocked hosts and port restrictions, to avoid blocking tests (eg those using local files).
501
        set_config('curlsecurityblockedhosts', '');
502
        set_config('curlsecurityallowedport', '');
503
 
504
        // Execute all the adhoc tasks.
505
        while ($task = \core\task\manager::get_next_adhoc_task(time())) {
506
            $task->execute();
507
            \core\task\manager::adhoc_task_complete($task);
508
        }
509
 
510
        // We need to keep the installed dataroot filedir files.
511
        // So each time we reset the dataroot before running a test, the default files are still installed.
512
        self::save_original_data_files();
513
 
514
        // Store version hash in the database and in a file.
515
        self::store_versions_hash();
516
 
517
        // Store database data and structure.
518
        self::store_database_state();
519
    }
520
 
521
    /**
522
     * Builds dirroot/phpunit.xml file using defaults from /phpunit.xml.dist
523
     * @static
524
     * @return bool true means main config file created, false means only dataroot file created
525
     */
526
    public static function build_config_file() {
527
        global $CFG;
528
 
529
        $template = <<<EOF
530
            <testsuite name="@component@_testsuite">
531
              <directory suffix="_test.php">@dir@</directory>
532
            </testsuite>
533
 
534
        EOF;
535
        $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
536
 
537
        $suites = '';
538
        $includelists = [];
539
        $excludelists = [];
540
 
541
        $subsystems = core_component::get_core_subsystems();
542
        $subsystems['core'] = $CFG->dirroot . '/lib';
543
        foreach ($subsystems as $subsystem => $fulldir) {
544
            if (empty($fulldir)) {
545
                continue;
546
            }
547
            if (!file_exists("{$fulldir}/tests/")) {
548
                // There are no tests - skip this directory.
549
                continue;
550
            }
551
 
552
            $dir = substr($fulldir, strlen($CFG->dirroot) + 1);
553
            if ($coverageinfo = self::get_coverage_info($fulldir)) {
554
                $includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
555
                $excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
556
            }
557
        }
558
 
559
        $plugintypes = core_component::get_plugin_types();
560
        ksort($plugintypes);
561
        foreach (array_keys($plugintypes) as $type) {
562
            $plugs = core_component::get_plugin_list($type);
563
            ksort($plugs);
564
            foreach ($plugs as $plug => $plugindir) {
565
                if (!file_exists("{$plugindir}/tests/")) {
566
                    // There are no tests - skip this directory.
567
                    continue;
568
                }
569
 
570
                $dir = substr($plugindir, strlen($CFG->dirroot) + 1);
571
                $testdir = "{$dir}/tests";
572
                $component = "{$type}_{$plug}";
573
 
574
                $suite = str_replace('@component@', $component, $template);
575
                $suite = str_replace('@dir@', $testdir, $suite);
576
 
577
                $suites .= $suite;
578
 
579
                if ($coverageinfo = self::get_coverage_info($plugindir)) {
580
 
581
                    $includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
582
                    $excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
583
                }
584
            }
585
        }
586
 
587
        // Start a sequence between 100000 and 199000 to ensure each call to init produces
588
        // different ids in the database.  This reduces the risk that hard coded values will
589
        // end up being placed in phpunit or behat test code.
590
        $sequencestart = 100000 + mt_rand(0, 99) * 1000;
591
 
592
        $data = preg_replace('| *<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', trim($suites, "\n"), $data, 1);
593
        $data = str_replace(
594
            '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
595
            '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
596
            $data);
597
 
598
        $coverages = self::get_coverage_config($includelists, $excludelists);
599
        $data = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $data);
600
 
601
        $result = false;
602
        if (is_writable($CFG->dirroot)) {
603
            if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
604
                testing_fix_file_permissions("$CFG->dirroot/phpunit.xml");
605
            }
606
        }
607
 
608
        return (bool)$result;
609
    }
610
 
611
    /**
612
     * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
613
     *
614
     * @static
615
     * @return void, stops if can not write files
616
     */
617
    public static function build_component_config_files() {
618
        global $CFG;
619
 
620
        $template = <<<EOT
621
            <testsuites>
622
              <testsuite name="@component@_testsuite">
623
                <directory suffix="_test.php">.</directory>
624
              </testsuite>
625
            </testsuites>
626
          EOT;
627
        $coveragedefault = <<<EOT
628
            <include>
629
              <directory suffix=".php">.</directory>
630
            </include>
631
            <exclude>
632
              <directory suffix="_test.php">.</directory>
633
            </exclude>
634
        EOT;
635
 
636
        // Start a sequence between 100000 and 199000 to ensure each call to init produces
637
        // different ids in the database.  This reduces the risk that hard coded values will
638
        // end up being placed in phpunit or behat test code.
639
        $sequencestart = 100000 + mt_rand(0, 99) * 1000;
640
 
641
        // Use the upstream file as source for the distributed configurations
642
        $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
643
        $ftemplate = preg_replace('| *<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
644
 
645
        // Gets all the components with tests
646
        $components = tests_finder::get_components_with_tests('phpunit');
647
 
648
        // Create the corresponding phpunit.xml file for each component
649
        foreach ($components as $cname => $cpath) {
650
            // Calculate the component suite
651
            $ctemplate = $template;
652
            $ctemplate = str_replace('@component@', $cname, $ctemplate);
653
 
654
            $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
655
 
656
            // Check for coverage configurations.
657
            if ($coverageinfo = self::get_coverage_info($cpath)) {
658
                $coverages = self::get_coverage_config($coverageinfo->get_includelists(''), $coverageinfo->get_excludelists(''));
659
            } else {
660
                $coverages = $coveragedefault;
661
            }
662
            $fcontents = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $fcontents);
663
 
664
            // Apply it to the file template.
665
            $fcontents = str_replace(
666
                '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
667
                '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
668
                $fcontents);
669
 
670
            // fix link to schema
671
            $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/');
672
            $fcontents = str_replace('lib/phpunit/', str_repeat('../', $level).'lib/phpunit/', $fcontents);
673
 
674
            // Write the file
675
            $result = false;
676
            if (is_writable($cpath)) {
677
                if ($result = (bool)file_put_contents("$cpath/phpunit.xml", $fcontents)) {
678
                    testing_fix_file_permissions("$cpath/phpunit.xml");
679
                }
680
            }
681
            // Problems writing file, throw error
682
            if (!$result) {
683
                phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, "Can not create $cpath/phpunit.xml configuration file, verify dir permissions");
684
            }
685
        }
686
    }
687
 
688
    /**
689
     * To be called from debugging() only.
690
     * @param string $message
691
     * @param int $level
692
     * @param string $from
693
     */
694
    public static function debugging_triggered($message, $level, $from) {
695
        // Store only if debugging triggered from actual test,
696
        // we need normal debugging outside of tests to find problems in our phpunit integration.
697
        $backtrace = debug_backtrace();
698
 
699
        // Only for advanced_testcase, database_driver_testcase (and descendants). Others aren't
700
        // able to manage the debugging sink, so any debugging has to be output normally and, hopefully,
701
        // PHPUnit execution will catch that unexpected output properly.
702
        $sinksupport = false;
703
        foreach ($backtrace as $bt) {
704
            if (isset($bt['object']) && is_object($bt['object'])
705
                && (
706
                    $bt['object'] instanceof advanced_testcase ||
707
                    $bt['object'] instanceof database_driver_testcase)
708
            ) {
709
                $sinksupport = true;
710
                break;
711
            }
712
        }
713
        if (!$sinksupport) {
714
            return false;
715
        }
716
 
717
        // Verify that we are inside a PHPUnit test (little bit redundant, because
718
        // we already have checked above that this is an advanced/database_driver
719
        // testcase, but let's keep things double safe for now).
720
        foreach ($backtrace as $bt) {
721
            if (isset($bt['object']) && is_object($bt['object'])
722
                    && $bt['object'] instanceof PHPUnit\Framework\TestCase) {
723
                $debug = new stdClass();
724
                $debug->message = $message;
725
                $debug->level   = $level;
726
                $debug->from    = $from;
727
 
728
                self::$debuggings[] = $debug;
729
 
730
                return true;
731
            }
732
        }
733
        return false;
734
    }
735
 
736
    /**
737
     * Resets the list of debugging messages.
738
     */
739
    public static function reset_debugging() {
740
        self::$debuggings = array();
741
        set_debugging(DEBUG_DEVELOPER);
742
    }
743
 
744
    /**
745
     * Returns all debugging messages triggered during test.
746
     * @return array with instances having message, level and stacktrace property.
747
     */
748
    public static function get_debugging_messages() {
749
        return self::$debuggings;
750
    }
751
 
752
    /**
753
     * Prints out any debug messages accumulated during test execution.
754
     *
755
     * @param bool $return true to return the messages or false to print them directly. Default false.
756
     * @return bool|string false if no debug messages, true if debug triggered or string of messages
757
     */
758
    public static function display_debugging_messages($return = false) {
759
        if (empty(self::$debuggings)) {
760
            return false;
761
        }
762
 
763
        $debugstring = '';
764
        foreach(self::$debuggings as $debug) {
765
            $debugstring .= 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
766
        }
767
 
768
        if ($return) {
769
            return $debugstring;
770
        }
771
        echo $debugstring;
772
        return true;
773
    }
774
 
775
    /**
776
     * Start message redirection.
777
     *
778
     * Note: Do not call directly from tests,
779
     *       use $sink = $this->redirectMessages() instead.
780
     *
781
     * @return phpunit_message_sink
782
     */
783
    public static function start_message_redirection() {
784
        if (self::$messagesink) {
785
            self::stop_message_redirection();
786
        }
787
        self::$messagesink = new phpunit_message_sink();
788
        return self::$messagesink;
789
    }
790
 
791
    /**
792
     * End message redirection.
793
     *
794
     * Note: Do not call directly from tests,
795
     *       use $sink->close() instead.
796
     */
797
    public static function stop_message_redirection() {
798
        self::$messagesink = null;
799
    }
800
 
801
    /**
802
     * Are messages redirected to some sink?
803
     *
804
     * Note: to be called from messagelib.php only!
805
     *
806
     * @return bool
807
     */
808
    public static function is_redirecting_messages() {
809
        return !empty(self::$messagesink);
810
    }
811
 
812
    /**
813
     * To be called from messagelib.php only!
814
     *
815
     * @param stdClass $message record from messages table
816
     * @return bool true means send message, false means message "sent" to sink.
817
     */
818
    public static function message_sent($message) {
819
        if (self::$messagesink) {
820
            self::$messagesink->add_message($message);
821
        }
822
    }
823
 
824
    /**
825
     * Start phpmailer redirection.
826
     *
827
     * Note: Do not call directly from tests,
828
     *       use $sink = $this->redirectEmails() instead.
829
     *
830
     * @return phpunit_phpmailer_sink
831
     */
832
    public static function start_phpmailer_redirection() {
833
        if (self::$phpmailersink) {
834
            // If an existing mailer sink is active, just clear it.
835
            self::$phpmailersink->clear();
836
        } else {
837
            self::$phpmailersink = new phpunit_phpmailer_sink();
838
        }
839
        return self::$phpmailersink;
840
    }
841
 
842
    /**
843
     * End phpmailer redirection.
844
     *
845
     * Note: Do not call directly from tests,
846
     *       use $sink->close() instead.
847
     */
848
    public static function stop_phpmailer_redirection() {
849
        self::$phpmailersink = null;
850
    }
851
 
852
    /**
853
     * Are messages for phpmailer redirected to some sink?
854
     *
855
     * Note: to be called from moodle_phpmailer.php only!
856
     *
857
     * @return bool
858
     */
859
    public static function is_redirecting_phpmailer() {
860
        return !empty(self::$phpmailersink);
861
    }
862
 
863
    /**
864
     * To be called from messagelib.php only!
865
     *
866
     * @param stdClass $message record from messages table
867
     * @return bool true means send message, false means message "sent" to sink.
868
     */
869
    public static function phpmailer_sent($message) {
870
        if (self::$phpmailersink) {
871
            self::$phpmailersink->add_message($message);
872
        }
873
    }
874
 
875
    /**
876
     * Start event redirection.
877
     *
878
     * @private
879
     * Note: Do not call directly from tests,
880
     *       use $sink = $this->redirectEvents() instead.
881
     *
882
     * @return phpunit_event_sink
883
     */
884
    public static function start_event_redirection() {
885
        if (self::$eventsink) {
886
            self::stop_event_redirection();
887
        }
888
        self::$eventsink = new phpunit_event_sink();
889
        return self::$eventsink;
890
    }
891
 
892
    /**
893
     * End event redirection.
894
     *
895
     * @private
896
     * Note: Do not call directly from tests,
897
     *       use $sink->close() instead.
898
     */
899
    public static function stop_event_redirection() {
900
        self::$eventsink = null;
901
    }
902
 
903
    /**
904
     * Are events redirected to some sink?
905
     *
906
     * Note: to be called from \core\event\base only!
907
     *
908
     * @private
909
     * @return bool
910
     */
911
    public static function is_redirecting_events() {
912
        return !empty(self::$eventsink);
913
    }
914
 
915
    /**
916
     * To be called from \core\event\base only!
917
     *
918
     * @private
919
     * @param \core\event\base $event record from event_read table
920
     * @return bool true means send event, false means event "sent" to sink.
921
     */
922
    public static function event_triggered(\core\event\base $event) {
923
        if (self::$eventsink) {
924
            self::$eventsink->add_event($event);
925
        }
926
    }
927
 
928
    /**
929
     * Gets the name of the locale for testing environment (Australian English)
930
     * depending on platform environment.
931
     *
932
     * @return string the locale name.
933
     */
934
    protected static function get_locale_name() {
935
        global $CFG;
936
        if ($CFG->ostype === 'WINDOWS') {
937
            return 'English_Australia.1252';
938
        } else {
939
            return 'en_AU.UTF-8';
940
        }
941
    }
942
 
943
    /**
944
     * Executes all adhoc tasks in the queue. Useful for testing asynchronous behaviour.
945
     *
946
     * @return void
947
     */
948
    public static function run_all_adhoc_tasks() {
949
        $now = time();
950
        while (($task = \core\task\manager::get_next_adhoc_task($now)) !== null) {
951
            try {
952
                $task->execute();
953
                \core\task\manager::adhoc_task_complete($task);
954
            } catch (Exception $e) {
955
                \core\task\manager::adhoc_task_failed($task);
956
            }
957
        }
958
    }
959
 
960
    /**
961
     * Helper function to call a protected/private method of an object using reflection.
962
     *
963
     * Example 1. Calling a protected object method:
964
     *   $result = call_internal_method($myobject, 'method_name', [$param1, $param2], '\my\namespace\myobjectclassname');
965
     *
966
     * Example 2. Calling a protected static method:
967
     *   $result = call_internal_method(null, 'method_name', [$param1, $param2], '\my\namespace\myclassname');
968
     *
969
     * @param object|null $object the object on which to call the method, or null if calling a static method.
970
     * @param string $methodname the name of the protected/private method.
971
     * @param array $params the array of function params to pass to the method.
972
     * @param string $classname the fully namespaced name of the class the object was created from (base in the case of mocks),
973
     *        or the name of the static class when calling a static method.
974
     * @return mixed the respective return value of the method.
975
     */
976
    public static function call_internal_method($object, $methodname, array $params, $classname) {
977
        $reflection = new \ReflectionClass($classname);
978
        $method = $reflection->getMethod($methodname);
979
        return $method->invokeArgs($object, $params);
980
    }
981
 
982
    /**
983
     * Pad the supplied string with $level levels of indentation.
984
     *
985
     * @param   string  $string The string to pad
986
     * @param   int     $level The number of levels of indentation to pad
987
     * @return  string
988
     */
989
    protected static function pad(string $string, int $level): string {
990
        return str_repeat(" ", $level * 2) . "{$string}\n";
991
    }
992
 
993
    /**
994
     * Normalise any text to always use unix line endings (line-feeds).
995
     *
996
     * @param   string  $text The text to normalize
997
     * @return  string
998
     */
999
    public static function normalise_line_endings(string $text): string {
1000
        return str_replace(["\r\n", "\r"], "\n", $text);
1001
    }
1002
 
1003
    /**
1004
     * Get the coverage config for the supplied includelist and excludelist configuration.
1005
     *
1006
     * @param   string[] $includelists The list of files/folders in the includelist.
1007
     * @param   string[] $excludelists The list of files/folders in the excludelist.
1008
     * @return  string
1009
     */
1010
    protected static function get_coverage_config(array $includelists, array $excludelists): string {
1011
        $coverages = '';
1012
        if (!empty($includelists)) {
1013
            $coverages .= self::pad("<include>", 2);
1014
            foreach ($includelists as $line) {
1015
                $coverages .= self::pad($line, 3);
1016
            }
1017
            $coverages .= self::pad("</include>", 2);
1018
            if (!empty($excludelists)) {
1019
                $coverages .= self::pad("<exclude>", 2);
1020
                foreach ($excludelists as $line) {
1021
                    $coverages .= self::pad($line, 3);
1022
                }
1023
                $coverages .= self::pad("</exclude>", 2);
1024
            }
1025
        }
1026
 
1027
        return $coverages;
1028
    }
1029
 
1030
    /**
1031
     * Get the phpunit_coverage_info for the specified plugin or subsystem directory.
1032
     *
1033
     * @param   string  $fulldir The directory to find the coverage info file in.
1034
     * @return  phpunit_coverage_info
1035
     */
1036
    protected static function get_coverage_info(string $fulldir): phpunit_coverage_info {
1037
        $coverageconfig = "{$fulldir}/tests/coverage.php";
1038
        if (file_exists($coverageconfig)) {
1039
            $coverageinfo = require($coverageconfig);
1040
            if (!$coverageinfo instanceof phpunit_coverage_info) {
1041
                throw new \coding_exception("{$coverageconfig} does not return a phpunit_coverage_info");
1042
            }
1043
 
1044
            return $coverageinfo;
1045
        }
1046
 
1047
        return new phpunit_coverage_info();;
1048
    }
1049
 
1050
    /**
1051
     * Whether the current process is an isolated test process.
1052
     *
1053
     * @return bool
1054
     */
1055
    public static function is_in_isolated_process(): bool {
1056
        // Note: There is no function to call, or much to go by in order to tell whether we are in an isolated process
1057
        // during Bootstrap, when this function is called.
1058
        // We can do so by testing the existence of the wrapper function, but there is nothing set until that point.
1059
        return function_exists('__phpunit_run_isolated_test');
1060
    }
1061
}