Proyectos de Subversion Moodle

Rev

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