Skip to content

Commit

Permalink
Merge branch 'MDL-62849-34' of git://github.com/abgreeve/moodle into …
Browse files Browse the repository at this point in the history
…MOODLE_34_STABLE
  • Loading branch information
andrewnicols committed Jul 17, 2018
2 parents b88a479 + f1900fa commit 52a3e01
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,34 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {
return $data;
}

/**
* Returns all of the files in the draftarea.
*
* @param int $draftitemid The draft item ID
* @param string $filepath path for the uploaded files.
* @return array An array of files associated with this draft item id.
*/
function file_get_all_files_in_draftarea(int $draftitemid, string $filepath = '/') : array {
$files = [];
$draftfiles = file_get_drafarea_files($draftitemid, $filepath);
file_get_drafarea_folders($draftitemid, $filepath, $draftfiles);

if (!empty($draftfiles)) {
foreach ($draftfiles->list as $draftfile) {
if ($draftfile->type == 'file') {
$files[] = $draftfile;
}
}

if (isset($draftfiles->children)) {
foreach ($draftfiles->children as $draftfile) {
$files = array_merge($files, file_get_all_files_in_draftarea($draftitemid, $draftfile->filepath));
}
}
}
return $files;
}

/**
* Returns draft area itemid for a given element.
*
Expand Down
4 changes: 2 additions & 2 deletions lib/form/filemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ public function validateSubmitValue($value) {
return;
}

$draftfiles = file_get_drafarea_files($value);
$draftfiles = file_get_all_files_in_draftarea($value);
$wrongfiles = array();

if (empty($draftfiles)) {
// No file uploaded, nothing to check here.
return;
}

foreach ($draftfiles->list as $file) {
foreach ($draftfiles as $file) {
if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
$wrongfiles[] = $file->filename;
}
Expand Down
49 changes: 49 additions & 0 deletions lib/tests/filelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,55 @@ public function test_file_remove_editor_orphaned_files() {
$this->assertContains($file->get_filename(), $expected);
}
}

/**
* Test that all files in the draftarea are returned.
*/
public function test_file_get_all_files_in_draftarea() {
$this->resetAfterTest();
$this->setAdminUser();

$filerecord = ['filename' => 'basepic.jpg'];
$file = self::create_draft_file($filerecord);

$secondrecord = [
'filename' => 'infolder.jpg',
'filepath' => '/assignment/',
'itemid' => $file->get_itemid()
];
$file = self::create_draft_file($secondrecord);

$thirdrecord = [
'filename' => 'deeperfolder.jpg',
'filepath' => '/assignment/pics/',
'itemid' => $file->get_itemid()
];
$file = self::create_draft_file($thirdrecord);

$fourthrecord = [
'filename' => 'differentimage.jpg',
'filepath' => '/secondfolder/',
'itemid' => $file->get_itemid()
];
$file = self::create_draft_file($fourthrecord);

// This record has the same name as the last record, but it's in a different folder.
// Just checking this is also returned.
$fifthrecord = [
'filename' => 'differentimage.jpg',
'filepath' => '/assignment/pics/',
'itemid' => $file->get_itemid()
];
$file = self::create_draft_file($fifthrecord);

$allfiles = file_get_all_files_in_draftarea($file->get_itemid());
$this->assertCount(5, $allfiles);
$this->assertEquals($filerecord['filename'], $allfiles[0]->filename);
$this->assertEquals($secondrecord['filename'], $allfiles[1]->filename);
$this->assertEquals($thirdrecord['filename'], $allfiles[2]->filename);
$this->assertEquals($fourthrecord['filename'], $allfiles[3]->filename);
$this->assertEquals($fifthrecord['filename'], $allfiles[4]->filename);
}
}

/**
Expand Down

0 comments on commit 52a3e01

Please sign in to comment.