|
16 | 16 |
|
17 | 17 | namespace mod_cms;
|
18 | 18 |
|
| 19 | +use context_module; |
19 | 20 | use core_customfield\{category_controller, field_controller};
|
20 | 21 | use mod_cms\customfield\cmsfield_handler;
|
21 | 22 | use mod_cms\local\datasource\fields as dsfields;
|
22 | 23 | use mod_cms\local\model\cms;
|
23 | 24 | use mod_cms\local\model\cms_types;
|
| 25 | +use mod_cms_generator; |
24 | 26 |
|
25 | 27 | defined('MOODLE_INTERNAL') || die();
|
26 | 28 |
|
@@ -51,6 +53,14 @@ protected function setUp(): void {
|
51 | 53 | $this->setAdminUser();
|
52 | 54 | }
|
53 | 55 |
|
| 56 | + /** |
| 57 | + * Get generator for mod_cms. |
| 58 | + * @return mod_cms_generator |
| 59 | + */ |
| 60 | + protected function get_generator(): mod_cms_generator { |
| 61 | + return $this->getDataGenerator()->get_plugin_generator('mod_cms'); |
| 62 | + } |
| 63 | + |
54 | 64 | /**
|
55 | 65 | * Tests short name.
|
56 | 66 | *
|
@@ -335,4 +345,79 @@ public function test_duplicate() {
|
335 | 345 | // Assert that the CMS type is not duplicated.
|
336 | 346 | $this->assertEquals($cms->get('typeid'), $newcms->get('typeid'));
|
337 | 347 | }
|
| 348 | + |
| 349 | + /** |
| 350 | + * Tests backup and restore of embedded files in textarea fields. |
| 351 | + * |
| 352 | + * @covers \mod_cms\local\datasource\fields::instance_backup_define_structure |
| 353 | + * @covers \mod_cms\local\datasource\fields::restore_define_structure |
| 354 | + */ |
| 355 | + public function test_file_backup_and_restore() { |
| 356 | + if (!method_exists('\core_customfield\handler', 'backup_define_structure')) { |
| 357 | + $this->markTestSkipped('Only test if backup and restore is supported for embedded files.'); |
| 358 | + } |
| 359 | + |
| 360 | + $filename = 'somefilename.txt'; |
| 361 | + |
| 362 | + $course = $this->getDataGenerator()->create_course(); |
| 363 | + $cmstype = $this->get_generator()->create_cms_type(['datasources' => 'fields']); |
| 364 | + $category = $this->get_generator()->create_datasource_fields_category($cmstype); |
| 365 | + $cffield = $this->get_generator()->create_datasource_fields_field([ |
| 366 | + 'categoryid' => $category->get('id'), |
| 367 | + 'shortname' => 'field1', |
| 368 | + 'type' => 'textarea' |
| 369 | + ]); |
| 370 | + |
| 371 | + $fs = get_file_storage(); |
| 372 | + |
| 373 | + $fileid = $this->get_generator()->make_file($filename, 'Some content'); |
| 374 | + |
| 375 | + // Create data for making a module. Add the file to the custom field. |
| 376 | + $instancedata = [ |
| 377 | + 'modulename' => 'cms', |
| 378 | + 'course' => $course->id, |
| 379 | + 'section' => 0, |
| 380 | + 'visible' => true, |
| 381 | + 'typeid' => $cmstype->get('id'), |
| 382 | + 'name' => 'Some module', |
| 383 | + 'customfield_field1_editor' => [ |
| 384 | + 'text' => 'Here is a file: @@PLUGINFILE@@/'.$filename, |
| 385 | + 'format' => FORMAT_HTML, |
| 386 | + 'itemid' => $fileid, |
| 387 | + ] |
| 388 | + ]; |
| 389 | + |
| 390 | + $module = create_module((object) $instancedata); |
| 391 | + $cm = get_coursemodule_from_id('', $module->coursemodule, 0, false, MUST_EXIST); |
| 392 | + $cms = new cms($cm->instance); |
| 393 | + $context = context_module::instance($cm->id); |
| 394 | + |
| 395 | + // Get the data ID to find the file with. |
| 396 | + $cfhandler = cmsfield_handler::create($cmstype->get('id')); |
| 397 | + $d = $cfhandler->get_instance_data($cms->get('id')); |
| 398 | + $itemid = $d[$cffield->get('id')]->get('id'); |
| 399 | + |
| 400 | + // Check if the permanent file exists. |
| 401 | + $file = $fs->get_file($context->id, 'customfield_textarea', 'value', $itemid, '/', $filename); |
| 402 | + $this->assertNotEmpty($file); |
| 403 | + |
| 404 | + // Duplicate the module (which is done via backup and restore). |
| 405 | + $newcm = duplicate_module($course, $cm); |
| 406 | + $newcms = new cms($newcm->instance); |
| 407 | + $newcontext = context_module::instance($newcm->id); |
| 408 | + |
| 409 | + // 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'); |
| 412 | + |
| 413 | + // Check if the permanent file exists. |
| 414 | + $newfile = $fs->get_file($newcontext->id, 'customfield_textarea', 'value', $itemid, '/', $filename); |
| 415 | + $this->assertNotEmpty($newfile); |
| 416 | + |
| 417 | + // Check that the files are distinct. |
| 418 | + $this->assertNotEquals($file->get_id(), $newfile->get_id()); |
| 419 | + |
| 420 | + // Check the files have the same content. |
| 421 | + $this->assertEquals($file->get_content(), $newfile->get_content()); |
| 422 | + } |
338 | 423 | }
|
0 commit comments