Skip to content

Commit 30bc87d

Browse files
committed
Add phpunit test for decoding
1 parent 5e1ead0 commit 30bc87d

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

tests/datasource_fields_test.php

+43-7
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,21 @@ public function test_file_backup_and_restore() {
372372

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

375-
// Create data for making a module. Add the file to the custom field.
375+
// File from other place in the server.
376+
$syscontext = \context_system::instance();
377+
$filedata = [
378+
'contextid' => $syscontext->id,
379+
'component' => 'course',
380+
'filearea' => 'unittest',
381+
'itemid' => 0,
382+
'filepath' => '/textfiles/',
383+
'filename' => 'testtext.txt',
384+
];
385+
$fs->create_file_from_string($filedata, 'text contents');
386+
$url = \moodle_url::make_pluginfile_url($filedata['contextid'], $filedata['component'], $filedata['filearea'],
387+
$filedata['itemid'], $filedata['filepath'], $filedata['filename']);
388+
389+
// Create data for making a module. Add the files to the custom field.
376390
$instancedata = [
377391
'modulename' => 'cms',
378392
'course' => $course->id,
@@ -381,7 +395,7 @@ public function test_file_backup_and_restore() {
381395
'typeid' => $cmstype->get('id'),
382396
'name' => 'Some module',
383397
'customfield_field1_editor' => [
384-
'text' => 'Here is a file: @@PLUGINFILE@@/'.$filename,
398+
'text' => 'Here is a file: @@PLUGINFILE@@/' . $filename . ' AND ' . $url->out(),
385399
'format' => FORMAT_HTML,
386400
'itemid' => $fileid,
387401
]
@@ -394,8 +408,11 @@ public function test_file_backup_and_restore() {
394408

395409
// Get the data ID to find the file with.
396410
$cfhandler = cmsfield_handler::create($cmstype->get('id'));
397-
$d = $cfhandler->get_instance_data($cms->get('id'));
398-
$itemid = $d[$cffield->get('id')]->get('id');
411+
$instancedata = $cfhandler->get_instance_data($cms->get('id'));
412+
$fielddata = $instancedata[$cffield->get('id')];
413+
$itemid = $fielddata->get('id');
414+
$originaltext = $fielddata->get('value');
415+
$originalexportvalue = $fielddata->export_value();
399416

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

409426
// Get the data ID to find the new file with.
410-
$d = $cfhandler->get_instance_data($newcms->get('id'));
411-
$itemid = $d[$cffield->get('id')]->get('id');
427+
$newinstancedata = $cfhandler->get_instance_data($newcms->get('id'));
428+
$newfielddata = $newinstancedata[$cffield->get('id')];
429+
$newitemid = $newfielddata->get('id');
430+
$newtext = $newfielddata->get('value');
431+
$newexportvalue = $newfielddata->export_value();
412432

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

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

420440
// Check the files have the same content.
421441
$this->assertEquals($file->get_content(), $newfile->get_content());
442+
443+
// Value should be same but export value should have different URL.
444+
$this->assertEquals($originaltext, $newtext);
445+
$this->assertNotEquals($originalexportvalue, $newexportvalue);
446+
447+
// Check the URL is using correct ids.
448+
$this->assertStringContainsString(
449+
'/' . $context->id . '/customfield_textarea/value/' . $itemid . '/' . $filename,
450+
$originalexportvalue);
451+
$this->assertStringContainsString(
452+
'/' . $newcontext->id . '/customfield_textarea/value/' . $newitemid . '/' . $filename,
453+
$newexportvalue);
454+
455+
// Check URL is correctly restored.
456+
$this->assertStringContainsString($url->out(), $originalexportvalue);
457+
$this->assertStringContainsString($url->out(), $newexportvalue);
422458
}
423459
}

0 commit comments

Comments
 (0)