helperstorage.php 7.2 KB

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