public function test_search_areas() { global $CFG; $this->resetAfterTest(); set_config('enableglobalsearch', true); $fakeareaid = \core_search\manager::generate_areaid('mod_unexisting', 'chihuaquita'); $searcharea = \core_search\manager::get_search_area($this->forumpostareaid); $this->assertInstanceOf('\\core_search\\base', $searcharea); $this->assertFalse(\core_search\manager::get_search_area($fakeareaid)); $this->assertArrayHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list()); $this->assertArrayNotHasKey($fakeareaid, \core_search\manager::get_search_areas_list()); // Enabled by default once global search is enabled. $this->assertArrayHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list(true)); list($componentname, $varname) = $searcharea->get_config_var_name(); set_config($varname . '_enabled', 0, $componentname); \core_search\manager::clear_static(); $this->assertArrayNotHasKey('mod_forum', \core_search\manager::get_search_areas_list(true)); set_config($varname . '_enabled', 1, $componentname); // Although the result is wrong, we want to check that \core_search\manager::get_search_areas_list returns cached results. $this->assertArrayNotHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list(true)); // Now we check the real result. \core_search\manager::clear_static(); $this->assertArrayHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list(true)); }
/** * 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, $FULLME; // Stop any message redirection. self::stop_message_redirection(); // Stop any message redirection. self::stop_event_redirection(); // Start a new email redirection. // This will clear any existing phpmailer redirection. // We redirect all phpmailer output to this message sink which is // called instead of phpmailer actually sending the message. self::start_phpmailer_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(); $localename = self::get_locale_name(); $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 ($FULLME !== self::get_global_backup('FULLME')) { $warnings[] = 'Warning: unexpected change of $FULLME'; } if (setlocale(LC_TIME, 0) !== $localename) { $warnings[] = 'Warning: unexpected change of locale'; } } 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'); $FULLME = self::get_global_backup('FULLME'); $_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, true); filter_manager::reset_caches(); core_filetypes::reset_caches(); \core_search\manager::clear_static(); core_user::reset_caches(); if (class_exists('core_media_manager', false)) { core_media_manager::reset_caches(); } // Reset static unit test options. if (class_exists('\\availability_date\\condition', false)) { \availability_date\condition::set_current_time_for_test(0); } // Reset internal users. core_user::reset_internal_users(); //TODO MDL-25290: add more resets here and probably refactor them to new core function // 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); // Reset other singletons. if (class_exists('core_plugin_manager')) { core_plugin_manager::reset_caches(true); } if (class_exists('\\core\\update\\checker')) { \core\update\checker::reset_caches(true); } // Clear static cache within restore. if (class_exists('restore_section_structure_step')) { restore_section_structure_step::reset_caches(); } // purge dataroot directory self::reset_dataroot(); // restore original config once more in case resetting of caches changed CFG $CFG = self::get_global_backup('CFG'); // inform data generator self::get_data_generator()->reset(); // fix PHP settings error_reporting($CFG->debug); // Reset the date/time class. core_date::phpunit_reset(); // Make sure the time locale is consistent - that is Australian English. setlocale(LC_TIME, $localename); // Reset the log manager cache. get_log_manager(true); // verify db writes just in case something goes wrong in reset if (self::$lastdbwrites != $DB->perf_get_writes()) { error_log('Unexpected DB writes in phpunit_util::reset_all_data()'); self::$lastdbwrites = $DB->perf_get_writes(); } if ($warnings) { $warnings = implode("\n", $warnings); trigger_error($warnings, E_USER_WARNING); } }