Skip to content

Commit

Permalink
MDL-70921 repository: filter function can not accept argument by ref
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marinaglancy committed Feb 25, 2021
1 parent a93828a commit 465d1ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 1 addition & 4 deletions repository/lib.php
Expand Up @@ -2139,12 +2139,9 @@ public function get_option($config = '') {
* @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)
Expand Down
41 changes: 41 additions & 0 deletions repository/tests/repositorylib_test.php
Expand Up @@ -596,4 +596,45 @@ function test_delete_all_for_context() {
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']);
}
}

0 comments on commit 465d1ef

Please sign in to comment.