Make $userId mandatory for searchByTags

$userId is now a mandatory parameter for searchByTags.

Also fixed some places in the code where the argument was missing (Node
API and View)
This commit is contained in:
Vincent Petry 2014-12-12 11:18:35 +01:00
parent 25dde7e93b
commit 15ecb28d50
10 changed files with 73 additions and 32 deletions

View 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();

View 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',

View 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` ' .

View 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);
}

View 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);
}
/**

View 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'];

View File

@ -99,7 +99,7 @@ class NonExistingFolder extends Folder {
throw new NotFoundException();
}
public function searchByTag($mime) {
public function searchByTag($tag, $userId) {
throw new NotFoundException();
}

View 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'];

View 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

View 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');
/**