MDL-70921 repository: filter function can not accept argument by ref

PHP8.0 shows error that filter function accepts value by reference
The nested filtering was never working but also this function was never called on a tree with sub-levels,
so this logic is now removed
This commit is contained in:
Marina Glancy
2021-02-25 18:24:52 +01:00
parent a93828a188
commit 465d1ef495
2 changed files with 42 additions and 4 deletions
+1 -4
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)
+41
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']);
}
}