Merge branch 'MDL-70921-master' of git://github.com/marinaglancy/moodle

This commit is contained in:
Sara Arjona 2021-03-03 15:24:44 +01:00
commit 1285a8d868
2 changed files with 42 additions and 4 deletions

View File

@ -2139,12 +2139,9 @@ abstract class repository implements cacheable_object {
* @param array $value
* @return bool
*/
public function filter(&$value) {
public function filter($value) {
$accepted_types = optional_param_array('accepted_types', '', PARAM_RAW);
if (isset($value['children'])) {
if (!empty($value['children'])) {
$value['children'] = array_filter($value['children'], array($this, 'filter'));
}
return true; // always return directories
} else {
if ($accepted_types == '*' or empty($accepted_types)

View File

@ -596,4 +596,45 @@ class core_repositorylib_testcase extends advanced_testcase {
delete_user($user);
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
}
/**
* Create test file in user private files
*
* @param string $filepath file path
* @param string $filename file name
*/
private function create_user_private_file(string $filepath, string $filename): void {
global $USER;
$filerecord = [];
$filerecord['contextid'] = context_user::instance($USER->id)->id;
$filerecord['component'] = 'user';
$filerecord['filearea'] = 'private';
$filerecord['itemid'] = 0;
$filerecord['filepath'] = $filepath;
$filerecord['filename'] = $filename;
$filerecord['userid'] = $USER->id;
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, hash("md5", $filepath . $filename));
}
public function test_listing_and_filter() {
$this->resetAfterTest(true);
$this->setUser($this->getDataGenerator()->create_user());
$repoid = $this->getDataGenerator()->create_repository('user')->id;
$this->create_user_private_file('/', 'image1.jpg');
$this->create_user_private_file('/', 'file1.txt');
$this->create_user_private_file('/folder/', 'image2.jpg');
$this->create_user_private_file('/folder/', 'file2.txt');
$this->create_user_private_file('/ftexts/', 'file3.txt');
// Listing without filters returns 4 records (2 files and 2 directories).
$repo = repository::get_repository_by_id($repoid, context_system::instance());
$this->assertCount(4, $repo->get_listing()['list']);
// Listing with filters returns 3 records (1 files and 2 directories).
$_POST['accepted_types'] = ['.jpg'];
$this->assertCount(3, $repo->get_listing()['list']);
}
}