2014-03-10 18:19:18 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
namespace Test;
|
2016-09-12 22:33:38 +03:00
|
|
|
use OC\Files\Storage\Temporary;
|
2016-05-19 10:38:52 +03:00
|
|
|
|
2014-03-10 18:19:18 +04:00
|
|
|
/**
|
|
|
|
* Test the storage functions of OC_Helper
|
2015-11-20 13:27:11 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
2014-03-10 18:19:18 +04:00
|
|
|
*/
|
2016-05-19 10:38:52 +03:00
|
|
|
class HelperStorageTest extends \Test\TestCase {
|
2014-11-12 17:54:41 +03:00
|
|
|
/** @var string */
|
2014-03-10 18:19:18 +04:00
|
|
|
private $user;
|
2014-11-12 17:54:41 +03:00
|
|
|
/** @var \OC\Files\Storage\Storage */
|
2014-03-10 18:19:18 +04:00
|
|
|
private $storageMock;
|
2014-11-12 17:54:41 +03:00
|
|
|
/** @var \OC\Files\Storage\Storage */
|
|
|
|
private $storage;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-12 17:54:41 +03:00
|
|
|
parent::setUp();
|
2014-03-10 18:19:18 +04:00
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
$this->user = $this->getUniqueID('user_');
|
2019-11-18 13:54:26 +03:00
|
|
|
\OC_User::useBackend('dummy');
|
2015-12-17 17:10:11 +03:00
|
|
|
\OC::$server->getUserManager()->createUser($this->user, $this->user);
|
2014-03-25 19:37:46 +04:00
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
$this->storage = \OC\Files\Filesystem::getStorage('/');
|
2014-03-10 18:19:18 +04:00
|
|
|
\OC\Files\Filesystem::tearDown();
|
2014-03-25 19:37:46 +04:00
|
|
|
\OC_User::setUserId($this->user);
|
2014-03-10 18:19:18 +04:00
|
|
|
\OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');
|
2014-03-25 19:37:46 +04:00
|
|
|
\OC\Files\Filesystem::clearMounts();
|
|
|
|
|
|
|
|
$this->storageMock = null;
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2014-03-25 19:37:46 +04:00
|
|
|
$this->user = null;
|
2014-03-10 18:19:18 +04:00
|
|
|
|
2014-03-25 19:37:46 +04:00
|
|
|
if ($this->storageMock) {
|
|
|
|
$this->storageMock->getCache()->clear();
|
|
|
|
$this->storageMock = null;
|
|
|
|
}
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($this->storage, [], '/');
|
2014-03-25 19:37:46 +04:00
|
|
|
|
|
|
|
\OC_User::setUserId('');
|
2015-12-17 17:59:23 +03:00
|
|
|
$user = \OC::$server->getUserManager()->get($this->user);
|
|
|
|
if ($user !== null) { $user->delete(); }
|
2014-12-04 18:48:07 +03:00
|
|
|
\OC::$server->getConfig()->deleteAllUserValues($this->user);
|
2014-11-12 17:54:41 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
2014-03-25 19:37:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a storage mock that returns the given value as
|
|
|
|
* free space
|
|
|
|
*
|
|
|
|
* @param int $freeSpace free space value
|
|
|
|
* @return \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
private function getStorageMock($freeSpace = 12) {
|
2016-09-12 22:33:38 +03:00
|
|
|
$this->storageMock = $this->getMockBuilder(Temporary::class)
|
|
|
|
->setMethods(['free_space'])
|
|
|
|
->setConstructorArgs([''])
|
|
|
|
->getMock();
|
2014-03-10 18:19:18 +04:00
|
|
|
|
|
|
|
$this->storageMock->expects($this->once())
|
|
|
|
->method('free_space')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(12);
|
2014-03-25 19:37:46 +04:00
|
|
|
return $this->storageMock;
|
2014-03-10 18:19:18 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 19:37:46 +04:00
|
|
|
/**
|
|
|
|
* Test getting the storage info
|
|
|
|
*/
|
|
|
|
function testGetStorageInfo() {
|
|
|
|
$homeStorage = $this->getStorageMock(12);
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage->file_put_contents('test.txt', '01234');
|
2014-03-10 18:19:18 +04:00
|
|
|
|
2014-03-25 19:37:46 +04:00
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
|
|
|
$this->assertEquals(12, $storageInfo['free']);
|
|
|
|
$this->assertEquals(5, $storageInfo['used']);
|
|
|
|
$this->assertEquals(17, $storageInfo['total']);
|
2014-03-10 18:19:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-25 19:37:46 +04:00
|
|
|
* Test getting the storage info, ignoring extra mount points
|
2014-03-10 18:19:18 +04:00
|
|
|
*/
|
2014-03-25 19:37:46 +04:00
|
|
|
function testGetStorageInfoExcludingExtStorage() {
|
|
|
|
$homeStorage = $this->getStorageMock(12);
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage->file_put_contents('test.txt', '01234');
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$extStorage = new \OC\Files\Storage\Temporary([]);
|
2014-03-25 19:37:46 +04:00
|
|
|
$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
|
|
|
|
$extStorage->getScanner()->scan(''); // update root size
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
|
2014-03-10 18:19:18 +04:00
|
|
|
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
|
|
|
$this->assertEquals(12, $storageInfo['free']);
|
|
|
|
$this->assertEquals(5, $storageInfo['used']);
|
|
|
|
$this->assertEquals(17, $storageInfo['total']);
|
|
|
|
}
|
|
|
|
|
2014-03-25 19:37:46 +04:00
|
|
|
/**
|
|
|
|
* Test getting the storage info, including extra mount points
|
|
|
|
*/
|
|
|
|
function testGetStorageInfoIncludingExtStorage() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$homeStorage = new \OC\Files\Storage\Temporary([]);
|
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage->file_put_contents('test.txt', '01234');
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$extStorage = new \OC\Files\Storage\Temporary([]);
|
2014-03-25 19:37:46 +04:00
|
|
|
$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
|
|
|
|
$extStorage->getScanner()->scan(''); // update root size
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
|
2014-03-25 19:37:46 +04:00
|
|
|
|
|
|
|
$config = \OC::$server->getConfig();
|
2015-12-02 18:11:38 +03:00
|
|
|
$oldConfig = $config->getSystemValue('quota_include_external_storage', false);
|
|
|
|
$config->setSystemValue('quota_include_external_storage', 'true');
|
|
|
|
|
|
|
|
$config->setUserValue($this->user, 'files', 'quota', '25');
|
2014-03-25 19:37:46 +04:00
|
|
|
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
|
|
|
$this->assertEquals(3, $storageInfo['free']);
|
|
|
|
$this->assertEquals(22, $storageInfo['used']);
|
|
|
|
$this->assertEquals(25, $storageInfo['total']);
|
|
|
|
|
2015-12-02 18:11:38 +03:00
|
|
|
$config->setSystemValue('quota_include_external_storage', $oldConfig);
|
|
|
|
$config->setUserValue($this->user, 'files', 'quota', 'default');
|
2014-03-25 19:37:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting the storage info excluding extra mount points
|
|
|
|
* when user has no quota set, even when quota ext storage option
|
|
|
|
* was set
|
|
|
|
*/
|
|
|
|
function testGetStorageInfoIncludingExtStorageWithNoUserQuota() {
|
|
|
|
$homeStorage = $this->getStorageMock(12);
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage->file_put_contents('test.txt', '01234');
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$extStorage = new \OC\Files\Storage\Temporary([]);
|
2014-03-25 19:37:46 +04:00
|
|
|
$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
|
|
|
|
$extStorage->getScanner()->scan(''); // update root size
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
|
2014-03-25 19:37:46 +04:00
|
|
|
|
2015-12-02 18:11:38 +03:00
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
$oldConfig = $config->getSystemValue('quota_include_external_storage', false);
|
|
|
|
$config->setSystemValue('quota_include_external_storage', 'true');
|
2014-03-25 19:37:46 +04:00
|
|
|
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
2017-05-11 19:35:48 +03:00
|
|
|
$this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage');
|
|
|
|
$this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used');
|
|
|
|
$this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage');
|
2014-03-25 19:37:46 +04:00
|
|
|
|
2015-12-02 18:11:38 +03:00
|
|
|
$config->setSystemValue('quota_include_external_storage', $oldConfig);
|
2014-03-25 19:37:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 18:19:18 +04:00
|
|
|
/**
|
|
|
|
* Test getting the storage info with quota enabled
|
|
|
|
*/
|
|
|
|
function testGetStorageInfoWithQuota() {
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage = $this->getStorageMock(12);
|
|
|
|
$homeStorage->file_put_contents('test.txt', '01234');
|
|
|
|
$homeStorage = new \OC\Files\Storage\Wrapper\Quota(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-03-25 19:37:46 +04:00
|
|
|
'storage' => $homeStorage,
|
2014-03-10 18:19:18 +04:00
|
|
|
'quota' => 7
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
2014-03-10 18:19:18 +04:00
|
|
|
);
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-10 18:19:18 +04:00
|
|
|
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
|
|
|
$this->assertEquals(2, $storageInfo['free']);
|
|
|
|
$this->assertEquals(5, $storageInfo['used']);
|
|
|
|
$this->assertEquals(7, $storageInfo['total']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting the storage info when data exceeds quota
|
|
|
|
*/
|
|
|
|
function testGetStorageInfoWhenSizeExceedsQuota() {
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage = $this->getStorageMock(12);
|
|
|
|
$homeStorage->file_put_contents('test.txt', '0123456789');
|
|
|
|
$homeStorage = new \OC\Files\Storage\Wrapper\Quota(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-03-25 19:37:46 +04:00
|
|
|
'storage' => $homeStorage,
|
2014-03-10 18:19:18 +04:00
|
|
|
'quota' => 7
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
2014-03-10 18:19:18 +04:00
|
|
|
);
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-10 18:19:18 +04:00
|
|
|
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
|
|
|
$this->assertEquals(0, $storageInfo['free']);
|
|
|
|
$this->assertEquals(10, $storageInfo['used']);
|
|
|
|
// total = quota
|
|
|
|
$this->assertEquals(7, $storageInfo['total']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting the storage info when the remaining
|
|
|
|
* free storage space is less than the quota
|
|
|
|
*/
|
|
|
|
function testGetStorageInfoWhenFreeSpaceLessThanQuota() {
|
2014-03-25 19:37:46 +04:00
|
|
|
$homeStorage = $this->getStorageMock(12);
|
|
|
|
$homeStorage->file_put_contents('test.txt', '01234');
|
|
|
|
$homeStorage = new \OC\Files\Storage\Wrapper\Quota(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-03-25 19:37:46 +04:00
|
|
|
'storage' => $homeStorage,
|
2014-03-10 18:19:18 +04:00
|
|
|
'quota' => 18
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
2014-03-10 18:19:18 +04:00
|
|
|
);
|
2020-03-26 11:30:18 +03:00
|
|
|
\OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
|
2014-03-10 18:19:18 +04:00
|
|
|
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo('');
|
|
|
|
$this->assertEquals(12, $storageInfo['free']);
|
|
|
|
$this->assertEquals(5, $storageInfo['used']);
|
|
|
|
// total = free + used (because quota > total)
|
|
|
|
$this->assertEquals(17, $storageInfo['total']);
|
|
|
|
}
|
|
|
|
}
|