Skip to content

Commit

Permalink
MDL-62248 core_privacy: Test writer writes user prefs in right contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
FMCorz committed Apr 30, 2018
1 parent af099b4 commit 97b05c8
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions privacy/classes/tests/request/content_writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class content_writer implements \core_privacy\local\request\content_writer {
protected $customfiles;

/**
* @var array The site-wide user preferences which have been exported.
* @var \stdClass The user preferences which have been exported.
*/
protected $userprefs = [];
protected $userprefs;

/**
* Whether any data has been exported at all within the current context.
Expand All @@ -80,7 +80,7 @@ public function has_any_data() {
$hasmetadata = !empty($this->metadata->{$this->context->id});
$hasfiles = !empty($this->files->{$this->context->id});
$hascustomfiles = !empty($this->customfiles->{$this->context->id});
$hasuserprefs = !empty($this->userprefs);
$hasuserprefs = !empty($this->userprefs->{$this->context->id});

return $hasdata || $hasrelateddata || $hasmetadata || $hasfiles || $hascustomfiles || $hasuserprefs;
}
Expand All @@ -97,6 +97,7 @@ public function __construct(\core_privacy\local\request\writer $writer) {
$this->metadata = (object) [];
$this->files = (object) [];
$this->customfiles = (object) [];
$this->userprefs = (object) [];
}

/**
Expand Down Expand Up @@ -142,6 +143,13 @@ public function set_context(\context $context) : \core_privacy\local\request\con
];
}

if (isset($this->userprefs->{$this->context->id}) && empty((array) $this->userprefs->{$this->context->id})) {
$this->userprefs->{$this->context->id} = (object) [
'children' => (object) [],
'data' => [],
];
}

return $this;
}

Expand Down Expand Up @@ -386,11 +394,13 @@ public function export_user_preference(
string $value,
string $description
) : \core_privacy\local\request\content_writer {
if (!isset($this->userprefs[$component])) {
$this->userprefs[$component] = (object) [];
$prefs = $this->fetch_root($this->userprefs, []);

if (!isset($prefs->{$component})) {
$prefs->{$component} = (object) [];
}

$this->userprefs[$component]->$key = (object) [
$prefs->{$component}->$key = (object) [
'value' => $value,
'description' => $description,
];
Expand All @@ -405,8 +415,9 @@ public function export_user_preference(
* @return \stdClass
*/
public function get_user_preferences(string $component) {
if (isset($this->userprefs[$component])) {
return $this->userprefs[$component];
$prefs = $this->fetch_root($this->userprefs, []);
if (isset($prefs->{$component})) {
return $prefs->{$component};
} else {
return (object) [];
}
Expand Down

0 comments on commit 97b05c8

Please sign in to comment.