Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HelperStorageTest.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /**
  11. * Test the storage functions of OC_Helper
  12. *
  13. * @group DB
  14. */
  15. class HelperStorageTest extends \Test\TestCase {
  16. /** @var string */
  17. private $user;
  18. /** @var \OC\Files\Storage\Storage */
  19. private $storageMock;
  20. /** @var \OC\Files\Storage\Storage */
  21. private $storage;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->user = $this->getUniqueID('user_');
  25. \OC_User::useBackend('dummy');
  26. \OC::$server->getUserManager()->createUser($this->user, $this->user);
  27. $this->storage = \OC\Files\Filesystem::getStorage('/');
  28. \OC\Files\Filesystem::tearDown();
  29. \OC_User::setUserId($this->user);
  30. \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');
  31. \OC\Files\Filesystem::clearMounts();
  32. $this->storageMock = null;
  33. }
  34. protected function tearDown(): void {
  35. $this->user = null;
  36. if ($this->storageMock) {
  37. $this->storageMock->getCache()->clear();
  38. $this->storageMock = null;
  39. }
  40. \OC\Files\Filesystem::tearDown();
  41. \OC\Files\Filesystem::mount($this->storage, [], '/');
  42. \OC_User::setUserId('');
  43. $user = \OC::$server->getUserManager()->get($this->user);
  44. if ($user !== null) {
  45. $user->delete();
  46. }
  47. \OC::$server->getConfig()->deleteAllUserValues($this->user);
  48. parent::tearDown();
  49. }
  50. /**
  51. * Returns a storage mock that returns the given value as
  52. * free space
  53. *
  54. * @param int $freeSpace free space value
  55. * @return \OC\Files\Storage\Storage
  56. */
  57. private function getStorageMock($freeSpace = 12) {
  58. $this->storageMock = $this->getMockBuilder(Temporary::class)
  59. ->setMethods(['free_space'])
  60. ->setConstructorArgs([''])
  61. ->getMock();
  62. $this->storageMock->expects($this->once())
  63. ->method('free_space')
  64. ->willReturn(12);
  65. return $this->storageMock;
  66. }
  67. /**
  68. * Test getting the storage info
  69. */
  70. public function testGetStorageInfo() {
  71. $homeStorage = $this->getStorageMock(12);
  72. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  73. $homeStorage->file_put_contents('test.txt', '01234');
  74. $storageInfo = \OC_Helper::getStorageInfo('');
  75. $this->assertEquals(12, $storageInfo['free']);
  76. $this->assertEquals(5, $storageInfo['used']);
  77. $this->assertEquals(17, $storageInfo['total']);
  78. }
  79. /**
  80. * Test getting the storage info, ignoring extra mount points
  81. */
  82. public function testGetStorageInfoExcludingExtStorage() {
  83. $homeStorage = $this->getStorageMock(12);
  84. \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files');
  85. $homeStorage->file_put_contents('test.txt', '01234');
  86. $extStorage = new \OC\Files\Storage\Temporary([]);
  87. $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
  88. $extStorage->getScanner()->scan(''); // update root size
  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. }