Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode mod_cms content on restore #125

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backup/moodle2/restore_cms_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static function define_decode_contents() {

$contents[] = new restore_decode_content('cms', ['intro']);
$contents[] = new restore_decode_content('cms_types', ['description']);
$contents[] = new restore_decode_content('customfield_data', 'value');

return $contents;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/local/datasource/restore/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function process_restore_ds_fields(array $data) {
/**
* Code to be run after restoration.
*/
public function after_execute() {
public function after_restore() {
$cmsid = $this->stepslib->get_new_parentid('cms');
$cms = new cms($cmsid);
$ds = new dsfields($cms);
Expand Down
50 changes: 43 additions & 7 deletions tests/datasource_fields_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,21 @@ public function test_file_backup_and_restore() {

$fileid = $this->get_generator()->make_file($filename, 'Some content');

// Create data for making a module. Add the file to the custom field.
// File from other place in the server.
$syscontext = \context_system::instance();
$filedata = [
'contextid' => $syscontext->id,
'component' => 'course',
'filearea' => 'unittest',
'itemid' => 0,
'filepath' => '/textfiles/',
'filename' => 'testtext.txt',
];
$fs->create_file_from_string($filedata, 'text contents');
$url = \moodle_url::make_pluginfile_url($filedata['contextid'], $filedata['component'], $filedata['filearea'],
$filedata['itemid'], $filedata['filepath'], $filedata['filename']);

// Create data for making a module. Add the files to the custom field.
$instancedata = [
'modulename' => 'cms',
'course' => $course->id,
Expand All @@ -381,7 +395,7 @@ public function test_file_backup_and_restore() {
'typeid' => $cmstype->get('id'),
'name' => 'Some module',
'customfield_field1_editor' => [
'text' => 'Here is a file: @@PLUGINFILE@@/'.$filename,
'text' => 'Here is a file: @@PLUGINFILE@@/' . $filename . ' AND ' . $url->out(),
'format' => FORMAT_HTML,
'itemid' => $fileid,
]
Expand All @@ -394,8 +408,11 @@ public function test_file_backup_and_restore() {

// Get the data ID to find the file with.
$cfhandler = cmsfield_handler::create($cmstype->get('id'));
$d = $cfhandler->get_instance_data($cms->get('id'));
$itemid = $d[$cffield->get('id')]->get('id');
$instancedata = $cfhandler->get_instance_data($cms->get('id'));
$fielddata = $instancedata[$cffield->get('id')];
$itemid = $fielddata->get('id');
$originaltext = $fielddata->get('value');
$originalexportvalue = $fielddata->export_value();

// Check if the permanent file exists.
$file = $fs->get_file($context->id, 'customfield_textarea', 'value', $itemid, '/', $filename);
Expand All @@ -407,17 +424,36 @@ public function test_file_backup_and_restore() {
$newcontext = context_module::instance($newcm->id);

// Get the data ID to find the new file with.
$d = $cfhandler->get_instance_data($newcms->get('id'));
$itemid = $d[$cffield->get('id')]->get('id');
$newinstancedata = $cfhandler->get_instance_data($newcms->get('id'));
$newfielddata = $newinstancedata[$cffield->get('id')];
$newitemid = $newfielddata->get('id');
$newtext = $newfielddata->get('value');
$newexportvalue = $newfielddata->export_value();

// Check if the permanent file exists.
$newfile = $fs->get_file($newcontext->id, 'customfield_textarea', 'value', $itemid, '/', $filename);
$newfile = $fs->get_file($newcontext->id, 'customfield_textarea', 'value', $newitemid, '/', $filename);
$this->assertNotEmpty($newfile);

// Check that the files are distinct.
$this->assertNotEquals($file->get_id(), $newfile->get_id());

// Check the files have the same content.
$this->assertEquals($file->get_content(), $newfile->get_content());

// Value should be same but export value should have different URL.
$this->assertEquals($originaltext, $newtext);
$this->assertNotEquals($originalexportvalue, $newexportvalue);

// Check the URL is using correct ids.
$this->assertStringContainsString(
'/' . $context->id . '/customfield_textarea/value/' . $itemid . '/' . $filename,
$originalexportvalue);
$this->assertStringContainsString(
'/' . $newcontext->id . '/customfield_textarea/value/' . $newitemid . '/' . $filename,
$newexportvalue);

// Check URL is correctly restored.
$this->assertStringContainsString($url->out(), $originalexportvalue);
$this->assertStringContainsString($url->out(), $newexportvalue);
}
}
Loading