Proyectos de Subversion Moodle

Rev

Rev 1 | | 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();
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>
11 efrain 532
              <exclude>@dir@/classes</exclude>
1 efrain 533
            </testsuite>
534
 
535
        EOF;
536
        $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
537
 
538
        $suites = '';
539
        $includelists = [];
540
        $excludelists = [];
541
 
542
        $subsystems = core_component::get_core_subsystems();
543
        $subsystems['core'] = $CFG->dirroot . '/lib';
544
        foreach ($subsystems as $subsystem => $fulldir) {
545
            if (empty($fulldir)) {
546
                continue;
547
            }
548
            if (!file_exists("{$fulldir}/tests/")) {
549
                // There are no tests - skip this directory.
550
                continue;
551
            }
552
 
553
            $dir = substr($fulldir, strlen($CFG->dirroot) + 1);
554
            if ($coverageinfo = self::get_coverage_info($fulldir)) {
555
                $includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
556
                $excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
557
            }
558
        }
559
 
560
        $plugintypes = core_component::get_plugin_types();
561
        ksort($plugintypes);
562
        foreach (array_keys($plugintypes) as $type) {
563
            $plugs = core_component::get_plugin_list($type);
564
            ksort($plugs);
565
            foreach ($plugs as $plug => $plugindir) {
566
                if (!file_exists("{$plugindir}/tests/")) {
567
                    // There are no tests - skip this directory.
568
                    continue;
569
                }
570
 
571
                $dir = substr($plugindir, strlen($CFG->dirroot) + 1);
572
                $testdir = "{$dir}/tests";
573
                $component = "{$type}_{$plug}";
574
 
575
                $suite = str_replace('@component@', $component, $template);
576
                $suite = str_replace('@dir@', $testdir, $suite);
577
 
578
                $suites .= $suite;
579
 
580
                if ($coverageinfo = self::get_coverage_info($plugindir)) {
581
 
582
                    $includelists = array_merge($includelists, $coverageinfo->get_includelists($dir));
583
                    $excludelists = array_merge($excludelists, $coverageinfo->get_excludelists($dir));
584
                }
585
            }
586
        }
587
 
588
        // Start a sequence between 100000 and 199000 to ensure each call to init produces
589
        // different ids in the database.  This reduces the risk that hard coded values will
590
        // end up being placed in phpunit or behat test code.
591
        $sequencestart = 100000 + mt_rand(0, 99) * 1000;
592
 
593
        $data = preg_replace('| *<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', trim($suites, "\n"), $data, 1);
594
        $data = str_replace(
595
            '<const name="PHPUNIT_SEQUENCE_START" value=""/>',
596
            '<const name="PHPUNIT_SEQUENCE_START" value="' . $sequencestart . '"/>',
597
            $data);
598
 
599
        $coverages = self::get_coverage_config($includelists, $excludelists);
600
        $data = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $data);
601
 
602
        $result = false;
603
        if (is_writable($CFG->dirroot)) {
604
            if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
605
                testing_fix_file_permissions("$CFG->dirroot/phpunit.xml");
606
            }
607
        }
608
 
609
        return (bool)$result;
610
    }
611
 
612
    /**
613
     * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
614
     *
615
     * @static
616
     * @return void, stops if can not write files
617
     */
618
    public static function build_component_config_files() {
619
        global $CFG;
620
 
621
        $template = <<<EOT
622
            <testsuites>
623
              <testsuite name="@component@_testsuite">
624
                <directory suffix="_test.php">.</directory>
11 efrain 625
                <exclude>./classes</exclude>
1 efrain 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;
637
 
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;
642
 
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);
646
 
647
        // Gets all the components with tests
648
        $components = tests_finder::get_components_with_tests('phpunit');
649
 
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);
655
 
656
            $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
657
 
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;
663
            }
664
            $fcontents = preg_replace('| *<!--@coveragelist@-->|s', trim($coverages, "\n"), $fcontents);
665
 
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);
671
 
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);
675
 
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");
681
                }
682
            }
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");
686
            }
687
        }
688
    }
689
 
690
    /**
691
     * To be called from debugging() only.
692
     * @param string $message
693
     * @param int $level
694
     * @param string $from
695
     */
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();
700
 
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)
710
            ) {
711
                $sinksupport = true;
712
                break;
713
            }
714
        }
715
        if (!$sinksupport) {
716
            return false;
717
        }
718
 
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;
729
 
730
                self::$debuggings[] = $debug;
731
 
732
                return true;
733
            }
734
        }
735
        return false;
