ExpirationTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_Trashbin\Tests;
  27. use OCA\Files_Trashbin\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 const FAKE_TIME_NOW = 1000000;
  34. public function expirationData() {
  35. $today = 100 * self::SECONDS_PER_DAY;
  36. $back10Days = (100 - 10) * self::SECONDS_PER_DAY;
  37. $back20Days = (100 - 20) * self::SECONDS_PER_DAY;
  38. $back30Days = (100 - 30) * self::SECONDS_PER_DAY;
  39. $back35Days = (100 - 35) * self::SECONDS_PER_DAY;
  40. // it should never happen, but who knows :/
  41. $ahead100Days = (100 + 100) * self::SECONDS_PER_DAY;
  42. return [
  43. // Expiration is disabled - always should return false
  44. [ 'disabled', $today, $back10Days, false, false],
  45. [ 'disabled', $today, $back10Days, true, false],
  46. [ 'disabled', $today, $ahead100Days, true, false],
  47. // Default: expire in 30 days or earlier when quota requirements are met
  48. [ 'auto', $today, $back10Days, false, false],
  49. [ 'auto', $today, $back35Days, false, false],
  50. [ 'auto', $today, $back10Days, true, true],
  51. [ 'auto', $today, $back35Days, true, true],
  52. [ 'auto', $today, $ahead100Days, true, true],
  53. // The same with 'auto'
  54. [ 'auto, auto', $today, $back10Days, false, false],
  55. [ 'auto, auto', $today, $back35Days, false, false],
  56. [ 'auto, auto', $today, $back10Days, true, true],
  57. [ 'auto, auto', $today, $back35Days, true, true],
  58. // Keep for 15 days but expire anytime if space needed
  59. [ '15, auto', $today, $back10Days, false, false],
  60. [ '15, auto', $today, $back20Days, false, false],
  61. [ '15, auto', $today, $back10Days, true, true],
  62. [ '15, auto', $today, $back20Days, true, true],
  63. [ '15, auto', $today, $ahead100Days, true, true],
  64. // Expire anytime if space needed, Expire all older than max
  65. [ 'auto, 15', $today, $back10Days, false, false],
  66. [ 'auto, 15', $today, $back20Days, false, true],
  67. [ 'auto, 15', $today, $back10Days, true, true],
  68. [ 'auto, 15', $today, $back20Days, true, true],
  69. [ 'auto, 15', $today, $ahead100Days, true, true],
  70. // Expire all older than max OR older than min if space needed
  71. [ '15, 25', $today, $back10Days, false, false],
  72. [ '15, 25', $today, $back20Days, false, false],
  73. [ '15, 25', $today, $back30Days, false, true],
  74. [ '15, 25', $today, $back10Days, false, false],
  75. [ '15, 25', $today, $back20Days, true, true],
  76. [ '15, 25', $today, $back30Days, true, true],
  77. [ '15, 25', $today, $ahead100Days, true, false],
  78. // Expire all older than max OR older than min if space needed
  79. // Max<Min case
  80. [ '25, 15', $today, $back10Days, false, false],
  81. [ '25, 15', $today, $back20Days, false, false],
  82. [ '25, 15', $today, $back30Days, false, true],
  83. [ '25, 15', $today, $back10Days, false, false],
  84. [ '25, 15', $today, $back20Days, true, false],
  85. [ '25, 15', $today, $back30Days, true, true],
  86. [ '25, 15', $today, $ahead100Days, true, false],
  87. ];
  88. }
  89. /**
  90. * @dataProvider expirationData
  91. *
  92. * @param string $retentionObligation
  93. * @param int $timeNow
  94. * @param int $timestamp
  95. * @param bool $quotaExceeded
  96. * @param string $expectedResult
  97. */
  98. public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult) {
  99. $mockedConfig = $this->getMockedConfig($retentionObligation);
  100. $mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
  101. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  102. $actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
  103. $this->assertEquals($expectedResult, $actualResult);
  104. }
  105. public function timestampTestData(): array {
  106. return [
  107. [ 'disabled', false],
  108. [ 'auto', false ],
  109. [ 'auto,auto', false ],
  110. [ 'auto, auto', false ],
  111. [ 'auto, 3', self::FAKE_TIME_NOW - (3 * self::SECONDS_PER_DAY) ],
  112. [ '5, auto', false ],
  113. [ '3, 5', self::FAKE_TIME_NOW - (5 * self::SECONDS_PER_DAY) ],
  114. [ '10, 3', self::FAKE_TIME_NOW - (10 * self::SECONDS_PER_DAY) ],
  115. ];
  116. }
  117. /**
  118. * @dataProvider timestampTestData
  119. *
  120. * @param string $configValue
  121. * @param int $expectedMaxAgeTimestamp
  122. */
  123. public function testGetMaxAgeAsTimestamp($configValue, $expectedMaxAgeTimestamp) {
  124. $mockedConfig = $this->getMockedConfig($configValue);
  125. $mockedTimeFactory = $this->getMockedTimeFactory(
  126. self::FAKE_TIME_NOW
  127. );
  128. $expiration = new Expiration($mockedConfig, $mockedTimeFactory);
  129. $actualTimestamp = $expiration->getMaxAgeAsTimestamp();
  130. $this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp);
  131. }
  132. /**
  133. * @param int $time
  134. * @return ITimeFactory|MockObject
  135. */
  136. private function getMockedTimeFactory($time) {
  137. $mockedTimeFactory = $this->createMock(ITimeFactory::class);
  138. $mockedTimeFactory->expects($this->any())
  139. ->method('getTime')
  140. ->willReturn($time);
  141. return $mockedTimeFactory;
  142. }
  143. /**
  144. * @param string $returnValue
  145. * @return IConfig|MockObject
  146. */
  147. private function getMockedConfig($returnValue) {
  148. $mockedConfig = $this->createMock(IConfig::class);
  149. $mockedConfig->expects($this->any())
  150. ->method('getSystemValue')
  151. ->willReturn($returnValue);
  152. return $mockedConfig;
  153. }
  154. }