From 542a7490f2070ed1d8a7456cf7f0e58a2e25acb0 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 1 Feb 2021 20:26:18 +0000 Subject: [PATCH] MDL-70763 core: export preferences for correct user. Ensure we are using the ID of the given user rather than falling back to that of the current user during privacy export. --- lib/editor/classes/privacy/provider.php | 2 +- lib/editor/tests/privacy_provider_test.php | 27 +++++++++++++++------- lib/form/classes/privacy/provider.php | 4 +--- lib/form/tests/privacy_provider_test.php | 17 ++++++++++---- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/editor/classes/privacy/provider.php b/lib/editor/classes/privacy/provider.php index 9d807a6dbcc..f044192e148 100644 --- a/lib/editor/classes/privacy/provider.php +++ b/lib/editor/classes/privacy/provider.php @@ -61,7 +61,7 @@ class provider implements * @param int $userid The userid of the user whose data is to be exported. */ public static function export_user_preferences(int $userid) { - $preference = get_user_preferences('htmleditor'); + $preference = get_user_preferences('htmleditor', null, $userid); if (!empty($preference)) { $desc = get_string('privacy:preference:htmleditor', 'core_editor', get_string('pluginname', "editor_{$preference}")); diff --git a/lib/editor/tests/privacy_provider_test.php b/lib/editor/tests/privacy_provider_test.php index 6d1dcf912c8..07d4a53212f 100644 --- a/lib/editor/tests/privacy_provider_test.php +++ b/lib/editor/tests/privacy_provider_test.php @@ -53,14 +53,19 @@ class core_editor_privacy_provider_testcase extends \core_privacy\tests\provider * When preference exists but is empty, there should be no export. */ public function test_empty_preference() { - global $USER; - $this->resetAfterTest(); + + // Create test user, add some preferences. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + set_user_preference('htmleditor', '', $user); + + // Switch to admin user (so we can validate preferences of the correct user are being exported). $this->setAdminUser(); - set_user_preference('htmleditor', ''); - - provider::export_user_preferences($USER->id); + // Export test users preferences. + provider::export_user_preferences($user->id); $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data()); } @@ -68,13 +73,19 @@ class core_editor_privacy_provider_testcase extends \core_privacy\tests\provider * When an editor is set, the name of that editor will be reported. */ public function test_editor_atto() { - global $USER; $this->resetAfterTest(); - $this->setAdminUser(); + + // Create test user, add some preferences. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); set_user_preference('htmleditor', 'atto'); - provider::export_user_preferences($USER->id); + // Switch to admin user (so we can validate preferences of the correct user are being exported). + $this->setAdminUser(); + + // Export test users preferences. + provider::export_user_preferences($user->id); $this->assertTrue(writer::with_context(\context_system::instance())->has_any_data()); $prefs = writer::with_context(\context_system::instance())->get_user_preferences('core_editor'); diff --git a/lib/form/classes/privacy/provider.php b/lib/form/classes/privacy/provider.php index 991d5062c23..974be8c6532 100644 --- a/lib/form/classes/privacy/provider.php +++ b/lib/form/classes/privacy/provider.php @@ -63,9 +63,7 @@ class provider implements * @param int $userid The ID of the user whose data is to be exported. */ public static function export_user_preferences(int $userid) { - - $preference = get_user_preferences('filemanager_recentviewmode'); - + $preference = get_user_preferences('filemanager_recentviewmode', null, $userid); if ($preference !== null) { switch ($preference) { case 1: diff --git a/lib/form/tests/privacy_provider_test.php b/lib/form/tests/privacy_provider_test.php index bdf3dabf8a7..42f389f0b29 100644 --- a/lib/form/tests/privacy_provider_test.php +++ b/lib/form/tests/privacy_provider_test.php @@ -23,6 +23,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core_form\privacy\provider; use core_privacy\local\request\writer; defined('MOODLE_INTERNAL') || die(); @@ -43,7 +44,7 @@ class core_form_privacy_provider_testcase extends \core_privacy\tests\provider_t $this->resetAfterTest(); $this->setAdminUser(); - \core_form\privacy\provider::export_user_preferences($USER->id); + provider::export_user_preferences($USER->id); $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data()); } @@ -55,13 +56,19 @@ class core_form_privacy_provider_testcase extends \core_privacy\tests\provider_t * @param string $desc Text describing the preference */ public function test_filemanager_recentviewmode(string $val, string $desc) { - global $USER; $this->resetAfterTest(); + + // Create test user, add some preferences. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + set_user_preference('filemanager_recentviewmode', $val, $user); + + // Switch to admin user (so we can validate preferences of the correct user are being exported). $this->setAdminUser(); - set_user_preference('filemanager_recentviewmode', $val); - - core_form\privacy\provider::export_user_preferences($USER->id); + // Export test users preferences. + provider::export_user_preferences($user->id); $this->assertTrue(writer::with_context(\context_system::instance())->has_any_data()); $prefs = writer::with_context(\context_system::instance())->get_user_preferences('core_form');