expiration.php 7.6 KB

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