@@ -372,7 +372,21 @@ public function test_file_backup_and_restore() {
372
372
373
373
$ fileid = $ this ->get_generator ()->make_file ($ filename , 'Some content ' );
374
374
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.
376
390
$ instancedata = [
377
391
'modulename ' => 'cms ' ,
378
392
'course ' => $ course ->id ,
@@ -381,7 +395,7 @@ public function test_file_backup_and_restore() {
381
395
'typeid ' => $ cmstype ->get ('id ' ),
382
396
'name ' => 'Some module ' ,
383
397
'customfield_field1_editor ' => [
384
- 'text ' => 'Here is a file: @@PLUGINFILE@@/ ' . $ filename ,
398
+ 'text ' => 'Here is a file: @@PLUGINFILE@@/ ' . $ filename . ' AND ' . $ url -> out () ,
385
399
'format ' => FORMAT_HTML ,
386
400
'itemid ' => $ fileid ,
387
401
]
@@ -394,8 +408,11 @@ public function test_file_backup_and_restore() {
394
408
395
409
// Get the data ID to find the file with.
396
410
$ 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 ();
399
416
400
417
// Check if the permanent file exists.
401
418
$ file = $ fs ->get_file ($ context ->id , 'customfield_textarea ' , 'value ' , $ itemid , '/ ' , $ filename );
@@ -407,17 +424,36 @@ public function test_file_backup_and_restore() {
407
424
$ newcontext = context_module::instance ($ newcm ->id );
408
425
409
426
// 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 ();
412
432
413
433
// 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 );
415
435
$ this ->assertNotEmpty ($ newfile );
416
436
417
437
// Check that the files are distinct.
418
438
$ this ->assertNotEquals ($ file ->get_id (), $ newfile ->get_id ());
419
439
420
440
// Check the files have the same content.
421
441
$ 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 );
422
458
}
423
459
}
0 commit comments