ExpirationTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Trashbin\Tests;
  8. use OCA\Files_Trashbin\Expiration;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IConfig;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. class ExpirationTest extends \Test\TestCase {
  13. public const SECONDS_PER_DAY = 86400; //60*60*24
  14. public const FAKE_TIME_NOW = 1000000;
  15. public function expirationData() {
  16. $today = 100 * self::SECONDS_PER_DAY;
  17. $back10Days = (100 - 10) * self::SECONDS_PER_DAY;
  18. $back20Days = (100 - 20) * self::SECONDS_PER_DAY;
  19. $back30Days = (100 - 30) * self::SECONDS_PER_DAY;
  20. $back35Days = (100 - 35) * self::SECONDS_PER_DAY;
  21. // it should never happen, but who knows :/
  22. $ahead100Days = (100 + 100) * self::SECONDS_PER_DAY;
  23. return [
  24. // Expiration is disabled - always should return false
  25. [ 'disabled', $today, $back10Days, false, false],
  26. [ 'disabled', $today, $back10Days, true, false],
  27. [ 'disabled', $today, $ahead100Days, true, false],
  28. // Default: expire in 30 days or earlier when quota requirements are met
  29. [ 'auto', $today, $back10Days, false, false],
  30. [ 'auto', $today, $back35Days, false, false],
  31. [ 'auto', $today, $back10Days, true, true],
  32. [ 'auto', $today, $back35Days, true, true],
  33. [ 'auto', $today, $ahead100Days, true, true],
  34. // The same with 'auto'
  35. [ 'auto, auto', $today, $back10Days, false, false],
  36. [ 'auto, auto', $today, $back35Days, false, false],
  37. [ 'auto, auto', $today, $back10Days, true, true],
  38. [ 'auto, auto', $today, $back35Days, true, true],
  39. // Keep for 15 days but expire anytime if space needed
  40. [ '15, auto', $today, $back10Days, false, false],
  41. [ '15, auto', $today, $back20Days, false, false],
  42. [ '15, auto', $today, $back10Days, true, true],
  43. [ '15, auto', $today, $back20Days, true, true],
  44. [ '15, auto', $today, $ahead100Days, true, true],
  45. // Expire anytime if space needed, Expire all older than max
  46. [ 'auto, 15', $today, $back10Days, false, false],
  47. [ 'auto, 15', $today, $back20Days, false, true],
  48. [ 'auto, 15', $today, $back10Days, true, true],
  49. [ 'auto, 15', $today, $back20Days, true, true],
  50. [ 'auto, 15', $today, $ahead100Days, true, true],
  51. // Expire all older than max OR older than min if space needed
  52. [ '15, 25', $today, $back10Days, false, false],
  53. [ '15, 25', $today, $back20Days, false, false],
  54. [ '15, 25', $today, $back30Days, false, true],
  55. [ '15, 25', $today, $back10Days, false, false],
  56. [ '15, 25', $today, $back20Days, true, true],
  57. [ '15, 25', $today, $back30Days, true, true],
  58. [ '15, 25', $today, $ahead100Days, true, false],
  59. // Expire all older than max OR older than min if space needed
  60. // Max<Min case
  61. [ '25, 15', $today, $back10Days, false, false],
  62. [ '25, 15', $today, $back20Days, false, false],
  63. [ '25, 15', $today, $back30Days, false, true],
  64. [ '25, 15', $today, $back10Days, false, false],
  65. [ '25, 15', $today, $back20Days, true, false],
  66. [ '25, 15', $today, $back30Days, true, true],
  67. [ '25, 15', $today, $ahead100Days, true, false],
  68. ];
  69. }
  70. /**
  71. * @dataProvider expirationData
  72. *
  73. * @param string $retentionObligation
  74. * @param int $timeNow
  75. * @param int $timestamp
  76. * @param bool $quotaExceeded
  77. * @param string $expectedResult
  78. */
  79. public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult) {
  80. $mockedConfig = $this->getMockedConfig($retentionObligation);
  81. $mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
  82. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  83. $actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
  84. $this->assertEquals($expectedResult, $actualResult);
  85. }
  86. public function timestampTestData(): array {
  87. return [
  88. [ 'disabled', false],
  89. [ 'auto', false ],
  90. [ 'auto,auto', false ],
  91. [ 'auto, auto', false ],
  92. [ 'auto, 3', self::FAKE_TIME_NOW - (3 * self::SECONDS_PER_DAY) ],
  93. [ '5, auto', false ],
  94. [ '3, 5', self::FAKE_TIME_NOW - (5 * self::SECONDS_PER_DAY) ],
  95. [ '10, 3', self::FAKE_TIME_NOW - (10 * self::SECONDS_PER_DAY) ],
  96. ];
  97. }
  98. /**
  99. * @dataProvider timestampTestData
  100. *
  101. * @param string $configValue
  102. * @param int $expectedMaxAgeTimestamp
  103. */
  104. public function testGetMaxAgeAsTimestamp($configValue, $expectedMaxAgeTimestamp) {
  105. $mockedConfig = $this->getMockedConfig($configValue);
  106. $mockedTimeFactory = $this->getMockedTimeFactory(
  107. self::FAKE_TIME_NOW
  108. );
  109. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  110. $actualTimestamp = $expiration->getMaxAgeAsTimestamp();
  111. $this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp);
  112. }
  113. /**
  114. * @param int $time
  115. * @return ITimeFactory|MockObject
  116. */
  117. private function getMockedTimeFactory($time) {
  118. $mockedTimeFactory = $this->createMock(ITimeFactory::class);
  119. $mockedTimeFactory->expects($this->any())
  120. ->method('getTime')
  121. ->willReturn($time);
  122. return $mockedTimeFactory;
  123. }
  124. /**
  125. * @param string $returnValue
  126. * @return IConfig|MockObject
  127. */
  128. private function getMockedConfig($returnValue) {
  129. $mockedConfig = $this->createMock(IConfig::class);
  130. $mockedConfig->expects($this->any())
  131. ->method('getSystemValue')
  132. ->willReturn($returnValue);
  133. return $mockedConfig;
  134. }
  135. }