当前位置: 首页>>代码示例>>PHP>>正文


PHP manager::init_empty_session方法代码示例

本文整理汇总了PHP中Core\Session\manager::init_empty_session方法的典型用法代码示例。如果您正苦于以下问题:PHP manager::init_empty_session方法的具体用法?PHP manager::init_empty_session怎么用?PHP manager::init_empty_session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Core\Session\manager的用法示例。


在下文中一共展示了manager::init_empty_session方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: reset_all_data

 /**
  * 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
//.........这里部分代码省略.........
开发者ID:sumitnegi933,项目名称:Moodle_lms_New,代码行数:101,代码来源:util.php

示例2: setUp

 /**
  * Test set up.
  *
  * This is executed before running any tests in this file.
  */
 public function setUp()
 {
     global $SESSION;
     $this->resetAfterTest();
     manager::init_empty_session();
     // Set this user as the admin.
     $this->setAdminUser();
     $data = new stdClass();
     $data->enrolstartdate = time();
     $data->secret = 'secret';
     $toolrecord = $this->getDataGenerator()->create_lti_tool($data);
     $this->tool = helper::get_lti_tool($toolrecord->id);
     $SESSION->notifications = [];
 }
开发者ID:dg711,项目名称:moodle,代码行数:19,代码来源:tool_provider_test.php

示例3: stdClass

require_once $CFG->libdir . '/dmllib.php';
require_once $CFG->libdir . '/moodlelib.php';
require_once $CFG->libdir . '/deprecatedlib.php';
require_once $CFG->libdir . '/adminlib.php';
require_once $CFG->libdir . '/componentlib.class.php';
require_once $CFG->dirroot . '/cache/lib.php';
// Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
// Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes.
if (defined('COMPONENT_CLASSLOADER')) {
    spl_autoload_register(COMPONENT_CLASSLOADER);
} else {
    spl_autoload_register('core_component::classloader');
}
require $CFG->dirroot . '/version.php';
$CFG->target_release = $release;
\core\session\manager::init_empty_session();
global $SESSION;
global $USER;
global $COURSE;
$COURSE = new stdClass();
$COURSE->id = 1;
global $SITE;
$SITE = $COURSE;
define('SITEID', 1);
//Database types
$databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'), 'mariadb' => moodle_database::get_driver_instance('mariadb', 'native'), 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'), 'oci' => moodle_database::get_driver_instance('oci', 'native'), 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), 'mssql' => moodle_database::get_driver_instance('mssql', 'native'));
foreach ($databases as $type => $database) {
    if ($database->driver_installed() !== true) {
        unset($databases[$type]);
    }
}
开发者ID:mongo0se,项目名称:moodle,代码行数:31,代码来源:install.php

示例4: cron_setup_user

/**
 * Sets up current user and course environment (lang, etc.) in cron.
 * Do not use outside of cron script!
 *
 * @param stdClass $user full user object, null means default cron user (admin),
 *                 value 'reset' means reset internal static caches.
 * @param stdClass $course full course record, null means $SITE
 * @return void
 */
function cron_setup_user($user = NULL, $course = NULL)
{
    global $CFG, $SITE, $PAGE;
    if (!CLI_SCRIPT) {
        throw new coding_exception('Function cron_setup_user() cannot be used in normal requests!');
    }
    static $cronuser = NULL;
    static $cronsession = NULL;
    if ($user === 'reset') {
        $cronuser = null;
        $cronsession = null;
        \core\session\manager::init_empty_session();
        return;
    }
    if (empty($cronuser)) {
        /// ignore admins timezone, language and locale - use site default instead!
        $cronuser = get_admin();
        $cronuser->timezone = $CFG->timezone;
        $cronuser->lang = '';
        $cronuser->theme = '';
        unset($cronuser->description);
        $cronsession = new stdClass();
    }
    if (!$user) {
        // Cached default cron user (==modified admin for now).
        \core\session\manager::init_empty_session();
        \core\session\manager::set_user($cronuser);
        $GLOBALS['SESSION'] = $cronsession;
    } else {
        // Emulate real user session - needed for caps in cron.
        if ($GLOBALS['USER']->id != $user->id) {
            \core\session\manager::init_empty_session();
            \core\session\manager::set_user($user);
        }
    }
    // TODO MDL-19774 relying on global $PAGE in cron is a bad idea.
    // Temporary hack so that cron does not give fatal errors.
    $PAGE = new moodle_page();
    if ($course) {
        $PAGE->set_course($course);
    } else {
        $PAGE->set_course($SITE);
    }
    // TODO: it should be possible to improve perf by caching some limited number of users here ;-)
}
开发者ID:alanaipe2015,项目名称:moodle,代码行数:54,代码来源:sessionlib.php

