ExpirationTest.php 6.3 KB

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