From 19b2fe6a3ab4d9ded3c6974ac109d5d5b02251bb Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 2 Dec 2015 16:48:15 +0100 Subject: [PATCH] Fix mimetype filter in getDirectoryContent --- lib/private/files/view.php | 8 ++------ tests/lib/files/view.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 7854790d0e..6abefff419 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 186cf28d7c..1fc4b9ab68 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); + } }