From 15ecb28d50e5b8ce4100075caa52d96d4f00ae13 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 12 Dec 2014 11:18:35 +0100 Subject: [PATCH] Make $userId mandatory for searchByTags $userId is now a mandatory parameter for searchByTags. Also fixed some places in the code where the argument was missing (Node API and View) --- apps/files_sharing/lib/cache.php | 2 +- apps/files_sharing/tests/cache.php | 5 ++- lib/private/files/cache/cache.php | 5 +-- .../files/cache/wrapper/cachewrapper.php | 2 +- lib/private/files/filesystem.php | 5 ++- lib/private/files/node/folder.php | 21 +++++----- lib/private/files/node/nonexistingfolder.php | 2 +- lib/private/files/view.php | 21 +++++----- lib/public/files/folder.php | 3 +- tests/lib/files/node/folder.php | 39 +++++++++++++++++++ 10 files changed, 73 insertions(+), 32 deletions(-) diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index da8155ec6f..e3bee14587 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -351,7 +351,7 @@ class Shared_Cache extends Cache { * @param string $userId owner of the tags * @return array file data */ - public function searchByTag($tag, $userId = null) { + public function searchByTag($tag, $userId) { // TODO: inject this $tagger = \OC::$server->getTagManager()->load('files', null, null, $userId); $result = array(); diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php index c40a014d55..b60bba73db 100644 --- a/apps/files_sharing/tests/cache.php +++ b/apps/files_sharing/tests/cache.php @@ -208,17 +208,18 @@ class Test_Files_Sharing_Cache extends TestCase { * Test searching by tag */ function testSearchByTag() { + $userId = \OC::$server->getUserSession()->getUser()->getUId(); $id1 = $this->sharedCache->get('bar.txt')['fileid']; $id2 = $this->sharedCache->get('subdir/another too.txt')['fileid']; $id3 = $this->sharedCache->get('subdir/not a text file.xml')['fileid']; $id4 = $this->sharedCache->get('subdir/another.txt')['fileid']; - $tagManager = \OC::$server->getTagManager()->load('files'); + $tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId); $tagManager->tagAs($id1, 'tag1'); $tagManager->tagAs($id1, 'tag2'); $tagManager->tagAs($id2, 'tag1'); $tagManager->tagAs($id3, 'tag1'); $tagManager->tagAs($id4, 'tag2'); - $results = $this->sharedStorage->getCache()->searchByTag('tag1'); + $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId); $check = array( array( 'name' => 'bar.txt', diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index a4ae3a069f..9df64db7f0 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -512,10 +512,7 @@ class Cache { * @param string $userId owner of the tags * @return array file data */ - public function searchByTag($tag, $userId = null) { - if (is_null($userId)) { - $userId = \OC::$server->getUserSession()->getUser()->getUID(); - } + public function searchByTag($tag, $userId) { $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . '`mimetype`, `mimepart`, `size`, `mtime`, ' . '`encrypted`, `unencrypted_size`, `etag`, `permissions` ' . diff --git a/lib/private/files/cache/wrapper/cachewrapper.php b/lib/private/files/cache/wrapper/cachewrapper.php index 4da7c7ecf6..83811520e4 100644 --- a/lib/private/files/cache/wrapper/cachewrapper.php +++ b/lib/private/files/cache/wrapper/cachewrapper.php @@ -187,7 +187,7 @@ class CacheWrapper extends Cache { * @param string $userId owner of the tags * @return array file data */ - public function searchByTag($tag, $userId = null) { + public function searchByTag($tag, $userId) { $results = $this->cache->searchByTag($tag, $userId); return array_map(array($this, 'formatCacheEntry'), $results); } diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php index 3d55564f0c..ed2be59c09 100644 --- a/lib/private/files/filesystem.php +++ b/lib/private/files/filesystem.php @@ -688,10 +688,11 @@ class Filesystem { /** * @param string|int $tag name or tag id + * @param string $userId owner of the tags * @return FileInfo[] array or file info */ - static public function searchByTag($tag) { - return self::$defaultInstance->searchByTag($tag); + static public function searchByTag($tag, $userId) { + return self::$defaultInstance->searchByTag($tag, $userId); } /** diff --git a/lib/private/files/node/folder.php b/lib/private/files/node/folder.php index a65e641388..bdfb234671 100644 --- a/lib/private/files/node/folder.php +++ b/lib/private/files/node/folder.php @@ -223,7 +223,7 @@ class Folder extends Node implements \OCP\Files\Folder { * @return \OC\Files\Node\Node[] */ public function search($query) { - return $this->searchCommon('%' . $query . '%', 'search'); + return $this->searchCommon('search', array('%' . $query . '%')); } /** @@ -233,25 +233,26 @@ class Folder extends Node implements \OCP\Files\Folder { * @return Node[] */ public function searchByMime($mimetype) { - return $this->searchCommon($mimetype, 'searchByMime'); + return $this->searchCommon('searchByMime', array($mimetype)); } /** * search for files by tag * - * @param string $tag + * @param string|int $tag name or tag id + * @param string $userId owner of the tags * @return Node[] */ - public function searchByTag($tag) { - return $this->searchCommon($tag, 'searchByTag'); + public function searchByTag($tag, $userId) { + return $this->searchCommon('searchByTag', array($tag, $userId)); } /** - * @param string $query - * @param string $method + * @param string $method cache method + * @param array $args call args * @return \OC\Files\Node\Node[] */ - private function searchCommon($query, $method) { + private function searchCommon($method, $args) { $files = array(); $rootLength = strlen($this->path); /** @@ -262,7 +263,7 @@ class Folder extends Node implements \OCP\Files\Folder { $cache = $storage->getCache(''); - $results = $cache->$method($query); + $results = call_user_func_array(array($cache, $method), $args); foreach ($results as $result) { if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { $result['internalPath'] = $result['path']; @@ -279,7 +280,7 @@ class Folder extends Node implements \OCP\Files\Folder { $cache = $storage->getCache(''); $relativeMountPoint = substr($mount->getMountPoint(), $rootLength); - $results = $cache->$method($query); + $results = call_user_func_array(array($cache, $method), $args); foreach ($results as $result) { $result['internalPath'] = $result['path']; $result['path'] = $relativeMountPoint . $result['path']; diff --git a/lib/private/files/node/nonexistingfolder.php b/lib/private/files/node/nonexistingfolder.php index 9d452a94b9..04f741e8a4 100644 --- a/lib/private/files/node/nonexistingfolder.php +++ b/lib/private/files/node/nonexistingfolder.php @@ -99,7 +99,7 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } - public function searchByTag($mime) { + public function searchByTag($tag, $userId) { throw new NotFoundException(); } diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 7090e03d40..73faf261c1 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -1111,7 +1111,7 @@ class View { * @return FileInfo[] */ public function search($query) { - return $this->searchCommon('%' . $query . '%', 'search'); + return $this->searchCommon('search', array('%' . $query . '%')); } /** @@ -1121,7 +1121,7 @@ class View { * @return FileInfo[] */ public function searchRaw($query) { - return $this->searchCommon($query, 'search'); + return $this->searchCommon('search', array($query)); } /** @@ -1131,25 +1131,26 @@ class View { * @return FileInfo[] */ public function searchByMime($mimetype) { - return $this->searchCommon($mimetype, 'searchByMime'); + return $this->searchCommon('searchByMime', array($mimetype)); } /** * search for files by tag * * @param string|int $tag name or tag id + * @param string $userId owner of the tags * @return FileInfo[] */ - public function searchByTag($tag) { - return $this->searchCommon($tag, 'searchByTag'); + public function searchByTag($tag, $userId) { + return $this->searchCommon('searchByTag', array($tag, $userId)); } /** - * @param string $query - * @param string $method + * @param string $method cache method + * @param array $args * @return FileInfo[] */ - private function searchCommon($query, $method) { + private function searchCommon($method, $args) { $files = array(); $rootLength = strlen($this->fakeRoot); @@ -1158,7 +1159,7 @@ class View { if ($storage) { $cache = $storage->getCache(''); - $results = $cache->$method($query); + $results = call_user_func_array(array($cache, $method), $args); foreach ($results as $result) { if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { $internalPath = $result['path']; @@ -1175,7 +1176,7 @@ class View { $cache = $storage->getCache(''); $relativeMountPoint = substr($mountPoint, $rootLength); - $results = $cache->$method($query); + $results = call_user_func_array(array($cache, $method), $args); if ($results) { foreach ($results as $result) { $internalPath = $result['path']; diff --git a/lib/public/files/folder.php b/lib/public/files/folder.php index f54602d469..9797fbc46e 100644 --- a/lib/public/files/folder.php +++ b/lib/public/files/folder.php @@ -120,9 +120,10 @@ interface Folder extends Node { * search for files by tag * * @param string|int $tag tag name or tag id + * @param string $userId owner of the tags * @return \OCP\Files\Node[] */ - public function searchByTag($tag); + public function searchByTag($tag, $userId); /** * get a file or folder inside the folder by it's internal id diff --git a/tests/lib/files/node/folder.php b/tests/lib/files/node/folder.php index d8c047a2b7..e69a277697 100644 --- a/tests/lib/files/node/folder.php +++ b/tests/lib/files/node/folder.php @@ -405,6 +405,45 @@ class Folder extends \Test\TestCase { $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); } + public function testSearchByTag() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('searchByTag') + ->with('tag1', 'user1') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array())); + + $view->expects($this->once()) + ->method('resolvePath') + ->will($this->returnValue(array($storage, 'foo'))); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->searchByTag('tag1', 'user1'); + $this->assertEquals(1, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + } + public function testSearchSubStorages() { $manager = $this->getMock('\OC\Files\Mount\Manager'); /**