$userId is now a mandatory parameter for searchByTags.
Also fixed some places in the code where the argument was missing (Node
API and View)
* @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();
* 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',
* @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` ' .
* @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);
}
/**
* @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);
}
/**
* @return \OC\Files\Node\Node[]
*/
public function search($query) {
- return $this->searchCommon('%' . $query . '%', 'search');
+ return $this->searchCommon('search', array('%' . $query . '%'));
}
/**
* @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);
/**
$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'];
$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'];
throw new NotFoundException();
}
- public function searchByTag($mime) {
+ public function searchByTag($tag, $userId) {
throw new NotFoundException();
}
* @return FileInfo[]
*/
public function search($query) {
- return $this->searchCommon('%' . $query . '%', 'search');
+ return $this->searchCommon('search', array('%' . $query . '%'));
}
/**
* @return FileInfo[]
*/
public function searchRaw($query) {
- return $this->searchCommon($query, 'search');
+ return $this->searchCommon('search', array($query));
}
/**
* @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);
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'];
$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'];
* 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
$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');
/**