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.
// 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);
// 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);
}
}
+ 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();