1
0

HelperStorageTest.php 7.3 KB

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