From 745d8706b973ff0494af54f183acc0da361f0e83 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 10 Dec 2014 15:59:41 +0100 Subject: [PATCH] Add user parameter to tag manager --- lib/private/server.php | 3 +- lib/private/tagmanager.php | 22 ++++++++------ lib/public/itagmanager.php | 7 +++-- tests/lib/tags.php | 62 +++++++++++++++++++++++--------------- 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/lib/private/server.php b/lib/private/server.php index e0105506970..02cc4e2755a 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/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/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)); } } -- 2.39.5