示例5: before_scenario

 /**
  * Resets the test environment.
  *
  * @param BeforeScenarioScope $scope scope passed by event fired before scenario.
  * @throws behat_stop_exception If here we are not using the test database it should be because of a coding error
  */
 public function before_scenario(BeforeScenarioScope $scope)
 {
     global $DB, $CFG;
     // As many checks as we can.
     if (!defined('BEHAT_TEST') || !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || !behat_util::is_test_site()) {
         throw new behat_stop_exception('Behat only can modify the test database and the test dataroot!');
     }
     $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
     $driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
     try {
         $session = $this->getSession();
     } catch (CurlExec $e) {
         // Exception thrown by WebDriver, so only @javascript tests will be caugth; in
         // behat_util::check_server_status() we already checked that the server is running.
         throw new behat_stop_exception($driverexceptionmsg);
     } catch (DriverException $e) {
         throw new behat_stop_exception($driverexceptionmsg);
     } catch (UnknownError $e) {
         // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
         throw new behat_stop_exception($e->getMessage());
     }
     $suitename = $scope->getSuite()->getName();
     // Register behat selectors for theme, if suite is changed. We do it for every suite change.
     if ($suitename !== self::$runningsuite) {
         behat_context_helper::set_environment($scope->getEnvironment());
         // We need the Mink session to do it and we do it only before the first scenario.
         $namedpartialclass = 'behat_partial_named_selector';
         $namedexactclass = 'behat_exact_named_selector';
         if ($suitename !== 'default') {
             // If override selector exist, then set it as default behat selectors class.
             $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_partial', true);
             if (class_exists($overrideclass)) {
                 $namedpartialclass = $overrideclass;
             }
             // If override selector exist, then set it as default behat selectors class.
             $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_exact', true);
             if (class_exists($overrideclass)) {
                 $namedexactclass = $overrideclass;
             }
         }
         $this->getSession()->getSelectorsHandler()->registerSelector('named_partial', new $namedpartialclass());
         $this->getSession()->getSelectorsHandler()->registerSelector('named_exact', new $namedexactclass());
     }
     // Reset mink session between the scenarios.
     $session->reset();
     // Reset $SESSION.
     \core\session\manager::init_empty_session();
     behat_util::reset_all_data();
     // Assign valid data to admin user (some generator-related code needs a valid user).
     $user = $DB->get_record('user', array('username' => 'admin'));
     \core\session\manager::set_user($user);
     // Reset the browser if specified in config.php.
     if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) {
         $now = time();
         if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) {
             $session->restart();
             self::$lastbrowsersessionstart = $now;
         }
     }
     // Set the theme if not default.
     if ($suitename !== "default") {
         set_config('theme', $suitename);
         self::$runningsuite = $suitename;
     }
     // Start always in the the homepage.
     try {
         // Let's be conservative as we never know when new upstream issues will affect us.
         $session->visit($this->locate_path('/'));
     } catch (UnknownError $e) {
         throw new behat_stop_exception($e->getMessage());
     }
     // Checking that the root path is a Moodle test site.
     if (self::is_first_scenario()) {
         $notestsiteexception = new behat_stop_exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' . 'ensure you started the built-in web server in the correct directory or your web server is correctly started and set up');
         $this->find("xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", $notestsiteexception);
         self::$initprocessesfinished = true;
     }
     // Run all test with medium (1024x768) screen size, to avoid responsive problems.
     $this->resize_window('medium');
 }
开发者ID:janeklb,项目名称:moodle,代码行数:86,代码来源:behat_hooks.php

