Merge branch 'MDL-62285-master' of git://github.com/ryanwyllie/moodle

This commit is contained in:
Andrew Nicols
2018-05-08 11:08:03 +08:00
2 changed files with 64 additions and 6 deletions
@@ -244,7 +244,9 @@ class moodle_content_writer implements content_writer {
$path = [];
$contexts = array_reverse($this->context->get_parent_contexts(true));
foreach ($contexts as $context) {
$path[] = shorten_filename(clean_param($context->get_context_name(), PARAM_FILE), MAX_FILENAME_SIZE, true);
$name = $context->get_context_name();
$id = $context->id;
$path[] = shorten_filename(clean_param("{$name} {$id}", PARAM_FILE), MAX_FILENAME_SIZE, true);
}
return $path;
+61 -5
View File
@@ -677,6 +677,62 @@ class moodle_content_writer_test extends advanced_testcase {
$this->assertEquals($desc, $data->description);
}
/**
* Writing user preferences for two different blocks with the same name and
* same parent context should generate two different context paths and export
* files.
*/
public function test_export_user_preference_context_block_multiple_instances() {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$block1 = $generator->create_block('online_users', ['parentcontextid' => $coursecontext->id]);
$block2 = $generator->create_block('online_users', ['parentcontextid' => $coursecontext->id]);
$block1context = context_block::instance($block1->id);
$block2context = context_block::instance($block2->id);
$component = 'block';
$desc = 'test preference';
$block1key = 'block1key';
$block1value = 'block1value';
$block2key = 'block2key';
$block2value = 'block2value';
$writer = $this->get_writer_instance();
// Confirm that we have two different block contexts with the same name
// and the same parent context id.
$this->assertNotEquals($block1context->id, $block2context->id);
$this->assertEquals($block1context->get_context_name(), $block2context->get_context_name());
$this->assertEquals($block1context->get_parent_context()->id, $block2context->get_parent_context()->id);
$retrieveexport = function($context) use ($writer, $component) {
$fileroot = $this->fetch_exported_content($writer);
$contextpath = $this->get_context_path($context, [get_string('userpreferences')], "{$component}.json");
$this->assertTrue($fileroot->hasChild($contextpath));
$json = $fileroot->getChild($contextpath)->getContent();
return json_decode($json);
};
$writer->set_context($block1context)
->export_user_preference($component, $block1key, $block1value, $desc);
$writer->set_context($block2context)
->export_user_preference($component, $block2key, $block2value, $desc);
$block1export = $retrieveexport($block1context);
$block2export = $retrieveexport($block2context);
// Confirm that the exports didn't write to the same file.
$this->assertTrue(isset($block1export->$block1key));
$this->assertTrue(isset($block2export->$block2key));
$this->assertFalse(isset($block1export->$block2key));
$this->assertFalse(isset($block2export->$block1key));
$this->assertEquals($block1value, $block1export->$block1key->value);
$this->assertEquals($block2value, $block2export->$block2key->value);
}
/**
* User preferences can be exported against the system.
*
@@ -933,7 +989,7 @@ class moodle_content_writer_test extends advanced_testcase {
$fileroot = $this->fetch_exported_content($writer);
$contextpath = $this->get_context_path($context, $subcontext, 'data.json');
$expectedpath = 'System/'.$expected.'/data.json';
$expectedpath = "System {$context->id}/{$expected}/data.json";
$this->assertEquals($expectedpath, $contextpath);
$json = $fileroot->getChild($contextpath)->getContent();
@@ -963,7 +1019,7 @@ class moodle_content_writer_test extends advanced_testcase {
$fileroot = $this->fetch_exported_content($writer);
$contextpath = $this->get_context_path($context, $subcontext, 'name.json');
$expectedpath = 'System/'.$expected.'/name.json';
$expectedpath = "System {$context->id}/{$expected}/name.json";
$this->assertEquals($expectedpath, $contextpath);
$json = $fileroot->getChild($contextpath)->getContent();
@@ -993,7 +1049,7 @@ class moodle_content_writer_test extends advanced_testcase {
$fileroot = $this->fetch_exported_content($writer);
$contextpath = $this->get_context_path($context, $subcontext, 'metadata.json');
$expectedpath = 'System/'.$expected.'/metadata.json';
$expectedpath = "System {$context->id}/{$expected}/metadata.json";
$this->assertEquals($expectedpath, $contextpath);
$json = $fileroot->getChild($contextpath)->getContent();
@@ -1020,9 +1076,9 @@ class moodle_content_writer_test extends advanced_testcase {
// Add json as mime type to avoid lose the extension when shortening filenames.
core_filetypes::add_type('json', 'application/json', 'archive', [], '', 'JSON file archive');
}
$expectedpath = 'System/User preferences/'.$expected.'.json';
$context = \context_system::instance();
$expectedpath = "System {$context->id}/User preferences/{$expected}.json";
$component = $longtext;
$writer = $this->get_writer_instance()