Skip to content

Commit

Permalink
Merge branch 'MDL-62060-34' of git://github.com/rezaies/moodle into M…
Browse files Browse the repository at this point in the history
…OODLE_34_STABLE
  • Loading branch information
junpataleta committed May 1, 2018
2 parents fc7c30e + 970515e commit 998fad0
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
11 changes: 7 additions & 4 deletions privacy/classes/local/request/moodle_content_writer.php
Expand Up @@ -79,11 +79,12 @@ public function set_context(\context $context) : content_writer {
*
* @param array $subcontext The location within the current context that this data belongs.
* @param \stdClass $data The data to be exported
* @return content_writer
*/
public function export_data(array $subcontext, \stdClass $data) : content_writer {
$path = $this->get_path($subcontext, 'data.json');

$this->write_data($path, json_encode($data));
$this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE));

return $this;
}
Expand All @@ -97,6 +98,7 @@ public function export_data(array $subcontext, \stdClass $data) : content_writer
* @param string $key The metadata name.
* @param string $value The metadata value.
* @param string $description The description of the value.
* @return content_writer
*/
public function export_metadata(array $subcontext, string $key, $value, string $description) : content_writer {
$path = $this->get_full_path($subcontext, 'metadata.json');
Expand All @@ -113,7 +115,7 @@ public function export_metadata(array $subcontext, string $key, $value, string $
];

$path = $this->get_path($subcontext, 'metadata.json');
$this->write_data($path, json_encode($data));
$this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE));

return $this;
}
Expand All @@ -124,11 +126,12 @@ public function export_metadata(array $subcontext, string $key, $value, string $
* @param array $subcontext The location within the current context that this data belongs.
* @param string $name The name of the file to be exported.
* @param \stdClass $data The related data to export.
* @return content_writer
*/
public function export_related_data(array $subcontext, $name, $data) : content_writer {
$path = $this->get_path($subcontext, "{$name}.json");

$this->write_data($path, json_encode($data));
$this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE));

return $this;
}
Expand Down Expand Up @@ -227,7 +230,7 @@ public function export_user_preference(string $component, string $key, string $v
'value' => $value,
'description' => $description,
];
$this->write_data($path, json_encode($data));
$this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE));

return $this;
}
Expand Down
119 changes: 119 additions & 0 deletions privacy/tests/moodle_content_writer_test.php
Expand Up @@ -417,6 +417,8 @@ public function test_export_area_files() {
* Exporting a single stored_file should cause that file to be output in the files directory.
*
* @dataProvider export_file_provider
* @param string $filearea File area
* @param int $itemid Item ID
* @param string $filepath File path
* @param string $filename File name
* @param string $content Content
Expand Down Expand Up @@ -794,6 +796,123 @@ public function export_user_preference_provider() {
];
}

/**
* Test that exported data is human readable.
*
* @dataProvider unescaped_unicode_export_provider
* @param string $text
*/
public function test_export_data_unescaped_unicode($text) {
$context = \context_system::instance();
$subcontext = [];
$data = (object) ['key' => $text];

$writer = $this->get_writer_instance()
->set_context($context)
->export_data($subcontext, $data);

$fileroot = $this->fetch_exported_content($writer);

$contextpath = $this->get_context_path($context, $subcontext, 'data.json');

$json = $fileroot->getChild($contextpath)->getContent();
$this->assertRegExp("/$text/", $json);

$expanded = json_decode($json);
$this->assertEquals($data, $expanded);
}

/**
* Test that exported metadata is human readable.
*
* @dataProvider unescaped_unicode_export_provider
* @param string $text
*/
public function test_export_metadata_unescaped_unicode($text) {
$context = \context_system::instance();
$subcontext = ['a', 'b', 'c'];

$writer = $this->get_writer_instance()
->set_context($context)
->export_metadata($subcontext, $text, $text, $text);

$fileroot = $this->fetch_exported_content($writer);

$contextpath = $this->get_context_path($context, $subcontext, 'metadata.json');

$json = $fileroot->getChild($contextpath)->getContent();
$this->assertRegExp("/$text.*$text.*$text/", $json);

$expanded = json_decode($json);
$this->assertTrue(isset($expanded->$text));
$this->assertEquals($text, $expanded->$text->value);
$this->assertEquals($text, $expanded->$text->description);
}

/**
* Test that exported related data is human readable.
*
* @dataProvider unescaped_unicode_export_provider
* @param string $text
*/
public function test_export_related_data_unescaped_unicode($text) {
$context = \context_system::instance();
$subcontext = [];
$data = (object) ['key' => $text];

$writer = $this->get_writer_instance()
->set_context($context)
->export_related_data($subcontext, 'name', $data);

$fileroot = $this->fetch_exported_content($writer);

$contextpath = $this->get_context_path($context, $subcontext, 'name.json');

$json = $fileroot->getChild($contextpath)->getContent();
$this->assertRegExp("/$text/", $json);

$expanded = json_decode($json);
$this->assertEquals($data, $expanded);
}

/**
* Test that exported user preference is human readable.
*
* @dataProvider unescaped_unicode_export_provider
* @param string $text
*/
public function test_export_user_preference_unescaped_unicode($text) {
$context = \context_system::instance();
$component = 'core_privacy';

$writer = $this->get_writer_instance()
->set_context($context)
->export_user_preference($component, $text, $text, $text);

$fileroot = $this->fetch_exported_content($writer);

$contextpath = $this->get_context_path($context, [get_string('userpreferences')], "{$component}.json");

$json = $fileroot->getChild($contextpath)->getContent();
$this->assertRegExp("/$text.*$text.*$text/", $json);

$expanded = json_decode($json);
$this->assertTrue(isset($expanded->$text));
$this->assertEquals($text, $expanded->$text->value);
$this->assertEquals($text, $expanded->$text->description);
}

/**
* Provider for various user preferences.
*
* @return array
*/
public function unescaped_unicode_export_provider() {
return [
'Unicode' => ['ةكءيٓ‌پچژکگیٹڈڑہھےâîûğŞAaÇÖáǽ你好!'],
];
}

/**
* Get a fresh content writer.
*
Expand Down

0 comments on commit 998fad0

Please sign in to comment.