From e321ecd592fef123772c6e61e175b6a34a3c5044 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 22 Jul 2016 14:37:37 +0200 Subject: add recent files to node api --- tests/lib/Files/Node/FolderTest.php | 171 +++++++++++++++++++++++++++++++++++- 1 file changed, 167 insertions(+), 4 deletions(-) (limited to 'tests/lib/Files') diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index 7ce9fff1419..cae6b4a80c0 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -12,6 +12,8 @@ use OC\Files\Cache\Cache; use OC\Files\FileInfo; use OC\Files\Mount\MountPoint; use OC\Files\Node\Node; +use OC\Files\Storage\Temporary; +use OC\Files\Storage\Wrapper\Jail; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OC\Files\View; @@ -760,9 +762,9 @@ class FolderTest extends \Test\TestCase { public function uniqueNameProvider() { return [ // input, existing, expected - ['foo', [] , 'foo'], - ['foo', ['foo'] , 'foo (2)'], - ['foo', ['foo', 'foo (2)'] , 'foo (3)'] + ['foo', [], 'foo'], + ['foo', ['foo'], 'foo (2)'], + ['foo', ['foo', 'foo (2)'], 'foo (3)'] ]; } @@ -782,7 +784,7 @@ class FolderTest extends \Test\TestCase { ->method('file_exists') ->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) { foreach ($existingFiles as $existing) { - if ($folderPath . '/' . $existing === $path){ + if ($folderPath . '/' . $existing === $path) { return true; } } @@ -792,4 +794,165 @@ class FolderTest extends \Test\TestCase { $node = new \OC\Files\Node\Folder($root, $view, $folderPath); $this->assertEquals($expected, $node->getNonExistingName($name)); } + + public function testRecent() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + $folderPath = '/bar/foo'; + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */ + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */ + $folderInfo = $this->getMockBuilder('\OC\Files\FileInfo') + ->disableOriginalConstructor()->getMock(); + + $baseTime = 1000; + $storage = new Temporary(); + $mount = new MountPoint($storage, ''); + + $folderInfo->expects($this->any()) + ->method('getMountPoint') + ->will($this->returnValue($mount)); + + $cache = $storage->getCache(); + + $id1 = $cache->put('bar/foo/inside.txt', [ + 'storage_mtime' => $baseTime, + 'mtime' => $baseTime, + 'mimetype' => 'text/plain', + 'size' => 3 + ]); + $id2 = $cache->put('bar/foo/old.txt', [ + 'storage_mtime' => $baseTime - 100, + 'mtime' => $baseTime - 100, + 'mimetype' => 'text/plain', + 'size' => 3 + ]); + $cache->put('bar/asd/outside.txt', [ + 'storage_mtime' => $baseTime, + 'mtime' => $baseTime, + 'mimetype' => 'text/plain', + 'size' => 3 + ]); + $cache->put('bar/foo/toold.txt', [ + 'storage_mtime' => $baseTime - 600, + 'mtime' => $baseTime - 600, + 'mimetype' => 'text/plain', + 'size' => 3 + ]); + + $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); + + + $nodes = $node->getRecent($baseTime - 500); + $ids = array_map(function (Node $node) { + return (int)$node->getId(); + }, $nodes); + $this->assertEquals([$id1, $id2], $ids); + } + + public function testRecentFolder() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + $folderPath = '/bar/foo'; + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */ + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */ + $folderInfo = $this->getMockBuilder('\OC\Files\FileInfo') + ->disableOriginalConstructor()->getMock(); + + $baseTime = 1000; + $storage = new Temporary(); + $mount = new MountPoint($storage, ''); + + $folderInfo->expects($this->any()) + ->method('getMountPoint') + ->will($this->returnValue($mount)); + + $cache = $storage->getCache(); + + $id1 = $cache->put('bar/foo/folder', [ + 'storage_mtime' => $baseTime, + 'mtime' => $baseTime, + 'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER, + 'size' => 3 + ]); + $id2 = $cache->put('bar/foo/folder/bar.txt', [ + 'storage_mtime' => $baseTime, + 'mtime' => $baseTime, + 'mimetype' => 'text/plain', + 'size' => 3, + 'parent' => $id1 + ]); + $id3 = $cache->put('bar/foo/folder/asd.txt', [ + 'storage_mtime' => $baseTime, + 'mtime' => $baseTime - 100, + 'mimetype' => 'text/plain', + 'size' => 3, + 'parent' => $id1 + ]); + + $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); + + + $nodes = $node->getRecent($baseTime - 500); + $ids = array_map(function (Node $node) { + return (int)$node->getId(); + }, $nodes); + $this->assertEquals([$id2, $id1, $id3], $ids);// sort folders before files with the same mtime, folders get the lowest child mtime + } + + public function testRecentJail() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + $folderPath = '/bar/foo'; + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */ + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */ + $folderInfo = $this->getMockBuilder('\OC\Files\FileInfo') + ->disableOriginalConstructor()->getMock(); + + $baseTime = 1000; + $storage = new Temporary(); + $jail = new Jail([ + 'storage' => $storage, + 'root' => 'folder' + ]); + $mount = new MountPoint($jail, '/bar/foo'); + + $folderInfo->expects($this->any()) + ->method('getMountPoint') + ->will($this->returnValue($mount)); + + $cache = $storage->getCache(); + + $id1 = $cache->put('folder/inside.txt', [ + 'storage_mtime' => $baseTime, + 'mtime' => $baseTime, + 'mimetype' => 'text/plain', + 'size' => 3 + ]); + $cache->put('outside.txt', [ + 'storage_mtime' => $baseTime - 100, + 'mtime' => $baseTime - 100, + 'mimetype' => 'text/plain', + 'size' => 3 + ]); + + $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); + + $nodes = $node->getRecent($baseTime - 500); + $ids = array_map(function (Node $node) { + return (int)$node->getId(); + }, $nodes); + $this->assertEquals([$id1], $ids); + } } -- cgit v1.2.3 From a4ba3eadd0d1e04dc4ef5d11dae59c7dc98c4466 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 11 Jul 2016 12:58:43 +0200 Subject: fix test --- apps/files/tests/Controller/ApiControllerTest.php | 8 ++++++- apps/files/tests/Controller/ViewControllerTest.php | 25 ++++++++++++++++------ tests/lib/Files/Node/FolderTest.php | 5 ++++- 3 files changed, 30 insertions(+), 8 deletions(-) (limited to 'tests/lib/Files') diff --git a/apps/files/tests/Controller/ApiControllerTest.php b/apps/files/tests/Controller/ApiControllerTest.php index 1d39c88021b..348150e0e08 100644 --- a/apps/files/tests/Controller/ApiControllerTest.php +++ b/apps/files/tests/Controller/ApiControllerTest.php @@ -59,6 +59,8 @@ class ApiControllerTest extends TestCase { private $shareManager; /** @var \OCP\IConfig */ private $config; + /** @var \OC\Files\Node\Folder */ + private $userFolder; public function setUp() { $this->request = $this->getMockBuilder('\OCP\IRequest') @@ -82,6 +84,9 @@ class ApiControllerTest extends TestCase { ->disableOriginalConstructor() ->getMock(); $this->config = $this->getMock('\OCP\IConfig'); + $this->userFolder = $this->getMockBuilder('\OC\Files\Node\Folder') + ->disableOriginalConstructor() + ->getMock(); $this->apiController = new ApiController( $this->appName, @@ -90,7 +95,8 @@ class ApiControllerTest extends TestCase { $this->tagService, $this->preview, $this->shareManager, - $this->config + $this->config, + $this->userFolder ); } diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index ceb48a2241f..25b5b6e04e0 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -191,7 +191,16 @@ class ViewControllerTest extends TestCase { 'appname' => 'files', 'script' => 'list.php', 'order' => 0, - 'name' => new \OC_L10N_String(new \OC_L10N('files'), 'All files', []), + 'name' => (string)new \OC_L10N_String(new \OC_L10N('files'), 'All files', []), + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'recent', + 'appname' => 'files', + 'script' => 'list.php', + 'order' => 2, + 'name' => (string)new \OC_L10N_String(new \OC_L10N('files'), 'Recent', []), 'active' => false, 'icon' => '', ], @@ -209,7 +218,7 @@ class ViewControllerTest extends TestCase { 'appname' => 'files_sharing', 'script' => 'list.php', 'order' => 10, - 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with you', []), + 'name' => (string)new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with you', []), 'active' => false, 'icon' => '', ], @@ -218,7 +227,7 @@ class ViewControllerTest extends TestCase { 'appname' => 'files_sharing', 'script' => 'list.php', 'order' => 15, - 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with others', []), + 'name' => (string)new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with others', []), 'active' => false, 'icon' => '', ], @@ -227,7 +236,7 @@ class ViewControllerTest extends TestCase { 'appname' => 'files_sharing', 'script' => 'list.php', 'order' => 20, - 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared by link', []), + 'name' => (string)new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared by link', []), 'active' => false, 'icon' => '', ], @@ -236,7 +245,7 @@ class ViewControllerTest extends TestCase { 'appname' => 'systemtags', 'script' => 'list.php', 'order' => 25, - 'name' => new \OC_L10N_String(new \OC_L10N('systemtags'), 'Tags', []), + 'name' => (string)new \OC_L10N_String(new \OC_L10N('systemtags'), 'Tags', []), 'active' => false, 'icon' => '', ], @@ -245,7 +254,7 @@ class ViewControllerTest extends TestCase { 'appname' => 'files_trashbin', 'script' => 'list.php', 'order' => 50, - 'name' => new \OC_L10N_String(new \OC_L10N('files_trashbin'), 'Deleted files', []), + 'name' => (string)new \OC_L10N_String(new \OC_L10N('files_trashbin'), 'Deleted files', []), 'active' => false, 'icon' => '', ], @@ -272,6 +281,10 @@ class ViewControllerTest extends TestCase { 'id' => 'files', 'content' => null, ], + [ + 'id' => 'recent', + 'content' => null, + ], [ 'id' => 'favorites', 'content' => null, diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index cae6b4a80c0..eef78e7d428 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -890,7 +890,7 @@ class FolderTest extends \Test\TestCase { 'parent' => $id1 ]); $id3 = $cache->put('bar/foo/folder/asd.txt', [ - 'storage_mtime' => $baseTime, + 'storage_mtime' => $baseTime - 100, 'mtime' => $baseTime - 100, 'mimetype' => 'text/plain', 'size' => 3, @@ -905,6 +905,9 @@ class FolderTest extends \Test\TestCase { return (int)$node->getId(); }, $nodes); $this->assertEquals([$id2, $id1, $id3], $ids);// sort folders before files with the same mtime, folders get the lowest child mtime + $this->assertEquals($baseTime, $nodes[0]->getMTime()); + $this->assertEquals($baseTime - 100, $nodes[1]->getMTime()); + $this->assertEquals($baseTime - 100, $nodes[2]->getMTime()); } public function testRecentJail() { -- cgit v1.2.3 From 81e103074ea6ce9e7035734bc527ab582dbca89f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 22 Jul 2016 13:58:53 +0200 Subject: use limit instead of since when listing recent files --- apps/files/lib/Controller/ApiController.php | 3 +- lib/private/Files/Node/Folder.php | 49 +++++------------------------ lib/private/Files/Node/LazyRoot.php | 2 +- lib/public/Files/Folder.php | 5 +-- tests/lib/Files/Node/FolderTest.php | 13 ++++---- 5 files changed, 18 insertions(+), 54 deletions(-) (limited to 'tests/lib/Files') diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php index 8adc73a0a45..7ce83bfca15 100644 --- a/apps/files/lib/Controller/ApiController.php +++ b/apps/files/lib/Controller/ApiController.php @@ -198,8 +198,7 @@ class ApiController extends Controller { * @return DataResponse */ public function getRecentFiles() { - $since = time() - (60 * 60 * 24 * 7);//1 week - $nodes = $this->userFolder->getRecent($since); + $nodes = $this->userFolder->getRecent(100); $files = $this->formatNodes($nodes); return new DataResponse(['files' => $files]); } diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index 7746757c2a5..e67e4817e2a 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -363,10 +363,11 @@ class Folder extends Node implements \OCP\Files\Folder { } /** - * @param int $since + * @param int $limit + * @param int $offset * @return \OCP\Files\Node[] */ - public function getRecent($since) { + public function getRecent($limit, $offset = 0) { $mimetypeLoader = \OC::$server->getMimeTypeLoader(); $mounts = $this->root->getMountsIn($this->path); $mounts[] = $this->getMountPoint(); @@ -387,55 +388,19 @@ class Folder extends Node implements \OCP\Files\Folder { $query = $builder ->select('f.*') ->from('filecache', 'f') - ->where($builder->expr()->gt('f.storage_mtime', $builder->createNamedParameter($since, IQueryBuilder::PARAM_INT))) ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) ->andWhere($builder->expr()->orX( // handle non empty folders separate $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), $builder->expr()->eq('f.size', new Literal(0)) )) - ->orderBy('f.mtime', 'DESC'); + ->orderBy('f.mtime', 'DESC') + ->setMaxResults($limit) + ->setFirstResult($offset); $result = $query->execute()->fetchAll(); - // select folders with their mtime being the mtime of the oldest file in the folder - // this way we still show new folders but dont bumb the folder every time a file in it is changed - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $query = $builder - ->select('p.fileid', 'p.storage', 'p.mimetype', 'p.mimepart', 'p.size', 'p.path', 'p.etag', 'f1.storage_mtime', 'f1.mtime', 'p.permissions') - ->from('filecache', 'f1') - ->leftJoin('f1', 'filecache', 'f2', $builder->expr()->andX( // find the f1 with lowest mtime in the folder - $builder->expr()->eq('f1.parent', 'f2.parent'), - $builder->expr()->gt('f1.storage_mtime', 'f2.storage_mtime') - )) - ->innerJoin('f1', 'filecache', 'p', $builder->expr()->eq('f1.parent', 'p.fileid')) - ->where($builder->expr()->isNull('f2.fileid')) - ->andWhere($builder->expr()->gt('f1.storage_mtime', $builder->createNamedParameter($since, IQueryBuilder::PARAM_INT))) - ->andWhere($builder->expr()->in('f1.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) - ->andWhere($builder->expr()->neq('f1.size', new Literal(0))) - ->orderBy('f1.storage_mtime', 'DESC'); - - $folderResults = $query->execute()->fetchAll(); - - $found = []; // we sometimes get duplicate folders - $folderResults = array_filter($folderResults, function ($item) use (&$found) { - $isFound = isset($found[$item['fileid']]); - $found[$item['fileid']] = true; - return !$isFound; - }); - - $result = array_merge($folderResults, $result); - - usort($result, function ($a, $b) use ($folderMimetype) { - $diff = $b['mtime'] - $a['mtime']; - if ($diff === 0) { - return $a['mimetype'] === $folderMimetype ? -1 : 1; - } else { - return $diff; - } - }); - - $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { + $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { $mount = $mountMap[$entry['storage']]; $entry['internalPath'] = $entry['path']; $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); diff --git a/lib/private/Files/Node/LazyRoot.php b/lib/private/Files/Node/LazyRoot.php index adc41153313..317b8144653 100644 --- a/lib/private/Files/Node/LazyRoot.php +++ b/lib/private/Files/Node/LazyRoot.php @@ -474,7 +474,7 @@ class LazyRoot implements IRootFolder { /** * @inheritDoc */ - public function getRecent($type) { + public function getRecent($limit, $offset = 0) { return $this->__call(__FUNCTION__, func_get_args()); } } diff --git a/lib/public/Files/Folder.php b/lib/public/Files/Folder.php index b6686732f42..8f8576d8503 100644 --- a/lib/public/Files/Folder.php +++ b/lib/public/Files/Folder.php @@ -177,9 +177,10 @@ interface Folder extends Node { public function getNonExistingName($name); /** - * @param int $since + * @param int $limit + * @param int $offset * @return \OCP\Files\Node[] * @since 9.1.0 */ - public function getRecent($since); + public function getRecent($limit, $offset = 0); } diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index eef78e7d428..18acfcae1fa 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -836,7 +836,7 @@ class FolderTest extends \Test\TestCase { 'mimetype' => 'text/plain', 'size' => 3 ]); - $cache->put('bar/foo/toold.txt', [ + $id3 = $cache->put('bar/foo/older.txt', [ 'storage_mtime' => $baseTime - 600, 'mtime' => $baseTime - 600, 'mimetype' => 'text/plain', @@ -846,11 +846,11 @@ class FolderTest extends \Test\TestCase { $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); - $nodes = $node->getRecent($baseTime - 500); + $nodes = $node->getRecent(5); $ids = array_map(function (Node $node) { return (int)$node->getId(); }, $nodes); - $this->assertEquals([$id1, $id2], $ids); + $this->assertEquals([$id1, $id2, $id3], $ids); } public function testRecentFolder() { @@ -900,14 +900,13 @@ class FolderTest extends \Test\TestCase { $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); - $nodes = $node->getRecent($baseTime - 500); + $nodes = $node->getRecent(5); $ids = array_map(function (Node $node) { return (int)$node->getId(); }, $nodes); - $this->assertEquals([$id2, $id1, $id3], $ids);// sort folders before files with the same mtime, folders get the lowest child mtime + $this->assertEquals([$id2, $id3], $ids); $this->assertEquals($baseTime, $nodes[0]->getMTime()); $this->assertEquals($baseTime - 100, $nodes[1]->getMTime()); - $this->assertEquals($baseTime - 100, $nodes[2]->getMTime()); } public function testRecentJail() { @@ -952,7 +951,7 @@ class FolderTest extends \Test\TestCase { $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); - $nodes = $node->getRecent($baseTime - 500); + $nodes = $node->getRecent(5); $ids = array_map(function (Node $node) { return (int)$node->getId(); }, $nodes); -- cgit v1.2.3