736
    }
737
 
738
    /**
739
     * Resets the list of debugging messages.
740
     */
741
    public static function reset_debugging() {
742
        self::$debuggings = array();
743
        set_debugging(DEBUG_DEVELOPER);
744
    }
745
 
746
    /**
747
     * Returns all debugging messages triggered during test.
748
     * @return array with instances having message, level and stacktrace property.
749
     */
750
    public static function get_debugging_messages() {
751
        return self::$debuggings;
752
    }
753
 
754
    /**
755
     * Prints out any debug messages accumulated during test execution.
756
     *
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
759
     */
760
    public static function display_debugging_messages($return = false) {
761
        if (empty(self::$debuggings)) {
762
            return false;
763
        }
764
 
765
        $debugstring = '';
766
        foreach(self::$debuggings as $debug) {
767
            $debugstring .= 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
768
        }
769
 
770
        if ($return) {
771
            return $debugstring;
772
        }
773
        echo $debugstring;
774
        return true;
775
    }
776
 
777
    /**
778
     * Start message redirection.
779
     *
780
     * Note: Do not call directly from tests,
781
     *       use $sink = $this->redirectMessages() instead.
782
     *
783
     * @return phpunit_message_sink
784
     */
785
    public static function start_message_redirection() {
786
        if (self::$messagesink) {
787
            self::stop_message_redirection();
788
        }
789
        self::$messagesink = new phpunit_message_sink();
790
        return self::$messagesink;
791
    }
792
 
793
    /**
794
     * End message redirection.
795
     *
796
     * Note: Do not call directly from tests,
797
     *       use $sink->close() instead.
798
     */
799
    public static function stop_message_redirection() {
800
        self::$messagesink = null;
801
    }
802
 
803
    /**
804
     * Are messages redirected to some sink?
805
     *
806
     * Note: to be called from messagelib.php only!
807
     *
808
     * @return bool
809
     */
810
    public static function is_redirecting_messages() {
811
        return !empty(self::$messagesink);
812
    }
813
 
814
    /**
815
     * To be called from messagelib.php only!
816
     *
817
     * @param stdClass $message record from messages table
818
     * @return bool true means send message, false means message "sent" to sink.
819
     */
820
    public static function message_sent($message) {
821
        if (self::$messagesink) {
822
            self::$messagesink->add_message($message);
823
        }
824
    }
825
 
826
    /**
827
     * Start phpmailer redirection.
828
     *
829
     * Note: Do not call directly from tests,
830
     *       use $sink = $this->redirectEmails() instead.
831
     *
832
     * @return phpunit_phpmailer_sink
833
     */
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();
840
        }
841
        return self::$phpmailersink;
842
    }
843
 
844
    /**
845
     * End phpmailer redirection.
846
     *
847
     * Note: Do not call directly from tests,
848
     *       use $sink->close() instead.
849
     */
850
    public static function stop_phpmailer_redirection() {
851
        self::$phpmailersink = null;
852
    }
853
 
854
    /**
855
     * Are messages for phpmailer redirected to some sink?
856
     *
857
     * Note: to be called from moodle_phpmailer.php only!
858
     *
859
     * @return bool
860
     */
861
    public static function is_redirecting_phpmailer() {
862
        return !empty(self::$phpmailersink);
863
    }
864
 
865
    /**
866
     * To be called from messagelib.php only!
867
     *
868
     * @param stdClass $message record from messages table
869
     * @return bool true means send message, false means message "sent" to sink.
870
     */
871
    public static function phpmailer_sent($message) {
872
        if (self::$phpmailersink) {
873
            self::$phpmailersink->add_message($message);
874
        }
875
    }
876
 
877
    /**
878
     * Start event redirection.
879
     *
880
     * @private
881
     * Note: Do not call directly from tests,
882
     *       use $sink = $this->redirectEvents() instead.
883
     *
884
     * @return phpunit_event_sink
885
     */
886
    public static function start_event_redirection() {
887
        if (self::$eventsink) {
888
            self::stop_event_redirection();
889
        }
890
        self::$eventsink = new phpunit_event_sink();
891
        return self::$eventsink;
892
    }
893
 
894
    /**
895
     * End event redirection.
896
     *
897
     * @private
898
     * Note: Do not call directly from tests,
899
     *       use $sink->close() instead.
900
     */
901
    public static function stop_event_redirection() {
902
        self::$eventsink = null;
903
    }
904
 
905
    /**
906
     * Are events redirected to some sink?
907
     *
908
     * Note: to be called from \core\event\base only!
909
     *
910
     * @private
911
     * @return bool
912
     */
