From 465d1ef495d1a21990251b81d14d7eff245c6df3 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Sun, 21 Feb 2021 23:30:11 +0100 Subject: [PATCH] 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 --- repository/lib.php | 5 +-- repository/tests/repositorylib_test.php | 41 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/repository/lib.php b/repository/lib.php index 6429f1c4805..08f3c250d01 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -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) diff --git a/repository/tests/repositorylib_test.php b/repository/tests/repositorylib_test.php index bdaac85bb0f..00211c9f37f 100644 --- a/repository/tests/repositorylib_test.php +++ b/repository/tests/repositorylib_test.php @@ -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']); + } }