示例6: before_scenario

 /**
  * Resets the test environment.
  *
  * @throws coding_exception If here we are not using the test database it should be because of a coding error
  * @BeforeScenario
  */
 public function before_scenario($event)
 {
     global $DB, $SESSION, $CFG;
     // As many checks as we can.
     if (!defined('BEHAT_TEST') || !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || !behat_util::is_test_site()) {
         throw new coding_exception('Behat only can modify the test database and the test dataroot!');
     }
     $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
     $driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
     try {
         $session = $this->getSession();
     } catch (CurlExec $e) {
         // Exception thrown by WebDriver, so only @javascript tests will be caugth; in
         // behat_util::is_server_running() we already checked that the server is running.
         throw new Exception($driverexceptionmsg);
     } catch (DriverException $e) {
         throw new Exception($driverexceptionmsg);
     } catch (UnknownError $e) {
         // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
         $this->throw_unknown_exception($e);
     }
     // We need the Mink session to do it and we do it only before the first scenario.
     if (self::is_first_scenario()) {
         behat_selectors::register_moodle_selectors($session);
         behat_context_helper::set_session($session);
     }
     // Reset $SESSION.
     \core\session\manager::init_empty_session();
     behat_util::reset_database();
     behat_util::reset_dataroot();
     accesslib_clear_all_caches(true);
     // Reset the nasty strings list used during the last test.
     nasty_strings::reset_used_strings();
     // Assign valid data to admin user (some generator-related code needs a valid user).
     $user = $DB->get_record('user', array('username' => 'admin'));
     \core\session\manager::set_user($user);
     // Reset the browser if specified in config.php.
     if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) {
         $now = time();
         if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) {
             $session->restart();
             self::$lastbrowsersessionstart = $now;
         }
     }
     // Start always in the the homepage.
     try {
         // Let's be conservative as we never know when new upstream issues will affect us.
         $session->visit($this->locate_path('/'));
     } catch (UnknownError $e) {
         $this->throw_unknown_exception($e);
     }
     // Checking that the root path is a Moodle test site.
     if (self::is_first_scenario()) {
         $notestsiteexception = new Exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' . 'ensure you started the built-in web server in the correct directory or your web server is correctly started and set up');
         $this->find("xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", $notestsiteexception);
         self::$initprocessesfinished = true;
     }
     // Run all test with medium (1024x768) screen size, to avoid responsive problems.
     $this->resize_window('medium');
 }
开发者ID:sumitnegi933,项目名称:Moodle_lms_New,代码行数:66,代码来源:behat_hooks.php

示例7: test_loginas

 /**
  * Test loginas.
  * @copyright  2103 Rajesh Taneja <rajesh@moodle.com>
  */
 public function test_loginas()
 {
     global $USER, $SESSION;
     $this->resetAfterTest();
     // Set current user as Admin user and save it for later use.
     $this->setAdminUser();
     $adminuser = $USER;
     $adminsession = $SESSION;
     $user = $this->getDataGenerator()->create_user();
     $_SESSION['extra'] = true;
     // Try admin loginas this user in system context.
     $this->assertObjectNotHasAttribute('realuser', $USER);
     \core\session\manager::loginas($user->id, context_system::instance());
     $this->assertSame($user->id, $USER->id);
     $this->assertSame(context_system::instance(), $USER->loginascontext);
     $this->assertSame($adminuser->id, $USER->realuser);
     $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
     $this->assertSame($GLOBALS['USER'], $USER);
     $this->assertNotSame($adminuser, $_SESSION['REALUSER']);
     $this->assertEquals($adminuser, $_SESSION['REALUSER']);
     $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
     $this->assertSame($GLOBALS['SESSION'], $SESSION);
     $this->assertNotSame($adminsession, $_SESSION['REALSESSION']);
     $this->assertEquals($adminsession, $_SESSION['REALSESSION']);
     $this->assertArrayNotHasKey('extra', $_SESSION);
     // Set user as current user and login as admin user in course context.
     \core\session\manager::init_empty_session();
     $this->setUser($user);
     $this->assertNotEquals($adminuser->id, $USER->id);
     $course = $this->getDataGenerator()->create_course();
     $coursecontext = context_course::instance($course->id);
     // Catch event triggered.
     $sink = $this->redirectEvents();
     \core\session\manager::loginas($adminuser->id, $coursecontext);
     $events = $sink->get_events();
     $sink->close();
     $event = array_pop($events);
     $this->assertSame($adminuser->id, $USER->id);
     $this->assertSame($coursecontext, $USER->loginascontext);
     $this->assertSame($user->id, $USER->realuser);
     // Test event captured has proper information.
     $this->assertInstanceOf('\\core\\event\\user_loggedinas', $event);
     $this->assertSame($user->id, $event->objectid);
     $this->assertSame($adminuser->id, $event->relateduserid);
     $this->assertSame($course->id, $event->courseid);
     $this->assertEquals($coursecontext, $event->get_context());
     $oldfullname = fullname($user, true);
     $newfullname = fullname($adminuser, true);
     $expectedlogdata = array($course->id, "course", "loginas", "../user/view.php?id={$course->id}&amp;user={$user->id}", "{$oldfullname} -> {$newfullname}");
     $this->assertEventLegacyLogData($expectedlogdata, $event);
 }
