MDL-58486 user_prefs: always update $USER cache when applicable

set_user_preference and unset_user_preference let you pass the user to
set preferences for as an id. Previously, if you passed $USER->id there,
those methods did not update the Cache in $USER, which was surprising,
and not easy to debug.

Now, we always update the preference cache in $USER if the preference is
being changed for the current user.
This commit is contained in:
Tim Hunt
2017-04-04 16:22:57 +01:00
parent 3f48012eb5
commit 7a27817f98
2 changed files with 26 additions and 0 deletions
+6
View File
@@ -1896,6 +1896,9 @@ function set_user_preference($name, $value, $user = null) {
// Update value in cache.
$user->preference[$name] = $value;
if ($user !== $USER && $user->id == $USER->id) {
$USER->preference[$name] = $value;
}
// Set reload flag for other sessions.
mark_user_preferences_changed($user->id);
@@ -1965,6 +1968,9 @@ function unset_user_preference($name, $user = null) {
// Delete the preference from cache.
unset($user->preference[$name]);
if ($user !== $USER && $user->id == $USER->id) {
unset($USER->preference[$name]);
}
// Set reload flag for other sessions.
mark_user_preferences_changed($user->id);
+20
View File
@@ -1170,6 +1170,26 @@ class core_moodlelib_testcase extends advanced_testcase {
}
}
public function test_set_user_preference_for_current_user() {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
set_user_preference('test_pref', 2);
set_user_preference('test_pref', 1, $USER->id);
$this->assertEquals(1, get_user_preferences('test_pref'));
}
public function test_unset_user_preference_for_current_user() {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
set_user_preference('test_pref', 1);
unset_user_preference('test_pref', $USER->id);
$this->assertNull(get_user_preferences('test_pref'));
}
public function test_get_extra_user_fields() {
global $CFG, $USER, $DB;
$this->resetAfterTest();