diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index e3bee14587..21f807f353 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -344,6 +344,20 @@ class Shared_Cache extends Cache { return $result; } + /** + * Checks whether the given file has the given tag. + * + * @param \OCP\ITags $tagger + * @param array $fileData file data + * @param string $tag tag to check for + * @return boolean true if the given file has the expected tag, + * false otherwise + */ + private function hasTag($tagger, $fileData, $tag) { + $tags = $tagger->getTagsForObjects(array((int)$fileData['fileid'])); + return (!empty($tags) && in_array($tag, current($tags))); + } + /** * search for files by tag * @@ -356,28 +370,24 @@ class Shared_Cache extends Cache { $tagger = \OC::$server->getTagManager()->load('files', null, null, $userId); $result = array(); $exploreDirs = array(''); + // check if root is tagged + $file = $this->get(''); + if ($this->hasTag($tagger, $file, $tag)) { + $result[] = $file; + } // FIXME: this is so wrong and unefficient, need to replace with actual DB queries while (count($exploreDirs) > 0) { $dir = array_pop($exploreDirs); $files = $this->getFolderContents($dir); - // no results? if (!$files) { - // maybe it's a single shared file - $file = $this->get(''); - $tags = $tagger->getTagsForObjects(array((int)$file['fileid'])); - if (!empty($tags) && in_array($tag, current($tags))) { - $result[] = $file; - } continue; } foreach ($files as $file) { + if ($this->hasTag($tagger, $file, $tag)) { + $result[] = $file; + } if ($file['mimetype'] === 'httpd/unix-directory') { $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/'); - } else { - $tags = $tagger->getTagsForObjects(array((int)$file['fileid'])); - if (!empty($tags) && in_array($tag, current($tags))) { - $result[] = $file; - } } } } diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php index b60bba73db..f3f8f924b4 100644 --- a/apps/files_sharing/tests/cache.php +++ b/apps/files_sharing/tests/cache.php @@ -22,7 +22,6 @@ use OCA\Files_sharing\Tests\TestCase; * License along with this library. If not, see . * */ - class Test_Files_Sharing_Cache extends TestCase { /** @@ -238,6 +237,62 @@ class Test_Files_Sharing_Cache extends TestCase { $tagManager->delete(array('tag1', 'tag2')); } + /** + * Test searching by tag for multiple sections of the tree + */ + function testSearchByTagTree() { + $userId = \OC::$server->getUserSession()->getUser()->getUId(); + $this->sharedStorage->mkdir('subdir/emptydir'); + $this->sharedStorage->mkdir('subdir/emptydir2'); + $this->ownerStorage->getScanner()->scan(''); + $allIds = array( + $this->sharedCache->get('')['fileid'], + $this->sharedCache->get('bar.txt')['fileid'], + $this->sharedCache->get('subdir/another too.txt')['fileid'], + $this->sharedCache->get('subdir/not a text file.xml')['fileid'], + $this->sharedCache->get('subdir/another.txt')['fileid'], + $this->sharedCache->get('subdir/emptydir')['fileid'], + $this->sharedCache->get('subdir/emptydir2')['fileid'], + ); + $tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId); + foreach ($allIds as $id) { + $tagManager->tagAs($id, 'tag1'); + } + $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId); + $check = array( + array( + 'name' => 'shareddir', + 'path' => '' + ), + array( + 'name' => 'bar.txt', + 'path' => 'bar.txt' + ), + array( + 'name' => 'another.txt', + 'path' => 'subdir/another.txt' + ), + array( + 'name' => 'another too.txt', + 'path' => 'subdir/another too.txt' + ), + array( + 'name' => 'emptydir', + 'path' => 'subdir/emptydir' + ), + array( + 'name' => 'emptydir2', + 'path' => 'subdir/emptydir2' + ), + array( + 'name' => 'not a text file.xml', + 'path' => 'subdir/not a text file.xml' + ), + ); + $this->verifyFiles($check, $results); + $tagManager->delete(array('tag1')); + } + function testGetFolderContentsInRoot() { $results = $this->user2View->getDirectoryContent('/');