1
0

ExpirationTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\AppFramework\Utility\ITimeFactory;
  27. use OCP\IConfig;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. class ExpirationTest extends \Test\TestCase {
  30. const SECONDS_PER_DAY = 86400; //60*60*24
  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::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  106. [ 'auto,auto', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  107. [ 'auto, auto', Expiration::NO_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. [ 'g,a,r,b,a,g,e', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ],
  113. [ '-3,8', Expiration::NO_OBLIGATION, Expiration::NO_OBLIGATION, true ]
  114. ];
  115. }
  116. /**
  117. * @dataProvider configData
  118. *
  119. * @param string $configValue
  120. * @param int $expectedMinAge
  121. * @param int $expectedMaxAge
  122. * @param bool $expectedCanPurgeToSaveSpace
  123. */
  124. public function testParseRetentionObligation($configValue, $expectedMinAge, $expectedMaxAge, $expectedCanPurgeToSaveSpace){
  125. $mockedConfig = $this->getMockedConfig($configValue);
  126. $mockedTimeFactory = $this->getMockedTimeFactory(
  127. time()
  128. );
  129. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  130. $this->assertAttributeEquals($expectedMinAge, 'minAge', $expiration);
  131. $this->assertAttributeEquals($expectedMaxAge, 'maxAge', $expiration);
  132. $this->assertAttributeEquals($expectedCanPurgeToSaveSpace, 'canPurgeToSaveSpace', $expiration);
  133. }
  134. /**
  135. * @param int $time
  136. * @return ITimeFactory|MockObject
  137. */
  138. private function getMockedTimeFactory($time){
  139. $mockedTimeFactory = $this->createMock(ITimeFactory::class);
  140. $mockedTimeFactory->expects($this->any())
  141. ->method('getTime')
  142. ->willReturn($time);
  143. return $mockedTimeFactory;
  144. }
  145. /**
  146. * @param string $returnValue
  147. * @return IConfig|MockObject
  148. */
  149. private function getMockedConfig($returnValue){
  150. $mockedConfig = $this->createMock(IConfig::class);
  151. $mockedConfig->expects($this->any())
  152. ->method('getSystemValue')
  153. ->willReturn($returnValue);
  154. return $mockedConfig;
  155. }
  156. }