diff options
Diffstat (limited to 'tests/lib/HelperStorageTest.php')
-rw-r--r-- | tests/lib/HelperStorageTest.php | 116 |
1 files changed, 66 insertions, 50 deletions
diff --git a/tests/lib/HelperStorageTest.php b/tests/lib/HelperStorageTest.php index d3f480502b2..140be04ec64 100644 --- a/tests/lib/HelperStorageTest.php +++ b/tests/lib/HelperStorageTest.php @@ -1,15 +1,20 @@ <?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. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test; +use OC\Files\Filesystem; +use OC\Files\Storage\Storage; use OC\Files\Storage\Temporary; +use OC\Files\Storage\Wrapper\Quota; use OCP\Files\Mount\IMountManager; +use OCP\IConfig; +use OCP\Server; use Test\Traits\UserTrait; /** @@ -22,39 +27,42 @@ class HelperStorageTest extends \Test\TestCase { /** @var string */ private $user; - /** @var \OC\Files\Storage\Storage */ + /** @var Storage */ private $storageMock; - /** @var \OC\Files\Storage\Storage */ + /** @var Storage */ private $storage; + private bool $savedQuotaIncludeExternalStorage; protected function setUp(): void { parent::setUp(); $this->user = $this->getUniqueID('user_'); $this->createUser($this->user, $this->user); + $this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage(); - \OC\Files\Filesystem::tearDown(); + Filesystem::tearDown(); \OC_User::setUserId($this->user); - \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files'); + Filesystem::init($this->user, '/' . $this->user . '/files'); /** @var IMountManager $manager */ - $manager = \OC::$server->get(IMountManager::class); + $manager = Server::get(IMountManager::class); $manager->removeMount('/' . $this->user); $this->storageMock = null; } protected function tearDown(): void { + $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage); $this->user = null; if ($this->storageMock) { $this->storageMock->getCache()->clear(); $this->storageMock = null; } - \OC\Files\Filesystem::tearDown(); + Filesystem::tearDown(); \OC_User::setUserId(''); - \OC::$server->getConfig()->deleteAllUserValues($this->user); + Server::get(IConfig::class)->deleteAllUserValues($this->user); parent::tearDown(); } @@ -64,26 +72,26 @@ class HelperStorageTest extends \Test\TestCase { * free space * * @param int $freeSpace free space value - * @return \OC\Files\Storage\Storage + * @return Storage */ private function getStorageMock($freeSpace = 12) { $this->storageMock = $this->getMockBuilder(Temporary::class) - ->setMethods(['free_space']) - ->setConstructorArgs(['']) + ->onlyMethods(['free_space']) + ->setConstructorArgs([[]]) ->getMock(); $this->storageMock->expects($this->once()) ->method('free_space') - ->willReturn(12); + ->willReturn($freeSpace); return $this->storageMock; } /** * Test getting the storage info */ - public function testGetStorageInfo() { + public function testGetStorageInfo(): void { $homeStorage = $this->getStorageMock(12); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); $storageInfo = \OC_Helper::getStorageInfo(''); @@ -92,22 +100,35 @@ class HelperStorageTest extends \Test\TestCase { $this->assertEquals(17, $storageInfo['total']); } + private function getIncludeExternalStorage(): bool { + $class = new \ReflectionClass(\OC_Helper::class); + $prop = $class->getProperty('quotaIncludeExternalStorage'); + $prop->setAccessible(true); + return $prop->getValue(null) ?? false; + } + + private function setIncludeExternalStorage(bool $include) { + $class = new \ReflectionClass(\OC_Helper::class); + $prop = $class->getProperty('quotaIncludeExternalStorage'); + $prop->setAccessible(true); + $prop->setValue(null, $include); + } + /** * Test getting the storage info, ignoring extra mount points */ - public function testGetStorageInfoExcludingExtStorage() { + public function testGetStorageInfoExcludingExtStorage(): void { $homeStorage = $this->getStorageMock(12); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); - $extStorage = new \OC\Files\Storage\Temporary([]); + $extStorage = new Temporary([]); $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size - $config = \OC::$server->getConfig(); - $config->setSystemValue('quota_include_external_storage', false); + $this->setIncludeExternalStorage(false); - \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); + Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free']); @@ -118,21 +139,20 @@ class HelperStorageTest extends \Test\TestCase { /** * Test getting the storage info, including extra mount points */ - public function testGetStorageInfoIncludingExtStorage() { - $homeStorage = new \OC\Files\Storage\Temporary([]); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + public function testGetStorageInfoIncludingExtStorage(): void { + $homeStorage = new Temporary([]); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); - $extStorage = new \OC\Files\Storage\Temporary([]); + $extStorage = new Temporary([]); $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size - \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); + Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); - $config = \OC::$server->getConfig(); - $oldConfig = $config->getSystemValue('quota_include_external_storage', false); - $config->setSystemValue('quota_include_external_storage', 'true'); + $this->setIncludeExternalStorage(true); + $config = Server::get(IConfig::class); $config->setUserValue($this->user, 'files', 'quota', '25'); $storageInfo = \OC_Helper::getStorageInfo(''); @@ -140,7 +160,6 @@ class HelperStorageTest extends \Test\TestCase { $this->assertEquals(22, $storageInfo['used']); $this->assertEquals(25, $storageInfo['total']); - $config->setSystemValue('quota_include_external_storage', $oldConfig); $config->setUserValue($this->user, 'files', 'quota', 'default'); } @@ -149,43 +168,40 @@ class HelperStorageTest extends \Test\TestCase { * when user has no quota set, even when quota ext storage option * was set */ - public function testGetStorageInfoIncludingExtStorageWithNoUserQuota() { + public function testGetStorageInfoIncludingExtStorageWithNoUserQuota(): void { $homeStorage = $this->getStorageMock(12); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $homeStorage->file_put_contents('test.txt', '01234'); - $extStorage = new \OC\Files\Storage\Temporary([]); + $extStorage = new Temporary([]); $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); $extStorage->getScanner()->scan(''); // update root size - \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); + Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); - $config = \OC::$server->getConfig(); - $oldConfig = $config->getSystemValue('quota_include_external_storage', false); - $config->setSystemValue('quota_include_external_storage', 'true'); + $config = Server::get(IConfig::class); + $this->setIncludeExternalStorage(true); $storageInfo = \OC_Helper::getStorageInfo(''); $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'); - - $config->setSystemValue('quota_include_external_storage', $oldConfig); } /** * Test getting the storage info with quota enabled */ - public function testGetStorageInfoWithQuota() { + public function testGetStorageInfoWithQuota(): void { $homeStorage = $this->getStorageMock(12); $homeStorage->file_put_contents('test.txt', '01234'); - $homeStorage = new \OC\Files\Storage\Wrapper\Quota( + $homeStorage = new Quota( [ 'storage' => $homeStorage, 'quota' => 7 ] ); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(2, $storageInfo['free']); @@ -196,16 +212,16 @@ class HelperStorageTest extends \Test\TestCase { /** * Test getting the storage info when data exceeds quota */ - public function testGetStorageInfoWhenSizeExceedsQuota() { + public function testGetStorageInfoWhenSizeExceedsQuota(): void { $homeStorage = $this->getStorageMock(12); $homeStorage->file_put_contents('test.txt', '0123456789'); - $homeStorage = new \OC\Files\Storage\Wrapper\Quota( + $homeStorage = new Quota( [ 'storage' => $homeStorage, 'quota' => 7 ] ); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(0, $storageInfo['free']); @@ -218,16 +234,16 @@ class HelperStorageTest extends \Test\TestCase { * Test getting the storage info when the remaining * free storage space is less than the quota */ - public function testGetStorageInfoWhenFreeSpaceLessThanQuota() { + public function testGetStorageInfoWhenFreeSpaceLessThanQuota(): void { $homeStorage = $this->getStorageMock(12); $homeStorage->file_put_contents('test.txt', '01234'); - $homeStorage = new \OC\Files\Storage\Wrapper\Quota( + $homeStorage = new Quota( [ 'storage' => $homeStorage, 'quota' => 18 ] ); - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); + Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); $storageInfo = \OC_Helper::getStorageInfo(''); $this->assertEquals(12, $storageInfo['free']); |