开发者ID:EmmanuelYupit,项目名称:educursos,代码行数:55,代码来源:session_manager_test.php

示例8: test_log_writing

 public function test_log_writing()
 {
     global $DB;
     $this->resetAfterTest();
     $this->preventResetByRollback();
     // Logging waits till the transaction gets committed.
     $this->setAdminUser();
     $user1 = $this->getDataGenerator()->create_user();
     $user2 = $this->getDataGenerator()->create_user();
     $course1 = $this->getDataGenerator()->create_course();
     $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
     $course2 = $this->getDataGenerator()->create_course();
     $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
     // Test all plugins are disabled by this command.
     set_config('enabled_stores', '', 'tool_log');
     $manager = get_log_manager(true);
     $stores = $manager->get_readers();
     $this->assertCount(0, $stores);
     // Enable logging plugin.
     set_config('enabled_stores', 'logstore_standard', 'tool_log');
     set_config('buffersize', 0, 'logstore_standard');
     set_config('logguests', 1, 'logstore_standard');
     $manager = get_log_manager(true);
     $stores = $manager->get_readers();
     $this->assertCount(1, $stores);
     $this->assertEquals(array('logstore_standard'), array_keys($stores));
     /** @var \logstore_standard\log\store $store */
     $store = $stores['logstore_standard'];
     $this->assertInstanceOf('logstore_standard\\log\\store', $store);
     $this->assertInstanceOf('tool_log\\log\\writer', $store);
     $this->assertTrue($store->is_logging());
     $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
     $this->assertCount(0, $logs);
     $this->setCurrentTimeStart();
     $this->setUser(0);
     $event1 = \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
     $event1->trigger();
     $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
     $this->assertCount(1, $logs);
     $log1 = reset($logs);
     unset($log1->id);
     $log1->other = unserialize($log1->other);
     $log1 = (array) $log1;
     $data = $event1->get_data();
     $data['origin'] = 'cli';
     $data['ip'] = null;
     $data['realuserid'] = null;
     $this->assertEquals($data, $log1);
     $this->setAdminUser();
     \core\session\manager::loginas($user1->id, context_system::instance());
     $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
     logstore_standard_restore::hack_executing(1);
     $event2 = \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
     $event2->trigger();
     logstore_standard_restore::hack_executing(0);
     \core\session\manager::init_empty_session();
     $this->assertFalse(\core\session\manager::is_loggedinas());
     $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
     $this->assertCount(3, $logs);
     array_shift($logs);
     $log2 = array_shift($logs);
     $this->assertSame('\\core\\event\\user_loggedinas', $log2->eventname);
     $this->assertSame('cli', $log2->origin);
     $log3 = array_shift($logs);
     unset($log3->id);
     $log3->other = unserialize($log3->other);
     $log3 = (array) $log3;
     $data = $event2->get_data();
     $data['origin'] = 'restore';
     $data['ip'] = null;
     $data['realuserid'] = 2;
     $this->assertEquals($data, $log3);
     // Test table exists.
     $tablename = $store->get_internal_log_table_name();
     $this->assertTrue($DB->get_manager()->table_exists($tablename));
     // Test reading.
     $this->assertSame(3, $store->get_events_select_count('', array()));
     $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0);
     // Is actually sorted by "timecreated ASC, id ASC".
     $this->assertCount(3, $events);
     $resev1 = array_shift($events);
     array_shift($events);
     $resev2 = array_shift($events);
     $this->assertEquals($event1->get_data(), $resev1->get_data());
     $this->assertEquals($event2->get_data(), $resev2->get_data());
     // Test buffering.
     set_config('buffersize', 3, 'logstore_standard');
     $manager = get_log_manager(true);
     $stores = $manager->get_readers();
     /** @var \logstore_standard\log\store $store */
     $store = $stores['logstore_standard'];
     $DB->delete_records('logstore_standard_log');
     \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
     $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
     \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
     $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
     $store->flush();
     $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
     \logstore_standard\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
     $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
