From: Robin Appelman Date: Wed, 2 Dec 2015 15:48:15 +0000 (+0100) Subject: Fix mimetype filter in getDirectoryContent X-Git-Tag: v9.0beta1~570^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=19b2fe6a3ab4d9ded3c6974ac109d5d5b02251bb;p=nextcloud-server.git Fix mimetype filter in getDirectoryContent --- diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 7854790d0e5..6abefff4198 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -1427,13 +1427,9 @@ class View { if ($mimetype_filter) { $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { if (strpos($mimetype_filter, '/')) { - if ($file->getMimetype() === $mimetype_filter) { - $result[] = $file; - } + return $file->getMimetype() === $mimetype_filter; } else { - if ($file->getMimePart() === $mimetype_filter) { - $result[] = $file; - } + return $file->getMimePart() === $mimetype_filter; } }); } diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index 186cf28d7c3..1fc4b9ab684 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -2389,4 +2389,39 @@ class View extends \Test\TestCase { $view = new \OC\Files\View('/' . $this->user . '/files'); $this->assertEquals('foo', $view->rmdir('mount')); } + + public function mimeFilterProvider() { + return [ + [null, ['test1.txt', 'test2.txt', 'test3.md', 'test4.png']], + ['text/plain', ['test1.txt', 'test2.txt']], + ['text/markdown', ['test3.md']], + ['text', ['test1.txt', 'test2.txt', 'test3.md']], + ]; + } + + /** + * @param string $filter + * @param string[] $expected + * @dataProvider mimeFilterProvider + */ + public function testGetDirectoryContentMimeFilter($filter, $expected) { + $storage1 = new Temporary(); + $root = $this->getUniqueID('/'); + \OC\Files\Filesystem::mount($storage1, array(), $root . '/'); + $view = new \OC\Files\View($root); + + $view->file_put_contents('test1.txt', 'asd'); + $view->file_put_contents('test2.txt', 'asd'); + $view->file_put_contents('test3.md', 'asd'); + $view->file_put_contents('test4.png', ''); + + $content = $view->getDirectoryContent('', $filter); + + $files = array_map(function(FileInfo $info) { + return $info->getName(); + }, $content); + sort($files); + + $this->assertEquals($expected, $files); + } }