diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-12-12 14:27:19 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-12-12 14:27:19 +0100 |
commit | 4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19 (patch) | |
tree | fbad2095651334fc34b3729ae5102ad5e1cd866f | |
parent | 6b4502adebf1d756707e8fb5b8ab697c1c746e6b (diff) | |
parent | 3878c3782ff37dc56a4e463acea351a61ed7b4c2 (diff) | |
download | nextcloud-server-4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19.tar.gz nextcloud-server-4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19.zip |
Merge pull request #12778 from owncloud/searchbytags2
Added searchByTags to view, storage and cache
-rw-r--r-- | apps/files_sharing/lib/cache.php | 40 | ||||
-rw-r--r-- | apps/files_sharing/tests/cache.php | 34 | ||||
-rw-r--r-- | lib/private/files/cache/cache.php | 45 | ||||
-rw-r--r-- | lib/private/files/cache/wrapper/cachejail.php | 12 | ||||
-rw-r--r-- | lib/private/files/cache/wrapper/cachewrapper.php | 12 | ||||
-rw-r--r-- | lib/private/files/filesystem.php | 9 | ||||
-rw-r--r-- | lib/private/files/node/folder.php | 25 | ||||
-rw-r--r-- | lib/private/files/node/nonexistingfolder.php | 4 | ||||
-rw-r--r-- | lib/private/files/view.php | 27 | ||||
-rw-r--r-- | lib/private/server.php | 3 | ||||
-rw-r--r-- | lib/private/tagmanager.php | 22 | ||||
-rw-r--r-- | lib/public/files/folder.php | 9 | ||||
-rw-r--r-- | lib/public/itagmanager.php | 7 | ||||
-rw-r--r-- | tests/lib/files/cache/cache.php | 57 | ||||
-rw-r--r-- | tests/lib/files/node/folder.php | 39 | ||||
-rw-r--r-- | tests/lib/tags.php | 62 | ||||
-rw-r--r-- | tests/lib/testcase.php | 21 |
17 files changed, 375 insertions, 53 deletions
diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index e09b64cb039..e3bee145876 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -345,6 +345,46 @@ class Shared_Cache extends Cache { } /** + * search for files by tag + * + * @param string|int $tag tag to search for + * @param string $userId owner of the tags + * @return array file data + */ + public function searchByTag($tag, $userId) { + // TODO: inject this + $tagger = \OC::$server->getTagManager()->load('files', null, null, $userId); + $result = array(); + $exploreDirs = array(''); + // 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 ($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; + } + } + } + } + return $result; + } + + /** * get the size of a folder and set it in the cache * * @param string $path diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php index aec1983bad3..b60bba73db8 100644 --- a/apps/files_sharing/tests/cache.php +++ b/apps/files_sharing/tests/cache.php @@ -204,6 +204,40 @@ class Test_Files_Sharing_Cache extends TestCase { $this->verifyFiles($check, $results); } + /** + * 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', 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', $userId); + $check = array( + array( + 'name' => 'bar.txt', + 'path' => 'bar.txt' + ), + array( + 'name' => 'another too.txt', + 'path' => 'subdir/another too.txt' + ), + array( + 'name' => 'not a text file.xml', + 'path' => 'subdir/not a text file.xml' + ), + ); + $this->verifyFiles($check, $results); + $tagManager->delete(array('tag1', 'tag2')); + } + function testGetFolderContentsInRoot() { $results = $this->user2View->getDirectoryContent('/'); diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 4157da2281c..9df64db7f07 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -504,6 +504,51 @@ class Cache { } /** + * Search for files by tag of a given users. + * + * Note that every user can tag files differently. + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return array file data + */ + public function searchByTag($tag, $userId) { + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . + '`mimetype`, `mimepart`, `size`, `mtime`, ' . + '`encrypted`, `unencrypted_size`, `etag`, `permissions` ' . + 'FROM `*PREFIX*filecache` `file`, ' . + '`*PREFIX*vcategory_to_object` `tagmap`, ' . + '`*PREFIX*vcategory` `tag` ' . + // JOIN filecache to vcategory_to_object + 'WHERE `file`.`fileid` = `tagmap`.`objid` '. + // JOIN vcategory_to_object to vcategory + 'AND `tagmap`.`type` = `tag`.`type` ' . + 'AND `tagmap`.`categoryid` = `tag`.`id` ' . + // conditions + 'AND `file`.`storage` = ? '. + 'AND `tag`.`type` = \'files\' ' . + 'AND `tag`.`uid` = ? '; + if (is_int($tag)) { + $sql .= 'AND `tag`.`id` = ? '; + } else { + $sql .= 'AND `tag`.`category` = ? '; + } + $result = \OC_DB::executeAudited( + $sql, + array( + $this->getNumericStorageId(), + $userId, + $tag + ) + ); + $files = array(); + while ($row = $result->fetchRow()) { + $files[] = $row; + } + return $files; + } + + /** * update the folder size and the size of all parent folders * * @param string|boolean $path diff --git a/lib/private/files/cache/wrapper/cachejail.php b/lib/private/files/cache/wrapper/cachejail.php index 7982293f5ed..f4ffc67d76a 100644 --- a/lib/private/files/cache/wrapper/cachejail.php +++ b/lib/private/files/cache/wrapper/cachejail.php @@ -198,6 +198,18 @@ class CacheJail extends CacheWrapper { } /** + * search for files by mimetype + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return array + */ + public function searchByTag($tag, $userId) { + $results = $this->cache->searchByTag($tag, $userId); + return $this->formatSearchResults($results); + } + + /** * update the folder size and the size of all parent folders * * @param string|boolean $path diff --git a/lib/private/files/cache/wrapper/cachewrapper.php b/lib/private/files/cache/wrapper/cachewrapper.php index d3d64e3f0a9..83811520e4b 100644 --- a/lib/private/files/cache/wrapper/cachewrapper.php +++ b/lib/private/files/cache/wrapper/cachewrapper.php @@ -181,6 +181,18 @@ class CacheWrapper extends Cache { } /** + * search for files by tag + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return array file data + */ + public function searchByTag($tag, $userId) { + $results = $this->cache->searchByTag($tag, $userId); + return array_map(array($this, 'formatCacheEntry'), $results); + } + + /** * update the folder size and the size of all parent folders * * @param string|boolean $path diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php index 90643839e22..ed2be59c092 100644 --- a/lib/private/files/filesystem.php +++ b/lib/private/files/filesystem.php @@ -687,6 +687,15 @@ 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, $userId) { + return self::$defaultInstance->searchByTag($tag, $userId); + } + + /** * check if a file or folder has been updated since $time * * @param string $path diff --git a/lib/private/files/node/folder.php b/lib/private/files/node/folder.php index 6fdcff13e1c..bdfb2346716 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,15 +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)); } /** - * @param string $query - * @param string $method + * search for files by tag + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return Node[] + */ + public function searchByTag($tag, $userId) { + return $this->searchCommon('searchByTag', array($tag, $userId)); + } + + /** + * @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); /** @@ -252,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']; @@ -269,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 0346cbf1e21..04f741e8a46 100644 --- a/lib/private/files/node/nonexistingfolder.php +++ b/lib/private/files/node/nonexistingfolder.php @@ -99,6 +99,10 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } + public function searchByTag($tag, $userId) { + throw new NotFoundException(); + } + public function getById($id) { throw new NotFoundException(); } diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 93edbede607..c01763cdad3 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,15 +1131,26 @@ class View { * @return FileInfo[] */ public function searchByMime($mimetype) { - return $this->searchCommon($mimetype, 'searchByMime'); + return $this->searchCommon('searchByMime', array($mimetype)); } /** - * @param string $query - * @param string $method + * 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, $userId) { + return $this->searchCommon('searchByTag', array($tag, $userId)); + } + + /** + * @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); @@ -1148,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']; @@ -1165,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/private/server.php b/lib/private/server.php index 94444623cfb..6066e2fa35e 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -87,8 +87,7 @@ class Server extends SimpleContainer implements IServerContainer { }); $this->registerService('TagManager', function (Server $c) { $tagMapper = $c->query('TagMapper'); - $user = \OC_User::getUser(); - return new TagManager($tagMapper, $user); + return new TagManager($tagMapper, $c->getUserSession()); }); $this->registerService('RootFolder', function (Server $c) { // TODO: get user and user manager from container as well diff --git a/lib/private/tagmanager.php b/lib/private/tagmanager.php index d5bff04acff..6c7eeab87eb 100644 --- a/lib/private/tagmanager.php +++ b/lib/private/tagmanager.php @@ -38,11 +38,11 @@ use OC\Tagging\TagMapper; class TagManager implements \OCP\ITagManager { /** - * User + * User session * - * @var string + * @var \OCP\IUserSession */ - private $user; + private $userSession; /** * TagMapper @@ -55,12 +55,11 @@ class TagManager implements \OCP\ITagManager { * Constructor. * * @param TagMapper $mapper Instance of the TagMapper abstraction layer. - * @param string $user The user whose data the object will operate on. + * @param \OCP\IUserSession $userSession the user session */ - public function __construct(TagMapper $mapper, $user) { - + public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) { $this->mapper = $mapper; - $this->user = $user; + $this->userSession = $userSession; } @@ -71,10 +70,15 @@ class TagManager implements \OCP\ITagManager { * @param string $type The type identifier e.g. 'contact' or 'event'. * @param array $defaultTags An array of default tags to be used if none are stored. * @param boolean $includeShared Whether to include tags for items shared with this user by others. + * @param string $userId user for which to retrieve the tags, defaults to the currently + * logged in user * @return \OCP\ITags */ - public function load($type, $defaultTags=array(), $includeShared=false) { - return new Tags($this->mapper, $this->user, $type, $defaultTags, $includeShared); + public function load($type, $defaultTags = array(), $includeShared = false, $userId = null) { + if (is_null($userId)) { + $userId = $this->userSession->getUser()->getUId(); + } + return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared); } } diff --git a/lib/public/files/folder.php b/lib/public/files/folder.php index 7fec1c529a5..9797fbc46ed 100644 --- a/lib/public/files/folder.php +++ b/lib/public/files/folder.php @@ -117,6 +117,15 @@ interface Folder extends Node { public function searchByMime($mimetype); /** + * 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, $userId); + + /** * get a file or folder inside the folder by it's internal id * * @param int $id diff --git a/lib/public/itagmanager.php b/lib/public/itagmanager.php index 54daa5cc1cb..ac80eebc72d 100644 --- a/lib/public/itagmanager.php +++ b/lib/public/itagmanager.php @@ -43,14 +43,15 @@ namespace OCP; interface ITagManager { /** - * Create a new \OCP\ITags instance and load tags from db. + * Create a new \OCP\ITags instance and load tags from db for the current user. * * @see \OCP\ITags * @param string $type The type identifier e.g. 'contact' or 'event'. * @param array $defaultTags An array of default tags to be used if none are stored. * @param boolean $includeShared Whether to include tags for items shared with this user by others. + * @param string $userId user for which to retrieve the tags, defaults to the currently + * logged in user * @return \OCP\ITags */ - public function load($type, $defaultTags=array(), $includeShared=false); - + public function load($type, $defaultTags = array(), $includeShared = false, $userId = null); } diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php index 7e44cb898ac..1af8e4da960 100644 --- a/tests/lib/files/cache/cache.php +++ b/tests/lib/files/cache/cache.php @@ -270,6 +270,63 @@ class Cache extends \Test\TestCase { $this->assertEquals(2, count($this->cache->searchByMime('foo/file'))); } + function testSearchByTag() { + $userId = $this->getUniqueId('user'); + \OC_User::createUser($userId, $userId); + $this->loginAsUser($userId); + $user = new \OC\User\User($userId, null); + + $file1 = 'folder'; + $file2 = 'folder/foobar'; + $file3 = 'folder/foo'; + $file4 = 'folder/foo2'; + $file5 = 'folder/foo3'; + $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'); + $fileData = array(); + $fileData['foobar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'); + $fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'); + $fileData['foo2'] = array('size' => 25, 'mtime' => 28, 'mimetype' => 'foo/file'); + $fileData['foo3'] = array('size' => 88, 'mtime' => 34, 'mimetype' => 'foo/file'); + + $id1 = $this->cache->put($file1, $data1); + $id2 = $this->cache->put($file2, $fileData['foobar']); + $id3 = $this->cache->put($file3, $fileData['foo']); + $id4 = $this->cache->put($file4, $fileData['foo2']); + $id5 = $this->cache->put($file5, $fileData['foo3']); + + $tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId); + $this->assertTrue($tagManager->tagAs($id1, 'tag1')); + $this->assertTrue($tagManager->tagAs($id1, 'tag2')); + $this->assertTrue($tagManager->tagAs($id2, 'tag2')); + $this->assertTrue($tagManager->tagAs($id3, 'tag1')); + $this->assertTrue($tagManager->tagAs($id4, 'tag2')); + + // use tag name + $results = $this->cache->searchByTag('tag1', $userId); + + $this->assertEquals(2, count($results)); + + $this->assertEquals('folder', $results[0]['name']); + $this->assertEquals('foo', $results[1]['name']); + + // use tag id + $tags = $tagManager->getTagsForUser($userId); + $this->assertNotEmpty($tags); + $tags = array_filter($tags, function($tag) { return $tag->getName() === 'tag2'; }); + $results = $this->cache->searchByTag(current($tags)->getId(), $userId); + $this->assertEquals(3, count($results)); + + $this->assertEquals('folder', $results[0]['name']); + $this->assertEquals('foobar', $results[1]['name']); + $this->assertEquals('foo2', $results[2]['name']); + + $tagManager->delete('tag1'); + $tagManager->delete('tag2'); + + $this->logout(); + \OC_User::deleteUser($userId); + } + function testMove() { $file1 = 'folder'; $file2 = 'folder/bar'; diff --git a/tests/lib/files/node/folder.php b/tests/lib/files/node/folder.php index d8c047a2b75..e69a2776979 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'); /** diff --git a/tests/lib/tags.php b/tests/lib/tags.php index 78f5085df39..71296d2e346 100644 --- a/tests/lib/tags.php +++ b/tests/lib/tags.php @@ -23,7 +23,10 @@ class Test_Tags extends \Test\TestCase { protected $objectType; + /** @var \OC\IUser */ protected $user; + /** @var \OC\IUserSession */ + protected $userSession; protected $backupGlobals = FALSE; /** @var \OC\Tagging\TagMapper */ protected $tagMapper; @@ -35,12 +38,19 @@ class Test_Tags extends \Test\TestCase { OC_User::clearBackends(); OC_User::useBackend('dummy'); - $this->user = $this->getUniqueID('user_'); + $userId = $this->getUniqueID('user_'); + OC_User::createUser($userId, 'pass'); + OC_User::setUserId($userId); + $this->user = new OC\User\User($userId, null); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $this->objectType = $this->getUniqueID('type_'); - OC_User::createUser($this->user, 'pass'); - OC_User::setUserId($this->user); $this->tagMapper = new OC\Tagging\TagMapper(\OC::$server->getDb()); - $this->tagMgr = new OC\TagManager($this->tagMapper, $this->user); + $this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession); } @@ -166,7 +176,7 @@ class Test_Tags extends \Test\TestCase { ); } - public function testdeleteTags() { + public function testDeleteTags() { $defaultTags = array('Friends', 'Family', 'Work', 'Other'); $tagger = $this->tagMgr->load($this->objectType, $defaultTags); @@ -177,7 +187,6 @@ class Test_Tags extends \Test\TestCase { $tagger->delete(array('Friends', 'Work', 'Other')); $this->assertEquals(0, count($tagger->getTags())); - } public function testRenameTag() { @@ -233,27 +242,32 @@ class Test_Tags extends \Test\TestCase { } public function testShareTags() { - $test_tag = 'TestTag'; + $testTag = 'TestTag'; OCP\Share::registerBackend('test', 'Test_Share_Backend'); $tagger = $this->tagMgr->load('test'); - $tagger->tagAs(1, $test_tag); - - $other_user = $this->getUniqueID('user2_'); - OC_User::createUser($other_user, 'pass'); - - OC_User::setUserId($other_user); - $other_tagMgr = new OC\TagManager($this->tagMapper, $other_user); - $other_tagger = $other_tagMgr->load('test'); - $this->assertFalse($other_tagger->hasTag($test_tag)); - - OC_User::setUserId($this->user); - OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $other_user, \OCP\Constants::PERMISSION_READ); - - OC_User::setUserId($other_user); - $other_tagger = $other_tagMgr->load('test', array(), true); // Update tags, load shared ones. - $this->assertTrue($other_tagger->hasTag($test_tag)); - $this->assertContains(1, $other_tagger->getIdsForTag($test_tag)); + $tagger->tagAs(1, $testTag); + + $otherUserId = $this->getUniqueID('user2_'); + OC_User::createUser($otherUserId, 'pass'); + OC_User::setUserId($otherUserId); + $otherUserSession = $this->getMock('\OCP\IUserSession'); + $otherUserSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue(new OC\User\User($otherUserId, null))); + + $otherTagMgr = new OC\TagManager($this->tagMapper, $otherUserSession); + $otherTagger = $otherTagMgr->load('test'); + $this->assertFalse($otherTagger->hasTag($testTag)); + + OC_User::setUserId($this->user->getUID()); + OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ); + + OC_User::setUserId($otherUserId); + $otherTagger = $otherTagMgr->load('test', array(), true); // Update tags, load shared ones. + $this->assertTrue($otherTagger->hasTag($testTag)); + $this->assertContains(1, $otherTagger->getIdsForTag($testTag)); } } diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index 27c28329535..1ea3aa13547 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -148,4 +148,25 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { \OC_FileProxy::$enabled = true; \OC_FileProxy::clearProxies(); } + + /** + * Login and setup FS as a given user, + * sets the given user as the current user. + * + * @param string $user user id + */ + static protected function loginAsUser($user) { + self::logout(); + \OC\Files\Filesystem::tearDown(); + \OC_User::setUserId($user); + \OC_Util::setupFS($user); + } + + /** + * Logout the current user and tear down the filesystem. + */ + static protected function logout() { + \OC_Util::tearDownFS(); + \OC_User::setUserId(''); + } } |