mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-36985 core_files: added option to remove draft files on save
text areas can now specify $options['removeorphaneddrafts'] when saving their data using file_postupdate_standard_editor(). If set to true, this option clears all user drafts which are not referenced in the text
This commit is contained in:
parent
27466d7548
commit
f3c2afd0e8
3 changed files with 91 additions and 6 deletions
|
@ -1340,6 +1340,51 @@ EOF;
|
|||
$this->assertEquals(0, $fileinfo['foldercount']); // No subdirectories inside the directory.
|
||||
$this->assertEquals($file->get_filesize(), $fileinfo['filesize_without_references']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming that draft files not referenced in the editor text are removed.
|
||||
*/
|
||||
public function test_file_remove_editor_orphaned_files() {
|
||||
global $USER, $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create three draft files.
|
||||
$filerecord = ['filename' => 'file1.png'];
|
||||
$file = self::create_draft_file($filerecord);
|
||||
$draftitemid = $file->get_itemid();
|
||||
|
||||
$filerecord['itemid'] = $draftitemid;
|
||||
|
||||
$filerecord['filename'] = 'file2.png';
|
||||
self::create_draft_file($filerecord);
|
||||
|
||||
$filerecord['filename'] = 'file 3.png';
|
||||
self::create_draft_file($filerecord);
|
||||
|
||||
// Confirm the user drafts area lists 3 files.
|
||||
$fs = get_file_storage();
|
||||
$usercontext = context_user::instance($USER->id);
|
||||
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'itemid', 0);
|
||||
$this->assertCount(3, $draftfiles);
|
||||
|
||||
// Now, spoof some editor text content, referencing 2 of the files; one requiring name encoding, one not.
|
||||
$editor = [
|
||||
'itemid' => $draftitemid,
|
||||
'text' => '
|
||||
<img src="'.$CFG->wwwroot.'/draftfile.php/'.$usercontext->id.'/user/draft/'.$draftitemid.'/file%203.png" alt="">
|
||||
<img src="'.$CFG->wwwroot.'/draftfile.php/'.$usercontext->id.'/user/draft/'.$draftitemid.'/file1.png" alt="">'
|
||||
];
|
||||
|
||||
// Run the remove orphaned drafts function and confirm that only the referenced files remain in the user drafts.
|
||||
$expected = ['file1.png', 'file 3.png']; // The drafts we expect will not be removed (are referenced in the online text).
|
||||
file_remove_editor_orphaned_files($editor);
|
||||
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'itemid', 0);
|
||||
$this->assertCount(2, $draftfiles);
|
||||
foreach ($draftfiles as $file) {
|
||||
$this->assertContains($file->get_filename(), $expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue