Index: lib/accesslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/accesslib.php,v retrieving revision 1.575 diff -u -r1.575 accesslib.php --- lib/accesslib.php 20 Mar 2009 07:41:49 -0000 1.575 +++ lib/accesslib.php 20 Mar 2009 08:13:25 -0000 @@ -162,13 +162,42 @@ define('ROLENAME_ORIGINALANDSHORT', 3); // the name as defined in the role definition and the shortname in brackets define('ROLENAME_ALIAS_RAW', 4); // the name as defined by a role alias, in raw form suitable for editing -$context_cache = array(); // Cache of all used context objects for performance (by level and instance) -$context_cache_id = array(); // Index to above cache by id +// Caches. Although these are global variables, they MUST NOT be used outside accesslib. +$context_cache = array(); // Cache of all used context objects for performance (by level and instance) +$context_cache_id = array(); // Index to above cache by id +$cron_cache = array(); // Used in get_role_access +$cached_system_context = null; // Used in get_system_context +$preloadedcourses = array(); // Used in preload_course_contexts. +$capability_names = null; // Used in is_valid_capability $DIRTYCONTEXTS = null; // dirty contexts cache $ACCESS = array(); // cache of caps for cron user switching and has_capability for other users (==not $USER) $RDEFS = array(); // role definitions cache - helps a lot with mem usage in cron +/** + * This method should ONLY BE USED BY UNIT TESTS. It clears all of + * accesslib's private caches. You need to do this before setting up test data, + * and also at the end fo the tests. + */ +function accesslib_clear_all_caches_for_unit_testing() { + global $UNITTEST; + if (empty($UNITTEST->running)) { + throw new coding_exception('You must not call clear_all_caches outside of unit tests.'); + } + global $context_cache, $context_cache_id, $cron_cache, $cached_system_context, + $preloadedcourses, $capability_names, $DIRTYCONTEXTS, $ACCESS, $RDEFS; + $context_cache = array(); // Cache of all used context objects for performance (by level and instance) + $context_cache_id = array(); // Index to above cache by id + $cron_cache = array(); // Used in get_role_access + $cached_system_context = null; // Used in get_system_context + $preloadedcourses = array(); // Used in preload_course_contexts. + $capability_names = null; // Used in is_valid_capability + + $DIRTYCONTEXTS = null; // dirty contexts cache + $ACCESS = array(); // cache of caps for cron user switching and has_capability for other users (==not $USER) + $RDEFS = array(); // role definitions cache - helps a lot with mem usage in cron +} + function get_role_context_caps($roleid, $context) { global $DB; @@ -243,7 +272,7 @@ // we need extra caching in CLI scripts and cron if (CLI_SCRIPT) { - static $cron_cache = array(); + global $cron_cache; if (!isset($cron_cache[$roleid])) { $cron_cache[$roleid] = array(); @@ -2097,19 +2126,17 @@ * @return mixed system context or null */ function get_system_context($cache=true) { - global $DB; - - static $cached = null; + global $DB, $cached_system_context; if ($cache and defined('SYSCONTEXTID')) { - if (is_null($cached)) { - $cached = new object(); - $cached->id = SYSCONTEXTID; - $cached->contextlevel = CONTEXT_SYSTEM; - $cached->instanceid = 0; - $cached->path = '/'.SYSCONTEXTID; - $cached->depth = 1; + if (is_null($cached_system_context)) { + $cached_system_context = new object(); + $cached_system_context->id = SYSCONTEXTID; + $cached_system_context->contextlevel = CONTEXT_SYSTEM; + $cached_system_context->instanceid = 0; + $cached_system_context->path = '/'.SYSCONTEXTID; + $cached_system_context->depth = 1; } - return $cached; + return $cached_system_context; } try { // TODO: can not use get_record() because we do not know if query failed :-( @@ -2152,8 +2179,8 @@ define('SYSCONTEXTID', $context->id); } - $cached = $context; - return $cached; + $cached_system_context = $context; + return $cached_system_context; } /** @@ -2348,7 +2375,7 @@ global $context_cache, $context_cache_id, $DB; // Users can call this multiple times without doing any harm - static $preloadedcourses = array(); + global $preloadedcourses; if (array_key_exists($courseid, $preloadedcourses)) { return; } @@ -3857,14 +3884,14 @@ * @return book true if capability exists */ function is_valid_capability($capabilityname, $cached=true) { - static $capsnames = null; // one request per page only + global $capability_names; // one request per page only - if (is_null($capsnames) or !$cached) { + if (is_null($capability_names) or !$cached) { global $DB; - $capsnames = $DB->get_records_menu('capabilities', null, '', 'name, 1'); + $capability_names = $DB->get_records_menu('capabilities', null, '', 'name, 1'); } - return array_key_exists($capabilityname, $capsnames); + return array_key_exists($capabilityname, $capability_names); } /**