2012-10-21 04:12:58 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file. */
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
namespace Test\Files;
|
|
|
|
|
2013-02-10 15:44:27 +04:00
|
|
|
class TemporaryNoTouch extends \OC\Files\Storage\Temporary {
|
|
|
|
public function touch($path, $mtime = null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
class View extends \PHPUnit_Framework_TestCase {
|
2012-10-21 04:12:58 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage[] $storages;
|
|
|
|
*/
|
|
|
|
private $storages = array();
|
|
|
|
|
|
|
|
public function setUp() {
|
2013-06-26 22:48:54 +04:00
|
|
|
\OC_User::clearBackends();
|
|
|
|
\OC_User::useBackend(new \OC_User_Dummy());
|
|
|
|
|
|
|
|
//login
|
|
|
|
\OC_User::createUser('test', 'test');
|
|
|
|
$this->user=\OC_User::getUser();
|
|
|
|
\OC_User::setUserId('test');
|
|
|
|
|
2012-10-26 20:07:01 +04:00
|
|
|
\OC\Files\Filesystem::clearMounts();
|
2012-10-21 04:12:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2013-06-26 22:48:54 +04:00
|
|
|
\OC_User::setUserId($this->user);
|
2012-10-21 04:12:58 +04:00
|
|
|
foreach ($this->storages as $storage) {
|
|
|
|
$cache = $storage->getCache();
|
2012-10-28 14:26:31 +04:00
|
|
|
$ids = $cache->getAll();
|
2012-11-15 03:57:30 +04:00
|
|
|
$permissionsCache = $storage->getPermissionsCache();
|
|
|
|
$permissionsCache->removeMultiple($ids, \OC_User::getUser());
|
2012-10-21 04:12:58 +04:00
|
|
|
$cache->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2012-10-21 04:12:58 +04:00
|
|
|
public function testCacheAPI() {
|
|
|
|
$storage1 = $this->getTestStorage();
|
|
|
|
$storage2 = $this->getTestStorage();
|
|
|
|
$storage3 = $this->getTestStorage();
|
2012-10-26 20:07:01 +04:00
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
|
|
|
\OC\Files\Filesystem::mount($storage3, array(), '/folder/anotherstorage');
|
2012-10-21 04:12:58 +04:00
|
|
|
$textSize = strlen("dummy file data\n");
|
|
|
|
$imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
|
|
|
|
$storageSize = $textSize * 2 + $imageSize;
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('/foo.txt');
|
2012-10-21 04:12:58 +04:00
|
|
|
$this->assertEquals($textSize, $cachedData['size']);
|
|
|
|
$this->assertEquals('text/plain', $cachedData['mimetype']);
|
2013-01-07 04:03:11 +04:00
|
|
|
$this->assertNotEquals(-1, $cachedData['permissions']);
|
2012-10-21 04:12:58 +04:00
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
$cachedData = $rootView->getFileInfo('/');
|
2012-10-21 04:12:58 +04:00
|
|
|
$this->assertEquals($storageSize * 3, $cachedData['size']);
|
|
|
|
$this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
$cachedData = $rootView->getFileInfo('/folder');
|
2012-10-21 04:12:58 +04:00
|
|
|
$this->assertEquals($storageSize + $textSize, $cachedData['size']);
|
|
|
|
$this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
$folderData = $rootView->getDirectoryContent('/');
|
2012-10-21 04:12:58 +04:00
|
|
|
/**
|
|
|
|
* expected entries:
|
2012-11-25 19:08:35 +04:00
|
|
|
* folder
|
2012-10-21 04:12:58 +04:00
|
|
|
* foo.png
|
|
|
|
* foo.txt
|
|
|
|
* substorage
|
|
|
|
*/
|
|
|
|
$this->assertEquals(4, count($folderData));
|
2012-11-25 19:08:35 +04:00
|
|
|
$this->assertEquals('folder', $folderData[0]['name']);
|
|
|
|
$this->assertEquals('foo.png', $folderData[1]['name']);
|
|
|
|
$this->assertEquals('foo.txt', $folderData[2]['name']);
|
2012-10-21 04:12:58 +04:00
|
|
|
$this->assertEquals('substorage', $folderData[3]['name']);
|
|
|
|
|
2012-11-25 19:08:35 +04:00
|
|
|
$this->assertEquals($storageSize + $textSize, $folderData[0]['size']);
|
|
|
|
$this->assertEquals($imageSize, $folderData[1]['size']);
|
|
|
|
$this->assertEquals($textSize, $folderData[2]['size']);
|
2012-10-21 04:12:58 +04:00
|
|
|
$this->assertEquals($storageSize, $folderData[3]['size']);
|
2012-10-26 14:37:49 +04:00
|
|
|
|
2012-11-25 02:41:39 +04:00
|
|
|
$folderData = $rootView->getDirectoryContent('/substorage');
|
|
|
|
/**
|
|
|
|
* expected entries:
|
2012-11-25 19:08:35 +04:00
|
|
|
* folder
|
2012-11-25 02:41:39 +04:00
|
|
|
* foo.png
|
|
|
|
* foo.txt
|
|
|
|
*/
|
|
|
|
$this->assertEquals(3, count($folderData));
|
2012-11-25 19:08:35 +04:00
|
|
|
$this->assertEquals('folder', $folderData[0]['name']);
|
|
|
|
$this->assertEquals('foo.png', $folderData[1]['name']);
|
|
|
|
$this->assertEquals('foo.txt', $folderData[2]['name']);
|
2012-11-25 02:41:39 +04:00
|
|
|
|
2012-10-26 14:37:49 +04:00
|
|
|
$folderView = new \OC\Files\View('/folder');
|
|
|
|
$this->assertEquals($rootView->getFileInfo('/folder'), $folderView->getFileInfo('/'));
|
2012-10-26 14:43:23 +04:00
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('/foo.txt');
|
|
|
|
$this->assertFalse($cachedData['encrypted']);
|
|
|
|
$id = $rootView->putFileInfo('/foo.txt', array('encrypted' => true));
|
|
|
|
$cachedData = $rootView->getFileInfo('/foo.txt');
|
|
|
|
$this->assertTrue($cachedData['encrypted']);
|
|
|
|
$this->assertEquals($cachedData['fileid'], $id);
|
2012-12-11 04:06:21 +04:00
|
|
|
|
|
|
|
$this->assertFalse($rootView->getFileInfo('/non/existing'));
|
|
|
|
$this->assertEquals(array(), $rootView->getDirectoryContent('/non/existing'));
|
2012-10-21 04:12:58 +04:00
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-01-27 03:13:16 +04:00
|
|
|
function testGetPath() {
|
|
|
|
$storage1 = $this->getTestStorage();
|
|
|
|
$storage2 = $this->getTestStorage();
|
|
|
|
$storage3 = $this->getTestStorage();
|
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
|
|
|
\OC\Files\Filesystem::mount($storage3, array(), '/folder/anotherstorage');
|
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('/foo.txt');
|
|
|
|
$id1 = $cachedData['fileid'];
|
|
|
|
$this->assertEquals('/foo.txt', $rootView->getPath($id1));
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('/substorage/foo.txt');
|
|
|
|
$id2 = $cachedData['fileid'];
|
|
|
|
$this->assertEquals('/substorage/foo.txt', $rootView->getPath($id2));
|
|
|
|
|
|
|
|
$folderView = new \OC\Files\View('/substorage');
|
|
|
|
$this->assertEquals('/foo.txt', $folderView->getPath($id2));
|
|
|
|
$this->assertNull($folderView->getPath($id1));
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-01-22 23:57:15 +04:00
|
|
|
function testMountPointOverwrite() {
|
|
|
|
$storage1 = $this->getTestStorage(false);
|
|
|
|
$storage2 = $this->getTestStorage();
|
|
|
|
$storage1->mkdir('substorage');
|
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
$folderContent = $rootView->getDirectoryContent('/');
|
|
|
|
$this->assertEquals(4, count($folderContent));
|
|
|
|
}
|
|
|
|
|
2012-12-15 06:20:50 +04:00
|
|
|
function testCacheIncompleteFolder() {
|
|
|
|
$storage1 = $this->getTestStorage(false);
|
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
|
|
|
|
$entries = $rootView->getDirectoryContent('/');
|
|
|
|
$this->assertEquals(3, count($entries));
|
|
|
|
|
|
|
|
// /folder will already be in the cache but not scanned
|
|
|
|
$entries = $rootView->getDirectoryContent('/folder');
|
|
|
|
$this->assertEquals(1, count($entries));
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:05:29 +04:00
|
|
|
public function testAutoScan() {
|
|
|
|
$storage1 = $this->getTestStorage(false);
|
|
|
|
$storage2 = $this->getTestStorage(false);
|
2012-10-26 20:07:01 +04:00
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
2012-10-22 00:05:29 +04:00
|
|
|
$textSize = strlen("dummy file data\n");
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('/');
|
2012-10-22 00:05:29 +04:00
|
|
|
$this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
|
|
|
|
$this->assertEquals(-1, $cachedData['size']);
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
$folderData = $rootView->getDirectoryContent('/substorage/folder');
|
2012-10-22 00:05:29 +04:00
|
|
|
$this->assertEquals('text/plain', $folderData[0]['mimetype']);
|
|
|
|
$this->assertEquals($textSize, $folderData[0]['size']);
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2012-10-26 15:23:15 +04:00
|
|
|
function testSearch() {
|
|
|
|
$storage1 = $this->getTestStorage();
|
|
|
|
$storage2 = $this->getTestStorage();
|
|
|
|
$storage3 = $this->getTestStorage();
|
2012-10-26 20:07:01 +04:00
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
|
|
|
\OC\Files\Filesystem::mount($storage3, array(), '/folder/anotherstorage');
|
2012-10-26 15:23:15 +04:00
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
|
|
|
|
$results = $rootView->search('foo');
|
|
|
|
$this->assertEquals(6, count($results));
|
|
|
|
$paths = array();
|
|
|
|
foreach ($results as $result) {
|
2012-10-26 20:07:01 +04:00
|
|
|
$this->assertEquals($result['path'], \OC\Files\Filesystem::normalizePath($result['path']));
|
2012-10-26 15:23:15 +04:00
|
|
|
$paths[] = $result['path'];
|
|
|
|
}
|
|
|
|
$this->assertContains('/foo.txt', $paths);
|
|
|
|
$this->assertContains('/foo.png', $paths);
|
|
|
|
$this->assertContains('/substorage/foo.txt', $paths);
|
|
|
|
$this->assertContains('/substorage/foo.png', $paths);
|
|
|
|
$this->assertContains('/folder/anotherstorage/foo.txt', $paths);
|
|
|
|
$this->assertContains('/folder/anotherstorage/foo.png', $paths);
|
|
|
|
|
|
|
|
$folderView = new \OC\Files\View('/folder');
|
|
|
|
$results = $folderView->search('bar');
|
|
|
|
$this->assertEquals(2, count($results));
|
|
|
|
$paths = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$paths[] = $result['path'];
|
|
|
|
}
|
|
|
|
$this->assertContains('/anotherstorage/folder/bar.txt', $paths);
|
|
|
|
$this->assertContains('/bar.txt', $paths);
|
|
|
|
|
|
|
|
$results = $folderView->search('foo');
|
|
|
|
$this->assertEquals(2, count($results));
|
|
|
|
$paths = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$paths[] = $result['path'];
|
|
|
|
}
|
|
|
|
$this->assertContains('/anotherstorage/foo.txt', $paths);
|
|
|
|
$this->assertContains('/anotherstorage/foo.png', $paths);
|
2012-10-27 12:34:25 +04:00
|
|
|
|
|
|
|
$this->assertEquals(6, count($rootView->searchByMime('text')));
|
|
|
|
$this->assertEquals(3, count($folderView->searchByMime('text')));
|
2012-10-26 15:23:15 +04:00
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2012-10-28 14:43:45 +04:00
|
|
|
function testWatcher() {
|
|
|
|
$storage1 = $this->getTestStorage();
|
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('foo.txt');
|
|
|
|
$this->assertEquals(16, $cachedData['size']);
|
|
|
|
|
2013-02-10 15:27:35 +04:00
|
|
|
$rootView->putFileInfo('foo.txt', array('storage_mtime' => 10));
|
2012-10-28 14:43:45 +04:00
|
|
|
$storage1->file_put_contents('foo.txt', 'foo');
|
|
|
|
clearstatcache();
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('foo.txt');
|
|
|
|
$this->assertEquals(3, $cachedData['size']);
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-05-19 22:20:46 +04:00
|
|
|
function testCopyBetweenStorages() {
|
|
|
|
$storage1 = $this->getTestStorage();
|
|
|
|
$storage2 = $this->getTestStorage();
|
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
2013-05-19 23:04:41 +04:00
|
|
|
$rootView->mkdir('substorage/emptyfolder');
|
2013-05-19 22:20:46 +04:00
|
|
|
$rootView->copy('substorage', 'anotherfolder');
|
|
|
|
$this->assertTrue($rootView->is_dir('/anotherfolder'));
|
|
|
|
$this->assertTrue($rootView->is_dir('/substorage'));
|
2013-05-19 23:04:41 +04:00
|
|
|
$this->assertTrue($rootView->is_dir('/anotherfolder/emptyfolder'));
|
|
|
|
$this->assertTrue($rootView->is_dir('/substorage/emptyfolder'));
|
2013-05-19 22:20:46 +04:00
|
|
|
$this->assertTrue($rootView->file_exists('/anotherfolder/foo.txt'));
|
|
|
|
$this->assertTrue($rootView->file_exists('/anotherfolder/foo.png'));
|
|
|
|
$this->assertTrue($rootView->file_exists('/anotherfolder/folder/bar.txt'));
|
|
|
|
$this->assertTrue($rootView->file_exists('/substorage/foo.txt'));
|
|
|
|
$this->assertTrue($rootView->file_exists('/substorage/foo.png'));
|
|
|
|
$this->assertTrue($rootView->file_exists('/substorage/folder/bar.txt'));
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-05-19 22:20:46 +04:00
|
|
|
function testMoveBetweenStorages() {
|
|
|
|
$storage1 = $this->getTestStorage();
|
|
|
|
$storage2 = $this->getTestStorage();
|
|
|
|
\OC\Files\Filesystem::mount($storage1, array(), '/');
|
|
|
|
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');
|
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
$rootView->rename('foo.txt', 'substorage/folder/foo.txt');
|
|
|
|
$this->assertFalse($rootView->file_exists('foo.txt'));
|
|
|
|
$this->assertTrue($rootView->file_exists('substorage/folder/foo.txt'));
|
|
|
|
$rootView->rename('substorage/folder', 'anotherfolder');
|
|
|
|
$this->assertFalse($rootView->is_dir('substorage/folder'));
|
|
|
|
$this->assertTrue($rootView->file_exists('anotherfolder/foo.txt'));
|
|
|
|
$this->assertTrue($rootView->file_exists('anotherfolder/bar.txt'));
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-02-10 15:44:27 +04:00
|
|
|
function testTouch() {
|
|
|
|
$storage = $this->getTestStorage(true, '\Test\Files\TemporaryNoTouch');
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::mount($storage, array(), '/');
|
|
|
|
|
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
$oldCachedData = $rootView->getFileInfo('foo.txt');
|
|
|
|
|
|
|
|
$rootView->touch('foo.txt', 500);
|
|
|
|
|
|
|
|
$cachedData = $rootView->getFileInfo('foo.txt');
|
|
|
|
$this->assertEquals(500, $cachedData['mtime']);
|
|
|
|
$this->assertEquals($oldCachedData['storage_mtime'], $cachedData['storage_mtime']);
|
|
|
|
|
|
|
|
$rootView->putFileInfo('foo.txt', array('storage_mtime' => 1000)); //make sure the watcher detects the change
|
|
|
|
$rootView->file_put_contents('foo.txt', 'asd');
|
|
|
|
$cachedData = $rootView->getFileInfo('foo.txt');
|
|
|
|
$this->assertGreaterThanOrEqual($cachedData['mtime'], $oldCachedData['mtime']);
|
|
|
|
$this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']);
|
|
|
|
}
|
|
|
|
|
2012-10-21 04:12:58 +04:00
|
|
|
/**
|
2012-10-22 00:05:29 +04:00
|
|
|
* @param bool $scan
|
2012-10-26 14:30:25 +04:00
|
|
|
* @return \OC\Files\Storage\Storage
|
2012-10-21 04:12:58 +04:00
|
|
|
*/
|
2013-02-10 15:44:27 +04:00
|
|
|
private function getTestStorage($scan = true, $class = '\OC\Files\Storage\Temporary') {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
|
|
|
$storage = new $class(array());
|
2012-10-21 04:12:58 +04:00
|
|
|
$textData = "dummy file data\n";
|
|
|
|
$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
|
|
|
|
$storage->mkdir('folder');
|
|
|
|
$storage->file_put_contents('foo.txt', $textData);
|
|
|
|
$storage->file_put_contents('foo.png', $imgData);
|
|
|
|
$storage->file_put_contents('folder/bar.txt', $textData);
|
|
|
|
|
2012-10-22 00:05:29 +04:00
|
|
|
if ($scan) {
|
|
|
|
$scanner = $storage->getScanner();
|
|
|
|
$scanner->scan('');
|
|
|
|
}
|
2012-10-21 04:12:58 +04:00
|
|
|
$this->storages[] = $storage;
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
}
|