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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. namespace OCA\Files_Versions\Tests;
  25. use \OCA\Files_Versions\Expiration;
  26. use OCP\IConfig;
  27. class ExpirationTest extends \Test\TestCase {
  28. const SECONDS_PER_DAY = 86400; //60*60*24
  29. public function expirationData(){
  30. $today = 100*self::SECONDS_PER_DAY;
  31. $back10Days = (100-10)*self::SECONDS_PER_DAY;
  32. $back20Days = (100-20)*self::SECONDS_PER_DAY;
  33. $back30Days = (100-30)*self::SECONDS_PER_DAY;
  34. $back35Days = (100-35)*self::SECONDS_PER_DAY;
  35. // it should never happen, but who knows :/
  36. $ahead100Days = (100+100)*self::SECONDS_PER_DAY;
  37. return [
  38. // Expiration is disabled - always should return false
  39. [ 'disabled', $today, $back10Days, false, false],
  40. [ 'disabled', $today, $back10Days, true, false],
  41. [ 'disabled', $today, $ahead100Days, true, false],
  42. // Default: expire in 30 days or earlier when quota requirements are met
  43. [ 'auto', $today, $back10Days, false, false],
  44. [ 'auto', $today, $back35Days, false, false],
  45. [ 'auto', $today, $back10Days, true, true],
  46. [ 'auto', $today, $back35Days, true, true],
  47. [ 'auto', $today, $ahead100Days, true, true],
  48. // The same with 'auto'
  49. [ 'auto, auto', $today, $back10Days, false, false],
  50. [ 'auto, auto', $today, $back35Days, false, false],
  51. [ 'auto, auto', $today, $back10Days, true, true],
  52. [ 'auto, auto', $today, $back35Days, true, true],
  53. // Keep for 15 days but expire anytime if space needed
  54. [ '15, auto', $today, $back10Days, false, false],
  55. [ '15, auto', $today, $back20Days, false, false],
  56. [ '15, auto', $today, $back10Days, true, true],
  57. [ '15, auto', $today, $back20Days, true, true],
  58. [ '15, auto', $today, $ahead100Days, true, true],
  59. // Expire anytime if space needed, Expire all older than max
  60. [ 'auto, 15', $today, $back10Days, false, false],
  61. [ 'auto, 15', $today, $back20Days, false, true],
  62. [ 'auto, 15', $today, $back10Days, true, true],
  63. [ 'auto, 15', $today, $back20Days, true, true],
  64. [ 'auto, 15', $today, $ahead100Days, true, true],
  65. // Expire all older than max OR older than min if space needed
  66. [ '15, 25', $today, $back10Days, false, false],
  67. [ '15, 25', $today, $back20Days, false, false],
  68. [ '15, 25', $today, $back30Days, false, true],
  69. [ '15, 25', $today, $back10Days, false, false],
  70. [ '15, 25', $today, $back20Days, true, true],
  71. [ '15, 25', $today, $back30Days, true, true],
  72. [ '15, 25', $today, $ahead100Days, true, false],
  73. // Expire all older than max OR older than min if space needed
  74. // Max<Min case
  75. [ '25, 15', $today, $back10Days, false, false],
  76. [ '25, 15', $today, $back20Days, false, false],
  77. [ '25, 15', $today, $back30Days, false, true],
  78. [ '25, 15', $today, $back10Days, false, false],
  79. [ '25, 15', $today, $back20Days, true, false],
  80. [ '25, 15', $today, $back30Days, true, true],
  81. [ '25, 15', $today, $ahead100Days, true, false],
  82. ];
  83. }
  84. /**
  85. * @dataProvider expirationData
  86. *
  87. * @param string $retentionObligation
  88. * @param int $timeNow
  89. * @param int $timestamp
  90. * @param bool $quotaExceeded
  91. * @param string $expectedResult
  92. */
  93. public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult){
  94. $mockedConfig = $this->getMockedConfig($retentionObligation);
  95. $mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
  96. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  97. $actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
  98. $this->assertEquals($expectedResult, $actualResult);
  99. }
  100. public function configData(){
  101. return [
  102. [ 'disabled', null, null, null],
  103. [ 'auto', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  104. [ 'auto,auto', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  105. [ 'auto, auto', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  106. [ 'auto, 3', Expiration::NO_OBLIGATION, 3, true ],
  107. [ '5, auto', 5, Expiration::NO_OBLIGATION, true ],
  108. [ '3, 5', 3, 5, false ],
  109. [ '10, 3', 10, 10, false ],
  110. [ 'g,a,r,b,a,g,e', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  111. [ '-3,8', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ]
  112. ];
  113. }
  114. /**
  115. * @dataProvider configData
  116. *
  117. * @param string $configValue
  118. * @param int $expectedMinAge
  119. * @param int $expectedMaxAge
  120. * @param bool $expectedCanPurgeToSaveSpace
  121. */
  122. public function testParseRetentionObligation($configValue, $expectedMinAge, $expectedMaxAge, $expectedCanPurgeToSaveSpace){
  123. $mockedConfig = $this->getMockedConfig($configValue);
  124. $mockedTimeFactory = $this->getMockedTimeFactory(
  125. time()
  126. );
  127. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  128. $this->assertAttributeEquals($expectedMinAge, 'minAge', $expiration);
  129. $this->assertAttributeEquals($expectedMaxAge, 'maxAge', $expiration);
  130. $this->assertAttributeEquals($expectedCanPurgeToSaveSpace, 'canPurgeToSaveSpace', $expiration);
  131. }
  132. /**
  133. *
  134. * @param int $time
  135. * @return \OCP\AppFramework\Utility\ITimeFactory
  136. */
  137. private function getMockedTimeFactory($time){
  138. $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory')
  139. ->disableOriginalConstructor()
  140. ->setMethods(['getTime'])
  141. ->getMock()
  142. ;
  143. $mockedTimeFactory->expects($this->any())->method('getTime')->will(
  144. $this->returnValue($time)
  145. );
  146. return $mockedTimeFactory;
  147. }
  148. /**
  149. *
  150. * @param string $returnValue
  151. * @return \OCP\IConfig
  152. */
  153. private function getMockedConfig($returnValue){
  154. $mockedConfig = $this->getMockBuilder(IConfig::class)
  155. ->disableOriginalConstructor()
  156. ->setMethods(
  157. [
  158. 'setSystemValues',
  159. 'setSystemValue',
  160. 'getSystemValue',
  161. 'getFilteredSystemValue',
  162. 'deleteSystemValue',
  163. 'getAppKeys',
  164. 'setAppValue',
  165. 'getAppValue',
  166. 'deleteAppValue',
  167. 'deleteAppValues',
  168. 'setUserValue',
  169. 'getUserValue',
  170. 'getUserValueForUsers',
  171. 'getUserKeys',
  172. 'deleteUserValue',
  173. 'deleteAllUserValues',
  174. 'deleteAppFromAllUsers',
  175. 'getUsersForUserValue'
  176. ]
  177. )
  178. ->getMock()
  179. ;
  180. $mockedConfig->expects($this->any())->method('getSystemValue')->will(
  181. $this->returnValue($returnValue)
  182. );
  183. return $mockedConfig;
  184. }
  185. }