mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-70976 core_files: Allow for draft files url inserted in content
* The file file_remove_editor_orphaned_files should take into account URL that have been embedded in a tag content instead of an attribute (like src attribute) * This will fix issue with inserting H5P content in calendar events.
This commit is contained in:
parent
033858e249
commit
8ee689f612
@ -928,7 +928,7 @@ function file_remove_editor_orphaned_files($editor) {
|
||||
// Find those draft files included in the text, and generate their hashes.
|
||||
$context = context_user::instance($USER->id);
|
||||
$baseurl = $CFG->wwwroot . '/draftfile.php/' . $context->id . '/user/draft/' . $editor['itemid'] . '/';
|
||||
$pattern = "/" . preg_quote($baseurl, '/') . "(.+?)[\?\"']/";
|
||||
$pattern = "/" . preg_quote($baseurl, '/') . "(.+?)[\?\"'<>\s:\\\\]/";
|
||||
preg_match_all($pattern, $editor['text'], $matches);
|
||||
$usedfilehashes = [];
|
||||
foreach ($matches[1] as $matchedfilename) {
|
||||
|
@ -1725,25 +1725,30 @@ EOF;
|
||||
$filerecord['filename'] = 'file 3.png';
|
||||
self::create_draft_file($filerecord);
|
||||
|
||||
$filerecord['filename'] = 'file4.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);
|
||||
$this->assertCount(4, $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="">'
|
||||
'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=\"\">
|
||||
<span>{$CFG->wwwroot}/draftfile.php/{$usercontext->id}/user/draft/{$draftitemid}/file4.png</span>"
|
||||
];
|
||||
|
||||
// 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).
|
||||
// The drafts we expect will not be removed (are referenced in the online text).
|
||||
$expected = ['file1.png', 'file 3.png', 'file4.png'];
|
||||
file_remove_editor_orphaned_files($editor);
|
||||
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'itemid', 0);
|
||||
$this->assertCount(2, $draftfiles);
|
||||
$this->assertCount(3, $draftfiles);
|
||||
foreach ($draftfiles as $file) {
|
||||
$this->assertContains($file->get_filename(), $expected);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user