1
0

ExpirationTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\Tests;
  8. use OCA\Files_Versions\Expiration;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IConfig;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use Psr\Log\LoggerInterface;
  13. class ExpirationTest extends \Test\TestCase {
  14. public const SECONDS_PER_DAY = 86400; //60*60*24
  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): void {
  80. $mockedConfig = $this->getMockedConfig($retentionObligation);
  81. $mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
  82. $mockedLogger = $this->createMock(LoggerInterface::class);
  83. $expiration = new Expiration($mockedConfig, $mockedTimeFactory, $mockedLogger);
  84. $actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
  85. $this->assertEquals($expectedResult, $actualResult);
  86. }
  87. /**
  88. * @param int $time
  89. * @return ITimeFactory|MockObject
  90. */
  91. private function getMockedTimeFactory($time) {
  92. $mockedTimeFactory = $this->createMock(ITimeFactory::class);
  93. $mockedTimeFactory->expects($this->any())
  94. ->method('getTime')
  95. ->willReturn($time);
  96. return $mockedTimeFactory;
  97. }
  98. /**
  99. * @param string $returnValue
  100. * @return IConfig|MockObject
  101. */
  102. private function getMockedConfig($returnValue) {
  103. $mockedConfig = $this->createMock(IConfig::class);
  104. $mockedConfig->expects($this->any())
  105. ->method('getSystemValue')
  106. ->willReturn($returnValue);
  107. return $mockedConfig;
  108. }
  109. }