You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HelperStorageTest.php 7.8KB

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