diff --git a/blocks/tests/privacy_test.php b/blocks/tests/privacy_test.php index d8384a3f79a..8c6327eeede 100644 --- a/blocks/tests/privacy_test.php +++ b/blocks/tests/privacy_test.php @@ -300,18 +300,18 @@ class core_block_privacy_testcase extends provider_testcase { // Export data. provider::export_user_data(new approved_contextlist($u1, 'core_block', [$bmyprofile->context->id, $blogin->context->id, $bmentees->context->id, $bprivatefiles->context->id])); - $prefs = writer::with_context($bmentees->context)->get_user_preferences('core_block'); + $prefs = writer::with_context($bmentees->context)->get_user_context_preferences('core_block'); $this->assertEmpty((array) $prefs); - $prefs = writer::with_context($blogin->context)->get_user_preferences('core_block'); + $prefs = writer::with_context($blogin->context)->get_user_context_preferences('core_block'); $this->assertEquals($no, $prefs->block_is_docked->value); $this->assertEquals($yes, $prefs->block_is_hidden->value); - $prefs = writer::with_context($bprivatefiles->context)->get_user_preferences('core_block'); + $prefs = writer::with_context($bprivatefiles->context)->get_user_context_preferences('core_block'); $this->assertObjectNotHasAttribute('block_is_docked', $prefs); $this->assertEquals($no, $prefs->block_is_hidden->value); - $prefs = writer::with_context($bmyprofile->context)->get_user_preferences('core_block'); + $prefs = writer::with_context($bmyprofile->context)->get_user_context_preferences('core_block'); $this->assertEquals($yes, $prefs->block_is_docked->value); $this->assertObjectNotHasAttribute('block_is_hidden', $prefs); } diff --git a/privacy/classes/tests/request/content_writer.php b/privacy/classes/tests/request/content_writer.php index a5515b0b1b6..bc7f49751a1 100644 --- a/privacy/classes/tests/request/content_writer.php +++ b/privacy/classes/tests/request/content_writer.php @@ -82,7 +82,18 @@ class content_writer implements \core_privacy\local\request\content_writer { $hascustomfiles = !empty($this->customfiles->{$this->context->id}); $hasuserprefs = !empty($this->userprefs->{$this->context->id}); - return $hasdata || $hasrelateddata || $hasmetadata || $hasfiles || $hascustomfiles || $hasuserprefs; + $systemcontext = \context_system::instance(); + $hasglobaluserprefs = !empty($this->userprefs->{$systemcontext->id}); + + $hasanydata = $hasdata; + $hasanydata = $hasanydata || $hasrelateddata; + $hasanydata = $hasanydata || $hasmetadata; + $hasanydata = $hasanydata || $hasfiles; + $hasanydata = $hasanydata || $hascustomfiles; + $hasanydata = $hasanydata || $hasuserprefs; + $hasanydata = $hasanydata || $hasglobaluserprefs; + + return $hasanydata; } /** @@ -415,6 +426,22 @@ class content_writer implements \core_privacy\local\request\content_writer { * @return \stdClass */ public function get_user_preferences(string $component) { + $context = \context_system::instance(); + $prefs = $this->fetch_root($this->userprefs, [], $context->id); + if (isset($prefs->{$component})) { + return $prefs->{$component}; + } else { + return (object) []; + } + } + + /** + * Get all user preferences for the specified component. + * + * @param string $component The name of the component. + * @return \stdClass + */ + public function get_user_context_preferences(string $component) { $prefs = $this->fetch_root($this->userprefs, []); if (isset($prefs->{$component})) { return $prefs->{$component}; @@ -437,17 +464,19 @@ class content_writer implements \core_privacy\local\request\content_writer { * * @param \stdClass $base The base to use - e.g. $this->data * @param array $subcontext The subcontext to fetch + * @param int $temporarycontextid A temporary context ID to use for the fetch. * @return array */ - protected function fetch_root($base, $subcontext) { - if (!isset($base->{$this->context->id})) { - $base->{$this->context->id} = (object) [ + protected function fetch_root($base, $subcontext, $temporarycontextid = null) { + $contextid = !empty($temporarycontextid) ? $temporarycontextid : $this->context->id; + if (!isset($base->{$contextid})) { + $base->{$contextid} = (object) [ 'children' => (object) [], 'data' => [], ]; } - $current = $base->{$this->context->id}; + $current = $base->{$contextid}; foreach ($subcontext as $node) { if (!isset($current->children->{$node})) { $current->children->{$node} = (object) [ diff --git a/privacy/tests/tests_content_writer_test.php b/privacy/tests/tests_content_writer_test.php index f8d2bfa6a1e..3783ba2ebe3 100644 --- a/privacy/tests/tests_content_writer_test.php +++ b/privacy/tests/tests_content_writer_test.php @@ -188,14 +188,26 @@ class tests_content_writer_test extends advanced_testcase { */ public function test_export_user_preference() { $context = \context_system::instance(); + $adminuser = \core_user::get_user_by_username('admin'); + $usercontext = \context_user::instance($adminuser->id); $writer = $this->get_writer_instance(); $writer->set_context($context) + ->export_user_preference('core_privacy', 'somekey', 'value0', 'description0'); + $writer->set_context($usercontext) ->export_user_preference('core_tests', 'somekey', 'value1', 'description1') ->export_user_preference('core_privacy', 'somekey', 'value2', 'description2') ->export_user_preference('core_tests', 'someotherkey', 'value2', 'description2'); - $someprefs = $writer->get_user_preferences('core_tests'); + $writer->set_context($usercontext); + + $someprefs = $writer->get_user_preferences('core_privacy'); + $this->assertCount(1, (array) $someprefs); + $this->assertTrue(isset($someprefs->somekey)); + $this->assertEquals('value0', $someprefs->somekey->value); + $this->assertEquals('description0', $someprefs->somekey->description); + + $someprefs = $writer->get_user_context_preferences('core_tests'); $this->assertCount(2, (array) $someprefs); $this->assertTrue(isset($someprefs->somekey)); $this->assertEquals('value1', $someprefs->somekey->value); @@ -204,7 +216,7 @@ class tests_content_writer_test extends advanced_testcase { $this->assertEquals('value2', $someprefs->someotherkey->value); $this->assertEquals('description2', $someprefs->someotherkey->description); - $someprefs = $writer->get_user_preferences('core_privacy'); + $someprefs = $writer->get_user_context_preferences('core_privacy'); $this->assertCount(1, (array) $someprefs); $this->assertTrue(isset($someprefs->somekey)); $this->assertEquals('value2', $someprefs->somekey->value); @@ -216,25 +228,35 @@ class tests_content_writer_test extends advanced_testcase { */ public function test_export_user_preference_no_context_clash() { $writer = $this->get_writer_instance(); - $context = \context_system::instance(); - $writer->set_context($context) - ->export_user_preference('core_tests', 'somekey', 'value1', 'description1'); - + $coursecontext = \context_course::instance(SITEID); $adminuser = \core_user::get_user_by_username('admin'); $usercontext = \context_user::instance($adminuser->id); + + $writer->set_context($context) + ->export_user_preference('core_tests', 'somekey', 'value0', 'description0'); + $writer->set_context($coursecontext) + ->export_user_preference('core_tests', 'somekey', 'value1', 'description1'); $writer->set_context($usercontext) ->export_user_preference('core_tests', 'somekey', 'value2', 'description2'); - $writer->set_context($context); + // Set the course context and fetch with get_user_preferences to get the global preference. + $writer->set_context($coursecontext); $someprefs = $writer->get_user_preferences('core_tests'); $this->assertCount(1, (array) $someprefs); $this->assertTrue(isset($someprefs->somekey)); + $this->assertEquals('value0', $someprefs->somekey->value); + $this->assertEquals('description0', $someprefs->somekey->description); + + // Set the course context and fetch with get_user_context_preferences. + $someprefs = $writer->get_user_context_preferences('core_tests'); + $this->assertCount(1, (array) $someprefs); + $this->assertTrue(isset($someprefs->somekey)); $this->assertEquals('value1', $someprefs->somekey->value); $this->assertEquals('description1', $someprefs->somekey->description); $writer->set_context($usercontext); - $someprefs = $writer->get_user_preferences('core_tests'); + $someprefs = $writer->get_user_context_preferences('core_tests'); $this->assertCount(1, (array) $someprefs); $this->assertTrue(isset($someprefs->somekey)); $this->assertEquals('value2', $someprefs->somekey->value);