913
    public static function is_redirecting_events() {
914
        return !empty(self::$eventsink);
915
    }
916
 
917
    /**
918
     * To be called from \core\event\base only!
919
     *
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.
923
     */
924
    public static function event_triggered(\core\event\base $event) {
925
        if (self::$eventsink) {
926
            self::$eventsink->add_event($event);
927
        }
928
    }
929
 
930
    /**
931
     * Gets the name of the locale for testing environment (Australian English)
932
     * depending on platform environment.
933
     *
934
     * @return string the locale name.
935
     */
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';
942
        }
943
    }
944
 
945
    /**
946
     * Executes all adhoc tasks in the queue. Useful for testing asynchronous behaviour.
947
     *
948
     * @return void
949
     */
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);
958
            }
959
        }
960
    }
961
 
962
    /**
963
     * Helper function to call a protected/private method of an object using reflection.
964
     *
965
     * Example 1. Calling a protected object method:
966
     *   $result = call_internal_method($myobject, 'method_name', [$param1, $param2], '\my\namespace\myobjectclassname');
967
     *
968
     * Example 2. Calling a protected static method:
969
     *   $result = call_internal_method(null, 'method_name', [$param1, $param2], '\my\namespace\myclassname');
970
     *
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.
977
     */
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);
982
    }
983
 
984
    /**
985
     * Pad the supplied string with $level levels of indentation.
986
     *
987
     * @param   string  $string The string to pad
988
     * @param   int     $level The number of levels of indentation to pad
989
     * @return  string
990
     */
991
    protected static function pad(string $string, int $level): string {
992
        return str_repeat(" ", $level * 2) . "{$string}\n";
993
    }
994
 
995
    /**
996
     * Normalise any text to always use unix line endings (line-feeds).
997
     *
998
     * @param   string  $text The text to normalize
999
     * @return  string
1000
     */
1001
    public static function normalise_line_endings(string $text): string {
1002
        return str_replace(["\r\n", "\r"], "\n", $text);
1003
    }
1004
 
1005
    /**
1006
     * Get the coverage config for the supplied includelist and excludelist configuration.
1007
     *
1008
     * @param   string[] $includelists The list of files/folders in the includelist.
1009
     * @param   string[] $excludelists The list of files/folders in the excludelist.
1010
     * @return  string
1011
     */
1012
    protected static function get_coverage_config(array $includelists, array $excludelists): string {
1013
        $coverages = '';
1014
        if (!empty($includelists)) {
1015
            $coverages .= self::pad("<include>", 2);
1016
            foreach ($includelists as $line) {
1017
                $coverages .= self::pad($line, 3);
1018
            }
1019
            $coverages .= self::pad("</include>", 2);
1020
            if (!empty($excludelists)) {
1021
                $coverages .= self::pad("<exclude>", 2);
1022
                foreach ($excludelists as $line) {
1023
                    $coverages .= self::pad($line, 3);
1024
                }
1025
                $coverages .= self::pad("</exclude>", 2);
1026
            }
1027
        }
1028
 
1029
        return $coverages;
1030
    }
1031
 
1032
    /**
1033
     * Get the phpunit_coverage_info for the specified plugin or subsystem directory.
1034
     *
1035
     * @param   string  $fulldir The directory to find the coverage info file in.
1036
     * @return  phpunit_coverage_info
1037
     */
1038
    protected static function get_coverage_info(string $fulldir): phpunit_coverage_info {
1039
        $coverageconfig = "{$fulldir}/tests/coverage.php";
1040
        if (file_exists($coverageconfig)) {
1041
            $coverageinfo = require($coverageconfig);
1042
            if (!$coverageinfo instanceof phpunit_coverage_info) {
1043
                throw new \coding_exception("{$coverageconfig} does not return a phpunit_coverage_info");
1044
            }
1045
 
1046
            return $coverageinfo;
1047
        }
1048
 
1049
        return new phpunit_coverage_info();;
1050
    }
1051
 
1052
    /**
1053
     * Whether the current process is an isolated test process.
1054
     *
1055
     * @return bool
1056
     */
1057
    public static function is_in_isolated_process(): bool {
1058
        // Note: There is no function to call, or much to go by in order to tell whether we are in an isolated process
1059
        // during Bootstrap, when this function is called.
1060
        // We can do so by testing the existence of the wrapper function, but there is nothing set until that point.
1061
        return function_exists('__phpunit_run_isolated_test');
1062
    }
1063
}