1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
|
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Björn Schießle <bjoern@schiessle.org>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Joas Schilling <coding@schilljs.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Stefan Weil <sw@weilnetz.de>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Versions\Tests;
require_once __DIR__ . '/../appinfo/app.php';
use OC\Files\Storage\Temporary;
use OCP\IConfig;
use OCP\IUser;
/**
* Class Test_Files_versions
* this class provide basic files versions test
*
* @group DB
*/
class VersioningTest extends \Test\TestCase {
const TEST_VERSIONS_USER = 'test-versions-user';
const TEST_VERSIONS_USER2 = 'test-versions-user2';
const USERS_VERSIONS_ROOT = '/test-versions-user/files_versions';
/**
* @var \OC\Files\View
*/
private $rootView;
private $user1;
private $user2;
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
$application = new \OCA\Files_Sharing\AppInfo\Application();
// create test user
self::loginHelper(self::TEST_VERSIONS_USER2, true);
self::loginHelper(self::TEST_VERSIONS_USER, true);
}
public static function tearDownAfterClass(): void {
// cleanup test user
$user = \OC::$server->getUserManager()->get(self::TEST_VERSIONS_USER);
if ($user !== null) { $user->delete(); }
$user = \OC::$server->getUserManager()->get(self::TEST_VERSIONS_USER2);
if ($user !== null) { $user->delete(); }
parent::tearDownAfterClass();
}
protected function setUp(): void {
parent::setUp();
$config = \OC::$server->getConfig();
$mockConfig = $this->createMock(IConfig::class);
$mockConfig->expects($this->any())
->method('getSystemValue')
->will($this->returnCallback(function ($key, $default) use ($config) {
if ($key === 'filesystem_check_changes') {
return \OC\Files\Cache\Watcher::CHECK_ONCE;
} else {
return $config->getSystemValue($key, $default);
}
}));
$this->overwriteService(\OC\AllConfig::class, $mockConfig);
// clear hooks
\OC_Hook::clear();
\OC::registerShareHooks();
\OCA\Files_Versions\Hooks::connectHooks();
self::loginHelper(self::TEST_VERSIONS_USER);
$this->rootView = new \OC\Files\View();
if (!$this->rootView->file_exists(self::USERS_VERSIONS_ROOT)) {
$this->rootView->mkdir(self::USERS_VERSIONS_ROOT);
}
$this->user1 = $this->createMock(IUser::class);
$this->user1->method('getUID')
->willReturn(self::TEST_VERSIONS_USER);
$this->user2 = $this->createMock(IUser::class);
$this->user2->method('getUID')
->willReturn(self::TEST_VERSIONS_USER2);
}
protected function tearDown(): void {
$this->restoreService(\OC\AllConfig::class);
if ($this->rootView) {
$this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/');
$this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/');
$this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files_versions/');
$this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files_versions/');
}
\OC_Hook::clear();
parent::tearDown();
}
/**
* @medium
* test expire logic
* @dataProvider versionsProvider
*/
public function testGetExpireList($versions, $sizeOfAllDeletedFiles) {
// last interval end at 2592000
$startTime = 5000000;
$testClass = new VersionStorageToTest();
[$deleted, $size] = $testClass->callProtectedGetExpireList($startTime, $versions);
// we should have deleted 16 files each of the size 1
$this->assertEquals($sizeOfAllDeletedFiles, $size);
// the deleted array should only contain versions which should be deleted
foreach($deleted as $key => $path) {
unset($versions[$key]);
$this->assertEquals("delete", substr($path, 0, strlen("delete")));
}
// the versions array should only contain versions which should be kept
foreach ($versions as $version) {
$this->assertEquals("keep", $version['path']);
}
}
public function versionsProvider() {
return array(
// first set of versions uniformly distributed versions
array(
array(
// first slice (10sec) keep one version every 2 seconds
array("version" => 4999999, "path" => "keep", "size" => 1),
array("version" => 4999998, "path" => "delete", "size" => 1),
array("version" => 4999997, "path" => "keep", "size" => 1),
array("version" => 4999995, "path" => "keep", "size" => 1),
array("version" => 4999994, "path" => "delete", "size" => 1),
//next slice (60sec) starts at 4999990 keep one version every 10 secons
array("version" => 4999988, "path" => "keep", "size" => 1),
array("version" => 4999978, "path" => "keep", "size" => 1),
array("version" => 4999975, "path" => "delete", "size" => 1),
array("version" => 4999972, "path" => "delete", "size" => 1),
array("version" => 4999967, "path" => "keep", "size" => 1),
array("version" => 4999958, "path" => "delete", "size" => 1),
array("version" => 4999957, "path" => "keep", "size" => 1),
//next slice (3600sec) start at 4999940 keep one version every 60 seconds
array("version" => 4999900, "path" => "keep", "size" => 1),
array("version" => 4999841, "path" => "delete", "size" => 1),
array("version" => 4999840, "path" => "keep", "size" => 1),
array("version" => 4999780, "path" => "keep", "size" => 1),
array("version" => 4996401, "path" => "keep", "size" => 1),
// next slice (86400sec) start at 4996400 keep one version every 3600 seconds
array("version" => 4996350, "path" => "delete", "size" => 1),
array("version" => 4992800, "path" => "keep", "size" => 1),
array("version" => 4989800, "path" => "delete", "size" => 1),
array("version" => 4989700, "path" => "delete", "size" => 1),
array("version" => 4989200, "path" => "keep", "size" => 1),
// next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
array("version" => 4913600, "path" => "keep", "size" => 1),
array("version" => 4852800, "path" => "delete", "size" => 1),
array("version" => 4827201, "path" => "delete", "size" => 1),
array("version" => 4827200, "path" => "keep", "size" => 1),
array("version" => 4777201, "path" => "delete", "size" => 1),
array("version" => 4777501, "path" => "delete", "size" => 1),
array("version" => 4740000, "path" => "keep", "size" => 1),
// final slice starts at 2408000 keep one version every 604800 secons
array("version" => 2408000, "path" => "keep", "size" => 1),
array("version" => 1803201, "path" => "delete", "size" => 1),
array("version" => 1803200, "path" => "keep", "size" => 1),
array("version" => 1800199, "path" => "delete", "size" => 1),
array("version" => 1800100, "path" => "delete", "size" => 1),
array("version" => 1198300, "path" => "keep", "size" => 1),
),
16 // size of all deleted files (every file has the size 1)
),
// second set of versions, here we have only really old versions
array(
array(
// first slice (10sec) keep one version every 2 seconds
// next slice (60sec) starts at 4999990 keep one version every 10 secons
// next slice (3600sec) start at 4999940 keep one version every 60 seconds
// next slice (86400sec) start at 4996400 keep one version every 3600 seconds
array("version" => 4996400, "path" => "keep", "size" => 1),
array("version" => 4996350, "path" => "delete", "size" => 1),
array("version" => 4996350, "path" => "delete", "size" => 1),
array("version" => 4992800, "path" => "keep", "size" => 1),
array("version" => 4989800, "path" => "delete", "size" => 1),
array("version" => 4989700, "path" => "delete", "size" => 1),
array("version" => 4989200, "path" => "keep", "size" => 1),
// next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
array("version" => 4913600, "path" => "keep", "size" => 1),
array("version" => 4852800, "path" => "delete", "size" => 1),
array("version" => 4827201, "path" => "delete", "size" => 1),
array("version" => 4827200, "path" => "keep", "size" => 1),
array("version" => 4777201, "path" => "delete", "size" => 1),
array("version" => 4777501, "path" => "delete", "size" => 1),
array("version" => 4740000, "path" => "keep", "size" => 1),
// final slice starts at 2408000 keep one version every 604800 secons
array("version" => 2408000, "path" => "keep", "size" => 1),
array("version" => 1803201, "path" => "delete", "size" => 1),
array("version" => 1803200, "path" => "keep", "size" => 1),
array("version" => 1800199, "path" => "delete", "size" => 1),
array("version" => 1800100, "path" => "delete", "size" => 1),
array("version" => 1198300, "path" => "keep", "size" => 1),
),
11 // size of all deleted files (every file has the size 1)
),
// third set of versions, with some gaps between
array(
array(
// first slice (10sec) keep one version every 2 seconds
array("version" => 4999999, "path" => "keep", "size" => 1),
array("version" => 4999998, "path" => "delete", "size" => 1),
array("version" => 4999997, "path" => "keep", "size" => 1),
array("version" => 4999995, "path" => "keep", "size" => 1),
array("version" => 4999994, "path" => "delete", "size" => 1),
//next slice (60sec) starts at 4999990 keep one version every 10 secons
array("version" => 4999988, "path" => "keep", "size" => 1),
array("version" => 4999978, "path" => "keep", "size" => 1),
//next slice (3600sec) start at 4999940 keep one version every 60 seconds
// next slice (86400sec) start at 4996400 keep one version every 3600 seconds
array("version" => 4989200, "path" => "keep", "size" => 1),
// next slice (2592000sec) start at 4913600 keep one version every 86400 seconds
array("version" => 4913600, "path" => "keep", "size" => 1),
array("version" => 4852800, "path" => "delete", "size" => 1),
array("version" => 4827201, "path" => "delete", "size" => 1),
array("version" => 4827200, "path" => "keep", "size" => 1),
array("version" => 4777201, "path" => "delete", "size" => 1),
array("version" => 4777501, "path" => "delete", "size" => 1),
array("version" => 4740000, "path" => "keep", "size" => 1),
// final slice starts at 2408000 keep one version every 604800 secons
array("version" => 2408000, "path" => "keep", "size" => 1),
array("version" => 1803201, "path" => "delete", "size" => 1),
array("version" => 1803200, "path" => "keep", "size" => 1),
array("version" => 1800199, "path" => "delete", "size" => 1),
array("version" => 1800100, "path" => "delete", "size" => 1),
array("version" => 1198300, "path" => "keep", "size" => 1),
),
9 // size of all deleted files (every file has the size 1)
),
);
}
public function testRename() {
\OC\Files\Filesystem::file_put_contents("test.txt", "test file");
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
// create some versions
$v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
$v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
$v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
// execute rename hook of versions app
\OC\Files\Filesystem::rename("test.txt", "test2.txt");
$this->runCommands();
$this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist');
$this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist');
$this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists');
$this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists');
}
public function testRenameInSharedFolder() {
\OC\Files\Filesystem::mkdir('folder1');
\OC\Files\Filesystem::mkdir('folder1/folder2');
\OC\Files\Filesystem::file_put_contents("folder1/test.txt", "test file");
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
$this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1');
// create some versions
$v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2;
$v1Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t1;
$v2Renamed = self::USERS_VERSIONS_ROOT . '/folder1/folder2/test.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
$node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_ALL);
$share = \OC::$server->getShareManager()->createShare($share);
\OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
self::loginHelper(self::TEST_VERSIONS_USER2);
$this->assertTrue(\OC\Files\Filesystem::file_exists('folder1/test.txt'));
// execute rename hook of versions app
\OC\Files\Filesystem::rename('/folder1/test.txt', '/folder1/folder2/test.txt');
$this->runCommands();
self::loginHelper(self::TEST_VERSIONS_USER);
$this->assertFalse($this->rootView->file_exists($v1), 'version 1 of old file does not exist');
$this->assertFalse($this->rootView->file_exists($v2), 'version 2 of old file does not exist');
$this->assertTrue($this->rootView->file_exists($v1Renamed), 'version 1 of renamed file exists');
$this->assertTrue($this->rootView->file_exists($v2Renamed), 'version 2 of renamed file exists');
\OC::$server->getShareManager()->deleteShare($share);
}
public function testMoveFolder() {
\OC\Files\Filesystem::mkdir('folder1');
\OC\Files\Filesystem::mkdir('folder2');
\OC\Files\Filesystem::file_put_contents('folder1/test.txt', 'test file');
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
// create some versions
$this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/folder1');
$v1 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/folder1/test.txt.v' . $t2;
$v1Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t1;
$v2Renamed = self::USERS_VERSIONS_ROOT . '/folder2/folder1/test.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
// execute rename hook of versions app
\OC\Files\Filesystem::rename('folder1', 'folder2/folder1');
$this->runCommands();
$this->assertFalse($this->rootView->file_exists($v1));
$this->assertFalse($this->rootView->file_exists($v2));
$this->assertTrue($this->rootView->file_exists($v1Renamed));
$this->assertTrue($this->rootView->file_exists($v2Renamed));
}
public function testMoveFileIntoSharedFolderAsRecipient() {
\OC\Files\Filesystem::mkdir('folder1');
$fileInfo = \OC\Files\Filesystem::getFileInfo('folder1');
$node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_ALL);
$share = \OC::$server->getShareManager()->createShare($share);
\OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
self::loginHelper(self::TEST_VERSIONS_USER2);
$versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions';
\OC\Files\Filesystem::file_put_contents('test.txt', 'test file');
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
$this->rootView->mkdir($versionsFolder2);
// create some versions
$v1 = $versionsFolder2 . '/test.txt.v' . $t1;
$v2 = $versionsFolder2 . '/test.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
// move file into the shared folder as recipient
\OC\Files\Filesystem::rename('/test.txt', '/folder1/test.txt');
$this->assertFalse($this->rootView->file_exists($v1));
$this->assertFalse($this->rootView->file_exists($v2));
self::loginHelper(self::TEST_VERSIONS_USER);
$versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions';
$v1Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t1;
$v2Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t2;
$this->assertTrue($this->rootView->file_exists($v1Renamed));
$this->assertTrue($this->rootView->file_exists($v2Renamed));
\OC::$server->getShareManager()->deleteShare($share);
}
public function testMoveFolderIntoSharedFolderAsRecipient() {
\OC\Files\Filesystem::mkdir('folder1');
$node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder1');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_ALL);
$share = \OC::$server->getShareManager()->createShare($share);
\OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
self::loginHelper(self::TEST_VERSIONS_USER2);
$versionsFolder2 = '/' . self::TEST_VERSIONS_USER2 . '/files_versions';
\OC\Files\Filesystem::mkdir('folder2');
\OC\Files\Filesystem::file_put_contents('folder2/test.txt', 'test file');
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
$this->rootView->mkdir($versionsFolder2);
$this->rootView->mkdir($versionsFolder2 . '/folder2');
// create some versions
$v1 = $versionsFolder2 . '/folder2/test.txt.v' . $t1;
$v2 = $versionsFolder2 . '/folder2/test.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
// move file into the shared folder as recipient
\OC\Files\Filesystem::rename('/folder2', '/folder1/folder2');
$this->assertFalse($this->rootView->file_exists($v1));
$this->assertFalse($this->rootView->file_exists($v2));
self::loginHelper(self::TEST_VERSIONS_USER);
$versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions';
$v1Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t1;
$v2Renamed = $versionsFolder1 . '/folder1/folder2/test.txt.v' . $t2;
$this->assertTrue($this->rootView->file_exists($v1Renamed));
$this->assertTrue($this->rootView->file_exists($v2Renamed));
\OC::$server->getShareManager()->deleteShare($share);
}
public function testRenameSharedFile() {
\OC\Files\Filesystem::file_put_contents("test.txt", "test file");
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
$this->rootView->mkdir(self::USERS_VERSIONS_ROOT);
// create some versions
$v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
// the renamed versions should not exist! Because we only moved the mount point!
$v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
$v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
$node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('test.txt');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE);
$share = \OC::$server->getShareManager()->createShare($share);
\OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
self::loginHelper(self::TEST_VERSIONS_USER2);
$this->assertTrue(\OC\Files\Filesystem::file_exists('test.txt'));
// execute rename hook of versions app
\OC\Files\Filesystem::rename('test.txt', 'test2.txt');
self::loginHelper(self::TEST_VERSIONS_USER);
$this->runCommands();
$this->assertTrue($this->rootView->file_exists($v1));
$this->assertTrue($this->rootView->file_exists($v2));
$this->assertFalse($this->rootView->file_exists($v1Renamed));
$this->assertFalse($this->rootView->file_exists($v2Renamed));
\OC::$server->getShareManager()->deleteShare($share);
}
public function testCopy() {
\OC\Files\Filesystem::file_put_contents("test.txt", "test file");
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
// create some versions
$v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2;
$v1Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1;
$v2Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2;
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
// execute copy hook of versions app
\OC\Files\Filesystem::copy("test.txt", "test2.txt");
$this->runCommands();
$this->assertTrue($this->rootView->file_exists($v1), 'version 1 of original file exists');
$this->assertTrue($this->rootView->file_exists($v2), 'version 2 of original file exists');
$this->assertTrue($this->rootView->file_exists($v1Copied), 'version 1 of copied file exists');
$this->assertTrue($this->rootView->file_exists($v2Copied), 'version 2 of copied file exists');
}
/**
* test if we find all versions and if the versions array contain
* the correct 'path' and 'name'
*/
public function testGetVersions() {
$t1 = time();
// second version is two weeks older, this way we make sure that no
// version will be expired
$t2 = $t1 - 60 * 60 * 24 * 14;
// create some versions
$v1 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/subfolder/test.txt.v' . $t2;
$this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/subfolder/');
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
// execute copy hook of versions app
$versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, '/subfolder/test.txt');
$this->assertCount(2, $versions);
foreach ($versions as $version) {
$this->assertSame('/subfolder/test.txt', $version['path']);
$this->assertSame('test.txt', $version['name']);
}
//cleanup
$this->rootView->deleteAll(self::USERS_VERSIONS_ROOT . '/subfolder');
}
/**
* test if we find all versions and if the versions array contain
* the correct 'path' and 'name'
*/
public function testGetVersionsEmptyFile() {
// execute copy hook of versions app
$versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, '');
$this->assertCount(0, $versions);
$versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, null);
$this->assertCount(0, $versions);
}
public function testExpireNonexistingFile() {
$this->logout();
// needed to have a FS setup (the background job does this)
\OC_Util::setupFS(self::TEST_VERSIONS_USER);
$this->assertFalse(\OCA\Files_Versions\Storage::expire('/void/unexist.txt', self::TEST_VERSIONS_USER));
}
public function testExpireNonexistingUser() {
$this->expectException(\OC\User\NoUserException::class);
$this->logout();
// needed to have a FS setup (the background job does this)
\OC_Util::setupFS(self::TEST_VERSIONS_USER);
\OC\Files\Filesystem::file_put_contents("test.txt", "test file");
$this->assertFalse(\OCA\Files_Versions\Storage::expire('test.txt', 'unexist'));
}
public function testRestoreSameStorage() {
\OC\Files\Filesystem::mkdir('sub');
$this->doTestRestore();
}
public function testRestoreCrossStorage() {
$storage2 = new Temporary(array());
\OC\Files\Filesystem::mount($storage2, array(), self::TEST_VERSIONS_USER . '/files/sub');
$this->doTestRestore();
}
public function testRestoreNoPermission() {
$this->loginAsUser(self::TEST_VERSIONS_USER);
$userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER);
$node = $userHome->newFolder('folder');
$file = $node->newFile('test.txt');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_READ);
$share = \OC::$server->getShareManager()->createShare($share);
\OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
$versions = $this->createAndCheckVersions(
\OC\Files\Filesystem::getView(),
'folder/test.txt'
);
$file->putContent('test file');
$this->loginAsUser(self::TEST_VERSIONS_USER2);
$firstVersion = current($versions);
$this->assertFalse(\OCA\Files_Versions\Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user2), 'Revert did not happen');
$this->loginAsUser(self::TEST_VERSIONS_USER);
\OC::$server->getShareManager()->deleteShare($share);
$this->assertEquals('test file', $file->getContent(), 'File content has not changed');
}
public function testRestoreMovedShare() {
$this->loginAsUser(self::TEST_VERSIONS_USER);
$userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER);
$node = $userHome->newFolder('folder');
$file = $node->newFile('test.txt');
$userHome2 = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER2);
$userHome2->newFolder('subfolder');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_ALL);
$share = \OC::$server->getShareManager()->createShare($share);
$shareManager = \OC::$server->getShareManager();
$shareManager->acceptShare($share, self::TEST_VERSIONS_USER2);
$share->setTarget("subfolder/folder");
$shareManager->moveShare($share, self::TEST_VERSIONS_USER2);
$versions = $this->createAndCheckVersions(
\OC\Files\Filesystem::getView(),
'folder/test.txt'
);
$file->putContent('test file');
$this->loginAsUser(self::TEST_VERSIONS_USER2);
$firstVersion = current($versions);
$this->assertTrue(\OCA\Files_Versions\Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user1));
$this->loginAsUser(self::TEST_VERSIONS_USER);
\OC::$server->getShareManager()->deleteShare($share);
$this->assertEquals('version 2', $file->getContent(), 'File content has not changed');
}
/**
* @param string $hookName name of hook called
* @param string $params variable to receive parameters provided by hook
*/
private function connectMockHooks($hookName, &$params) {
if ($hookName === null) {
return;
}
$eventHandler = $this->getMockBuilder(\stdclass::class)
->setMethods(['callback'])
->getMock();
$eventHandler->expects($this->any())
->method('callback')
->will($this->returnCallback(
function($p) use (&$params) {
$params = $p;
}
));
\OCP\Util::connectHook(
'\OCP\Versions',
$hookName,
$eventHandler,
'callback'
);
}
private function doTestRestore() {
$filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt';
$this->rootView->file_put_contents($filePath, 'test file');
$t0 = $this->rootView->filemtime($filePath);
// not exactly the same timestamp as the file
$t1 = time() - 60;
// second version is two weeks older
$t2 = $t1 - 60 * 60 * 24 * 14;
// create some versions
$v1 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t1;
$v2 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t2;
$this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/sub');
$this->rootView->file_put_contents($v1, 'version1');
$this->rootView->file_put_contents($v2, 'version2');
$oldVersions = \OCA\Files_Versions\Storage::getVersions(
self::TEST_VERSIONS_USER, '/sub/test.txt'
);
$this->assertCount(2, $oldVersions);
$this->assertEquals('test file', $this->rootView->file_get_contents($filePath));
$info1 = $this->rootView->getFileInfo($filePath);
$params = array();
$this->connectMockHooks('rollback', $params);
$this->assertTrue(\OCA\Files_Versions\Storage::rollback('sub/test.txt', $t2, $this->user1));
$expectedParams = array(
'path' => '/sub/test.txt',
);
$this->assertEquals($expectedParams['path'], $params['path']);
$this->assertTrue(array_key_exists('revision', $params));
$this->assertTrue($params['revision'] > 0);
$this->assertEquals('version2', $this->rootView->file_get_contents($filePath));
$info2 = $this->rootView->getFileInfo($filePath);
$this->assertNotEquals(
$info2['etag'],
$info1['etag'],
'Etag must change after rolling back version'
);
$this->assertEquals(
$info2['fileid'],
$info1['fileid'],
'File id must not change after rolling back version'
);
$this->assertEquals(
$info2['mtime'],
$t2,
'Restored file has mtime from version'
);
$newVersions = \OCA\Files_Versions\Storage::getVersions(
self::TEST_VERSIONS_USER, '/sub/test.txt'
);
$this->assertTrue(
$this->rootView->file_exists(self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t0),
'A version file was created for the file before restoration'
);
$this->assertTrue(
$this->rootView->file_exists($v1),
'Untouched version file is still there'
);
$this->assertFalse(
$this->rootView->file_exists($v2),
'Restored version file gone from files_version folder'
);
$this->assertCount(2, $newVersions, 'Additional version created');
$this->assertTrue(
isset($newVersions[$t0 . '#' . 'test.txt']),
'A version was created for the file before restoration'
);
$this->assertTrue(
isset($newVersions[$t1 . '#' . 'test.txt']),
'Untouched version is still there'
);
$this->assertFalse(
isset($newVersions[$t2 . '#' . 'test.txt']),
'Restored version is not in the list any more'
);
}
/**
* Test whether versions are created when overwriting as owner
*/
public function testStoreVersionAsOwner() {
$this->loginAsUser(self::TEST_VERSIONS_USER);
$this->createAndCheckVersions(
\OC\Files\Filesystem::getView(),
'test.txt'
);
}
/**
* Test whether versions are created when overwriting as share recipient
*/
public function testStoreVersionAsRecipient() {
$this->loginAsUser(self::TEST_VERSIONS_USER);
\OC\Files\Filesystem::mkdir('folder');
\OC\Files\Filesystem::file_put_contents('folder/test.txt', 'test file');
$node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('folder');
$share = \OC::$server->getShareManager()->newShare();
$share->setNode($node)
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setSharedBy(self::TEST_VERSIONS_USER)
->setSharedWith(self::TEST_VERSIONS_USER2)
->setPermissions(\OCP\Constants::PERMISSION_ALL);
$share = \OC::$server->getShareManager()->createShare($share);
\OC::$server->getShareManager()->acceptShare($share, self::TEST_VERSIONS_USER2);
$this->loginAsUser(self::TEST_VERSIONS_USER2);
$this->createAndCheckVersions(
\OC\Files\Filesystem::getView(),
'folder/test.txt'
);
\OC::$server->getShareManager()->deleteShare($share);
}
/**
* Test whether versions are created when overwriting anonymously.
*
* When uploading through a public link or publicwebdav, no user
* is logged in. File modification must still be able to find
* the owner and create versions.
*/
public function testStoreVersionAsAnonymous() {
$this->logout();
// note: public link upload does this,
// needed to make the hooks fire
\OC_Util::setupFS(self::TEST_VERSIONS_USER);
$userView = new \OC\Files\View('/' . self::TEST_VERSIONS_USER . '/files');
$this->createAndCheckVersions(
$userView,
'test.txt'
);
}
/**
* @param \OC\Files\View $view
* @param string $path
*/
private function createAndCheckVersions(\OC\Files\View $view, $path) {
$view->file_put_contents($path, 'test file');
$view->file_put_contents($path, 'version 1');
$view->file_put_contents($path, 'version 2');
$this->loginAsUser(self::TEST_VERSIONS_USER);
// need to scan for the versions
[$rootStorage,] = $this->rootView->resolvePath(self::TEST_VERSIONS_USER . '/files_versions');
$rootStorage->getScanner()->scan('files_versions');
$versions = \OCA\Files_Versions\Storage::getVersions(
self::TEST_VERSIONS_USER, '/' . $path
);
// note: we cannot predict how many versions are created due to
// test run timing
$this->assertGreaterThan(0, count($versions));
return $versions;
}
/**
* @param string $user
* @param bool $create
*/
public static function loginHelper($user, $create = false) {
if ($create) {
$backend = new \Test\Util\User\Dummy();
$backend->createUser($user, $user);
\OC::$server->getUserManager()->registerBackend($backend);
}
$storage = new \ReflectionClass('\OCA\Files_Sharing\SharedStorage');
$isInitialized = $storage->getProperty('initialized');
$isInitialized->setAccessible(true);
$isInitialized->setValue($storage, false);
$isInitialized->setAccessible(false);
\OC_Util::tearDownFS();
\OC_User::setUserId('');
\OC\Files\Filesystem::tearDown();
\OC_User::setUserId($user);
\OC_Util::setupFS($user);
\OC::$server->getUserFolder($user);
}
}
// extend the original class to make it possible to test protected methods
class VersionStorageToTest extends \OCA\Files_Versions\Storage {
/**
* @param integer $time
*/
public function callProtectedGetExpireList($time, $versions) {
return self::getExpireList($time, $versions);
}
}
|