Navigation Menu

Skip to content

Commit

Permalink
MDL-70763 core: export preferences for correct user.
Browse files Browse the repository at this point in the history
Ensure we are using the ID of the given user rather than falling
back to that of the current user during privacy export.
  • Loading branch information
paulholden authored and sarjona committed Apr 28, 2021
1 parent a305ee2 commit f49cb04
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/editor/classes/privacy/provider.php
Expand Up @@ -61,7 +61,7 @@ public static function get_metadata(collection $collection) : collection {
* @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}"));
Expand Down
27 changes: 19 additions & 8 deletions lib/editor/tests/privacy_provider_test.php
Expand Up @@ -53,28 +53,39 @@ public function test_no_preference() {
* When preference exists but is empty, there should be no export.
*/
public function test_empty_preference() {
global $USER;

$this->resetAfterTest();
$this->setAdminUser();

set_user_preference('htmleditor', '');
// Create test user, add some preferences.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

provider::export_user_preferences($USER->id);
set_user_preference('htmleditor', '', $user);

// 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->assertFalse(writer::with_context(\context_system::instance())->has_any_data());
}

/**
* 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');
Expand Down
4 changes: 1 addition & 3 deletions lib/form/classes/privacy/provider.php
Expand Up @@ -63,9 +63,7 @@ public static function get_metadata(collection $collection) : collection {
* @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:
Expand Down
17 changes: 12 additions & 5 deletions lib/form/tests/privacy_provider_test.php
Expand Up @@ -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();
Expand All @@ -43,7 +44,7 @@ public function test_no_preference() {
$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());
}

Expand All @@ -55,13 +56,19 @@ public function test_no_preference() {
* @param string $desc Text describing the preference
*/
public function test_filemanager_recentviewmode(string $val, string $desc) {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();

set_user_preference('filemanager_recentviewmode', $val);
// 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();

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');
Expand Down

0 comments on commit f49cb04

Please sign in to comment.