//.........这里部分代码省略.........
开发者ID:Hirenvaghasiya,项目名称:moodle,代码行数:101,代码来源:store_test.php

示例9: setUser

 /**
  * Set current $USER, reset access cache.
  * @static
  * @param null|int|stdClass $user user record, null or 0 means non-logged-in, positive integer means userid
  * @return void
  */
 public static function setUser($user = null)
 {
     global $CFG, $DB;
     if (is_object($user)) {
         $user = clone $user;
     } else {
         if (!$user) {
             $user = new stdClass();
             $user->id = 0;
             $user->mnethostid = $CFG->mnet_localhost_id;
         } else {
             $user = $DB->get_record('user', array('id' => $user));
         }
     }
     unset($user->description);
     unset($user->access);
     unset($user->preference);
     // Enusre session is empty, as it may contain caches and user specific info.
     \core\session\manager::init_empty_session();
     \core\session\manager::set_user($user);
 }
开发者ID:aleph-n,项目名称:lms.aaenl,代码行数:27,代码来源:advanced_testcase.php

示例10: test_log_writing

 public function test_log_writing()
 {
     global $DB, $CFG;
     $this->resetAfterTest();
     $this->preventResetByRollback();
     // Logging waits till the transaction gets committed.
     $dbman = $DB->get_manager();
     $this->assertTrue($dbman->table_exists('logstore_standard_log'));
     $DB->delete_records('logstore_standard_log');
     $this->setAdminUser();
     $user1 = $this->getDataGenerator()->create_user();
     $user2 = $this->getDataGenerator()->create_user();
     $course1 = $this->getDataGenerator()->create_course();
     $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
     $course2 = $this->getDataGenerator()->create_course();
     $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
     // Test all plugins are disabled by this command.
     set_config('enabled_stores', '', 'tool_log');
     $manager = get_log_manager(true);
     $stores = $manager->get_readers();
     $this->assertCount(0, $stores);
     // Fake the settings, we will abuse the standard plugin table here...
     $parts = explode('_', get_class($DB));
     set_config('dbdriver', $parts[1] . '/' . $parts[0], 'logstore_database');
     set_config('dbhost', $CFG->dbhost, 'logstore_database');
     set_config('dbuser', $CFG->dbuser, 'logstore_database');
     set_config('dbpass', $CFG->dbpass, 'logstore_database');
     set_config('dbname', $CFG->dbname, 'logstore_database');
     set_config('dbtable', $CFG->prefix . 'logstore_standard_log', 'logstore_database');
     if (!empty($CFG->dboptions['dbpersist'])) {
         set_config('dbpersist', 1, 'logstore_database');
     } else {
         set_config('dbpersist', 0, 'logstore_database');
     }
     if (!empty($CFG->dboptions['dbsocket'])) {
         set_config('dbsocket', $CFG->dboptions['dbsocket'], 'logstore_database');
     } else {
         set_config('dbsocket', '', 'logstore_database');
     }
     if (!empty($CFG->dboptions['dbport'])) {
         set_config('dbport', $CFG->dboptions['dbport'], 'logstore_database');
     } else {
         set_config('dbport', '', 'logstore_database');
     }
     if (!empty($CFG->dboptions['dbschema'])) {
         set_config('dbschema', $CFG->dboptions['dbschema'], 'logstore_database');
     } else {
         set_config('dbschema', '', 'logstore_database');
     }
     if (!empty($CFG->dboptions['dbcollation'])) {
         set_config('dbcollation', $CFG->dboptions['dbcollation'], 'logstore_database');
     } else {
         set_config('dbcollation', '', 'logstore_database');
     }
     // Enable logging plugin.
     set_config('enabled_stores', 'logstore_database', 'tool_log');
     set_config('buffersize', 0, 'logstore_database');
     set_config('logguests', 1, 'logstore_database');
     $manager = get_log_manager(true);
     $stores = $manager->get_readers();
     $this->assertCount(1, $stores);
     $this->assertEquals(array('logstore_database'), array_keys($stores));
     $store = $stores['logstore_database'];
     $this->assertInstanceOf('logstore_database\\log\\store', $store);
     $this->assertInstanceOf('tool_log\\log\\writer', $store);
     $this->assertTrue($store->is_logging());
     $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
     $this->assertCount(0, $logs);
     $this->setCurrentTimeStart();
     $this->setUser(0);
     $event1 = \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
     $event1->trigger();
     $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
     $this->assertCount(1, $logs);
     $log1 = reset($logs);
     unset($log1->id);
     $log1->other = unserialize($log1->other);
     $log1 = (array) $log1;
     $data = $event1->get_data();
     $data['origin'] = 'cli';
     $data['ip'] = null;
     $data['realuserid'] = null;
     $this->assertEquals($data, $log1);
     $this->setAdminUser();
     \core\session\manager::loginas($user1->id, context_system::instance());
     $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
     $event2 = \logstore_database\event\unittest_executed::create(array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
     $event2->trigger();
     \core\session\manager::init_empty_session();
     $this->assertFalse(\core\session\manager::is_loggedinas());
     $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
     $this->assertCount(3, $logs);
     array_shift($logs);
     $log2 = array_shift($logs);
     $this->assertSame('\\core\\event\\user_loggedinas', $log2->eventname);
     $log3 = array_shift($logs);
     unset($log3->id);
     $log3->other = unserialize($log3->other);
     $log3 = (array) $log3;
     $data = $event2->get_data();
//.........这里部分代码省略.........
开发者ID:pzhu2004,项目名称:moodle,代码行数:101,代码来源:store_test.php

示例11: test_sesskey

 public function test_sesskey()
 {
     global $USER;
     $this->resetAfterTest();
     $user = $this->getDataGenerator()->create_user();
     \core\session\manager::init_empty_session();
     $this->assertObjectNotHasAttribute('sesskey', $USER);
     $sesskey = sesskey();
     $this->assertNotEmpty($sesskey);
     $this->assertSame($sesskey, $USER->sesskey);
     $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
     $this->assertSame($GLOBALS['USER'], $USER);
     $this->assertSame($sesskey, sesskey());
     // Test incomplete session init - the sesskeys should return random values.
     $_SESSION = array();
     unset($GLOBALS['USER']);
     unset($GLOBALS['SESSION']);
     $this->assertFalse(sesskey());
     $this->assertArrayNotHasKey('USER', $GLOBALS);
     $this->assertFalse(sesskey());
 }
开发者ID:alanaipe2015,项目名称:moodle,代码行数:21,代码来源:sessionlib_test.php

示例12: test_session_persistance

 /**
  * Test that session notifications are persisted across session clears.
  */
 public function test_session_persistance()
 {
     global $PAGE, $SESSION;
     // Initially there won't be any notifications.
     $this->assertCount(0, $SESSION->notifications);
     // Adding a notification should make one available to fetch.
     \core\notification::success('Notification created');
     $this->assertCount(1, $SESSION->notifications);
     // Re-creating the session will not empty the notification bag.
     \core\session\manager::init_empty_session();
     $this->assertCount(1, $SESSION->notifications);
 }
开发者ID:evltuma,项目名称:moodle,代码行数:15,代码来源:notification_test.php


注:本文中的Core\Session\manager::init_empty_session方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。