/**
* Reset contents of all database tables to initial values, reset caches, etc.
*
* Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
*
* @static
* @param bool $detectchanges
* true - changes in global state and database are reported as errors
* false - no errors reported
* null - only critical problems are reported as errors
* @return void
*/
public static function reset_all_data($detectchanges = false)
{
global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
// Stop any message redirection.
phpunit_util::stop_message_redirection();
// Stop any message redirection.
phpunit_util::stop_phpmailer_redirection();
// Stop any message redirection.
phpunit_util::stop_event_redirection();
// We used to call gc_collect_cycles here to ensure desctructors were called between tests.
// This accounted for 25% of the total time running phpunit - so we removed it.
// Show any unhandled debugging messages, the runbare() could already reset it.
self::display_debugging_messages();
self::reset_debugging();
// reset global $DB in case somebody mocked it
$DB = self::get_global_backup('DB');
if ($DB->is_transaction_started()) {
// we can not reset inside transaction
$DB->force_transaction_rollback();
}
$resetdb = self::reset_database();
$warnings = array();
if ($detectchanges === true) {
if ($resetdb) {
$warnings[] = 'Warning: unexpected database modification, resetting DB state';
}
$oldcfg = self::get_global_backup('CFG');
$oldsite = self::get_global_backup('SITE');
foreach ($CFG as $k => $v) {
if (!property_exists($oldcfg, $k)) {
$warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
} else {
if ($oldcfg->{$k} !== $CFG->{$k}) {
$warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
}
}
unset($oldcfg->{$k});
}
if ($oldcfg) {
foreach ($oldcfg as $k => $v) {
$warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
}
}
if ($USER->id != 0) {
$warnings[] = 'Warning: unexpected change of $USER';
}
if ($COURSE->id != $oldsite->id) {
$warnings[] = 'Warning: unexpected change of $COURSE';
}
}
if (ini_get('max_execution_time') != 0) {
// This is special warning for all resets because we do not want any
// libraries to mess with timeouts unintentionally.
// Our PHPUnit integration is not supposed to change it either.
if ($detectchanges !== false) {
$warnings[] = 'Warning: max_execution_time was changed to ' . ini_get('max_execution_time');
}
set_time_limit(0);
}
// restore original globals
$_SERVER = self::get_global_backup('_SERVER');
$CFG = self::get_global_backup('CFG');
$SITE = self::get_global_backup('SITE');
$_GET = array();
$_POST = array();
$_FILES = array();
$_REQUEST = array();
$COURSE = $SITE;
// reinitialise following globals
$OUTPUT = new bootstrap_renderer();
$PAGE = new moodle_page();
$FULLME = null;
$ME = null;
$SCRIPT = null;
// Empty sessison and set fresh new not-logged-in user.
\core\session\manager::init_empty_session();
// reset all static caches
\core\event\manager::phpunit_reset();
accesslib_clear_all_caches(true);
get_string_manager()->reset_caches(true);
reset_text_filters_cache(true);
events_get_handlers('reset');
core_text::reset_caches();
get_message_processors(false, true);
filter_manager::reset_caches();
// Reset internal users.
core_user::reset_internal_users();
//TODO MDL-25290: add more resets here and probably refactor them to new core function
//.........这里部分代码省略.........
/**
* Helper method to apply filters to some text and return the result.
* @param string $text the text to filter.
* @param array $skipfilters any filters not to apply, even if they are configured.
* @return string the filtered text.
*/
protected function filter_text($text, $skipfilters)
{
global $PAGE;
$filtermanager = filter_manager::instance();
$filtermanager->setup_page_for_filters($PAGE, $PAGE->context);
$filteroptions = array('originalformat' => FORMAT_HTML, 'noclean' => false);
return $filtermanager->filter_text($text, $PAGE->context, $filteroptions, $skipfilters);
}
/**
* Given some text in HTML format, this function will pass it
* through any filters that have been configured for this context.
*
* @deprecated use the text formatting in a standard way instead,
* this was abused mostly for embedding of attachments
*
* @param string $text The text to be passed through format filters
* @param int $courseid The current course.
* @return string the filtered string.
*/
function filter_text($text, $courseid = NULL)
{
global $CFG, $COURSE;
if (!$courseid) {
$courseid = $COURSE->id;
}
if (!($context = context_course::instance($courseid, IGNORE_MISSING))) {
return $text;
}
return filter_manager::instance()->filter_text($text, $context);
}
/**
* Given some text in HTML format, this function will pass it
* through any filters that have been configured for this context.
*
* @deprecated use the text formatting in a standard way instead,
* this was abused mostly for embedding of attachments
*
* @param string $text The text to be passed through format filters
* @param int $courseid The current course.
* @return string the filtered string.
*/
function filter_text($text, $courseid = NULL)
{
global $CFG, $COURSE;
if (!$courseid) {
$courseid = $COURSE->id;
}
if (!($context = get_context_instance(CONTEXT_COURSE, $courseid))) {
return $text;
}
return filter_manager::instance()->filter_text($text, $context);
}
/**
* Given some text in HTML format, this function will pass it
* through any filters that have been configured for this context.
*
* @param string $text The text to be passed through format filters
* @param int $courseid The current course.
* @return string the filtered string.
*/
function filter_text($text, $courseid = NULL)
{
global $CFG, $COURSE, $PAGE;
if (empty($courseid)) {
$courseid = $COURSE->id;
// (copied from format_text)
}
$context = $PAGE->context;
return filter_manager::instance()->filter_text($text, $context, $courseid);
}
/**
* Given a simple string, this function returns the string
* processed by enabled string filters if $CFG->filterall is enabled
*
* This function should be used to print short strings (non html) that
* need filter processing e.g. activity titles, post subjects,
* glossary concepts.
*
* @global object
* @global object
* @global object
* @staticvar bool $strcache
* @param string $string The string to be filtered.
* @param boolean $striplinks To strip any link in the result text.
Moodle 1.8 default changed from false to true! MDL-8713
* @param array $options options array/object or courseid
* @return string
*/
function format_string($string, $striplinks = true, $options = NULL)
{
global $CFG, $COURSE, $PAGE;
//We'll use a in-memory cache here to speed up repeated strings
static $strcache = false;
if (empty($CFG->version) or $CFG->version < 2010072800 or during_initial_install()) {
// do not filter anything during installation or before upgrade completes
return $string = strip_tags($string);
}
if ($strcache === false or count($strcache) > 2000) {
// this number might need some tuning to limit memory usage in cron
$strcache = array();
}
if (is_numeric($options)) {
// legacy courseid usage
$options = array('context' => get_context_instance(CONTEXT_COURSE, $options));
} else {
$options = (array) $options;
// detach object, we can not modify it
}
if (empty($options['context'])) {
// fallback to $PAGE->context this may be problematic in CLI and other non-standard pages :-(
$options['context'] = $PAGE->context;
} else {
if (is_numeric($options['context'])) {
$options['context'] = get_context_instance_by_id($options['context']);
}
}
if (!$options['context']) {
// we did not find any context? weird
return $string = strip_tags($string);
}
//Calculate md5
$md5 = md5($string . '<+>' . $striplinks . '<+>' . $options['context']->id . '<+>' . current_language());
//Fetch from cache if possible
if (isset($strcache[$md5])) {
return $strcache[$md5];
}
// First replace all ampersands not followed by html entity code
// Regular expression moved to its own method for easier unit testing
$string = replace_ampersands_not_followed_by_entity($string);
if (!empty($CFG->filterall)) {
$string = filter_manager::instance()->filter_string($string, $options['context']);
}
// If the site requires it, strip ALL tags from this string
if (!empty($CFG->formatstringstriptags)) {
$string = strip_tags($string);
} else {
// Otherwise strip just links if that is required (default)
if ($striplinks) {
//strip links in string
$string = strip_links($string);
}
$string = clean_text($string);
}
//Store to cache
$strcache[$md5] = $string;
return $string;
}
开发者ID:hatone,项目名称:moodle,代码行数:77,代码来源:weblib.php
示例15: initialise_theme_and_output
/**
* Method for use by Moodle core to set up the theme. Do not
* use this in your own code.
*
* Make sure the right theme for this page is loaded. Tell our
* blocks_manager about the theme block regions, and then, if
* we are $PAGE, set up the global $OUTPUT.
*
* @return void
*/
public function initialise_theme_and_output()
{
global $OUTPUT, $PAGE, $SITE, $CFG;
if (!empty($this->_wherethemewasinitialised)) {
return;
}
if (!during_initial_install()) {
// Detect PAGE->context mess.
$this->magic_get_context();
}
if (!$this->_course && !during_initial_install()) {
$this->set_course($SITE);
}
if (is_null($this->_theme)) {
$themename = $this->resolve_theme();
$this->_theme = theme_config::load($themename);
}
$this->_theme->setup_blocks($this->pagelayout, $this->blocks);
if ($this->_theme->enable_dock && !empty($CFG->allowblockstodock)) {
$this->requires->strings_for_js(array('addtodock', 'undockitem', 'dockblock', 'undockblock', 'undockall', 'hidedockpanel', 'hidepanel'), 'block');
$this->requires->string_for_js('thisdirectionvertical', 'langconfig');
$this->requires->yui_module('moodle-core-dock-loader', 'M.core.dock.loader.initLoader');
}
if ($this === $PAGE) {
$target = null;
if ($this->pagelayout === 'maintenance') {
// If the page is using the maintenance layout then we're going to force target to maintenance.
// This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
// page layout.
$target = RENDERER_TARGET_MAINTENANCE;
}
$OUTPUT = $this->get_renderer('core', null, $target);
}
if (!during_initial_install()) {
$filtermanager = filter_manager::instance();
$filtermanager->setup_page_for_globally_available_filters($this);
}
$this->_wherethemewasinitialised = debug_backtrace();
}
/**
* Reset contents of all database tables to initial values, reset caches, etc.
*/
public static function reset_all_data()
{
// Reset database.
self::reset_database();
// Purge dataroot directory.
self::reset_dataroot();
// Reset all static caches.
accesslib_clear_all_caches(true);
// Reset the nasty strings list used during the last test.
nasty_strings::reset_used_strings();
filter_manager::reset_caches();
// Reset course and module caches.
if (class_exists('format_base')) {
// If file containing class is not loaded, there is no cache there anyway.
format_base::reset_course_cache(0);
}
get_fast_modinfo(0, 0, true);
// Inform data generator.
self::get_data_generator()->reset();
// Initialise $CFG with default values. This is needed for behat cli process, so we don't have modified
// $CFG values from the old run. @see set_config.
initialise_cfg();
}
请发表评论