Skip to content

Commit

Permalink
properly use fileinfo objects
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Mar 21, 2016
1 parent d0dd76b commit 0b0b325
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 34 deletions.
28 changes: 12 additions & 16 deletions lib/private/files/node/folder.php
Expand Up @@ -90,14 +90,14 @@ public function getDirectoryListing() {

/**
* @param string $path
* @param array $info
* @param FileInfo $info
* @return File|Folder
*/
protected function createNode($path, $info = array()) {
if (!isset($info['mimetype'])) {
protected function createNode($path, FileInfo $info = null) {
if (is_null($info)) {
$isDir = $this->view->is_dir($path);
} else {
$isDir = $info['mimetype'] === 'httpd/unix-directory';
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
}
if ($isDir) {
return new Folder($this->root, $this->view, $path, $info);
Expand Down Expand Up @@ -211,10 +211,9 @@ public function searchByTag($tag, $userId) {
private function searchCommon($method, $args) {
$files = array();
$rootLength = strlen($this->path);
/**
* @var \OC\Files\Storage\Storage $storage
*/
list($storage, $internalPath) = $this->view->resolvePath($this->path);
$mount = $this->root->getMount($this->path);
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($this->path);
$internalPath = rtrim($internalPath, '/');
if ($internalPath !== '') {
$internalPath = $internalPath . '/';
Expand All @@ -229,7 +228,7 @@ private function searchCommon($method, $args) {
$result['internalPath'] = $result['path'];
$result['path'] = substr($result['path'], $internalRootLength);
$result['storage'] = $storage;
$files[] = $result;
$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
}
}

Expand All @@ -245,17 +244,14 @@ private function searchCommon($method, $args) {
$result['internalPath'] = $result['path'];
$result['path'] = $relativeMountPoint . $result['path'];
$result['storage'] = $storage;
$files[] = $result;
$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
}
}
}

$result = array();
foreach ($files as $file) {
$result[] = $this->createNode($this->normalizePath($this->path . '/' . $file['path']), $file);
}

return $result;
return array_map(function(FileInfo $file) {
return $this->createNode($file->getPath(), $file);
}, $files);
}

/**
Expand Down
81 changes: 63 additions & 18 deletions tests/lib/files/node/folder.php
Expand Up @@ -376,6 +376,14 @@ public function testSearch() {
->method('getCache')
->will($this->returnValue($cache));

$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('foo'));

$cache->expects($this->once())
->method('search')
->with('%qw%')
Expand All @@ -388,9 +396,10 @@ public function testSearch() {
->with('/bar/foo')
->will($this->returnValue(array()));

$view->expects($this->once())
->method('resolvePath')
->will($this->returnValue(array($storage, 'foo')));
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->will($this->returnValue($mount));

$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->search('qw');
Expand All @@ -404,13 +413,21 @@ public function testSearchInRoot() {
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn'), array($manager, $view, $this->user));
$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), 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(''));

$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('files'));

$storage->expects($this->once())
->method('getCache')
->will($this->returnValue($cache));
Expand All @@ -428,9 +445,10 @@ public function testSearchInRoot() {
->with('')
->will($this->returnValue(array()));

$view->expects($this->once())
->method('resolvePath')
->will($this->returnValue(array($storage, 'files')));
$root->expects($this->once())
->method('getMount')
->with('')
->will($this->returnValue($mount));

$result = $root->search('qw');
$this->assertEquals(1, count($result));
Expand All @@ -450,6 +468,14 @@ public function testSearchInStorageRoot() {
$storage = $this->getMock('\OC\Files\Storage\Storage');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));

$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue(''));

$storage->expects($this->once())
->method('getCache')
->will($this->returnValue($cache));
Expand All @@ -466,9 +492,10 @@ public function testSearchInStorageRoot() {
->with('/bar')
->will($this->returnValue(array()));

$view->expects($this->once())
->method('resolvePath')
->will($this->returnValue(array($storage, '')));
$root->expects($this->once())
->method('getMount')
->with('/bar')
->will($this->returnValue($mount));

$node = new \OC\Files\Node\Folder($root, $view, '/bar');
$result = $node->search('qw');
Expand All @@ -489,6 +516,14 @@ public function testSearchByTag() {
$storage = $this->getMock('\OC\Files\Storage\Storage');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));

$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('foo'));

$storage->expects($this->once())
->method('getCache')
->will($this->returnValue($cache));
Expand All @@ -505,9 +540,10 @@ public function testSearchByTag() {
->with('/bar/foo')
->will($this->returnValue(array()));

$view->expects($this->once())
->method('resolvePath')
->will($this->returnValue(array($storage, 'foo')));
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->will($this->returnValue($mount));

$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->searchByTag('tag1', 'user1');
Expand All @@ -531,6 +567,14 @@ public function testSearchSubStorages() {
$subStorage = $this->getMock('\OC\Files\Storage\Storage');
$subMount = $this->getMock('\OC\Files\Mount\MountPoint', array(), array(null, ''));

$mount = $this->getMock('\OCP\Files\Mount\IMountPoint');
$mount->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
$mount->expects($this->once())
->method('getInternalPath')
->will($this->returnValue('foo'));

$subMount->expects($this->once())
->method('getStorage')
->will($this->returnValue($subStorage));
Expand Down Expand Up @@ -566,9 +610,10 @@ public function testSearchSubStorages() {
->with('/bar/foo')
->will($this->returnValue(array($subMount)));

$view->expects($this->once())
->method('resolvePath')
->will($this->returnValue(array($storage, 'foo')));
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->will($this->returnValue($mount));


$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
Expand Down Expand Up @@ -602,7 +647,7 @@ public function testGetById() {

$view->expects($this->once())
->method('getFileInfo')
->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', [], null)));
->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', ['mimetype' => 'text/plain'], null)));

$storage->expects($this->once())
->method('getCache')
Expand Down Expand Up @@ -684,7 +729,7 @@ public function testGetByIdMultipleStorages() {

$view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', [], null)));
->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', ['mimetype' => 'plain'], null)));

$storage->expects($this->any())
->method('getCache')
Expand Down

0 comments on commit 0b0b325

Please sign in to comment.