1
0

HelperStorageTest.php 7.5 KB

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