]> source.dussan.org Git - nextcloud-server.git/commitdiff
Make $userId mandatory for searchByTags
authorVincent Petry <pvince81@owncloud.com>
Fri, 12 Dec 2014 10:18:35 +0000 (11:18 +0100)
committerVincent Petry <pvince81@owncloud.com>
Fri, 12 Dec 2014 10:18:35 +0000 (11:18 +0100)
$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
apps/files_sharing/tests/cache.php
lib/private/files/cache/cache.php
lib/private/files/cache/wrapper/cachewrapper.php
lib/private/files/filesystem.php
lib/private/files/node/folder.php
lib/private/files/node/nonexistingfolder.php
lib/private/files/view.php
lib/public/files/folder.php
tests/lib/files/node/folder.php

index da8155ec6fac299052b3ac2ac4e451b4a14703d6..e3bee1458765c40f519b97b51e49103c92df047b 100644 (file)
@@ -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();
index c40a014d5577adb27118ded10021c75a0785a8cd..b60bba73db84a9a224bf4e5ecf85d91369a76493 100644 (file)
@@ -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',
index a4ae3a069fe8187830865deb5a5e21fb5face262..9df64db7f07ae8c64e347599426fae7729138974 100644 (file)
@@ -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` ' .
index 4da7c7ecf6f36017b942615ef4a12ba3eb5c34d0..83811520e4bf38f0e29465dff3a0f0b227bf7f01 100644 (file)
@@ -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);
        }
index 3d55564f0c65dee3a39842e8ffc8fd00eef49803..ed2be59c092e6ed8c046a959caf242a5af9485b8 100644 (file)
@@ -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);
        }
 
        /**
index a65e641388d3dba0c03095316d0f20f1a796362f..bdfb2346716d240c9967d5ec4febb6b7348be86e 100644 (file)
@@ -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'];
index 9d452a94b9cd3760bf58cac61716aa3a4f6189ed..04f741e8a46efab5b380466732183b4ce5d0a5fa 100644 (file)
@@ -99,7 +99,7 @@ class NonExistingFolder extends Folder {
                throw new NotFoundException();
        }
 
-       public function searchByTag($mime) {
+       public function searchByTag($tag, $userId) {
                throw new NotFoundException();
        }
 
index 7090e03d40c7d53c123ac58232c362b310ee292d..73faf261c1468090254c7351c98cfc3566af5daf 100644 (file)
@@ -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'];
index f54602d469d54777d2ba1d2f6a0d7ac4c7894d60..9797fbc46ed05feba511e45cf3d956c133e7c31a 100644 (file)
@@ -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
index d8c047a2b757a35f99136a51116736346014a633..e69a277697918549048e81a747d53d818c1285f5 100644 (file)
@@ -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');
                /**