@@ -25,418 +25,418 @@ |
||
25 | 25 | use Test\TestCase; |
26 | 26 | |
27 | 27 | class UpdateAvailableNotificationsTest extends TestCase { |
28 | - private ServerVersion&MockObject $serverVersion; |
|
29 | - private IConfig&MockObject $config; |
|
30 | - private IManager&MockObject $notificationManager; |
|
31 | - private IGroupManager&MockObject $groupManager; |
|
32 | - private IAppManager&MockObject $appManager; |
|
33 | - private IAppConfig&MockObject $appConfig; |
|
34 | - private ITimeFactory&MockObject $timeFactory; |
|
35 | - private Installer&MockObject $installer; |
|
36 | - private VersionCheck&MockObject $versionCheck; |
|
37 | - |
|
38 | - protected function setUp(): void { |
|
39 | - parent::setUp(); |
|
40 | - |
|
41 | - $this->serverVersion = $this->createMock(ServerVersion::class); |
|
42 | - $this->config = $this->createMock(IConfig::class); |
|
43 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
44 | - $this->notificationManager = $this->createMock(IManager::class); |
|
45 | - $this->groupManager = $this->createMock(IGroupManager::class); |
|
46 | - $this->appManager = $this->createMock(IAppManager::class); |
|
47 | - $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
48 | - $this->installer = $this->createMock(Installer::class); |
|
49 | - $this->versionCheck = $this->createMock(VersionCheck::class); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return UpdateAvailableNotifications|MockObject |
|
54 | - */ |
|
55 | - protected function getJob(array $methods = []): UpdateAvailableNotifications { |
|
56 | - if (empty($methods)) { |
|
57 | - return new UpdateAvailableNotifications( |
|
58 | - $this->timeFactory, |
|
59 | - $this->serverVersion, |
|
60 | - $this->config, |
|
61 | - $this->appConfig, |
|
62 | - $this->notificationManager, |
|
63 | - $this->groupManager, |
|
64 | - $this->appManager, |
|
65 | - $this->installer, |
|
66 | - $this->versionCheck, |
|
67 | - ); |
|
68 | - } |
|
69 | - { |
|
70 | - return $this->getMockBuilder(UpdateAvailableNotifications::class) |
|
71 | - ->setConstructorArgs([ |
|
72 | - $this->timeFactory, |
|
73 | - $this->serverVersion, |
|
74 | - $this->config, |
|
75 | - $this->appConfig, |
|
76 | - $this->notificationManager, |
|
77 | - $this->groupManager, |
|
78 | - $this->appManager, |
|
79 | - $this->installer, |
|
80 | - $this->versionCheck, |
|
81 | - ]) |
|
82 | - ->onlyMethods($methods) |
|
83 | - ->getMock(); |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - public function testRun(): void { |
|
88 | - $job = $this->getJob([ |
|
89 | - 'checkCoreUpdate', |
|
90 | - 'checkAppUpdates', |
|
91 | - ]); |
|
92 | - |
|
93 | - $job->expects($this->once()) |
|
94 | - ->method('checkCoreUpdate'); |
|
95 | - $job->expects($this->once()) |
|
96 | - ->method('checkAppUpdates'); |
|
97 | - |
|
98 | - $this->config->expects(self::exactly(2)) |
|
99 | - ->method('getSystemValueBool') |
|
100 | - ->willReturnMap([ |
|
101 | - ['debug', false, true], |
|
102 | - ['has_internet_connection', true, true], |
|
103 | - ]); |
|
104 | - self::invokePrivate($job, 'run', [null]); |
|
105 | - } |
|
106 | - |
|
107 | - public function testRunNoInternet(): void { |
|
108 | - $job = $this->getJob([ |
|
109 | - 'checkCoreUpdate', |
|
110 | - 'checkAppUpdates', |
|
111 | - ]); |
|
112 | - |
|
113 | - $job->expects($this->never()) |
|
114 | - ->method('checkCoreUpdate'); |
|
115 | - $job->expects($this->never()) |
|
116 | - ->method('checkAppUpdates'); |
|
117 | - |
|
118 | - $this->config |
|
119 | - ->expects(self::once()) |
|
120 | - ->method('getSystemValueBool') |
|
121 | - ->with('has_internet_connection', true) |
|
122 | - ->willReturn(false); |
|
123 | - |
|
124 | - self::invokePrivate($job, 'run', [null]); |
|
125 | - } |
|
126 | - |
|
127 | - public static function dataCheckCoreUpdate(): array { |
|
128 | - return [ |
|
129 | - ['daily', null, null, null, null], |
|
130 | - ['git', null, null, null, null], |
|
131 | - ['beta', [], null, null, null], |
|
132 | - ['beta', false, false, null, null], |
|
133 | - ['beta', false, false, null, 13], |
|
134 | - ['beta', [ |
|
135 | - 'version' => '9.2.0', |
|
136 | - 'versionstring' => 'Nextcloud 11.0.0', |
|
137 | - ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
138 | - ['stable', [], null, null, null], |
|
139 | - ['stable', false, false, null, null], |
|
140 | - ['stable', false, false, null, 6], |
|
141 | - ['stable', [ |
|
142 | - 'version' => '9.2.0', |
|
143 | - 'versionstring' => 'Nextcloud 11.0.0', |
|
144 | - ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
145 | - ['production', [], null, null, null], |
|
146 | - ['production', false, false, null, null], |
|
147 | - ['production', false, false, null, 2], |
|
148 | - ['production', [ |
|
149 | - 'version' => '9.2.0', |
|
150 | - 'versionstring' => 'Nextcloud 11.0.0', |
|
151 | - ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
152 | - ]; |
|
153 | - } |
|
154 | - |
|
155 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckCoreUpdate')] |
|
156 | - public function testCheckCoreUpdate(string $channel, mixed $versionCheck, mixed $version, ?string $readableVersion, ?int $errorDays): void { |
|
157 | - $job = $this->getJob([ |
|
158 | - 'createNotifications', |
|
159 | - 'clearErrorNotifications', |
|
160 | - 'sendErrorNotifications', |
|
161 | - ]); |
|
162 | - |
|
163 | - $this->serverVersion->expects($this->once()) |
|
164 | - ->method('getChannel') |
|
165 | - ->willReturn($channel); |
|
166 | - |
|
167 | - if ($versionCheck === null) { |
|
168 | - $this->versionCheck->expects($this->never()) |
|
169 | - ->method('check'); |
|
170 | - } else { |
|
171 | - $this->versionCheck->expects($this->once()) |
|
172 | - ->method('check') |
|
173 | - ->willReturn($versionCheck); |
|
174 | - } |
|
175 | - |
|
176 | - if ($version === null) { |
|
177 | - $job->expects($this->never()) |
|
178 | - ->method('createNotifications'); |
|
179 | - $job->expects($versionCheck === null ? $this->never() : $this->once()) |
|
180 | - ->method('clearErrorNotifications'); |
|
181 | - } elseif ($version === false) { |
|
182 | - $job->expects($this->never()) |
|
183 | - ->method('createNotifications'); |
|
184 | - $job->expects($this->never()) |
|
185 | - ->method('clearErrorNotifications'); |
|
186 | - |
|
187 | - $this->appConfig->expects($this->once()) |
|
188 | - ->method('getAppValueInt') |
|
189 | - ->willReturn($errorDays ?? 0); |
|
190 | - $this->appConfig->expects($this->once()) |
|
191 | - ->method('setAppValueInt') |
|
192 | - ->with('update_check_errors', $errorDays + 1); |
|
193 | - $job->expects($errorDays !== null ? $this->once() : $this->never()) |
|
194 | - ->method('sendErrorNotifications') |
|
195 | - ->with($errorDays + 1); |
|
196 | - } else { |
|
197 | - $this->appConfig->expects($this->once()) |
|
198 | - ->method('setAppValueInt') |
|
199 | - ->with('update_check_errors', 0); |
|
200 | - $job->expects($this->once()) |
|
201 | - ->method('clearErrorNotifications'); |
|
202 | - $job->expects($this->once()) |
|
203 | - ->method('createNotifications') |
|
204 | - ->with('core', $version, $readableVersion); |
|
205 | - } |
|
206 | - |
|
207 | - $this->config->expects(self::any()) |
|
208 | - ->method('getSystemValueBool') |
|
209 | - ->willReturnMap([ |
|
210 | - ['updatechecker', true, true], |
|
211 | - ['has_internet_connection', true, true], |
|
212 | - ]); |
|
213 | - |
|
214 | - self::invokePrivate($job, 'checkCoreUpdate'); |
|
215 | - } |
|
216 | - |
|
217 | - public static function dataCheckAppUpdates(): array { |
|
218 | - return [ |
|
219 | - [ |
|
220 | - ['app1', 'app2'], |
|
221 | - [ |
|
222 | - ['app1', false], |
|
223 | - ['app2', '1.9.2'], |
|
224 | - ], |
|
225 | - [ |
|
226 | - ['app2', '1.9.2', ''], |
|
227 | - ], |
|
228 | - ], |
|
229 | - ]; |
|
230 | - } |
|
231 | - |
|
232 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckAppUpdates')] |
|
233 | - public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications): void { |
|
234 | - $job = $this->getJob([ |
|
235 | - 'isUpdateAvailable', |
|
236 | - 'createNotifications', |
|
237 | - ]); |
|
238 | - |
|
239 | - $this->appManager->expects($this->once()) |
|
240 | - ->method('getEnabledApps') |
|
241 | - ->willReturn($apps); |
|
242 | - |
|
243 | - $job->expects($this->exactly(\count($apps))) |
|
244 | - ->method('isUpdateAvailable') |
|
245 | - ->willReturnMap($isUpdateAvailable); |
|
246 | - |
|
247 | - $i = 0; |
|
248 | - $job->expects($this->exactly(\count($notifications))) |
|
249 | - ->method('createNotifications') |
|
250 | - ->willReturnCallback(function () use ($notifications, &$i): void { |
|
251 | - $this->assertEquals($notifications[$i], func_get_args()); |
|
252 | - $i++; |
|
253 | - }); |
|
254 | - |
|
255 | - |
|
256 | - self::invokePrivate($job, 'checkAppUpdates'); |
|
257 | - } |
|
258 | - |
|
259 | - public static function dataCreateNotifications(): array { |
|
260 | - return [ |
|
261 | - ['app1', '1.0.0', '1.0.0', false, false, null, null], |
|
262 | - ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]], |
|
263 | - ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]], |
|
264 | - ]; |
|
265 | - } |
|
266 | - |
|
267 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataCreateNotifications')] |
|
268 | - public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
269 | - $job = $this->getJob([ |
|
270 | - 'deleteOutdatedNotifications', |
|
271 | - 'getUsersToNotify', |
|
272 | - ]); |
|
273 | - |
|
274 | - $this->appConfig->expects($this->once()) |
|
275 | - ->method('getAppValueString') |
|
276 | - ->with($app, '') |
|
277 | - ->willReturn($lastNotification ?: ''); |
|
278 | - |
|
279 | - if ($lastNotification !== $version) { |
|
280 | - $this->appConfig->expects($this->once()) |
|
281 | - ->method('setAppValueString') |
|
282 | - ->with($app, $version); |
|
283 | - } |
|
284 | - |
|
285 | - if ($callDelete === false) { |
|
286 | - $job->expects($this->never()) |
|
287 | - ->method('deleteOutdatedNotifications'); |
|
288 | - } else { |
|
289 | - $job->expects($this->once()) |
|
290 | - ->method('deleteOutdatedNotifications') |
|
291 | - ->with($app, $callDelete); |
|
292 | - } |
|
293 | - |
|
294 | - if ($users === null) { |
|
295 | - $job->expects($this->never()) |
|
296 | - ->method('getUsersToNotify'); |
|
297 | - } else { |
|
298 | - $job->expects($this->once()) |
|
299 | - ->method('getUsersToNotify') |
|
300 | - ->willReturn($users); |
|
301 | - } |
|
302 | - |
|
303 | - if ($createNotification) { |
|
304 | - $notification = $this->createMock(INotification::class); |
|
305 | - $notification->expects($this->once()) |
|
306 | - ->method('setApp') |
|
307 | - ->with('updatenotification') |
|
308 | - ->willReturnSelf(); |
|
309 | - $notification->expects($this->once()) |
|
310 | - ->method('setDateTime') |
|
311 | - ->willReturnSelf(); |
|
312 | - $notification->expects($this->once()) |
|
313 | - ->method('setObject') |
|
314 | - ->with($app, $version) |
|
315 | - ->willReturnSelf(); |
|
316 | - $notification->expects($this->once()) |
|
317 | - ->method('setSubject') |
|
318 | - ->with('update_available') |
|
319 | - ->willReturnSelf(); |
|
320 | - |
|
321 | - if ($userNotifications !== null) { |
|
322 | - $notification->expects($this->exactly(\count($userNotifications))) |
|
323 | - ->method('setUser') |
|
324 | - ->willReturnSelf(); |
|
325 | - |
|
326 | - $this->notificationManager->expects($this->exactly(\count($userNotifications))) |
|
327 | - ->method('notify'); |
|
328 | - } |
|
329 | - |
|
330 | - $this->notificationManager->expects($this->once()) |
|
331 | - ->method('createNotification') |
|
332 | - ->willReturn($notification); |
|
333 | - } else { |
|
334 | - $this->notificationManager->expects($this->never()) |
|
335 | - ->method('createNotification'); |
|
336 | - } |
|
337 | - |
|
338 | - self::invokePrivate($job, 'createNotifications', [$app, $version]); |
|
339 | - } |
|
340 | - |
|
341 | - public static function dataGetUsersToNotify(): array { |
|
342 | - return [ |
|
343 | - [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']], |
|
344 | - [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']], |
|
345 | - ]; |
|
346 | - } |
|
347 | - |
|
348 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataGetUsersToNotify')] |
|
349 | - public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected): void { |
|
350 | - $job = $this->getJob(); |
|
351 | - |
|
352 | - $this->appConfig->expects($this->once()) |
|
353 | - ->method('getAppValueArray') |
|
354 | - ->with('notify_groups', ['admin']) |
|
355 | - ->willReturn($groups); |
|
356 | - |
|
357 | - $groupMap = []; |
|
358 | - foreach ($groupUsers as $gid => $uids) { |
|
359 | - if ($uids === null) { |
|
360 | - $group = null; |
|
361 | - } else { |
|
362 | - $group = $this->getGroup($gid); |
|
363 | - $group->expects($this->any()) |
|
364 | - ->method('getUsers') |
|
365 | - ->willReturn($this->getUsers($uids)); |
|
366 | - } |
|
367 | - $groupMap[] = [$gid, $group]; |
|
368 | - } |
|
369 | - $this->groupManager->expects($this->exactly(\count($groups))) |
|
370 | - ->method('get') |
|
371 | - ->willReturnMap($groupMap); |
|
372 | - |
|
373 | - $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
374 | - $this->assertEquals($expected, $result); |
|
375 | - |
|
376 | - // Test caching |
|
377 | - $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
378 | - $this->assertEquals($expected, $result); |
|
379 | - } |
|
380 | - |
|
381 | - public static function dataDeleteOutdatedNotifications(): array { |
|
382 | - return [ |
|
383 | - ['app1', '1.1.0'], |
|
384 | - ['app2', '1.2.0'], |
|
385 | - ]; |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * @param string $app |
|
390 | - * @param string $version |
|
391 | - */ |
|
392 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteOutdatedNotifications')] |
|
393 | - public function testDeleteOutdatedNotifications(string $app, string $version): void { |
|
394 | - $notification = $this->createMock(INotification::class); |
|
395 | - $notification->expects($this->once()) |
|
396 | - ->method('setApp') |
|
397 | - ->with('updatenotification') |
|
398 | - ->willReturnSelf(); |
|
399 | - $notification->expects($this->once()) |
|
400 | - ->method('setObject') |
|
401 | - ->with($app, $version) |
|
402 | - ->willReturnSelf(); |
|
403 | - |
|
404 | - $this->notificationManager->expects($this->once()) |
|
405 | - ->method('createNotification') |
|
406 | - ->willReturn($notification); |
|
407 | - $this->notificationManager->expects($this->once()) |
|
408 | - ->method('markProcessed') |
|
409 | - ->with($notification); |
|
410 | - |
|
411 | - $job = $this->getJob(); |
|
412 | - self::invokePrivate($job, 'deleteOutdatedNotifications', [$app, $version]); |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * @param string[] $userIds |
|
417 | - * @return IUser[]|MockObject[] |
|
418 | - */ |
|
419 | - protected function getUsers(array $userIds): array { |
|
420 | - $users = []; |
|
421 | - foreach ($userIds as $uid) { |
|
422 | - $user = $this->createMock(IUser::class); |
|
423 | - $user->expects($this->any()) |
|
424 | - ->method('getUID') |
|
425 | - ->willReturn($uid); |
|
426 | - $users[] = $user; |
|
427 | - } |
|
428 | - return $users; |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * @param string $gid |
|
433 | - * @return IGroup|MockObject |
|
434 | - */ |
|
435 | - protected function getGroup(string $gid) { |
|
436 | - $group = $this->createMock(IGroup::class); |
|
437 | - $group->expects($this->any()) |
|
438 | - ->method('getGID') |
|
439 | - ->willReturn($gid); |
|
440 | - return $group; |
|
441 | - } |
|
28 | + private ServerVersion&MockObject $serverVersion; |
|
29 | + private IConfig&MockObject $config; |
|
30 | + private IManager&MockObject $notificationManager; |
|
31 | + private IGroupManager&MockObject $groupManager; |
|
32 | + private IAppManager&MockObject $appManager; |
|
33 | + private IAppConfig&MockObject $appConfig; |
|
34 | + private ITimeFactory&MockObject $timeFactory; |
|
35 | + private Installer&MockObject $installer; |
|
36 | + private VersionCheck&MockObject $versionCheck; |
|
37 | + |
|
38 | + protected function setUp(): void { |
|
39 | + parent::setUp(); |
|
40 | + |
|
41 | + $this->serverVersion = $this->createMock(ServerVersion::class); |
|
42 | + $this->config = $this->createMock(IConfig::class); |
|
43 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
44 | + $this->notificationManager = $this->createMock(IManager::class); |
|
45 | + $this->groupManager = $this->createMock(IGroupManager::class); |
|
46 | + $this->appManager = $this->createMock(IAppManager::class); |
|
47 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
|
48 | + $this->installer = $this->createMock(Installer::class); |
|
49 | + $this->versionCheck = $this->createMock(VersionCheck::class); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return UpdateAvailableNotifications|MockObject |
|
54 | + */ |
|
55 | + protected function getJob(array $methods = []): UpdateAvailableNotifications { |
|
56 | + if (empty($methods)) { |
|
57 | + return new UpdateAvailableNotifications( |
|
58 | + $this->timeFactory, |
|
59 | + $this->serverVersion, |
|
60 | + $this->config, |
|
61 | + $this->appConfig, |
|
62 | + $this->notificationManager, |
|
63 | + $this->groupManager, |
|
64 | + $this->appManager, |
|
65 | + $this->installer, |
|
66 | + $this->versionCheck, |
|
67 | + ); |
|
68 | + } |
|
69 | + { |
|
70 | + return $this->getMockBuilder(UpdateAvailableNotifications::class) |
|
71 | + ->setConstructorArgs([ |
|
72 | + $this->timeFactory, |
|
73 | + $this->serverVersion, |
|
74 | + $this->config, |
|
75 | + $this->appConfig, |
|
76 | + $this->notificationManager, |
|
77 | + $this->groupManager, |
|
78 | + $this->appManager, |
|
79 | + $this->installer, |
|
80 | + $this->versionCheck, |
|
81 | + ]) |
|
82 | + ->onlyMethods($methods) |
|
83 | + ->getMock(); |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + public function testRun(): void { |
|
88 | + $job = $this->getJob([ |
|
89 | + 'checkCoreUpdate', |
|
90 | + 'checkAppUpdates', |
|
91 | + ]); |
|
92 | + |
|
93 | + $job->expects($this->once()) |
|
94 | + ->method('checkCoreUpdate'); |
|
95 | + $job->expects($this->once()) |
|
96 | + ->method('checkAppUpdates'); |
|
97 | + |
|
98 | + $this->config->expects(self::exactly(2)) |
|
99 | + ->method('getSystemValueBool') |
|
100 | + ->willReturnMap([ |
|
101 | + ['debug', false, true], |
|
102 | + ['has_internet_connection', true, true], |
|
103 | + ]); |
|
104 | + self::invokePrivate($job, 'run', [null]); |
|
105 | + } |
|
106 | + |
|
107 | + public function testRunNoInternet(): void { |
|
108 | + $job = $this->getJob([ |
|
109 | + 'checkCoreUpdate', |
|
110 | + 'checkAppUpdates', |
|
111 | + ]); |
|
112 | + |
|
113 | + $job->expects($this->never()) |
|
114 | + ->method('checkCoreUpdate'); |
|
115 | + $job->expects($this->never()) |
|
116 | + ->method('checkAppUpdates'); |
|
117 | + |
|
118 | + $this->config |
|
119 | + ->expects(self::once()) |
|
120 | + ->method('getSystemValueBool') |
|
121 | + ->with('has_internet_connection', true) |
|
122 | + ->willReturn(false); |
|
123 | + |
|
124 | + self::invokePrivate($job, 'run', [null]); |
|
125 | + } |
|
126 | + |
|
127 | + public static function dataCheckCoreUpdate(): array { |
|
128 | + return [ |
|
129 | + ['daily', null, null, null, null], |
|
130 | + ['git', null, null, null, null], |
|
131 | + ['beta', [], null, null, null], |
|
132 | + ['beta', false, false, null, null], |
|
133 | + ['beta', false, false, null, 13], |
|
134 | + ['beta', [ |
|
135 | + 'version' => '9.2.0', |
|
136 | + 'versionstring' => 'Nextcloud 11.0.0', |
|
137 | + ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
138 | + ['stable', [], null, null, null], |
|
139 | + ['stable', false, false, null, null], |
|
140 | + ['stable', false, false, null, 6], |
|
141 | + ['stable', [ |
|
142 | + 'version' => '9.2.0', |
|
143 | + 'versionstring' => 'Nextcloud 11.0.0', |
|
144 | + ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
145 | + ['production', [], null, null, null], |
|
146 | + ['production', false, false, null, null], |
|
147 | + ['production', false, false, null, 2], |
|
148 | + ['production', [ |
|
149 | + 'version' => '9.2.0', |
|
150 | + 'versionstring' => 'Nextcloud 11.0.0', |
|
151 | + ], '9.2.0', 'Nextcloud 11.0.0', null], |
|
152 | + ]; |
|
153 | + } |
|
154 | + |
|
155 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckCoreUpdate')] |
|
156 | + public function testCheckCoreUpdate(string $channel, mixed $versionCheck, mixed $version, ?string $readableVersion, ?int $errorDays): void { |
|
157 | + $job = $this->getJob([ |
|
158 | + 'createNotifications', |
|
159 | + 'clearErrorNotifications', |
|
160 | + 'sendErrorNotifications', |
|
161 | + ]); |
|
162 | + |
|
163 | + $this->serverVersion->expects($this->once()) |
|
164 | + ->method('getChannel') |
|
165 | + ->willReturn($channel); |
|
166 | + |
|
167 | + if ($versionCheck === null) { |
|
168 | + $this->versionCheck->expects($this->never()) |
|
169 | + ->method('check'); |
|
170 | + } else { |
|
171 | + $this->versionCheck->expects($this->once()) |
|
172 | + ->method('check') |
|
173 | + ->willReturn($versionCheck); |
|
174 | + } |
|
175 | + |
|
176 | + if ($version === null) { |
|
177 | + $job->expects($this->never()) |
|
178 | + ->method('createNotifications'); |
|
179 | + $job->expects($versionCheck === null ? $this->never() : $this->once()) |
|
180 | + ->method('clearErrorNotifications'); |
|
181 | + } elseif ($version === false) { |
|
182 | + $job->expects($this->never()) |
|
183 | + ->method('createNotifications'); |
|
184 | + $job->expects($this->never()) |
|
185 | + ->method('clearErrorNotifications'); |
|
186 | + |
|
187 | + $this->appConfig->expects($this->once()) |
|
188 | + ->method('getAppValueInt') |
|
189 | + ->willReturn($errorDays ?? 0); |
|
190 | + $this->appConfig->expects($this->once()) |
|
191 | + ->method('setAppValueInt') |
|
192 | + ->with('update_check_errors', $errorDays + 1); |
|
193 | + $job->expects($errorDays !== null ? $this->once() : $this->never()) |
|
194 | + ->method('sendErrorNotifications') |
|
195 | + ->with($errorDays + 1); |
|
196 | + } else { |
|
197 | + $this->appConfig->expects($this->once()) |
|
198 | + ->method('setAppValueInt') |
|
199 | + ->with('update_check_errors', 0); |
|
200 | + $job->expects($this->once()) |
|
201 | + ->method('clearErrorNotifications'); |
|
202 | + $job->expects($this->once()) |
|
203 | + ->method('createNotifications') |
|
204 | + ->with('core', $version, $readableVersion); |
|
205 | + } |
|
206 | + |
|
207 | + $this->config->expects(self::any()) |
|
208 | + ->method('getSystemValueBool') |
|
209 | + ->willReturnMap([ |
|
210 | + ['updatechecker', true, true], |
|
211 | + ['has_internet_connection', true, true], |
|
212 | + ]); |
|
213 | + |
|
214 | + self::invokePrivate($job, 'checkCoreUpdate'); |
|
215 | + } |
|
216 | + |
|
217 | + public static function dataCheckAppUpdates(): array { |
|
218 | + return [ |
|
219 | + [ |
|
220 | + ['app1', 'app2'], |
|
221 | + [ |
|
222 | + ['app1', false], |
|
223 | + ['app2', '1.9.2'], |
|
224 | + ], |
|
225 | + [ |
|
226 | + ['app2', '1.9.2', ''], |
|
227 | + ], |
|
228 | + ], |
|
229 | + ]; |
|
230 | + } |
|
231 | + |
|
232 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckAppUpdates')] |
|
233 | + public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications): void { |
|
234 | + $job = $this->getJob([ |
|
235 | + 'isUpdateAvailable', |
|
236 | + 'createNotifications', |
|
237 | + ]); |
|
238 | + |
|
239 | + $this->appManager->expects($this->once()) |
|
240 | + ->method('getEnabledApps') |
|
241 | + ->willReturn($apps); |
|
242 | + |
|
243 | + $job->expects($this->exactly(\count($apps))) |
|
244 | + ->method('isUpdateAvailable') |
|
245 | + ->willReturnMap($isUpdateAvailable); |
|
246 | + |
|
247 | + $i = 0; |
|
248 | + $job->expects($this->exactly(\count($notifications))) |
|
249 | + ->method('createNotifications') |
|
250 | + ->willReturnCallback(function () use ($notifications, &$i): void { |
|
251 | + $this->assertEquals($notifications[$i], func_get_args()); |
|
252 | + $i++; |
|
253 | + }); |
|
254 | + |
|
255 | + |
|
256 | + self::invokePrivate($job, 'checkAppUpdates'); |
|
257 | + } |
|
258 | + |
|
259 | + public static function dataCreateNotifications(): array { |
|
260 | + return [ |
|
261 | + ['app1', '1.0.0', '1.0.0', false, false, null, null], |
|
262 | + ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]], |
|
263 | + ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]], |
|
264 | + ]; |
|
265 | + } |
|
266 | + |
|
267 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataCreateNotifications')] |
|
268 | + public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
269 | + $job = $this->getJob([ |
|
270 | + 'deleteOutdatedNotifications', |
|
271 | + 'getUsersToNotify', |
|
272 | + ]); |
|
273 | + |
|
274 | + $this->appConfig->expects($this->once()) |
|
275 | + ->method('getAppValueString') |
|
276 | + ->with($app, '') |
|
277 | + ->willReturn($lastNotification ?: ''); |
|
278 | + |
|
279 | + if ($lastNotification !== $version) { |
|
280 | + $this->appConfig->expects($this->once()) |
|
281 | + ->method('setAppValueString') |
|
282 | + ->with($app, $version); |
|
283 | + } |
|
284 | + |
|
285 | + if ($callDelete === false) { |
|
286 | + $job->expects($this->never()) |
|
287 | + ->method('deleteOutdatedNotifications'); |
|
288 | + } else { |
|
289 | + $job->expects($this->once()) |
|
290 | + ->method('deleteOutdatedNotifications') |
|
291 | + ->with($app, $callDelete); |
|
292 | + } |
|
293 | + |
|
294 | + if ($users === null) { |
|
295 | + $job->expects($this->never()) |
|
296 | + ->method('getUsersToNotify'); |
|
297 | + } else { |
|
298 | + $job->expects($this->once()) |
|
299 | + ->method('getUsersToNotify') |
|
300 | + ->willReturn($users); |
|
301 | + } |
|
302 | + |
|
303 | + if ($createNotification) { |
|
304 | + $notification = $this->createMock(INotification::class); |
|
305 | + $notification->expects($this->once()) |
|
306 | + ->method('setApp') |
|
307 | + ->with('updatenotification') |
|
308 | + ->willReturnSelf(); |
|
309 | + $notification->expects($this->once()) |
|
310 | + ->method('setDateTime') |
|
311 | + ->willReturnSelf(); |
|
312 | + $notification->expects($this->once()) |
|
313 | + ->method('setObject') |
|
314 | + ->with($app, $version) |
|
315 | + ->willReturnSelf(); |
|
316 | + $notification->expects($this->once()) |
|
317 | + ->method('setSubject') |
|
318 | + ->with('update_available') |
|
319 | + ->willReturnSelf(); |
|
320 | + |
|
321 | + if ($userNotifications !== null) { |
|
322 | + $notification->expects($this->exactly(\count($userNotifications))) |
|
323 | + ->method('setUser') |
|
324 | + ->willReturnSelf(); |
|
325 | + |
|
326 | + $this->notificationManager->expects($this->exactly(\count($userNotifications))) |
|
327 | + ->method('notify'); |
|
328 | + } |
|
329 | + |
|
330 | + $this->notificationManager->expects($this->once()) |
|
331 | + ->method('createNotification') |
|
332 | + ->willReturn($notification); |
|
333 | + } else { |
|
334 | + $this->notificationManager->expects($this->never()) |
|
335 | + ->method('createNotification'); |
|
336 | + } |
|
337 | + |
|
338 | + self::invokePrivate($job, 'createNotifications', [$app, $version]); |
|
339 | + } |
|
340 | + |
|
341 | + public static function dataGetUsersToNotify(): array { |
|
342 | + return [ |
|
343 | + [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']], |
|
344 | + [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']], |
|
345 | + ]; |
|
346 | + } |
|
347 | + |
|
348 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetUsersToNotify')] |
|
349 | + public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected): void { |
|
350 | + $job = $this->getJob(); |
|
351 | + |
|
352 | + $this->appConfig->expects($this->once()) |
|
353 | + ->method('getAppValueArray') |
|
354 | + ->with('notify_groups', ['admin']) |
|
355 | + ->willReturn($groups); |
|
356 | + |
|
357 | + $groupMap = []; |
|
358 | + foreach ($groupUsers as $gid => $uids) { |
|
359 | + if ($uids === null) { |
|
360 | + $group = null; |
|
361 | + } else { |
|
362 | + $group = $this->getGroup($gid); |
|
363 | + $group->expects($this->any()) |
|
364 | + ->method('getUsers') |
|
365 | + ->willReturn($this->getUsers($uids)); |
|
366 | + } |
|
367 | + $groupMap[] = [$gid, $group]; |
|
368 | + } |
|
369 | + $this->groupManager->expects($this->exactly(\count($groups))) |
|
370 | + ->method('get') |
|
371 | + ->willReturnMap($groupMap); |
|
372 | + |
|
373 | + $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
374 | + $this->assertEquals($expected, $result); |
|
375 | + |
|
376 | + // Test caching |
|
377 | + $result = self::invokePrivate($job, 'getUsersToNotify'); |
|
378 | + $this->assertEquals($expected, $result); |
|
379 | + } |
|
380 | + |
|
381 | + public static function dataDeleteOutdatedNotifications(): array { |
|
382 | + return [ |
|
383 | + ['app1', '1.1.0'], |
|
384 | + ['app2', '1.2.0'], |
|
385 | + ]; |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * @param string $app |
|
390 | + * @param string $version |
|
391 | + */ |
|
392 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteOutdatedNotifications')] |
|
393 | + public function testDeleteOutdatedNotifications(string $app, string $version): void { |
|
394 | + $notification = $this->createMock(INotification::class); |
|
395 | + $notification->expects($this->once()) |
|
396 | + ->method('setApp') |
|
397 | + ->with('updatenotification') |
|
398 | + ->willReturnSelf(); |
|
399 | + $notification->expects($this->once()) |
|
400 | + ->method('setObject') |
|
401 | + ->with($app, $version) |
|
402 | + ->willReturnSelf(); |
|
403 | + |
|
404 | + $this->notificationManager->expects($this->once()) |
|
405 | + ->method('createNotification') |
|
406 | + ->willReturn($notification); |
|
407 | + $this->notificationManager->expects($this->once()) |
|
408 | + ->method('markProcessed') |
|
409 | + ->with($notification); |
|
410 | + |
|
411 | + $job = $this->getJob(); |
|
412 | + self::invokePrivate($job, 'deleteOutdatedNotifications', [$app, $version]); |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * @param string[] $userIds |
|
417 | + * @return IUser[]|MockObject[] |
|
418 | + */ |
|
419 | + protected function getUsers(array $userIds): array { |
|
420 | + $users = []; |
|
421 | + foreach ($userIds as $uid) { |
|
422 | + $user = $this->createMock(IUser::class); |
|
423 | + $user->expects($this->any()) |
|
424 | + ->method('getUID') |
|
425 | + ->willReturn($uid); |
|
426 | + $users[] = $user; |
|
427 | + } |
|
428 | + return $users; |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * @param string $gid |
|
433 | + * @return IGroup|MockObject |
|
434 | + */ |
|
435 | + protected function getGroup(string $gid) { |
|
436 | + $group = $this->createMock(IGroup::class); |
|
437 | + $group->expects($this->any()) |
|
438 | + ->method('getGID') |
|
439 | + ->willReturn($gid); |
|
440 | + return $group; |
|
441 | + } |
|
442 | 442 | } |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | $i = 0; |
248 | 248 | $job->expects($this->exactly(\count($notifications))) |
249 | 249 | ->method('createNotifications') |
250 | - ->willReturnCallback(function () use ($notifications, &$i): void { |
|
250 | + ->willReturnCallback(function() use ($notifications, &$i): void { |
|
251 | 251 | $this->assertEquals($notifications[$i], func_get_args()); |
252 | 252 | $i++; |
253 | 253 | }); |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | } |
266 | 266 | |
267 | 267 | #[\PHPUnit\Framework\Attributes\DataProvider('dataCreateNotifications')] |
268 | - public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
268 | + public function testCreateNotifications(string $app, string $version, string | false $lastNotification, string | false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { |
|
269 | 269 | $job = $this->getJob([ |
270 | 270 | 'deleteOutdatedNotifications', |
271 | 271 | 'getUsersToNotify', |
@@ -37,664 +37,664 @@ |
||
37 | 37 | * @group DB |
38 | 38 | */ |
39 | 39 | class TrashbinTest extends \Test\TestCase { |
40 | - public const TEST_TRASHBIN_USER1 = 'test-trashbin-user1'; |
|
41 | - public const TEST_TRASHBIN_USER2 = 'test-trashbin-user2'; |
|
42 | - |
|
43 | - private $trashRoot1; |
|
44 | - private $trashRoot2; |
|
45 | - |
|
46 | - private static $rememberRetentionObligation; |
|
47 | - private static bool $trashBinStatus; |
|
48 | - private View $rootView; |
|
49 | - |
|
50 | - public static function setUpBeforeClass(): void { |
|
51 | - parent::setUpBeforeClass(); |
|
52 | - |
|
53 | - $appManager = Server::get(IAppManager::class); |
|
54 | - self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin'); |
|
55 | - |
|
56 | - // reset backend |
|
57 | - Server::get(IUserManager::class)->clearBackends(); |
|
58 | - Server::get(IUserManager::class)->registerBackend(new Database()); |
|
59 | - |
|
60 | - // clear share hooks |
|
61 | - \OC_Hook::clear('OCP\\Share'); |
|
62 | - \OC::registerShareHooks(Server::get(SystemConfig::class)); |
|
63 | - |
|
64 | - // init files sharing |
|
65 | - new Application(); |
|
66 | - |
|
67 | - //disable encryption |
|
68 | - Server::get(IAppManager::class)->disableApp('encryption'); |
|
69 | - |
|
70 | - $config = Server::get(IConfig::class); |
|
71 | - //configure trashbin |
|
72 | - self::$rememberRetentionObligation = (string)$config->getSystemValue('trashbin_retention_obligation', Expiration::DEFAULT_RETENTION_OBLIGATION); |
|
73 | - /** @var Expiration $expiration */ |
|
74 | - $expiration = Server::get(Expiration::class); |
|
75 | - $expiration->setRetentionObligation('auto, 2'); |
|
76 | - |
|
77 | - // register trashbin hooks |
|
78 | - $trashbinApp = new TrashbinApplication(); |
|
79 | - $trashbinApp->boot(new BootContext(new DIContainer('', [], \OC::$server))); |
|
80 | - |
|
81 | - // create test user |
|
82 | - self::loginHelper(self::TEST_TRASHBIN_USER2, true); |
|
83 | - self::loginHelper(self::TEST_TRASHBIN_USER1, true); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - public static function tearDownAfterClass(): void { |
|
88 | - // cleanup test user |
|
89 | - $user = Server::get(IUserManager::class)->get(self::TEST_TRASHBIN_USER1); |
|
90 | - if ($user !== null) { |
|
91 | - $user->delete(); |
|
92 | - } |
|
93 | - |
|
94 | - /** @var Expiration $expiration */ |
|
95 | - $expiration = Server::get(Expiration::class); |
|
96 | - $expiration->setRetentionObligation(self::$rememberRetentionObligation); |
|
97 | - |
|
98 | - \OC_Hook::clear(); |
|
99 | - |
|
100 | - Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); |
|
101 | - |
|
102 | - if (self::$trashBinStatus) { |
|
103 | - Server::get(IAppManager::class)->enableApp('files_trashbin'); |
|
104 | - } |
|
105 | - |
|
106 | - parent::tearDownAfterClass(); |
|
107 | - } |
|
108 | - |
|
109 | - protected function setUp(): void { |
|
110 | - parent::setUp(); |
|
111 | - |
|
112 | - Server::get(IAppManager::class)->enableApp('files_trashbin'); |
|
113 | - $config = Server::get(IConfig::class); |
|
114 | - $mockConfig = $this->getMockBuilder(AllConfig::class) |
|
115 | - ->onlyMethods(['getSystemValue']) |
|
116 | - ->setConstructorArgs([Server::get(SystemConfig::class)]) |
|
117 | - ->getMock(); |
|
118 | - $mockConfig->expects($this->any()) |
|
119 | - ->method('getSystemValue') |
|
120 | - ->willReturnCallback(static function ($key, $default) use ($config) { |
|
121 | - if ($key === 'filesystem_check_changes') { |
|
122 | - return Watcher::CHECK_ONCE; |
|
123 | - } else { |
|
124 | - return $config->getSystemValue($key, $default); |
|
125 | - } |
|
126 | - }); |
|
127 | - $this->overwriteService(AllConfig::class, $mockConfig); |
|
128 | - |
|
129 | - $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin'; |
|
130 | - $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin'; |
|
131 | - $this->rootView = new View(); |
|
132 | - self::loginHelper(self::TEST_TRASHBIN_USER1); |
|
133 | - } |
|
134 | - |
|
135 | - protected function tearDown(): void { |
|
136 | - $this->restoreService(AllConfig::class); |
|
137 | - // disable trashbin to be able to properly clean up |
|
138 | - Server::get(IAppManager::class)->disableApp('files_trashbin'); |
|
139 | - |
|
140 | - $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER1 . '/files'); |
|
141 | - $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER2 . '/files'); |
|
142 | - $this->rootView->deleteAll($this->trashRoot1); |
|
143 | - $this->rootView->deleteAll($this->trashRoot2); |
|
144 | - |
|
145 | - // clear trash table |
|
146 | - $connection = Server::get(IDBConnection::class); |
|
147 | - $connection->executeUpdate('DELETE FROM `*PREFIX*files_trash`'); |
|
148 | - |
|
149 | - parent::tearDown(); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * test expiration of files older then the max storage time defined for the trash |
|
154 | - */ |
|
155 | - public function testExpireOldFiles(): void { |
|
156 | - |
|
157 | - /** @var ITimeFactory $time */ |
|
158 | - $time = Server::get(ITimeFactory::class); |
|
159 | - $currentTime = $time->getTime(); |
|
160 | - $expireAt = $currentTime - 2 * 24 * 60 * 60; |
|
161 | - $expiredDate = $currentTime - 3 * 24 * 60 * 60; |
|
162 | - |
|
163 | - // create some files |
|
164 | - Filesystem::file_put_contents('file1.txt', 'file1'); |
|
165 | - Filesystem::file_put_contents('file2.txt', 'file2'); |
|
166 | - Filesystem::file_put_contents('file3.txt', 'file3'); |
|
167 | - |
|
168 | - // delete them so that they end up in the trash bin |
|
169 | - Filesystem::unlink('file1.txt'); |
|
170 | - Filesystem::unlink('file2.txt'); |
|
171 | - Filesystem::unlink('file3.txt'); |
|
172 | - |
|
173 | - //make sure that files are in the trash bin |
|
174 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); |
|
175 | - $this->assertSame(3, count($filesInTrash)); |
|
176 | - |
|
177 | - // every second file will get a date in the past so that it will get expired |
|
178 | - $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate); |
|
179 | - |
|
180 | - $testClass = new TrashbinForTesting(); |
|
181 | - [$sizeOfDeletedFiles, $count] = $testClass->dummyDeleteExpiredFiles($manipulatedList, $expireAt); |
|
182 | - |
|
183 | - $this->assertSame(10, $sizeOfDeletedFiles); |
|
184 | - $this->assertSame(2, $count); |
|
185 | - |
|
186 | - // only file2.txt should be left |
|
187 | - $remainingFiles = array_slice($manipulatedList, $count); |
|
188 | - $this->assertCount(1, $remainingFiles); |
|
189 | - $remainingFile = reset($remainingFiles); |
|
190 | - // TODO: failing test |
|
191 | - #$this->assertSame('file2.txt', $remainingFile['name']); |
|
192 | - |
|
193 | - // check that file1.txt and file3.txt was really deleted |
|
194 | - $newTrashContent = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); |
|
195 | - $this->assertCount(1, $newTrashContent); |
|
196 | - $element = reset($newTrashContent); |
|
197 | - // TODO: failing test |
|
198 | - #$this->assertSame('file2.txt', $element['name']); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * test expiration of files older then the max storage time defined for the trash |
|
203 | - * in this test we delete a shared file and check if both trash bins, the one from |
|
204 | - * the owner of the file and the one from the user who deleted the file get expired |
|
205 | - * correctly |
|
206 | - */ |
|
207 | - public function testExpireOldFilesShared(): void { |
|
208 | - $currentTime = time(); |
|
209 | - $folder = 'trashTest-' . $currentTime . '/'; |
|
210 | - $expiredDate = $currentTime - 3 * 24 * 60 * 60; |
|
211 | - |
|
212 | - // create some files |
|
213 | - Filesystem::mkdir($folder); |
|
214 | - Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1'); |
|
215 | - Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2'); |
|
216 | - Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3'); |
|
217 | - Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4'); |
|
218 | - |
|
219 | - //share user1-4.txt with user2 |
|
220 | - $node = \OC::$server->getUserFolder(self::TEST_TRASHBIN_USER1)->get($folder); |
|
221 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
222 | - $share->setShareType(IShare::TYPE_USER) |
|
223 | - ->setNode($node) |
|
224 | - ->setSharedBy(self::TEST_TRASHBIN_USER1) |
|
225 | - ->setSharedWith(self::TEST_TRASHBIN_USER2) |
|
226 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
227 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
228 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_TRASHBIN_USER2); |
|
229 | - |
|
230 | - // delete them so that they end up in the trash bin |
|
231 | - Filesystem::unlink($folder . 'user1-1.txt'); |
|
232 | - Filesystem::unlink($folder . 'user1-2.txt'); |
|
233 | - Filesystem::unlink($folder . 'user1-3.txt'); |
|
234 | - |
|
235 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); |
|
236 | - $this->assertSame(3, count($filesInTrash)); |
|
237 | - |
|
238 | - // every second file will get a date in the past so that it will get expired |
|
239 | - $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate); |
|
240 | - |
|
241 | - // login as user2 |
|
242 | - self::loginHelper(self::TEST_TRASHBIN_USER2); |
|
243 | - |
|
244 | - $this->assertTrue(Filesystem::file_exists($folder . 'user1-4.txt')); |
|
245 | - |
|
246 | - // create some files |
|
247 | - Filesystem::file_put_contents('user2-1.txt', 'file1'); |
|
248 | - Filesystem::file_put_contents('user2-2.txt', 'file2'); |
|
249 | - |
|
250 | - // delete them so that they end up in the trash bin |
|
251 | - Filesystem::unlink('user2-1.txt'); |
|
252 | - Filesystem::unlink('user2-2.txt'); |
|
253 | - |
|
254 | - $filesInTrashUser2 = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name'); |
|
255 | - $this->assertSame(2, count($filesInTrashUser2)); |
|
256 | - |
|
257 | - // every second file will get a date in the past so that it will get expired |
|
258 | - $this->manipulateDeleteTime($filesInTrashUser2, $this->trashRoot2, $expiredDate); |
|
259 | - |
|
260 | - Filesystem::unlink($folder . 'user1-4.txt'); |
|
261 | - |
|
262 | - $this->runCommands(); |
|
263 | - |
|
264 | - $filesInTrashUser2AfterDelete = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2); |
|
265 | - |
|
266 | - // user2-1.txt should have been expired |
|
267 | - $this->verifyArray($filesInTrashUser2AfterDelete, ['user2-2.txt', 'user1-4.txt']); |
|
268 | - |
|
269 | - self::loginHelper(self::TEST_TRASHBIN_USER1); |
|
270 | - |
|
271 | - // user1-1.txt and user1-3.txt should have been expired |
|
272 | - $filesInTrashUser1AfterDelete = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); |
|
273 | - |
|
274 | - $this->verifyArray($filesInTrashUser1AfterDelete, ['user1-2.txt', 'user1-4.txt']); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * verify that the array contains the expected results |
|
279 | - * |
|
280 | - * @param FileInfo[] $result |
|
281 | - * @param string[] $expected |
|
282 | - */ |
|
283 | - private function verifyArray(array $result, array $expected): void { |
|
284 | - $this->assertCount(count($expected), $result); |
|
285 | - foreach ($expected as $expectedFile) { |
|
286 | - $found = false; |
|
287 | - foreach ($result as $fileInTrash) { |
|
288 | - if ($expectedFile === $fileInTrash['name']) { |
|
289 | - $found = true; |
|
290 | - break; |
|
291 | - } |
|
292 | - } |
|
293 | - if (!$found) { |
|
294 | - // if we didn't found the expected file, something went wrong |
|
295 | - $this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin"); |
|
296 | - } |
|
297 | - } |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * @param FileInfo[] $files |
|
302 | - */ |
|
303 | - private function manipulateDeleteTime(array $files, string $trashRoot, int $expireDate): array { |
|
304 | - $counter = 0; |
|
305 | - foreach ($files as &$file) { |
|
306 | - // modify every second file |
|
307 | - $counter = ($counter + 1) % 2; |
|
308 | - if ($counter === 1) { |
|
309 | - $source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime']; |
|
310 | - $target = Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); |
|
311 | - $this->rootView->rename($source, $target); |
|
312 | - $file['mtime'] = $expireDate; |
|
313 | - } |
|
314 | - } |
|
315 | - return \OCA\Files\Helper::sortFiles($files, 'mtime'); |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * test expiration of old files in the trash bin until the max size |
|
321 | - * of the trash bin is met again |
|
322 | - */ |
|
323 | - public function testExpireOldFilesUtilLimitsAreMet(): void { |
|
324 | - |
|
325 | - // create some files |
|
326 | - Filesystem::file_put_contents('file1.txt', 'file1'); |
|
327 | - Filesystem::file_put_contents('file2.txt', 'file2'); |
|
328 | - Filesystem::file_put_contents('file3.txt', 'file3'); |
|
329 | - |
|
330 | - // delete them so that they end up in the trash bin |
|
331 | - Filesystem::unlink('file3.txt'); |
|
332 | - sleep(1); // make sure that every file has a unique mtime |
|
333 | - Filesystem::unlink('file2.txt'); |
|
334 | - sleep(1); // make sure that every file has a unique mtime |
|
335 | - Filesystem::unlink('file1.txt'); |
|
336 | - |
|
337 | - //make sure that files are in the trash bin |
|
338 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
339 | - $this->assertSame(3, count($filesInTrash)); |
|
340 | - |
|
341 | - $testClass = new TrashbinForTesting(); |
|
342 | - $sizeOfDeletedFiles = $testClass->dummyDeleteFiles($filesInTrash, -8); |
|
343 | - |
|
344 | - // the two oldest files (file3.txt and file2.txt) should be deleted |
|
345 | - $this->assertSame(10, $sizeOfDeletedFiles); |
|
346 | - |
|
347 | - $newTrashContent = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); |
|
348 | - $this->assertSame(1, count($newTrashContent)); |
|
349 | - $element = reset($newTrashContent); |
|
350 | - $this->assertSame('file1.txt', $element['name']); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Test restoring a file |
|
355 | - */ |
|
356 | - public function testRestoreFileInRoot(): void { |
|
357 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
358 | - $file = $userFolder->newFile('file1.txt'); |
|
359 | - $file->putContent('foo'); |
|
360 | - |
|
361 | - $this->assertTrue($userFolder->nodeExists('file1.txt')); |
|
362 | - |
|
363 | - $file->delete(); |
|
364 | - |
|
365 | - $this->assertFalse($userFolder->nodeExists('file1.txt')); |
|
366 | - |
|
367 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
368 | - $this->assertCount(1, $filesInTrash); |
|
369 | - |
|
370 | - /** @var FileInfo */ |
|
371 | - $trashedFile = $filesInTrash[0]; |
|
372 | - |
|
373 | - $this->assertTrue( |
|
374 | - Trashbin::restore( |
|
375 | - 'file1.txt.d' . $trashedFile->getMtime(), |
|
376 | - $trashedFile->getName(), |
|
377 | - $trashedFile->getMtime() |
|
378 | - ) |
|
379 | - ); |
|
380 | - |
|
381 | - $file = $userFolder->get('file1.txt'); |
|
382 | - $this->assertEquals('foo', $file->getContent()); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Test restoring a file in subfolder |
|
387 | - */ |
|
388 | - public function testRestoreFileInSubfolder(): void { |
|
389 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
390 | - $folder = $userFolder->newFolder('folder'); |
|
391 | - $file = $folder->newFile('file1.txt'); |
|
392 | - $file->putContent('foo'); |
|
393 | - |
|
394 | - $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
395 | - |
|
396 | - $file->delete(); |
|
397 | - |
|
398 | - $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
399 | - |
|
400 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
401 | - $this->assertCount(1, $filesInTrash); |
|
402 | - |
|
403 | - /** @var FileInfo */ |
|
404 | - $trashedFile = $filesInTrash[0]; |
|
405 | - |
|
406 | - $this->assertTrue( |
|
407 | - Trashbin::restore( |
|
408 | - 'file1.txt.d' . $trashedFile->getMtime(), |
|
409 | - $trashedFile->getName(), |
|
410 | - $trashedFile->getMtime() |
|
411 | - ) |
|
412 | - ); |
|
413 | - |
|
414 | - $file = $userFolder->get('folder/file1.txt'); |
|
415 | - $this->assertEquals('foo', $file->getContent()); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Test restoring a folder |
|
420 | - */ |
|
421 | - public function testRestoreFolder(): void { |
|
422 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
423 | - $folder = $userFolder->newFolder('folder'); |
|
424 | - $file = $folder->newFile('file1.txt'); |
|
425 | - $file->putContent('foo'); |
|
426 | - |
|
427 | - $this->assertTrue($userFolder->nodeExists('folder')); |
|
428 | - |
|
429 | - $folder->delete(); |
|
430 | - |
|
431 | - $this->assertFalse($userFolder->nodeExists('folder')); |
|
432 | - |
|
433 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
434 | - $this->assertCount(1, $filesInTrash); |
|
40 | + public const TEST_TRASHBIN_USER1 = 'test-trashbin-user1'; |
|
41 | + public const TEST_TRASHBIN_USER2 = 'test-trashbin-user2'; |
|
42 | + |
|
43 | + private $trashRoot1; |
|
44 | + private $trashRoot2; |
|
45 | + |
|
46 | + private static $rememberRetentionObligation; |
|
47 | + private static bool $trashBinStatus; |
|
48 | + private View $rootView; |
|
49 | + |
|
50 | + public static function setUpBeforeClass(): void { |
|
51 | + parent::setUpBeforeClass(); |
|
52 | + |
|
53 | + $appManager = Server::get(IAppManager::class); |
|
54 | + self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin'); |
|
55 | + |
|
56 | + // reset backend |
|
57 | + Server::get(IUserManager::class)->clearBackends(); |
|
58 | + Server::get(IUserManager::class)->registerBackend(new Database()); |
|
59 | + |
|
60 | + // clear share hooks |
|
61 | + \OC_Hook::clear('OCP\\Share'); |
|
62 | + \OC::registerShareHooks(Server::get(SystemConfig::class)); |
|
63 | + |
|
64 | + // init files sharing |
|
65 | + new Application(); |
|
66 | + |
|
67 | + //disable encryption |
|
68 | + Server::get(IAppManager::class)->disableApp('encryption'); |
|
69 | + |
|
70 | + $config = Server::get(IConfig::class); |
|
71 | + //configure trashbin |
|
72 | + self::$rememberRetentionObligation = (string)$config->getSystemValue('trashbin_retention_obligation', Expiration::DEFAULT_RETENTION_OBLIGATION); |
|
73 | + /** @var Expiration $expiration */ |
|
74 | + $expiration = Server::get(Expiration::class); |
|
75 | + $expiration->setRetentionObligation('auto, 2'); |
|
76 | + |
|
77 | + // register trashbin hooks |
|
78 | + $trashbinApp = new TrashbinApplication(); |
|
79 | + $trashbinApp->boot(new BootContext(new DIContainer('', [], \OC::$server))); |
|
80 | + |
|
81 | + // create test user |
|
82 | + self::loginHelper(self::TEST_TRASHBIN_USER2, true); |
|
83 | + self::loginHelper(self::TEST_TRASHBIN_USER1, true); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + public static function tearDownAfterClass(): void { |
|
88 | + // cleanup test user |
|
89 | + $user = Server::get(IUserManager::class)->get(self::TEST_TRASHBIN_USER1); |
|
90 | + if ($user !== null) { |
|
91 | + $user->delete(); |
|
92 | + } |
|
93 | + |
|
94 | + /** @var Expiration $expiration */ |
|
95 | + $expiration = Server::get(Expiration::class); |
|
96 | + $expiration->setRetentionObligation(self::$rememberRetentionObligation); |
|
97 | + |
|
98 | + \OC_Hook::clear(); |
|
99 | + |
|
100 | + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); |
|
101 | + |
|
102 | + if (self::$trashBinStatus) { |
|
103 | + Server::get(IAppManager::class)->enableApp('files_trashbin'); |
|
104 | + } |
|
105 | + |
|
106 | + parent::tearDownAfterClass(); |
|
107 | + } |
|
108 | + |
|
109 | + protected function setUp(): void { |
|
110 | + parent::setUp(); |
|
111 | + |
|
112 | + Server::get(IAppManager::class)->enableApp('files_trashbin'); |
|
113 | + $config = Server::get(IConfig::class); |
|
114 | + $mockConfig = $this->getMockBuilder(AllConfig::class) |
|
115 | + ->onlyMethods(['getSystemValue']) |
|
116 | + ->setConstructorArgs([Server::get(SystemConfig::class)]) |
|
117 | + ->getMock(); |
|
118 | + $mockConfig->expects($this->any()) |
|
119 | + ->method('getSystemValue') |
|
120 | + ->willReturnCallback(static function ($key, $default) use ($config) { |
|
121 | + if ($key === 'filesystem_check_changes') { |
|
122 | + return Watcher::CHECK_ONCE; |
|
123 | + } else { |
|
124 | + return $config->getSystemValue($key, $default); |
|
125 | + } |
|
126 | + }); |
|
127 | + $this->overwriteService(AllConfig::class, $mockConfig); |
|
128 | + |
|
129 | + $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin'; |
|
130 | + $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin'; |
|
131 | + $this->rootView = new View(); |
|
132 | + self::loginHelper(self::TEST_TRASHBIN_USER1); |
|
133 | + } |
|
134 | + |
|
135 | + protected function tearDown(): void { |
|
136 | + $this->restoreService(AllConfig::class); |
|
137 | + // disable trashbin to be able to properly clean up |
|
138 | + Server::get(IAppManager::class)->disableApp('files_trashbin'); |
|
139 | + |
|
140 | + $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER1 . '/files'); |
|
141 | + $this->rootView->deleteAll('/' . self::TEST_TRASHBIN_USER2 . '/files'); |
|
142 | + $this->rootView->deleteAll($this->trashRoot1); |
|
143 | + $this->rootView->deleteAll($this->trashRoot2); |
|
144 | + |
|
145 | + // clear trash table |
|
146 | + $connection = Server::get(IDBConnection::class); |
|
147 | + $connection->executeUpdate('DELETE FROM `*PREFIX*files_trash`'); |
|
148 | + |
|
149 | + parent::tearDown(); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * test expiration of files older then the max storage time defined for the trash |
|
154 | + */ |
|
155 | + public function testExpireOldFiles(): void { |
|
156 | + |
|
157 | + /** @var ITimeFactory $time */ |
|
158 | + $time = Server::get(ITimeFactory::class); |
|
159 | + $currentTime = $time->getTime(); |
|
160 | + $expireAt = $currentTime - 2 * 24 * 60 * 60; |
|
161 | + $expiredDate = $currentTime - 3 * 24 * 60 * 60; |
|
162 | + |
|
163 | + // create some files |
|
164 | + Filesystem::file_put_contents('file1.txt', 'file1'); |
|
165 | + Filesystem::file_put_contents('file2.txt', 'file2'); |
|
166 | + Filesystem::file_put_contents('file3.txt', 'file3'); |
|
167 | + |
|
168 | + // delete them so that they end up in the trash bin |
|
169 | + Filesystem::unlink('file1.txt'); |
|
170 | + Filesystem::unlink('file2.txt'); |
|
171 | + Filesystem::unlink('file3.txt'); |
|
172 | + |
|
173 | + //make sure that files are in the trash bin |
|
174 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); |
|
175 | + $this->assertSame(3, count($filesInTrash)); |
|
176 | + |
|
177 | + // every second file will get a date in the past so that it will get expired |
|
178 | + $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate); |
|
179 | + |
|
180 | + $testClass = new TrashbinForTesting(); |
|
181 | + [$sizeOfDeletedFiles, $count] = $testClass->dummyDeleteExpiredFiles($manipulatedList, $expireAt); |
|
182 | + |
|
183 | + $this->assertSame(10, $sizeOfDeletedFiles); |
|
184 | + $this->assertSame(2, $count); |
|
185 | + |
|
186 | + // only file2.txt should be left |
|
187 | + $remainingFiles = array_slice($manipulatedList, $count); |
|
188 | + $this->assertCount(1, $remainingFiles); |
|
189 | + $remainingFile = reset($remainingFiles); |
|
190 | + // TODO: failing test |
|
191 | + #$this->assertSame('file2.txt', $remainingFile['name']); |
|
192 | + |
|
193 | + // check that file1.txt and file3.txt was really deleted |
|
194 | + $newTrashContent = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); |
|
195 | + $this->assertCount(1, $newTrashContent); |
|
196 | + $element = reset($newTrashContent); |
|
197 | + // TODO: failing test |
|
198 | + #$this->assertSame('file2.txt', $element['name']); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * test expiration of files older then the max storage time defined for the trash |
|
203 | + * in this test we delete a shared file and check if both trash bins, the one from |
|
204 | + * the owner of the file and the one from the user who deleted the file get expired |
|
205 | + * correctly |
|
206 | + */ |
|
207 | + public function testExpireOldFilesShared(): void { |
|
208 | + $currentTime = time(); |
|
209 | + $folder = 'trashTest-' . $currentTime . '/'; |
|
210 | + $expiredDate = $currentTime - 3 * 24 * 60 * 60; |
|
211 | + |
|
212 | + // create some files |
|
213 | + Filesystem::mkdir($folder); |
|
214 | + Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1'); |
|
215 | + Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2'); |
|
216 | + Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3'); |
|
217 | + Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4'); |
|
218 | + |
|
219 | + //share user1-4.txt with user2 |
|
220 | + $node = \OC::$server->getUserFolder(self::TEST_TRASHBIN_USER1)->get($folder); |
|
221 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
222 | + $share->setShareType(IShare::TYPE_USER) |
|
223 | + ->setNode($node) |
|
224 | + ->setSharedBy(self::TEST_TRASHBIN_USER1) |
|
225 | + ->setSharedWith(self::TEST_TRASHBIN_USER2) |
|
226 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
227 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
228 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_TRASHBIN_USER2); |
|
229 | + |
|
230 | + // delete them so that they end up in the trash bin |
|
231 | + Filesystem::unlink($folder . 'user1-1.txt'); |
|
232 | + Filesystem::unlink($folder . 'user1-2.txt'); |
|
233 | + Filesystem::unlink($folder . 'user1-3.txt'); |
|
234 | + |
|
235 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); |
|
236 | + $this->assertSame(3, count($filesInTrash)); |
|
237 | + |
|
238 | + // every second file will get a date in the past so that it will get expired |
|
239 | + $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate); |
|
240 | + |
|
241 | + // login as user2 |
|
242 | + self::loginHelper(self::TEST_TRASHBIN_USER2); |
|
243 | + |
|
244 | + $this->assertTrue(Filesystem::file_exists($folder . 'user1-4.txt')); |
|
245 | + |
|
246 | + // create some files |
|
247 | + Filesystem::file_put_contents('user2-1.txt', 'file1'); |
|
248 | + Filesystem::file_put_contents('user2-2.txt', 'file2'); |
|
249 | + |
|
250 | + // delete them so that they end up in the trash bin |
|
251 | + Filesystem::unlink('user2-1.txt'); |
|
252 | + Filesystem::unlink('user2-2.txt'); |
|
253 | + |
|
254 | + $filesInTrashUser2 = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name'); |
|
255 | + $this->assertSame(2, count($filesInTrashUser2)); |
|
256 | + |
|
257 | + // every second file will get a date in the past so that it will get expired |
|
258 | + $this->manipulateDeleteTime($filesInTrashUser2, $this->trashRoot2, $expiredDate); |
|
259 | + |
|
260 | + Filesystem::unlink($folder . 'user1-4.txt'); |
|
261 | + |
|
262 | + $this->runCommands(); |
|
263 | + |
|
264 | + $filesInTrashUser2AfterDelete = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2); |
|
265 | + |
|
266 | + // user2-1.txt should have been expired |
|
267 | + $this->verifyArray($filesInTrashUser2AfterDelete, ['user2-2.txt', 'user1-4.txt']); |
|
268 | + |
|
269 | + self::loginHelper(self::TEST_TRASHBIN_USER1); |
|
270 | + |
|
271 | + // user1-1.txt and user1-3.txt should have been expired |
|
272 | + $filesInTrashUser1AfterDelete = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); |
|
273 | + |
|
274 | + $this->verifyArray($filesInTrashUser1AfterDelete, ['user1-2.txt', 'user1-4.txt']); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * verify that the array contains the expected results |
|
279 | + * |
|
280 | + * @param FileInfo[] $result |
|
281 | + * @param string[] $expected |
|
282 | + */ |
|
283 | + private function verifyArray(array $result, array $expected): void { |
|
284 | + $this->assertCount(count($expected), $result); |
|
285 | + foreach ($expected as $expectedFile) { |
|
286 | + $found = false; |
|
287 | + foreach ($result as $fileInTrash) { |
|
288 | + if ($expectedFile === $fileInTrash['name']) { |
|
289 | + $found = true; |
|
290 | + break; |
|
291 | + } |
|
292 | + } |
|
293 | + if (!$found) { |
|
294 | + // if we didn't found the expected file, something went wrong |
|
295 | + $this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin"); |
|
296 | + } |
|
297 | + } |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * @param FileInfo[] $files |
|
302 | + */ |
|
303 | + private function manipulateDeleteTime(array $files, string $trashRoot, int $expireDate): array { |
|
304 | + $counter = 0; |
|
305 | + foreach ($files as &$file) { |
|
306 | + // modify every second file |
|
307 | + $counter = ($counter + 1) % 2; |
|
308 | + if ($counter === 1) { |
|
309 | + $source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime']; |
|
310 | + $target = Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); |
|
311 | + $this->rootView->rename($source, $target); |
|
312 | + $file['mtime'] = $expireDate; |
|
313 | + } |
|
314 | + } |
|
315 | + return \OCA\Files\Helper::sortFiles($files, 'mtime'); |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * test expiration of old files in the trash bin until the max size |
|
321 | + * of the trash bin is met again |
|
322 | + */ |
|
323 | + public function testExpireOldFilesUtilLimitsAreMet(): void { |
|
324 | + |
|
325 | + // create some files |
|
326 | + Filesystem::file_put_contents('file1.txt', 'file1'); |
|
327 | + Filesystem::file_put_contents('file2.txt', 'file2'); |
|
328 | + Filesystem::file_put_contents('file3.txt', 'file3'); |
|
329 | + |
|
330 | + // delete them so that they end up in the trash bin |
|
331 | + Filesystem::unlink('file3.txt'); |
|
332 | + sleep(1); // make sure that every file has a unique mtime |
|
333 | + Filesystem::unlink('file2.txt'); |
|
334 | + sleep(1); // make sure that every file has a unique mtime |
|
335 | + Filesystem::unlink('file1.txt'); |
|
336 | + |
|
337 | + //make sure that files are in the trash bin |
|
338 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
339 | + $this->assertSame(3, count($filesInTrash)); |
|
340 | + |
|
341 | + $testClass = new TrashbinForTesting(); |
|
342 | + $sizeOfDeletedFiles = $testClass->dummyDeleteFiles($filesInTrash, -8); |
|
343 | + |
|
344 | + // the two oldest files (file3.txt and file2.txt) should be deleted |
|
345 | + $this->assertSame(10, $sizeOfDeletedFiles); |
|
346 | + |
|
347 | + $newTrashContent = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); |
|
348 | + $this->assertSame(1, count($newTrashContent)); |
|
349 | + $element = reset($newTrashContent); |
|
350 | + $this->assertSame('file1.txt', $element['name']); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Test restoring a file |
|
355 | + */ |
|
356 | + public function testRestoreFileInRoot(): void { |
|
357 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
358 | + $file = $userFolder->newFile('file1.txt'); |
|
359 | + $file->putContent('foo'); |
|
360 | + |
|
361 | + $this->assertTrue($userFolder->nodeExists('file1.txt')); |
|
362 | + |
|
363 | + $file->delete(); |
|
364 | + |
|
365 | + $this->assertFalse($userFolder->nodeExists('file1.txt')); |
|
366 | + |
|
367 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
368 | + $this->assertCount(1, $filesInTrash); |
|
369 | + |
|
370 | + /** @var FileInfo */ |
|
371 | + $trashedFile = $filesInTrash[0]; |
|
372 | + |
|
373 | + $this->assertTrue( |
|
374 | + Trashbin::restore( |
|
375 | + 'file1.txt.d' . $trashedFile->getMtime(), |
|
376 | + $trashedFile->getName(), |
|
377 | + $trashedFile->getMtime() |
|
378 | + ) |
|
379 | + ); |
|
380 | + |
|
381 | + $file = $userFolder->get('file1.txt'); |
|
382 | + $this->assertEquals('foo', $file->getContent()); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Test restoring a file in subfolder |
|
387 | + */ |
|
388 | + public function testRestoreFileInSubfolder(): void { |
|
389 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
390 | + $folder = $userFolder->newFolder('folder'); |
|
391 | + $file = $folder->newFile('file1.txt'); |
|
392 | + $file->putContent('foo'); |
|
393 | + |
|
394 | + $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
395 | + |
|
396 | + $file->delete(); |
|
397 | + |
|
398 | + $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
399 | + |
|
400 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
401 | + $this->assertCount(1, $filesInTrash); |
|
402 | + |
|
403 | + /** @var FileInfo */ |
|
404 | + $trashedFile = $filesInTrash[0]; |
|
405 | + |
|
406 | + $this->assertTrue( |
|
407 | + Trashbin::restore( |
|
408 | + 'file1.txt.d' . $trashedFile->getMtime(), |
|
409 | + $trashedFile->getName(), |
|
410 | + $trashedFile->getMtime() |
|
411 | + ) |
|
412 | + ); |
|
413 | + |
|
414 | + $file = $userFolder->get('folder/file1.txt'); |
|
415 | + $this->assertEquals('foo', $file->getContent()); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Test restoring a folder |
|
420 | + */ |
|
421 | + public function testRestoreFolder(): void { |
|
422 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
423 | + $folder = $userFolder->newFolder('folder'); |
|
424 | + $file = $folder->newFile('file1.txt'); |
|
425 | + $file->putContent('foo'); |
|
426 | + |
|
427 | + $this->assertTrue($userFolder->nodeExists('folder')); |
|
428 | + |
|
429 | + $folder->delete(); |
|
430 | + |
|
431 | + $this->assertFalse($userFolder->nodeExists('folder')); |
|
432 | + |
|
433 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
434 | + $this->assertCount(1, $filesInTrash); |
|
435 | 435 | |
436 | - /** @var FileInfo */ |
|
437 | - $trashedFolder = $filesInTrash[0]; |
|
436 | + /** @var FileInfo */ |
|
437 | + $trashedFolder = $filesInTrash[0]; |
|
438 | 438 | |
439 | - $this->assertTrue( |
|
440 | - Trashbin::restore( |
|
441 | - 'folder.d' . $trashedFolder->getMtime(), |
|
442 | - $trashedFolder->getName(), |
|
443 | - $trashedFolder->getMtime() |
|
444 | - ) |
|
445 | - ); |
|
439 | + $this->assertTrue( |
|
440 | + Trashbin::restore( |
|
441 | + 'folder.d' . $trashedFolder->getMtime(), |
|
442 | + $trashedFolder->getName(), |
|
443 | + $trashedFolder->getMtime() |
|
444 | + ) |
|
445 | + ); |
|
446 | 446 | |
447 | - $file = $userFolder->get('folder/file1.txt'); |
|
448 | - $this->assertEquals('foo', $file->getContent()); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Test restoring a file from inside a trashed folder |
|
453 | - */ |
|
454 | - public function testRestoreFileFromTrashedSubfolder(): void { |
|
455 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
456 | - $folder = $userFolder->newFolder('folder'); |
|
457 | - $file = $folder->newFile('file1.txt'); |
|
458 | - $file->putContent('foo'); |
|
447 | + $file = $userFolder->get('folder/file1.txt'); |
|
448 | + $this->assertEquals('foo', $file->getContent()); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Test restoring a file from inside a trashed folder |
|
453 | + */ |
|
454 | + public function testRestoreFileFromTrashedSubfolder(): void { |
|
455 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
456 | + $folder = $userFolder->newFolder('folder'); |
|
457 | + $file = $folder->newFile('file1.txt'); |
|
458 | + $file->putContent('foo'); |
|
459 | 459 | |
460 | - $this->assertTrue($userFolder->nodeExists('folder')); |
|
460 | + $this->assertTrue($userFolder->nodeExists('folder')); |
|
461 | 461 | |
462 | - $folder->delete(); |
|
463 | - |
|
464 | - $this->assertFalse($userFolder->nodeExists('folder')); |
|
465 | - |
|
466 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
467 | - $this->assertCount(1, $filesInTrash); |
|
468 | - |
|
469 | - /** @var FileInfo */ |
|
470 | - $trashedFile = $filesInTrash[0]; |
|
471 | - |
|
472 | - $this->assertTrue( |
|
473 | - Trashbin::restore( |
|
474 | - 'folder.d' . $trashedFile->getMtime() . '/file1.txt', |
|
475 | - 'file1.txt', |
|
476 | - $trashedFile->getMtime() |
|
477 | - ) |
|
478 | - ); |
|
462 | + $folder->delete(); |
|
463 | + |
|
464 | + $this->assertFalse($userFolder->nodeExists('folder')); |
|
465 | + |
|
466 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
467 | + $this->assertCount(1, $filesInTrash); |
|
468 | + |
|
469 | + /** @var FileInfo */ |
|
470 | + $trashedFile = $filesInTrash[0]; |
|
471 | + |
|
472 | + $this->assertTrue( |
|
473 | + Trashbin::restore( |
|
474 | + 'folder.d' . $trashedFile->getMtime() . '/file1.txt', |
|
475 | + 'file1.txt', |
|
476 | + $trashedFile->getMtime() |
|
477 | + ) |
|
478 | + ); |
|
479 | 479 | |
480 | - $file = $userFolder->get('file1.txt'); |
|
481 | - $this->assertEquals('foo', $file->getContent()); |
|
482 | - } |
|
480 | + $file = $userFolder->get('file1.txt'); |
|
481 | + $this->assertEquals('foo', $file->getContent()); |
|
482 | + } |
|
483 | 483 | |
484 | - /** |
|
485 | - * Test restoring a file whenever the source folder was removed. |
|
486 | - * The file should then land in the root. |
|
487 | - */ |
|
488 | - public function testRestoreFileWithMissingSourceFolder(): void { |
|
489 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
490 | - $folder = $userFolder->newFolder('folder'); |
|
491 | - $file = $folder->newFile('file1.txt'); |
|
492 | - $file->putContent('foo'); |
|
484 | + /** |
|
485 | + * Test restoring a file whenever the source folder was removed. |
|
486 | + * The file should then land in the root. |
|
487 | + */ |
|
488 | + public function testRestoreFileWithMissingSourceFolder(): void { |
|
489 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
490 | + $folder = $userFolder->newFolder('folder'); |
|
491 | + $file = $folder->newFile('file1.txt'); |
|
492 | + $file->putContent('foo'); |
|
493 | 493 | |
494 | - $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
494 | + $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
495 | 495 | |
496 | - $file->delete(); |
|
496 | + $file->delete(); |
|
497 | 497 | |
498 | - $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
499 | - |
|
500 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
501 | - $this->assertCount(1, $filesInTrash); |
|
502 | - |
|
503 | - /** @var FileInfo */ |
|
504 | - $trashedFile = $filesInTrash[0]; |
|
505 | - |
|
506 | - // delete source folder |
|
507 | - $folder->delete(); |
|
508 | - |
|
509 | - $this->assertTrue( |
|
510 | - Trashbin::restore( |
|
511 | - 'file1.txt.d' . $trashedFile->getMtime(), |
|
512 | - $trashedFile->getName(), |
|
513 | - $trashedFile->getMtime() |
|
514 | - ) |
|
515 | - ); |
|
516 | - |
|
517 | - $file = $userFolder->get('file1.txt'); |
|
518 | - $this->assertEquals('foo', $file->getContent()); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Test restoring a file in the root folder whenever there is another file |
|
523 | - * with the same name in the root folder |
|
524 | - */ |
|
525 | - public function testRestoreFileDoesNotOverwriteExistingInRoot(): void { |
|
526 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
527 | - $file = $userFolder->newFile('file1.txt'); |
|
528 | - $file->putContent('foo'); |
|
529 | - |
|
530 | - $this->assertTrue($userFolder->nodeExists('file1.txt')); |
|
531 | - |
|
532 | - $file->delete(); |
|
533 | - |
|
534 | - $this->assertFalse($userFolder->nodeExists('file1.txt')); |
|
535 | - |
|
536 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
537 | - $this->assertCount(1, $filesInTrash); |
|
538 | - |
|
539 | - /** @var FileInfo */ |
|
540 | - $trashedFile = $filesInTrash[0]; |
|
541 | - |
|
542 | - // create another file |
|
543 | - $file = $userFolder->newFile('file1.txt'); |
|
544 | - $file->putContent('bar'); |
|
545 | - |
|
546 | - $this->assertTrue( |
|
547 | - Trashbin::restore( |
|
548 | - 'file1.txt.d' . $trashedFile->getMtime(), |
|
549 | - $trashedFile->getName(), |
|
550 | - $trashedFile->getMtime() |
|
551 | - ) |
|
552 | - ); |
|
553 | - |
|
554 | - $anotherFile = $userFolder->get('file1.txt'); |
|
555 | - $this->assertEquals('bar', $anotherFile->getContent()); |
|
556 | - |
|
557 | - $restoredFile = $userFolder->get('file1 (restored).txt'); |
|
558 | - $this->assertEquals('foo', $restoredFile->getContent()); |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Test restoring a file whenever there is another file |
|
563 | - * with the same name in the source folder |
|
564 | - */ |
|
565 | - public function testRestoreFileDoesNotOverwriteExistingInSubfolder(): void { |
|
566 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
567 | - $folder = $userFolder->newFolder('folder'); |
|
568 | - $file = $folder->newFile('file1.txt'); |
|
569 | - $file->putContent('foo'); |
|
570 | - |
|
571 | - $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
572 | - |
|
573 | - $file->delete(); |
|
574 | - |
|
575 | - $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
576 | - |
|
577 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
578 | - $this->assertCount(1, $filesInTrash); |
|
579 | - |
|
580 | - /** @var FileInfo */ |
|
581 | - $trashedFile = $filesInTrash[0]; |
|
582 | - |
|
583 | - // create another file |
|
584 | - $file = $folder->newFile('file1.txt'); |
|
585 | - $file->putContent('bar'); |
|
586 | - |
|
587 | - $this->assertTrue( |
|
588 | - Trashbin::restore( |
|
589 | - 'file1.txt.d' . $trashedFile->getMtime(), |
|
590 | - $trashedFile->getName(), |
|
591 | - $trashedFile->getMtime() |
|
592 | - ) |
|
593 | - ); |
|
594 | - |
|
595 | - $anotherFile = $userFolder->get('folder/file1.txt'); |
|
596 | - $this->assertEquals('bar', $anotherFile->getContent()); |
|
597 | - |
|
598 | - $restoredFile = $userFolder->get('folder/file1 (restored).txt'); |
|
599 | - $this->assertEquals('foo', $restoredFile->getContent()); |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * Test restoring a non-existing file from trashbin, returns false |
|
604 | - */ |
|
605 | - public function testRestoreUnexistingFile(): void { |
|
606 | - $this->assertFalse( |
|
607 | - Trashbin::restore( |
|
608 | - 'unexist.txt.d123456', |
|
609 | - 'unexist.txt', |
|
610 | - '123456' |
|
611 | - ) |
|
612 | - ); |
|
613 | - } |
|
614 | - |
|
615 | - /** |
|
616 | - * Test restoring a file into a read-only folder, will restore |
|
617 | - * the file to root instead |
|
618 | - */ |
|
619 | - public function testRestoreFileIntoReadOnlySourceFolder(): void { |
|
620 | - $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
621 | - $folder = $userFolder->newFolder('folder'); |
|
622 | - $file = $folder->newFile('file1.txt'); |
|
623 | - $file->putContent('foo'); |
|
624 | - |
|
625 | - $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
626 | - |
|
627 | - $file->delete(); |
|
628 | - |
|
629 | - $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
630 | - |
|
631 | - $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
632 | - $this->assertCount(1, $filesInTrash); |
|
633 | - |
|
634 | - /** @var FileInfo */ |
|
635 | - $trashedFile = $filesInTrash[0]; |
|
636 | - |
|
637 | - // delete source folder |
|
638 | - [$storage, $internalPath] = $this->rootView->resolvePath('/' . self::TEST_TRASHBIN_USER1 . '/files/folder'); |
|
639 | - if ($storage instanceof Local) { |
|
640 | - $folderAbsPath = $storage->getSourcePath($internalPath); |
|
641 | - // make folder read-only |
|
642 | - chmod($folderAbsPath, 0555); |
|
643 | - |
|
644 | - $this->assertTrue( |
|
645 | - Trashbin::restore( |
|
646 | - 'file1.txt.d' . $trashedFile->getMtime(), |
|
647 | - $trashedFile->getName(), |
|
648 | - $trashedFile->getMtime() |
|
649 | - ) |
|
650 | - ); |
|
651 | - |
|
652 | - $file = $userFolder->get('file1.txt'); |
|
653 | - $this->assertEquals('foo', $file->getContent()); |
|
654 | - |
|
655 | - chmod($folderAbsPath, 0755); |
|
656 | - } |
|
657 | - } |
|
658 | - |
|
659 | - /** |
|
660 | - * @param string $user |
|
661 | - * @param bool $create |
|
662 | - */ |
|
663 | - public static function loginHelper($user, $create = false) { |
|
664 | - if ($create) { |
|
665 | - try { |
|
666 | - Server::get(IUserManager::class)->createUser($user, $user); |
|
667 | - } catch (\Exception $e) { // catch username is already being used from previous aborted runs |
|
668 | - } |
|
669 | - } |
|
670 | - |
|
671 | - \OC_Util::tearDownFS(); |
|
672 | - \OC_User::setUserId(''); |
|
673 | - Filesystem::tearDown(); |
|
674 | - \OC_User::setUserId($user); |
|
675 | - \OC_Util::setupFS($user); |
|
676 | - Server::get(IRootFolder::class)->getUserFolder($user); |
|
677 | - } |
|
498 | + $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
499 | + |
|
500 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
501 | + $this->assertCount(1, $filesInTrash); |
|
502 | + |
|
503 | + /** @var FileInfo */ |
|
504 | + $trashedFile = $filesInTrash[0]; |
|
505 | + |
|
506 | + // delete source folder |
|
507 | + $folder->delete(); |
|
508 | + |
|
509 | + $this->assertTrue( |
|
510 | + Trashbin::restore( |
|
511 | + 'file1.txt.d' . $trashedFile->getMtime(), |
|
512 | + $trashedFile->getName(), |
|
513 | + $trashedFile->getMtime() |
|
514 | + ) |
|
515 | + ); |
|
516 | + |
|
517 | + $file = $userFolder->get('file1.txt'); |
|
518 | + $this->assertEquals('foo', $file->getContent()); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Test restoring a file in the root folder whenever there is another file |
|
523 | + * with the same name in the root folder |
|
524 | + */ |
|
525 | + public function testRestoreFileDoesNotOverwriteExistingInRoot(): void { |
|
526 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
527 | + $file = $userFolder->newFile('file1.txt'); |
|
528 | + $file->putContent('foo'); |
|
529 | + |
|
530 | + $this->assertTrue($userFolder->nodeExists('file1.txt')); |
|
531 | + |
|
532 | + $file->delete(); |
|
533 | + |
|
534 | + $this->assertFalse($userFolder->nodeExists('file1.txt')); |
|
535 | + |
|
536 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
537 | + $this->assertCount(1, $filesInTrash); |
|
538 | + |
|
539 | + /** @var FileInfo */ |
|
540 | + $trashedFile = $filesInTrash[0]; |
|
541 | + |
|
542 | + // create another file |
|
543 | + $file = $userFolder->newFile('file1.txt'); |
|
544 | + $file->putContent('bar'); |
|
545 | + |
|
546 | + $this->assertTrue( |
|
547 | + Trashbin::restore( |
|
548 | + 'file1.txt.d' . $trashedFile->getMtime(), |
|
549 | + $trashedFile->getName(), |
|
550 | + $trashedFile->getMtime() |
|
551 | + ) |
|
552 | + ); |
|
553 | + |
|
554 | + $anotherFile = $userFolder->get('file1.txt'); |
|
555 | + $this->assertEquals('bar', $anotherFile->getContent()); |
|
556 | + |
|
557 | + $restoredFile = $userFolder->get('file1 (restored).txt'); |
|
558 | + $this->assertEquals('foo', $restoredFile->getContent()); |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Test restoring a file whenever there is another file |
|
563 | + * with the same name in the source folder |
|
564 | + */ |
|
565 | + public function testRestoreFileDoesNotOverwriteExistingInSubfolder(): void { |
|
566 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
567 | + $folder = $userFolder->newFolder('folder'); |
|
568 | + $file = $folder->newFile('file1.txt'); |
|
569 | + $file->putContent('foo'); |
|
570 | + |
|
571 | + $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
572 | + |
|
573 | + $file->delete(); |
|
574 | + |
|
575 | + $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
576 | + |
|
577 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
578 | + $this->assertCount(1, $filesInTrash); |
|
579 | + |
|
580 | + /** @var FileInfo */ |
|
581 | + $trashedFile = $filesInTrash[0]; |
|
582 | + |
|
583 | + // create another file |
|
584 | + $file = $folder->newFile('file1.txt'); |
|
585 | + $file->putContent('bar'); |
|
586 | + |
|
587 | + $this->assertTrue( |
|
588 | + Trashbin::restore( |
|
589 | + 'file1.txt.d' . $trashedFile->getMtime(), |
|
590 | + $trashedFile->getName(), |
|
591 | + $trashedFile->getMtime() |
|
592 | + ) |
|
593 | + ); |
|
594 | + |
|
595 | + $anotherFile = $userFolder->get('folder/file1.txt'); |
|
596 | + $this->assertEquals('bar', $anotherFile->getContent()); |
|
597 | + |
|
598 | + $restoredFile = $userFolder->get('folder/file1 (restored).txt'); |
|
599 | + $this->assertEquals('foo', $restoredFile->getContent()); |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * Test restoring a non-existing file from trashbin, returns false |
|
604 | + */ |
|
605 | + public function testRestoreUnexistingFile(): void { |
|
606 | + $this->assertFalse( |
|
607 | + Trashbin::restore( |
|
608 | + 'unexist.txt.d123456', |
|
609 | + 'unexist.txt', |
|
610 | + '123456' |
|
611 | + ) |
|
612 | + ); |
|
613 | + } |
|
614 | + |
|
615 | + /** |
|
616 | + * Test restoring a file into a read-only folder, will restore |
|
617 | + * the file to root instead |
|
618 | + */ |
|
619 | + public function testRestoreFileIntoReadOnlySourceFolder(): void { |
|
620 | + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); |
|
621 | + $folder = $userFolder->newFolder('folder'); |
|
622 | + $file = $folder->newFile('file1.txt'); |
|
623 | + $file->putContent('foo'); |
|
624 | + |
|
625 | + $this->assertTrue($userFolder->nodeExists('folder/file1.txt')); |
|
626 | + |
|
627 | + $file->delete(); |
|
628 | + |
|
629 | + $this->assertFalse($userFolder->nodeExists('folder/file1.txt')); |
|
630 | + |
|
631 | + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); |
|
632 | + $this->assertCount(1, $filesInTrash); |
|
633 | + |
|
634 | + /** @var FileInfo */ |
|
635 | + $trashedFile = $filesInTrash[0]; |
|
636 | + |
|
637 | + // delete source folder |
|
638 | + [$storage, $internalPath] = $this->rootView->resolvePath('/' . self::TEST_TRASHBIN_USER1 . '/files/folder'); |
|
639 | + if ($storage instanceof Local) { |
|
640 | + $folderAbsPath = $storage->getSourcePath($internalPath); |
|
641 | + // make folder read-only |
|
642 | + chmod($folderAbsPath, 0555); |
|
643 | + |
|
644 | + $this->assertTrue( |
|
645 | + Trashbin::restore( |
|
646 | + 'file1.txt.d' . $trashedFile->getMtime(), |
|
647 | + $trashedFile->getName(), |
|
648 | + $trashedFile->getMtime() |
|
649 | + ) |
|
650 | + ); |
|
651 | + |
|
652 | + $file = $userFolder->get('file1.txt'); |
|
653 | + $this->assertEquals('foo', $file->getContent()); |
|
654 | + |
|
655 | + chmod($folderAbsPath, 0755); |
|
656 | + } |
|
657 | + } |
|
658 | + |
|
659 | + /** |
|
660 | + * @param string $user |
|
661 | + * @param bool $create |
|
662 | + */ |
|
663 | + public static function loginHelper($user, $create = false) { |
|
664 | + if ($create) { |
|
665 | + try { |
|
666 | + Server::get(IUserManager::class)->createUser($user, $user); |
|
667 | + } catch (\Exception $e) { // catch username is already being used from previous aborted runs |
|
668 | + } |
|
669 | + } |
|
670 | + |
|
671 | + \OC_Util::tearDownFS(); |
|
672 | + \OC_User::setUserId(''); |
|
673 | + Filesystem::tearDown(); |
|
674 | + \OC_User::setUserId($user); |
|
675 | + \OC_Util::setupFS($user); |
|
676 | + Server::get(IRootFolder::class)->getUserFolder($user); |
|
677 | + } |
|
678 | 678 | } |
679 | 679 | |
680 | 680 | |
681 | 681 | // just a dummy class to make protected methods available for testing |
682 | 682 | class TrashbinForTesting extends Trashbin { |
683 | 683 | |
684 | - /** |
|
685 | - * @param FileInfo[] $files |
|
686 | - * @param integer $limit |
|
687 | - */ |
|
688 | - public function dummyDeleteExpiredFiles($files) { |
|
689 | - // dummy value for $retention_obligation because it is not needed here |
|
690 | - return parent::deleteExpiredFiles($files, TrashbinTest::TEST_TRASHBIN_USER1); |
|
691 | - } |
|
692 | - |
|
693 | - /** |
|
694 | - * @param FileInfo[] $files |
|
695 | - * @param integer $availableSpace |
|
696 | - */ |
|
697 | - public function dummyDeleteFiles($files, $availableSpace) { |
|
698 | - return parent::deleteFiles($files, TrashbinTest::TEST_TRASHBIN_USER1, $availableSpace); |
|
699 | - } |
|
684 | + /** |
|
685 | + * @param FileInfo[] $files |
|
686 | + * @param integer $limit |
|
687 | + */ |
|
688 | + public function dummyDeleteExpiredFiles($files) { |
|
689 | + // dummy value for $retention_obligation because it is not needed here |
|
690 | + return parent::deleteExpiredFiles($files, TrashbinTest::TEST_TRASHBIN_USER1); |
|
691 | + } |
|
692 | + |
|
693 | + /** |
|
694 | + * @param FileInfo[] $files |
|
695 | + * @param integer $availableSpace |
|
696 | + */ |
|
697 | + public function dummyDeleteFiles($files, $availableSpace) { |
|
698 | + return parent::deleteFiles($files, TrashbinTest::TEST_TRASHBIN_USER1, $availableSpace); |
|
699 | + } |
|
700 | 700 | } |
@@ -20,51 +20,51 @@ |
||
20 | 20 | use Test\TestCase; |
21 | 21 | |
22 | 22 | class TrashbinPluginTest extends TestCase { |
23 | - private Server $server; |
|
23 | + private Server $server; |
|
24 | 24 | |
25 | - protected function setUp(): void { |
|
26 | - parent::setUp(); |
|
25 | + protected function setUp(): void { |
|
26 | + parent::setUp(); |
|
27 | 27 | |
28 | - $tree = $this->createMock(Tree::class); |
|
29 | - $this->server = new Server($tree); |
|
30 | - } |
|
28 | + $tree = $this->createMock(Tree::class); |
|
29 | + $this->server = new Server($tree); |
|
30 | + } |
|
31 | 31 | |
32 | - #[\PHPUnit\Framework\Attributes\DataProvider('quotaProvider')] |
|
33 | - public function testQuota(int $quota, int $fileSize, bool $expectedResult): void { |
|
34 | - $fileInfo = $this->createMock(ITrashItem::class); |
|
35 | - $fileInfo->method('getSize') |
|
36 | - ->willReturn($fileSize); |
|
32 | + #[\PHPUnit\Framework\Attributes\DataProvider('quotaProvider')] |
|
33 | + public function testQuota(int $quota, int $fileSize, bool $expectedResult): void { |
|
34 | + $fileInfo = $this->createMock(ITrashItem::class); |
|
35 | + $fileInfo->method('getSize') |
|
36 | + ->willReturn($fileSize); |
|
37 | 37 | |
38 | - $trashNode = $this->createMock(ITrash::class); |
|
39 | - $trashNode->method('getFileInfo') |
|
40 | - ->willReturn($fileInfo); |
|
38 | + $trashNode = $this->createMock(ITrash::class); |
|
39 | + $trashNode->method('getFileInfo') |
|
40 | + ->willReturn($fileInfo); |
|
41 | 41 | |
42 | - $restoreNode = $this->createMock(RestoreFolder::class); |
|
42 | + $restoreNode = $this->createMock(RestoreFolder::class); |
|
43 | 43 | |
44 | - $this->server->tree->method('getNodeForPath') |
|
45 | - ->willReturn($trashNode, $restoreNode); |
|
44 | + $this->server->tree->method('getNodeForPath') |
|
45 | + ->willReturn($trashNode, $restoreNode); |
|
46 | 46 | |
47 | - $previewManager = $this->createMock(IPreview::class); |
|
47 | + $previewManager = $this->createMock(IPreview::class); |
|
48 | 48 | |
49 | - $view = $this->createMock(View::class); |
|
50 | - $view->method('free_space') |
|
51 | - ->willReturn($quota); |
|
49 | + $view = $this->createMock(View::class); |
|
50 | + $view->method('free_space') |
|
51 | + ->willReturn($quota); |
|
52 | 52 | |
53 | - $plugin = new TrashbinPlugin($previewManager, $view); |
|
54 | - $plugin->initialize($this->server); |
|
53 | + $plugin = new TrashbinPlugin($previewManager, $view); |
|
54 | + $plugin->initialize($this->server); |
|
55 | 55 | |
56 | - $sourcePath = 'trashbin/test/trash/file1'; |
|
57 | - $destinationPath = 'trashbin/test/restore/file1'; |
|
58 | - $this->assertEquals($expectedResult, $plugin->beforeMove($sourcePath, $destinationPath)); |
|
59 | - } |
|
56 | + $sourcePath = 'trashbin/test/trash/file1'; |
|
57 | + $destinationPath = 'trashbin/test/restore/file1'; |
|
58 | + $this->assertEquals($expectedResult, $plugin->beforeMove($sourcePath, $destinationPath)); |
|
59 | + } |
|
60 | 60 | |
61 | - public static function quotaProvider(): array { |
|
62 | - return [ |
|
63 | - [ 1024, 512, true ], |
|
64 | - [ 512, 513, false ], |
|
65 | - [ FileInfo::SPACE_NOT_COMPUTED, 1024, true ], |
|
66 | - [ FileInfo::SPACE_UNKNOWN, 1024, true ], |
|
67 | - [ FileInfo::SPACE_UNLIMITED, 1024, true ] |
|
68 | - ]; |
|
69 | - } |
|
61 | + public static function quotaProvider(): array { |
|
62 | + return [ |
|
63 | + [ 1024, 512, true ], |
|
64 | + [ 512, 513, false ], |
|
65 | + [ FileInfo::SPACE_NOT_COMPUTED, 1024, true ], |
|
66 | + [ FileInfo::SPACE_UNKNOWN, 1024, true ], |
|
67 | + [ FileInfo::SPACE_UNLIMITED, 1024, true ] |
|
68 | + ]; |
|
69 | + } |
|
70 | 70 | } |
@@ -29,196 +29,196 @@ |
||
29 | 29 | * @package OCA\Files_Trashbin\Tests\Command |
30 | 30 | */ |
31 | 31 | class CleanUpTest extends TestCase { |
32 | - protected IUserManager&MockObject $userManager; |
|
33 | - protected IRootFolder&MockObject $rootFolder; |
|
34 | - protected IDBConnection $dbConnection; |
|
35 | - protected CleanUp $cleanup; |
|
36 | - protected string $trashTable = 'files_trash'; |
|
37 | - protected string $user0 = 'user0'; |
|
38 | - |
|
39 | - protected function setUp(): void { |
|
40 | - parent::setUp(); |
|
41 | - $this->rootFolder = $this->createMock(IRootFolder::class); |
|
42 | - $this->userManager = $this->createMock(IUserManager::class); |
|
43 | - |
|
44 | - $this->dbConnection = Server::get(IDBConnection::class); |
|
45 | - |
|
46 | - $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->dbConnection); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * populate files_trash table with 10 dummy values |
|
51 | - */ |
|
52 | - public function initTable(): void { |
|
53 | - $query = $this->dbConnection->getQueryBuilder(); |
|
54 | - $query->delete($this->trashTable)->executeStatement(); |
|
55 | - for ($i = 0; $i < 10; $i++) { |
|
56 | - $query->insert($this->trashTable) |
|
57 | - ->values([ |
|
58 | - 'id' => $query->expr()->literal('file' . $i), |
|
59 | - 'timestamp' => $query->expr()->literal($i), |
|
60 | - 'location' => $query->expr()->literal('.'), |
|
61 | - 'user' => $query->expr()->literal('user' . $i % 2) |
|
62 | - ])->executeStatement(); |
|
63 | - } |
|
64 | - $getAllQuery = $this->dbConnection->getQueryBuilder(); |
|
65 | - $result = $getAllQuery->select('id') |
|
66 | - ->from($this->trashTable) |
|
67 | - ->executeQuery() |
|
68 | - ->fetchAll(); |
|
69 | - $this->assertCount(10, $result); |
|
70 | - } |
|
71 | - |
|
72 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRemoveDeletedFiles')] |
|
73 | - public function testRemoveDeletedFiles(bool $nodeExists): void { |
|
74 | - $this->initTable(); |
|
75 | - $this->rootFolder |
|
76 | - ->method('nodeExists') |
|
77 | - ->with('/' . $this->user0 . '/files_trashbin') |
|
78 | - ->willReturnOnConsecutiveCalls($nodeExists, false); |
|
79 | - if ($nodeExists) { |
|
80 | - $this->rootFolder |
|
81 | - ->method('get') |
|
82 | - ->with('/' . $this->user0 . '/files_trashbin') |
|
83 | - ->willReturn($this->rootFolder); |
|
84 | - $this->rootFolder |
|
85 | - ->method('delete'); |
|
86 | - } else { |
|
87 | - $this->rootFolder->expects($this->never())->method('get'); |
|
88 | - $this->rootFolder->expects($this->never())->method('delete'); |
|
89 | - } |
|
90 | - self::invokePrivate($this->cleanup, 'removeDeletedFiles', [$this->user0, new NullOutput(), false]); |
|
91 | - |
|
92 | - if ($nodeExists) { |
|
93 | - // if the delete operation was executed only files from user1 |
|
94 | - // should be left. |
|
95 | - $query = $this->dbConnection->getQueryBuilder(); |
|
96 | - $query->select('user') |
|
97 | - ->from($this->trashTable); |
|
98 | - |
|
99 | - $qResult = $query->executeQuery(); |
|
100 | - $result = $qResult->fetchAll(); |
|
101 | - $qResult->closeCursor(); |
|
102 | - |
|
103 | - $this->assertCount(5, $result); |
|
104 | - foreach ($result as $r) { |
|
105 | - $this->assertSame('user1', $r['user']); |
|
106 | - } |
|
107 | - } else { |
|
108 | - // if no delete operation was executed we should still have all 10 |
|
109 | - // database entries |
|
110 | - $getAllQuery = $this->dbConnection->getQueryBuilder(); |
|
111 | - $result = $getAllQuery->select('id') |
|
112 | - ->from($this->trashTable) |
|
113 | - ->executeQuery() |
|
114 | - ->fetchAll(); |
|
115 | - $this->assertCount(10, $result); |
|
116 | - } |
|
117 | - } |
|
118 | - public static function dataTestRemoveDeletedFiles(): array { |
|
119 | - return [ |
|
120 | - [true], |
|
121 | - [false] |
|
122 | - ]; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * test remove deleted files from users given as parameter |
|
127 | - */ |
|
128 | - public function testExecuteDeleteListOfUsers(): void { |
|
129 | - $userIds = ['user1', 'user2', 'user3']; |
|
130 | - $instance = $this->getMockBuilder(CleanUp::class) |
|
131 | - ->onlyMethods(['removeDeletedFiles']) |
|
132 | - ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection]) |
|
133 | - ->getMock(); |
|
134 | - $instance->expects($this->exactly(count($userIds))) |
|
135 | - ->method('removeDeletedFiles') |
|
136 | - ->willReturnCallback(function ($user) use ($userIds): void { |
|
137 | - $this->assertTrue(in_array($user, $userIds)); |
|
138 | - }); |
|
139 | - $this->userManager->expects($this->exactly(count($userIds))) |
|
140 | - ->method('userExists')->willReturn(true); |
|
141 | - $inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class); |
|
142 | - $inputInterface->method('getArgument') |
|
143 | - ->with('user_id') |
|
144 | - ->willReturn($userIds); |
|
145 | - $inputInterface->method('getOption') |
|
146 | - ->willReturnMap([ |
|
147 | - ['all-users', false], |
|
148 | - ['verbose', false], |
|
149 | - ]); |
|
150 | - $outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class); |
|
151 | - self::invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * test remove deleted files of all users |
|
156 | - */ |
|
157 | - public function testExecuteAllUsers(): void { |
|
158 | - $userIds = []; |
|
159 | - $backendUsers = ['user1', 'user2']; |
|
160 | - $instance = $this->getMockBuilder(CleanUp::class) |
|
161 | - ->onlyMethods(['removeDeletedFiles']) |
|
162 | - ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection]) |
|
163 | - ->getMock(); |
|
164 | - $backend = $this->createMock(UserInterface::class); |
|
165 | - $backend->method('getUsers') |
|
166 | - ->with('', 500, 0) |
|
167 | - ->willReturn($backendUsers); |
|
168 | - $instance->expects($this->exactly(count($backendUsers))) |
|
169 | - ->method('removeDeletedFiles') |
|
170 | - ->willReturnCallback(function ($user) use ($backendUsers): void { |
|
171 | - $this->assertTrue(in_array($user, $backendUsers)); |
|
172 | - }); |
|
173 | - $inputInterface = $this->createMock(InputInterface::class); |
|
174 | - $inputInterface->method('getArgument') |
|
175 | - ->with('user_id') |
|
176 | - ->willReturn($userIds); |
|
177 | - $inputInterface->method('getOption') |
|
178 | - ->willReturnMap([ |
|
179 | - ['all-users', true], |
|
180 | - ['verbose', false], |
|
181 | - ]); |
|
182 | - $outputInterface = $this->createMock(OutputInterface::class); |
|
183 | - $this->userManager |
|
184 | - ->method('getBackends') |
|
185 | - ->willReturn([$backend]); |
|
186 | - self::invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
187 | - } |
|
188 | - |
|
189 | - public function testExecuteNoUsersAndNoAllUsers(): void { |
|
190 | - $inputInterface = $this->createMock(InputInterface::class); |
|
191 | - $inputInterface->method('getArgument') |
|
192 | - ->with('user_id') |
|
193 | - ->willReturn([]); |
|
194 | - $inputInterface->method('getOption') |
|
195 | - ->willReturnMap([ |
|
196 | - ['all-users', false], |
|
197 | - ['verbose', false], |
|
198 | - ]); |
|
199 | - $outputInterface = $this->createMock(OutputInterface::class); |
|
200 | - |
|
201 | - $this->expectException(InvalidOptionException::class); |
|
202 | - $this->expectExceptionMessage('Either specify a user_id or --all-users'); |
|
203 | - |
|
204 | - self::invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]); |
|
205 | - } |
|
206 | - |
|
207 | - public function testExecuteUsersAndAllUsers(): void { |
|
208 | - $inputInterface = $this->createMock(InputInterface::class); |
|
209 | - $inputInterface->method('getArgument') |
|
210 | - ->with('user_id') |
|
211 | - ->willReturn(['user1', 'user2']); |
|
212 | - $inputInterface->method('getOption') |
|
213 | - ->willReturnMap([ |
|
214 | - ['all-users', true], |
|
215 | - ['verbose', false], |
|
216 | - ]); |
|
217 | - $outputInterface = $this->createMock(OutputInterface::class); |
|
218 | - |
|
219 | - $this->expectException(InvalidOptionException::class); |
|
220 | - $this->expectExceptionMessage('Either specify a user_id or --all-users'); |
|
221 | - |
|
222 | - self::invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]); |
|
223 | - } |
|
32 | + protected IUserManager&MockObject $userManager; |
|
33 | + protected IRootFolder&MockObject $rootFolder; |
|
34 | + protected IDBConnection $dbConnection; |
|
35 | + protected CleanUp $cleanup; |
|
36 | + protected string $trashTable = 'files_trash'; |
|
37 | + protected string $user0 = 'user0'; |
|
38 | + |
|
39 | + protected function setUp(): void { |
|
40 | + parent::setUp(); |
|
41 | + $this->rootFolder = $this->createMock(IRootFolder::class); |
|
42 | + $this->userManager = $this->createMock(IUserManager::class); |
|
43 | + |
|
44 | + $this->dbConnection = Server::get(IDBConnection::class); |
|
45 | + |
|
46 | + $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->dbConnection); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * populate files_trash table with 10 dummy values |
|
51 | + */ |
|
52 | + public function initTable(): void { |
|
53 | + $query = $this->dbConnection->getQueryBuilder(); |
|
54 | + $query->delete($this->trashTable)->executeStatement(); |
|
55 | + for ($i = 0; $i < 10; $i++) { |
|
56 | + $query->insert($this->trashTable) |
|
57 | + ->values([ |
|
58 | + 'id' => $query->expr()->literal('file' . $i), |
|
59 | + 'timestamp' => $query->expr()->literal($i), |
|
60 | + 'location' => $query->expr()->literal('.'), |
|
61 | + 'user' => $query->expr()->literal('user' . $i % 2) |
|
62 | + ])->executeStatement(); |
|
63 | + } |
|
64 | + $getAllQuery = $this->dbConnection->getQueryBuilder(); |
|
65 | + $result = $getAllQuery->select('id') |
|
66 | + ->from($this->trashTable) |
|
67 | + ->executeQuery() |
|
68 | + ->fetchAll(); |
|
69 | + $this->assertCount(10, $result); |
|
70 | + } |
|
71 | + |
|
72 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRemoveDeletedFiles')] |
|
73 | + public function testRemoveDeletedFiles(bool $nodeExists): void { |
|
74 | + $this->initTable(); |
|
75 | + $this->rootFolder |
|
76 | + ->method('nodeExists') |
|
77 | + ->with('/' . $this->user0 . '/files_trashbin') |
|
78 | + ->willReturnOnConsecutiveCalls($nodeExists, false); |
|
79 | + if ($nodeExists) { |
|
80 | + $this->rootFolder |
|
81 | + ->method('get') |
|
82 | + ->with('/' . $this->user0 . '/files_trashbin') |
|
83 | + ->willReturn($this->rootFolder); |
|
84 | + $this->rootFolder |
|
85 | + ->method('delete'); |
|
86 | + } else { |
|
87 | + $this->rootFolder->expects($this->never())->method('get'); |
|
88 | + $this->rootFolder->expects($this->never())->method('delete'); |
|
89 | + } |
|
90 | + self::invokePrivate($this->cleanup, 'removeDeletedFiles', [$this->user0, new NullOutput(), false]); |
|
91 | + |
|
92 | + if ($nodeExists) { |
|
93 | + // if the delete operation was executed only files from user1 |
|
94 | + // should be left. |
|
95 | + $query = $this->dbConnection->getQueryBuilder(); |
|
96 | + $query->select('user') |
|
97 | + ->from($this->trashTable); |
|
98 | + |
|
99 | + $qResult = $query->executeQuery(); |
|
100 | + $result = $qResult->fetchAll(); |
|
101 | + $qResult->closeCursor(); |
|
102 | + |
|
103 | + $this->assertCount(5, $result); |
|
104 | + foreach ($result as $r) { |
|
105 | + $this->assertSame('user1', $r['user']); |
|
106 | + } |
|
107 | + } else { |
|
108 | + // if no delete operation was executed we should still have all 10 |
|
109 | + // database entries |
|
110 | + $getAllQuery = $this->dbConnection->getQueryBuilder(); |
|
111 | + $result = $getAllQuery->select('id') |
|
112 | + ->from($this->trashTable) |
|
113 | + ->executeQuery() |
|
114 | + ->fetchAll(); |
|
115 | + $this->assertCount(10, $result); |
|
116 | + } |
|
117 | + } |
|
118 | + public static function dataTestRemoveDeletedFiles(): array { |
|
119 | + return [ |
|
120 | + [true], |
|
121 | + [false] |
|
122 | + ]; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * test remove deleted files from users given as parameter |
|
127 | + */ |
|
128 | + public function testExecuteDeleteListOfUsers(): void { |
|
129 | + $userIds = ['user1', 'user2', 'user3']; |
|
130 | + $instance = $this->getMockBuilder(CleanUp::class) |
|
131 | + ->onlyMethods(['removeDeletedFiles']) |
|
132 | + ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection]) |
|
133 | + ->getMock(); |
|
134 | + $instance->expects($this->exactly(count($userIds))) |
|
135 | + ->method('removeDeletedFiles') |
|
136 | + ->willReturnCallback(function ($user) use ($userIds): void { |
|
137 | + $this->assertTrue(in_array($user, $userIds)); |
|
138 | + }); |
|
139 | + $this->userManager->expects($this->exactly(count($userIds))) |
|
140 | + ->method('userExists')->willReturn(true); |
|
141 | + $inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class); |
|
142 | + $inputInterface->method('getArgument') |
|
143 | + ->with('user_id') |
|
144 | + ->willReturn($userIds); |
|
145 | + $inputInterface->method('getOption') |
|
146 | + ->willReturnMap([ |
|
147 | + ['all-users', false], |
|
148 | + ['verbose', false], |
|
149 | + ]); |
|
150 | + $outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class); |
|
151 | + self::invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * test remove deleted files of all users |
|
156 | + */ |
|
157 | + public function testExecuteAllUsers(): void { |
|
158 | + $userIds = []; |
|
159 | + $backendUsers = ['user1', 'user2']; |
|
160 | + $instance = $this->getMockBuilder(CleanUp::class) |
|
161 | + ->onlyMethods(['removeDeletedFiles']) |
|
162 | + ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection]) |
|
163 | + ->getMock(); |
|
164 | + $backend = $this->createMock(UserInterface::class); |
|
165 | + $backend->method('getUsers') |
|
166 | + ->with('', 500, 0) |
|
167 | + ->willReturn($backendUsers); |
|
168 | + $instance->expects($this->exactly(count($backendUsers))) |
|
169 | + ->method('removeDeletedFiles') |
|
170 | + ->willReturnCallback(function ($user) use ($backendUsers): void { |
|
171 | + $this->assertTrue(in_array($user, $backendUsers)); |
|
172 | + }); |
|
173 | + $inputInterface = $this->createMock(InputInterface::class); |
|
174 | + $inputInterface->method('getArgument') |
|
175 | + ->with('user_id') |
|
176 | + ->willReturn($userIds); |
|
177 | + $inputInterface->method('getOption') |
|
178 | + ->willReturnMap([ |
|
179 | + ['all-users', true], |
|
180 | + ['verbose', false], |
|
181 | + ]); |
|
182 | + $outputInterface = $this->createMock(OutputInterface::class); |
|
183 | + $this->userManager |
|
184 | + ->method('getBackends') |
|
185 | + ->willReturn([$backend]); |
|
186 | + self::invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
187 | + } |
|
188 | + |
|
189 | + public function testExecuteNoUsersAndNoAllUsers(): void { |
|
190 | + $inputInterface = $this->createMock(InputInterface::class); |
|
191 | + $inputInterface->method('getArgument') |
|
192 | + ->with('user_id') |
|
193 | + ->willReturn([]); |
|
194 | + $inputInterface->method('getOption') |
|
195 | + ->willReturnMap([ |
|
196 | + ['all-users', false], |
|
197 | + ['verbose', false], |
|
198 | + ]); |
|
199 | + $outputInterface = $this->createMock(OutputInterface::class); |
|
200 | + |
|
201 | + $this->expectException(InvalidOptionException::class); |
|
202 | + $this->expectExceptionMessage('Either specify a user_id or --all-users'); |
|
203 | + |
|
204 | + self::invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]); |
|
205 | + } |
|
206 | + |
|
207 | + public function testExecuteUsersAndAllUsers(): void { |
|
208 | + $inputInterface = $this->createMock(InputInterface::class); |
|
209 | + $inputInterface->method('getArgument') |
|
210 | + ->with('user_id') |
|
211 | + ->willReturn(['user1', 'user2']); |
|
212 | + $inputInterface->method('getOption') |
|
213 | + ->willReturnMap([ |
|
214 | + ['all-users', true], |
|
215 | + ['verbose', false], |
|
216 | + ]); |
|
217 | + $outputInterface = $this->createMock(OutputInterface::class); |
|
218 | + |
|
219 | + $this->expectException(InvalidOptionException::class); |
|
220 | + $this->expectExceptionMessage('Either specify a user_id or --all-users'); |
|
221 | + |
|
222 | + self::invokePrivate($this->cleanup, 'execute', [$inputInterface, $outputInterface]); |
|
223 | + } |
|
224 | 224 | } |
@@ -14,133 +14,133 @@ |
||
14 | 14 | use PHPUnit\Framework\MockObject\MockObject; |
15 | 15 | |
16 | 16 | class ExpirationTest extends \Test\TestCase { |
17 | - public const SECONDS_PER_DAY = 86400; //60*60*24 |
|
18 | - |
|
19 | - public const FAKE_TIME_NOW = 1000000; |
|
20 | - |
|
21 | - public static function expirationData(): array { |
|
22 | - $today = 100 * self::SECONDS_PER_DAY; |
|
23 | - $back10Days = (100 - 10) * self::SECONDS_PER_DAY; |
|
24 | - $back20Days = (100 - 20) * self::SECONDS_PER_DAY; |
|
25 | - $back30Days = (100 - 30) * self::SECONDS_PER_DAY; |
|
26 | - $back35Days = (100 - 35) * self::SECONDS_PER_DAY; |
|
27 | - |
|
28 | - // it should never happen, but who knows :/ |
|
29 | - $ahead100Days = (100 + 100) * self::SECONDS_PER_DAY; |
|
30 | - |
|
31 | - return [ |
|
32 | - // Expiration is disabled - always should return false |
|
33 | - [ 'disabled', $today, $back10Days, false, false], |
|
34 | - [ 'disabled', $today, $back10Days, true, false], |
|
35 | - [ 'disabled', $today, $ahead100Days, true, false], |
|
36 | - |
|
37 | - // Default: expire in 30 days or earlier when quota requirements are met |
|
38 | - [ 'auto', $today, $back10Days, false, false], |
|
39 | - [ 'auto', $today, $back35Days, false, false], |
|
40 | - [ 'auto', $today, $back10Days, true, true], |
|
41 | - [ 'auto', $today, $back35Days, true, true], |
|
42 | - [ 'auto', $today, $ahead100Days, true, true], |
|
43 | - |
|
44 | - // The same with 'auto' |
|
45 | - [ 'auto, auto', $today, $back10Days, false, false], |
|
46 | - [ 'auto, auto', $today, $back35Days, false, false], |
|
47 | - [ 'auto, auto', $today, $back10Days, true, true], |
|
48 | - [ 'auto, auto', $today, $back35Days, true, true], |
|
49 | - |
|
50 | - // Keep for 15 days but expire anytime if space needed |
|
51 | - [ '15, auto', $today, $back10Days, false, false], |
|
52 | - [ '15, auto', $today, $back20Days, false, false], |
|
53 | - [ '15, auto', $today, $back10Days, true, true], |
|
54 | - [ '15, auto', $today, $back20Days, true, true], |
|
55 | - [ '15, auto', $today, $ahead100Days, true, true], |
|
56 | - |
|
57 | - // Expire anytime if space needed, Expire all older than max |
|
58 | - [ 'auto, 15', $today, $back10Days, false, false], |
|
59 | - [ 'auto, 15', $today, $back20Days, false, true], |
|
60 | - [ 'auto, 15', $today, $back10Days, true, true], |
|
61 | - [ 'auto, 15', $today, $back20Days, true, true], |
|
62 | - [ 'auto, 15', $today, $ahead100Days, true, true], |
|
63 | - |
|
64 | - // Expire all older than max OR older than min if space needed |
|
65 | - [ '15, 25', $today, $back10Days, false, false], |
|
66 | - [ '15, 25', $today, $back20Days, false, false], |
|
67 | - [ '15, 25', $today, $back30Days, false, true], |
|
68 | - [ '15, 25', $today, $back10Days, false, false], |
|
69 | - [ '15, 25', $today, $back20Days, true, true], |
|
70 | - [ '15, 25', $today, $back30Days, true, true], |
|
71 | - [ '15, 25', $today, $ahead100Days, true, false], |
|
72 | - |
|
73 | - // Expire all older than max OR older than min if space needed |
|
74 | - // Max<Min case |
|
75 | - [ '25, 15', $today, $back10Days, false, false], |
|
76 | - [ '25, 15', $today, $back20Days, false, false], |
|
77 | - [ '25, 15', $today, $back30Days, false, true], |
|
78 | - [ '25, 15', $today, $back10Days, false, false], |
|
79 | - [ '25, 15', $today, $back20Days, true, false], |
|
80 | - [ '25, 15', $today, $back30Days, true, true], |
|
81 | - [ '25, 15', $today, $ahead100Days, true, false], |
|
82 | - ]; |
|
83 | - } |
|
84 | - |
|
85 | - #[\PHPUnit\Framework\Attributes\DataProvider('expirationData')] |
|
86 | - public function testExpiration(string $retentionObligation, int $timeNow, int $timestamp, bool $quotaExceeded, bool $expectedResult): void { |
|
87 | - $mockedConfig = $this->getMockedConfig($retentionObligation); |
|
88 | - $mockedTimeFactory = $this->getMockedTimeFactory($timeNow); |
|
89 | - |
|
90 | - $expiration = new Expiration($mockedConfig, $mockedTimeFactory); |
|
91 | - $actualResult = $expiration->isExpired($timestamp, $quotaExceeded); |
|
92 | - |
|
93 | - $this->assertEquals($expectedResult, $actualResult); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - public static function timestampTestData(): array { |
|
98 | - return [ |
|
99 | - [ 'disabled', false], |
|
100 | - [ 'auto', false ], |
|
101 | - [ 'auto,auto', false ], |
|
102 | - [ 'auto, auto', false ], |
|
103 | - [ 'auto, 3', self::FAKE_TIME_NOW - (3 * self::SECONDS_PER_DAY) ], |
|
104 | - [ '5, auto', false ], |
|
105 | - [ '3, 5', self::FAKE_TIME_NOW - (5 * self::SECONDS_PER_DAY) ], |
|
106 | - [ '10, 3', self::FAKE_TIME_NOW - (10 * self::SECONDS_PER_DAY) ], |
|
107 | - ]; |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - #[\PHPUnit\Framework\Attributes\DataProvider('timestampTestData')] |
|
112 | - public function testGetMaxAgeAsTimestamp(string $configValue, bool|int $expectedMaxAgeTimestamp): void { |
|
113 | - $mockedConfig = $this->getMockedConfig($configValue); |
|
114 | - $mockedTimeFactory = $this->getMockedTimeFactory( |
|
115 | - self::FAKE_TIME_NOW |
|
116 | - ); |
|
117 | - |
|
118 | - $expiration = new Expiration($mockedConfig, $mockedTimeFactory); |
|
119 | - $actualTimestamp = $expiration->getMaxAgeAsTimestamp(); |
|
120 | - $this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @return ITimeFactory|MockObject |
|
125 | - */ |
|
126 | - private function getMockedTimeFactory(int $time) { |
|
127 | - $mockedTimeFactory = $this->createMock(ITimeFactory::class); |
|
128 | - $mockedTimeFactory->expects($this->any()) |
|
129 | - ->method('getTime') |
|
130 | - ->willReturn($time); |
|
131 | - |
|
132 | - return $mockedTimeFactory; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @return IConfig|MockObject |
|
137 | - */ |
|
138 | - private function getMockedConfig(string $returnValue) { |
|
139 | - $mockedConfig = $this->createMock(IConfig::class); |
|
140 | - $mockedConfig->expects($this->any()) |
|
141 | - ->method('getSystemValue') |
|
142 | - ->willReturn($returnValue); |
|
143 | - |
|
144 | - return $mockedConfig; |
|
145 | - } |
|
17 | + public const SECONDS_PER_DAY = 86400; //60*60*24 |
|
18 | + |
|
19 | + public const FAKE_TIME_NOW = 1000000; |
|
20 | + |
|
21 | + public static function expirationData(): array { |
|
22 | + $today = 100 * self::SECONDS_PER_DAY; |
|
23 | + $back10Days = (100 - 10) * self::SECONDS_PER_DAY; |
|
24 | + $back20Days = (100 - 20) * self::SECONDS_PER_DAY; |
|
25 | + $back30Days = (100 - 30) * self::SECONDS_PER_DAY; |
|
26 | + $back35Days = (100 - 35) * self::SECONDS_PER_DAY; |
|
27 | + |
|
28 | + // it should never happen, but who knows :/ |
|
29 | + $ahead100Days = (100 + 100) * self::SECONDS_PER_DAY; |
|
30 | + |
|
31 | + return [ |
|
32 | + // Expiration is disabled - always should return false |
|
33 | + [ 'disabled', $today, $back10Days, false, false], |
|
34 | + [ 'disabled', $today, $back10Days, true, false], |
|
35 | + [ 'disabled', $today, $ahead100Days, true, false], |
|
36 | + |
|
37 | + // Default: expire in 30 days or earlier when quota requirements are met |
|
38 | + [ 'auto', $today, $back10Days, false, false], |
|
39 | + [ 'auto', $today, $back35Days, false, false], |
|
40 | + [ 'auto', $today, $back10Days, true, true], |
|
41 | + [ 'auto', $today, $back35Days, true, true], |
|
42 | + [ 'auto', $today, $ahead100Days, true, true], |
|
43 | + |
|
44 | + // The same with 'auto' |
|
45 | + [ 'auto, auto', $today, $back10Days, false, false], |
|
46 | + [ 'auto, auto', $today, $back35Days, false, false], |
|
47 | + [ 'auto, auto', $today, $back10Days, true, true], |
|
48 | + [ 'auto, auto', $today, $back35Days, true, true], |
|
49 | + |
|
50 | + // Keep for 15 days but expire anytime if space needed |
|
51 | + [ '15, auto', $today, $back10Days, false, false], |
|
52 | + [ '15, auto', $today, $back20Days, false, false], |
|
53 | + [ '15, auto', $today, $back10Days, true, true], |
|
54 | + [ '15, auto', $today, $back20Days, true, true], |
|
55 | + [ '15, auto', $today, $ahead100Days, true, true], |
|
56 | + |
|
57 | + // Expire anytime if space needed, Expire all older than max |
|
58 | + [ 'auto, 15', $today, $back10Days, false, false], |
|
59 | + [ 'auto, 15', $today, $back20Days, false, true], |
|
60 | + [ 'auto, 15', $today, $back10Days, true, true], |
|
61 | + [ 'auto, 15', $today, $back20Days, true, true], |
|
62 | + [ 'auto, 15', $today, $ahead100Days, true, true], |
|
63 | + |
|
64 | + // Expire all older than max OR older than min if space needed |
|
65 | + [ '15, 25', $today, $back10Days, false, false], |
|
66 | + [ '15, 25', $today, $back20Days, false, false], |
|
67 | + [ '15, 25', $today, $back30Days, false, true], |
|
68 | + [ '15, 25', $today, $back10Days, false, false], |
|
69 | + [ '15, 25', $today, $back20Days, true, true], |
|
70 | + [ '15, 25', $today, $back30Days, true, true], |
|
71 | + [ '15, 25', $today, $ahead100Days, true, false], |
|
72 | + |
|
73 | + // Expire all older than max OR older than min if space needed |
|
74 | + // Max<Min case |
|
75 | + [ '25, 15', $today, $back10Days, false, false], |
|
76 | + [ '25, 15', $today, $back20Days, false, false], |
|
77 | + [ '25, 15', $today, $back30Days, false, true], |
|
78 | + [ '25, 15', $today, $back10Days, false, false], |
|
79 | + [ '25, 15', $today, $back20Days, true, false], |
|
80 | + [ '25, 15', $today, $back30Days, true, true], |
|
81 | + [ '25, 15', $today, $ahead100Days, true, false], |
|
82 | + ]; |
|
83 | + } |
|
84 | + |
|
85 | + #[\PHPUnit\Framework\Attributes\DataProvider('expirationData')] |
|
86 | + public function testExpiration(string $retentionObligation, int $timeNow, int $timestamp, bool $quotaExceeded, bool $expectedResult): void { |
|
87 | + $mockedConfig = $this->getMockedConfig($retentionObligation); |
|
88 | + $mockedTimeFactory = $this->getMockedTimeFactory($timeNow); |
|
89 | + |
|
90 | + $expiration = new Expiration($mockedConfig, $mockedTimeFactory); |
|
91 | + $actualResult = $expiration->isExpired($timestamp, $quotaExceeded); |
|
92 | + |
|
93 | + $this->assertEquals($expectedResult, $actualResult); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + public static function timestampTestData(): array { |
|
98 | + return [ |
|
99 | + [ 'disabled', false], |
|
100 | + [ 'auto', false ], |
|
101 | + [ 'auto,auto', false ], |
|
102 | + [ 'auto, auto', false ], |
|
103 | + [ 'auto, 3', self::FAKE_TIME_NOW - (3 * self::SECONDS_PER_DAY) ], |
|
104 | + [ '5, auto', false ], |
|
105 | + [ '3, 5', self::FAKE_TIME_NOW - (5 * self::SECONDS_PER_DAY) ], |
|
106 | + [ '10, 3', self::FAKE_TIME_NOW - (10 * self::SECONDS_PER_DAY) ], |
|
107 | + ]; |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + #[\PHPUnit\Framework\Attributes\DataProvider('timestampTestData')] |
|
112 | + public function testGetMaxAgeAsTimestamp(string $configValue, bool|int $expectedMaxAgeTimestamp): void { |
|
113 | + $mockedConfig = $this->getMockedConfig($configValue); |
|
114 | + $mockedTimeFactory = $this->getMockedTimeFactory( |
|
115 | + self::FAKE_TIME_NOW |
|
116 | + ); |
|
117 | + |
|
118 | + $expiration = new Expiration($mockedConfig, $mockedTimeFactory); |
|
119 | + $actualTimestamp = $expiration->getMaxAgeAsTimestamp(); |
|
120 | + $this->assertEquals($expectedMaxAgeTimestamp, $actualTimestamp); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @return ITimeFactory|MockObject |
|
125 | + */ |
|
126 | + private function getMockedTimeFactory(int $time) { |
|
127 | + $mockedTimeFactory = $this->createMock(ITimeFactory::class); |
|
128 | + $mockedTimeFactory->expects($this->any()) |
|
129 | + ->method('getTime') |
|
130 | + ->willReturn($time); |
|
131 | + |
|
132 | + return $mockedTimeFactory; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @return IConfig|MockObject |
|
137 | + */ |
|
138 | + private function getMockedConfig(string $returnValue) { |
|
139 | + $mockedConfig = $this->createMock(IConfig::class); |
|
140 | + $mockedConfig->expects($this->any()) |
|
141 | + ->method('getSystemValue') |
|
142 | + ->willReturn($returnValue); |
|
143 | + |
|
144 | + return $mockedConfig; |
|
145 | + } |
|
146 | 146 | } |
@@ -30,55 +30,55 @@ discard block |
||
30 | 30 | |
31 | 31 | return [ |
32 | 32 | // Expiration is disabled - always should return false |
33 | - [ 'disabled', $today, $back10Days, false, false], |
|
34 | - [ 'disabled', $today, $back10Days, true, false], |
|
35 | - [ 'disabled', $today, $ahead100Days, true, false], |
|
33 | + ['disabled', $today, $back10Days, false, false], |
|
34 | + ['disabled', $today, $back10Days, true, false], |
|
35 | + ['disabled', $today, $ahead100Days, true, false], |
|
36 | 36 | |
37 | 37 | // Default: expire in 30 days or earlier when quota requirements are met |
38 | - [ 'auto', $today, $back10Days, false, false], |
|
39 | - [ 'auto', $today, $back35Days, false, false], |
|
40 | - [ 'auto', $today, $back10Days, true, true], |
|
41 | - [ 'auto', $today, $back35Days, true, true], |
|
42 | - [ 'auto', $today, $ahead100Days, true, true], |
|
38 | + ['auto', $today, $back10Days, false, false], |
|
39 | + ['auto', $today, $back35Days, false, false], |
|
40 | + ['auto', $today, $back10Days, true, true], |
|
41 | + ['auto', $today, $back35Days, true, true], |
|
42 | + ['auto', $today, $ahead100Days, true, true], |
|
43 | 43 | |
44 | 44 | // The same with 'auto' |
45 | - [ 'auto, auto', $today, $back10Days, false, false], |
|
46 | - [ 'auto, auto', $today, $back35Days, false, false], |
|
47 | - [ 'auto, auto', $today, $back10Days, true, true], |
|
48 | - [ 'auto, auto', $today, $back35Days, true, true], |
|
45 | + ['auto, auto', $today, $back10Days, false, false], |
|
46 | + ['auto, auto', $today, $back35Days, false, false], |
|
47 | + ['auto, auto', $today, $back10Days, true, true], |
|
48 | + ['auto, auto', $today, $back35Days, true, true], |
|
49 | 49 | |
50 | 50 | // Keep for 15 days but expire anytime if space needed |
51 | - [ '15, auto', $today, $back10Days, false, false], |
|
52 | - [ '15, auto', $today, $back20Days, false, false], |
|
53 | - [ '15, auto', $today, $back10Days, true, true], |
|
54 | - [ '15, auto', $today, $back20Days, true, true], |
|
55 | - [ '15, auto', $today, $ahead100Days, true, true], |
|
51 | + ['15, auto', $today, $back10Days, false, false], |
|
52 | + ['15, auto', $today, $back20Days, false, false], |
|
53 | + ['15, auto', $today, $back10Days, true, true], |
|
54 | + ['15, auto', $today, $back20Days, true, true], |
|
55 | + ['15, auto', $today, $ahead100Days, true, true], |
|
56 | 56 | |
57 | 57 | // Expire anytime if space needed, Expire all older than max |
58 | - [ 'auto, 15', $today, $back10Days, false, false], |
|
59 | - [ 'auto, 15', $today, $back20Days, false, true], |
|
60 | - [ 'auto, 15', $today, $back10Days, true, true], |
|
61 | - [ 'auto, 15', $today, $back20Days, true, true], |
|
62 | - [ 'auto, 15', $today, $ahead100Days, true, true], |
|
58 | + ['auto, 15', $today, $back10Days, false, false], |
|
59 | + ['auto, 15', $today, $back20Days, false, true], |
|
60 | + ['auto, 15', $today, $back10Days, true, true], |
|
61 | + ['auto, 15', $today, $back20Days, true, true], |
|
62 | + ['auto, 15', $today, $ahead100Days, true, true], |
|
63 | 63 | |
64 | 64 | // Expire all older than max OR older than min if space needed |
65 | - [ '15, 25', $today, $back10Days, false, false], |
|
66 | - [ '15, 25', $today, $back20Days, false, false], |
|
67 | - [ '15, 25', $today, $back30Days, false, true], |
|
68 | - [ '15, 25', $today, $back10Days, false, false], |
|
69 | - [ '15, 25', $today, $back20Days, true, true], |
|
70 | - [ '15, 25', $today, $back30Days, true, true], |
|
71 | - [ '15, 25', $today, $ahead100Days, true, false], |
|
65 | + ['15, 25', $today, $back10Days, false, false], |
|
66 | + ['15, 25', $today, $back20Days, false, false], |
|
67 | + ['15, 25', $today, $back30Days, false, true], |
|
68 | + ['15, 25', $today, $back10Days, false, false], |
|
69 | + ['15, 25', $today, $back20Days, true, true], |
|
70 | + ['15, 25', $today, $back30Days, true, true], |
|
71 | + ['15, 25', $today, $ahead100Days, true, false], |
|
72 | 72 | |
73 | 73 | // Expire all older than max OR older than min if space needed |
74 | 74 | // Max<Min case |
75 | - [ '25, 15', $today, $back10Days, false, false], |
|
76 | - [ '25, 15', $today, $back20Days, false, false], |
|
77 | - [ '25, 15', $today, $back30Days, false, true], |
|
78 | - [ '25, 15', $today, $back10Days, false, false], |
|
79 | - [ '25, 15', $today, $back20Days, true, false], |
|
80 | - [ '25, 15', $today, $back30Days, true, true], |
|
81 | - [ '25, 15', $today, $ahead100Days, true, false], |
|
75 | + ['25, 15', $today, $back10Days, false, false], |
|
76 | + ['25, 15', $today, $back20Days, false, false], |
|
77 | + ['25, 15', $today, $back30Days, false, true], |
|
78 | + ['25, 15', $today, $back10Days, false, false], |
|
79 | + ['25, 15', $today, $back20Days, true, false], |
|
80 | + ['25, 15', $today, $back30Days, true, true], |
|
81 | + ['25, 15', $today, $ahead100Days, true, false], |
|
82 | 82 | ]; |
83 | 83 | } |
84 | 84 | |
@@ -96,20 +96,20 @@ discard block |
||
96 | 96 | |
97 | 97 | public static function timestampTestData(): array { |
98 | 98 | return [ |
99 | - [ 'disabled', false], |
|
100 | - [ 'auto', false ], |
|
101 | - [ 'auto,auto', false ], |
|
102 | - [ 'auto, auto', false ], |
|
103 | - [ 'auto, 3', self::FAKE_TIME_NOW - (3 * self::SECONDS_PER_DAY) ], |
|
104 | - [ '5, auto', false ], |
|
105 | - [ '3, 5', self::FAKE_TIME_NOW - (5 * self::SECONDS_PER_DAY) ], |
|
106 | - [ '10, 3', self::FAKE_TIME_NOW - (10 * self::SECONDS_PER_DAY) ], |
|
99 | + ['disabled', false], |
|
100 | + ['auto', false], |
|
101 | + ['auto,auto', false], |
|
102 | + ['auto, auto', false], |
|
103 | + ['auto, 3', self::FAKE_TIME_NOW - (3 * self::SECONDS_PER_DAY)], |
|
104 | + ['5, auto', false], |
|
105 | + ['3, 5', self::FAKE_TIME_NOW - (5 * self::SECONDS_PER_DAY)], |
|
106 | + ['10, 3', self::FAKE_TIME_NOW - (10 * self::SECONDS_PER_DAY)], |
|
107 | 107 | ]; |
108 | 108 | } |
109 | 109 | |
110 | 110 | |
111 | 111 | #[\PHPUnit\Framework\Attributes\DataProvider('timestampTestData')] |
112 | - public function testGetMaxAgeAsTimestamp(string $configValue, bool|int $expectedMaxAgeTimestamp): void { |
|
112 | + public function testGetMaxAgeAsTimestamp(string $configValue, bool | int $expectedMaxAgeTimestamp): void { |
|
113 | 113 | $mockedConfig = $this->getMockedConfig($configValue); |
114 | 114 | $mockedTimeFactory = $this->getMockedTimeFactory( |
115 | 115 | self::FAKE_TIME_NOW |
@@ -34,13 +34,13 @@ discard block |
||
34 | 34 | use Test\Traits\MountProviderTrait; |
35 | 35 | |
36 | 36 | class TemporaryNoCross extends Temporary { |
37 | - public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, ?bool $preserveMtime = null): bool { |
|
38 | - return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime); |
|
39 | - } |
|
37 | + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, ?bool $preserveMtime = null): bool { |
|
38 | + return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime); |
|
39 | + } |
|
40 | 40 | |
41 | - public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { |
|
42 | - return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
43 | - } |
|
41 | + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { |
|
42 | + return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
43 | + } |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
@@ -51,630 +51,630 @@ discard block |
||
51 | 51 | * @package OCA\Files_Trashbin\Tests |
52 | 52 | */ |
53 | 53 | class StorageTest extends \Test\TestCase { |
54 | - use MountProviderTrait; |
|
55 | - |
|
56 | - private string $user; |
|
57 | - private View $rootView; |
|
58 | - private View $userView; |
|
59 | - |
|
60 | - // 239 chars so appended timestamp of 12 chars will exceed max length of 250 chars |
|
61 | - private const LONG_FILENAME = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
62 | - // 250 chars |
|
63 | - private const MAX_FILENAME = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
64 | - |
|
65 | - protected function setUp(): void { |
|
66 | - parent::setUp(); |
|
67 | - |
|
68 | - \OC_Hook::clear(); |
|
69 | - \OC::$server->boot(); |
|
70 | - |
|
71 | - // register trashbin hooks |
|
72 | - $trashbinApp = new Application(); |
|
73 | - $trashbinApp->boot($this->createMock(IBootContext::class)); |
|
74 | - |
|
75 | - $this->user = $this->getUniqueId('user'); |
|
76 | - Server::get(IUserManager::class)->createUser($this->user, $this->user); |
|
77 | - |
|
78 | - // this will setup the FS |
|
79 | - $this->loginAsUser($this->user); |
|
80 | - |
|
81 | - Storage::setupStorage(); |
|
82 | - |
|
83 | - $this->rootView = new View('/'); |
|
84 | - $this->userView = new View('/' . $this->user . '/files/'); |
|
85 | - $this->userView->file_put_contents('test.txt', 'foo'); |
|
86 | - $this->userView->file_put_contents(static::LONG_FILENAME, 'foo'); |
|
87 | - $this->userView->file_put_contents(static::MAX_FILENAME, 'foo'); |
|
88 | - |
|
89 | - $this->userView->mkdir('folder'); |
|
90 | - $this->userView->file_put_contents('folder/inside.txt', 'bar'); |
|
91 | - } |
|
92 | - |
|
93 | - protected function tearDown(): void { |
|
94 | - Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); |
|
95 | - $this->logout(); |
|
96 | - $user = Server::get(IUserManager::class)->get($this->user); |
|
97 | - if ($user !== null) { |
|
98 | - $user->delete(); |
|
99 | - } |
|
100 | - \OC_Hook::clear(); |
|
101 | - parent::tearDown(); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Test that deleting a file puts it into the trashbin. |
|
106 | - */ |
|
107 | - public function testSingleStorageDeleteFile(): void { |
|
108 | - $this->assertTrue($this->userView->file_exists('test.txt')); |
|
109 | - $this->userView->unlink('test.txt'); |
|
110 | - [$storage,] = $this->userView->resolvePath('test.txt'); |
|
111 | - $storage->getScanner()->scan(''); // make sure we check the storage |
|
112 | - $this->assertFalse($this->userView->getFileInfo('test.txt')); |
|
113 | - |
|
114 | - // check if file is in trashbin |
|
115 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
116 | - $this->assertCount(1, $results); |
|
117 | - $name = $results[0]->getName(); |
|
118 | - $this->assertEquals('test.txt', substr($name, 0, strrpos($name, '.'))); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Test that deleting a folder puts it into the trashbin. |
|
123 | - */ |
|
124 | - public function testSingleStorageDeleteFolder(): void { |
|
125 | - $this->assertTrue($this->userView->file_exists('folder/inside.txt')); |
|
126 | - $this->userView->rmdir('folder'); |
|
127 | - [$storage,] = $this->userView->resolvePath('folder/inside.txt'); |
|
128 | - $storage->getScanner()->scan(''); // make sure we check the storage |
|
129 | - $this->assertFalse($this->userView->getFileInfo('folder')); |
|
130 | - |
|
131 | - // check if folder is in trashbin |
|
132 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
133 | - $this->assertCount(1, $results); |
|
134 | - $name = $results[0]->getName(); |
|
135 | - $this->assertEquals('folder', substr($name, 0, strrpos($name, '.'))); |
|
136 | - |
|
137 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/'); |
|
138 | - $this->assertEquals(1, count($results)); |
|
139 | - $name = $results[0]->getName(); |
|
140 | - $this->assertEquals('inside.txt', $name); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Test that deleting a file with a long filename puts it into the trashbin. |
|
145 | - */ |
|
146 | - public function testSingleStorageDeleteLongFilename(): void { |
|
147 | - $truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
148 | - |
|
149 | - $this->assertTrue($this->userView->file_exists(static::LONG_FILENAME)); |
|
150 | - $this->userView->unlink(static::LONG_FILENAME); |
|
151 | - [$storage,] = $this->userView->resolvePath(static::LONG_FILENAME); |
|
152 | - $storage->getScanner()->scan(''); // make sure we check the storage |
|
153 | - $this->assertFalse($this->userView->getFileInfo(static::LONG_FILENAME)); |
|
154 | - |
|
155 | - // check if file is in trashbin |
|
156 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
157 | - $this->assertCount(1, $results); |
|
158 | - $name = $results[0]->getName(); |
|
159 | - $this->assertEquals($truncatedFilename, substr($name, 0, strrpos($name, '.'))); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Test that deleting a file with the max filename length puts it into the trashbin. |
|
164 | - */ |
|
165 | - public function testSingleStorageDeleteMaxLengthFilename(): void { |
|
166 | - $truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
167 | - |
|
168 | - $this->assertTrue($this->userView->file_exists(static::MAX_FILENAME)); |
|
169 | - $this->userView->unlink(static::MAX_FILENAME); |
|
170 | - [$storage,] = $this->userView->resolvePath(static::MAX_FILENAME); |
|
171 | - $storage->getScanner()->scan(''); // make sure we check the storage |
|
172 | - $this->assertFalse($this->userView->getFileInfo(static::MAX_FILENAME)); |
|
173 | - |
|
174 | - // check if file is in trashbin |
|
175 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
176 | - $this->assertCount(1, $results); |
|
177 | - $name = $results[0]->getName(); |
|
178 | - $this->assertEquals($truncatedFilename, substr($name, 0, strrpos($name, '.'))); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Test that deleting a file from another mounted storage properly |
|
183 | - * lands in the trashbin. This is a cross-storage situation because |
|
184 | - * the trashbin folder is in the root storage while the mounted one |
|
185 | - * isn't. |
|
186 | - */ |
|
187 | - public function testCrossStorageDeleteFile(): void { |
|
188 | - $storage2 = new Temporary([]); |
|
189 | - Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
190 | - |
|
191 | - $this->userView->file_put_contents('substorage/subfile.txt', 'foo'); |
|
192 | - $storage2->getScanner()->scan(''); |
|
193 | - $this->assertTrue($storage2->file_exists('subfile.txt')); |
|
194 | - $this->userView->unlink('substorage/subfile.txt'); |
|
195 | - |
|
196 | - $storage2->getScanner()->scan(''); |
|
197 | - $this->assertFalse($this->userView->getFileInfo('substorage/subfile.txt')); |
|
198 | - $this->assertFalse($storage2->file_exists('subfile.txt')); |
|
199 | - |
|
200 | - // check if file is in trashbin |
|
201 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
202 | - $this->assertCount(1, $results); |
|
203 | - $name = $results[0]->getName(); |
|
204 | - $this->assertEquals('subfile.txt', substr($name, 0, strrpos($name, '.'))); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Test that deleting a folder from another mounted storage properly |
|
209 | - * lands in the trashbin. This is a cross-storage situation because |
|
210 | - * the trashbin folder is in the root storage while the mounted one |
|
211 | - * isn't. |
|
212 | - */ |
|
213 | - public function testCrossStorageDeleteFolder(): void { |
|
214 | - $storage2 = new Temporary([]); |
|
215 | - Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
216 | - |
|
217 | - $this->userView->mkdir('substorage/folder'); |
|
218 | - $this->userView->file_put_contents('substorage/folder/subfile.txt', 'bar'); |
|
219 | - $storage2->getScanner()->scan(''); |
|
220 | - $this->assertTrue($storage2->file_exists('folder/subfile.txt')); |
|
221 | - $this->userView->rmdir('substorage/folder'); |
|
222 | - |
|
223 | - $storage2->getScanner()->scan(''); |
|
224 | - $this->assertFalse($this->userView->getFileInfo('substorage/folder')); |
|
225 | - $this->assertFalse($storage2->file_exists('folder/subfile.txt')); |
|
226 | - |
|
227 | - // check if folder is in trashbin |
|
228 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
229 | - $this->assertCount(1, $results); |
|
230 | - $name = $results[0]->getName(); |
|
231 | - $this->assertEquals('folder', substr($name, 0, strrpos($name, '.'))); |
|
232 | - |
|
233 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/'); |
|
234 | - $this->assertCount(1, $results); |
|
235 | - $name = $results[0]->getName(); |
|
236 | - $this->assertEquals('subfile.txt', $name); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Test that deleted versions properly land in the trashbin. |
|
241 | - */ |
|
242 | - public function testDeleteVersionsOfFile(): void { |
|
243 | - // trigger a version (multiple would not work because of the expire logic) |
|
244 | - $this->userView->file_put_contents('test.txt', 'v1'); |
|
245 | - |
|
246 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/'); |
|
247 | - $this->assertEquals(1, count($results)); |
|
248 | - |
|
249 | - $this->userView->unlink('test.txt'); |
|
250 | - |
|
251 | - // rescan trash storage |
|
252 | - [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
253 | - $rootStorage->getScanner()->scan(''); |
|
254 | - |
|
255 | - // check if versions are in trashbin |
|
256 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
257 | - $this->assertCount(1, $results); |
|
258 | - $name = $results[0]->getName(); |
|
259 | - $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
260 | - |
|
261 | - // versions deleted |
|
262 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/'); |
|
263 | - $this->assertCount(0, $results); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Test that deleted versions properly land in the trashbin. |
|
268 | - */ |
|
269 | - public function testDeleteVersionsOfFolder(): void { |
|
270 | - // trigger a version (multiple would not work because of the expire logic) |
|
271 | - $this->userView->file_put_contents('folder/inside.txt', 'v1'); |
|
272 | - |
|
273 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); |
|
274 | - $this->assertCount(1, $results); |
|
275 | - |
|
276 | - $this->userView->rmdir('folder'); |
|
277 | - |
|
278 | - // rescan trash storage |
|
279 | - [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
280 | - $rootStorage->getScanner()->scan(''); |
|
281 | - |
|
282 | - // check if versions are in trashbin |
|
283 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
284 | - $this->assertCount(1, $results); |
|
285 | - $name = $results[0]->getName(); |
|
286 | - $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d'))); |
|
287 | - |
|
288 | - // check if versions are in trashbin |
|
289 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/'); |
|
290 | - $this->assertCount(1, $results); |
|
291 | - $name = $results[0]->getName(); |
|
292 | - $this->assertEquals('inside.txt.v', substr($name, 0, strlen('inside.txt.v'))); |
|
293 | - |
|
294 | - // versions deleted |
|
295 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); |
|
296 | - $this->assertCount(0, $results); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * Test that deleted versions properly land in the trashbin when deleting as share recipient. |
|
301 | - */ |
|
302 | - public function testDeleteVersionsOfFileAsRecipient(): void { |
|
303 | - $this->userView->mkdir('share'); |
|
304 | - // trigger a version (multiple would not work because of the expire logic) |
|
305 | - $this->userView->file_put_contents('share/test.txt', 'v1'); |
|
306 | - $this->userView->file_put_contents('share/test.txt', 'v2'); |
|
307 | - |
|
308 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/'); |
|
309 | - $this->assertCount(1, $results); |
|
310 | - |
|
311 | - $recipientUser = $this->getUniqueId('recipient_'); |
|
312 | - Server::get(IUserManager::class)->createUser($recipientUser, $recipientUser); |
|
313 | - |
|
314 | - $node = Server::get(IRootFolder::class)->getUserFolder($this->user)->get('share'); |
|
315 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
316 | - $share->setNode($node) |
|
317 | - ->setShareType(IShare::TYPE_USER) |
|
318 | - ->setSharedBy($this->user) |
|
319 | - ->setSharedWith($recipientUser) |
|
320 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
321 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
322 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, $recipientUser); |
|
323 | - |
|
324 | - $this->loginAsUser($recipientUser); |
|
325 | - |
|
326 | - // delete as recipient |
|
327 | - $recipientView = new View('/' . $recipientUser . '/files'); |
|
328 | - $recipientView->unlink('share/test.txt'); |
|
329 | - |
|
330 | - // rescan trash storage for both users |
|
331 | - [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
332 | - $rootStorage->getScanner()->scan(''); |
|
333 | - |
|
334 | - // check if versions are in trashbin for both users |
|
335 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
336 | - $this->assertCount(1, $results, 'Versions in owner\'s trashbin'); |
|
337 | - $name = $results[0]->getName(); |
|
338 | - $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
339 | - |
|
340 | - $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions'); |
|
341 | - $this->assertCount(1, $results, 'Versions in recipient\'s trashbin'); |
|
342 | - $name = $results[0]->getName(); |
|
343 | - $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
344 | - |
|
345 | - // versions deleted |
|
346 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/'); |
|
347 | - $this->assertCount(0, $results); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Test that deleted versions properly land in the trashbin when deleting as share recipient. |
|
352 | - */ |
|
353 | - public function testDeleteVersionsOfFolderAsRecipient(): void { |
|
354 | - $this->userView->mkdir('share'); |
|
355 | - $this->userView->mkdir('share/folder'); |
|
356 | - // trigger a version (multiple would not work because of the expire logic) |
|
357 | - $this->userView->file_put_contents('share/folder/test.txt', 'v1'); |
|
358 | - $this->userView->file_put_contents('share/folder/test.txt', 'v2'); |
|
359 | - |
|
360 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/folder/'); |
|
361 | - $this->assertCount(1, $results); |
|
362 | - |
|
363 | - $recipientUser = $this->getUniqueId('recipient_'); |
|
364 | - Server::get(IUserManager::class)->createUser($recipientUser, $recipientUser); |
|
365 | - $node = Server::get(IRootFolder::class)->getUserFolder($this->user)->get('share'); |
|
366 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
367 | - $share->setNode($node) |
|
368 | - ->setShareType(IShare::TYPE_USER) |
|
369 | - ->setSharedBy($this->user) |
|
370 | - ->setSharedWith($recipientUser) |
|
371 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
372 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
373 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, $recipientUser); |
|
374 | - |
|
375 | - $this->loginAsUser($recipientUser); |
|
376 | - |
|
377 | - // delete as recipient |
|
378 | - $recipientView = new View('/' . $recipientUser . '/files'); |
|
379 | - $recipientView->rmdir('share/folder'); |
|
380 | - |
|
381 | - // rescan trash storage |
|
382 | - [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
383 | - $rootStorage->getScanner()->scan(''); |
|
384 | - |
|
385 | - // check if versions are in trashbin for owner |
|
386 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
387 | - $this->assertCount(1, $results); |
|
388 | - $name = $results[0]->getName(); |
|
389 | - $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d'))); |
|
390 | - |
|
391 | - // check if file versions are in trashbin for owner |
|
392 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/'); |
|
393 | - $this->assertCount(1, $results); |
|
394 | - $name = $results[0]->getName(); |
|
395 | - $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
396 | - |
|
397 | - // check if versions are in trashbin for recipient |
|
398 | - $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions'); |
|
399 | - $this->assertCount(1, $results); |
|
400 | - $name = $results[0]->getName(); |
|
401 | - $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d'))); |
|
402 | - |
|
403 | - // check if file versions are in trashbin for recipient |
|
404 | - $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions/' . $name . '/'); |
|
405 | - $this->assertCount(1, $results); |
|
406 | - $name = $results[0]->getName(); |
|
407 | - $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
408 | - |
|
409 | - // versions deleted |
|
410 | - $results = $this->rootView->getDirectoryContent($recipientUser . '/files_versions/share/folder/'); |
|
411 | - $this->assertCount(0, $results); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Test that versions are not auto-trashed when moving a file between |
|
416 | - * storages. This is because rename() between storages would call |
|
417 | - * unlink() which should NOT trigger the version deletion logic. |
|
418 | - */ |
|
419 | - public function testKeepFileAndVersionsWhenMovingFileBetweenStorages(): void { |
|
420 | - $storage2 = new Temporary([]); |
|
421 | - Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
422 | - |
|
423 | - // trigger a version (multiple would not work because of the expire logic) |
|
424 | - $this->userView->file_put_contents('test.txt', 'v1'); |
|
425 | - |
|
426 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
427 | - $this->assertCount(0, $results); |
|
428 | - |
|
429 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/'); |
|
430 | - $this->assertCount(1, $results); |
|
431 | - |
|
432 | - // move to another storage |
|
433 | - $this->userView->rename('test.txt', 'substorage/test.txt'); |
|
434 | - $this->assertTrue($this->userView->file_exists('substorage/test.txt')); |
|
435 | - |
|
436 | - // rescan trash storage |
|
437 | - [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
438 | - $rootStorage->getScanner()->scan(''); |
|
439 | - |
|
440 | - // versions were moved too |
|
441 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage'); |
|
442 | - $this->assertCount(1, $results); |
|
443 | - |
|
444 | - // check that nothing got trashed by the rename's unlink() call |
|
445 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
446 | - $this->assertCount(0, $results); |
|
447 | - |
|
448 | - // check that versions were moved and not trashed |
|
449 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/'); |
|
450 | - $this->assertCount(0, $results); |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Test that versions are not auto-trashed when moving a file between |
|
455 | - * storages. This is because rename() between storages would call |
|
456 | - * unlink() which should NOT trigger the version deletion logic. |
|
457 | - */ |
|
458 | - public function testKeepFileAndVersionsWhenMovingFolderBetweenStorages(): void { |
|
459 | - $storage2 = new Temporary([]); |
|
460 | - Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
461 | - |
|
462 | - // trigger a version (multiple would not work because of the expire logic) |
|
463 | - $this->userView->file_put_contents('folder/inside.txt', 'v1'); |
|
464 | - |
|
465 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
466 | - $this->assertCount(0, $results); |
|
467 | - |
|
468 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); |
|
469 | - $this->assertCount(1, $results); |
|
470 | - |
|
471 | - // move to another storage |
|
472 | - $this->userView->rename('folder', 'substorage/folder'); |
|
473 | - $this->assertTrue($this->userView->file_exists('substorage/folder/inside.txt')); |
|
474 | - |
|
475 | - // rescan trash storage |
|
476 | - [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
477 | - $rootStorage->getScanner()->scan(''); |
|
478 | - |
|
479 | - // versions were moved too |
|
480 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage/folder/'); |
|
481 | - $this->assertCount(1, $results); |
|
482 | - |
|
483 | - // check that nothing got trashed by the rename's unlink() call |
|
484 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
485 | - $this->assertCount(0, $results); |
|
486 | - |
|
487 | - // check that versions were moved and not trashed |
|
488 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/'); |
|
489 | - $this->assertCount(0, $results); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Delete should fail if the source file can't be deleted. |
|
494 | - */ |
|
495 | - public function testSingleStorageDeleteFileFail(): void { |
|
496 | - /** |
|
497 | - * @var Temporary&MockObject $storage |
|
498 | - */ |
|
499 | - $storage = $this->getMockBuilder(Temporary::class) |
|
500 | - ->setConstructorArgs([[]]) |
|
501 | - ->onlyMethods(['rename', 'unlink', 'moveFromStorage']) |
|
502 | - ->getMock(); |
|
503 | - |
|
504 | - $storage->expects($this->any()) |
|
505 | - ->method('rename') |
|
506 | - ->willReturn(false); |
|
507 | - $storage->expects($this->any()) |
|
508 | - ->method('moveFromStorage') |
|
509 | - ->willReturn(false); |
|
510 | - $storage->expects($this->any()) |
|
511 | - ->method('unlink') |
|
512 | - ->willReturn(false); |
|
513 | - |
|
514 | - $cache = $storage->getCache(); |
|
515 | - |
|
516 | - Filesystem::mount($storage, [], '/' . $this->user); |
|
517 | - $storage->mkdir('files'); |
|
518 | - $this->userView->file_put_contents('test.txt', 'foo'); |
|
519 | - $this->assertTrue($storage->file_exists('files/test.txt')); |
|
520 | - $this->assertFalse($this->userView->unlink('test.txt')); |
|
521 | - $this->assertTrue($storage->file_exists('files/test.txt')); |
|
522 | - $this->assertTrue($cache->inCache('files/test.txt')); |
|
523 | - |
|
524 | - // file should not be in the trashbin |
|
525 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
526 | - $this->assertCount(0, $results); |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Delete should fail if the source folder can't be deleted. |
|
531 | - */ |
|
532 | - public function testSingleStorageDeleteFolderFail(): void { |
|
533 | - /** |
|
534 | - * @var Temporary&MockObject $storage |
|
535 | - */ |
|
536 | - $storage = $this->getMockBuilder(Temporary::class) |
|
537 | - ->setConstructorArgs([[]]) |
|
538 | - ->onlyMethods(['rename', 'unlink', 'rmdir']) |
|
539 | - ->getMock(); |
|
540 | - |
|
541 | - $storage->expects($this->any()) |
|
542 | - ->method('rmdir') |
|
543 | - ->willReturn(false); |
|
544 | - |
|
545 | - $cache = $storage->getCache(); |
|
546 | - |
|
547 | - Filesystem::mount($storage, [], '/' . $this->user); |
|
548 | - $storage->mkdir('files'); |
|
549 | - $this->userView->mkdir('folder'); |
|
550 | - $this->userView->file_put_contents('folder/test.txt', 'foo'); |
|
551 | - $this->assertTrue($storage->file_exists('files/folder/test.txt')); |
|
552 | - $this->assertFalse($this->userView->rmdir('files/folder')); |
|
553 | - $this->assertTrue($storage->file_exists('files/folder')); |
|
554 | - $this->assertTrue($storage->file_exists('files/folder/test.txt')); |
|
555 | - $this->assertTrue($cache->inCache('files/folder')); |
|
556 | - $this->assertTrue($cache->inCache('files/folder/test.txt')); |
|
557 | - |
|
558 | - // file should not be in the trashbin |
|
559 | - $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
560 | - $this->assertCount(0, $results); |
|
561 | - } |
|
562 | - |
|
563 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldMoveToTrash')] |
|
564 | - public function testShouldMoveToTrash(string $mountPoint, string $path, bool $userExists, bool $appDisablesTrash, bool $expected): void { |
|
565 | - $fileID = 1; |
|
566 | - $cache = $this->createMock(ICache::class); |
|
567 | - $cache->expects($this->any())->method('getId')->willReturn($fileID); |
|
568 | - $tmpStorage = $this->createMock(Temporary::class); |
|
569 | - $tmpStorage->expects($this->any())->method('getCache')->willReturn($cache); |
|
570 | - $userManager = $this->getMockBuilder(IUserManager::class) |
|
571 | - ->disableOriginalConstructor()->getMock(); |
|
572 | - $userManager->expects($this->any()) |
|
573 | - ->method('userExists')->willReturn($userExists); |
|
574 | - $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
|
575 | - $eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
576 | - $rootFolder = $this->createMock(IRootFolder::class); |
|
577 | - $userFolder = $this->createMock(Folder::class); |
|
578 | - $node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock(); |
|
579 | - $trashManager = $this->createMock(ITrashManager::class); |
|
580 | - $event = $this->getMockBuilder(MoveToTrashEvent::class)->disableOriginalConstructor()->getMock(); |
|
581 | - $event->expects($this->any())->method('shouldMoveToTrashBin')->willReturn(!$appDisablesTrash); |
|
582 | - |
|
583 | - $userFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]); |
|
584 | - $rootFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]); |
|
585 | - $rootFolder->expects($this->any())->method('getUserFolder')->willReturn($userFolder); |
|
586 | - |
|
587 | - $storage = $this->getMockBuilder(Storage::class) |
|
588 | - ->setConstructorArgs( |
|
589 | - [ |
|
590 | - ['mountPoint' => $mountPoint, 'storage' => $tmpStorage], |
|
591 | - $trashManager, |
|
592 | - $userManager, |
|
593 | - $logger, |
|
594 | - $eventDispatcher, |
|
595 | - $rootFolder |
|
596 | - ] |
|
597 | - ) |
|
598 | - ->onlyMethods(['createMoveToTrashEvent']) |
|
599 | - ->getMock(); |
|
600 | - |
|
601 | - $storage->expects($this->any())->method('createMoveToTrashEvent')->with($node) |
|
602 | - ->willReturn($event); |
|
603 | - |
|
604 | - $this->assertSame($expected, |
|
605 | - $this->invokePrivate($storage, 'shouldMoveToTrash', [$path]) |
|
606 | - ); |
|
607 | - } |
|
608 | - |
|
609 | - public static function dataTestShouldMoveToTrash(): array { |
|
610 | - return [ |
|
611 | - ['/schiesbn/', '/files/test.txt', true, false, true], |
|
612 | - ['/schiesbn/', '/files/test.txt', false, false, false], |
|
613 | - ['/schiesbn/', '/test.txt', true, false, false], |
|
614 | - ['/schiesbn/', '/test.txt', false, false, false], |
|
615 | - // other apps disables the trashbin |
|
616 | - ['/schiesbn/', '/files/test.txt', true, true, false], |
|
617 | - ['/schiesbn/', '/files/test.txt', false, true, false], |
|
618 | - ]; |
|
619 | - } |
|
620 | - |
|
621 | - /** |
|
622 | - * Test that deleting a file doesn't error when nobody is logged in |
|
623 | - */ |
|
624 | - public function testSingleStorageDeleteFileLoggedOut(): void { |
|
625 | - $this->logout(); |
|
626 | - |
|
627 | - if (!$this->userView->file_exists('test.txt')) { |
|
628 | - $this->markTestSkipped('Skipping since the current home storage backend requires the user to logged in'); |
|
629 | - } else { |
|
630 | - $this->userView->unlink('test.txt'); |
|
631 | - $this->addToAssertionCount(1); |
|
632 | - } |
|
633 | - } |
|
634 | - |
|
635 | - public function testTrashbinCollision(): void { |
|
636 | - $this->userView->file_put_contents('test.txt', 'foo'); |
|
637 | - $this->userView->file_put_contents('folder/test.txt', 'bar'); |
|
638 | - |
|
639 | - $timeFactory = $this->createMock(ITimeFactory::class); |
|
640 | - $timeFactory->method('getTime') |
|
641 | - ->willReturn(1000); |
|
642 | - |
|
643 | - $lockingProvider = Server::get(ILockingProvider::class); |
|
644 | - |
|
645 | - $this->overwriteService(ITimeFactory::class, $timeFactory); |
|
646 | - |
|
647 | - $this->userView->unlink('test.txt'); |
|
648 | - |
|
649 | - $this->assertTrue($this->rootView->file_exists('/' . $this->user . '/files_trashbin/files/test.txt.d1000')); |
|
650 | - |
|
651 | - /** @var \OC\Files\Storage\Storage $trashStorage */ |
|
652 | - [$trashStorage, $trashInternalPath] = $this->rootView->resolvePath('/' . $this->user . '/files_trashbin/files/test.txt.d1000'); |
|
653 | - |
|
654 | - /// simulate a concurrent delete |
|
655 | - $trashStorage->acquireLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
656 | - |
|
657 | - $this->userView->unlink('folder/test.txt'); |
|
658 | - |
|
659 | - $trashStorage->releaseLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
660 | - |
|
661 | - $this->assertTrue($this->rootView->file_exists($this->user . '/files_trashbin/files/test.txt.d1001')); |
|
662 | - |
|
663 | - $this->assertEquals('foo', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1000')); |
|
664 | - $this->assertEquals('bar', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1001')); |
|
665 | - } |
|
666 | - |
|
667 | - public function testMoveFromStoragePreserveFileId(): void { |
|
668 | - $this->userView->file_put_contents('test.txt', 'foo'); |
|
669 | - $fileId = $this->userView->getFileInfo('test.txt')->getId(); |
|
670 | - |
|
671 | - $externalStorage = new TemporaryNoCross([]); |
|
672 | - $externalStorage->getScanner()->scan(''); |
|
673 | - Filesystem::mount($externalStorage, [], '/' . $this->user . '/files/storage'); |
|
674 | - |
|
675 | - $this->assertTrue($this->userView->rename('test.txt', 'storage/test.txt')); |
|
676 | - $this->assertTrue($externalStorage->file_exists('test.txt')); |
|
677 | - |
|
678 | - $this->assertEquals($fileId, $this->userView->getFileInfo('storage/test.txt')->getId()); |
|
679 | - } |
|
54 | + use MountProviderTrait; |
|
55 | + |
|
56 | + private string $user; |
|
57 | + private View $rootView; |
|
58 | + private View $userView; |
|
59 | + |
|
60 | + // 239 chars so appended timestamp of 12 chars will exceed max length of 250 chars |
|
61 | + private const LONG_FILENAME = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
62 | + // 250 chars |
|
63 | + private const MAX_FILENAME = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
64 | + |
|
65 | + protected function setUp(): void { |
|
66 | + parent::setUp(); |
|
67 | + |
|
68 | + \OC_Hook::clear(); |
|
69 | + \OC::$server->boot(); |
|
70 | + |
|
71 | + // register trashbin hooks |
|
72 | + $trashbinApp = new Application(); |
|
73 | + $trashbinApp->boot($this->createMock(IBootContext::class)); |
|
74 | + |
|
75 | + $this->user = $this->getUniqueId('user'); |
|
76 | + Server::get(IUserManager::class)->createUser($this->user, $this->user); |
|
77 | + |
|
78 | + // this will setup the FS |
|
79 | + $this->loginAsUser($this->user); |
|
80 | + |
|
81 | + Storage::setupStorage(); |
|
82 | + |
|
83 | + $this->rootView = new View('/'); |
|
84 | + $this->userView = new View('/' . $this->user . '/files/'); |
|
85 | + $this->userView->file_put_contents('test.txt', 'foo'); |
|
86 | + $this->userView->file_put_contents(static::LONG_FILENAME, 'foo'); |
|
87 | + $this->userView->file_put_contents(static::MAX_FILENAME, 'foo'); |
|
88 | + |
|
89 | + $this->userView->mkdir('folder'); |
|
90 | + $this->userView->file_put_contents('folder/inside.txt', 'bar'); |
|
91 | + } |
|
92 | + |
|
93 | + protected function tearDown(): void { |
|
94 | + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); |
|
95 | + $this->logout(); |
|
96 | + $user = Server::get(IUserManager::class)->get($this->user); |
|
97 | + if ($user !== null) { |
|
98 | + $user->delete(); |
|
99 | + } |
|
100 | + \OC_Hook::clear(); |
|
101 | + parent::tearDown(); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Test that deleting a file puts it into the trashbin. |
|
106 | + */ |
|
107 | + public function testSingleStorageDeleteFile(): void { |
|
108 | + $this->assertTrue($this->userView->file_exists('test.txt')); |
|
109 | + $this->userView->unlink('test.txt'); |
|
110 | + [$storage,] = $this->userView->resolvePath('test.txt'); |
|
111 | + $storage->getScanner()->scan(''); // make sure we check the storage |
|
112 | + $this->assertFalse($this->userView->getFileInfo('test.txt')); |
|
113 | + |
|
114 | + // check if file is in trashbin |
|
115 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
116 | + $this->assertCount(1, $results); |
|
117 | + $name = $results[0]->getName(); |
|
118 | + $this->assertEquals('test.txt', substr($name, 0, strrpos($name, '.'))); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Test that deleting a folder puts it into the trashbin. |
|
123 | + */ |
|
124 | + public function testSingleStorageDeleteFolder(): void { |
|
125 | + $this->assertTrue($this->userView->file_exists('folder/inside.txt')); |
|
126 | + $this->userView->rmdir('folder'); |
|
127 | + [$storage,] = $this->userView->resolvePath('folder/inside.txt'); |
|
128 | + $storage->getScanner()->scan(''); // make sure we check the storage |
|
129 | + $this->assertFalse($this->userView->getFileInfo('folder')); |
|
130 | + |
|
131 | + // check if folder is in trashbin |
|
132 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
133 | + $this->assertCount(1, $results); |
|
134 | + $name = $results[0]->getName(); |
|
135 | + $this->assertEquals('folder', substr($name, 0, strrpos($name, '.'))); |
|
136 | + |
|
137 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/'); |
|
138 | + $this->assertEquals(1, count($results)); |
|
139 | + $name = $results[0]->getName(); |
|
140 | + $this->assertEquals('inside.txt', $name); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Test that deleting a file with a long filename puts it into the trashbin. |
|
145 | + */ |
|
146 | + public function testSingleStorageDeleteLongFilename(): void { |
|
147 | + $truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
148 | + |
|
149 | + $this->assertTrue($this->userView->file_exists(static::LONG_FILENAME)); |
|
150 | + $this->userView->unlink(static::LONG_FILENAME); |
|
151 | + [$storage,] = $this->userView->resolvePath(static::LONG_FILENAME); |
|
152 | + $storage->getScanner()->scan(''); // make sure we check the storage |
|
153 | + $this->assertFalse($this->userView->getFileInfo(static::LONG_FILENAME)); |
|
154 | + |
|
155 | + // check if file is in trashbin |
|
156 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
157 | + $this->assertCount(1, $results); |
|
158 | + $name = $results[0]->getName(); |
|
159 | + $this->assertEquals($truncatedFilename, substr($name, 0, strrpos($name, '.'))); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Test that deleting a file with the max filename length puts it into the trashbin. |
|
164 | + */ |
|
165 | + public function testSingleStorageDeleteMaxLengthFilename(): void { |
|
166 | + $truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'; |
|
167 | + |
|
168 | + $this->assertTrue($this->userView->file_exists(static::MAX_FILENAME)); |
|
169 | + $this->userView->unlink(static::MAX_FILENAME); |
|
170 | + [$storage,] = $this->userView->resolvePath(static::MAX_FILENAME); |
|
171 | + $storage->getScanner()->scan(''); // make sure we check the storage |
|
172 | + $this->assertFalse($this->userView->getFileInfo(static::MAX_FILENAME)); |
|
173 | + |
|
174 | + // check if file is in trashbin |
|
175 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
176 | + $this->assertCount(1, $results); |
|
177 | + $name = $results[0]->getName(); |
|
178 | + $this->assertEquals($truncatedFilename, substr($name, 0, strrpos($name, '.'))); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Test that deleting a file from another mounted storage properly |
|
183 | + * lands in the trashbin. This is a cross-storage situation because |
|
184 | + * the trashbin folder is in the root storage while the mounted one |
|
185 | + * isn't. |
|
186 | + */ |
|
187 | + public function testCrossStorageDeleteFile(): void { |
|
188 | + $storage2 = new Temporary([]); |
|
189 | + Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
190 | + |
|
191 | + $this->userView->file_put_contents('substorage/subfile.txt', 'foo'); |
|
192 | + $storage2->getScanner()->scan(''); |
|
193 | + $this->assertTrue($storage2->file_exists('subfile.txt')); |
|
194 | + $this->userView->unlink('substorage/subfile.txt'); |
|
195 | + |
|
196 | + $storage2->getScanner()->scan(''); |
|
197 | + $this->assertFalse($this->userView->getFileInfo('substorage/subfile.txt')); |
|
198 | + $this->assertFalse($storage2->file_exists('subfile.txt')); |
|
199 | + |
|
200 | + // check if file is in trashbin |
|
201 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
202 | + $this->assertCount(1, $results); |
|
203 | + $name = $results[0]->getName(); |
|
204 | + $this->assertEquals('subfile.txt', substr($name, 0, strrpos($name, '.'))); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Test that deleting a folder from another mounted storage properly |
|
209 | + * lands in the trashbin. This is a cross-storage situation because |
|
210 | + * the trashbin folder is in the root storage while the mounted one |
|
211 | + * isn't. |
|
212 | + */ |
|
213 | + public function testCrossStorageDeleteFolder(): void { |
|
214 | + $storage2 = new Temporary([]); |
|
215 | + Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
216 | + |
|
217 | + $this->userView->mkdir('substorage/folder'); |
|
218 | + $this->userView->file_put_contents('substorage/folder/subfile.txt', 'bar'); |
|
219 | + $storage2->getScanner()->scan(''); |
|
220 | + $this->assertTrue($storage2->file_exists('folder/subfile.txt')); |
|
221 | + $this->userView->rmdir('substorage/folder'); |
|
222 | + |
|
223 | + $storage2->getScanner()->scan(''); |
|
224 | + $this->assertFalse($this->userView->getFileInfo('substorage/folder')); |
|
225 | + $this->assertFalse($storage2->file_exists('folder/subfile.txt')); |
|
226 | + |
|
227 | + // check if folder is in trashbin |
|
228 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
229 | + $this->assertCount(1, $results); |
|
230 | + $name = $results[0]->getName(); |
|
231 | + $this->assertEquals('folder', substr($name, 0, strrpos($name, '.'))); |
|
232 | + |
|
233 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/' . $name . '/'); |
|
234 | + $this->assertCount(1, $results); |
|
235 | + $name = $results[0]->getName(); |
|
236 | + $this->assertEquals('subfile.txt', $name); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Test that deleted versions properly land in the trashbin. |
|
241 | + */ |
|
242 | + public function testDeleteVersionsOfFile(): void { |
|
243 | + // trigger a version (multiple would not work because of the expire logic) |
|
244 | + $this->userView->file_put_contents('test.txt', 'v1'); |
|
245 | + |
|
246 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/'); |
|
247 | + $this->assertEquals(1, count($results)); |
|
248 | + |
|
249 | + $this->userView->unlink('test.txt'); |
|
250 | + |
|
251 | + // rescan trash storage |
|
252 | + [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
253 | + $rootStorage->getScanner()->scan(''); |
|
254 | + |
|
255 | + // check if versions are in trashbin |
|
256 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
257 | + $this->assertCount(1, $results); |
|
258 | + $name = $results[0]->getName(); |
|
259 | + $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
260 | + |
|
261 | + // versions deleted |
|
262 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/'); |
|
263 | + $this->assertCount(0, $results); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Test that deleted versions properly land in the trashbin. |
|
268 | + */ |
|
269 | + public function testDeleteVersionsOfFolder(): void { |
|
270 | + // trigger a version (multiple would not work because of the expire logic) |
|
271 | + $this->userView->file_put_contents('folder/inside.txt', 'v1'); |
|
272 | + |
|
273 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); |
|
274 | + $this->assertCount(1, $results); |
|
275 | + |
|
276 | + $this->userView->rmdir('folder'); |
|
277 | + |
|
278 | + // rescan trash storage |
|
279 | + [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
280 | + $rootStorage->getScanner()->scan(''); |
|
281 | + |
|
282 | + // check if versions are in trashbin |
|
283 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
284 | + $this->assertCount(1, $results); |
|
285 | + $name = $results[0]->getName(); |
|
286 | + $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d'))); |
|
287 | + |
|
288 | + // check if versions are in trashbin |
|
289 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/'); |
|
290 | + $this->assertCount(1, $results); |
|
291 | + $name = $results[0]->getName(); |
|
292 | + $this->assertEquals('inside.txt.v', substr($name, 0, strlen('inside.txt.v'))); |
|
293 | + |
|
294 | + // versions deleted |
|
295 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); |
|
296 | + $this->assertCount(0, $results); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * Test that deleted versions properly land in the trashbin when deleting as share recipient. |
|
301 | + */ |
|
302 | + public function testDeleteVersionsOfFileAsRecipient(): void { |
|
303 | + $this->userView->mkdir('share'); |
|
304 | + // trigger a version (multiple would not work because of the expire logic) |
|
305 | + $this->userView->file_put_contents('share/test.txt', 'v1'); |
|
306 | + $this->userView->file_put_contents('share/test.txt', 'v2'); |
|
307 | + |
|
308 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/'); |
|
309 | + $this->assertCount(1, $results); |
|
310 | + |
|
311 | + $recipientUser = $this->getUniqueId('recipient_'); |
|
312 | + Server::get(IUserManager::class)->createUser($recipientUser, $recipientUser); |
|
313 | + |
|
314 | + $node = Server::get(IRootFolder::class)->getUserFolder($this->user)->get('share'); |
|
315 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
316 | + $share->setNode($node) |
|
317 | + ->setShareType(IShare::TYPE_USER) |
|
318 | + ->setSharedBy($this->user) |
|
319 | + ->setSharedWith($recipientUser) |
|
320 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
321 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
322 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, $recipientUser); |
|
323 | + |
|
324 | + $this->loginAsUser($recipientUser); |
|
325 | + |
|
326 | + // delete as recipient |
|
327 | + $recipientView = new View('/' . $recipientUser . '/files'); |
|
328 | + $recipientView->unlink('share/test.txt'); |
|
329 | + |
|
330 | + // rescan trash storage for both users |
|
331 | + [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
332 | + $rootStorage->getScanner()->scan(''); |
|
333 | + |
|
334 | + // check if versions are in trashbin for both users |
|
335 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
336 | + $this->assertCount(1, $results, 'Versions in owner\'s trashbin'); |
|
337 | + $name = $results[0]->getName(); |
|
338 | + $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
339 | + |
|
340 | + $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions'); |
|
341 | + $this->assertCount(1, $results, 'Versions in recipient\'s trashbin'); |
|
342 | + $name = $results[0]->getName(); |
|
343 | + $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
344 | + |
|
345 | + // versions deleted |
|
346 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/'); |
|
347 | + $this->assertCount(0, $results); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Test that deleted versions properly land in the trashbin when deleting as share recipient. |
|
352 | + */ |
|
353 | + public function testDeleteVersionsOfFolderAsRecipient(): void { |
|
354 | + $this->userView->mkdir('share'); |
|
355 | + $this->userView->mkdir('share/folder'); |
|
356 | + // trigger a version (multiple would not work because of the expire logic) |
|
357 | + $this->userView->file_put_contents('share/folder/test.txt', 'v1'); |
|
358 | + $this->userView->file_put_contents('share/folder/test.txt', 'v2'); |
|
359 | + |
|
360 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/share/folder/'); |
|
361 | + $this->assertCount(1, $results); |
|
362 | + |
|
363 | + $recipientUser = $this->getUniqueId('recipient_'); |
|
364 | + Server::get(IUserManager::class)->createUser($recipientUser, $recipientUser); |
|
365 | + $node = Server::get(IRootFolder::class)->getUserFolder($this->user)->get('share'); |
|
366 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
367 | + $share->setNode($node) |
|
368 | + ->setShareType(IShare::TYPE_USER) |
|
369 | + ->setSharedBy($this->user) |
|
370 | + ->setSharedWith($recipientUser) |
|
371 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
372 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
373 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, $recipientUser); |
|
374 | + |
|
375 | + $this->loginAsUser($recipientUser); |
|
376 | + |
|
377 | + // delete as recipient |
|
378 | + $recipientView = new View('/' . $recipientUser . '/files'); |
|
379 | + $recipientView->rmdir('share/folder'); |
|
380 | + |
|
381 | + // rescan trash storage |
|
382 | + [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
383 | + $rootStorage->getScanner()->scan(''); |
|
384 | + |
|
385 | + // check if versions are in trashbin for owner |
|
386 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions'); |
|
387 | + $this->assertCount(1, $results); |
|
388 | + $name = $results[0]->getName(); |
|
389 | + $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d'))); |
|
390 | + |
|
391 | + // check if file versions are in trashbin for owner |
|
392 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/' . $name . '/'); |
|
393 | + $this->assertCount(1, $results); |
|
394 | + $name = $results[0]->getName(); |
|
395 | + $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
396 | + |
|
397 | + // check if versions are in trashbin for recipient |
|
398 | + $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions'); |
|
399 | + $this->assertCount(1, $results); |
|
400 | + $name = $results[0]->getName(); |
|
401 | + $this->assertEquals('folder.d', substr($name, 0, strlen('folder.d'))); |
|
402 | + |
|
403 | + // check if file versions are in trashbin for recipient |
|
404 | + $results = $this->rootView->getDirectoryContent($recipientUser . '/files_trashbin/versions/' . $name . '/'); |
|
405 | + $this->assertCount(1, $results); |
|
406 | + $name = $results[0]->getName(); |
|
407 | + $this->assertEquals('test.txt.v', substr($name, 0, strlen('test.txt.v'))); |
|
408 | + |
|
409 | + // versions deleted |
|
410 | + $results = $this->rootView->getDirectoryContent($recipientUser . '/files_versions/share/folder/'); |
|
411 | + $this->assertCount(0, $results); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Test that versions are not auto-trashed when moving a file between |
|
416 | + * storages. This is because rename() between storages would call |
|
417 | + * unlink() which should NOT trigger the version deletion logic. |
|
418 | + */ |
|
419 | + public function testKeepFileAndVersionsWhenMovingFileBetweenStorages(): void { |
|
420 | + $storage2 = new Temporary([]); |
|
421 | + Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
422 | + |
|
423 | + // trigger a version (multiple would not work because of the expire logic) |
|
424 | + $this->userView->file_put_contents('test.txt', 'v1'); |
|
425 | + |
|
426 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
427 | + $this->assertCount(0, $results); |
|
428 | + |
|
429 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/'); |
|
430 | + $this->assertCount(1, $results); |
|
431 | + |
|
432 | + // move to another storage |
|
433 | + $this->userView->rename('test.txt', 'substorage/test.txt'); |
|
434 | + $this->assertTrue($this->userView->file_exists('substorage/test.txt')); |
|
435 | + |
|
436 | + // rescan trash storage |
|
437 | + [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
438 | + $rootStorage->getScanner()->scan(''); |
|
439 | + |
|
440 | + // versions were moved too |
|
441 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage'); |
|
442 | + $this->assertCount(1, $results); |
|
443 | + |
|
444 | + // check that nothing got trashed by the rename's unlink() call |
|
445 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
446 | + $this->assertCount(0, $results); |
|
447 | + |
|
448 | + // check that versions were moved and not trashed |
|
449 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/'); |
|
450 | + $this->assertCount(0, $results); |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Test that versions are not auto-trashed when moving a file between |
|
455 | + * storages. This is because rename() between storages would call |
|
456 | + * unlink() which should NOT trigger the version deletion logic. |
|
457 | + */ |
|
458 | + public function testKeepFileAndVersionsWhenMovingFolderBetweenStorages(): void { |
|
459 | + $storage2 = new Temporary([]); |
|
460 | + Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
461 | + |
|
462 | + // trigger a version (multiple would not work because of the expire logic) |
|
463 | + $this->userView->file_put_contents('folder/inside.txt', 'v1'); |
|
464 | + |
|
465 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
466 | + $this->assertCount(0, $results); |
|
467 | + |
|
468 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); |
|
469 | + $this->assertCount(1, $results); |
|
470 | + |
|
471 | + // move to another storage |
|
472 | + $this->userView->rename('folder', 'substorage/folder'); |
|
473 | + $this->assertTrue($this->userView->file_exists('substorage/folder/inside.txt')); |
|
474 | + |
|
475 | + // rescan trash storage |
|
476 | + [$rootStorage,] = $this->rootView->resolvePath($this->user . '/files_trashbin'); |
|
477 | + $rootStorage->getScanner()->scan(''); |
|
478 | + |
|
479 | + // versions were moved too |
|
480 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage/folder/'); |
|
481 | + $this->assertCount(1, $results); |
|
482 | + |
|
483 | + // check that nothing got trashed by the rename's unlink() call |
|
484 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); |
|
485 | + $this->assertCount(0, $results); |
|
486 | + |
|
487 | + // check that versions were moved and not trashed |
|
488 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/'); |
|
489 | + $this->assertCount(0, $results); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Delete should fail if the source file can't be deleted. |
|
494 | + */ |
|
495 | + public function testSingleStorageDeleteFileFail(): void { |
|
496 | + /** |
|
497 | + * @var Temporary&MockObject $storage |
|
498 | + */ |
|
499 | + $storage = $this->getMockBuilder(Temporary::class) |
|
500 | + ->setConstructorArgs([[]]) |
|
501 | + ->onlyMethods(['rename', 'unlink', 'moveFromStorage']) |
|
502 | + ->getMock(); |
|
503 | + |
|
504 | + $storage->expects($this->any()) |
|
505 | + ->method('rename') |
|
506 | + ->willReturn(false); |
|
507 | + $storage->expects($this->any()) |
|
508 | + ->method('moveFromStorage') |
|
509 | + ->willReturn(false); |
|
510 | + $storage->expects($this->any()) |
|
511 | + ->method('unlink') |
|
512 | + ->willReturn(false); |
|
513 | + |
|
514 | + $cache = $storage->getCache(); |
|
515 | + |
|
516 | + Filesystem::mount($storage, [], '/' . $this->user); |
|
517 | + $storage->mkdir('files'); |
|
518 | + $this->userView->file_put_contents('test.txt', 'foo'); |
|
519 | + $this->assertTrue($storage->file_exists('files/test.txt')); |
|
520 | + $this->assertFalse($this->userView->unlink('test.txt')); |
|
521 | + $this->assertTrue($storage->file_exists('files/test.txt')); |
|
522 | + $this->assertTrue($cache->inCache('files/test.txt')); |
|
523 | + |
|
524 | + // file should not be in the trashbin |
|
525 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
526 | + $this->assertCount(0, $results); |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Delete should fail if the source folder can't be deleted. |
|
531 | + */ |
|
532 | + public function testSingleStorageDeleteFolderFail(): void { |
|
533 | + /** |
|
534 | + * @var Temporary&MockObject $storage |
|
535 | + */ |
|
536 | + $storage = $this->getMockBuilder(Temporary::class) |
|
537 | + ->setConstructorArgs([[]]) |
|
538 | + ->onlyMethods(['rename', 'unlink', 'rmdir']) |
|
539 | + ->getMock(); |
|
540 | + |
|
541 | + $storage->expects($this->any()) |
|
542 | + ->method('rmdir') |
|
543 | + ->willReturn(false); |
|
544 | + |
|
545 | + $cache = $storage->getCache(); |
|
546 | + |
|
547 | + Filesystem::mount($storage, [], '/' . $this->user); |
|
548 | + $storage->mkdir('files'); |
|
549 | + $this->userView->mkdir('folder'); |
|
550 | + $this->userView->file_put_contents('folder/test.txt', 'foo'); |
|
551 | + $this->assertTrue($storage->file_exists('files/folder/test.txt')); |
|
552 | + $this->assertFalse($this->userView->rmdir('files/folder')); |
|
553 | + $this->assertTrue($storage->file_exists('files/folder')); |
|
554 | + $this->assertTrue($storage->file_exists('files/folder/test.txt')); |
|
555 | + $this->assertTrue($cache->inCache('files/folder')); |
|
556 | + $this->assertTrue($cache->inCache('files/folder/test.txt')); |
|
557 | + |
|
558 | + // file should not be in the trashbin |
|
559 | + $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/'); |
|
560 | + $this->assertCount(0, $results); |
|
561 | + } |
|
562 | + |
|
563 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldMoveToTrash')] |
|
564 | + public function testShouldMoveToTrash(string $mountPoint, string $path, bool $userExists, bool $appDisablesTrash, bool $expected): void { |
|
565 | + $fileID = 1; |
|
566 | + $cache = $this->createMock(ICache::class); |
|
567 | + $cache->expects($this->any())->method('getId')->willReturn($fileID); |
|
568 | + $tmpStorage = $this->createMock(Temporary::class); |
|
569 | + $tmpStorage->expects($this->any())->method('getCache')->willReturn($cache); |
|
570 | + $userManager = $this->getMockBuilder(IUserManager::class) |
|
571 | + ->disableOriginalConstructor()->getMock(); |
|
572 | + $userManager->expects($this->any()) |
|
573 | + ->method('userExists')->willReturn($userExists); |
|
574 | + $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
|
575 | + $eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
576 | + $rootFolder = $this->createMock(IRootFolder::class); |
|
577 | + $userFolder = $this->createMock(Folder::class); |
|
578 | + $node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock(); |
|
579 | + $trashManager = $this->createMock(ITrashManager::class); |
|
580 | + $event = $this->getMockBuilder(MoveToTrashEvent::class)->disableOriginalConstructor()->getMock(); |
|
581 | + $event->expects($this->any())->method('shouldMoveToTrashBin')->willReturn(!$appDisablesTrash); |
|
582 | + |
|
583 | + $userFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]); |
|
584 | + $rootFolder->expects($this->any())->method('getById')->with($fileID)->willReturn([$node]); |
|
585 | + $rootFolder->expects($this->any())->method('getUserFolder')->willReturn($userFolder); |
|
586 | + |
|
587 | + $storage = $this->getMockBuilder(Storage::class) |
|
588 | + ->setConstructorArgs( |
|
589 | + [ |
|
590 | + ['mountPoint' => $mountPoint, 'storage' => $tmpStorage], |
|
591 | + $trashManager, |
|
592 | + $userManager, |
|
593 | + $logger, |
|
594 | + $eventDispatcher, |
|
595 | + $rootFolder |
|
596 | + ] |
|
597 | + ) |
|
598 | + ->onlyMethods(['createMoveToTrashEvent']) |
|
599 | + ->getMock(); |
|
600 | + |
|
601 | + $storage->expects($this->any())->method('createMoveToTrashEvent')->with($node) |
|
602 | + ->willReturn($event); |
|
603 | + |
|
604 | + $this->assertSame($expected, |
|
605 | + $this->invokePrivate($storage, 'shouldMoveToTrash', [$path]) |
|
606 | + ); |
|
607 | + } |
|
608 | + |
|
609 | + public static function dataTestShouldMoveToTrash(): array { |
|
610 | + return [ |
|
611 | + ['/schiesbn/', '/files/test.txt', true, false, true], |
|
612 | + ['/schiesbn/', '/files/test.txt', false, false, false], |
|
613 | + ['/schiesbn/', '/test.txt', true, false, false], |
|
614 | + ['/schiesbn/', '/test.txt', false, false, false], |
|
615 | + // other apps disables the trashbin |
|
616 | + ['/schiesbn/', '/files/test.txt', true, true, false], |
|
617 | + ['/schiesbn/', '/files/test.txt', false, true, false], |
|
618 | + ]; |
|
619 | + } |
|
620 | + |
|
621 | + /** |
|
622 | + * Test that deleting a file doesn't error when nobody is logged in |
|
623 | + */ |
|
624 | + public function testSingleStorageDeleteFileLoggedOut(): void { |
|
625 | + $this->logout(); |
|
626 | + |
|
627 | + if (!$this->userView->file_exists('test.txt')) { |
|
628 | + $this->markTestSkipped('Skipping since the current home storage backend requires the user to logged in'); |
|
629 | + } else { |
|
630 | + $this->userView->unlink('test.txt'); |
|
631 | + $this->addToAssertionCount(1); |
|
632 | + } |
|
633 | + } |
|
634 | + |
|
635 | + public function testTrashbinCollision(): void { |
|
636 | + $this->userView->file_put_contents('test.txt', 'foo'); |
|
637 | + $this->userView->file_put_contents('folder/test.txt', 'bar'); |
|
638 | + |
|
639 | + $timeFactory = $this->createMock(ITimeFactory::class); |
|
640 | + $timeFactory->method('getTime') |
|
641 | + ->willReturn(1000); |
|
642 | + |
|
643 | + $lockingProvider = Server::get(ILockingProvider::class); |
|
644 | + |
|
645 | + $this->overwriteService(ITimeFactory::class, $timeFactory); |
|
646 | + |
|
647 | + $this->userView->unlink('test.txt'); |
|
648 | + |
|
649 | + $this->assertTrue($this->rootView->file_exists('/' . $this->user . '/files_trashbin/files/test.txt.d1000')); |
|
650 | + |
|
651 | + /** @var \OC\Files\Storage\Storage $trashStorage */ |
|
652 | + [$trashStorage, $trashInternalPath] = $this->rootView->resolvePath('/' . $this->user . '/files_trashbin/files/test.txt.d1000'); |
|
653 | + |
|
654 | + /// simulate a concurrent delete |
|
655 | + $trashStorage->acquireLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
656 | + |
|
657 | + $this->userView->unlink('folder/test.txt'); |
|
658 | + |
|
659 | + $trashStorage->releaseLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
660 | + |
|
661 | + $this->assertTrue($this->rootView->file_exists($this->user . '/files_trashbin/files/test.txt.d1001')); |
|
662 | + |
|
663 | + $this->assertEquals('foo', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1000')); |
|
664 | + $this->assertEquals('bar', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1001')); |
|
665 | + } |
|
666 | + |
|
667 | + public function testMoveFromStoragePreserveFileId(): void { |
|
668 | + $this->userView->file_put_contents('test.txt', 'foo'); |
|
669 | + $fileId = $this->userView->getFileInfo('test.txt')->getId(); |
|
670 | + |
|
671 | + $externalStorage = new TemporaryNoCross([]); |
|
672 | + $externalStorage->getScanner()->scan(''); |
|
673 | + Filesystem::mount($externalStorage, [], '/' . $this->user . '/files/storage'); |
|
674 | + |
|
675 | + $this->assertTrue($this->userView->rename('test.txt', 'storage/test.txt')); |
|
676 | + $this->assertTrue($externalStorage->file_exists('test.txt')); |
|
677 | + |
|
678 | + $this->assertEquals($fileId, $this->userView->getFileInfo('storage/test.txt')->getId()); |
|
679 | + } |
|
680 | 680 | } |
@@ -47,961 +47,961 @@ |
||
47 | 47 | use Psr\Log\LoggerInterface; |
48 | 48 | |
49 | 49 | class Storage { |
50 | - public const DEFAULTENABLED = true; |
|
51 | - public const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota |
|
52 | - public const VERSIONS_ROOT = 'files_versions/'; |
|
53 | - |
|
54 | - public const DELETE_TRIGGER_MASTER_REMOVED = 0; |
|
55 | - public const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1; |
|
56 | - public const DELETE_TRIGGER_QUOTA_EXCEEDED = 2; |
|
57 | - |
|
58 | - // files for which we can remove the versions after the delete operation was successful |
|
59 | - private static $deletedFiles = []; |
|
60 | - |
|
61 | - private static $sourcePathAndUser = []; |
|
62 | - |
|
63 | - private static $max_versions_per_interval = [ |
|
64 | - //first 10sec, one version every 2sec |
|
65 | - 1 => ['intervalEndsAfter' => 10, 'step' => 2], |
|
66 | - //next minute, one version every 10sec |
|
67 | - 2 => ['intervalEndsAfter' => 60, 'step' => 10], |
|
68 | - //next hour, one version every minute |
|
69 | - 3 => ['intervalEndsAfter' => 3600, 'step' => 60], |
|
70 | - //next 24h, one version every hour |
|
71 | - 4 => ['intervalEndsAfter' => 86400, 'step' => 3600], |
|
72 | - //next 30days, one version per day |
|
73 | - 5 => ['intervalEndsAfter' => 2592000, 'step' => 86400], |
|
74 | - //until the end one version per week |
|
75 | - 6 => ['intervalEndsAfter' => -1, 'step' => 604800], |
|
76 | - ]; |
|
77 | - |
|
78 | - /** @var Application */ |
|
79 | - private static $application; |
|
80 | - |
|
81 | - /** |
|
82 | - * get the UID of the owner of the file and the path to the file relative to |
|
83 | - * owners files folder |
|
84 | - * |
|
85 | - * @param string $filename |
|
86 | - * @return array |
|
87 | - * @throws NoUserException |
|
88 | - */ |
|
89 | - public static function getUidAndFilename($filename) { |
|
90 | - $uid = Filesystem::getOwner($filename); |
|
91 | - $userManager = Server::get(IUserManager::class); |
|
92 | - // if the user with the UID doesn't exists, e.g. because the UID points |
|
93 | - // to a remote user with a federated cloud ID we use the current logged-in |
|
94 | - // user. We need a valid local user to create the versions |
|
95 | - if (!$userManager->userExists($uid)) { |
|
96 | - $uid = OC_User::getUser(); |
|
97 | - } |
|
98 | - Filesystem::initMountPoints($uid); |
|
99 | - if ($uid !== OC_User::getUser()) { |
|
100 | - $info = Filesystem::getFileInfo($filename); |
|
101 | - $ownerView = new View('/' . $uid . '/files'); |
|
102 | - try { |
|
103 | - $filename = $ownerView->getPath($info['fileid']); |
|
104 | - // make sure that the file name doesn't end with a trailing slash |
|
105 | - // can for example happen single files shared across servers |
|
106 | - $filename = rtrim($filename, '/'); |
|
107 | - } catch (NotFoundException $e) { |
|
108 | - $filename = null; |
|
109 | - } |
|
110 | - } |
|
111 | - return [$uid, $filename]; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Remember the owner and the owner path of the source file |
|
116 | - * |
|
117 | - * @param string $source source path |
|
118 | - */ |
|
119 | - public static function setSourcePathAndUser($source) { |
|
120 | - [$uid, $path] = self::getUidAndFilename($source); |
|
121 | - self::$sourcePathAndUser[$source] = ['uid' => $uid, 'path' => $path]; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Gets the owner and the owner path from the source path |
|
126 | - * |
|
127 | - * @param string $source source path |
|
128 | - * @return array with user id and path |
|
129 | - */ |
|
130 | - public static function getSourcePathAndUser($source) { |
|
131 | - if (isset(self::$sourcePathAndUser[$source])) { |
|
132 | - $uid = self::$sourcePathAndUser[$source]['uid']; |
|
133 | - $path = self::$sourcePathAndUser[$source]['path']; |
|
134 | - unset(self::$sourcePathAndUser[$source]); |
|
135 | - } else { |
|
136 | - $uid = $path = false; |
|
137 | - } |
|
138 | - return [$uid, $path]; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * get current size of all versions from a given user |
|
143 | - * |
|
144 | - * @param string $user user who owns the versions |
|
145 | - * @return int versions size |
|
146 | - */ |
|
147 | - private static function getVersionsSize($user) { |
|
148 | - $view = new View('/' . $user); |
|
149 | - $fileInfo = $view->getFileInfo('/files_versions'); |
|
150 | - return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * store a new version of a file. |
|
155 | - */ |
|
156 | - public static function store($filename) { |
|
157 | - // if the file gets streamed we need to remove the .part extension |
|
158 | - // to get the right target |
|
159 | - $ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
160 | - if ($ext === 'part') { |
|
161 | - $filename = substr($filename, 0, -5); |
|
162 | - } |
|
163 | - |
|
164 | - // we only handle existing files |
|
165 | - if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - // since hook paths are always relative to the "default filesystem view" |
|
170 | - // we always use the owner from there to get the full node |
|
171 | - $uid = Filesystem::getView()->getOwner(''); |
|
172 | - |
|
173 | - /** @var IRootFolder $rootFolder */ |
|
174 | - $rootFolder = Server::get(IRootFolder::class); |
|
175 | - $userFolder = $rootFolder->getUserFolder($uid); |
|
176 | - |
|
177 | - $eventDispatcher = Server::get(IEventDispatcher::class); |
|
178 | - try { |
|
179 | - $file = $userFolder->get($filename); |
|
180 | - } catch (NotFoundException $e) { |
|
181 | - return false; |
|
182 | - } |
|
183 | - |
|
184 | - $mount = $file->getMountPoint(); |
|
185 | - if ($mount instanceof SharedMount) { |
|
186 | - $ownerFolder = $rootFolder->getUserFolder($mount->getShare()->getShareOwner()); |
|
187 | - $ownerNode = $ownerFolder->getFirstNodeById($file->getId()); |
|
188 | - if ($ownerNode) { |
|
189 | - $file = $ownerNode; |
|
190 | - $uid = $mount->getShare()->getShareOwner(); |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - /** @var IUserManager $userManager */ |
|
195 | - $userManager = Server::get(IUserManager::class); |
|
196 | - $user = $userManager->get($uid); |
|
197 | - |
|
198 | - if (!$user) { |
|
199 | - return false; |
|
200 | - } |
|
201 | - |
|
202 | - // no use making versions for empty files |
|
203 | - if ($file->getSize() === 0) { |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - $event = new CreateVersionEvent($file); |
|
208 | - $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event); |
|
209 | - if ($event->shouldCreateVersion() === false) { |
|
210 | - return false; |
|
211 | - } |
|
212 | - |
|
213 | - /** @var IVersionManager $versionManager */ |
|
214 | - $versionManager = Server::get(IVersionManager::class); |
|
215 | - |
|
216 | - $versionManager->createVersion($user, $file); |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * mark file as deleted so that we can remove the versions if the file is gone |
|
222 | - * @param string $path |
|
223 | - */ |
|
224 | - public static function markDeletedFile($path) { |
|
225 | - [$uid, $filename] = self::getUidAndFilename($path); |
|
226 | - self::$deletedFiles[$path] = [ |
|
227 | - 'uid' => $uid, |
|
228 | - 'filename' => $filename]; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * delete the version from the storage and cache |
|
233 | - * |
|
234 | - * @param View $view |
|
235 | - * @param string $path |
|
236 | - */ |
|
237 | - protected static function deleteVersion($view, $path) { |
|
238 | - $view->unlink($path); |
|
239 | - /** |
|
240 | - * @var \OC\Files\Storage\Storage $storage |
|
241 | - * @var string $internalPath |
|
242 | - */ |
|
243 | - [$storage, $internalPath] = $view->resolvePath($path); |
|
244 | - $cache = $storage->getCache($internalPath); |
|
245 | - $cache->remove($internalPath); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Delete versions of a file |
|
250 | - */ |
|
251 | - public static function delete($path) { |
|
252 | - $deletedFile = self::$deletedFiles[$path]; |
|
253 | - $uid = $deletedFile['uid']; |
|
254 | - $filename = $deletedFile['filename']; |
|
255 | - |
|
256 | - if (!Filesystem::file_exists($path)) { |
|
257 | - $view = new View('/' . $uid . '/files_versions'); |
|
258 | - |
|
259 | - $versions = self::getVersions($uid, $filename); |
|
260 | - if (!empty($versions)) { |
|
261 | - foreach ($versions as $v) { |
|
262 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
263 | - self::deleteVersion($view, $filename . '.v' . $v['version']); |
|
264 | - \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
265 | - } |
|
266 | - } |
|
267 | - } |
|
268 | - unset(self::$deletedFiles[$path]); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Delete a version of a file |
|
273 | - */ |
|
274 | - public static function deleteRevision(string $path, int $revision): void { |
|
275 | - [$uid, $filename] = self::getUidAndFilename($path); |
|
276 | - $view = new View('/' . $uid . '/files_versions'); |
|
277 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $revision, 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
278 | - self::deleteVersion($view, $filename . '.v' . $revision); |
|
279 | - \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $revision, 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Rename or copy versions of a file of the given paths |
|
284 | - * |
|
285 | - * @param string $sourcePath source path of the file to move, relative to |
|
286 | - * the currently logged in user's "files" folder |
|
287 | - * @param string $targetPath target path of the file to move, relative to |
|
288 | - * the currently logged in user's "files" folder |
|
289 | - * @param string $operation can be 'copy' or 'rename' |
|
290 | - */ |
|
291 | - public static function renameOrCopy($sourcePath, $targetPath, $operation) { |
|
292 | - [$sourceOwner, $sourcePath] = self::getSourcePathAndUser($sourcePath); |
|
293 | - |
|
294 | - // it was a upload of a existing file if no old path exists |
|
295 | - // in this case the pre-hook already called the store method and we can |
|
296 | - // stop here |
|
297 | - if ($sourcePath === false) { |
|
298 | - return true; |
|
299 | - } |
|
300 | - |
|
301 | - [$targetOwner, $targetPath] = self::getUidAndFilename($targetPath); |
|
302 | - |
|
303 | - $sourcePath = ltrim($sourcePath, '/'); |
|
304 | - $targetPath = ltrim($targetPath, '/'); |
|
305 | - |
|
306 | - $rootView = new View(''); |
|
307 | - |
|
308 | - // did we move a directory ? |
|
309 | - if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
310 | - // does the directory exists for versions too ? |
|
311 | - if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) { |
|
312 | - // create missing dirs if necessary |
|
313 | - self::createMissingDirectories($targetPath, new View('/' . $targetOwner)); |
|
314 | - |
|
315 | - // move the directory containing the versions |
|
316 | - $rootView->$operation( |
|
317 | - '/' . $sourceOwner . '/files_versions/' . $sourcePath, |
|
318 | - '/' . $targetOwner . '/files_versions/' . $targetPath |
|
319 | - ); |
|
320 | - } |
|
321 | - } elseif ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) { |
|
322 | - // create missing dirs if necessary |
|
323 | - self::createMissingDirectories($targetPath, new View('/' . $targetOwner)); |
|
324 | - |
|
325 | - foreach ($versions as $v) { |
|
326 | - // move each version one by one to the target directory |
|
327 | - $rootView->$operation( |
|
328 | - '/' . $sourceOwner . '/files_versions/' . $sourcePath . '.v' . $v['version'], |
|
329 | - '/' . $targetOwner . '/files_versions/' . $targetPath . '.v' . $v['version'] |
|
330 | - ); |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - // if we moved versions directly for a file, schedule expiration check for that file |
|
335 | - if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
336 | - self::scheduleExpire($targetOwner, $targetPath); |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Rollback to an old version of a file. |
|
342 | - * |
|
343 | - * @param string $file file name |
|
344 | - * @param int $revision revision timestamp |
|
345 | - * @return bool |
|
346 | - */ |
|
347 | - public static function rollback(string $file, int $revision, IUser $user) { |
|
348 | - // add expected leading slash |
|
349 | - $filename = '/' . ltrim($file, '/'); |
|
350 | - |
|
351 | - // Fetch the userfolder to trigger view hooks |
|
352 | - $root = Server::get(IRootFolder::class); |
|
353 | - $userFolder = $root->getUserFolder($user->getUID()); |
|
354 | - |
|
355 | - $users_view = new View('/' . $user->getUID()); |
|
356 | - $files_view = new View('/' . $user->getUID() . '/files'); |
|
357 | - |
|
358 | - $versionCreated = false; |
|
359 | - |
|
360 | - $fileInfo = $files_view->getFileInfo($file); |
|
361 | - |
|
362 | - // check if user has the permissions to revert a version |
|
363 | - if (!$fileInfo->isUpdateable()) { |
|
364 | - return false; |
|
365 | - } |
|
366 | - |
|
367 | - //first create a new version |
|
368 | - $version = 'files_versions' . $filename . '.v' . $users_view->filemtime('files' . $filename); |
|
369 | - if (!$users_view->file_exists($version)) { |
|
370 | - $users_view->copy('files' . $filename, 'files_versions' . $filename . '.v' . $users_view->filemtime('files' . $filename)); |
|
371 | - $versionCreated = true; |
|
372 | - } |
|
373 | - |
|
374 | - $fileToRestore = 'files_versions' . $filename . '.v' . $revision; |
|
375 | - |
|
376 | - // Restore encrypted version of the old file for the newly restored file |
|
377 | - // This has to happen manually here since the file is manually copied below |
|
378 | - $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion(); |
|
379 | - $oldFileInfo = $users_view->getFileInfo($fileToRestore); |
|
380 | - $cache = $fileInfo->getStorage()->getCache(); |
|
381 | - $cache->update( |
|
382 | - $fileInfo->getId(), [ |
|
383 | - 'encrypted' => $oldVersion, |
|
384 | - 'encryptedVersion' => $oldVersion, |
|
385 | - 'size' => $oldFileInfo->getData()['size'], |
|
386 | - 'unencrypted_size' => $oldFileInfo->getData()['unencrypted_size'], |
|
387 | - ] |
|
388 | - ); |
|
389 | - |
|
390 | - // rollback |
|
391 | - if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) { |
|
392 | - $files_view->touch($file, $revision); |
|
393 | - Storage::scheduleExpire($user->getUID(), $file); |
|
394 | - |
|
395 | - return true; |
|
396 | - } elseif ($versionCreated) { |
|
397 | - self::deleteVersion($users_view, $version); |
|
398 | - } |
|
399 | - |
|
400 | - return false; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Stream copy file contents from $path1 to $path2 |
|
405 | - * |
|
406 | - * @param View $view view to use for copying |
|
407 | - * @param string $path1 source file to copy |
|
408 | - * @param string $path2 target file |
|
409 | - * |
|
410 | - * @return bool true for success, false otherwise |
|
411 | - */ |
|
412 | - private static function copyFileContents($view, $path1, $path2) { |
|
413 | - /** @var \OC\Files\Storage\Storage $storage1 */ |
|
414 | - [$storage1, $internalPath1] = $view->resolvePath($path1); |
|
415 | - /** @var \OC\Files\Storage\Storage $storage2 */ |
|
416 | - [$storage2, $internalPath2] = $view->resolvePath($path2); |
|
417 | - |
|
418 | - $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
419 | - $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
420 | - |
|
421 | - try { |
|
422 | - // TODO add a proper way of overwriting a file while maintaining file ids |
|
423 | - if ($storage1->instanceOfStorage(ObjectStoreStorage::class) |
|
424 | - || $storage2->instanceOfStorage(ObjectStoreStorage::class) |
|
425 | - ) { |
|
426 | - $source = $storage1->fopen($internalPath1, 'r'); |
|
427 | - $result = $source !== false; |
|
428 | - if ($result) { |
|
429 | - if ($storage2->instanceOfStorage(IWriteStreamStorage::class)) { |
|
430 | - /** @var IWriteStreamStorage $storage2 */ |
|
431 | - $storage2->writeStream($internalPath2, $source); |
|
432 | - } else { |
|
433 | - $target = $storage2->fopen($internalPath2, 'w'); |
|
434 | - $result = $target !== false; |
|
435 | - if ($result) { |
|
436 | - [, $result] = Files::streamCopy($source, $target, true); |
|
437 | - } |
|
438 | - // explicit check as S3 library closes streams already |
|
439 | - if (is_resource($target)) { |
|
440 | - fclose($target); |
|
441 | - } |
|
442 | - } |
|
443 | - } |
|
444 | - // explicit check as S3 library closes streams already |
|
445 | - if (is_resource($source)) { |
|
446 | - fclose($source); |
|
447 | - } |
|
448 | - |
|
449 | - if ($result !== false) { |
|
450 | - $storage1->unlink($internalPath1); |
|
451 | - } |
|
452 | - } else { |
|
453 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
454 | - } |
|
455 | - } finally { |
|
456 | - $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
457 | - $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
458 | - } |
|
459 | - |
|
460 | - return ($result !== false); |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * get a list of all available versions of a file in descending chronological order |
|
465 | - * @param string $uid user id from the owner of the file |
|
466 | - * @param string $filename file to find versions of, relative to the user files dir |
|
467 | - * @param string $userFullPath |
|
468 | - * @return array versions newest version first |
|
469 | - */ |
|
470 | - public static function getVersions($uid, $filename, $userFullPath = '') { |
|
471 | - $versions = []; |
|
472 | - if (empty($filename)) { |
|
473 | - return $versions; |
|
474 | - } |
|
475 | - // fetch for old versions |
|
476 | - $view = new View('/' . $uid . '/'); |
|
477 | - |
|
478 | - $pathinfo = pathinfo($filename); |
|
479 | - $versionedFile = $pathinfo['basename']; |
|
480 | - |
|
481 | - $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']); |
|
482 | - |
|
483 | - $dirContent = false; |
|
484 | - if ($view->is_dir($dir)) { |
|
485 | - $dirContent = $view->opendir($dir); |
|
486 | - } |
|
487 | - |
|
488 | - if ($dirContent === false) { |
|
489 | - return $versions; |
|
490 | - } |
|
491 | - |
|
492 | - if (is_resource($dirContent)) { |
|
493 | - while (($entryName = readdir($dirContent)) !== false) { |
|
494 | - if (!Filesystem::isIgnoredDir($entryName)) { |
|
495 | - $pathparts = pathinfo($entryName); |
|
496 | - $filename = $pathparts['filename']; |
|
497 | - if ($filename === $versionedFile) { |
|
498 | - $pathparts = pathinfo($entryName); |
|
499 | - $timestamp = substr($pathparts['extension'] ?? '', 1); |
|
500 | - if (!is_numeric($timestamp)) { |
|
501 | - Server::get(LoggerInterface::class)->error( |
|
502 | - 'Version file {path} has incorrect name format', |
|
503 | - [ |
|
504 | - 'path' => $entryName, |
|
505 | - 'app' => 'files_versions', |
|
506 | - ] |
|
507 | - ); |
|
508 | - continue; |
|
509 | - } |
|
510 | - $filename = $pathparts['filename']; |
|
511 | - $key = $timestamp . '#' . $filename; |
|
512 | - $versions[$key]['version'] = $timestamp; |
|
513 | - $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp((int)$timestamp); |
|
514 | - if (empty($userFullPath)) { |
|
515 | - $versions[$key]['preview'] = ''; |
|
516 | - } else { |
|
517 | - /** @var IURLGenerator $urlGenerator */ |
|
518 | - $urlGenerator = Server::get(IURLGenerator::class); |
|
519 | - $versions[$key]['preview'] = $urlGenerator->linkToRoute('files_version.Preview.getPreview', |
|
520 | - ['file' => $userFullPath, 'version' => $timestamp]); |
|
521 | - } |
|
522 | - $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename); |
|
523 | - $versions[$key]['name'] = $versionedFile; |
|
524 | - $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName); |
|
525 | - $versions[$key]['mimetype'] = Server::get(IMimeTypeDetector::class)->detectPath($versionedFile); |
|
526 | - } |
|
527 | - } |
|
528 | - } |
|
529 | - closedir($dirContent); |
|
530 | - } |
|
531 | - |
|
532 | - // sort with newest version first |
|
533 | - krsort($versions); |
|
534 | - |
|
535 | - return $versions; |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Expire versions that older than max version retention time |
|
540 | - * |
|
541 | - * @param string $uid |
|
542 | - */ |
|
543 | - public static function expireOlderThanMaxForUser($uid) { |
|
544 | - /** @var IRootFolder $root */ |
|
545 | - $root = Server::get(IRootFolder::class); |
|
546 | - try { |
|
547 | - /** @var Folder $versionsRoot */ |
|
548 | - $versionsRoot = $root->get('/' . $uid . '/files_versions'); |
|
549 | - } catch (NotFoundException $e) { |
|
550 | - return; |
|
551 | - } |
|
552 | - |
|
553 | - $expiration = self::getExpiration(); |
|
554 | - $threshold = $expiration->getMaxAgeAsTimestamp(); |
|
555 | - if (!$threshold) { |
|
556 | - return; |
|
557 | - } |
|
558 | - |
|
559 | - $allVersions = $versionsRoot->search(new SearchQuery( |
|
560 | - new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
561 | - new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', FileInfo::MIMETYPE_FOLDER), |
|
562 | - ]), |
|
563 | - 0, |
|
564 | - 0, |
|
565 | - [] |
|
566 | - )); |
|
567 | - |
|
568 | - /** @var VersionsMapper $versionsMapper */ |
|
569 | - $versionsMapper = Server::get(VersionsMapper::class); |
|
570 | - $userFolder = $root->getUserFolder($uid); |
|
571 | - $versionEntities = []; |
|
572 | - |
|
573 | - /** @var Node[] $versions */ |
|
574 | - $versions = array_filter($allVersions, function (Node $info) use ($threshold, $userFolder, $versionsMapper, $versionsRoot, &$versionEntities) { |
|
575 | - // Check that the file match '*.v*' |
|
576 | - $versionsBegin = strrpos($info->getName(), '.v'); |
|
577 | - if ($versionsBegin === false) { |
|
578 | - return false; |
|
579 | - } |
|
580 | - |
|
581 | - $version = (int)substr($info->getName(), $versionsBegin + 2); |
|
582 | - |
|
583 | - // Check that the version does not have a label. |
|
584 | - $path = $versionsRoot->getRelativePath($info->getPath()); |
|
585 | - if ($path === null) { |
|
586 | - throw new DoesNotExistException('Could not find relative path of (' . $info->getPath() . ')'); |
|
587 | - } |
|
588 | - |
|
589 | - try { |
|
590 | - $node = $userFolder->get(substr($path, 0, -strlen('.v' . $version))); |
|
591 | - $versionEntity = $versionsMapper->findVersionForFileId($node->getId(), $version); |
|
592 | - $versionEntities[$info->getId()] = $versionEntity; |
|
593 | - |
|
594 | - if ($versionEntity->getMetadataValue('label') !== null && $versionEntity->getMetadataValue('label') !== '') { |
|
595 | - return false; |
|
596 | - } |
|
597 | - } catch (NotFoundException $e) { |
|
598 | - // Original node not found, delete the version |
|
599 | - return true; |
|
600 | - } catch (StorageNotAvailableException|StorageInvalidException $e) { |
|
601 | - // Storage can't be used, but it might only be temporary so we can't always delete the version |
|
602 | - // since we can't determine if the version is named we take the safe route and don't expire |
|
603 | - return false; |
|
604 | - } catch (DoesNotExistException $ex) { |
|
605 | - // Version on FS can have no equivalent in the DB if they were created before the version naming feature. |
|
606 | - // So we ignore DoesNotExistException. |
|
607 | - } |
|
608 | - |
|
609 | - // Check that the version's timestamp is lower than $threshold |
|
610 | - return $version < $threshold; |
|
611 | - }); |
|
612 | - |
|
613 | - foreach ($versions as $version) { |
|
614 | - $internalPath = $version->getInternalPath(); |
|
615 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]); |
|
616 | - |
|
617 | - $versionEntity = isset($versionEntities[$version->getId()]) ? $versionEntities[$version->getId()] : null; |
|
618 | - if (!is_null($versionEntity)) { |
|
619 | - $versionsMapper->delete($versionEntity); |
|
620 | - } |
|
621 | - |
|
622 | - try { |
|
623 | - $version->delete(); |
|
624 | - \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]); |
|
625 | - } catch (NotPermittedException $e) { |
|
626 | - Server::get(LoggerInterface::class)->error("Missing permissions to delete version: {$internalPath}", ['app' => 'files_versions', 'exception' => $e]); |
|
627 | - } |
|
628 | - } |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * translate a timestamp into a string like "5 days ago" |
|
633 | - * |
|
634 | - * @param int $timestamp |
|
635 | - * @return string for example "5 days ago" |
|
636 | - */ |
|
637 | - private static function getHumanReadableTimestamp(int $timestamp): string { |
|
638 | - $diff = time() - $timestamp; |
|
639 | - |
|
640 | - if ($diff < 60) { // first minute |
|
641 | - return $diff . ' seconds ago'; |
|
642 | - } elseif ($diff < 3600) { //first hour |
|
643 | - return round($diff / 60) . ' minutes ago'; |
|
644 | - } elseif ($diff < 86400) { // first day |
|
645 | - return round($diff / 3600) . ' hours ago'; |
|
646 | - } elseif ($diff < 604800) { //first week |
|
647 | - return round($diff / 86400) . ' days ago'; |
|
648 | - } elseif ($diff < 2419200) { //first month |
|
649 | - return round($diff / 604800) . ' weeks ago'; |
|
650 | - } elseif ($diff < 29030400) { // first year |
|
651 | - return round($diff / 2419200) . ' months ago'; |
|
652 | - } else { |
|
653 | - return round($diff / 29030400) . ' years ago'; |
|
654 | - } |
|
655 | - } |
|
656 | - |
|
657 | - /** |
|
658 | - * returns all stored file versions from a given user |
|
659 | - * @param string $uid id of the user |
|
660 | - * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename |
|
661 | - */ |
|
662 | - private static function getAllVersions($uid) { |
|
663 | - $view = new View('/' . $uid . '/'); |
|
664 | - $dirs = [self::VERSIONS_ROOT]; |
|
665 | - $versions = []; |
|
666 | - |
|
667 | - while (!empty($dirs)) { |
|
668 | - $dir = array_pop($dirs); |
|
669 | - $files = $view->getDirectoryContent($dir); |
|
670 | - |
|
671 | - foreach ($files as $file) { |
|
672 | - $fileData = $file->getData(); |
|
673 | - $filePath = $dir . '/' . $fileData['name']; |
|
674 | - if ($file['type'] === 'dir') { |
|
675 | - $dirs[] = $filePath; |
|
676 | - } else { |
|
677 | - $versionsBegin = strrpos($filePath, '.v'); |
|
678 | - $relPathStart = strlen(self::VERSIONS_ROOT); |
|
679 | - $version = substr($filePath, $versionsBegin + 2); |
|
680 | - $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart); |
|
681 | - $key = $version . '#' . $relpath; |
|
682 | - $versions[$key] = ['path' => $relpath, 'timestamp' => $version]; |
|
683 | - } |
|
684 | - } |
|
685 | - } |
|
686 | - |
|
687 | - // newest version first |
|
688 | - krsort($versions); |
|
689 | - |
|
690 | - $result = [ |
|
691 | - 'all' => [], |
|
692 | - 'by_file' => [], |
|
693 | - ]; |
|
694 | - |
|
695 | - foreach ($versions as $key => $value) { |
|
696 | - $size = $view->filesize(self::VERSIONS_ROOT . '/' . $value['path'] . '.v' . $value['timestamp']); |
|
697 | - $filename = $value['path']; |
|
698 | - |
|
699 | - $result['all'][$key]['version'] = $value['timestamp']; |
|
700 | - $result['all'][$key]['path'] = $filename; |
|
701 | - $result['all'][$key]['size'] = $size; |
|
702 | - |
|
703 | - $result['by_file'][$filename][$key]['version'] = $value['timestamp']; |
|
704 | - $result['by_file'][$filename][$key]['path'] = $filename; |
|
705 | - $result['by_file'][$filename][$key]['size'] = $size; |
|
706 | - } |
|
707 | - |
|
708 | - return $result; |
|
709 | - } |
|
710 | - |
|
711 | - /** |
|
712 | - * get list of files we want to expire |
|
713 | - * @param array $versions list of versions |
|
714 | - * @param integer $time |
|
715 | - * @param bool $quotaExceeded is versions storage limit reached |
|
716 | - * @return array containing the list of to deleted versions and the size of them |
|
717 | - */ |
|
718 | - protected static function getExpireList($time, $versions, $quotaExceeded = false) { |
|
719 | - $expiration = self::getExpiration(); |
|
720 | - |
|
721 | - if ($expiration->shouldAutoExpire()) { |
|
722 | - // Exclude versions that are newer than the minimum age from the auto expiration logic. |
|
723 | - $minAge = $expiration->getMinAgeAsTimestamp(); |
|
724 | - if ($minAge !== false) { |
|
725 | - $versionsToAutoExpire = array_filter($versions, fn ($version) => $version['version'] < $minAge); |
|
726 | - } else { |
|
727 | - $versionsToAutoExpire = $versions; |
|
728 | - } |
|
729 | - |
|
730 | - [$toDelete, $size] = self::getAutoExpireList($time, $versionsToAutoExpire); |
|
731 | - } else { |
|
732 | - $size = 0; |
|
733 | - $toDelete = []; // versions we want to delete |
|
734 | - } |
|
735 | - |
|
736 | - foreach ($versions as $key => $version) { |
|
737 | - if (!is_numeric($version['version'])) { |
|
738 | - Server::get(LoggerInterface::class)->error( |
|
739 | - 'Found a non-numeric timestamp version: ' . json_encode($version), |
|
740 | - ['app' => 'files_versions']); |
|
741 | - continue; |
|
742 | - } |
|
743 | - if ($expiration->isExpired((int)($version['version']), $quotaExceeded) && !isset($toDelete[$key])) { |
|
744 | - $size += $version['size']; |
|
745 | - $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
746 | - } |
|
747 | - } |
|
748 | - |
|
749 | - return [$toDelete, $size]; |
|
750 | - } |
|
751 | - |
|
752 | - /** |
|
753 | - * get list of files we want to expire |
|
754 | - * @param array $versions list of versions |
|
755 | - * @param integer $time |
|
756 | - * @return array containing the list of to deleted versions and the size of them |
|
757 | - */ |
|
758 | - protected static function getAutoExpireList($time, $versions) { |
|
759 | - $size = 0; |
|
760 | - $toDelete = []; // versions we want to delete |
|
761 | - |
|
762 | - $interval = 1; |
|
763 | - $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
764 | - if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
765 | - $nextInterval = -1; |
|
766 | - } else { |
|
767 | - $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
768 | - } |
|
769 | - |
|
770 | - $firstVersion = reset($versions); |
|
771 | - |
|
772 | - if ($firstVersion === false) { |
|
773 | - return [$toDelete, $size]; |
|
774 | - } |
|
775 | - |
|
776 | - $firstKey = key($versions); |
|
777 | - $prevTimestamp = $firstVersion['version']; |
|
778 | - $nextVersion = $firstVersion['version'] - $step; |
|
779 | - unset($versions[$firstKey]); |
|
780 | - |
|
781 | - foreach ($versions as $key => $version) { |
|
782 | - $newInterval = true; |
|
783 | - while ($newInterval) { |
|
784 | - if ($nextInterval === -1 || $prevTimestamp > $nextInterval) { |
|
785 | - if ($version['version'] > $nextVersion) { |
|
786 | - //distance between two version too small, mark to delete |
|
787 | - $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
788 | - $size += $version['size']; |
|
789 | - Server::get(LoggerInterface::class)->info('Mark to expire ' . $version['path'] . ' next version should be ' . $nextVersion . ' or smaller. (prevTimestamp: ' . $prevTimestamp . '; step: ' . $step, ['app' => 'files_versions']); |
|
790 | - } else { |
|
791 | - $nextVersion = $version['version'] - $step; |
|
792 | - $prevTimestamp = $version['version']; |
|
793 | - } |
|
794 | - $newInterval = false; // version checked so we can move to the next one |
|
795 | - } else { // time to move on to the next interval |
|
796 | - $interval++; |
|
797 | - $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
798 | - $nextVersion = $prevTimestamp - $step; |
|
799 | - if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
800 | - $nextInterval = -1; |
|
801 | - } else { |
|
802 | - $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
803 | - } |
|
804 | - $newInterval = true; // we changed the interval -> check same version with new interval |
|
805 | - } |
|
806 | - } |
|
807 | - } |
|
808 | - |
|
809 | - return [$toDelete, $size]; |
|
810 | - } |
|
811 | - |
|
812 | - /** |
|
813 | - * Schedule versions expiration for the given file |
|
814 | - * |
|
815 | - * @param string $uid owner of the file |
|
816 | - * @param string $fileName file/folder for which to schedule expiration |
|
817 | - */ |
|
818 | - public static function scheduleExpire($uid, $fileName) { |
|
819 | - // let the admin disable auto expire |
|
820 | - $expiration = self::getExpiration(); |
|
821 | - if ($expiration->isEnabled()) { |
|
822 | - $command = new Expire($uid, $fileName); |
|
823 | - /** @var IBus $bus */ |
|
824 | - $bus = Server::get(IBus::class); |
|
825 | - $bus->push($command); |
|
826 | - } |
|
827 | - } |
|
828 | - |
|
829 | - /** |
|
830 | - * Expire versions which exceed the quota. |
|
831 | - * |
|
832 | - * This will setup the filesystem for the given user but will not |
|
833 | - * tear it down afterwards. |
|
834 | - * |
|
835 | - * @param string $filename path to file to expire |
|
836 | - * @param string $uid user for which to expire the version |
|
837 | - * @return bool|int|null |
|
838 | - */ |
|
839 | - public static function expire($filename, $uid) { |
|
840 | - $expiration = self::getExpiration(); |
|
841 | - |
|
842 | - /** @var LoggerInterface $logger */ |
|
843 | - $logger = Server::get(LoggerInterface::class); |
|
844 | - |
|
845 | - if ($expiration->isEnabled()) { |
|
846 | - // get available disk space for user |
|
847 | - $user = Server::get(IUserManager::class)->get($uid); |
|
848 | - if (is_null($user)) { |
|
849 | - $logger->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']); |
|
850 | - throw new NoUserException('Backends provided no user object for ' . $uid); |
|
851 | - } |
|
852 | - |
|
853 | - \OC_Util::setupFS($uid); |
|
854 | - |
|
855 | - try { |
|
856 | - if (!Filesystem::file_exists($filename)) { |
|
857 | - return false; |
|
858 | - } |
|
859 | - } catch (StorageNotAvailableException $e) { |
|
860 | - // if we can't check that the file hasn't been deleted we can only assume that it hasn't |
|
861 | - // note that this `StorageNotAvailableException` is about the file the versions originate from, |
|
862 | - // not the storage that the versions are stored on |
|
863 | - } |
|
864 | - |
|
865 | - if (empty($filename)) { |
|
866 | - // file maybe renamed or deleted |
|
867 | - return false; |
|
868 | - } |
|
869 | - $versionsFileview = new View('/' . $uid . '/files_versions'); |
|
870 | - |
|
871 | - $softQuota = true; |
|
872 | - $quota = $user->getQuota(); |
|
873 | - if ($quota === null || $quota === 'none') { |
|
874 | - $quota = Filesystem::free_space('/'); |
|
875 | - $softQuota = false; |
|
876 | - } else { |
|
877 | - $quota = Util::computerFileSize($quota); |
|
878 | - } |
|
879 | - |
|
880 | - // make sure that we have the current size of the version history |
|
881 | - $versionsSize = self::getVersionsSize($uid); |
|
882 | - |
|
883 | - // calculate available space for version history |
|
884 | - // subtract size of files and current versions size from quota |
|
885 | - if ($quota >= 0) { |
|
886 | - if ($softQuota) { |
|
887 | - $root = Server::get(IRootFolder::class); |
|
888 | - $userFolder = $root->getUserFolder($uid); |
|
889 | - if (is_null($userFolder)) { |
|
890 | - $availableSpace = 0; |
|
891 | - } else { |
|
892 | - $free = $quota - $userFolder->getSize(false); // remaining free space for user |
|
893 | - if ($free > 0) { |
|
894 | - $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions |
|
895 | - } else { |
|
896 | - $availableSpace = $free - $versionsSize; |
|
897 | - } |
|
898 | - } |
|
899 | - } else { |
|
900 | - $availableSpace = $quota; |
|
901 | - } |
|
902 | - } else { |
|
903 | - $availableSpace = PHP_INT_MAX; |
|
904 | - } |
|
905 | - |
|
906 | - $allVersions = Storage::getVersions($uid, $filename); |
|
907 | - |
|
908 | - $time = time(); |
|
909 | - [$toDelete, $sizeOfDeletedVersions] = self::getExpireList($time, $allVersions, $availableSpace <= 0); |
|
910 | - |
|
911 | - $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
912 | - $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
913 | - |
|
914 | - // if still not enough free space we rearrange the versions from all files |
|
915 | - if ($availableSpace <= 0) { |
|
916 | - $result = self::getAllVersions($uid); |
|
917 | - $allVersions = $result['all']; |
|
918 | - |
|
919 | - foreach ($result['by_file'] as $versions) { |
|
920 | - [$toDeleteNew, $size] = self::getExpireList($time, $versions, $availableSpace <= 0); |
|
921 | - $toDelete = array_merge($toDelete, $toDeleteNew); |
|
922 | - $sizeOfDeletedVersions += $size; |
|
923 | - } |
|
924 | - $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
925 | - $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
926 | - } |
|
927 | - |
|
928 | - foreach ($toDelete as $key => $path) { |
|
929 | - // Make sure to cleanup version table relations as expire does not pass deleteVersion |
|
930 | - try { |
|
931 | - /** @var VersionsMapper $versionsMapper */ |
|
932 | - $versionsMapper = Server::get(VersionsMapper::class); |
|
933 | - $file = Server::get(IRootFolder::class)->getUserFolder($uid)->get($filename); |
|
934 | - $pathparts = pathinfo($path); |
|
935 | - $timestamp = (int)substr($pathparts['extension'] ?? '', 1); |
|
936 | - $versionEntity = $versionsMapper->findVersionForFileId($file->getId(), $timestamp); |
|
937 | - if ($versionEntity->getMetadataValue('label') !== null && $versionEntity->getMetadataValue('label') !== '') { |
|
938 | - continue; |
|
939 | - } |
|
940 | - $versionsMapper->delete($versionEntity); |
|
941 | - } catch (DoesNotExistException $e) { |
|
942 | - } |
|
943 | - |
|
944 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
945 | - self::deleteVersion($versionsFileview, $path); |
|
946 | - \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
947 | - unset($allVersions[$key]); // update array with the versions we keep |
|
948 | - $logger->info('Expire: ' . $path, ['app' => 'files_versions']); |
|
949 | - } |
|
950 | - |
|
951 | - // Check if enough space is available after versions are rearranged. |
|
952 | - // If not we delete the oldest versions until we meet the size limit for versions, |
|
953 | - // but always keep the two latest versions |
|
954 | - $numOfVersions = count($allVersions) - 2 ; |
|
955 | - $i = 0; |
|
956 | - // sort oldest first and make sure that we start at the first element |
|
957 | - ksort($allVersions); |
|
958 | - reset($allVersions); |
|
959 | - while ($availableSpace < 0 && $i < $numOfVersions) { |
|
960 | - $version = current($allVersions); |
|
961 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $version['path'] . '.v' . $version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
962 | - self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']); |
|
963 | - \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $version['path'] . '.v' . $version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
964 | - $logger->info('running out of space! Delete oldest version: ' . $version['path'] . '.v' . $version['version'], ['app' => 'files_versions']); |
|
965 | - $versionsSize -= $version['size']; |
|
966 | - $availableSpace += $version['size']; |
|
967 | - next($allVersions); |
|
968 | - $i++; |
|
969 | - } |
|
970 | - |
|
971 | - return $versionsSize; // finally return the new size of the version history |
|
972 | - } |
|
973 | - |
|
974 | - return false; |
|
975 | - } |
|
976 | - |
|
977 | - /** |
|
978 | - * Create recursively missing directories inside of files_versions |
|
979 | - * that match the given path to a file. |
|
980 | - * |
|
981 | - * @param string $filename $path to a file, relative to the user's |
|
982 | - * "files" folder |
|
983 | - * @param View $view view on data/user/ |
|
984 | - */ |
|
985 | - public static function createMissingDirectories($filename, $view) { |
|
986 | - $dirname = Filesystem::normalizePath(dirname($filename)); |
|
987 | - $dirParts = explode('/', $dirname); |
|
988 | - $dir = '/files_versions'; |
|
989 | - foreach ($dirParts as $part) { |
|
990 | - $dir = $dir . '/' . $part; |
|
991 | - if (!$view->file_exists($dir)) { |
|
992 | - $view->mkdir($dir); |
|
993 | - } |
|
994 | - } |
|
995 | - } |
|
996 | - |
|
997 | - /** |
|
998 | - * Static workaround |
|
999 | - * @return Expiration |
|
1000 | - */ |
|
1001 | - protected static function getExpiration() { |
|
1002 | - if (self::$application === null) { |
|
1003 | - self::$application = Server::get(Application::class); |
|
1004 | - } |
|
1005 | - return self::$application->getContainer()->get(Expiration::class); |
|
1006 | - } |
|
50 | + public const DEFAULTENABLED = true; |
|
51 | + public const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota |
|
52 | + public const VERSIONS_ROOT = 'files_versions/'; |
|
53 | + |
|
54 | + public const DELETE_TRIGGER_MASTER_REMOVED = 0; |
|
55 | + public const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1; |
|
56 | + public const DELETE_TRIGGER_QUOTA_EXCEEDED = 2; |
|
57 | + |
|
58 | + // files for which we can remove the versions after the delete operation was successful |
|
59 | + private static $deletedFiles = []; |
|
60 | + |
|
61 | + private static $sourcePathAndUser = []; |
|
62 | + |
|
63 | + private static $max_versions_per_interval = [ |
|
64 | + //first 10sec, one version every 2sec |
|
65 | + 1 => ['intervalEndsAfter' => 10, 'step' => 2], |
|
66 | + //next minute, one version every 10sec |
|
67 | + 2 => ['intervalEndsAfter' => 60, 'step' => 10], |
|
68 | + //next hour, one version every minute |
|
69 | + 3 => ['intervalEndsAfter' => 3600, 'step' => 60], |
|
70 | + //next 24h, one version every hour |
|
71 | + 4 => ['intervalEndsAfter' => 86400, 'step' => 3600], |
|
72 | + //next 30days, one version per day |
|
73 | + 5 => ['intervalEndsAfter' => 2592000, 'step' => 86400], |
|
74 | + //until the end one version per week |
|
75 | + 6 => ['intervalEndsAfter' => -1, 'step' => 604800], |
|
76 | + ]; |
|
77 | + |
|
78 | + /** @var Application */ |
|
79 | + private static $application; |
|
80 | + |
|
81 | + /** |
|
82 | + * get the UID of the owner of the file and the path to the file relative to |
|
83 | + * owners files folder |
|
84 | + * |
|
85 | + * @param string $filename |
|
86 | + * @return array |
|
87 | + * @throws NoUserException |
|
88 | + */ |
|
89 | + public static function getUidAndFilename($filename) { |
|
90 | + $uid = Filesystem::getOwner($filename); |
|
91 | + $userManager = Server::get(IUserManager::class); |
|
92 | + // if the user with the UID doesn't exists, e.g. because the UID points |
|
93 | + // to a remote user with a federated cloud ID we use the current logged-in |
|
94 | + // user. We need a valid local user to create the versions |
|
95 | + if (!$userManager->userExists($uid)) { |
|
96 | + $uid = OC_User::getUser(); |
|
97 | + } |
|
98 | + Filesystem::initMountPoints($uid); |
|
99 | + if ($uid !== OC_User::getUser()) { |
|
100 | + $info = Filesystem::getFileInfo($filename); |
|
101 | + $ownerView = new View('/' . $uid . '/files'); |
|
102 | + try { |
|
103 | + $filename = $ownerView->getPath($info['fileid']); |
|
104 | + // make sure that the file name doesn't end with a trailing slash |
|
105 | + // can for example happen single files shared across servers |
|
106 | + $filename = rtrim($filename, '/'); |
|
107 | + } catch (NotFoundException $e) { |
|
108 | + $filename = null; |
|
109 | + } |
|
110 | + } |
|
111 | + return [$uid, $filename]; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Remember the owner and the owner path of the source file |
|
116 | + * |
|
117 | + * @param string $source source path |
|
118 | + */ |
|
119 | + public static function setSourcePathAndUser($source) { |
|
120 | + [$uid, $path] = self::getUidAndFilename($source); |
|
121 | + self::$sourcePathAndUser[$source] = ['uid' => $uid, 'path' => $path]; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Gets the owner and the owner path from the source path |
|
126 | + * |
|
127 | + * @param string $source source path |
|
128 | + * @return array with user id and path |
|
129 | + */ |
|
130 | + public static function getSourcePathAndUser($source) { |
|
131 | + if (isset(self::$sourcePathAndUser[$source])) { |
|
132 | + $uid = self::$sourcePathAndUser[$source]['uid']; |
|
133 | + $path = self::$sourcePathAndUser[$source]['path']; |
|
134 | + unset(self::$sourcePathAndUser[$source]); |
|
135 | + } else { |
|
136 | + $uid = $path = false; |
|
137 | + } |
|
138 | + return [$uid, $path]; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * get current size of all versions from a given user |
|
143 | + * |
|
144 | + * @param string $user user who owns the versions |
|
145 | + * @return int versions size |
|
146 | + */ |
|
147 | + private static function getVersionsSize($user) { |
|
148 | + $view = new View('/' . $user); |
|
149 | + $fileInfo = $view->getFileInfo('/files_versions'); |
|
150 | + return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * store a new version of a file. |
|
155 | + */ |
|
156 | + public static function store($filename) { |
|
157 | + // if the file gets streamed we need to remove the .part extension |
|
158 | + // to get the right target |
|
159 | + $ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
160 | + if ($ext === 'part') { |
|
161 | + $filename = substr($filename, 0, -5); |
|
162 | + } |
|
163 | + |
|
164 | + // we only handle existing files |
|
165 | + if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + // since hook paths are always relative to the "default filesystem view" |
|
170 | + // we always use the owner from there to get the full node |
|
171 | + $uid = Filesystem::getView()->getOwner(''); |
|
172 | + |
|
173 | + /** @var IRootFolder $rootFolder */ |
|
174 | + $rootFolder = Server::get(IRootFolder::class); |
|
175 | + $userFolder = $rootFolder->getUserFolder($uid); |
|
176 | + |
|
177 | + $eventDispatcher = Server::get(IEventDispatcher::class); |
|
178 | + try { |
|
179 | + $file = $userFolder->get($filename); |
|
180 | + } catch (NotFoundException $e) { |
|
181 | + return false; |
|
182 | + } |
|
183 | + |
|
184 | + $mount = $file->getMountPoint(); |
|
185 | + if ($mount instanceof SharedMount) { |
|
186 | + $ownerFolder = $rootFolder->getUserFolder($mount->getShare()->getShareOwner()); |
|
187 | + $ownerNode = $ownerFolder->getFirstNodeById($file->getId()); |
|
188 | + if ($ownerNode) { |
|
189 | + $file = $ownerNode; |
|
190 | + $uid = $mount->getShare()->getShareOwner(); |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + /** @var IUserManager $userManager */ |
|
195 | + $userManager = Server::get(IUserManager::class); |
|
196 | + $user = $userManager->get($uid); |
|
197 | + |
|
198 | + if (!$user) { |
|
199 | + return false; |
|
200 | + } |
|
201 | + |
|
202 | + // no use making versions for empty files |
|
203 | + if ($file->getSize() === 0) { |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + $event = new CreateVersionEvent($file); |
|
208 | + $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event); |
|
209 | + if ($event->shouldCreateVersion() === false) { |
|
210 | + return false; |
|
211 | + } |
|
212 | + |
|
213 | + /** @var IVersionManager $versionManager */ |
|
214 | + $versionManager = Server::get(IVersionManager::class); |
|
215 | + |
|
216 | + $versionManager->createVersion($user, $file); |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * mark file as deleted so that we can remove the versions if the file is gone |
|
222 | + * @param string $path |
|
223 | + */ |
|
224 | + public static function markDeletedFile($path) { |
|
225 | + [$uid, $filename] = self::getUidAndFilename($path); |
|
226 | + self::$deletedFiles[$path] = [ |
|
227 | + 'uid' => $uid, |
|
228 | + 'filename' => $filename]; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * delete the version from the storage and cache |
|
233 | + * |
|
234 | + * @param View $view |
|
235 | + * @param string $path |
|
236 | + */ |
|
237 | + protected static function deleteVersion($view, $path) { |
|
238 | + $view->unlink($path); |
|
239 | + /** |
|
240 | + * @var \OC\Files\Storage\Storage $storage |
|
241 | + * @var string $internalPath |
|
242 | + */ |
|
243 | + [$storage, $internalPath] = $view->resolvePath($path); |
|
244 | + $cache = $storage->getCache($internalPath); |
|
245 | + $cache->remove($internalPath); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Delete versions of a file |
|
250 | + */ |
|
251 | + public static function delete($path) { |
|
252 | + $deletedFile = self::$deletedFiles[$path]; |
|
253 | + $uid = $deletedFile['uid']; |
|
254 | + $filename = $deletedFile['filename']; |
|
255 | + |
|
256 | + if (!Filesystem::file_exists($path)) { |
|
257 | + $view = new View('/' . $uid . '/files_versions'); |
|
258 | + |
|
259 | + $versions = self::getVersions($uid, $filename); |
|
260 | + if (!empty($versions)) { |
|
261 | + foreach ($versions as $v) { |
|
262 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
263 | + self::deleteVersion($view, $filename . '.v' . $v['version']); |
|
264 | + \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
265 | + } |
|
266 | + } |
|
267 | + } |
|
268 | + unset(self::$deletedFiles[$path]); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Delete a version of a file |
|
273 | + */ |
|
274 | + public static function deleteRevision(string $path, int $revision): void { |
|
275 | + [$uid, $filename] = self::getUidAndFilename($path); |
|
276 | + $view = new View('/' . $uid . '/files_versions'); |
|
277 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $revision, 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
278 | + self::deleteVersion($view, $filename . '.v' . $revision); |
|
279 | + \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $revision, 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Rename or copy versions of a file of the given paths |
|
284 | + * |
|
285 | + * @param string $sourcePath source path of the file to move, relative to |
|
286 | + * the currently logged in user's "files" folder |
|
287 | + * @param string $targetPath target path of the file to move, relative to |
|
288 | + * the currently logged in user's "files" folder |
|
289 | + * @param string $operation can be 'copy' or 'rename' |
|
290 | + */ |
|
291 | + public static function renameOrCopy($sourcePath, $targetPath, $operation) { |
|
292 | + [$sourceOwner, $sourcePath] = self::getSourcePathAndUser($sourcePath); |
|
293 | + |
|
294 | + // it was a upload of a existing file if no old path exists |
|
295 | + // in this case the pre-hook already called the store method and we can |
|
296 | + // stop here |
|
297 | + if ($sourcePath === false) { |
|
298 | + return true; |
|
299 | + } |
|
300 | + |
|
301 | + [$targetOwner, $targetPath] = self::getUidAndFilename($targetPath); |
|
302 | + |
|
303 | + $sourcePath = ltrim($sourcePath, '/'); |
|
304 | + $targetPath = ltrim($targetPath, '/'); |
|
305 | + |
|
306 | + $rootView = new View(''); |
|
307 | + |
|
308 | + // did we move a directory ? |
|
309 | + if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
310 | + // does the directory exists for versions too ? |
|
311 | + if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) { |
|
312 | + // create missing dirs if necessary |
|
313 | + self::createMissingDirectories($targetPath, new View('/' . $targetOwner)); |
|
314 | + |
|
315 | + // move the directory containing the versions |
|
316 | + $rootView->$operation( |
|
317 | + '/' . $sourceOwner . '/files_versions/' . $sourcePath, |
|
318 | + '/' . $targetOwner . '/files_versions/' . $targetPath |
|
319 | + ); |
|
320 | + } |
|
321 | + } elseif ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) { |
|
322 | + // create missing dirs if necessary |
|
323 | + self::createMissingDirectories($targetPath, new View('/' . $targetOwner)); |
|
324 | + |
|
325 | + foreach ($versions as $v) { |
|
326 | + // move each version one by one to the target directory |
|
327 | + $rootView->$operation( |
|
328 | + '/' . $sourceOwner . '/files_versions/' . $sourcePath . '.v' . $v['version'], |
|
329 | + '/' . $targetOwner . '/files_versions/' . $targetPath . '.v' . $v['version'] |
|
330 | + ); |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + // if we moved versions directly for a file, schedule expiration check for that file |
|
335 | + if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
336 | + self::scheduleExpire($targetOwner, $targetPath); |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Rollback to an old version of a file. |
|
342 | + * |
|
343 | + * @param string $file file name |
|
344 | + * @param int $revision revision timestamp |
|
345 | + * @return bool |
|
346 | + */ |
|
347 | + public static function rollback(string $file, int $revision, IUser $user) { |
|
348 | + // add expected leading slash |
|
349 | + $filename = '/' . ltrim($file, '/'); |
|
350 | + |
|
351 | + // Fetch the userfolder to trigger view hooks |
|
352 | + $root = Server::get(IRootFolder::class); |
|
353 | + $userFolder = $root->getUserFolder($user->getUID()); |
|
354 | + |
|
355 | + $users_view = new View('/' . $user->getUID()); |
|
356 | + $files_view = new View('/' . $user->getUID() . '/files'); |
|
357 | + |
|
358 | + $versionCreated = false; |
|
359 | + |
|
360 | + $fileInfo = $files_view->getFileInfo($file); |
|
361 | + |
|
362 | + // check if user has the permissions to revert a version |
|
363 | + if (!$fileInfo->isUpdateable()) { |
|
364 | + return false; |
|
365 | + } |
|
366 | + |
|
367 | + //first create a new version |
|
368 | + $version = 'files_versions' . $filename . '.v' . $users_view->filemtime('files' . $filename); |
|
369 | + if (!$users_view->file_exists($version)) { |
|
370 | + $users_view->copy('files' . $filename, 'files_versions' . $filename . '.v' . $users_view->filemtime('files' . $filename)); |
|
371 | + $versionCreated = true; |
|
372 | + } |
|
373 | + |
|
374 | + $fileToRestore = 'files_versions' . $filename . '.v' . $revision; |
|
375 | + |
|
376 | + // Restore encrypted version of the old file for the newly restored file |
|
377 | + // This has to happen manually here since the file is manually copied below |
|
378 | + $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion(); |
|
379 | + $oldFileInfo = $users_view->getFileInfo($fileToRestore); |
|
380 | + $cache = $fileInfo->getStorage()->getCache(); |
|
381 | + $cache->update( |
|
382 | + $fileInfo->getId(), [ |
|
383 | + 'encrypted' => $oldVersion, |
|
384 | + 'encryptedVersion' => $oldVersion, |
|
385 | + 'size' => $oldFileInfo->getData()['size'], |
|
386 | + 'unencrypted_size' => $oldFileInfo->getData()['unencrypted_size'], |
|
387 | + ] |
|
388 | + ); |
|
389 | + |
|
390 | + // rollback |
|
391 | + if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) { |
|
392 | + $files_view->touch($file, $revision); |
|
393 | + Storage::scheduleExpire($user->getUID(), $file); |
|
394 | + |
|
395 | + return true; |
|
396 | + } elseif ($versionCreated) { |
|
397 | + self::deleteVersion($users_view, $version); |
|
398 | + } |
|
399 | + |
|
400 | + return false; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Stream copy file contents from $path1 to $path2 |
|
405 | + * |
|
406 | + * @param View $view view to use for copying |
|
407 | + * @param string $path1 source file to copy |
|
408 | + * @param string $path2 target file |
|
409 | + * |
|
410 | + * @return bool true for success, false otherwise |
|
411 | + */ |
|
412 | + private static function copyFileContents($view, $path1, $path2) { |
|
413 | + /** @var \OC\Files\Storage\Storage $storage1 */ |
|
414 | + [$storage1, $internalPath1] = $view->resolvePath($path1); |
|
415 | + /** @var \OC\Files\Storage\Storage $storage2 */ |
|
416 | + [$storage2, $internalPath2] = $view->resolvePath($path2); |
|
417 | + |
|
418 | + $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
419 | + $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
420 | + |
|
421 | + try { |
|
422 | + // TODO add a proper way of overwriting a file while maintaining file ids |
|
423 | + if ($storage1->instanceOfStorage(ObjectStoreStorage::class) |
|
424 | + || $storage2->instanceOfStorage(ObjectStoreStorage::class) |
|
425 | + ) { |
|
426 | + $source = $storage1->fopen($internalPath1, 'r'); |
|
427 | + $result = $source !== false; |
|
428 | + if ($result) { |
|
429 | + if ($storage2->instanceOfStorage(IWriteStreamStorage::class)) { |
|
430 | + /** @var IWriteStreamStorage $storage2 */ |
|
431 | + $storage2->writeStream($internalPath2, $source); |
|
432 | + } else { |
|
433 | + $target = $storage2->fopen($internalPath2, 'w'); |
|
434 | + $result = $target !== false; |
|
435 | + if ($result) { |
|
436 | + [, $result] = Files::streamCopy($source, $target, true); |
|
437 | + } |
|
438 | + // explicit check as S3 library closes streams already |
|
439 | + if (is_resource($target)) { |
|
440 | + fclose($target); |
|
441 | + } |
|
442 | + } |
|
443 | + } |
|
444 | + // explicit check as S3 library closes streams already |
|
445 | + if (is_resource($source)) { |
|
446 | + fclose($source); |
|
447 | + } |
|
448 | + |
|
449 | + if ($result !== false) { |
|
450 | + $storage1->unlink($internalPath1); |
|
451 | + } |
|
452 | + } else { |
|
453 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
454 | + } |
|
455 | + } finally { |
|
456 | + $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
457 | + $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
458 | + } |
|
459 | + |
|
460 | + return ($result !== false); |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * get a list of all available versions of a file in descending chronological order |
|
465 | + * @param string $uid user id from the owner of the file |
|
466 | + * @param string $filename file to find versions of, relative to the user files dir |
|
467 | + * @param string $userFullPath |
|
468 | + * @return array versions newest version first |
|
469 | + */ |
|
470 | + public static function getVersions($uid, $filename, $userFullPath = '') { |
|
471 | + $versions = []; |
|
472 | + if (empty($filename)) { |
|
473 | + return $versions; |
|
474 | + } |
|
475 | + // fetch for old versions |
|
476 | + $view = new View('/' . $uid . '/'); |
|
477 | + |
|
478 | + $pathinfo = pathinfo($filename); |
|
479 | + $versionedFile = $pathinfo['basename']; |
|
480 | + |
|
481 | + $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']); |
|
482 | + |
|
483 | + $dirContent = false; |
|
484 | + if ($view->is_dir($dir)) { |
|
485 | + $dirContent = $view->opendir($dir); |
|
486 | + } |
|
487 | + |
|
488 | + if ($dirContent === false) { |
|
489 | + return $versions; |
|
490 | + } |
|
491 | + |
|
492 | + if (is_resource($dirContent)) { |
|
493 | + while (($entryName = readdir($dirContent)) !== false) { |
|
494 | + if (!Filesystem::isIgnoredDir($entryName)) { |
|
495 | + $pathparts = pathinfo($entryName); |
|
496 | + $filename = $pathparts['filename']; |
|
497 | + if ($filename === $versionedFile) { |
|
498 | + $pathparts = pathinfo($entryName); |
|
499 | + $timestamp = substr($pathparts['extension'] ?? '', 1); |
|
500 | + if (!is_numeric($timestamp)) { |
|
501 | + Server::get(LoggerInterface::class)->error( |
|
502 | + 'Version file {path} has incorrect name format', |
|
503 | + [ |
|
504 | + 'path' => $entryName, |
|
505 | + 'app' => 'files_versions', |
|
506 | + ] |
|
507 | + ); |
|
508 | + continue; |
|
509 | + } |
|
510 | + $filename = $pathparts['filename']; |
|
511 | + $key = $timestamp . '#' . $filename; |
|
512 | + $versions[$key]['version'] = $timestamp; |
|
513 | + $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp((int)$timestamp); |
|
514 | + if (empty($userFullPath)) { |
|
515 | + $versions[$key]['preview'] = ''; |
|
516 | + } else { |
|
517 | + /** @var IURLGenerator $urlGenerator */ |
|
518 | + $urlGenerator = Server::get(IURLGenerator::class); |
|
519 | + $versions[$key]['preview'] = $urlGenerator->linkToRoute('files_version.Preview.getPreview', |
|
520 | + ['file' => $userFullPath, 'version' => $timestamp]); |
|
521 | + } |
|
522 | + $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename); |
|
523 | + $versions[$key]['name'] = $versionedFile; |
|
524 | + $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName); |
|
525 | + $versions[$key]['mimetype'] = Server::get(IMimeTypeDetector::class)->detectPath($versionedFile); |
|
526 | + } |
|
527 | + } |
|
528 | + } |
|
529 | + closedir($dirContent); |
|
530 | + } |
|
531 | + |
|
532 | + // sort with newest version first |
|
533 | + krsort($versions); |
|
534 | + |
|
535 | + return $versions; |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Expire versions that older than max version retention time |
|
540 | + * |
|
541 | + * @param string $uid |
|
542 | + */ |
|
543 | + public static function expireOlderThanMaxForUser($uid) { |
|
544 | + /** @var IRootFolder $root */ |
|
545 | + $root = Server::get(IRootFolder::class); |
|
546 | + try { |
|
547 | + /** @var Folder $versionsRoot */ |
|
548 | + $versionsRoot = $root->get('/' . $uid . '/files_versions'); |
|
549 | + } catch (NotFoundException $e) { |
|
550 | + return; |
|
551 | + } |
|
552 | + |
|
553 | + $expiration = self::getExpiration(); |
|
554 | + $threshold = $expiration->getMaxAgeAsTimestamp(); |
|
555 | + if (!$threshold) { |
|
556 | + return; |
|
557 | + } |
|
558 | + |
|
559 | + $allVersions = $versionsRoot->search(new SearchQuery( |
|
560 | + new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
561 | + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', FileInfo::MIMETYPE_FOLDER), |
|
562 | + ]), |
|
563 | + 0, |
|
564 | + 0, |
|
565 | + [] |
|
566 | + )); |
|
567 | + |
|
568 | + /** @var VersionsMapper $versionsMapper */ |
|
569 | + $versionsMapper = Server::get(VersionsMapper::class); |
|
570 | + $userFolder = $root->getUserFolder($uid); |
|
571 | + $versionEntities = []; |
|
572 | + |
|
573 | + /** @var Node[] $versions */ |
|
574 | + $versions = array_filter($allVersions, function (Node $info) use ($threshold, $userFolder, $versionsMapper, $versionsRoot, &$versionEntities) { |
|
575 | + // Check that the file match '*.v*' |
|
576 | + $versionsBegin = strrpos($info->getName(), '.v'); |
|
577 | + if ($versionsBegin === false) { |
|
578 | + return false; |
|
579 | + } |
|
580 | + |
|
581 | + $version = (int)substr($info->getName(), $versionsBegin + 2); |
|
582 | + |
|
583 | + // Check that the version does not have a label. |
|
584 | + $path = $versionsRoot->getRelativePath($info->getPath()); |
|
585 | + if ($path === null) { |
|
586 | + throw new DoesNotExistException('Could not find relative path of (' . $info->getPath() . ')'); |
|
587 | + } |
|
588 | + |
|
589 | + try { |
|
590 | + $node = $userFolder->get(substr($path, 0, -strlen('.v' . $version))); |
|
591 | + $versionEntity = $versionsMapper->findVersionForFileId($node->getId(), $version); |
|
592 | + $versionEntities[$info->getId()] = $versionEntity; |
|
593 | + |
|
594 | + if ($versionEntity->getMetadataValue('label') !== null && $versionEntity->getMetadataValue('label') !== '') { |
|
595 | + return false; |
|
596 | + } |
|
597 | + } catch (NotFoundException $e) { |
|
598 | + // Original node not found, delete the version |
|
599 | + return true; |
|
600 | + } catch (StorageNotAvailableException|StorageInvalidException $e) { |
|
601 | + // Storage can't be used, but it might only be temporary so we can't always delete the version |
|
602 | + // since we can't determine if the version is named we take the safe route and don't expire |
|
603 | + return false; |
|
604 | + } catch (DoesNotExistException $ex) { |
|
605 | + // Version on FS can have no equivalent in the DB if they were created before the version naming feature. |
|
606 | + // So we ignore DoesNotExistException. |
|
607 | + } |
|
608 | + |
|
609 | + // Check that the version's timestamp is lower than $threshold |
|
610 | + return $version < $threshold; |
|
611 | + }); |
|
612 | + |
|
613 | + foreach ($versions as $version) { |
|
614 | + $internalPath = $version->getInternalPath(); |
|
615 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]); |
|
616 | + |
|
617 | + $versionEntity = isset($versionEntities[$version->getId()]) ? $versionEntities[$version->getId()] : null; |
|
618 | + if (!is_null($versionEntity)) { |
|
619 | + $versionsMapper->delete($versionEntity); |
|
620 | + } |
|
621 | + |
|
622 | + try { |
|
623 | + $version->delete(); |
|
624 | + \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]); |
|
625 | + } catch (NotPermittedException $e) { |
|
626 | + Server::get(LoggerInterface::class)->error("Missing permissions to delete version: {$internalPath}", ['app' => 'files_versions', 'exception' => $e]); |
|
627 | + } |
|
628 | + } |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * translate a timestamp into a string like "5 days ago" |
|
633 | + * |
|
634 | + * @param int $timestamp |
|
635 | + * @return string for example "5 days ago" |
|
636 | + */ |
|
637 | + private static function getHumanReadableTimestamp(int $timestamp): string { |
|
638 | + $diff = time() - $timestamp; |
|
639 | + |
|
640 | + if ($diff < 60) { // first minute |
|
641 | + return $diff . ' seconds ago'; |
|
642 | + } elseif ($diff < 3600) { //first hour |
|
643 | + return round($diff / 60) . ' minutes ago'; |
|
644 | + } elseif ($diff < 86400) { // first day |
|
645 | + return round($diff / 3600) . ' hours ago'; |
|
646 | + } elseif ($diff < 604800) { //first week |
|
647 | + return round($diff / 86400) . ' days ago'; |
|
648 | + } elseif ($diff < 2419200) { //first month |
|
649 | + return round($diff / 604800) . ' weeks ago'; |
|
650 | + } elseif ($diff < 29030400) { // first year |
|
651 | + return round($diff / 2419200) . ' months ago'; |
|
652 | + } else { |
|
653 | + return round($diff / 29030400) . ' years ago'; |
|
654 | + } |
|
655 | + } |
|
656 | + |
|
657 | + /** |
|
658 | + * returns all stored file versions from a given user |
|
659 | + * @param string $uid id of the user |
|
660 | + * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename |
|
661 | + */ |
|
662 | + private static function getAllVersions($uid) { |
|
663 | + $view = new View('/' . $uid . '/'); |
|
664 | + $dirs = [self::VERSIONS_ROOT]; |
|
665 | + $versions = []; |
|
666 | + |
|
667 | + while (!empty($dirs)) { |
|
668 | + $dir = array_pop($dirs); |
|
669 | + $files = $view->getDirectoryContent($dir); |
|
670 | + |
|
671 | + foreach ($files as $file) { |
|
672 | + $fileData = $file->getData(); |
|
673 | + $filePath = $dir . '/' . $fileData['name']; |
|
674 | + if ($file['type'] === 'dir') { |
|
675 | + $dirs[] = $filePath; |
|
676 | + } else { |
|
677 | + $versionsBegin = strrpos($filePath, '.v'); |
|
678 | + $relPathStart = strlen(self::VERSIONS_ROOT); |
|
679 | + $version = substr($filePath, $versionsBegin + 2); |
|
680 | + $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart); |
|
681 | + $key = $version . '#' . $relpath; |
|
682 | + $versions[$key] = ['path' => $relpath, 'timestamp' => $version]; |
|
683 | + } |
|
684 | + } |
|
685 | + } |
|
686 | + |
|
687 | + // newest version first |
|
688 | + krsort($versions); |
|
689 | + |
|
690 | + $result = [ |
|
691 | + 'all' => [], |
|
692 | + 'by_file' => [], |
|
693 | + ]; |
|
694 | + |
|
695 | + foreach ($versions as $key => $value) { |
|
696 | + $size = $view->filesize(self::VERSIONS_ROOT . '/' . $value['path'] . '.v' . $value['timestamp']); |
|
697 | + $filename = $value['path']; |
|
698 | + |
|
699 | + $result['all'][$key]['version'] = $value['timestamp']; |
|
700 | + $result['all'][$key]['path'] = $filename; |
|
701 | + $result['all'][$key]['size'] = $size; |
|
702 | + |
|
703 | + $result['by_file'][$filename][$key]['version'] = $value['timestamp']; |
|
704 | + $result['by_file'][$filename][$key]['path'] = $filename; |
|
705 | + $result['by_file'][$filename][$key]['size'] = $size; |
|
706 | + } |
|
707 | + |
|
708 | + return $result; |
|
709 | + } |
|
710 | + |
|
711 | + /** |
|
712 | + * get list of files we want to expire |
|
713 | + * @param array $versions list of versions |
|
714 | + * @param integer $time |
|
715 | + * @param bool $quotaExceeded is versions storage limit reached |
|
716 | + * @return array containing the list of to deleted versions and the size of them |
|
717 | + */ |
|
718 | + protected static function getExpireList($time, $versions, $quotaExceeded = false) { |
|
719 | + $expiration = self::getExpiration(); |
|
720 | + |
|
721 | + if ($expiration->shouldAutoExpire()) { |
|
722 | + // Exclude versions that are newer than the minimum age from the auto expiration logic. |
|
723 | + $minAge = $expiration->getMinAgeAsTimestamp(); |
|
724 | + if ($minAge !== false) { |
|
725 | + $versionsToAutoExpire = array_filter($versions, fn ($version) => $version['version'] < $minAge); |
|
726 | + } else { |
|
727 | + $versionsToAutoExpire = $versions; |
|
728 | + } |
|
729 | + |
|
730 | + [$toDelete, $size] = self::getAutoExpireList($time, $versionsToAutoExpire); |
|
731 | + } else { |
|
732 | + $size = 0; |
|
733 | + $toDelete = []; // versions we want to delete |
|
734 | + } |
|
735 | + |
|
736 | + foreach ($versions as $key => $version) { |
|
737 | + if (!is_numeric($version['version'])) { |
|
738 | + Server::get(LoggerInterface::class)->error( |
|
739 | + 'Found a non-numeric timestamp version: ' . json_encode($version), |
|
740 | + ['app' => 'files_versions']); |
|
741 | + continue; |
|
742 | + } |
|
743 | + if ($expiration->isExpired((int)($version['version']), $quotaExceeded) && !isset($toDelete[$key])) { |
|
744 | + $size += $version['size']; |
|
745 | + $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
746 | + } |
|
747 | + } |
|
748 | + |
|
749 | + return [$toDelete, $size]; |
|
750 | + } |
|
751 | + |
|
752 | + /** |
|
753 | + * get list of files we want to expire |
|
754 | + * @param array $versions list of versions |
|
755 | + * @param integer $time |
|
756 | + * @return array containing the list of to deleted versions and the size of them |
|
757 | + */ |
|
758 | + protected static function getAutoExpireList($time, $versions) { |
|
759 | + $size = 0; |
|
760 | + $toDelete = []; // versions we want to delete |
|
761 | + |
|
762 | + $interval = 1; |
|
763 | + $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
764 | + if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
765 | + $nextInterval = -1; |
|
766 | + } else { |
|
767 | + $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
768 | + } |
|
769 | + |
|
770 | + $firstVersion = reset($versions); |
|
771 | + |
|
772 | + if ($firstVersion === false) { |
|
773 | + return [$toDelete, $size]; |
|
774 | + } |
|
775 | + |
|
776 | + $firstKey = key($versions); |
|
777 | + $prevTimestamp = $firstVersion['version']; |
|
778 | + $nextVersion = $firstVersion['version'] - $step; |
|
779 | + unset($versions[$firstKey]); |
|
780 | + |
|
781 | + foreach ($versions as $key => $version) { |
|
782 | + $newInterval = true; |
|
783 | + while ($newInterval) { |
|
784 | + if ($nextInterval === -1 || $prevTimestamp > $nextInterval) { |
|
785 | + if ($version['version'] > $nextVersion) { |
|
786 | + //distance between two version too small, mark to delete |
|
787 | + $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
788 | + $size += $version['size']; |
|
789 | + Server::get(LoggerInterface::class)->info('Mark to expire ' . $version['path'] . ' next version should be ' . $nextVersion . ' or smaller. (prevTimestamp: ' . $prevTimestamp . '; step: ' . $step, ['app' => 'files_versions']); |
|
790 | + } else { |
|
791 | + $nextVersion = $version['version'] - $step; |
|
792 | + $prevTimestamp = $version['version']; |
|
793 | + } |
|
794 | + $newInterval = false; // version checked so we can move to the next one |
|
795 | + } else { // time to move on to the next interval |
|
796 | + $interval++; |
|
797 | + $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
798 | + $nextVersion = $prevTimestamp - $step; |
|
799 | + if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
800 | + $nextInterval = -1; |
|
801 | + } else { |
|
802 | + $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
803 | + } |
|
804 | + $newInterval = true; // we changed the interval -> check same version with new interval |
|
805 | + } |
|
806 | + } |
|
807 | + } |
|
808 | + |
|
809 | + return [$toDelete, $size]; |
|
810 | + } |
|
811 | + |
|
812 | + /** |
|
813 | + * Schedule versions expiration for the given file |
|
814 | + * |
|
815 | + * @param string $uid owner of the file |
|
816 | + * @param string $fileName file/folder for which to schedule expiration |
|
817 | + */ |
|
818 | + public static function scheduleExpire($uid, $fileName) { |
|
819 | + // let the admin disable auto expire |
|
820 | + $expiration = self::getExpiration(); |
|
821 | + if ($expiration->isEnabled()) { |
|
822 | + $command = new Expire($uid, $fileName); |
|
823 | + /** @var IBus $bus */ |
|
824 | + $bus = Server::get(IBus::class); |
|
825 | + $bus->push($command); |
|
826 | + } |
|
827 | + } |
|
828 | + |
|
829 | + /** |
|
830 | + * Expire versions which exceed the quota. |
|
831 | + * |
|
832 | + * This will setup the filesystem for the given user but will not |
|
833 | + * tear it down afterwards. |
|
834 | + * |
|
835 | + * @param string $filename path to file to expire |
|
836 | + * @param string $uid user for which to expire the version |
|
837 | + * @return bool|int|null |
|
838 | + */ |
|
839 | + public static function expire($filename, $uid) { |
|
840 | + $expiration = self::getExpiration(); |
|
841 | + |
|
842 | + /** @var LoggerInterface $logger */ |
|
843 | + $logger = Server::get(LoggerInterface::class); |
|
844 | + |
|
845 | + if ($expiration->isEnabled()) { |
|
846 | + // get available disk space for user |
|
847 | + $user = Server::get(IUserManager::class)->get($uid); |
|
848 | + if (is_null($user)) { |
|
849 | + $logger->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']); |
|
850 | + throw new NoUserException('Backends provided no user object for ' . $uid); |
|
851 | + } |
|
852 | + |
|
853 | + \OC_Util::setupFS($uid); |
|
854 | + |
|
855 | + try { |
|
856 | + if (!Filesystem::file_exists($filename)) { |
|
857 | + return false; |
|
858 | + } |
|
859 | + } catch (StorageNotAvailableException $e) { |
|
860 | + // if we can't check that the file hasn't been deleted we can only assume that it hasn't |
|
861 | + // note that this `StorageNotAvailableException` is about the file the versions originate from, |
|
862 | + // not the storage that the versions are stored on |
|
863 | + } |
|
864 | + |
|
865 | + if (empty($filename)) { |
|
866 | + // file maybe renamed or deleted |
|
867 | + return false; |
|
868 | + } |
|
869 | + $versionsFileview = new View('/' . $uid . '/files_versions'); |
|
870 | + |
|
871 | + $softQuota = true; |
|
872 | + $quota = $user->getQuota(); |
|
873 | + if ($quota === null || $quota === 'none') { |
|
874 | + $quota = Filesystem::free_space('/'); |
|
875 | + $softQuota = false; |
|
876 | + } else { |
|
877 | + $quota = Util::computerFileSize($quota); |
|
878 | + } |
|
879 | + |
|
880 | + // make sure that we have the current size of the version history |
|
881 | + $versionsSize = self::getVersionsSize($uid); |
|
882 | + |
|
883 | + // calculate available space for version history |
|
884 | + // subtract size of files and current versions size from quota |
|
885 | + if ($quota >= 0) { |
|
886 | + if ($softQuota) { |
|
887 | + $root = Server::get(IRootFolder::class); |
|
888 | + $userFolder = $root->getUserFolder($uid); |
|
889 | + if (is_null($userFolder)) { |
|
890 | + $availableSpace = 0; |
|
891 | + } else { |
|
892 | + $free = $quota - $userFolder->getSize(false); // remaining free space for user |
|
893 | + if ($free > 0) { |
|
894 | + $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions |
|
895 | + } else { |
|
896 | + $availableSpace = $free - $versionsSize; |
|
897 | + } |
|
898 | + } |
|
899 | + } else { |
|
900 | + $availableSpace = $quota; |
|
901 | + } |
|
902 | + } else { |
|
903 | + $availableSpace = PHP_INT_MAX; |
|
904 | + } |
|
905 | + |
|
906 | + $allVersions = Storage::getVersions($uid, $filename); |
|
907 | + |
|
908 | + $time = time(); |
|
909 | + [$toDelete, $sizeOfDeletedVersions] = self::getExpireList($time, $allVersions, $availableSpace <= 0); |
|
910 | + |
|
911 | + $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
912 | + $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
913 | + |
|
914 | + // if still not enough free space we rearrange the versions from all files |
|
915 | + if ($availableSpace <= 0) { |
|
916 | + $result = self::getAllVersions($uid); |
|
917 | + $allVersions = $result['all']; |
|
918 | + |
|
919 | + foreach ($result['by_file'] as $versions) { |
|
920 | + [$toDeleteNew, $size] = self::getExpireList($time, $versions, $availableSpace <= 0); |
|
921 | + $toDelete = array_merge($toDelete, $toDeleteNew); |
|
922 | + $sizeOfDeletedVersions += $size; |
|
923 | + } |
|
924 | + $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
925 | + $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
926 | + } |
|
927 | + |
|
928 | + foreach ($toDelete as $key => $path) { |
|
929 | + // Make sure to cleanup version table relations as expire does not pass deleteVersion |
|
930 | + try { |
|
931 | + /** @var VersionsMapper $versionsMapper */ |
|
932 | + $versionsMapper = Server::get(VersionsMapper::class); |
|
933 | + $file = Server::get(IRootFolder::class)->getUserFolder($uid)->get($filename); |
|
934 | + $pathparts = pathinfo($path); |
|
935 | + $timestamp = (int)substr($pathparts['extension'] ?? '', 1); |
|
936 | + $versionEntity = $versionsMapper->findVersionForFileId($file->getId(), $timestamp); |
|
937 | + if ($versionEntity->getMetadataValue('label') !== null && $versionEntity->getMetadataValue('label') !== '') { |
|
938 | + continue; |
|
939 | + } |
|
940 | + $versionsMapper->delete($versionEntity); |
|
941 | + } catch (DoesNotExistException $e) { |
|
942 | + } |
|
943 | + |
|
944 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
945 | + self::deleteVersion($versionsFileview, $path); |
|
946 | + \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
947 | + unset($allVersions[$key]); // update array with the versions we keep |
|
948 | + $logger->info('Expire: ' . $path, ['app' => 'files_versions']); |
|
949 | + } |
|
950 | + |
|
951 | + // Check if enough space is available after versions are rearranged. |
|
952 | + // If not we delete the oldest versions until we meet the size limit for versions, |
|
953 | + // but always keep the two latest versions |
|
954 | + $numOfVersions = count($allVersions) - 2 ; |
|
955 | + $i = 0; |
|
956 | + // sort oldest first and make sure that we start at the first element |
|
957 | + ksort($allVersions); |
|
958 | + reset($allVersions); |
|
959 | + while ($availableSpace < 0 && $i < $numOfVersions) { |
|
960 | + $version = current($allVersions); |
|
961 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $version['path'] . '.v' . $version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
962 | + self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']); |
|
963 | + \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $version['path'] . '.v' . $version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]); |
|
964 | + $logger->info('running out of space! Delete oldest version: ' . $version['path'] . '.v' . $version['version'], ['app' => 'files_versions']); |
|
965 | + $versionsSize -= $version['size']; |
|
966 | + $availableSpace += $version['size']; |
|
967 | + next($allVersions); |
|
968 | + $i++; |
|
969 | + } |
|
970 | + |
|
971 | + return $versionsSize; // finally return the new size of the version history |
|
972 | + } |
|
973 | + |
|
974 | + return false; |
|
975 | + } |
|
976 | + |
|
977 | + /** |
|
978 | + * Create recursively missing directories inside of files_versions |
|
979 | + * that match the given path to a file. |
|
980 | + * |
|
981 | + * @param string $filename $path to a file, relative to the user's |
|
982 | + * "files" folder |
|
983 | + * @param View $view view on data/user/ |
|
984 | + */ |
|
985 | + public static function createMissingDirectories($filename, $view) { |
|
986 | + $dirname = Filesystem::normalizePath(dirname($filename)); |
|
987 | + $dirParts = explode('/', $dirname); |
|
988 | + $dir = '/files_versions'; |
|
989 | + foreach ($dirParts as $part) { |
|
990 | + $dir = $dir . '/' . $part; |
|
991 | + if (!$view->file_exists($dir)) { |
|
992 | + $view->mkdir($dir); |
|
993 | + } |
|
994 | + } |
|
995 | + } |
|
996 | + |
|
997 | + /** |
|
998 | + * Static workaround |
|
999 | + * @return Expiration |
|
1000 | + */ |
|
1001 | + protected static function getExpiration() { |
|
1002 | + if (self::$application === null) { |
|
1003 | + self::$application = Server::get(Application::class); |
|
1004 | + } |
|
1005 | + return self::$application->getContainer()->get(Expiration::class); |
|
1006 | + } |
|
1007 | 1007 | } |
@@ -27,136 +27,136 @@ |
||
27 | 27 | * @package OCA\Files_Versions\Tests\Command |
28 | 28 | */ |
29 | 29 | class CleanupTest extends TestCase { |
30 | - protected Manager&MockObject $userManager; |
|
31 | - protected IRootFolder&MockObject $rootFolder; |
|
32 | - protected VersionsMapper&MockObject $versionMapper; |
|
33 | - protected CleanUp $cleanup; |
|
34 | - |
|
35 | - protected function setUp(): void { |
|
36 | - parent::setUp(); |
|
37 | - |
|
38 | - $this->rootFolder = $this->createMock(IRootFolder::class); |
|
39 | - $this->userManager = $this->createMock(Manager::class); |
|
40 | - $this->versionMapper = $this->createMock(VersionsMapper::class); |
|
41 | - |
|
42 | - $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param boolean $nodeExists |
|
47 | - */ |
|
48 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataTestDeleteVersions')] |
|
49 | - public function testDeleteVersions(bool $nodeExists): void { |
|
50 | - $this->rootFolder->expects($this->once()) |
|
51 | - ->method('nodeExists') |
|
52 | - ->with('/testUser/files_versions') |
|
53 | - ->willReturn($nodeExists); |
|
54 | - |
|
55 | - $userFolder = $this->createMock(Folder::class); |
|
56 | - $userHomeStorage = $this->createMock(IStorage::class); |
|
57 | - $userHomeStorageCache = $this->createMock(ICache::class); |
|
58 | - $this->rootFolder->expects($this->once()) |
|
59 | - ->method('getUserFolder') |
|
60 | - ->willReturn($userFolder); |
|
61 | - $userFolder->expects($this->once()) |
|
62 | - ->method('getStorage') |
|
63 | - ->willReturn($userHomeStorage); |
|
64 | - $userHomeStorage->expects($this->once()) |
|
65 | - ->method('getCache') |
|
66 | - ->willReturn($userHomeStorageCache); |
|
67 | - $userHomeStorageCache->expects($this->once()) |
|
68 | - ->method('getNumericStorageId') |
|
69 | - ->willReturn(1); |
|
70 | - |
|
71 | - if ($nodeExists) { |
|
72 | - $this->rootFolder->expects($this->once()) |
|
73 | - ->method('get') |
|
74 | - ->with('/testUser/files_versions') |
|
75 | - ->willReturn($this->rootFolder); |
|
76 | - $this->rootFolder->expects($this->once()) |
|
77 | - ->method('delete'); |
|
78 | - } else { |
|
79 | - $this->rootFolder->expects($this->never()) |
|
80 | - ->method('get'); |
|
81 | - $this->rootFolder->expects($this->never()) |
|
82 | - ->method('delete'); |
|
83 | - } |
|
84 | - |
|
85 | - $this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']); |
|
86 | - } |
|
87 | - |
|
88 | - public static function dataTestDeleteVersions(): array { |
|
89 | - return [ |
|
90 | - [true], |
|
91 | - [false] |
|
92 | - ]; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * test delete versions from users given as parameter |
|
98 | - */ |
|
99 | - public function testExecuteDeleteListOfUsers(): void { |
|
100 | - $userIds = ['user1', 'user2', 'user3']; |
|
101 | - |
|
102 | - $instance = $this->getMockBuilder(CleanUp::class) |
|
103 | - ->onlyMethods(['deleteVersions']) |
|
104 | - ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper]) |
|
105 | - ->getMock(); |
|
106 | - $instance->expects($this->exactly(count($userIds))) |
|
107 | - ->method('deleteVersions') |
|
108 | - ->willReturnCallback(function ($user) use ($userIds): void { |
|
109 | - $this->assertTrue(in_array($user, $userIds)); |
|
110 | - }); |
|
111 | - |
|
112 | - $this->userManager->expects($this->exactly(count($userIds))) |
|
113 | - ->method('userExists')->willReturn(true); |
|
114 | - |
|
115 | - $inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class); |
|
116 | - $inputInterface->expects($this->once())->method('getArgument') |
|
117 | - ->with('user_id') |
|
118 | - ->willReturn($userIds); |
|
119 | - |
|
120 | - $outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class); |
|
121 | - |
|
122 | - $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * test delete versions of all users |
|
127 | - */ |
|
128 | - public function testExecuteAllUsers(): void { |
|
129 | - $userIds = []; |
|
130 | - $backendUsers = ['user1', 'user2']; |
|
131 | - |
|
132 | - $instance = $this->getMockBuilder(CleanUp::class) |
|
133 | - ->onlyMethods(['deleteVersions']) |
|
134 | - ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper]) |
|
135 | - ->getMock(); |
|
136 | - |
|
137 | - $backend = $this->getMockBuilder(UserInterface::class) |
|
138 | - ->disableOriginalConstructor()->getMock(); |
|
139 | - $backend->expects($this->once())->method('getUsers') |
|
140 | - ->with('', 500, 0) |
|
141 | - ->willReturn($backendUsers); |
|
142 | - |
|
143 | - $instance->expects($this->exactly(count($backendUsers))) |
|
144 | - ->method('deleteVersions') |
|
145 | - ->willReturnCallback(function ($user) use ($backendUsers): void { |
|
146 | - $this->assertTrue(in_array($user, $backendUsers)); |
|
147 | - }); |
|
148 | - |
|
149 | - $inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class); |
|
150 | - $inputInterface->expects($this->once())->method('getArgument') |
|
151 | - ->with('user_id') |
|
152 | - ->willReturn($userIds); |
|
153 | - |
|
154 | - $outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class); |
|
155 | - |
|
156 | - $this->userManager->expects($this->once()) |
|
157 | - ->method('getBackends') |
|
158 | - ->willReturn([$backend]); |
|
159 | - |
|
160 | - $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
161 | - } |
|
30 | + protected Manager&MockObject $userManager; |
|
31 | + protected IRootFolder&MockObject $rootFolder; |
|
32 | + protected VersionsMapper&MockObject $versionMapper; |
|
33 | + protected CleanUp $cleanup; |
|
34 | + |
|
35 | + protected function setUp(): void { |
|
36 | + parent::setUp(); |
|
37 | + |
|
38 | + $this->rootFolder = $this->createMock(IRootFolder::class); |
|
39 | + $this->userManager = $this->createMock(Manager::class); |
|
40 | + $this->versionMapper = $this->createMock(VersionsMapper::class); |
|
41 | + |
|
42 | + $this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param boolean $nodeExists |
|
47 | + */ |
|
48 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestDeleteVersions')] |
|
49 | + public function testDeleteVersions(bool $nodeExists): void { |
|
50 | + $this->rootFolder->expects($this->once()) |
|
51 | + ->method('nodeExists') |
|
52 | + ->with('/testUser/files_versions') |
|
53 | + ->willReturn($nodeExists); |
|
54 | + |
|
55 | + $userFolder = $this->createMock(Folder::class); |
|
56 | + $userHomeStorage = $this->createMock(IStorage::class); |
|
57 | + $userHomeStorageCache = $this->createMock(ICache::class); |
|
58 | + $this->rootFolder->expects($this->once()) |
|
59 | + ->method('getUserFolder') |
|
60 | + ->willReturn($userFolder); |
|
61 | + $userFolder->expects($this->once()) |
|
62 | + ->method('getStorage') |
|
63 | + ->willReturn($userHomeStorage); |
|
64 | + $userHomeStorage->expects($this->once()) |
|
65 | + ->method('getCache') |
|
66 | + ->willReturn($userHomeStorageCache); |
|
67 | + $userHomeStorageCache->expects($this->once()) |
|
68 | + ->method('getNumericStorageId') |
|
69 | + ->willReturn(1); |
|
70 | + |
|
71 | + if ($nodeExists) { |
|
72 | + $this->rootFolder->expects($this->once()) |
|
73 | + ->method('get') |
|
74 | + ->with('/testUser/files_versions') |
|
75 | + ->willReturn($this->rootFolder); |
|
76 | + $this->rootFolder->expects($this->once()) |
|
77 | + ->method('delete'); |
|
78 | + } else { |
|
79 | + $this->rootFolder->expects($this->never()) |
|
80 | + ->method('get'); |
|
81 | + $this->rootFolder->expects($this->never()) |
|
82 | + ->method('delete'); |
|
83 | + } |
|
84 | + |
|
85 | + $this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']); |
|
86 | + } |
|
87 | + |
|
88 | + public static function dataTestDeleteVersions(): array { |
|
89 | + return [ |
|
90 | + [true], |
|
91 | + [false] |
|
92 | + ]; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * test delete versions from users given as parameter |
|
98 | + */ |
|
99 | + public function testExecuteDeleteListOfUsers(): void { |
|
100 | + $userIds = ['user1', 'user2', 'user3']; |
|
101 | + |
|
102 | + $instance = $this->getMockBuilder(CleanUp::class) |
|
103 | + ->onlyMethods(['deleteVersions']) |
|
104 | + ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper]) |
|
105 | + ->getMock(); |
|
106 | + $instance->expects($this->exactly(count($userIds))) |
|
107 | + ->method('deleteVersions') |
|
108 | + ->willReturnCallback(function ($user) use ($userIds): void { |
|
109 | + $this->assertTrue(in_array($user, $userIds)); |
|
110 | + }); |
|
111 | + |
|
112 | + $this->userManager->expects($this->exactly(count($userIds))) |
|
113 | + ->method('userExists')->willReturn(true); |
|
114 | + |
|
115 | + $inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class); |
|
116 | + $inputInterface->expects($this->once())->method('getArgument') |
|
117 | + ->with('user_id') |
|
118 | + ->willReturn($userIds); |
|
119 | + |
|
120 | + $outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class); |
|
121 | + |
|
122 | + $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * test delete versions of all users |
|
127 | + */ |
|
128 | + public function testExecuteAllUsers(): void { |
|
129 | + $userIds = []; |
|
130 | + $backendUsers = ['user1', 'user2']; |
|
131 | + |
|
132 | + $instance = $this->getMockBuilder(CleanUp::class) |
|
133 | + ->onlyMethods(['deleteVersions']) |
|
134 | + ->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper]) |
|
135 | + ->getMock(); |
|
136 | + |
|
137 | + $backend = $this->getMockBuilder(UserInterface::class) |
|
138 | + ->disableOriginalConstructor()->getMock(); |
|
139 | + $backend->expects($this->once())->method('getUsers') |
|
140 | + ->with('', 500, 0) |
|
141 | + ->willReturn($backendUsers); |
|
142 | + |
|
143 | + $instance->expects($this->exactly(count($backendUsers))) |
|
144 | + ->method('deleteVersions') |
|
145 | + ->willReturnCallback(function ($user) use ($backendUsers): void { |
|
146 | + $this->assertTrue(in_array($user, $backendUsers)); |
|
147 | + }); |
|
148 | + |
|
149 | + $inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class); |
|
150 | + $inputInterface->expects($this->once())->method('getArgument') |
|
151 | + ->with('user_id') |
|
152 | + ->willReturn($userIds); |
|
153 | + |
|
154 | + $outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class); |
|
155 | + |
|
156 | + $this->userManager->expects($this->once()) |
|
157 | + ->method('getBackends') |
|
158 | + ->willReturn([$backend]); |
|
159 | + |
|
160 | + $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); |
|
161 | + } |
|
162 | 162 | } |
@@ -38,961 +38,961 @@ |
||
38 | 38 | * @group DB |
39 | 39 | */ |
40 | 40 | class VersioningTest extends \Test\TestCase { |
41 | - public const TEST_VERSIONS_USER = 'test-versions-user'; |
|
42 | - public const TEST_VERSIONS_USER2 = 'test-versions-user2'; |
|
43 | - public const USERS_VERSIONS_ROOT = '/test-versions-user/files_versions'; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var View |
|
47 | - */ |
|
48 | - private $rootView; |
|
49 | - /** |
|
50 | - * @var VersionsMapper |
|
51 | - */ |
|
52 | - private $versionsMapper; |
|
53 | - /** |
|
54 | - * @var IMimeTypeLoader |
|
55 | - */ |
|
56 | - private $mimeTypeLoader; |
|
57 | - private $user1; |
|
58 | - private $user2; |
|
59 | - |
|
60 | - public static function setUpBeforeClass(): void { |
|
61 | - parent::setUpBeforeClass(); |
|
62 | - |
|
63 | - $application = new Application(); |
|
64 | - |
|
65 | - // create test user |
|
66 | - self::loginHelper(self::TEST_VERSIONS_USER2, true); |
|
67 | - self::loginHelper(self::TEST_VERSIONS_USER, true); |
|
68 | - } |
|
69 | - |
|
70 | - public static function tearDownAfterClass(): void { |
|
71 | - // cleanup test user |
|
72 | - $user = Server::get(IUserManager::class)->get(self::TEST_VERSIONS_USER); |
|
73 | - if ($user !== null) { |
|
74 | - $user->delete(); |
|
75 | - } |
|
76 | - $user = Server::get(IUserManager::class)->get(self::TEST_VERSIONS_USER2); |
|
77 | - if ($user !== null) { |
|
78 | - $user->delete(); |
|
79 | - } |
|
80 | - |
|
81 | - parent::tearDownAfterClass(); |
|
82 | - } |
|
83 | - |
|
84 | - protected function setUp(): void { |
|
85 | - parent::setUp(); |
|
86 | - |
|
87 | - $config = Server::get(IConfig::class); |
|
88 | - $mockConfig = $this->getMockBuilder(AllConfig::class) |
|
89 | - ->onlyMethods(['getSystemValue']) |
|
90 | - ->setConstructorArgs([Server::get(SystemConfig::class)]) |
|
91 | - ->getMock(); |
|
92 | - $mockConfig->expects($this->any()) |
|
93 | - ->method('getSystemValue') |
|
94 | - ->willReturnCallback(function ($key, $default) use ($config) { |
|
95 | - if ($key === 'filesystem_check_changes') { |
|
96 | - return Watcher::CHECK_ONCE; |
|
97 | - } else { |
|
98 | - return $config->getSystemValue($key, $default); |
|
99 | - } |
|
100 | - }); |
|
101 | - $this->overwriteService(AllConfig::class, $mockConfig); |
|
102 | - |
|
103 | - // clear hooks |
|
104 | - \OC_Hook::clear(); |
|
105 | - \OC::registerShareHooks(Server::get(SystemConfig::class)); |
|
106 | - \OC::$server->boot(); |
|
107 | - |
|
108 | - self::loginHelper(self::TEST_VERSIONS_USER); |
|
109 | - $this->rootView = new View(); |
|
110 | - if (!$this->rootView->file_exists(self::USERS_VERSIONS_ROOT)) { |
|
111 | - $this->rootView->mkdir(self::USERS_VERSIONS_ROOT); |
|
112 | - } |
|
113 | - |
|
114 | - $this->versionsMapper = Server::get(VersionsMapper::class); |
|
115 | - $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class); |
|
116 | - |
|
117 | - $this->user1 = $this->createMock(IUser::class); |
|
118 | - $this->user1->method('getUID') |
|
119 | - ->willReturn(self::TEST_VERSIONS_USER); |
|
120 | - $this->user2 = $this->createMock(IUser::class); |
|
121 | - $this->user2->method('getUID') |
|
122 | - ->willReturn(self::TEST_VERSIONS_USER2); |
|
123 | - } |
|
124 | - |
|
125 | - protected function tearDown(): void { |
|
126 | - $this->restoreService(AllConfig::class); |
|
127 | - |
|
128 | - if ($this->rootView) { |
|
129 | - $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/'); |
|
130 | - $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/'); |
|
131 | - $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files_versions/'); |
|
132 | - $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files_versions/'); |
|
133 | - } |
|
134 | - |
|
135 | - \OC_Hook::clear(); |
|
136 | - |
|
137 | - parent::tearDown(); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @medium |
|
142 | - * test expire logic |
|
143 | - */ |
|
144 | - #[\PHPUnit\Framework\Attributes\DataProvider('versionsProvider')] |
|
145 | - public function testGetExpireList($versions, $sizeOfAllDeletedFiles): void { |
|
146 | - |
|
147 | - // last interval end at 2592000 |
|
148 | - $startTime = 5000000; |
|
149 | - |
|
150 | - $testClass = new VersionStorageToTest(); |
|
151 | - [$deleted, $size] = $testClass->callProtectedGetExpireList($startTime, $versions); |
|
152 | - |
|
153 | - // we should have deleted 16 files each of the size 1 |
|
154 | - $this->assertEquals($sizeOfAllDeletedFiles, $size); |
|
155 | - |
|
156 | - // the deleted array should only contain versions which should be deleted |
|
157 | - foreach ($deleted as $key => $path) { |
|
158 | - unset($versions[$key]); |
|
159 | - $this->assertEquals('delete', substr($path, 0, strlen('delete'))); |
|
160 | - } |
|
161 | - |
|
162 | - // the versions array should only contain versions which should be kept |
|
163 | - foreach ($versions as $version) { |
|
164 | - $this->assertEquals('keep', $version['path']); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - public static function versionsProvider(): array { |
|
169 | - return [ |
|
170 | - // first set of versions uniformly distributed versions |
|
171 | - [ |
|
172 | - [ |
|
173 | - // first slice (10sec) keep one version every 2 seconds |
|
174 | - ['version' => 4999999, 'path' => 'keep', 'size' => 1], |
|
175 | - ['version' => 4999998, 'path' => 'delete', 'size' => 1], |
|
176 | - ['version' => 4999997, 'path' => 'keep', 'size' => 1], |
|
177 | - ['version' => 4999995, 'path' => 'keep', 'size' => 1], |
|
178 | - ['version' => 4999994, 'path' => 'delete', 'size' => 1], |
|
179 | - //next slice (60sec) starts at 4999990 keep one version every 10 secons |
|
180 | - ['version' => 4999988, 'path' => 'keep', 'size' => 1], |
|
181 | - ['version' => 4999978, 'path' => 'keep', 'size' => 1], |
|
182 | - ['version' => 4999975, 'path' => 'delete', 'size' => 1], |
|
183 | - ['version' => 4999972, 'path' => 'delete', 'size' => 1], |
|
184 | - ['version' => 4999967, 'path' => 'keep', 'size' => 1], |
|
185 | - ['version' => 4999958, 'path' => 'delete', 'size' => 1], |
|
186 | - ['version' => 4999957, 'path' => 'keep', 'size' => 1], |
|
187 | - //next slice (3600sec) start at 4999940 keep one version every 60 seconds |
|
188 | - ['version' => 4999900, 'path' => 'keep', 'size' => 1], |
|
189 | - ['version' => 4999841, 'path' => 'delete', 'size' => 1], |
|
190 | - ['version' => 4999840, 'path' => 'keep', 'size' => 1], |
|
191 | - ['version' => 4999780, 'path' => 'keep', 'size' => 1], |
|
192 | - ['version' => 4996401, 'path' => 'keep', 'size' => 1], |
|
193 | - // next slice (86400sec) start at 4996400 keep one version every 3600 seconds |
|
194 | - ['version' => 4996350, 'path' => 'delete', 'size' => 1], |
|
195 | - ['version' => 4992800, 'path' => 'keep', 'size' => 1], |
|
196 | - ['version' => 4989800, 'path' => 'delete', 'size' => 1], |
|
197 | - ['version' => 4989700, 'path' => 'delete', 'size' => 1], |
|
198 | - ['version' => 4989200, 'path' => 'keep', 'size' => 1], |
|
199 | - // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds |
|
200 | - ['version' => 4913600, 'path' => 'keep', 'size' => 1], |
|
201 | - ['version' => 4852800, 'path' => 'delete', 'size' => 1], |
|
202 | - ['version' => 4827201, 'path' => 'delete', 'size' => 1], |
|
203 | - ['version' => 4827200, 'path' => 'keep', 'size' => 1], |
|
204 | - ['version' => 4777201, 'path' => 'delete', 'size' => 1], |
|
205 | - ['version' => 4777501, 'path' => 'delete', 'size' => 1], |
|
206 | - ['version' => 4740000, 'path' => 'keep', 'size' => 1], |
|
207 | - // final slice starts at 2408000 keep one version every 604800 secons |
|
208 | - ['version' => 2408000, 'path' => 'keep', 'size' => 1], |
|
209 | - ['version' => 1803201, 'path' => 'delete', 'size' => 1], |
|
210 | - ['version' => 1803200, 'path' => 'keep', 'size' => 1], |
|
211 | - ['version' => 1800199, 'path' => 'delete', 'size' => 1], |
|
212 | - ['version' => 1800100, 'path' => 'delete', 'size' => 1], |
|
213 | - ['version' => 1198300, 'path' => 'keep', 'size' => 1], |
|
214 | - ], |
|
215 | - 16 // size of all deleted files (every file has the size 1) |
|
216 | - ], |
|
217 | - // second set of versions, here we have only really old versions |
|
218 | - [ |
|
219 | - [ |
|
220 | - // first slice (10sec) keep one version every 2 seconds |
|
221 | - // next slice (60sec) starts at 4999990 keep one version every 10 secons |
|
222 | - // next slice (3600sec) start at 4999940 keep one version every 60 seconds |
|
223 | - // next slice (86400sec) start at 4996400 keep one version every 3600 seconds |
|
224 | - ['version' => 4996400, 'path' => 'keep', 'size' => 1], |
|
225 | - ['version' => 4996350, 'path' => 'delete', 'size' => 1], |
|
226 | - ['version' => 4996350, 'path' => 'delete', 'size' => 1], |
|
227 | - ['version' => 4992800, 'path' => 'keep', 'size' => 1], |
|
228 | - ['version' => 4989800, 'path' => 'delete', 'size' => 1], |
|
229 | - ['version' => 4989700, 'path' => 'delete', 'size' => 1], |
|
230 | - ['version' => 4989200, 'path' => 'keep', 'size' => 1], |
|
231 | - // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds |
|
232 | - ['version' => 4913600, 'path' => 'keep', 'size' => 1], |
|
233 | - ['version' => 4852800, 'path' => 'delete', 'size' => 1], |
|
234 | - ['version' => 4827201, 'path' => 'delete', 'size' => 1], |
|
235 | - ['version' => 4827200, 'path' => 'keep', 'size' => 1], |
|
236 | - ['version' => 4777201, 'path' => 'delete', 'size' => 1], |
|
237 | - ['version' => 4777501, 'path' => 'delete', 'size' => 1], |
|
238 | - ['version' => 4740000, 'path' => 'keep', 'size' => 1], |
|
239 | - // final slice starts at 2408000 keep one version every 604800 secons |
|
240 | - ['version' => 2408000, 'path' => 'keep', 'size' => 1], |
|
241 | - ['version' => 1803201, 'path' => 'delete', 'size' => 1], |
|
242 | - ['version' => 1803200, 'path' => 'keep', 'size' => 1], |
|
243 | - ['version' => 1800199, 'path' => 'delete', 'size' => 1], |
|
244 | - ['version' => 1800100, 'path' => 'delete', 'size' => 1], |
|
245 | - ['version' => 1198300, 'path' => 'keep', 'size' => 1], |
|
246 | - ], |
|
247 | - 11 // size of all deleted files (every file has the size 1) |
|
248 | - ], |
|
249 | - // third set of versions, with some gaps between |
|
250 | - [ |
|
251 | - [ |
|
252 | - // first slice (10sec) keep one version every 2 seconds |
|
253 | - ['version' => 4999999, 'path' => 'keep', 'size' => 1], |
|
254 | - ['version' => 4999998, 'path' => 'delete', 'size' => 1], |
|
255 | - ['version' => 4999997, 'path' => 'keep', 'size' => 1], |
|
256 | - ['version' => 4999995, 'path' => 'keep', 'size' => 1], |
|
257 | - ['version' => 4999994, 'path' => 'delete', 'size' => 1], |
|
258 | - //next slice (60sec) starts at 4999990 keep one version every 10 secons |
|
259 | - ['version' => 4999988, 'path' => 'keep', 'size' => 1], |
|
260 | - ['version' => 4999978, 'path' => 'keep', 'size' => 1], |
|
261 | - //next slice (3600sec) start at 4999940 keep one version every 60 seconds |
|
262 | - // next slice (86400sec) start at 4996400 keep one version every 3600 seconds |
|
263 | - ['version' => 4989200, 'path' => 'keep', 'size' => 1], |
|
264 | - // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds |
|
265 | - ['version' => 4913600, 'path' => 'keep', 'size' => 1], |
|
266 | - ['version' => 4852800, 'path' => 'delete', 'size' => 1], |
|
267 | - ['version' => 4827201, 'path' => 'delete', 'size' => 1], |
|
268 | - ['version' => 4827200, 'path' => 'keep', 'size' => 1], |
|
269 | - ['version' => 4777201, 'path' => 'delete', 'size' => 1], |
|
270 | - ['version' => 4777501, 'path' => 'delete', 'size' => 1], |
|
271 | - ['version' => 4740000, 'path' => 'keep', 'size' => 1], |
|
272 | - // final slice starts at 2408000 keep one version every 604800 secons |
|
273 | - ['version' => 2408000, 'path' => 'keep', 'size' => 1], |
|
274 | - ['version' => 1803201, 'path' => 'delete', 'size' => 1], |
|
275 | - ['version' => 1803200, 'path' => 'keep', 'size' => 1], |
|
276 | - ['version' => 1800199, 'path' => 'delete', 'size' => 1], |
|
277 | - ['version' => 1800100, 'path' => 'delete', 'size' => 1], |
|
278 | - ['version' => 1198300, 'path' => 'keep', 'size' => 1], |
|
279 | - ], |
|
280 | - 9 // size of all deleted files (every file has the size 1) |
|
281 | - ], |
|
282 | - // fourth set of versions: empty (see issue #19066) |
|
283 | - [ |
|
284 | - [], |
|
285 | - 0 |
|
286 | - ] |
|
287 | - |
|
288 | - ]; |
|
289 | - } |
|
290 | - |
|
291 | - public function testRename(): void { |
|
292 | - Filesystem::file_put_contents('test.txt', 'test file'); |
|
293 | - |
|
294 | - $t1 = time(); |
|
295 | - // second version is two weeks older, this way we make sure that no |
|
296 | - // version will be expired |
|
297 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
298 | - |
|
299 | - // create some versions |
|
300 | - $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; |
|
301 | - $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; |
|
302 | - $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; |
|
303 | - $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; |
|
304 | - |
|
305 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
306 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
307 | - |
|
308 | - // execute rename hook of versions app |
|
309 | - Filesystem::rename('test.txt', 'test2.txt'); |
|
310 | - |
|
311 | - $this->runCommands(); |
|
312 | - |
|
313 | - $this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist'); |
|
314 | - $this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist'); |
|
315 | - |
|
316 | - $this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists'); |
|
317 | - $this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists'); |
|
318 | - } |
|
319 | - |
|
320 | - public function testRenameInSharedFolder(): void { |
|
321 | - Filesystem::mkdir('folder1'); |
|
322 | - Filesystem::mkdir('folder1/folder2'); |
|
323 | - Filesystem::file_put_contents('folder1/test.txt', 'test file'); |
|
324 | - |
|
325 | - $t1 = time(); |
|
326 | - // second version is two weeks older, this way we make sure that no |
|
327 | - // version will be expired |
|
328 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
329 | - |
|
330 | - $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1'); |
|
331 | - // create some versions |
|
332 | - $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1; |
|
333 | - $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2; |
|
334 | - $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t1; |
|
335 | - $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t2; |
|
336 | - |
|
337 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
338 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
339 | - |
|
340 | - $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1'); |
|
341 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
342 | - $share->setNode($node) |
|
343 | - ->setShareType(IShare::TYPE_USER) |
|
344 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
345 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
346 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
347 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
348 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
349 | - |
|
350 | - self::loginHelper(self::TEST_VERSIONS_USER2); |
|
351 | - |
|
352 | - $this->assertTrue(Filesystem::file_exists('folder1/test.txt')); |
|
353 | - |
|
354 | - // execute rename hook of versions app |
|
355 | - Filesystem::rename('/folder1/test.txt', '/folder1/folder2/test.txt'); |
|
356 | - |
|
357 | - $this->runCommands(); |
|
358 | - |
|
359 | - self::loginHelper(self::TEST_VERSIONS_USER); |
|
360 | - |
|
361 | - $this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist'); |
|
362 | - $this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist'); |
|
363 | - |
|
364 | - $this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists'); |
|
365 | - $this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists'); |
|
366 | - |
|
367 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
368 | - } |
|
369 | - |
|
370 | - public function testMoveFolder(): void { |
|
371 | - Filesystem::mkdir('folder1'); |
|
372 | - Filesystem::mkdir('folder2'); |
|
373 | - Filesystem::file_put_contents('folder1/test.txt', 'test file'); |
|
374 | - |
|
375 | - $t1 = time(); |
|
376 | - // second version is two weeks older, this way we make sure that no |
|
377 | - // version will be expired |
|
378 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
379 | - |
|
380 | - // create some versions |
|
381 | - $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1'); |
|
382 | - $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1; |
|
383 | - $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2; |
|
384 | - $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t1; |
|
385 | - $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t2; |
|
41 | + public const TEST_VERSIONS_USER = 'test-versions-user'; |
|
42 | + public const TEST_VERSIONS_USER2 = 'test-versions-user2'; |
|
43 | + public const USERS_VERSIONS_ROOT = '/test-versions-user/files_versions'; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var View |
|
47 | + */ |
|
48 | + private $rootView; |
|
49 | + /** |
|
50 | + * @var VersionsMapper |
|
51 | + */ |
|
52 | + private $versionsMapper; |
|
53 | + /** |
|
54 | + * @var IMimeTypeLoader |
|
55 | + */ |
|
56 | + private $mimeTypeLoader; |
|
57 | + private $user1; |
|
58 | + private $user2; |
|
59 | + |
|
60 | + public static function setUpBeforeClass(): void { |
|
61 | + parent::setUpBeforeClass(); |
|
62 | + |
|
63 | + $application = new Application(); |
|
64 | + |
|
65 | + // create test user |
|
66 | + self::loginHelper(self::TEST_VERSIONS_USER2, true); |
|
67 | + self::loginHelper(self::TEST_VERSIONS_USER, true); |
|
68 | + } |
|
69 | + |
|
70 | + public static function tearDownAfterClass(): void { |
|
71 | + // cleanup test user |
|
72 | + $user = Server::get(IUserManager::class)->get(self::TEST_VERSIONS_USER); |
|
73 | + if ($user !== null) { |
|
74 | + $user->delete(); |
|
75 | + } |
|
76 | + $user = Server::get(IUserManager::class)->get(self::TEST_VERSIONS_USER2); |
|
77 | + if ($user !== null) { |
|
78 | + $user->delete(); |
|
79 | + } |
|
80 | + |
|
81 | + parent::tearDownAfterClass(); |
|
82 | + } |
|
83 | + |
|
84 | + protected function setUp(): void { |
|
85 | + parent::setUp(); |
|
86 | + |
|
87 | + $config = Server::get(IConfig::class); |
|
88 | + $mockConfig = $this->getMockBuilder(AllConfig::class) |
|
89 | + ->onlyMethods(['getSystemValue']) |
|
90 | + ->setConstructorArgs([Server::get(SystemConfig::class)]) |
|
91 | + ->getMock(); |
|
92 | + $mockConfig->expects($this->any()) |
|
93 | + ->method('getSystemValue') |
|
94 | + ->willReturnCallback(function ($key, $default) use ($config) { |
|
95 | + if ($key === 'filesystem_check_changes') { |
|
96 | + return Watcher::CHECK_ONCE; |
|
97 | + } else { |
|
98 | + return $config->getSystemValue($key, $default); |
|
99 | + } |
|
100 | + }); |
|
101 | + $this->overwriteService(AllConfig::class, $mockConfig); |
|
102 | + |
|
103 | + // clear hooks |
|
104 | + \OC_Hook::clear(); |
|
105 | + \OC::registerShareHooks(Server::get(SystemConfig::class)); |
|
106 | + \OC::$server->boot(); |
|
107 | + |
|
108 | + self::loginHelper(self::TEST_VERSIONS_USER); |
|
109 | + $this->rootView = new View(); |
|
110 | + if (!$this->rootView->file_exists(self::USERS_VERSIONS_ROOT)) { |
|
111 | + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT); |
|
112 | + } |
|
113 | + |
|
114 | + $this->versionsMapper = Server::get(VersionsMapper::class); |
|
115 | + $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class); |
|
116 | + |
|
117 | + $this->user1 = $this->createMock(IUser::class); |
|
118 | + $this->user1->method('getUID') |
|
119 | + ->willReturn(self::TEST_VERSIONS_USER); |
|
120 | + $this->user2 = $this->createMock(IUser::class); |
|
121 | + $this->user2->method('getUID') |
|
122 | + ->willReturn(self::TEST_VERSIONS_USER2); |
|
123 | + } |
|
124 | + |
|
125 | + protected function tearDown(): void { |
|
126 | + $this->restoreService(AllConfig::class); |
|
127 | + |
|
128 | + if ($this->rootView) { |
|
129 | + $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/'); |
|
130 | + $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/'); |
|
131 | + $this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files_versions/'); |
|
132 | + $this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files_versions/'); |
|
133 | + } |
|
134 | + |
|
135 | + \OC_Hook::clear(); |
|
136 | + |
|
137 | + parent::tearDown(); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @medium |
|
142 | + * test expire logic |
|
143 | + */ |
|
144 | + #[\PHPUnit\Framework\Attributes\DataProvider('versionsProvider')] |
|
145 | + public function testGetExpireList($versions, $sizeOfAllDeletedFiles): void { |
|
146 | + |
|
147 | + // last interval end at 2592000 |
|
148 | + $startTime = 5000000; |
|
149 | + |
|
150 | + $testClass = new VersionStorageToTest(); |
|
151 | + [$deleted, $size] = $testClass->callProtectedGetExpireList($startTime, $versions); |
|
152 | + |
|
153 | + // we should have deleted 16 files each of the size 1 |
|
154 | + $this->assertEquals($sizeOfAllDeletedFiles, $size); |
|
155 | + |
|
156 | + // the deleted array should only contain versions which should be deleted |
|
157 | + foreach ($deleted as $key => $path) { |
|
158 | + unset($versions[$key]); |
|
159 | + $this->assertEquals('delete', substr($path, 0, strlen('delete'))); |
|
160 | + } |
|
161 | + |
|
162 | + // the versions array should only contain versions which should be kept |
|
163 | + foreach ($versions as $version) { |
|
164 | + $this->assertEquals('keep', $version['path']); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + public static function versionsProvider(): array { |
|
169 | + return [ |
|
170 | + // first set of versions uniformly distributed versions |
|
171 | + [ |
|
172 | + [ |
|
173 | + // first slice (10sec) keep one version every 2 seconds |
|
174 | + ['version' => 4999999, 'path' => 'keep', 'size' => 1], |
|
175 | + ['version' => 4999998, 'path' => 'delete', 'size' => 1], |
|
176 | + ['version' => 4999997, 'path' => 'keep', 'size' => 1], |
|
177 | + ['version' => 4999995, 'path' => 'keep', 'size' => 1], |
|
178 | + ['version' => 4999994, 'path' => 'delete', 'size' => 1], |
|
179 | + //next slice (60sec) starts at 4999990 keep one version every 10 secons |
|
180 | + ['version' => 4999988, 'path' => 'keep', 'size' => 1], |
|
181 | + ['version' => 4999978, 'path' => 'keep', 'size' => 1], |
|
182 | + ['version' => 4999975, 'path' => 'delete', 'size' => 1], |
|
183 | + ['version' => 4999972, 'path' => 'delete', 'size' => 1], |
|
184 | + ['version' => 4999967, 'path' => 'keep', 'size' => 1], |
|
185 | + ['version' => 4999958, 'path' => 'delete', 'size' => 1], |
|
186 | + ['version' => 4999957, 'path' => 'keep', 'size' => 1], |
|
187 | + //next slice (3600sec) start at 4999940 keep one version every 60 seconds |
|
188 | + ['version' => 4999900, 'path' => 'keep', 'size' => 1], |
|
189 | + ['version' => 4999841, 'path' => 'delete', 'size' => 1], |
|
190 | + ['version' => 4999840, 'path' => 'keep', 'size' => 1], |
|
191 | + ['version' => 4999780, 'path' => 'keep', 'size' => 1], |
|
192 | + ['version' => 4996401, 'path' => 'keep', 'size' => 1], |
|
193 | + // next slice (86400sec) start at 4996400 keep one version every 3600 seconds |
|
194 | + ['version' => 4996350, 'path' => 'delete', 'size' => 1], |
|
195 | + ['version' => 4992800, 'path' => 'keep', 'size' => 1], |
|
196 | + ['version' => 4989800, 'path' => 'delete', 'size' => 1], |
|
197 | + ['version' => 4989700, 'path' => 'delete', 'size' => 1], |
|
198 | + ['version' => 4989200, 'path' => 'keep', 'size' => 1], |
|
199 | + // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds |
|
200 | + ['version' => 4913600, 'path' => 'keep', 'size' => 1], |
|
201 | + ['version' => 4852800, 'path' => 'delete', 'size' => 1], |
|
202 | + ['version' => 4827201, 'path' => 'delete', 'size' => 1], |
|
203 | + ['version' => 4827200, 'path' => 'keep', 'size' => 1], |
|
204 | + ['version' => 4777201, 'path' => 'delete', 'size' => 1], |
|
205 | + ['version' => 4777501, 'path' => 'delete', 'size' => 1], |
|
206 | + ['version' => 4740000, 'path' => 'keep', 'size' => 1], |
|
207 | + // final slice starts at 2408000 keep one version every 604800 secons |
|
208 | + ['version' => 2408000, 'path' => 'keep', 'size' => 1], |
|
209 | + ['version' => 1803201, 'path' => 'delete', 'size' => 1], |
|
210 | + ['version' => 1803200, 'path' => 'keep', 'size' => 1], |
|
211 | + ['version' => 1800199, 'path' => 'delete', 'size' => 1], |
|
212 | + ['version' => 1800100, 'path' => 'delete', 'size' => 1], |
|
213 | + ['version' => 1198300, 'path' => 'keep', 'size' => 1], |
|
214 | + ], |
|
215 | + 16 // size of all deleted files (every file has the size 1) |
|
216 | + ], |
|
217 | + // second set of versions, here we have only really old versions |
|
218 | + [ |
|
219 | + [ |
|
220 | + // first slice (10sec) keep one version every 2 seconds |
|
221 | + // next slice (60sec) starts at 4999990 keep one version every 10 secons |
|
222 | + // next slice (3600sec) start at 4999940 keep one version every 60 seconds |
|
223 | + // next slice (86400sec) start at 4996400 keep one version every 3600 seconds |
|
224 | + ['version' => 4996400, 'path' => 'keep', 'size' => 1], |
|
225 | + ['version' => 4996350, 'path' => 'delete', 'size' => 1], |
|
226 | + ['version' => 4996350, 'path' => 'delete', 'size' => 1], |
|
227 | + ['version' => 4992800, 'path' => 'keep', 'size' => 1], |
|
228 | + ['version' => 4989800, 'path' => 'delete', 'size' => 1], |
|
229 | + ['version' => 4989700, 'path' => 'delete', 'size' => 1], |
|
230 | + ['version' => 4989200, 'path' => 'keep', 'size' => 1], |
|
231 | + // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds |
|
232 | + ['version' => 4913600, 'path' => 'keep', 'size' => 1], |
|
233 | + ['version' => 4852800, 'path' => 'delete', 'size' => 1], |
|
234 | + ['version' => 4827201, 'path' => 'delete', 'size' => 1], |
|
235 | + ['version' => 4827200, 'path' => 'keep', 'size' => 1], |
|
236 | + ['version' => 4777201, 'path' => 'delete', 'size' => 1], |
|
237 | + ['version' => 4777501, 'path' => 'delete', 'size' => 1], |
|
238 | + ['version' => 4740000, 'path' => 'keep', 'size' => 1], |
|
239 | + // final slice starts at 2408000 keep one version every 604800 secons |
|
240 | + ['version' => 2408000, 'path' => 'keep', 'size' => 1], |
|
241 | + ['version' => 1803201, 'path' => 'delete', 'size' => 1], |
|
242 | + ['version' => 1803200, 'path' => 'keep', 'size' => 1], |
|
243 | + ['version' => 1800199, 'path' => 'delete', 'size' => 1], |
|
244 | + ['version' => 1800100, 'path' => 'delete', 'size' => 1], |
|
245 | + ['version' => 1198300, 'path' => 'keep', 'size' => 1], |
|
246 | + ], |
|
247 | + 11 // size of all deleted files (every file has the size 1) |
|
248 | + ], |
|
249 | + // third set of versions, with some gaps between |
|
250 | + [ |
|
251 | + [ |
|
252 | + // first slice (10sec) keep one version every 2 seconds |
|
253 | + ['version' => 4999999, 'path' => 'keep', 'size' => 1], |
|
254 | + ['version' => 4999998, 'path' => 'delete', 'size' => 1], |
|
255 | + ['version' => 4999997, 'path' => 'keep', 'size' => 1], |
|
256 | + ['version' => 4999995, 'path' => 'keep', 'size' => 1], |
|
257 | + ['version' => 4999994, 'path' => 'delete', 'size' => 1], |
|
258 | + //next slice (60sec) starts at 4999990 keep one version every 10 secons |
|
259 | + ['version' => 4999988, 'path' => 'keep', 'size' => 1], |
|
260 | + ['version' => 4999978, 'path' => 'keep', 'size' => 1], |
|
261 | + //next slice (3600sec) start at 4999940 keep one version every 60 seconds |
|
262 | + // next slice (86400sec) start at 4996400 keep one version every 3600 seconds |
|
263 | + ['version' => 4989200, 'path' => 'keep', 'size' => 1], |
|
264 | + // next slice (2592000sec) start at 4913600 keep one version every 86400 seconds |
|
265 | + ['version' => 4913600, 'path' => 'keep', 'size' => 1], |
|
266 | + ['version' => 4852800, 'path' => 'delete', 'size' => 1], |
|
267 | + ['version' => 4827201, 'path' => 'delete', 'size' => 1], |
|
268 | + ['version' => 4827200, 'path' => 'keep', 'size' => 1], |
|
269 | + ['version' => 4777201, 'path' => 'delete', 'size' => 1], |
|
270 | + ['version' => 4777501, 'path' => 'delete', 'size' => 1], |
|
271 | + ['version' => 4740000, 'path' => 'keep', 'size' => 1], |
|
272 | + // final slice starts at 2408000 keep one version every 604800 secons |
|
273 | + ['version' => 2408000, 'path' => 'keep', 'size' => 1], |
|
274 | + ['version' => 1803201, 'path' => 'delete', 'size' => 1], |
|
275 | + ['version' => 1803200, 'path' => 'keep', 'size' => 1], |
|
276 | + ['version' => 1800199, 'path' => 'delete', 'size' => 1], |
|
277 | + ['version' => 1800100, 'path' => 'delete', 'size' => 1], |
|
278 | + ['version' => 1198300, 'path' => 'keep', 'size' => 1], |
|
279 | + ], |
|
280 | + 9 // size of all deleted files (every file has the size 1) |
|
281 | + ], |
|
282 | + // fourth set of versions: empty (see issue #19066) |
|
283 | + [ |
|
284 | + [], |
|
285 | + 0 |
|
286 | + ] |
|
287 | + |
|
288 | + ]; |
|
289 | + } |
|
290 | + |
|
291 | + public function testRename(): void { |
|
292 | + Filesystem::file_put_contents('test.txt', 'test file'); |
|
293 | + |
|
294 | + $t1 = time(); |
|
295 | + // second version is two weeks older, this way we make sure that no |
|
296 | + // version will be expired |
|
297 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
298 | + |
|
299 | + // create some versions |
|
300 | + $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; |
|
301 | + $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; |
|
302 | + $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; |
|
303 | + $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; |
|
304 | + |
|
305 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
306 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
307 | + |
|
308 | + // execute rename hook of versions app |
|
309 | + Filesystem::rename('test.txt', 'test2.txt'); |
|
310 | + |
|
311 | + $this->runCommands(); |
|
312 | + |
|
313 | + $this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist'); |
|
314 | + $this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist'); |
|
315 | + |
|
316 | + $this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists'); |
|
317 | + $this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists'); |
|
318 | + } |
|
319 | + |
|
320 | + public function testRenameInSharedFolder(): void { |
|
321 | + Filesystem::mkdir('folder1'); |
|
322 | + Filesystem::mkdir('folder1/folder2'); |
|
323 | + Filesystem::file_put_contents('folder1/test.txt', 'test file'); |
|
324 | + |
|
325 | + $t1 = time(); |
|
326 | + // second version is two weeks older, this way we make sure that no |
|
327 | + // version will be expired |
|
328 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
329 | + |
|
330 | + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1'); |
|
331 | + // create some versions |
|
332 | + $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1; |
|
333 | + $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2; |
|
334 | + $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t1; |
|
335 | + $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t2; |
|
336 | + |
|
337 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
338 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
339 | + |
|
340 | + $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1'); |
|
341 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
342 | + $share->setNode($node) |
|
343 | + ->setShareType(IShare::TYPE_USER) |
|
344 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
345 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
346 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
347 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
348 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
349 | + |
|
350 | + self::loginHelper(self::TEST_VERSIONS_USER2); |
|
351 | + |
|
352 | + $this->assertTrue(Filesystem::file_exists('folder1/test.txt')); |
|
353 | + |
|
354 | + // execute rename hook of versions app |
|
355 | + Filesystem::rename('/folder1/test.txt', '/folder1/folder2/test.txt'); |
|
356 | + |
|
357 | + $this->runCommands(); |
|
358 | + |
|
359 | + self::loginHelper(self::TEST_VERSIONS_USER); |
|
360 | + |
|
361 | + $this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist'); |
|
362 | + $this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist'); |
|
363 | + |
|
364 | + $this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists'); |
|
365 | + $this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists'); |
|
366 | + |
|
367 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
368 | + } |
|
369 | + |
|
370 | + public function testMoveFolder(): void { |
|
371 | + Filesystem::mkdir('folder1'); |
|
372 | + Filesystem::mkdir('folder2'); |
|
373 | + Filesystem::file_put_contents('folder1/test.txt', 'test file'); |
|
374 | + |
|
375 | + $t1 = time(); |
|
376 | + // second version is two weeks older, this way we make sure that no |
|
377 | + // version will be expired |
|
378 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
379 | + |
|
380 | + // create some versions |
|
381 | + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1'); |
|
382 | + $v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1; |
|
383 | + $v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2; |
|
384 | + $v1Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t1; |
|
385 | + $v2Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t2; |
|
386 | 386 | |
387 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
388 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
387 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
388 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
389 | 389 | |
390 | - // execute rename hook of versions app |
|
391 | - Filesystem::rename('folder1', 'folder2/folder1'); |
|
390 | + // execute rename hook of versions app |
|
391 | + Filesystem::rename('folder1', 'folder2/folder1'); |
|
392 | 392 | |
393 | - $this->runCommands(); |
|
393 | + $this->runCommands(); |
|
394 | 394 | |
395 | - $this->assertFalse($this->rootView->file_exists($v1)); |
|
396 | - $this->assertFalse($this->rootView->file_exists($v2)); |
|
395 | + $this->assertFalse($this->rootView->file_exists($v1)); |
|
396 | + $this->assertFalse($this->rootView->file_exists($v2)); |
|
397 | 397 | |
398 | - $this->assertTrue($this->rootView->file_exists($v1Renamed)); |
|
399 | - $this->assertTrue($this->rootView->file_exists($v2Renamed)); |
|
400 | - } |
|
398 | + $this->assertTrue($this->rootView->file_exists($v1Renamed)); |
|
399 | + $this->assertTrue($this->rootView->file_exists($v2Renamed)); |
|
400 | + } |
|
401 | 401 | |
402 | 402 | |
403 | - public function testMoveFileIntoSharedFolderAsRecipient(): void { |
|
404 | - Filesystem::mkdir('folder1'); |
|
405 | - $fileInfo = Filesystem::getFileInfo('folder1'); |
|
403 | + public function testMoveFileIntoSharedFolderAsRecipient(): void { |
|
404 | + Filesystem::mkdir('folder1'); |
|
405 | + $fileInfo = Filesystem::getFileInfo('folder1'); |
|
406 | 406 | |
407 | - $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1'); |
|
408 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
409 | - $share->setNode($node) |
|
410 | - ->setShareType(IShare::TYPE_USER) |
|
411 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
412 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
413 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
414 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
415 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
407 | + $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1'); |
|
408 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
409 | + $share->setNode($node) |
|
410 | + ->setShareType(IShare::TYPE_USER) |
|
411 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
412 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
413 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
414 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
415 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
416 | 416 | |
417 | - self::loginHelper(self::TEST_VERSIONS_USER2); |
|
418 | - $versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions'; |
|
419 | - Filesystem::file_put_contents('test.txt', 'test file'); |
|
417 | + self::loginHelper(self::TEST_VERSIONS_USER2); |
|
418 | + $versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions'; |
|
419 | + Filesystem::file_put_contents('test.txt', 'test file'); |
|
420 | 420 | |
421 | - $t1 = time(); |
|
422 | - // second version is two weeks older, this way we make sure that no |
|
423 | - // version will be expired |
|
424 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
421 | + $t1 = time(); |
|
422 | + // second version is two weeks older, this way we make sure that no |
|
423 | + // version will be expired |
|
424 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
425 | 425 | |
426 | - $this->rootView->mkdir($versionsFolder2); |
|
427 | - // create some versions |
|
428 | - $v1 = $versionsFolder2 . '/test.txt.v' . $t1; |
|
429 | - $v2 = $versionsFolder2 . '/test.txt.v' . $t2; |
|
426 | + $this->rootView->mkdir($versionsFolder2); |
|
427 | + // create some versions |
|
428 | + $v1 = $versionsFolder2 . '/test.txt.v' . $t1; |
|
429 | + $v2 = $versionsFolder2 . '/test.txt.v' . $t2; |
|
430 | 430 | |
431 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
432 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
431 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
432 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
433 | 433 | |
434 | - // move file into the shared folder as recipient |
|
435 | - $success = Filesystem::rename('/test.txt', '/folder1/test.txt'); |
|
434 | + // move file into the shared folder as recipient |
|
435 | + $success = Filesystem::rename('/test.txt', '/folder1/test.txt'); |
|
436 | 436 | |
437 | - $this->assertTrue($success); |
|
438 | - $this->assertFalse($this->rootView->file_exists($v1)); |
|
439 | - $this->assertFalse($this->rootView->file_exists($v2)); |
|
437 | + $this->assertTrue($success); |
|
438 | + $this->assertFalse($this->rootView->file_exists($v1)); |
|
439 | + $this->assertFalse($this->rootView->file_exists($v2)); |
|
440 | 440 | |
441 | - self::loginHelper(self::TEST_VERSIONS_USER); |
|
441 | + self::loginHelper(self::TEST_VERSIONS_USER); |
|
442 | 442 | |
443 | - $versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions'; |
|
443 | + $versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions'; |
|
444 | 444 | |
445 | - $v1Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t1; |
|
446 | - $v2Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t2; |
|
445 | + $v1Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t1; |
|
446 | + $v2Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t2; |
|
447 | 447 | |
448 | - $this->assertTrue($this->rootView->file_exists($v1Renamed)); |
|
449 | - $this->assertTrue($this->rootView->file_exists($v2Renamed)); |
|
448 | + $this->assertTrue($this->rootView->file_exists($v1Renamed)); |
|
449 | + $this->assertTrue($this->rootView->file_exists($v2Renamed)); |
|
450 | 450 | |
451 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
452 | - } |
|
451 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
452 | + } |
|
453 | 453 | |
454 | - public function testMoveFolderIntoSharedFolderAsRecipient(): void { |
|
455 | - Filesystem::mkdir('folder1'); |
|
454 | + public function testMoveFolderIntoSharedFolderAsRecipient(): void { |
|
455 | + Filesystem::mkdir('folder1'); |
|
456 | 456 | |
457 | - $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1'); |
|
458 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
459 | - $share->setNode($node) |
|
460 | - ->setShareType(IShare::TYPE_USER) |
|
461 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
462 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
463 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
464 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
465 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
457 | + $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1'); |
|
458 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
459 | + $share->setNode($node) |
|
460 | + ->setShareType(IShare::TYPE_USER) |
|
461 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
462 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
463 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
464 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
465 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
466 | 466 | |
467 | - self::loginHelper(self::TEST_VERSIONS_USER2); |
|
468 | - $versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions'; |
|
469 | - Filesystem::mkdir('folder2'); |
|
470 | - Filesystem::file_put_contents('folder2/test.txt', 'test file'); |
|
467 | + self::loginHelper(self::TEST_VERSIONS_USER2); |
|
468 | + $versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions'; |
|
469 | + Filesystem::mkdir('folder2'); |
|
470 | + Filesystem::file_put_contents('folder2/test.txt', 'test file'); |
|
471 | 471 | |
472 | - $t1 = time(); |
|
473 | - // second version is two weeks older, this way we make sure that no |
|
474 | - // version will be expired |
|
475 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
472 | + $t1 = time(); |
|
473 | + // second version is two weeks older, this way we make sure that no |
|
474 | + // version will be expired |
|
475 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
476 | 476 | |
477 | - $this->rootView->mkdir($versionsFolder2); |
|
478 | - $this->rootView->mkdir($versionsFolder2 . '/folder2'); |
|
479 | - // create some versions |
|
480 | - $v1 = $versionsFolder2 . '/folder2/test.txt.v' . $t1; |
|
481 | - $v2 = $versionsFolder2 . '/folder2/test.txt.v' . $t2; |
|
477 | + $this->rootView->mkdir($versionsFolder2); |
|
478 | + $this->rootView->mkdir($versionsFolder2 . '/folder2'); |
|
479 | + // create some versions |
|
480 | + $v1 = $versionsFolder2 . '/folder2/test.txt.v' . $t1; |
|
481 | + $v2 = $versionsFolder2 . '/folder2/test.txt.v' . $t2; |
|
482 | 482 | |
483 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
484 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
483 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
484 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
485 | 485 | |
486 | - // move file into the shared folder as recipient |
|
487 | - Filesystem::rename('/folder2', '/folder1/folder2'); |
|
486 | + // move file into the shared folder as recipient |
|
487 | + Filesystem::rename('/folder2', '/folder1/folder2'); |
|
488 | 488 | |
489 | - $this->assertFalse($this->rootView->file_exists($v1)); |
|
490 | - $this->assertFalse($this->rootView->file_exists($v2)); |
|
489 | + $this->assertFalse($this->rootView->file_exists($v1)); |
|
490 | + $this->assertFalse($this->rootView->file_exists($v2)); |
|
491 | 491 | |
492 | - self::loginHelper(self::TEST_VERSIONS_USER); |
|
492 | + self::loginHelper(self::TEST_VERSIONS_USER); |
|
493 | 493 | |
494 | - $versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions'; |
|
494 | + $versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions'; |
|
495 | 495 | |
496 | - $v1Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t1; |
|
497 | - $v2Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t2; |
|
496 | + $v1Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t1; |
|
497 | + $v2Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t2; |
|
498 | 498 | |
499 | - $this->assertTrue($this->rootView->file_exists($v1Renamed)); |
|
500 | - $this->assertTrue($this->rootView->file_exists($v2Renamed)); |
|
499 | + $this->assertTrue($this->rootView->file_exists($v1Renamed)); |
|
500 | + $this->assertTrue($this->rootView->file_exists($v2Renamed)); |
|
501 | 501 | |
502 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
503 | - } |
|
502 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
503 | + } |
|
504 | 504 | |
505 | - public function testRenameSharedFile(): void { |
|
506 | - Filesystem::file_put_contents('test.txt', 'test file'); |
|
505 | + public function testRenameSharedFile(): void { |
|
506 | + Filesystem::file_put_contents('test.txt', 'test file'); |
|
507 | 507 | |
508 | - $t1 = time(); |
|
509 | - // second version is two weeks older, this way we make sure that no |
|
510 | - // version will be expired |
|
511 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
508 | + $t1 = time(); |
|
509 | + // second version is two weeks older, this way we make sure that no |
|
510 | + // version will be expired |
|
511 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
512 | 512 | |
513 | - $this->rootView->mkdir(self::USERS_VERSIONS_ROOT); |
|
514 | - // create some versions |
|
515 | - $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; |
|
516 | - $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; |
|
517 | - // the renamed versions should not exist! Because we only moved the mount point! |
|
518 | - $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; |
|
519 | - $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; |
|
513 | + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT); |
|
514 | + // create some versions |
|
515 | + $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; |
|
516 | + $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; |
|
517 | + // the renamed versions should not exist! Because we only moved the mount point! |
|
518 | + $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; |
|
519 | + $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; |
|
520 | 520 | |
521 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
522 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
521 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
522 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
523 | 523 | |
524 | - $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('test.txt'); |
|
525 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
526 | - $share->setNode($node) |
|
527 | - ->setShareType(IShare::TYPE_USER) |
|
528 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
529 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
530 | - ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE); |
|
531 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
532 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
524 | + $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('test.txt'); |
|
525 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
526 | + $share->setNode($node) |
|
527 | + ->setShareType(IShare::TYPE_USER) |
|
528 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
529 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
530 | + ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE); |
|
531 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
532 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
533 | 533 | |
534 | - self::loginHelper(self::TEST_VERSIONS_USER2); |
|
534 | + self::loginHelper(self::TEST_VERSIONS_USER2); |
|
535 | 535 | |
536 | - $this->assertTrue(Filesystem::file_exists('test.txt')); |
|
536 | + $this->assertTrue(Filesystem::file_exists('test.txt')); |
|
537 | 537 | |
538 | - // execute rename hook of versions app |
|
539 | - Filesystem::rename('test.txt', 'test2.txt'); |
|
538 | + // execute rename hook of versions app |
|
539 | + Filesystem::rename('test.txt', 'test2.txt'); |
|
540 | 540 | |
541 | - self::loginHelper(self::TEST_VERSIONS_USER); |
|
541 | + self::loginHelper(self::TEST_VERSIONS_USER); |
|
542 | 542 | |
543 | - $this->runCommands(); |
|
543 | + $this->runCommands(); |
|
544 | 544 | |
545 | - $this->assertTrue($this->rootView->file_exists($v1)); |
|
546 | - $this->assertTrue($this->rootView->file_exists($v2)); |
|
545 | + $this->assertTrue($this->rootView->file_exists($v1)); |
|
546 | + $this->assertTrue($this->rootView->file_exists($v2)); |
|
547 | 547 | |
548 | - $this->assertFalse($this->rootView->file_exists($v1Renamed)); |
|
549 | - $this->assertFalse($this->rootView->file_exists($v2Renamed)); |
|
548 | + $this->assertFalse($this->rootView->file_exists($v1Renamed)); |
|
549 | + $this->assertFalse($this->rootView->file_exists($v2Renamed)); |
|
550 | 550 | |
551 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
552 | - } |
|
551 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
552 | + } |
|
553 | 553 | |
554 | - public function testCopy(): void { |
|
555 | - Filesystem::file_put_contents('test.txt', 'test file'); |
|
554 | + public function testCopy(): void { |
|
555 | + Filesystem::file_put_contents('test.txt', 'test file'); |
|
556 | 556 | |
557 | - $t1 = time(); |
|
558 | - // second version is two weeks older, this way we make sure that no |
|
559 | - // version will be expired |
|
560 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
557 | + $t1 = time(); |
|
558 | + // second version is two weeks older, this way we make sure that no |
|
559 | + // version will be expired |
|
560 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
561 | 561 | |
562 | - // create some versions |
|
563 | - $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; |
|
564 | - $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; |
|
565 | - $v1Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; |
|
566 | - $v2Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; |
|
562 | + // create some versions |
|
563 | + $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; |
|
564 | + $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; |
|
565 | + $v1Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; |
|
566 | + $v2Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; |
|
567 | 567 | |
568 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
569 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
568 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
569 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
570 | 570 | |
571 | - // execute copy hook of versions app |
|
572 | - Filesystem::copy('test.txt', 'test2.txt'); |
|
571 | + // execute copy hook of versions app |
|
572 | + Filesystem::copy('test.txt', 'test2.txt'); |
|
573 | 573 | |
574 | - $this->runCommands(); |
|
574 | + $this->runCommands(); |
|
575 | 575 | |
576 | - $this->assertTrue($this->rootView->file_exists($v1), 'version 1 of original file exists'); |
|
577 | - $this->assertTrue($this->rootView->file_exists($v2), 'version 2 of original file exists'); |
|
576 | + $this->assertTrue($this->rootView->file_exists($v1), 'version 1 of original file exists'); |
|
577 | + $this->assertTrue($this->rootView->file_exists($v2), 'version 2 of original file exists'); |
|
578 | 578 | |
579 | - $this->assertTrue($this->rootView->file_exists($v1Copied), 'version 1 of copied file exists'); |
|
580 | - $this->assertTrue($this->rootView->file_exists($v2Copied), 'version 2 of copied file exists'); |
|
581 | - } |
|
579 | + $this->assertTrue($this->rootView->file_exists($v1Copied), 'version 1 of copied file exists'); |
|
580 | + $this->assertTrue($this->rootView->file_exists($v2Copied), 'version 2 of copied file exists'); |
|
581 | + } |
|
582 | 582 | |
583 | - /** |
|
584 | - * test if we find all versions and if the versions array contain |
|
585 | - * the correct 'path' and 'name' |
|
586 | - */ |
|
587 | - public function testGetVersions(): void { |
|
588 | - $t1 = time(); |
|
589 | - // second version is two weeks older, this way we make sure that no |
|
590 | - // version will be expired |
|
591 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
583 | + /** |
|
584 | + * test if we find all versions and if the versions array contain |
|
585 | + * the correct 'path' and 'name' |
|
586 | + */ |
|
587 | + public function testGetVersions(): void { |
|
588 | + $t1 = time(); |
|
589 | + // second version is two weeks older, this way we make sure that no |
|
590 | + // version will be expired |
|
591 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
592 | 592 | |
593 | - // create some versions |
|
594 | - $v1 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t1; |
|
595 | - $v2 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t2; |
|
593 | + // create some versions |
|
594 | + $v1 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t1; |
|
595 | + $v2 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t2; |
|
596 | 596 | |
597 | - $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/subfolder/'); |
|
597 | + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/subfolder/'); |
|
598 | 598 | |
599 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
600 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
599 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
600 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
601 | 601 | |
602 | - // execute copy hook of versions app |
|
603 | - $versions = Storage::getVersions(self::TEST_VERSIONS_USER, '/subfolder/test.txt'); |
|
602 | + // execute copy hook of versions app |
|
603 | + $versions = Storage::getVersions(self::TEST_VERSIONS_USER, '/subfolder/test.txt'); |
|
604 | 604 | |
605 | - $this->assertCount(2, $versions); |
|
605 | + $this->assertCount(2, $versions); |
|
606 | 606 | |
607 | - foreach ($versions as $version) { |
|
608 | - $this->assertSame('/subfolder/test.txt', $version['path']); |
|
609 | - $this->assertSame('test.txt', $version['name']); |
|
610 | - } |
|
607 | + foreach ($versions as $version) { |
|
608 | + $this->assertSame('/subfolder/test.txt', $version['path']); |
|
609 | + $this->assertSame('test.txt', $version['name']); |
|
610 | + } |
|
611 | 611 | |
612 | - //cleanup |
|
613 | - $this->rootView->deleteAll(self::USERS_VERSIONS_ROOT . '/subfolder'); |
|
614 | - } |
|
612 | + //cleanup |
|
613 | + $this->rootView->deleteAll(self::USERS_VERSIONS_ROOT . '/subfolder'); |
|
614 | + } |
|
615 | 615 | |
616 | - /** |
|
617 | - * test if we find all versions and if the versions array contain |
|
618 | - * the correct 'path' and 'name' |
|
619 | - */ |
|
620 | - public function testGetVersionsEmptyFile(): void { |
|
621 | - // execute copy hook of versions app |
|
622 | - $versions = Storage::getVersions(self::TEST_VERSIONS_USER, ''); |
|
623 | - $this->assertCount(0, $versions); |
|
616 | + /** |
|
617 | + * test if we find all versions and if the versions array contain |
|
618 | + * the correct 'path' and 'name' |
|
619 | + */ |
|
620 | + public function testGetVersionsEmptyFile(): void { |
|
621 | + // execute copy hook of versions app |
|
622 | + $versions = Storage::getVersions(self::TEST_VERSIONS_USER, ''); |
|
623 | + $this->assertCount(0, $versions); |
|
624 | 624 | |
625 | - $versions = Storage::getVersions(self::TEST_VERSIONS_USER, null); |
|
626 | - $this->assertCount(0, $versions); |
|
627 | - } |
|
625 | + $versions = Storage::getVersions(self::TEST_VERSIONS_USER, null); |
|
626 | + $this->assertCount(0, $versions); |
|
627 | + } |
|
628 | 628 | |
629 | - public function testExpireNonexistingFile(): void { |
|
630 | - $this->logout(); |
|
631 | - // needed to have a FS setup (the background job does this) |
|
632 | - \OC_Util::setupFS(self::TEST_VERSIONS_USER); |
|
629 | + public function testExpireNonexistingFile(): void { |
|
630 | + $this->logout(); |
|
631 | + // needed to have a FS setup (the background job does this) |
|
632 | + \OC_Util::setupFS(self::TEST_VERSIONS_USER); |
|
633 | 633 | |
634 | - $this->assertFalse(Storage::expire('/void/unexist.txt', self::TEST_VERSIONS_USER)); |
|
635 | - } |
|
634 | + $this->assertFalse(Storage::expire('/void/unexist.txt', self::TEST_VERSIONS_USER)); |
|
635 | + } |
|
636 | 636 | |
637 | 637 | |
638 | - public function testExpireNonexistingUser(): void { |
|
639 | - $this->expectException(NoUserException::class); |
|
638 | + public function testExpireNonexistingUser(): void { |
|
639 | + $this->expectException(NoUserException::class); |
|
640 | 640 | |
641 | - $this->logout(); |
|
642 | - // needed to have a FS setup (the background job does this) |
|
643 | - \OC_Util::setupFS(self::TEST_VERSIONS_USER); |
|
644 | - Filesystem::file_put_contents('test.txt', 'test file'); |
|
641 | + $this->logout(); |
|
642 | + // needed to have a FS setup (the background job does this) |
|
643 | + \OC_Util::setupFS(self::TEST_VERSIONS_USER); |
|
644 | + Filesystem::file_put_contents('test.txt', 'test file'); |
|
645 | 645 | |
646 | - $this->assertFalse(Storage::expire('test.txt', 'unexist')); |
|
647 | - } |
|
646 | + $this->assertFalse(Storage::expire('test.txt', 'unexist')); |
|
647 | + } |
|
648 | 648 | |
649 | - public function testRestoreSameStorage(): void { |
|
650 | - Filesystem::mkdir('sub'); |
|
651 | - $this->doTestRestore(); |
|
652 | - } |
|
649 | + public function testRestoreSameStorage(): void { |
|
650 | + Filesystem::mkdir('sub'); |
|
651 | + $this->doTestRestore(); |
|
652 | + } |
|
653 | 653 | |
654 | - public function testRestoreCrossStorage(): void { |
|
655 | - $storage2 = new Temporary([]); |
|
656 | - Filesystem::mount($storage2, [], self::TEST_VERSIONS_USER . '/files/sub'); |
|
654 | + public function testRestoreCrossStorage(): void { |
|
655 | + $storage2 = new Temporary([]); |
|
656 | + Filesystem::mount($storage2, [], self::TEST_VERSIONS_USER . '/files/sub'); |
|
657 | 657 | |
658 | - $this->doTestRestore(); |
|
659 | - } |
|
658 | + $this->doTestRestore(); |
|
659 | + } |
|
660 | 660 | |
661 | - public function testRestoreNoPermission(): void { |
|
662 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
661 | + public function testRestoreNoPermission(): void { |
|
662 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
663 | 663 | |
664 | - $userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER); |
|
665 | - $node = $userHome->newFolder('folder'); |
|
666 | - $file = $node->newFile('test.txt'); |
|
664 | + $userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER); |
|
665 | + $node = $userHome->newFolder('folder'); |
|
666 | + $file = $node->newFile('test.txt'); |
|
667 | 667 | |
668 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
669 | - $share->setNode($node) |
|
670 | - ->setShareType(IShare::TYPE_USER) |
|
671 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
672 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
673 | - ->setPermissions(Constants::PERMISSION_READ); |
|
674 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
675 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
676 | - |
|
677 | - $versions = $this->createAndCheckVersions( |
|
678 | - Filesystem::getView(), |
|
679 | - 'folder/test.txt' |
|
680 | - ); |
|
681 | - |
|
682 | - $file->putContent('test file'); |
|
683 | - |
|
684 | - $this->loginAsUser(self::TEST_VERSIONS_USER2); |
|
668 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
669 | + $share->setNode($node) |
|
670 | + ->setShareType(IShare::TYPE_USER) |
|
671 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
672 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
673 | + ->setPermissions(Constants::PERMISSION_READ); |
|
674 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
675 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
676 | + |
|
677 | + $versions = $this->createAndCheckVersions( |
|
678 | + Filesystem::getView(), |
|
679 | + 'folder/test.txt' |
|
680 | + ); |
|
681 | + |
|
682 | + $file->putContent('test file'); |
|
683 | + |
|
684 | + $this->loginAsUser(self::TEST_VERSIONS_USER2); |
|
685 | 685 | |
686 | - $firstVersion = current($versions); |
|
686 | + $firstVersion = current($versions); |
|
687 | 687 | |
688 | - $this->assertFalse(Storage::rollback('folder/test.txt', (int)$firstVersion['version'], $this->user2), 'Revert did not happen'); |
|
688 | + $this->assertFalse(Storage::rollback('folder/test.txt', (int)$firstVersion['version'], $this->user2), 'Revert did not happen'); |
|
689 | 689 | |
690 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
690 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
691 | 691 | |
692 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
693 | - $this->assertEquals('test file', $file->getContent(), 'File content has not changed'); |
|
694 | - } |
|
692 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
693 | + $this->assertEquals('test file', $file->getContent(), 'File content has not changed'); |
|
694 | + } |
|
695 | 695 | |
696 | - public function testRestoreMovedShare(): void { |
|
697 | - $this->markTestSkipped('Unreliable test'); |
|
698 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
699 | - |
|
700 | - $userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER); |
|
701 | - $node = $userHome->newFolder('folder'); |
|
702 | - $file = $node->newFile('test.txt'); |
|
703 | - |
|
704 | - $userHome2 = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER2); |
|
705 | - $userHome2->newFolder('subfolder'); |
|
706 | - |
|
707 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
708 | - $share->setNode($node) |
|
709 | - ->setShareType(IShare::TYPE_USER) |
|
710 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
711 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
712 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
713 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
714 | - $shareManager = Server::get(\OCP\Share\IManager::class); |
|
715 | - $shareManager->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
716 | - |
|
717 | - $share->setTarget('subfolder/folder'); |
|
718 | - $shareManager->moveShare($share, self::TEST_VERSIONS_USER2); |
|
719 | - |
|
720 | - $versions = $this->createAndCheckVersions( |
|
721 | - Filesystem::getView(), |
|
722 | - 'folder/test.txt' |
|
723 | - ); |
|
724 | - |
|
725 | - $file->putContent('test file'); |
|
726 | - |
|
727 | - $this->loginAsUser(self::TEST_VERSIONS_USER2); |
|
728 | - |
|
729 | - $firstVersion = current($versions); |
|
730 | - |
|
731 | - $this->assertTrue(Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user1)); |
|
732 | - |
|
733 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
734 | - |
|
735 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
736 | - $this->assertEquals('version 2', $file->getContent(), 'File content has not changed'); |
|
737 | - } |
|
738 | - |
|
739 | - /** |
|
740 | - * @param string $hookName name of hook called |
|
741 | - * @param string $params variable to receive parameters provided by hook |
|
742 | - */ |
|
743 | - private function connectMockHooks($hookName, &$params) { |
|
744 | - if ($hookName === null) { |
|
745 | - return; |
|
746 | - } |
|
747 | - |
|
748 | - $eventHandler = $this->getMockBuilder(DummyHookListener::class) |
|
749 | - ->onlyMethods(['callback']) |
|
750 | - ->getMock(); |
|
751 | - |
|
752 | - $eventHandler->expects($this->any()) |
|
753 | - ->method('callback') |
|
754 | - ->willReturnCallback( |
|
755 | - function ($p) use (&$params): void { |
|
756 | - $params = $p; |
|
757 | - } |
|
758 | - ); |
|
759 | - |
|
760 | - Util::connectHook( |
|
761 | - '\OCP\Versions', |
|
762 | - $hookName, |
|
763 | - $eventHandler, |
|
764 | - 'callback' |
|
765 | - ); |
|
766 | - } |
|
767 | - |
|
768 | - private function doTestRestore(): void { |
|
769 | - $filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt'; |
|
770 | - $this->rootView->file_put_contents($filePath, 'test file'); |
|
771 | - |
|
772 | - $fileInfo = $this->rootView->getFileInfo($filePath); |
|
773 | - $t0 = $this->rootView->filemtime($filePath); |
|
774 | - |
|
775 | - // not exactly the same timestamp as the file |
|
776 | - $t1 = time() - 60; |
|
777 | - // second version is two weeks older |
|
778 | - $t2 = $t1 - 60 * 60 * 24 * 14; |
|
779 | - |
|
780 | - // create some versions |
|
781 | - $v1 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t1; |
|
782 | - $v2 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t2; |
|
783 | - |
|
784 | - $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/sub'); |
|
785 | - |
|
786 | - $this->rootView->file_put_contents($v1, 'version1'); |
|
787 | - $fileInfoV1 = $this->rootView->getFileInfo($v1); |
|
788 | - $versionEntity = new VersionEntity(); |
|
789 | - $versionEntity->setFileId($fileInfo->getId()); |
|
790 | - $versionEntity->setTimestamp($t1); |
|
791 | - $versionEntity->setSize($fileInfoV1->getSize()); |
|
792 | - $versionEntity->setMimetype($this->mimeTypeLoader->getId($fileInfoV1->getMimetype())); |
|
793 | - $versionEntity->setMetadata([]); |
|
794 | - $this->versionsMapper->insert($versionEntity); |
|
795 | - |
|
796 | - $this->rootView->file_put_contents($v2, 'version2'); |
|
797 | - $fileInfoV2 = $this->rootView->getFileInfo($v2); |
|
798 | - $versionEntity = new VersionEntity(); |
|
799 | - $versionEntity->setFileId($fileInfo->getId()); |
|
800 | - $versionEntity->setTimestamp($t2); |
|
801 | - $versionEntity->setSize($fileInfoV2->getSize()); |
|
802 | - $versionEntity->setMimetype($this->mimeTypeLoader->getId($fileInfoV2->getMimetype())); |
|
803 | - $versionEntity->setMetadata([]); |
|
804 | - $this->versionsMapper->insert($versionEntity); |
|
805 | - |
|
806 | - $oldVersions = Storage::getVersions( |
|
807 | - self::TEST_VERSIONS_USER, '/sub/test.txt' |
|
808 | - ); |
|
809 | - |
|
810 | - $this->assertCount(2, $oldVersions); |
|
811 | - |
|
812 | - $this->assertEquals('test file', $this->rootView->file_get_contents($filePath)); |
|
813 | - $info1 = $this->rootView->getFileInfo($filePath); |
|
814 | - |
|
815 | - $eventDispatcher = Server::get(IEventDispatcher::class); |
|
816 | - $eventFired = false; |
|
817 | - $eventDispatcher->addListener(VersionRestoredEvent::class, function ($event) use (&$eventFired, $t2): void { |
|
818 | - $eventFired = true; |
|
819 | - $this->assertEquals('/sub/test.txt', $event->getVersion()->getVersionPath()); |
|
820 | - $this->assertTrue($event->getVersion()->getRevisionId() > 0); |
|
821 | - }); |
|
822 | - |
|
823 | - $versionManager = Server::get(IVersionManager::class); |
|
824 | - $versions = $versionManager->getVersionsForFile($this->user1, $info1); |
|
825 | - $version = array_filter($versions, function ($version) use ($t2) { |
|
826 | - return $version->getRevisionId() === $t2; |
|
827 | - }); |
|
828 | - $this->assertTrue($versionManager->rollback(current($version))); |
|
829 | - |
|
830 | - $this->assertTrue($eventFired, 'VersionRestoredEvent was not fired'); |
|
831 | - |
|
832 | - $this->assertEquals('version2', $this->rootView->file_get_contents($filePath)); |
|
833 | - $info2 = $this->rootView->getFileInfo($filePath); |
|
834 | - |
|
835 | - $this->assertNotEquals( |
|
836 | - $info2['etag'], |
|
837 | - $info1['etag'], |
|
838 | - 'Etag must change after rolling back version' |
|
839 | - ); |
|
840 | - $this->assertEquals( |
|
841 | - $info2['fileid'], |
|
842 | - $info1['fileid'], |
|
843 | - 'File id must not change after rolling back version' |
|
844 | - ); |
|
845 | - $this->assertEquals( |
|
846 | - $info2['mtime'], |
|
847 | - $t2, |
|
848 | - 'Restored file has mtime from version' |
|
849 | - ); |
|
850 | - |
|
851 | - $newVersions = Storage::getVersions( |
|
852 | - self::TEST_VERSIONS_USER, '/sub/test.txt' |
|
853 | - ); |
|
854 | - |
|
855 | - $this->assertTrue( |
|
856 | - $this->rootView->file_exists(self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t0), |
|
857 | - 'A version file was created for the file before restoration' |
|
858 | - ); |
|
859 | - $this->assertTrue( |
|
860 | - $this->rootView->file_exists($v1), |
|
861 | - 'Untouched version file is still there' |
|
862 | - ); |
|
863 | - $this->assertFalse( |
|
864 | - $this->rootView->file_exists($v2), |
|
865 | - 'Restored version file gone from files_version folder' |
|
866 | - ); |
|
867 | - |
|
868 | - $this->assertCount(2, $newVersions, 'Additional version created'); |
|
869 | - |
|
870 | - $this->assertTrue( |
|
871 | - isset($newVersions[$t0 . '#' . 'test.txt']), |
|
872 | - 'A version was created for the file before restoration' |
|
873 | - ); |
|
874 | - $this->assertTrue( |
|
875 | - isset($newVersions[$t1 . '#' . 'test.txt']), |
|
876 | - 'Untouched version is still there' |
|
877 | - ); |
|
878 | - $this->assertFalse( |
|
879 | - isset($newVersions[$t2 . '#' . 'test.txt']), |
|
880 | - 'Restored version is not in the list any more' |
|
881 | - ); |
|
882 | - } |
|
883 | - |
|
884 | - /** |
|
885 | - * Test whether versions are created when overwriting as owner |
|
886 | - */ |
|
887 | - public function testStoreVersionAsOwner(): void { |
|
888 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
889 | - |
|
890 | - $this->createAndCheckVersions( |
|
891 | - Filesystem::getView(), |
|
892 | - 'test.txt' |
|
893 | - ); |
|
894 | - } |
|
895 | - |
|
896 | - /** |
|
897 | - * Test whether versions are created when overwriting as share recipient |
|
898 | - */ |
|
899 | - public function testStoreVersionAsRecipient(): void { |
|
900 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
901 | - |
|
902 | - Filesystem::mkdir('folder'); |
|
903 | - Filesystem::file_put_contents('folder/test.txt', 'test file'); |
|
904 | - |
|
905 | - $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder'); |
|
906 | - $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
907 | - $share->setNode($node) |
|
908 | - ->setShareType(IShare::TYPE_USER) |
|
909 | - ->setSharedBy(self::TEST_VERSIONS_USER) |
|
910 | - ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
911 | - ->setPermissions(Constants::PERMISSION_ALL); |
|
912 | - $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
913 | - Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
914 | - |
|
915 | - $this->loginAsUser(self::TEST_VERSIONS_USER2); |
|
916 | - |
|
917 | - $this->createAndCheckVersions( |
|
918 | - Filesystem::getView(), |
|
919 | - 'folder/test.txt' |
|
920 | - ); |
|
921 | - |
|
922 | - Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
923 | - } |
|
924 | - |
|
925 | - /** |
|
926 | - * Test whether versions are created when overwriting anonymously. |
|
927 | - * |
|
928 | - * When uploading through a public link or publicwebdav, no user |
|
929 | - * is logged in. File modification must still be able to find |
|
930 | - * the owner and create versions. |
|
931 | - */ |
|
932 | - public function testStoreVersionAsAnonymous(): void { |
|
933 | - $this->logout(); |
|
934 | - |
|
935 | - // note: public link upload does this, |
|
936 | - // needed to make the hooks fire |
|
937 | - \OC_Util::setupFS(self::TEST_VERSIONS_USER); |
|
938 | - |
|
939 | - $userView = new View('/' . self::TEST_VERSIONS_USER . '/files'); |
|
940 | - $this->createAndCheckVersions( |
|
941 | - $userView, |
|
942 | - 'test.txt' |
|
943 | - ); |
|
944 | - } |
|
945 | - |
|
946 | - private function createAndCheckVersions(View $view, string $path): array { |
|
947 | - $view->file_put_contents($path, 'test file'); |
|
948 | - $view->file_put_contents($path, 'version 1'); |
|
949 | - $view->file_put_contents($path, 'version 2'); |
|
950 | - |
|
951 | - $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
952 | - |
|
953 | - // need to scan for the versions |
|
954 | - [$rootStorage,] = $this->rootView->resolvePath(self::TEST_VERSIONS_USER . '/files_versions'); |
|
955 | - $rootStorage->getScanner()->scan('files_versions'); |
|
956 | - |
|
957 | - $versions = Storage::getVersions( |
|
958 | - self::TEST_VERSIONS_USER, '/' . $path |
|
959 | - ); |
|
960 | - |
|
961 | - // note: we cannot predict how many versions are created due to |
|
962 | - // test run timing |
|
963 | - $this->assertGreaterThan(0, count($versions)); |
|
964 | - |
|
965 | - return $versions; |
|
966 | - } |
|
967 | - |
|
968 | - public static function loginHelper(string $user, bool $create = false) { |
|
969 | - if ($create) { |
|
970 | - $backend = new \Test\Util\User\Dummy(); |
|
971 | - $backend->createUser($user, $user); |
|
972 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
973 | - } |
|
974 | - |
|
975 | - \OC_Util::tearDownFS(); |
|
976 | - \OC_User::setUserId(''); |
|
977 | - Filesystem::tearDown(); |
|
978 | - \OC_User::setUserId($user); |
|
979 | - \OC_Util::setupFS($user); |
|
980 | - \OC::$server->getUserFolder($user); |
|
981 | - } |
|
696 | + public function testRestoreMovedShare(): void { |
|
697 | + $this->markTestSkipped('Unreliable test'); |
|
698 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
699 | + |
|
700 | + $userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER); |
|
701 | + $node = $userHome->newFolder('folder'); |
|
702 | + $file = $node->newFile('test.txt'); |
|
703 | + |
|
704 | + $userHome2 = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER2); |
|
705 | + $userHome2->newFolder('subfolder'); |
|
706 | + |
|
707 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
708 | + $share->setNode($node) |
|
709 | + ->setShareType(IShare::TYPE_USER) |
|
710 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
711 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
712 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
713 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
714 | + $shareManager = Server::get(\OCP\Share\IManager::class); |
|
715 | + $shareManager->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
716 | + |
|
717 | + $share->setTarget('subfolder/folder'); |
|
718 | + $shareManager->moveShare($share, self::TEST_VERSIONS_USER2); |
|
719 | + |
|
720 | + $versions = $this->createAndCheckVersions( |
|
721 | + Filesystem::getView(), |
|
722 | + 'folder/test.txt' |
|
723 | + ); |
|
724 | + |
|
725 | + $file->putContent('test file'); |
|
726 | + |
|
727 | + $this->loginAsUser(self::TEST_VERSIONS_USER2); |
|
728 | + |
|
729 | + $firstVersion = current($versions); |
|
730 | + |
|
731 | + $this->assertTrue(Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user1)); |
|
732 | + |
|
733 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
734 | + |
|
735 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
736 | + $this->assertEquals('version 2', $file->getContent(), 'File content has not changed'); |
|
737 | + } |
|
738 | + |
|
739 | + /** |
|
740 | + * @param string $hookName name of hook called |
|
741 | + * @param string $params variable to receive parameters provided by hook |
|
742 | + */ |
|
743 | + private function connectMockHooks($hookName, &$params) { |
|
744 | + if ($hookName === null) { |
|
745 | + return; |
|
746 | + } |
|
747 | + |
|
748 | + $eventHandler = $this->getMockBuilder(DummyHookListener::class) |
|
749 | + ->onlyMethods(['callback']) |
|
750 | + ->getMock(); |
|
751 | + |
|
752 | + $eventHandler->expects($this->any()) |
|
753 | + ->method('callback') |
|
754 | + ->willReturnCallback( |
|
755 | + function ($p) use (&$params): void { |
|
756 | + $params = $p; |
|
757 | + } |
|
758 | + ); |
|
759 | + |
|
760 | + Util::connectHook( |
|
761 | + '\OCP\Versions', |
|
762 | + $hookName, |
|
763 | + $eventHandler, |
|
764 | + 'callback' |
|
765 | + ); |
|
766 | + } |
|
767 | + |
|
768 | + private function doTestRestore(): void { |
|
769 | + $filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt'; |
|
770 | + $this->rootView->file_put_contents($filePath, 'test file'); |
|
771 | + |
|
772 | + $fileInfo = $this->rootView->getFileInfo($filePath); |
|
773 | + $t0 = $this->rootView->filemtime($filePath); |
|
774 | + |
|
775 | + // not exactly the same timestamp as the file |
|
776 | + $t1 = time() - 60; |
|
777 | + // second version is two weeks older |
|
778 | + $t2 = $t1 - 60 * 60 * 24 * 14; |
|
779 | + |
|
780 | + // create some versions |
|
781 | + $v1 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t1; |
|
782 | + $v2 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t2; |
|
783 | + |
|
784 | + $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/sub'); |
|
785 | + |
|
786 | + $this->rootView->file_put_contents($v1, 'version1'); |
|
787 | + $fileInfoV1 = $this->rootView->getFileInfo($v1); |
|
788 | + $versionEntity = new VersionEntity(); |
|
789 | + $versionEntity->setFileId($fileInfo->getId()); |
|
790 | + $versionEntity->setTimestamp($t1); |
|
791 | + $versionEntity->setSize($fileInfoV1->getSize()); |
|
792 | + $versionEntity->setMimetype($this->mimeTypeLoader->getId($fileInfoV1->getMimetype())); |
|
793 | + $versionEntity->setMetadata([]); |
|
794 | + $this->versionsMapper->insert($versionEntity); |
|
795 | + |
|
796 | + $this->rootView->file_put_contents($v2, 'version2'); |
|
797 | + $fileInfoV2 = $this->rootView->getFileInfo($v2); |
|
798 | + $versionEntity = new VersionEntity(); |
|
799 | + $versionEntity->setFileId($fileInfo->getId()); |
|
800 | + $versionEntity->setTimestamp($t2); |
|
801 | + $versionEntity->setSize($fileInfoV2->getSize()); |
|
802 | + $versionEntity->setMimetype($this->mimeTypeLoader->getId($fileInfoV2->getMimetype())); |
|
803 | + $versionEntity->setMetadata([]); |
|
804 | + $this->versionsMapper->insert($versionEntity); |
|
805 | + |
|
806 | + $oldVersions = Storage::getVersions( |
|
807 | + self::TEST_VERSIONS_USER, '/sub/test.txt' |
|
808 | + ); |
|
809 | + |
|
810 | + $this->assertCount(2, $oldVersions); |
|
811 | + |
|
812 | + $this->assertEquals('test file', $this->rootView->file_get_contents($filePath)); |
|
813 | + $info1 = $this->rootView->getFileInfo($filePath); |
|
814 | + |
|
815 | + $eventDispatcher = Server::get(IEventDispatcher::class); |
|
816 | + $eventFired = false; |
|
817 | + $eventDispatcher->addListener(VersionRestoredEvent::class, function ($event) use (&$eventFired, $t2): void { |
|
818 | + $eventFired = true; |
|
819 | + $this->assertEquals('/sub/test.txt', $event->getVersion()->getVersionPath()); |
|
820 | + $this->assertTrue($event->getVersion()->getRevisionId() > 0); |
|
821 | + }); |
|
822 | + |
|
823 | + $versionManager = Server::get(IVersionManager::class); |
|
824 | + $versions = $versionManager->getVersionsForFile($this->user1, $info1); |
|
825 | + $version = array_filter($versions, function ($version) use ($t2) { |
|
826 | + return $version->getRevisionId() === $t2; |
|
827 | + }); |
|
828 | + $this->assertTrue($versionManager->rollback(current($version))); |
|
829 | + |
|
830 | + $this->assertTrue($eventFired, 'VersionRestoredEvent was not fired'); |
|
831 | + |
|
832 | + $this->assertEquals('version2', $this->rootView->file_get_contents($filePath)); |
|
833 | + $info2 = $this->rootView->getFileInfo($filePath); |
|
834 | + |
|
835 | + $this->assertNotEquals( |
|
836 | + $info2['etag'], |
|
837 | + $info1['etag'], |
|
838 | + 'Etag must change after rolling back version' |
|
839 | + ); |
|
840 | + $this->assertEquals( |
|
841 | + $info2['fileid'], |
|
842 | + $info1['fileid'], |
|
843 | + 'File id must not change after rolling back version' |
|
844 | + ); |
|
845 | + $this->assertEquals( |
|
846 | + $info2['mtime'], |
|
847 | + $t2, |
|
848 | + 'Restored file has mtime from version' |
|
849 | + ); |
|
850 | + |
|
851 | + $newVersions = Storage::getVersions( |
|
852 | + self::TEST_VERSIONS_USER, '/sub/test.txt' |
|
853 | + ); |
|
854 | + |
|
855 | + $this->assertTrue( |
|
856 | + $this->rootView->file_exists(self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t0), |
|
857 | + 'A version file was created for the file before restoration' |
|
858 | + ); |
|
859 | + $this->assertTrue( |
|
860 | + $this->rootView->file_exists($v1), |
|
861 | + 'Untouched version file is still there' |
|
862 | + ); |
|
863 | + $this->assertFalse( |
|
864 | + $this->rootView->file_exists($v2), |
|
865 | + 'Restored version file gone from files_version folder' |
|
866 | + ); |
|
867 | + |
|
868 | + $this->assertCount(2, $newVersions, 'Additional version created'); |
|
869 | + |
|
870 | + $this->assertTrue( |
|
871 | + isset($newVersions[$t0 . '#' . 'test.txt']), |
|
872 | + 'A version was created for the file before restoration' |
|
873 | + ); |
|
874 | + $this->assertTrue( |
|
875 | + isset($newVersions[$t1 . '#' . 'test.txt']), |
|
876 | + 'Untouched version is still there' |
|
877 | + ); |
|
878 | + $this->assertFalse( |
|
879 | + isset($newVersions[$t2 . '#' . 'test.txt']), |
|
880 | + 'Restored version is not in the list any more' |
|
881 | + ); |
|
882 | + } |
|
883 | + |
|
884 | + /** |
|
885 | + * Test whether versions are created when overwriting as owner |
|
886 | + */ |
|
887 | + public function testStoreVersionAsOwner(): void { |
|
888 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
889 | + |
|
890 | + $this->createAndCheckVersions( |
|
891 | + Filesystem::getView(), |
|
892 | + 'test.txt' |
|
893 | + ); |
|
894 | + } |
|
895 | + |
|
896 | + /** |
|
897 | + * Test whether versions are created when overwriting as share recipient |
|
898 | + */ |
|
899 | + public function testStoreVersionAsRecipient(): void { |
|
900 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
901 | + |
|
902 | + Filesystem::mkdir('folder'); |
|
903 | + Filesystem::file_put_contents('folder/test.txt', 'test file'); |
|
904 | + |
|
905 | + $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder'); |
|
906 | + $share = Server::get(\OCP\Share\IManager::class)->newShare(); |
|
907 | + $share->setNode($node) |
|
908 | + ->setShareType(IShare::TYPE_USER) |
|
909 | + ->setSharedBy(self::TEST_VERSIONS_USER) |
|
910 | + ->setSharedWith(self::TEST_VERSIONS_USER2) |
|
911 | + ->setPermissions(Constants::PERMISSION_ALL); |
|
912 | + $share = Server::get(\OCP\Share\IManager::class)->createShare($share); |
|
913 | + Server::get(\OCP\Share\IManager::class)->acceptShare($share, self::TEST_VERSIONS_USER2); |
|
914 | + |
|
915 | + $this->loginAsUser(self::TEST_VERSIONS_USER2); |
|
916 | + |
|
917 | + $this->createAndCheckVersions( |
|
918 | + Filesystem::getView(), |
|
919 | + 'folder/test.txt' |
|
920 | + ); |
|
921 | + |
|
922 | + Server::get(\OCP\Share\IManager::class)->deleteShare($share); |
|
923 | + } |
|
924 | + |
|
925 | + /** |
|
926 | + * Test whether versions are created when overwriting anonymously. |
|
927 | + * |
|
928 | + * When uploading through a public link or publicwebdav, no user |
|
929 | + * is logged in. File modification must still be able to find |
|
930 | + * the owner and create versions. |
|
931 | + */ |
|
932 | + public function testStoreVersionAsAnonymous(): void { |
|
933 | + $this->logout(); |
|
934 | + |
|
935 | + // note: public link upload does this, |
|
936 | + // needed to make the hooks fire |
|
937 | + \OC_Util::setupFS(self::TEST_VERSIONS_USER); |
|
938 | + |
|
939 | + $userView = new View('/' . self::TEST_VERSIONS_USER . '/files'); |
|
940 | + $this->createAndCheckVersions( |
|
941 | + $userView, |
|
942 | + 'test.txt' |
|
943 | + ); |
|
944 | + } |
|
945 | + |
|
946 | + private function createAndCheckVersions(View $view, string $path): array { |
|
947 | + $view->file_put_contents($path, 'test file'); |
|
948 | + $view->file_put_contents($path, 'version 1'); |
|
949 | + $view->file_put_contents($path, 'version 2'); |
|
950 | + |
|
951 | + $this->loginAsUser(self::TEST_VERSIONS_USER); |
|
952 | + |
|
953 | + // need to scan for the versions |
|
954 | + [$rootStorage,] = $this->rootView->resolvePath(self::TEST_VERSIONS_USER . '/files_versions'); |
|
955 | + $rootStorage->getScanner()->scan('files_versions'); |
|
956 | + |
|
957 | + $versions = Storage::getVersions( |
|
958 | + self::TEST_VERSIONS_USER, '/' . $path |
|
959 | + ); |
|
960 | + |
|
961 | + // note: we cannot predict how many versions are created due to |
|
962 | + // test run timing |
|
963 | + $this->assertGreaterThan(0, count($versions)); |
|
964 | + |
|
965 | + return $versions; |
|
966 | + } |
|
967 | + |
|
968 | + public static function loginHelper(string $user, bool $create = false) { |
|
969 | + if ($create) { |
|
970 | + $backend = new \Test\Util\User\Dummy(); |
|
971 | + $backend->createUser($user, $user); |
|
972 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
973 | + } |
|
974 | + |
|
975 | + \OC_Util::tearDownFS(); |
|
976 | + \OC_User::setUserId(''); |
|
977 | + Filesystem::tearDown(); |
|
978 | + \OC_User::setUserId($user); |
|
979 | + \OC_Util::setupFS($user); |
|
980 | + \OC::$server->getUserFolder($user); |
|
981 | + } |
|
982 | 982 | } |
983 | 983 | |
984 | 984 | class DummyHookListener { |
985 | - public function callback() { |
|
986 | - } |
|
985 | + public function callback() { |
|
986 | + } |
|
987 | 987 | } |
988 | 988 | |
989 | 989 | // extend the original class to make it possible to test protected methods |
990 | 990 | class VersionStorageToTest extends Storage { |
991 | 991 | |
992 | - /** |
|
993 | - * @param integer $time |
|
994 | - */ |
|
995 | - public function callProtectedGetExpireList($time, $versions) { |
|
996 | - return self::getExpireList($time, $versions); |
|
997 | - } |
|
992 | + /** |
|
993 | + * @param integer $time |
|
994 | + */ |
|
995 | + public function callProtectedGetExpireList($time, $versions) { |
|
996 | + return self::getExpireList($time, $versions); |
|
997 | + } |
|
998 | 998 | } |