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 /tests/lib | |
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
Diffstat (limited to 'tests/lib')
-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 |
4 files changed, 155 insertions, 24 deletions
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(''); + } } |