~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/phpunit/classes/util.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
35
 */
36
36
class phpunit_util extends testing_util {
 
37
    /**
 
38
     * @var int last value of db writes counter, used for db resetting
 
39
     */
 
40
    public static $lastdbwrites = null;
 
41
 
37
42
    /** @var array An array of original globals, restored after each test */
38
43
    protected static $globals = array();
39
44
 
46
51
    /** @var phpunit_phpmailer_sink alternative target for phpmailer messaging */
47
52
    protected static $phpmailersink = null;
48
53
 
 
54
    /** @var phpunit_message_sink alternative target for moodle messaging */
 
55
    protected static $eventsink = null;
 
56
 
49
57
    /**
50
58
     * @var array Files to skip when resetting dataroot folder
51
59
     */
72
80
            initialise_cfg();
73
81
            return;
74
82
        }
75
 
        if ($dbhash !== self::get_version_hash()) {
 
83
        if ($dbhash !== core_component::get_all_versions_hash()) {
76
84
            // do not set CFG - the only way forward is to drop and reinstall
77
85
            return;
78
86
        }
101
109
        // Stop any message redirection.
102
110
        phpunit_util::stop_phpmailer_redirection();
103
111
 
 
112
        // Stop any message redirection.
 
113
        phpunit_util::stop_event_redirection();
 
114
 
104
115
        // Release memory and indirectly call destroy() methods to release resource handles, etc.
105
116
        gc_collect_cycles();
106
117
 
185
196
        $user = new stdClass();
186
197
        $user->id = 0;
187
198
        $user->mnethostid = $CFG->mnet_localhost_id;
188
 
        session_set_user($user);
 
199
        \core\session\manager::set_user($user);
189
200
 
190
201
        // reset all static caches
 
202
        \core\event\manager::phpunit_reset();
191
203
        accesslib_clear_all_caches(true);
192
204
        get_string_manager()->reset_caches(true);
193
205
        reset_text_filters_cache(true);
194
206
        events_get_handlers('reset');
195
 
        textlib::reset_caches();
 
207
        core_text::reset_caches();
196
208
        get_message_processors(false, true);
197
 
        if (class_exists('repository')) {
198
 
            repository::reset_caches();
199
 
        }
200
209
        filter_manager::reset_caches();
201
210
        //TODO MDL-25290: add more resets here and probably refactor them to new core function
202
211
 
208
217
        get_fast_modinfo(0, 0, true);
209
218
 
210
219
        // Reset other singletons.
211
 
        if (class_exists('plugin_manager')) {
212
 
            plugin_manager::reset_caches(true);
213
 
        }
214
 
        if (class_exists('available_update_checker')) {
215
 
            available_update_checker::reset_caches(true);
216
 
        }
217
 
        if (class_exists('available_update_deployer')) {
218
 
            available_update_deployer::reset_caches(true);
 
220
        if (class_exists('core_plugin_manager')) {
 
221
            core_plugin_manager::reset_caches(true);
 
222
        }
 
223
        if (class_exists('\core\update\checker')) {
 
224
            \core\update\checker::reset_caches(true);
 
225
        }
 
226
        if (class_exists('\core\update\deployer')) {
 
227
            \core\update\deployer::reset_caches(true);
219
228
        }
220
229
 
221
230
        // purge dataroot directory
243
252
    }
244
253
 
245
254
    /**
 
255
     * Reset all database tables to default values.
 
256
     * @static
 
257
     * @return bool true if reset done, false if skipped
 
258
     */
 
259
    public static function reset_database() {
 
260
        global $DB;
 
261
 
 
262
        if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) {
 
263
            return false;
 
264
        }
 
265
 
 
266
        if (!parent::reset_database()) {
 
267
            return false;
 
268
        }
 
269
 
 
270
        self::$lastdbwrites = $DB->perf_get_writes();
 
271
 
 
272
        return true;
 
273
    }
 
274
 
 
275
    /**
246
276
     * Called during bootstrap only!
247
277
     * @internal
248
278
     * @static
380
410
 
381
411
        install_cli_database($options, false);
382
412
 
 
413
        // We need to keep the installed dataroot filedir files.
 
414
        // So each time we reset the dataroot before running a test, the default files are still installed.
 
415
        self::save_original_data_files();
 
416
 
383
417
        // install timezone info
384
418
        $timezones = get_records_csv($CFG->libdir.'/timezone.txt', 'timezone');
385
419
        update_timezone_records($timezones);
407
441
 
408
442
        $suites = '';
409
443
 
410
 
        $plugintypes = get_plugin_types();
 
444
        $plugintypes = core_component::get_plugin_types();
411
445
        ksort($plugintypes);
412
446
        foreach ($plugintypes as $type=>$unused) {
413
 
            $plugs = get_plugin_list($type);
 
447
            $plugs = core_component::get_plugin_list($type);
414
448
            ksort($plugs);
415
449
            foreach ($plugs as $plug=>$fullplug) {
416
450
                if (!file_exists("$fullplug/tests/")) {
538
572
     */
539
573
    public static function reset_debugging() {
540
574
        self::$debuggings = array();
 
575
        set_debugging(DEBUG_DEVELOPER);
541
576
    }
542
577
 
543
578
    /**
660
695
            self::$phpmailersink->add_message($message);
661
696
        }
662
697
    }
 
698
 
 
699
    /**
 
700
     * Start event redirection.
 
701
     *
 
702
     * @private
 
703
     * Note: Do not call directly from tests,
 
704
     *       use $sink = $this->redirectEvents() instead.
 
705
     *
 
706
     * @return phpunit_event_sink
 
707
     */
 
708
    public static function start_event_redirection() {
 
709
        if (self::$eventsink) {
 
710
            self::stop_event_redirection();
 
711
        }
 
712
        self::$eventsink = new phpunit_event_sink();
 
713
        return self::$eventsink;
 
714
    }
 
715
 
 
716
    /**
 
717
     * End event redirection.
 
718
     *
 
719
     * @private
 
720
     * Note: Do not call directly from tests,
 
721
     *       use $sink->close() instead.
 
722
     */
 
723
    public static function stop_event_redirection() {
 
724
        self::$eventsink = null;
 
725
    }
 
726
 
 
727
    /**
 
728
     * Are events redirected to some sink?
 
729
     *
 
730
     * Note: to be called from \core\event\base only!
 
731
     *
 
732
     * @private
 
733
     * @return bool
 
734
     */
 
735
    public static function is_redirecting_events() {
 
736
        return !empty(self::$eventsink);
 
737
    }
 
738
 
 
739
    /**
 
740
     * To be called from \core\event\base only!
 
741
     *
 
742
     * @private
 
743
     * @param \core\event\base $event record from event_read table
 
744
     * @return bool true means send event, false means event "sent" to sink.
 
745
     */
 
746
    public static function event_triggered(\core\event\base $event) {
 
747
        if (self::$eventsink) {
 
748
            self::$eventsink->add_event($event);
 
749
        }
 
750
    }
663
751
}