123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947 |
- <?php
- /**
- * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
- */
- namespace Test\Files\Node;
- use OC\Files\Cache\Cache;
- use OC\Files\Cache\CacheEntry;
- use OC\Files\Config\CachedMountInfo;
- use OC\Files\FileInfo;
- use OC\Files\Mount\Manager;
- use OC\Files\Mount\MountPoint;
- use OC\Files\Node\Node;
- use OC\Files\Node\Root;
- use OC\Files\Storage\Temporary;
- use OC\Files\Storage\Wrapper\Jail;
- use OC\Files\View;
- use OC\User\User;
- use OCP\Files\Mount\IMountPoint;
- use OCP\Files\NotFoundException;
- use OCP\Files\Storage;
- /**
- * Class FolderTest
- *
- * @group DB
- *
- * @package Test\Files\Node
- */
- class FolderTest extends NodeTest {
- protected function createTestNode($root, $view, $path) {
- return new \OC\Files\Node\Folder($root, $view, $path);
- }
- protected function getNodeClass() {
- return '\OC\Files\Node\Folder';
- }
- protected function getNonExistingNodeClass() {
- return '\OC\Files\Node\NonExistingFolder';
- }
- protected function getViewDeleteMethod() {
- return 'rmdir';
- }
- public function testGetDirectoryContent() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $view->expects($this->any())
- ->method('getDirectoryContent')
- ->with('/bar/foo')
- ->will($this->returnValue(array(
- new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null),
- new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null)
- )));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $children = $node->getDirectoryListing();
- $this->assertEquals(2, count($children));
- $this->assertInstanceOf('\OC\Files\Node\File', $children[0]);
- $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]);
- $this->assertEquals('asd', $children[0]->getName());
- $this->assertEquals('qwerty', $children[1]->getName());
- $this->assertEquals(2, $children[0]->getId());
- $this->assertEquals(3, $children[1]->getId());
- }
- public function testGet() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $root->expects($this->once())
- ->method('get')
- ->with('/bar/foo/asd');
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $node->get('asd');
- }
- public function testNodeExists() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
- $root->expects($this->once())
- ->method('get')
- ->with('/bar/foo/asd')
- ->will($this->returnValue($child));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $this->assertTrue($node->nodeExists('asd'));
- }
- public function testNodeExistsNotExists() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $root->expects($this->once())
- ->method('get')
- ->with('/bar/foo/asd')
- ->will($this->throwException(new NotFoundException()));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $this->assertFalse($node->nodeExists('asd'));
- }
- public function testNewFolder() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/bar/foo')
- ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
- $view->expects($this->once())
- ->method('mkdir')
- ->with('/bar/foo/asd')
- ->will($this->returnValue(true));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
- $result = $node->newFolder('asd');
- $this->assertEquals($child, $result);
- }
- /**
- * @expectedException \OCP\Files\NotPermittedException
- */
- public function testNewFolderNotPermitted() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/bar/foo')
- ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $node->newFolder('asd');
- }
- public function testNewFile() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/bar/foo')
- ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
- $view->expects($this->once())
- ->method('touch')
- ->with('/bar/foo/asd')
- ->will($this->returnValue(true));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
- $result = $node->newFile('asd');
- $this->assertEquals($child, $result);
- }
- /**
- * @expectedException \OCP\Files\NotPermittedException
- */
- public function testNewFileNotPermitted() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/bar/foo')
- ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $node->newFile('asd');
- }
- public function testGetFreeSpace() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $view->expects($this->once())
- ->method('free_space')
- ->with('/bar/foo')
- ->will($this->returnValue(100));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $this->assertEquals(100, $node->getFreeSpace());
- }
- public function testSearch() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $storage = $this->createMock(Storage::class);
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $storage->expects($this->once())
- ->method('getCache')
- ->will($this->returnValue($cache));
- $mount = $this->createMock(IMountPoint::class);
- $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%')
- ->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()));
- $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');
- $this->assertEquals(1, count($result));
- $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
- }
- public function testSearchInRoot() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getUser', 'getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $storage = $this->createMock(Storage::class);
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $mount = $this->createMock(IMountPoint::class);
- $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));
- $cache->expects($this->once())
- ->method('search')
- ->with('%qw%')
- ->will($this->returnValue(array(
- array('fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'),
- array('fileid' => 3, 'path' => 'files_trashbin/foo2.d12345', 'name' => 'foo2.d12345', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'),
- )));
- $root->expects($this->once())
- ->method('getMountsIn')
- ->with('')
- ->will($this->returnValue(array()));
- $root->expects($this->once())
- ->method('getMount')
- ->with('')
- ->will($this->returnValue($mount));
- $result = $root->search('qw');
- $this->assertEquals(1, count($result));
- $this->assertEquals('/foo', $result[0]->getPath());
- }
- public function testSearchInStorageRoot() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $storage = $this->createMock(Storage::class);
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $mount = $this->createMock(IMountPoint::class);
- $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));
- $cache->expects($this->once())
- ->method('search')
- ->with('%qw%')
- ->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')
- ->will($this->returnValue(array()));
- $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');
- $this->assertEquals(1, count($result));
- $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
- }
- public function testSearchByTag() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $storage = $this->createMock(Storage::class);
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $mount = $this->createMock(IMountPoint::class);
- $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));
- $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()));
- $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');
- $this->assertEquals(1, count($result));
- $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
- }
- public function testSearchSubStorages() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $root->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $storage = $this->createMock(Storage::class);
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $subStorage = $this->createMock(Storage::class);
- $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
- $mount = $this->createMock(IMountPoint::class);
- $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));
- $subMount->expects($this->once())
- ->method('getMountPoint')
- ->will($this->returnValue('/bar/foo/bar/'));
- $storage->expects($this->once())
- ->method('getCache')
- ->will($this->returnValue($cache));
- $subStorage->expects($this->once())
- ->method('getCache')
- ->will($this->returnValue($subCache));
- $cache->expects($this->once())
- ->method('search')
- ->with('%qw%')
- ->will($this->returnValue(array(
- array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
- )));
- $subCache->expects($this->once())
- ->method('search')
- ->with('%qw%')
- ->will($this->returnValue(array(
- array('fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
- )));
- $root->expects($this->once())
- ->method('getMountsIn')
- ->with('/bar/foo')
- ->will($this->returnValue(array($subMount)));
- $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');
- $this->assertEquals(2, count($result));
- }
- public function testIsSubNode() {
- $file = new Node(null, null, '/foo/bar');
- $folder = new \OC\Files\Node\Folder(null, null, '/foo');
- $this->assertTrue($folder->isSubNode($file));
- $this->assertFalse($folder->isSubNode($folder));
- $file = new Node(null, null, '/foobar');
- $this->assertFalse($folder->isSubNode($file));
- }
- public function testGetById() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $storage = $this->createMock(\OC\Files\Storage\Storage::class);
- $mount = new MountPoint($storage, '/bar');
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
- $storage->expects($this->once())
- ->method('getCache')
- ->will($this->returnValue($cache));
- $this->userMountCache->expects($this->any())
- ->method('getMountsForFileId')
- ->with(1)
- ->will($this->returnValue([new CachedMountInfo(
- $this->user,
- 1,
- 0,
- '/bar/',
- 1,
- ''
- )]));
- $cache->expects($this->once())
- ->method('get')
- ->with(1)
- ->will($this->returnValue($fileInfo));
- $root->expects($this->once())
- ->method('getMountsIn')
- ->with('/bar/foo')
- ->will($this->returnValue(array()));
- $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->getById(1);
- $this->assertEquals(1, count($result));
- $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
- }
- public function testGetByIdOutsideFolder() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $storage = $this->createMock(\OC\Files\Storage\Storage::class);
- $mount = new MountPoint($storage, '/bar');
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null);
- $storage->expects($this->once())
- ->method('getCache')
- ->will($this->returnValue($cache));
- $this->userMountCache->expects($this->any())
- ->method('getMountsForFileId')
- ->with(1)
- ->will($this->returnValue([new CachedMountInfo(
- $this->user,
- 1,
- 0,
- '/bar/',
- 1,
- ''
- )]));
- $cache->expects($this->once())
- ->method('get')
- ->with(1)
- ->will($this->returnValue($fileInfo));
- $root->expects($this->once())
- ->method('getMountsIn')
- ->with('/bar/foo')
- ->will($this->returnValue(array()));
- $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->getById(1);
- $this->assertEquals(0, count($result));
- }
- public function testGetByIdMultipleStorages() {
- $manager = $this->createMock(Manager::class);
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $storage = $this->createMock(\OC\Files\Storage\Storage::class);
- $mount1 = new MountPoint($storage, '/bar');
- $mount2 = new MountPoint($storage, '/bar/foo/asd');
- $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
- $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
- $storage->expects($this->exactly(2))
- ->method('getCache')
- ->will($this->returnValue($cache));
- $this->userMountCache->expects($this->any())
- ->method('getMountsForFileId')
- ->with(1)
- ->will($this->returnValue([
- new CachedMountInfo(
- $this->user,
- 1,
- 0,
- '/bar/',
- 1,
- ''
- ),
- new CachedMountInfo(
- $this->user,
- 1,
- 0,
- '/bar/foo/asd/',
- 1,
- ''
- )
- ]));
- $storage->expects($this->any())
- ->method('getCache')
- ->will($this->returnValue($cache));
- $cache->expects($this->any())
- ->method('get')
- ->with(1)
- ->will($this->returnValue($fileInfo));
- $root->expects($this->any())
- ->method('getMountsIn')
- ->with('/bar/foo')
- ->will($this->returnValue(array($mount2)));
- $root->expects($this->once())
- ->method('getMount')
- ->with('/bar/foo')
- ->will($this->returnValue($mount1));
- $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
- $result = $node->getById(1);
- $this->assertEquals(2, count($result));
- $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
- $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath());
- }
- public function uniqueNameProvider() {
- return [
- // input, existing, expected
- ['foo', [], 'foo'],
- ['foo', ['foo'], 'foo (2)'],
- ['foo', ['foo', 'foo (2)'], 'foo (3)']
- ];
- }
- /**
- * @dataProvider uniqueNameProvider
- */
- public function testGetUniqueName($name, $existingFiles, $expected) {
- $manager = $this->createMock(Manager::class);
- $folderPath = '/bar/foo';
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getUser', 'getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- $view->expects($this->any())
- ->method('file_exists')
- ->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) {
- foreach ($existingFiles as $existing) {
- if ($folderPath . '/' . $existing === $path) {
- return true;
- }
- }
- return false;
- }));
- $node = new \OC\Files\Node\Folder($root, $view, $folderPath);
- $this->assertEquals($expected, $node->getNonExistingName($name));
- }
- public function testRecent() {
- $manager = $this->createMock(Manager::class);
- $folderPath = '/bar/foo';
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getUser', 'getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
- $folderInfo = $this->getMockBuilder(FileInfo::class)
- ->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
- ]);
- $id3 = $cache->put('bar/foo/older.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(5);
- $ids = array_map(function (Node $node) {
- return (int)$node->getId();
- }, $nodes);
- $this->assertEquals([$id1, $id2, $id3], $ids);
- }
- public function testRecentFolder() {
- $manager = $this->createMock(Manager::class);
- $folderPath = '/bar/foo';
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getUser', 'getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
- $folderInfo = $this->getMockBuilder(FileInfo::class)
- ->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 - 100,
- 'mtime' => $baseTime - 100,
- 'mimetype' => 'text/plain',
- 'size' => 3,
- 'parent' => $id1
- ]);
- $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
- $nodes = $node->getRecent(5);
- $ids = array_map(function (Node $node) {
- return (int)$node->getId();
- }, $nodes);
- $this->assertEquals([$id2, $id3], $ids);
- $this->assertEquals($baseTime, $nodes[0]->getMTime());
- $this->assertEquals($baseTime - 100, $nodes[1]->getMTime());
- }
- public function testRecentJail() {
- $manager = $this->createMock(Manager::class);
- $folderPath = '/bar/foo';
- /**
- * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
- */
- $view = $this->createMock(View::class);
- /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
- $root = $this->getMockBuilder(Root::class)
- ->setMethods(['getUser', 'getMountsIn', 'getMount'])
- ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
- ->getMock();
- /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
- $folderInfo = $this->getMockBuilder(FileInfo::class)
- ->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(5);
- $ids = array_map(function (Node $node) {
- return (int)$node->getId();
- }, $nodes);
- $this->assertEquals([$id1], $ids);
- }
- }
|