helperstorage.php 7.1 KB

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