nextcloud/tests/lib/Files/Node/FolderTest.php

1041 lines
32 KiB
PHP
Raw Normal View History

<?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;
2016-09-12 22:21:42 +03:00
use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint;
use OC\Files\Node\Folder;
use OC\Files\Node\Node;
2016-09-12 22:21:42 +03:00
use OC\Files\Node\Root;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
2016-07-22 15:37:37 +03:00
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\View;
2016-09-12 22:21:42 +03:00
use OCP\Files\Mount\IMountPoint;
2013-09-11 00:21:49 +04:00
use OCP\Files\NotFoundException;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchQuery;
2016-09-12 22:21:42 +03:00
use OCP\Files\Storage;
/**
* Class FolderTest
*
* @group DB
*
* @package Test\Files\Node
*/
class FolderTest extends NodeTest {
protected function createTestNode($root, $view, $path) {
return new 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() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
2016-09-12 22:21:42 +03:00
->getMock();
$root->expects($this->any())
->method('getUser')
->willReturn($this->user);
$view->expects($this->any())
->method('getDirectoryContent')
->with('/bar/foo')
->willReturn([
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 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() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$root->expects($this->once())
->method('get')
->with('/bar/foo/asd');
$node = new Folder($root, $view, '/bar/foo');
$node->get('asd');
}
public function testNodeExists() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$child = new Folder($root, $view, '/bar/foo/asd');
$root->expects($this->once())
->method('get')
->with('/bar/foo/asd')
->willReturn($child);
$node = new Folder($root, $view, '/bar/foo');
$this->assertTrue($node->nodeExists('asd'));
}
public function testNodeExistsNotExists() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$root->expects($this->once())
->method('get')
->with('/bar/foo/asd')
->will($this->throwException(new NotFoundException()));
$node = new Folder($root, $view, '/bar/foo');
$this->assertFalse($node->nodeExists('asd'));
}
public function testNewFolder() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
$view->expects($this->once())
->method('mkdir')
->with('/bar/foo/asd')
->willReturn(true);
$node = new Folder($root, $view, '/bar/foo');
$child = new Folder($root, $view, '/bar/foo/asd');
$result = $node->newFolder('asd');
$this->assertEquals($child, $result);
}
public function testNewFolderNotPermitted() {
$this->expectException(\OCP\Files\NotPermittedException::class);
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
$node = new Folder($root, $view, '/bar/foo');
$node->newFolder('asd');
}
public function testNewFile() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
$view->expects($this->once())
->method('touch')
->with('/bar/foo/asd')
->willReturn(true);
$node = new Folder($root, $view, '/bar/foo');
$child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
$result = $node->newFile('asd');
$this->assertEquals($child, $result);
}
public function testNewFileNotPermitted() {
$this->expectException(\OCP\Files\NotPermittedException::class);
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
$node = new Folder($root, $view, '/bar/foo');
$node->newFile('asd');
}
public function testGetFreeSpace() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
$view->expects($this->once())
->method('free_space')
->with('/bar/foo')
->willReturn(100);
$node = new Folder($root, $view, '/bar/foo');
$this->assertEquals(100, $node->getFreeSpace());
}
public function testSearch() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->method('getUser')
->willReturn($this->user);
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$storage->method('getCache')
->willReturn($cache);
2016-09-12 22:21:42 +03:00
$mount = $this->createMock(IMountPoint::class);
$mount->method('getStorage')
->willReturn($storage);
$mount->method('getInternalPath')
->willReturn('foo');
2016-03-21 16:20:33 +03:00
$cache->method('searchQuery')
->willReturn([
new CacheEntry(['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]);
$root->method('getMountsIn')
->with('/bar/foo')
->willReturn([]);
$root->method('getMount')
2016-03-21 16:20:33 +03:00
->with('/bar/foo')
->willReturn($mount);
$node = new 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() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
/** @var \PHPUnit\Framework\MockObject\MockObject|Storage $storage */
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
2016-09-12 22:21:42 +03:00
$mount = $this->createMock(IMountPoint::class);
$mount->method('getStorage')
->willReturn($storage);
$mount->method('getInternalPath')
->willReturn('files');
2016-03-21 16:20:33 +03:00
$storage->method('getCache')
->willReturn($cache);
$cache->method('searchQuery')
->willReturn([
new CacheEntry(['fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]);
$root->method('getMountsIn')
->with('')
->willReturn([]);
$root->method('getMount')
2016-03-21 16:20:33 +03:00
->with('')
->willReturn($mount);
$result = $root->search('qw');
$this->assertEquals(1, count($result));
$this->assertEquals('/foo', $result[0]->getPath());
}
public function testSearchInStorageRoot() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->method('getUser')
->willReturn($this->user);
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
2016-09-12 22:21:42 +03:00
$mount = $this->createMock(IMountPoint::class);
$mount->method('getStorage')
->willReturn($storage);
$mount->method('getInternalPath')
->willReturn('');
2016-03-21 16:20:33 +03:00
$storage->method('getCache')
->willReturn($cache);
$cache->method('searchQuery')
->willReturn([
new CacheEntry(['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]);
$root->method('getMountsIn')
->with('/bar')
->willReturn([]);
$root->method('getMount')
2016-03-21 16:20:33 +03:00
->with('/bar')
->willReturn($mount);
$node = new Folder($root, $view, '/bar');
$result = $node->search('qw');
$this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
}
public function testSearchSubStorages() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturn($this->user);
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
2016-09-12 22:21:42 +03:00
$subStorage = $this->createMock(Storage::class);
$subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
2016-09-12 22:21:42 +03:00
$mount = $this->createMock(IMountPoint::class);
$mount->method('getStorage')
->willReturn($storage);
$mount->method('getInternalPath')
->willReturn('foo');
2016-03-21 16:20:33 +03:00
$subMount->method('getStorage')
->willReturn($subStorage);
$subMount->method('getMountPoint')
->willReturn('/bar/foo/bar/');
$storage->method('getCache')
->willReturn($cache);
$subStorage->method('getCache')
->willReturn($subCache);
$cache->method('searchQuery')
->willReturn([
new CacheEntry(['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]);
$subCache->method('searchQuery')
->willReturn([
new CacheEntry(['fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]);
$root->method('getMountsIn')
->with('/bar/foo')
->willReturn([$subMount]);
$root->method('getMount')
2016-03-21 16:20:33 +03:00
->with('/bar/foo')
->willReturn($mount);
$node = new 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 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));
}
2014-08-05 18:58:10 +04:00
public function testGetById() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
2014-08-05 18:58:10 +04:00
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
2014-08-05 18:58:10 +04:00
*/
2016-09-12 22:21:42 +03:00
$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();
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount = new MountPoint($storage, '/bar');
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
2014-08-05 18:58:10 +04:00
$fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
2014-08-05 18:58:10 +04:00
$storage->expects($this->once())
->method('getCache')
->willReturn($cache);
2014-08-05 18:58:10 +04:00
$this->userMountCache->expects($this->any())
->method('getMountsForFileId')
->with(1)
->willReturn([new CachedMountInfo(
$this->user,
1,
0,
'/bar/',
1,
''
)]);
2014-08-05 18:58:10 +04:00
$cache->expects($this->once())
->method('get')
->with(1)
->willReturn($fileInfo);
2014-08-05 18:58:10 +04:00
$root->expects($this->once())
->method('getMountsIn')
->with('/bar/foo')
->willReturn([]);
2014-08-05 18:58:10 +04:00
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->willReturn($mount);
2014-08-05 18:58:10 +04:00
$node = new Folder($root, $view, '/bar/foo');
2014-08-05 18:58:10 +04:00
$result = $node->getById(1);
$this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
}
public function testGetByIdMountRoot() {
$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');
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$fileInfo = new CacheEntry(['path' => '', 'mimetype' => 'text/plain'], null);
$storage->expects($this->once())
->method('getCache')
->willReturn($cache);
$this->userMountCache->expects($this->any())
->method('getMountsForFileId')
->with(1)
->willReturn([new CachedMountInfo(
$this->user,
1,
0,
'/bar/',
1,
''
)]);
$cache->expects($this->once())
->method('get')
->with(1)
->willReturn($fileInfo);
$root->expects($this->once())
->method('getMount')
->with('/bar')
->willReturn($mount);
$node = new Folder($root, $view, '/bar');
$result = $node->getById(1);
$this->assertEquals(1, count($result));
$this->assertEquals('/bar', $result[0]->getPath());
}
2014-08-05 18:58:10 +04:00
public function testGetByIdOutsideFolder() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
2014-08-05 18:58:10 +04:00
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
2014-08-05 18:58:10 +04:00
*/
2016-09-12 22:21:42 +03:00
$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();
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount = new MountPoint($storage, '/bar');
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
2014-08-05 18:58:10 +04:00
$fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null);
2014-08-05 18:58:10 +04:00
$storage->expects($this->once())
->method('getCache')
->willReturn($cache);
2014-08-05 18:58:10 +04:00
$this->userMountCache->expects($this->any())
->method('getMountsForFileId')
->with(1)
->willReturn([new CachedMountInfo(
$this->user,
1,
0,
'/bar/',
1,
''
)]);
2014-08-05 18:58:10 +04:00
$cache->expects($this->once())
->method('get')
->with(1)
->willReturn($fileInfo);
2014-08-05 18:58:10 +04:00
$root->expects($this->once())
->method('getMountsIn')
->with('/bar/foo')
->willReturn([]);
2014-08-05 18:58:10 +04:00
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->willReturn($mount);
2014-08-05 18:58:10 +04:00
$node = new Folder($root, $view, '/bar/foo');
$result = $node->getById(1);
$this->assertEquals(0, count($result));
2014-08-05 18:58:10 +04:00
}
public function testGetByIdMultipleStorages() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
2014-08-05 18:58:10 +04:00
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
2014-08-05 18:58:10 +04:00
*/
2016-09-12 22:21:42 +03:00
$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();
2016-09-12 22:21:42 +03:00
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount1 = new MountPoint($storage, '/bar');
$mount2 = new MountPoint($storage, '/bar/foo/asd');
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
2014-08-05 18:58:10 +04:00
$fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
$storage->expects($this->exactly(2))
->method('getCache')
->willReturn($cache);
$this->userMountCache->expects($this->any())
->method('getMountsForFileId')
->with(1)
->willReturn([
new CachedMountInfo(
$this->user,
1,
0,
'/bar/',
1,
''
),
new CachedMountInfo(
$this->user,
1,
0,
'/bar/foo/asd/',
1,
''
),
]);
2014-08-05 18:58:10 +04:00
$storage->expects($this->any())
->method('getCache')
->willReturn($cache);
2014-08-05 18:58:10 +04:00
$cache->expects($this->any())
->method('get')
->with(1)
->willReturn($fileInfo);
2014-08-05 18:58:10 +04:00
$root->expects($this->any())
->method('getMountsIn')
->with('/bar/foo')
->willReturn([$mount2]);
2014-08-05 18:58:10 +04:00
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->willReturn($mount1);
2014-08-05 18:58:10 +04:00
$node = new Folder($root, $view, '/bar/foo');
2014-08-05 18:58:10 +04:00
$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
2016-07-22 15:37:37 +03:00
['foo', [], 'foo'],
['foo', ['foo'], 'foo (2)'],
['foo', ['foo', 'foo (2)'], 'foo (3)'],
];
}
/**
* @dataProvider uniqueNameProvider
*/
public function testGetUniqueName($name, $existingFiles, $expected) {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
$folderPath = '/bar/foo';
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
2016-09-12 22:21:42 +03:00
$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')
->willReturnCallback(function ($path) use ($existingFiles, $folderPath) {
foreach ($existingFiles as $existing) {
2016-07-22 15:37:37 +03:00
if ($folderPath . '/' . $existing === $path) {
return true;
}
}
return false;
});
$node = new Folder($root, $view, $folderPath);
$this->assertEquals($expected, $node->getNonExistingName($name));
}
2016-07-22 15:37:37 +03:00
public function testRecent() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
2016-07-22 15:37:37 +03:00
$folderPath = '/bar/foo';
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
2016-07-22 15:37:37 +03:00
*/
2016-09-12 22:21:42 +03:00
$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)
2016-07-22 15:37:37 +03:00
->disableOriginalConstructor()->getMock();
$baseTime = 1000;
$storage = new Temporary();
$mount = new MountPoint($storage, '');
$folderInfo->expects($this->any())
->method('getMountPoint')
->willReturn($mount);
2016-07-22 15:37:37 +03:00
$cache = $storage->getCache();
$id1 = $cache->put('bar/foo/inside.txt', [
'storage_mtime' => $baseTime,
'mtime' => $baseTime,
'mimetype' => 'text/plain',
'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_ALL,
2016-07-22 15:37:37 +03:00
]);
$id2 = $cache->put('bar/foo/old.txt', [
'storage_mtime' => $baseTime - 100,
'mtime' => $baseTime - 100,
'mimetype' => 'text/plain',
'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_READ,
2016-07-22 15:37:37 +03:00
]);
$cache->put('bar/asd/outside.txt', [
'storage_mtime' => $baseTime,
'mtime' => $baseTime,
'mimetype' => 'text/plain',
'size' => 3,
2016-07-22 15:37:37 +03:00
]);
$id3 = $cache->put('bar/foo/older.txt', [
2016-07-22 15:37:37 +03:00
'storage_mtime' => $baseTime - 600,
'mtime' => $baseTime - 600,
'mimetype' => 'text/plain',
'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_ALL,
2016-07-22 15:37:37 +03:00
]);
$node = new Folder($root, $view, $folderPath, $folderInfo);
2016-07-22 15:37:37 +03:00
$nodes = $node->getRecent(5);
2016-07-22 15:37:37 +03:00
$ids = array_map(function (Node $node) {
return (int)$node->getId();
}, $nodes);
$this->assertEquals([$id1, $id2, $id3], $ids);
2016-07-22 15:37:37 +03:00
}
public function testRecentFolder() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
2016-07-22 15:37:37 +03:00
$folderPath = '/bar/foo';
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
2016-07-22 15:37:37 +03:00
*/
2016-09-12 22:21:42 +03:00
$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)
2016-07-22 15:37:37 +03:00
->disableOriginalConstructor()->getMock();
$baseTime = 1000;
$storage = new Temporary();
$mount = new MountPoint($storage, '');
$folderInfo->expects($this->any())
->method('getMountPoint')
->willReturn($mount);
2016-07-22 15:37:37 +03:00
$cache = $storage->getCache();
$id1 = $cache->put('bar/foo/folder', [
'storage_mtime' => $baseTime,
'mtime' => $baseTime,
'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER,
'size' => 3,
'permissions' => 0,
2016-07-22 15:37:37 +03:00
]);
$id2 = $cache->put('bar/foo/folder/bar.txt', [
'storage_mtime' => $baseTime,
'mtime' => $baseTime,
'mimetype' => 'text/plain',
'size' => 3,
'parent' => $id1,
'permissions' => \OCP\Constants::PERMISSION_ALL,
2016-07-22 15:37:37 +03:00
]);
$id3 = $cache->put('bar/foo/folder/asd.txt', [
2016-07-11 13:58:43 +03:00
'storage_mtime' => $baseTime - 100,
2016-07-22 15:37:37 +03:00
'mtime' => $baseTime - 100,
'mimetype' => 'text/plain',
'size' => 3,
'parent' => $id1,
'permissions' => \OCP\Constants::PERMISSION_ALL,
2016-07-22 15:37:37 +03:00
]);
$node = new Folder($root, $view, $folderPath, $folderInfo);
2016-07-22 15:37:37 +03:00
$nodes = $node->getRecent(5);
2016-07-22 15:37:37 +03:00
$ids = array_map(function (Node $node) {
return (int)$node->getId();
}, $nodes);
$this->assertEquals([$id2, $id3], $ids);
2016-07-11 13:58:43 +03:00
$this->assertEquals($baseTime, $nodes[0]->getMTime());
$this->assertEquals($baseTime - 100, $nodes[1]->getMTime());
2016-07-22 15:37:37 +03:00
}
public function testRecentJail() {
2016-09-12 22:21:42 +03:00
$manager = $this->createMock(Manager::class);
2016-07-22 15:37:37 +03:00
$folderPath = '/bar/foo';
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
2016-07-22 15:37:37 +03:00
*/
2016-09-12 22:21:42 +03:00
$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)
2016-07-22 15:37:37 +03:00
->disableOriginalConstructor()->getMock();
$baseTime = 1000;
$storage = new Temporary();
$jail = new Jail([
'storage' => $storage,
'root' => 'folder',
2016-07-22 15:37:37 +03:00
]);
$mount = new MountPoint($jail, '/bar/foo');
$folderInfo->expects($this->any())
->method('getMountPoint')
->willReturn($mount);
2016-07-22 15:37:37 +03:00
$cache = $storage->getCache();
$id1 = $cache->put('folder/inside.txt', [
'storage_mtime' => $baseTime,
'mtime' => $baseTime,
'mimetype' => 'text/plain',
'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_ALL,
2016-07-22 15:37:37 +03:00
]);
$cache->put('outside.txt', [
'storage_mtime' => $baseTime - 100,
'mtime' => $baseTime - 100,
'mimetype' => 'text/plain',
'size' => 3,
2016-07-22 15:37:37 +03:00
]);
$node = new Folder($root, $view, $folderPath, $folderInfo);
2016-07-22 15:37:37 +03:00
$nodes = $node->getRecent(5);
2016-07-22 15:37:37 +03:00
$ids = array_map(function (Node $node) {
return (int)$node->getId();
}, $nodes);
$this->assertEquals([$id1], $ids);
}
public function offsetLimitProvider() {
return [
[0, 10, [10, 11, 12, 13, 14, 15, 16, 17]],
[0, 5, [10, 11, 12, 13, 14]],
[0, 2, [10, 11]],
[3, 2, [13, 14]],
[3, 5, [13, 14, 15, 16, 17]],
[5, 2, [15, 16]],
[6, 2, [16, 17]],
[7, 2, [17]],
[10, 2, []]
];
}
/**
* @dataProvider offsetLimitProvider
*/
public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array $expectedIds) {
$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')
->willReturn($this->user);
$storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subCache1 = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subStorage1 = $this->createMock(Storage::class);
$subMount1 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
$subCache2 = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subStorage2 = $this->createMock(Storage::class);
$subMount2 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
$mount = $this->createMock(IMountPoint::class);
$mount->method('getStorage')
->willReturn($storage);
$mount->method('getInternalPath')
->willReturn('foo');
$subMount1->method('getStorage')
->willReturn($subStorage1);
$subMount1->method('getMountPoint')
->willReturn('/bar/foo/bar/');
$storage->method('getCache')
->willReturn($cache);
$subStorage1->method('getCache')
->willReturn($subCache1);
$subMount2->method('getStorage')
->willReturn($subStorage2);
$subMount2->method('getMountPoint')
->willReturn('/bar/foo/bar2/');
$subStorage2->method('getCache')
->willReturn($subCache2);
$cache->method('searchQuery')
->willReturnCallback(function (ISearchQuery $query) {
return array_slice([
new CacheEntry(['fileid' => 10, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 11, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 12, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 13, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'])
], $query->getOffset(), $query->getOffset() + $query->getLimit());
});
$subCache1->method('searchQuery')
->willReturnCallback(function (ISearchQuery $query) {
return array_slice([
new CacheEntry(['fileid' => 14, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 15, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
], $query->getOffset(), $query->getOffset() + $query->getLimit());
});
$subCache2->method('searchQuery')
->willReturnCallback(function (ISearchQuery $query) {
return array_slice([
new CacheEntry(['fileid' => 16, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 17, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
], $query->getOffset(), $query->getOffset() + $query->getLimit());
});
$root->method('getMountsIn')
->with('/bar/foo')
->willReturn([$subMount1, $subMount2]);
$root->method('getMount')
->with('/bar/foo')
->willReturn($mount);
$node = new Folder($root, $view, '/bar/foo');
$comparison = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%foo%');
$query = new SearchQuery($comparison, $limit, $offset, []);
$result = $node->search($query);
$ids = array_map(function(Node $info) {
return $info->getId();
}, $result);
$this->assertEquals($expectedIds, $ids);
}
}