MDL-64658 files: Support constant to avoid file merging
... in file_save_draft_area_files
This commit is contained in:
@@ -29,6 +29,12 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7');
|
||||
|
||||
|
||||
/**
|
||||
* Do not process file merging when working with draft area files.
|
||||
*/
|
||||
define('IGNORE_FILE_MERGE', -1);
|
||||
|
||||
/**
|
||||
* Unlimited area size constant
|
||||
*/
|
||||
@@ -888,6 +894,7 @@ function file_remove_editor_orphaned_files($editor) {
|
||||
*
|
||||
* @category files
|
||||
* @param int $draftitemid the id of the primary draft area.
|
||||
* When set to -1 (probably, by a WebService) it won't process file merging, keeping the original state of the file area.
|
||||
* @param int $usercontextid the user's context id.
|
||||
* @param string $text some html content that needs to have files copied to the correct draft area.
|
||||
* @param bool $forcehttps force https urls.
|
||||
@@ -899,6 +906,11 @@ function file_merge_draft_areas($draftitemid, $usercontextid, $text, $forcehttps
|
||||
return null;
|
||||
}
|
||||
|
||||
// Do not merge files, leave it as it was.
|
||||
if ($draftitemid === IGNORE_FILE_MERGE) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$urls = extract_draft_file_urls_from_text($text, $forcehttps, $usercontextid, 'user', 'draft');
|
||||
|
||||
// No draft areas to rewrite.
|
||||
@@ -1015,6 +1027,7 @@ function file_copy_file_to_file_area($file, $filename, $itemid) {
|
||||
* @global stdClass $USER
|
||||
* @param int $draftitemid the id of the draft area to use. Normally obtained
|
||||
* from file_get_submitted_draft_itemid('elementname') or similar.
|
||||
* When set to -1 (probably, by a WebService) it won't process file merging, keeping the original state of the file area.
|
||||
* @param int $contextid This parameter and the next two identify the file area to save to.
|
||||
* @param string $component
|
||||
* @param string $filearea indentifies the file area.
|
||||
@@ -1028,6 +1041,11 @@ function file_copy_file_to_file_area($file, $filename, $itemid) {
|
||||
function file_save_draft_area_files($draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null, $forcehttps=false) {
|
||||
global $USER;
|
||||
|
||||
// Do not merge files, leave it as it was.
|
||||
if ($draftitemid === IGNORE_FILE_MERGE) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$usercontext = context_user::instance($USER->id);
|
||||
$fs = get_file_storage();
|
||||
|
||||
|
||||
@@ -807,6 +807,70 @@ class core_filelib_testcase extends advanced_testcase {
|
||||
$this->assertEquals($filecontent, $fileref->get_content());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test avoid file merging when working with draft areas.
|
||||
*/
|
||||
public function test_ignore_file_merging_in_draft_area() {
|
||||
global $USER, $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
$user = $generator->create_user();
|
||||
$usercontext = context_user::instance($user->id);
|
||||
$USER = $DB->get_record('user', array('id' => $user->id));
|
||||
|
||||
$repositorypluginname = 'user';
|
||||
|
||||
$args = array();
|
||||
$args['type'] = $repositorypluginname;
|
||||
$repos = repository::get_instances($args);
|
||||
$userrepository = reset($repos);
|
||||
$this->assertInstanceOf('repository', $userrepository);
|
||||
|
||||
$fs = get_file_storage();
|
||||
$syscontext = context_system::instance();
|
||||
|
||||
$filecontent = 'User file content';
|
||||
|
||||
// Create a user private file.
|
||||
$userfilerecord = new stdClass;
|
||||
$userfilerecord->contextid = $usercontext->id;
|
||||
$userfilerecord->component = 'user';
|
||||
$userfilerecord->filearea = 'private';
|
||||
$userfilerecord->itemid = 0;
|
||||
$userfilerecord->filepath = '/';
|
||||
$userfilerecord->filename = 'userfile.txt';
|
||||
$userfilerecord->source = 'test';
|
||||
$userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
|
||||
$userfileref = $fs->pack_reference($userfilerecord);
|
||||
$contenthash = $userfile->get_contenthash();
|
||||
|
||||
$filerecord = array(
|
||||
'contextid' => $syscontext->id,
|
||||
'component' => 'core',
|
||||
'filearea' => 'phpunit',
|
||||
'itemid' => 0,
|
||||
'filepath' => '/',
|
||||
'filename' => 'test.txt',
|
||||
);
|
||||
// Create a file reference.
|
||||
$fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
|
||||
$this->assertCount(2, $fs->get_area_files($usercontext->id, 'user', 'private')); // 2 because includes the '.' file.
|
||||
|
||||
// Save using empty draft item id, all files will be deleted.
|
||||
file_save_draft_area_files(0, $usercontext->id, 'user', 'private', 0);
|
||||
$this->assertCount(0, $fs->get_area_files($usercontext->id, 'user', 'private'));
|
||||
|
||||
// Create a file again.
|
||||
$userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
|
||||
$this->assertCount(2, $fs->get_area_files($usercontext->id, 'user', 'private'));
|
||||
|
||||
// Save without merge.
|
||||
file_save_draft_area_files(IGNORE_FILE_MERGE, $usercontext->id, 'user', 'private', 0);
|
||||
$this->assertCount(2, $fs->get_area_files($usercontext->id, 'user', 'private'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the strip_double_headers function in the curl class.
|
||||
*/
|
||||
|
||||
@@ -22,6 +22,8 @@ attribute on forms to avoid collisions in forms loaded in AJAX requests.
|
||||
* Behat timeout constants behat_base::TIMEOUT, EXTENDED_TIMEOUT, and REDUCED_TIMEOUT have been
|
||||
deprecated. Please instead use the functions behat_base::get_timeout(), get_extended_timeout(),
|
||||
and get_reduced_timeout(). These allow for timeouts to be increased by a setting in config.php.
|
||||
* The $draftitemid parameter of file_save_draft_area_files() function now supports the constant IGNORE_FILE_MERGE:
|
||||
When the parameter is set to that constant, the function won't process file merging, keeping the original state of the file area.
|
||||
|
||||
=== 3.6 ===
|
||||
|
||||
|
||||
Reference in New Issue
Block a user