1
0

ExpirationTest.php 7.6 KB

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