summaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/tests/ExpirationTest.php
blob: a9b4124236e1a4d7d88a9bfff4b5a5cfd30ce59a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author Joas Schilling <coding@schilljs.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Victor Dubiniuk <dubiniuk@owncloud.com>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */

use OCA\Files_Trashbin\Expiration;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

class ExpirationTest extends \Test\TestCase {
	public const SECONDS_PER_DAY = 86400; //60*60*24

	public const FAKE_TIME_NOW = 1000000;

	public function expirationData() {
		$today = 100*self::SECONDS_PER_DAY;
		$back10Days = (100-10)*self::SECONDS_PER_DAY;
		$back20Days = (100-20)*self::SECONDS_PER_DAY;
		$back30Days = (100-30)*self::SECONDS_PER_DAY;
		$back35Days = (100-35)*self::SECONDS_PER_DAY;

		// it should never happen, but who knows :/
		$ahead100Days = (100+100)*self::SECONDS_PER_DAY;

		return [
			// Expiration is disabled - always should return false
			[ 'disabled', $today, $back10Days, false, false],
			[ 'disabled', $today, $back10Days, true, false],
			[ 'disabled', $today, $ahead100Days, true, false],

			// Default: expire in 30 days or earlier when quota requirements are met
			[ 'auto', $today, $back10Days, false, false],
			[ 'auto', $today, $back35Days, false, false],
			[ 'auto', $today, $back10Days, true, true],
			[ 'auto', $today, $back35Days, true, true],
			[ 'auto', $today, $ahead100Days, true, true],

			// The same with 'auto'
			[ 'auto, auto', $today, $back10Days, false, false],
			[ 'auto, auto', $today, $back35Days, false, false],
			[ 'auto, auto', $today, $back10Days, true, true],
			[ 'auto, auto', $today, $back35Days, true, true],

			// Keep for 15 days but expire anytime if space needed
			[ '15, auto', $today, $back10Days, false, false],
			[ '15, auto', $today, $back20Days, false, false],
			[ '15, auto', $today, $back10Days, true, true],
			[ '15, auto', $today, $back20Days, true, true],
			[ '15, auto', $today, $ahead100Days, true, true],

			// Expire anytime if space needed, Expire all older than max
			[ 'auto, 15', $today, $back10Days, false, false],
			[ 'auto, 15', $today, $back20Days, false, true],
			[ 'auto, 15', $today, $back10Days, true, true],
			[ 'auto, 15', $today, $back20Days, true, true],
			[ 'auto, 15', $today, $ahead100Days, true, true],

			// Expire all older than max OR older than min if space needed
			[ '15, 25', $today, $back10Days, false, false],
			[ '15, 25', $today, $back20Days, false, false],
			[ '15, 25', $today, $back30Days, false, true],
			[ '15, 25', $today, $back10Days, false, false],
			[ '15, 25', $today, $back20Days, true, true],
			[ '15, 25', $today, $back30Days, true, true],
			[ '15, 25', $today, $ahead100Days, true, false],

			// Expire all older than max OR older than min if space needed
			// Max<Min case
			[ '25, 15', $today, $back10Days, false, false],
			[ '25, 15', $today, $back20Days, false, false],
			[ '25, 15', $today, $back30Days, false, true],
			[ '25, 15', $today, $back10Days, false, false],
			[ '25, 15', $today, $back20Days, true, false],
			[ '25, 15', $today, $back30Days, true, true],
			[ '25, 15', $today, $ahead100Days, true, false],
		];
	}

	/**
	 * @dataProvider expirationData
	 *
	 * @param string $retentionObligation
	 * @param int $timeNow
	 * @param int $timestamp
	 * @param bool $quotaExceeded
	 * @param string $expectedResult
	 */
	public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult) {
		$mockedConfig = $this->getMockedConfig($retentionObligation);
		$mockedTimeFactory = $this->getMockedTimeFactory($timeNow);

		$expiration = new Expiration($mockedConfig, $mockedTimeFactory);
		$actualResult = $expiration->isExpired($timestamp, $quotaExceeded);
		
		$this->assertEquals($expectedResult, $actualResult);
	}


	public function configData() {
		return [
			[ 'disabled', null, null, null],
			[ 'auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
			[ 'auto,auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
			[ 'auto, auto', Expiration::DEFAULT_RETENTION_OBLIGATION, Expiration::NO_OBLIGATION, true ],
			[ 'auto, 3', Expiration::NO_OBLIGATION, 3, true ],
			[ '5, auto', 5, Expiration::NO_OBLIGATION, true ],
			[ '3, 5', 3, 5, false ],
			[ '10, 3', 10, 10, false ],
		];
	}


	/**
	 * @dataProvider configData
	 *
	 * @param string $configValue
	 * @param int $expectedMinAge
	 * @param int $expectedMaxAge
	 * @param bool $expectedCanPurgeToSaveSpace
	 */
	public function testParseRetentionObligation($configValue, $expectedMinAge, $expectedMaxAge, $expectedCanPurgeToSaveSpace) {
		$mockedConfig = $this->getMockedConfig($configValue);
		$mockedTimeFactory = $this->getMockedTimeFactory(
				time()
		);

		$expiration = new Expiration($mockedConfig, $mockedTimeFactory);
		$this->assertAttributeEquals($expectedMinAge, 'minAge', $expiration);
		$this->assertAttributeEquals($expectedMaxAge, 'maxAge', $expiration);
		$this->assertAttributeEquals($expectedCanPurgeToSaveSpace, 'canPurgeToSaveSpace', $expiration);
	}


	public function timestampTestData() {
		return [
			[ 'disabled', false],
			[ 'auto', false ],
			[ 'auto,auto', false ],
			[ 'auto, auto', false ],
			[ 'auto, 3',  self::FAKE_TIME_NOW - (3*self::SECONDS_PER_DAY) ],
			[ '5, auto', false ],
			[ '3, 5', self::FAKE_TIME_NOW - (5*self::SECONDS_PER_DAY) ],
			[ '10, 3', self::FAKE_TIME_NOW - (10*self::SECONDS_PER_DAY) ],
		];
	}


	/**
	 * @dataProvider timestampTestData
	 *
	 * @param string $configValue
	 * @param int $expectedMaxAgeTimestamp
	 */
	public function testGetMaxAgeAsTimestamp($configValue, $expectedMaxAgeTimestamp) {
		$mockedConfig = $this->getMockedConfig($configValue);
		$mockedTimeFactory = $this->getMockedTimeFactory(
				self::FAKE_TIME_NOW
		);

		$expiration = new Expiration($mockedConfig, $mockedTimeFactory);
		$actualTimestamp = $expiration->getMaxAgeAsTimestamp();
		$this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp);
	}

	/**
	 * @param int $time
	 * @return ITimeFactory|MockObject
	 */
	private function getMockedTimeFactory($time) {
		$mockedTimeFactory = $this->createMock(ITimeFactory::class);
		$mockedTimeFactory->expects($this->any())
			->method('getTime')
			->willReturn($time);

		return $mockedTimeFactory;
	}

	/**
	 * @param string $returnValue
	 * @return IConfig|MockObject
	 */
	private function getMockedConfig($returnValue) {
		$mockedConfig = $this->createMock(IConfig::class);
		$mockedConfig->expects($this->any())
			->method('getSystemValue')
			->willReturn($returnValue);

		return $mockedConfig;
	}
}