You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExpirationTest.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. use OCA\Files_Trashbin\Expiration;
  25. use \OCA\Files_Trashbin\Tests;
  26. use OCP\IConfig;
  27. class ExpirationTest extends \Test\TestCase {
  28. const SECONDS_PER_DAY = 86400; //60*60*24
  29. const FAKE_TIME_NOW = 1000000;
  30. public function expirationData(){
  31. $today = 100*self::SECONDS_PER_DAY;
  32. $back10Days = (100-10)*self::SECONDS_PER_DAY;
  33. $back20Days = (100-20)*self::SECONDS_PER_DAY;
  34. $back30Days = (100-30)*self::SECONDS_PER_DAY;
  35. $back35Days = (100-35)*self::SECONDS_PER_DAY;
  36. // it should never happen, but who knows :/
  37. $ahead100Days = (100+100)*self::SECONDS_PER_DAY;
  38. return [
  39. // Expiration is disabled - always should return false
  40. [ 'disabled', $today, $back10Days, false, false],
  41. [ 'disabled', $today, $back10Days, true, false],
  42. [ 'disabled', $today, $ahead100Days, true, false],
  43. // Default: expire in 30 days or earlier when quota requirements are met
  44. [ 'auto', $today, $back10Days, false, false],
  45. [ 'auto', $today, $back35Days, false, false],
  46. [ 'auto', $today, $back10Days, true, true],
  47. [ 'auto', $today, $back35Days, true, true],
  48. [ 'auto', $today, $ahead100Days, true, true],
  49. // The same with 'auto'
  50. [ 'auto, auto', $today, $back10Days, false, false],
  51. [ 'auto, auto', $today, $back35Days, false, false],
  52. [ 'auto, auto', $today, $back10Days, true, true],
  53. [ 'auto, auto', $today, $back35Days, true, true],
  54. // Keep for 15 days but expire anytime if space needed
  55. [ '15, auto', $today, $back10Days, false, false],
  56. [ '15, auto', $today, $back20Days, false, false],
  57. [ '15, auto', $today, $back10Days, true, true],
  58. [ '15, auto', $today, $back20Days, true, true],
  59. [ '15, auto', $today, $ahead100Days, true, true],
  60. // Expire anytime if space needed, Expire all older than max
  61. [ 'auto, 15', $today, $back10Days, false, false],
  62. [ 'auto, 15', $today, $back20Days, false, true],
  63. [ 'auto, 15', $today, $back10Days, true, true],
  64. [ 'auto, 15', $today, $back20Days, true, true],
  65. [ 'auto, 15', $today, $ahead100Days, true, true],
  66. // Expire all older than max OR older than min if space needed
  67. [ '15, 25', $today, $back10Days, false, false],
  68. [ '15, 25', $today, $back20Days, false, false],
  69. [ '15, 25', $today, $back30Days, false, true],
  70. [ '15, 25', $today, $back10Days, false, false],
  71. [ '15, 25', $today, $back20Days, true, true],
  72. [ '15, 25', $today, $back30Days, true, true],
  73. [ '15, 25', $today, $ahead100Days, true, false],
  74. // Expire all older than max OR older than min if space needed
  75. // Max<Min case
  76. [ '25, 15', $today, $back10Days, false, false],
  77. [ '25, 15', $today, $back20Days, false, false],
  78. [ '25, 15', $today, $back30Days, false, true],
  79. [ '25, 15', $today, $back10Days, false, false],
  80. [ '25, 15', $today, $back20Days, true, false],
  81. [ '25, 15', $today, $back30Days, true, true],
  82. [ '25, 15', $today, $ahead100Days, true, false],
  83. ];
  84. }
  85. /**
  86. * @dataProvider expirationData
  87. *
  88. * @param string $retentionObligation
  89. * @param int $timeNow
  90. * @param int $timestamp
  91. * @param bool $quotaExceeded
  92. * @param string $expectedResult
  93. */
  94. public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult){
  95. $mockedConfig = $this->getMockedConfig($retentionObligation);
  96. $mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
  97. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  98. $actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
  99. $this->assertEquals($expectedResult, $actualResult);
  100. }
  101. public function configData(){
  102. return [
  103. [ 'disabled', null, null, null],
  104. [ 'auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  105. [ 'auto,auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  106. [ 'auto, auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  107. [ 'auto, 3', Expiration::NO_OBLIGATION, 3, true ],
  108. [ '5, auto', 5, Expiration::NO_OBLIGATION, true ],
  109. [ '3, 5', 3, 5, false ],
  110. [ '10, 3', 10, 10, false ],
  111. ];
  112. }
  113. /**
  114. * @dataProvider configData
  115. *
  116. * @param string $configValue
  117. * @param int $expectedMinAge
  118. * @param int $expectedMaxAge
  119. * @param bool $expectedCanPurgeToSaveSpace
  120. */
  121. public function testParseRetentionObligation($configValue, $expectedMinAge, $expectedMaxAge, $expectedCanPurgeToSaveSpace){
  122. $mockedConfig = $this->getMockedConfig($configValue);
  123. $mockedTimeFactory = $this->getMockedTimeFactory(
  124. time()
  125. );
  126. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  127. $this->assertAttributeEquals($expectedMinAge, 'minAge', $expiration);
  128. $this->assertAttributeEquals($expectedMaxAge, 'maxAge', $expiration);
  129. $this->assertAttributeEquals($expectedCanPurgeToSaveSpace, 'canPurgeToSaveSpace', $expiration);
  130. }
  131. public function timestampTestData(){
  132. return [
  133. [ 'disabled', false],
  134. [ 'auto', false ],
  135. [ 'auto,auto', false ],
  136. [ 'auto, auto', false ],
  137. [ 'auto, 3', self::FAKE_TIME_NOW - (3*self::SECONDS_PER_DAY) ],
  138. [ '5, auto', false ],
  139. [ '3, 5', self::FAKE_TIME_NOW - (5*self::SECONDS_PER_DAY) ],
  140. [ '10, 3', self::FAKE_TIME_NOW - (10*self::SECONDS_PER_DAY) ],
  141. ];
  142. }
  143. /**
  144. * @dataProvider timestampTestData
  145. *
  146. * @param string $configValue
  147. * @param int $expectedMaxAgeTimestamp
  148. */
  149. public function testGetMaxAgeAsTimestamp($configValue, $expectedMaxAgeTimestamp){
  150. $mockedConfig = $this->getMockedConfig($configValue);
  151. $mockedTimeFactory = $this->getMockedTimeFactory(
  152. self::FAKE_TIME_NOW
  153. );
  154. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  155. $actualTimestamp = $expiration->getMaxAgeAsTimestamp();
  156. $this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp);
  157. }
  158. /**
  159. *
  160. * @param int $time
  161. * @return \OCP\AppFramework\Utility\ITimeFactory
  162. */
  163. private function getMockedTimeFactory($time){
  164. $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory')
  165. ->disableOriginalConstructor()
  166. ->setMethods(['getTime'])
  167. ->getMock()
  168. ;
  169. $mockedTimeFactory->expects($this->any())->method('getTime')->will(
  170. $this->returnValue($time)
  171. );
  172. return $mockedTimeFactory;
  173. }
  174. /**
  175. *
  176. * @param string $returnValue
  177. * @return IConfig
  178. */
  179. private function getMockedConfig($returnValue){
  180. $mockedConfig = $this->getMockBuilder(IConfig::class)
  181. ->disableOriginalConstructor()
  182. ->setMethods(
  183. [
  184. 'setSystemValues',
  185. 'setSystemValue',
  186. 'getSystemValue',
  187. 'getFilteredSystemValue',
  188. 'deleteSystemValue',
  189. 'getAppKeys',
  190. 'setAppValue',
  191. 'getAppValue',
  192. 'deleteAppValue',
  193. 'deleteAppValues',
  194. 'setUserValue',
  195. 'getUserValue',
  196. 'getUserValueForUsers',
  197. 'getUserKeys',
  198. 'deleteUserValue',
  199. 'deleteAllUserValues',
  200. 'deleteAppFromAllUsers',
  201. 'getUsersForUserValue'
  202. ]
  203. )
  204. ->getMock()
  205. ;
  206. $mockedConfig->expects($this->any())->method('getSystemValue')->will(
  207. $this->returnValue($returnValue)
  208. );
  209. return $mockedConfig;
  210. }
  211. }