ExpirationTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\IConfig;
  27. use PHPUnit\Framework\MockObject\MockObject;
  28. class ExpirationTest extends \Test\TestCase {
  29. const SECONDS_PER_DAY = 86400; //60*60*24
  30. const FAKE_TIME_NOW = 1000000;
  31. public function expirationData(){
  32. $today = 100*self::SECONDS_PER_DAY;
  33. $back10Days = (100-10)*self::SECONDS_PER_DAY;
  34. $back20Days = (100-20)*self::SECONDS_PER_DAY;
  35. $back30Days = (100-30)*self::SECONDS_PER_DAY;
  36. $back35Days = (100-35)*self::SECONDS_PER_DAY;
  37. // it should never happen, but who knows :/
  38. $ahead100Days = (100+100)*self::SECONDS_PER_DAY;
  39. return [
  40. // Expiration is disabled - always should return false
  41. [ 'disabled', $today, $back10Days, false, false],
  42. [ 'disabled', $today, $back10Days, true, false],
  43. [ 'disabled', $today, $ahead100Days, true, false],
  44. // Default: expire in 30 days or earlier when quota requirements are met
  45. [ 'auto', $today, $back10Days, false, false],
  46. [ 'auto', $today, $back35Days, false, false],
  47. [ 'auto', $today, $back10Days, true, true],
  48. [ 'auto', $today, $back35Days, true, true],
  49. [ 'auto', $today, $ahead100Days, true, true],
  50. // The same with 'auto'
  51. [ 'auto, auto', $today, $back10Days, false, false],
  52. [ 'auto, auto', $today, $back35Days, false, false],
  53. [ 'auto, auto', $today, $back10Days, true, true],
  54. [ 'auto, auto', $today, $back35Days, true, true],
  55. // Keep for 15 days but expire anytime if space needed
  56. [ '15, auto', $today, $back10Days, false, false],
  57. [ '15, auto', $today, $back20Days, false, false],
  58. [ '15, auto', $today, $back10Days, true, true],
  59. [ '15, auto', $today, $back20Days, true, true],
  60. [ '15, auto', $today, $ahead100Days, true, true],
  61. // Expire anytime if space needed, Expire all older than max
  62. [ 'auto, 15', $today, $back10Days, false, false],
  63. [ 'auto, 15', $today, $back20Days, false, true],
  64. [ 'auto, 15', $today, $back10Days, true, true],
  65. [ 'auto, 15', $today, $back20Days, true, true],
  66. [ 'auto, 15', $today, $ahead100Days, true, true],
  67. // Expire all older than max OR older than min if space needed
  68. [ '15, 25', $today, $back10Days, false, false],
  69. [ '15, 25', $today, $back20Days, false, false],
  70. [ '15, 25', $today, $back30Days, false, true],
  71. [ '15, 25', $today, $back10Days, false, false],
  72. [ '15, 25', $today, $back20Days, true, true],
  73. [ '15, 25', $today, $back30Days, true, true],
  74. [ '15, 25', $today, $ahead100Days, true, false],
  75. // Expire all older than max OR older than min if space needed
  76. // Max<Min case
  77. [ '25, 15', $today, $back10Days, false, false],
  78. [ '25, 15', $today, $back20Days, false, false],
  79. [ '25, 15', $today, $back30Days, false, true],
  80. [ '25, 15', $today, $back10Days, false, false],
  81. [ '25, 15', $today, $back20Days, true, false],
  82. [ '25, 15', $today, $back30Days, true, true],
  83. [ '25, 15', $today, $ahead100Days, true, false],
  84. ];
  85. }
  86. /**
  87. * @dataProvider expirationData
  88. *
  89. * @param string $retentionObligation
  90. * @param int $timeNow
  91. * @param int $timestamp
  92. * @param bool $quotaExceeded
  93. * @param string $expectedResult
  94. */
  95. public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult){
  96. $mockedConfig = $this->getMockedConfig($retentionObligation);
  97. $mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
  98. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  99. $actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
  100. $this->assertEquals($expectedResult, $actualResult);
  101. }
  102. public function configData(){
  103. return [
  104. [ 'disabled', null, null, null],
  105. [ 'auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  106. [ 'auto,auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  107. [ 'auto, auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  108. [ 'auto, 3', Expiration::NO_OBLIGATION, 3, true ],
  109. [ '5, auto', 5, Expiration::NO_OBLIGATION, true ],
  110. [ '3, 5', 3, 5, false ],
  111. [ '10, 3', 10, 10, false ],
  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. public function timestampTestData(){
  133. return [
  134. [ 'disabled', false],
  135. [ 'auto', false ],
  136. [ 'auto,auto', false ],
  137. [ 'auto, auto', false ],
  138. [ 'auto, 3', self::FAKE_TIME_NOW - (3*self::SECONDS_PER_DAY) ],
  139. [ '5, auto', false ],
  140. [ '3, 5', self::FAKE_TIME_NOW - (5*self::SECONDS_PER_DAY) ],
  141. [ '10, 3', self::FAKE_TIME_NOW - (10*self::SECONDS_PER_DAY) ],
  142. ];
  143. }
  144. /**
  145. * @dataProvider timestampTestData
  146. *
  147. * @param string $configValue
  148. * @param int $expectedMaxAgeTimestamp
  149. */
  150. public function testGetMaxAgeAsTimestamp($configValue, $expectedMaxAgeTimestamp){
  151. $mockedConfig = $this->getMockedConfig($configValue);
  152. $mockedTimeFactory = $this->getMockedTimeFactory(
  153. self::FAKE_TIME_NOW
  154. );
  155. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  156. $actualTimestamp = $expiration->getMaxAgeAsTimestamp();
  157. $this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp);
  158. }
  159. /**
  160. * @param int $time
  161. * @return ITimeFactory|MockObject
  162. */
  163. private function getMockedTimeFactory($time){
  164. $mockedTimeFactory = $this->createMock(ITimeFactory::class);
  165. $mockedTimeFactory->expects($this->any())
  166. ->method('getTime')
  167. ->willReturn($time);
  168. return $mockedTimeFactory;
  169. }
  170. /**
  171. * @param string $returnValue
  172. * @return IConfig|MockObject
  173. */
  174. private function getMockedConfig($returnValue){
  175. $mockedConfig = $this->createMock(IConfig::class);
  176. $mockedConfig->expects($this->any())
  177. ->method('getSystemValue')
  178. ->willReturn($returnValue);
  179. return $mockedConfig;
  180. }
  181. }