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

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