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 6.8KB

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