From 5e1ead0458187dc5e9661574c134658570d8212d Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Thu, 23 May 2024 11:22:40 +0800 Subject: [PATCH 1/2] Decode mod_cms content on restore --- backup/moodle2/restore_cms_activity_task.class.php | 1 + classes/local/datasource/restore/fields.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/backup/moodle2/restore_cms_activity_task.class.php b/backup/moodle2/restore_cms_activity_task.class.php index e3ea738..f82a940 100644 --- a/backup/moodle2/restore_cms_activity_task.class.php +++ b/backup/moodle2/restore_cms_activity_task.class.php @@ -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; } diff --git a/classes/local/datasource/restore/fields.php b/classes/local/datasource/restore/fields.php index 86f91e7..de7f986 100644 --- a/classes/local/datasource/restore/fields.php +++ b/classes/local/datasource/restore/fields.php @@ -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); From 30bc87dac50ef73d7dcd5563e4562960becb37be Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Tue, 27 Aug 2024 10:29:28 +1000 Subject: [PATCH 2/2] Add phpunit test for decoding --- tests/datasource_fields_test.php | 50 +++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tests/datasource_fields_test.php b/tests/datasource_fields_test.php index 2b3421f..04f4711 100644 --- a/tests/datasource_fields_test.php +++ b/tests/datasource_fields_test.php @@ -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, @@ -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, ] @@ -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); @@ -407,11 +424,14 @@ 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. @@ -419,5 +439,21 @@ public function test_file_backup_and_restore() { // 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); } }