1
0

HelperStorageTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\Files\Storage\Temporary;
  9. use OCP\Files\Mount\IMountManager;
  10. use OCP\IConfig;
  11. use Test\Traits\UserTrait;
  12. /**
  13. * Test the storage functions of OC_Helper
  14. *
  15. * @group DB
  16. */
  17. class HelperStorageTest extends \Test\TestCase {
  18. use UserTrait;
  19. /** @var string */
  20. private $user;
  21. /** @var \OC\Files\Storage\Storage */
  22. private $storageMock;
  23. /** @var \OC\Files\Storage\Storage */
  24. private $storage;
  25. private bool $savedQuotaIncludeExternalStorage;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->user = $this->getUniqueID('user_');
  29. $this->createUser($this->user, $this->user);
  30. $this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage();
  31. \OC\Files\Filesystem::tearDown();
  32. \OC_User::setUserId($this->user);
  33. \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');
  34. /** @var IMountManager $manager */
  35. $manager = \OC::$server->get(IMountManager::class);
  36. $manager->removeMount('/' . $this->user);
  37. $this->storageMock = null;
  38. }
  39. protected function tearDown(): void {
  40. $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage);
  41. $this->user = null;
  42. if ($this->storageMock) {
  43. $this->storageMock->getCache()->clear();
  44. $this->storageMock = null;
  45. }
  46. \OC\Files\Filesystem::tearDown();
  47. \OC_User::setUserId('');
  48. \OC::$server->getConfig()->deleteAllUserValues($this->user);
  49. parent::tearDown();
  50. }
  51. /**
  52. * Returns a storage mock that returns the given value as
  53. * free space
  54. *
  55. * @param int $freeSpace free space value
  56. * @return \OC\Files\Storage\Storage
  57. */
  58. private function getStorageMock($freeSpace = 12) {
  59. $this->storageMock = $this->getMockBuilder(Temporary::class)
  60. ->setMethods(['free_space'])
  61. ->setConstructorArgs([''])
  62. ->getMock();
  63. $this->storageMock->expects($this->once())
  64. ->method('free_space')
  65. ->willReturn(12);
  66. return $this->storageMock;
  67. }
  68. /**
  69. * Test getting the storage info
  70. */
  71. public function testGetStorageInfo() {
  72. $homeStorage = $this->getStorageMock(12);
  73. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  74. $homeStorage->file_put_contents('test.txt', '01234');
  75. $storageInfo = \OC_Helper::getStorageInfo('');
  76. $this->assertEquals(12, $storageInfo['free']);
  77. $this->assertEquals(5, $storageInfo['used']);
  78. $this->assertEquals(17, $storageInfo['total']);
  79. }
  80. private function getIncludeExternalStorage(): bool {
  81. $class = new \ReflectionClass(\OC_Helper::class);
  82. $prop = $class->getProperty('quotaIncludeExternalStorage');
  83. $prop->setAccessible(true);
  84. return $prop->getValue(null) ?? false;
  85. }
  86. private function setIncludeExternalStorage(bool $include) {
  87. $class = new \ReflectionClass(\OC_Helper::class);
  88. $prop = $class->getProperty('quotaIncludeExternalStorage');
  89. $prop->setAccessible(true);
  90. $prop->setValue(null, $include);
  91. }
  92. /**
  93. * Test getting the storage info, ignoring extra mount points
  94. */
  95. public function testGetStorageInfoExcludingExtStorage() {
  96. $homeStorage = $this->getStorageMock(12);
  97. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  98. $homeStorage->file_put_contents('test.txt', '01234');
  99. $extStorage = new \OC\Files\Storage\Temporary([]);
  100. $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
  101. $extStorage->getScanner()->scan(''); // update root size
  102. $this->setIncludeExternalStorage(false);
  103. \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
  104. $storageInfo = \OC_Helper::getStorageInfo('');
  105. $this->assertEquals(12, $storageInfo['free']);
  106. $this->assertEquals(5, $storageInfo['used']);
  107. $this->assertEquals(17, $storageInfo['total']);
  108. }
  109. /**
  110. * Test getting the storage info, including extra mount points
  111. */
  112. public function testGetStorageInfoIncludingExtStorage() {
  113. $homeStorage = new \OC\Files\Storage\Temporary([]);
  114. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  115. $homeStorage->file_put_contents('test.txt', '01234');
  116. $extStorage = new \OC\Files\Storage\Temporary([]);
  117. $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
  118. $extStorage->getScanner()->scan(''); // update root size
  119. \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
  120. $this->setIncludeExternalStorage(true);
  121. $config = \OC::$server->get(IConfig::class);
  122. $config->setUserValue($this->user, 'files', 'quota', '25');
  123. $storageInfo = \OC_Helper::getStorageInfo('');
  124. $this->assertEquals(3, $storageInfo['free']);
  125. $this->assertEquals(22, $storageInfo['used']);
  126. $this->assertEquals(25, $storageInfo['total']);
  127. $config->setUserValue($this->user, 'files', 'quota', 'default');
  128. }
  129. /**
  130. * Test getting the storage info excluding extra mount points
  131. * when user has no quota set, even when quota ext storage option
  132. * was set
  133. */
  134. public function testGetStorageInfoIncludingExtStorageWithNoUserQuota() {
  135. $homeStorage = $this->getStorageMock(12);
  136. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  137. $homeStorage->file_put_contents('test.txt', '01234');
  138. $extStorage = new \OC\Files\Storage\Temporary([]);
  139. $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
  140. $extStorage->getScanner()->scan(''); // update root size
  141. \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');
  142. $config = \OC::$server->getConfig();
  143. $this->setIncludeExternalStorage(true);
  144. $storageInfo = \OC_Helper::getStorageInfo('');
  145. $this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage');
  146. $this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used');
  147. $this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage');
  148. }
  149. /**
  150. * Test getting the storage info with quota enabled
  151. */
  152. public function testGetStorageInfoWithQuota() {
  153. $homeStorage = $this->getStorageMock(12);
  154. $homeStorage->file_put_contents('test.txt', '01234');
  155. $homeStorage = new \OC\Files\Storage\Wrapper\Quota(
  156. [
  157. 'storage' => $homeStorage,
  158. 'quota' => 7
  159. ]
  160. );
  161. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  162. $storageInfo = \OC_Helper::getStorageInfo('');
  163. $this->assertEquals(2, $storageInfo['free']);
  164. $this->assertEquals(5, $storageInfo['used']);
  165. $this->assertEquals(7, $storageInfo['total']);
  166. }
  167. /**
  168. * Test getting the storage info when data exceeds quota
  169. */
  170. public function testGetStorageInfoWhenSizeExceedsQuota() {
  171. $homeStorage = $this->getStorageMock(12);
  172. $homeStorage->file_put_contents('test.txt', '0123456789');
  173. $homeStorage = new \OC\Files\Storage\Wrapper\Quota(
  174. [
  175. 'storage' => $homeStorage,
  176. 'quota' => 7
  177. ]
  178. );
  179. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  180. $storageInfo = \OC_Helper::getStorageInfo('');
  181. $this->assertEquals(0, $storageInfo['free']);
  182. $this->assertEquals(10, $storageInfo['used']);
  183. // total = quota
  184. $this->assertEquals(7, $storageInfo['total']);
  185. }
  186. /**
  187. * Test getting the storage info when the remaining
  188. * free storage space is less than the quota
  189. */
  190. public function testGetStorageInfoWhenFreeSpaceLessThanQuota() {
  191. $homeStorage = $this->getStorageMock(12);
  192. $homeStorage->file_put_contents('test.txt', '01234');
  193. $homeStorage = new \OC\Files\Storage\Wrapper\Quota(
  194. [
  195. 'storage' => $homeStorage,
  196. 'quota' => 18
  197. ]
  198. );
  199. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  200. $storageInfo = \OC_Helper::getStorageInfo('');
  201. $this->assertEquals(12, $storageInfo['free']);
  202. $this->assertEquals(5, $storageInfo['used']);
  203. // total = free + used (because quota > total)
  204. $this->assertEquals(17, $storageInfo['total']);
  205. }
  206. }