1
0

ExpirationTest.php 7.1 KB

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