-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathobject_file_system_test.php
More file actions
985 lines (810 loc) · 41 KB
/
object_file_system_test.php
File metadata and controls
985 lines (810 loc) · 41 KB
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
980
981
982
983
984
985
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_objectfs\tests;
defined('MOODLE_INTERNAL') || die();
use tool_objectfs\local\store\object_file_system;
use tool_objectfs\local\manager;
require_once(__DIR__ . '/classes/test_client.php');
require_once(__DIR__ . '/tool_objectfs_testcase.php');
class object_file_system_test extends tool_objectfs_testcase {
public function set_externalclient_config($key, $value) {
// Get a reflection of externalclient object as a property.
$reflection = new \ReflectionClass($this->filesystem);
$externalclientref = $reflection->getParentClass()->getProperty('externalclient');
$externalclientref->setAccessible(true);
// Get a reflection of externalclient->$key property.
$property = new \ReflectionProperty($externalclientref->getValue($this->filesystem), $key);
$property->setAccessible(true);
// Set new value for externalclient->$key property.
$property->setValue($externalclientref->getValue($this->filesystem), $value);
}
public function test_get_remote_path_from_storedfile_returns_local_path_if_local() {
$file = $this->create_local_file();
$expectedpath = $this->get_local_path_from_storedfile($file);
$reflection = new \ReflectionMethod(object_file_system::class, 'get_remote_path_from_storedfile');
$reflection->setAccessible(true);
$actualpath = $reflection->invokeArgs($this->filesystem, [$file]);
$this->assertEquals($expectedpath, $actualpath);
}
public function test_get_remote_path_from_storedfile_returns_external_path_if_not_local() {
$file = $this->create_remote_file();
$expectedpath = $this->get_external_path_from_storedfile($file);
$reflection = new \ReflectionMethod(object_file_system::class, 'get_remote_path_from_storedfile');
$reflection->setAccessible(true);
$actualpath = $reflection->invokeArgs($this->filesystem, [$file]);
$this->assertEquals($expectedpath, $actualpath);
}
public function test_get_remote_path_from_storedfile_returns_external_path_if_duplicated_and_preferexternal() {
set_config('preferexternal', true, 'tool_objectfs');
$this->reset_file_system(); // Needed to load new config.
$file = $this->create_duplicated_file();
$expectedpath = $this->get_external_path_from_storedfile($file);
$reflection = new \ReflectionMethod(object_file_system::class, 'get_remote_path_from_storedfile');
$reflection->setAccessible(true);
$actualpath = $reflection->invokeArgs($this->filesystem, [$file]);
$this->assertEquals($expectedpath, $actualpath);
}
public function test_get_local_path_from_hash_will_fetch_remote_if_fetchifnotfound() {
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$expectedpath = $this->get_local_path_from_hash($filehash);
$reflection = new \ReflectionMethod(object_file_system::class, 'get_local_path_from_hash');
$reflection->setAccessible(true);
$actualpath = $reflection->invokeArgs($this->filesystem, [$filehash, true]);
$this->assertEquals($expectedpath, $actualpath);
$this->assertTrue(is_readable($actualpath));
}
public function test_get_local_path_from_hash_fetch_remote_will_restore_file_permissions() {
global $CFG;
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$reflection = new \ReflectionMethod(object_file_system::class, 'get_local_path_from_hash');
$reflection->setAccessible(true);
$localpath = $reflection->invokeArgs($this->filesystem, [$filehash, true]);
$fileperms = substr(sprintf('%04o', fileperms($localpath)), -4);
$cfgperms = substr(sprintf('%04o', $CFG->filepermissions), -4);
$this->assertEquals($cfgperms, $fileperms);
}
public function test_copy_object_from_external_to_local() {
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$localpath = $this->get_local_path_from_storedfile($file);
$location = $this->filesystem->copy_object_from_external_to_local_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_DUPLICATED, $location);
$this->assertTrue(is_readable($localpath));
}
public function test_copy_object_from_external_to_local_by_hash_if_local() {
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
$location = $this->filesystem->copy_object_from_external_to_local_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_LOCAL, $location);
}
public function test_copy_object_from_external_to_local_by_hash_succeeds_if_already_duplicated() {
$file = $this->create_duplicated_file();
$filehash = $file->get_contenthash();
$location = $this->filesystem->copy_object_from_external_to_local_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_DUPLICATED, $location);
}
public function test_copy_object_from_external_to_local_by_hash_if_not_local_and_not_remote() {
$fakehash = 'this is a fake hash';
$location = $this->filesystem->copy_object_from_external_to_local_by_hash($fakehash);
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_copy_object_from_local_to_external_by_hash() {
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
$externalpath = $this->get_external_path_from_storedfile($file);
$location = $this->filesystem->copy_object_from_local_to_external_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_DUPLICATED, $location);
$this->assertTrue(is_readable($externalpath));
}
public function test_copy_object_from_local_to_external_by_hash_if_remote() {
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$location = $this->filesystem->copy_object_from_local_to_external_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_EXTERNAL, $location);
}
public function test_copy_object_from_local_to_external_by_hash_succeeds_if_already_duplicated() {
$file = $this->create_duplicated_file();
$filehash = $file->get_contenthash();
$location = $this->filesystem->copy_object_from_local_to_external_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_DUPLICATED, $location);
}
public function test_copy_object_from_local_to_external_by_hash_if_not_local_and_not_remote() {
$fakehash = 'this is a fake hash';
$location = $this->filesystem->copy_object_from_local_to_external_by_hash($fakehash);
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_delete_object_from_local_by_hash() {
$file = $this->create_duplicated_file();
$filehash = $file->get_contenthash();
$localpath = $this->get_local_path_from_storedfile($file);
$location = $this->filesystem->delete_object_from_local_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_EXTERNAL, $location);
$this->assertFalse(is_readable($localpath));
}
public function test_delete_object_from_local_by_hash_if_not_remote() {
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
$localpath = $this->get_local_path_from_storedfile($file);
$location = $this->filesystem->delete_object_from_local_by_hash($filehash);
$this->assertEquals(OBJECT_LOCATION_LOCAL, $location);
$this->assertTrue(is_readable($localpath));
}
public function test_delete_object_from_local_by_hash_if_not_local() {
$fakehash = 'this is a fake hash';
$location = $this->filesystem->delete_object_from_local_by_hash($fakehash);
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_delete_object_from_local_by_hash_if_can_verify_external_object() {
$file = $this->create_duplicated_file();
$contenthash = $file->get_contenthash();
$externalpath = $this->get_external_path_from_hash($contenthash);
$localpath = $this->get_local_path_from_storedfile($file);
$differentfilepath = __DIR__ . '/fixtures/test.txt';
copy($differentfilepath, $externalpath);
$location = $this->filesystem->delete_object_from_local_by_hash($contenthash);
$this->assertEquals(OBJECT_LOCATION_EXTERNAL, $location);
$this->assertFalse(is_readable($localpath));
}
/**
* @return array
*/
public function delete_empty_folders_provider() {
return [
[
/*
/filedir/test/d1/file1
/filedir/test/d2/file2
deleted: 0 dirs
*/
['/d1', '/d2'], ['file1', 'file2'], [true, true], true,
],
[
/*
/filedir/test/d1/file1
/filedir/test/d2/
deleted: 1dirs
- /filedir/test/d2/
*/
['/d1', '/d2'], ['file1'], [true, false], true,
],
[
/*
/filedir/test/d1/
/filedir/test/d2/file2
deleted: 1 dirs
- /filedir/test/d1/
*/
['/d1', '/d2'], ['', 'file1'], [false, true], true,
],
[
/*
/filedir/test/d1/
/filedir/test/d2/
deleted: 3 dirs
- /filedir/test/d1/
- /filedir/test/d2/
- /filedir/test/
*/
['/d1', '/d2'], [], [false, false], false,
],
];
}
/**
* @dataProvider delete_empty_folders_provider
* @param array $dirs Dirs to be created.
* @param array $files Files to be created.
* @param array $expectedparentreadable Indicates whether a dir will remain after calling 'delete_empty_folders'.
* @param bool $expectedgrandparentpathreadable If grandparent dir exists after calling 'delete_empty_folders'.
*/
public function test_delete_empty_folders(
$dirs,
$files,
$expectedparentreadable,
$expectedgrandparentpathreadable
) {
global $CFG;
$testdir = $CFG->dataroot . '/filedir/test';
foreach ($dirs as $key => $dir) {
$fullpath = $testdir . $dir;
mkdir($fullpath, 0777, true);
$file = !empty($files[$key]) ? $files[$key] : '';
if ($file !== '') {
touch($fullpath . '/' . $file);
}
}
$this->filesystem->delete_empty_dirs($testdir);
foreach ($dirs as $key => $dir) {
$this->assertEquals($expectedparentreadable[$key], is_readable($testdir . $dir));
}
$this->assertEquals($expectedgrandparentpathreadable, is_readable($testdir));
// Totara 12 clean-up.
remove_dir($testdir);
}
public function test_readfile_if_object_is_local() {
$expectedcontent = 'expected content';
$file = $this->create_local_file($expectedcontent);
$this->expectOutputString($expectedcontent);
$this->filesystem->readfile($file);
}
public function test_readfile_if_object_is_remote() {
$expectedcontent = 'expected content';
$file = $this->create_remote_file($expectedcontent);
$this->expectOutputString($expectedcontent);
$this->filesystem->readfile($file);
}
public function test_readfile_updates_object_with_error_location_on_fail() {
global $DB;
$fakefile = $this->create_error_file();
// Phpunit will fail if PHP warning is thrown (which we want)
// so we surpress here.
set_error_handler(array($this, 'test_error_surpressor'));
$this->filesystem->readfile($fakefile);
restore_error_handler();
$location = $DB->get_field('tool_objectfs_objects', 'location', array('contenthash' => $fakefile->get_contenthash()));
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_get_content_if_object_is_local() {
$expectedcontent = 'expected content';
$file = $this->create_local_file($expectedcontent);
$actualcontent = $this->filesystem->get_content($file);
$this->assertEquals($expectedcontent, $actualcontent);
}
public function test_get_content_if_object_is_remote() {
$expectedcontent = 'expected content';
$file = $this->create_remote_file($expectedcontent);
$actualcontent = $this->filesystem->get_content($file);
$this->assertEquals($expectedcontent, $actualcontent);
}
public function test_get_content_updates_object_with_error_location_on_fail() {
global $DB;
$fakefile = $this->create_error_file();
// Phpunit will fail if PHP warning is thrown (which we want)
// so we surpress here.
set_error_handler(array($this, 'test_error_surpressor'));
$this->filesystem->get_content($fakefile);
restore_error_handler();
$location = $DB->get_field('tool_objectfs_objects', 'location', array('contenthash' => $fakefile->get_contenthash()));
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_error_surpressor() {
// We do nothing. We cant surpess warnings
// normally because phpunit will still fail.
}
public function test_xsendfile_updates_object_with_error_location_on_fail() {
global $DB;
$fakefile = $this->create_error_file();
// Phpunit will fail if PHP warning is thrown (which we want)
// so we surpress here.
set_error_handler(array($this, 'test_error_surpressor'));
$this->filesystem->xsendfile($fakefile->get_contenthash());
restore_error_handler();
$location = $DB->get_field('tool_objectfs_objects', 'location', array('contenthash' => $fakefile->get_contenthash()));
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_get_content_file_handle_if_object_is_local() {
$file = $this->create_local_file();
$filehandle = $this->filesystem->get_content_file_handle($file);
$this->assertTrue(is_resource($filehandle));
}
public function test_get_content_file_handle_if_object_is_remote() {
$file = $this->create_remote_file();
$filehandle = $this->filesystem->get_content_file_handle($file);
$this->assertTrue(is_resource($filehandle));
}
public function test_get_content_file_handle_will_pull_remote_object_if_gzopen() {
$file = $this->create_remote_file();
$localpath = $this->get_local_path_from_storedfile($file);
$filehandle = $this->filesystem->get_content_file_handle($file, \stored_file::FILE_HANDLE_GZOPEN);
$this->assertTrue(is_resource($filehandle));
$this->assertTrue(is_readable($localpath));
}
public function test_get_content_file_handle_updates_object_with_error_location_on_fail() {
global $DB;
$fakefile = $this->create_error_file();
// Phpunit will fail if PHP warning is thrown (which we want)
// so we surpress here.
set_error_handler(array($this, 'test_error_surpressor'));
$filehandle = $this->filesystem->get_content_file_handle($fakefile);
restore_error_handler();
$location = $DB->get_field('tool_objectfs_objects', 'location', array('contenthash' => $fakefile->get_contenthash()));
$this->assertEquals(OBJECT_LOCATION_ERROR, $location);
}
public function test_remove_file_will_remove_local_file() {
global $DB;
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
// Delete file record so remove file will remove.
$DB->delete_records('files', array('contenthash' => $filehash));
$this->filesystem->remove_file($filehash);
$islocalreadable = $this->filesystem->is_file_readable_locally_by_hash($filehash);
$this->assertFalse($islocalreadable);
}
public function test_remove_file_will_not_remove_remote_file() {
global $DB;
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
// Delete file record so remove file will remove.
$DB->delete_records('files', array('contenthash' => $filehash));
$this->filesystem->remove_file($filehash);
$isremotereadable = $this->is_externally_readable_by_hash($filehash);
$this->assertTrue($isremotereadable);
}
public function test_file_is_seekable() {
$file = $this->create_remote_file('this is some content for the remote file');
$filehandle = $this->filesystem->get_content_file_handle($file);
$metadata = stream_get_meta_data($filehandle);
$this->assertTrue($metadata['seekable']);
$this->assertEquals(0, fseek($filehandle, 10, SEEK_SET));
$this->assertTrue(rewind($filehandle));
}
public function test_object_storage_deleter_can_delete_object_if_enabledelete_is_on_and_object_is_local() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = TOOL_OBJECTFS_DELETE_EXTERNAL_FULL;
$this->filesystem = new test_file_system();
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->assertFalse($this->is_locally_readable_by_hash($filehash));
$this->assertFalse($this->is_externally_readable_by_hash($filehash));
}
public function test_object_storage_deleter_can_delete_object_if_enabledelete_is_off_and_object_is_local() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = false;
$this->filesystem = new test_file_system();
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->assertFalse($this->is_locally_readable_by_hash($filehash));
$this->assertFalse($this->is_externally_readable_by_hash($filehash));
}
public function test_object_storage_deleter_can_delete_object_if_enabledelete_is_on_and_object_is_duplicated() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = TOOL_OBJECTFS_DELETE_EXTERNAL_FULL;
$this->filesystem = new test_file_system();
$file = $this->create_duplicated_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->assertFalse($this->is_locally_readable_by_hash($filehash));
$this->assertFalse($this->is_externally_readable_by_hash($filehash));
}
public function test_object_storage_deleter_can_delete_object_if_enabledelete_is_off_and_object_is_duplicated() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = false;
$this->filesystem = new test_file_system();
$file = $this->create_duplicated_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->assertFalse($this->is_locally_readable_by_hash($filehash));
$this->assertTrue($this->is_externally_readable_by_hash($filehash));
}
public function test_object_storage_deleter_can_delete_object_if_enabledelete_is_on_and_object_is_remote() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = TOOL_OBJECTFS_DELETE_EXTERNAL_FULL;
$this->filesystem = new test_file_system();
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->assertFalse($this->is_locally_readable_by_hash($filehash));
$this->assertFalse($this->is_externally_readable_by_hash($filehash));
}
public function test_object_storage_deleter_can_delete_object_if_enabledelete_is_off_and_object_is_remote() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = false;
$this->filesystem = new test_file_system();
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->assertFalse($this->is_locally_readable_by_hash($filehash));
$this->assertTrue($this->is_externally_readable_by_hash($filehash));
}
public function test_object_storage_locker_can_acquire_lock_if_object_is_not_locked() {
$this->filesystem = new test_file_system();
$file = $this->create_local_file();
$filehash = $file->get_contenthash();
$lock = $this->acquire_object_lock($filehash);
$this->assertEquals(gettype($lock), 'object');
$this->assertEquals(get_class($lock), 'core\lock\lock');
$lock->release();
}
public function test_can_recover_object_if_deleted_while_duplicated() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = TOOL_OBJECTFS_DELETE_EXTERNAL_TRASH;
$this->filesystem = new test_file_system();
$file = $this->create_duplicated_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->recover_file($file);
$this->assertTrue($this->is_externally_readable_by_hash($filehash));
}
public function test_can_recover_object_if_deleted_while_external() {
global $CFG;
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = TOOL_OBJECTFS_DELETE_EXTERNAL_TRASH;
$this->filesystem = new test_file_system();
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
$this->delete_draft_files($filehash);
$this->filesystem->remove_file($filehash);
$this->recover_file($file);
$this->assertTrue($this->is_externally_readable_by_hash($filehash));
}
public function test_can_generate_signed_url_by_hash_if_object_is_external() {
$this->filesystem = new test_file_system();
$file = $this->create_remote_file();
$filehash = $file->get_contenthash();
try {
$signedurl = $this->filesystem->externalclient->generate_presigned_url($filehash);
$this->assertTrue($this->is_externally_readable_by_url($signedurl));
} catch (\coding_exception $e) {
$this->assertEquals($e->a, 'Pre-signed URLs not supported');
}
}
public function test_presigned_url_configured_method_returns_false_if_not_configured() {
$this->filesystem = new test_file_system();
$this->assertFalse($this->filesystem->presigned_url_configured());
}
public function test_presigned_url_configured_method_returns_true_if_configured() {
$this->filesystem = new test_file_system();
$externalclient = $this->filesystem->get_external_client();
if (!$externalclient->support_presigned_urls()) {
$this->markTestSkipped('Pre-signed URLs not supported for given storage.');
}
$this->set_externalclient_config('enablepresignedurls', '1');
$this->assertTrue($this->filesystem->presigned_url_configured());
}
public function test_presigned_url_should_redirect_provider() {
$provider = array();
// Testing defaults.
$provider[] = array('Default', 'Default', false);
// Testing $enablepresignedurls.
$provider[] = array(1, 'Default', true);
$provider[] = array('1', 'Default', true);
$provider[] = array(0, 'Default', false);
$provider[] = array('0', 'Default', false);
$provider[] = array('', 'Default', false);
$provider[] = array(null, 'Default', false);
// Testing $presignedminfilesize.
$provider[] = array(1, 0, true);
$provider[] = array(1, '0', true);
$provider[] = array(1, '', true);
// Testing minimum file size to be greater than file size.
// 12 is a size of the file with 'test content' content.
$provider[] = array(1, 13, false);
$provider[] = array(1, '13', false);
// Testing minimum file size to be less than file size.
// 12 is a size of the file with 'test content' content.
$provider[] = array(1, 11, true);
$provider[] = array(1, '11', true);
// Testing nulls and empty strings.
$provider[] = array(null, null, false);
$provider[] = array(null, '', false);
$provider[] = array('', null, false);
$provider[] = array('', '', false);
return $provider;
}
/**
* @dataProvider test_presigned_url_should_redirect_provider
*
* @param $enablepresignedurls mixed enable pre-signed URLs.
* @param $presignedminfilesize mixed minimum file size to be redirected to pre-signed URL.
* @param $result boolean expected result.
* @throws \dml_exception
*/
public function test_presigned_url_should_redirect_method_with_data_provider($enablepresignedurls,
$presignedminfilesize, $result) {
$this->filesystem = new test_file_system();
$externalclient = $this->filesystem->get_external_client();
if (!$externalclient->support_presigned_urls()) {
$this->markTestSkipped('Pre-signed URLs not supported for given storage.');
}
if ($enablepresignedurls !== 'Default') {
$this->set_externalclient_config('enablepresignedurls', $enablepresignedurls);
}
if ($presignedminfilesize !== 'Default') {
$this->set_externalclient_config('presignedminfilesize', $presignedminfilesize);
}
if ($this->filesystem->presigned_url_configured()) {
$file = $this->create_local_file('test content');
set_config('signingwhitelist', '*', 'tool_objectfs');
$this->assertEquals($result, $this->filesystem->presigned_url_should_redirect($file->get_contenthash()));
$this->assertEquals($result, $this->filesystem->presigned_url_should_redirect_file($file));
} else {
$this->assertEquals($result, false);
}
}
/**
* Data provider for test_get_expiration_time_method_if_supported().
*
* @return array
*/
public function test_get_expiration_time_method_if_supported_provider() {
$now = time();
// Seconds after the minute from X.
$secondsafternow = ($now % MINSECS);
$secondsafternowsub100 = $secondsafternow;
$secondsafternowadd30 = $secondsafternow;
$secondsafternowadd100 = $secondsafternow;
$secondsafternowaddweek = ($now + WEEKSECS) % MINSECS;
return [
// Default Pre-Signed URL expiration time and int-like 'Expires' header.
[7200, $now, 0, $now + 7200 + MINSECS - $secondsafternow],
[7200, $now, $now - 100, $now + (2 * MINSECS) - $secondsafternowsub100],
[7200, $now, $now + 30, $now + (2 * MINSECS) - $secondsafternowadd30],
[7200, $now, $now + 100, $now + (2 * MINSECS) - $secondsafternowadd100],
[7200, $now, $now + WEEKSECS + HOURSECS, $now + WEEKSECS - MINSECS - $secondsafternowaddweek],
// Default Pre-Signed URL expiration time and string-like 'Expires' header.
[7200, $now, 'Thu, 01 Jan 1970 00:00:00 GMT', $now + 7200 + MINSECS - $secondsafternow],
[7200, $now, userdate($now - 100, '%a, %d %b %Y %H:%M:%S'), $now + (2 * MINSECS) - $secondsafternowsub100],
[7200, $now, userdate($now + 30, '%a, %d %b %Y %H:%M:%S'), $now + (2 * MINSECS) - $secondsafternowadd30],
[7200, $now, userdate($now + 100, '%a, %d %b %Y %H:%M:%S'), $now + (2 * MINSECS) - $secondsafternowadd100],
[7200, $now, userdate($now + WEEKSECS + HOURSECS, '%a, %d %b %Y %H:%M:%S'), $now + WEEKSECS - MINSECS - $secondsafternowaddweek],
// Custom Pre-Signed URL expiration time and int-like 'Expires' header.
[0, $now, 0, $now + (2 * MINSECS) - $secondsafternow],
[600, $now, 0, $now + 600 + MINSECS - $secondsafternow],
[600, $now, $now - 100, $now + (2 * MINSECS) - $secondsafternowsub100],
[600, $now, $now + 30, $now + (2 * MINSECS) - $secondsafternowadd30],
[600, $now, $now + 100, $now + (2 * MINSECS) - $secondsafternowadd100],
[600, $now, $now + WEEKSECS + HOURSECS, $now + WEEKSECS - MINSECS - $secondsafternowaddweek],
// Custom Pre-Signed URL expiration time and string-like 'Expires' header.
[0, $now, 'Thu, 01 Jan 1970 00:00:00 GMT', $now + (2 * MINSECS) - $secondsafternow],
[600, $now, 'Thu, 01 Jan 1970 00:00:00 GMT', $now + 600 + MINSECS - $secondsafternow],
[600, $now, userdate($now - 100, '%a, %d %b %Y %H:%M:%S'), $now + (2 * MINSECS) - $secondsafternowsub100],
[600, $now, userdate($now + 30, '%a, %d %b %Y %H:%M:%S'), $now + (2 * MINSECS) - $secondsafternowadd30],
[600, $now, userdate($now + 100, '%a, %d %b %Y %H:%M:%S'), $now + (2 * MINSECS) - $secondsafternowadd100],
[600, $now, userdate($now + WEEKSECS + HOURSECS, '%a, %d %b %Y %H:%M:%S'), $now + WEEKSECS - MINSECS - $secondsafternowaddweek],
];
}
/**
* Test S3 and DO clients get_expiration_time() method.
* Available when running integration tests.
*
* @dataProvider test_get_expiration_time_method_if_supported_provider
*
* @param int $expirationsetting Pre-Signed URL expiration time
* @param int $now Now timestamp
* @param mixed $expiresheader 'Expires' header
* @param int $expectedresult Expiration timestamp for URL
*/
public function test_get_expiration_time_method_if_supported($expirationsetting, $now, $expiresheader, $expectedresult) {
$this->filesystem = new test_file_system();
$externalclient = $this->filesystem->get_external_client();
if (!$externalclient->support_presigned_urls()) {
$this->markTestSkipped('Pre-signed URLs not supported for given storage.');
}
if ($expirationsetting !== 7200) {
$this->set_externalclient_config('expirationtime', $expirationsetting);
}
$this->assertEquals($expectedresult, $externalclient->get_expiration_time($now, $expiresheader));
}
/**
* Test copy_content_from_storedfile() method does direct copying.
*/
public function test_copy_content_from_storedfile() {
$file = $this->create_remote_file();
// Confirm, that the file is not readable locally.
$this->assertFalse($this->filesystem->is_file_readable_locally_by_storedfile($file));
// Get the current remote path.
$currentpath = $this->filesystem->get_remote_path_from_storedfile($file);
// Copy the file to new external path.
$result = $this->filesystem->copy_content_from_storedfile($file, $currentpath . '_new');
// Confirm the file copied successfully and method returns true.
$this->assertTrue($result);
// Confirm, that the file wasn't downloaded locally and was copied directly to new path.
$this->assertFalse($this->filesystem->is_file_readable_locally_by_storedfile($file));
}
/**
* Test get_filesize_by_contenthash() returns file size by its contenthash.
*/
public function test_get_filesize_by_contenthash() {
// Test existing file.
$file = $this->create_local_file();
$actual = $this->filesystem->get_filesize_by_contenthash($file->get_contenthash());
$this->assertEquals($file->get_filesize(), $actual);
// Test missing file.
$fakehash = 'this is a fake hash';
$actual = $this->filesystem->get_filesize_by_contenthash($fakehash);
$this->assertFalse($actual);
}
/**
* Data provider for test_get_valid_http_ranges().
*
* @return array
*/
public function test_get_valid_http_ranges_provider() {
return [
['', 0, false],
['bytes=0-', 100, (object)['rangefrom' => 0, 'rangeto' => 99, 'length' => 100]],
['bytes=0-49/100', 100, (object)['rangefrom' => 0, 'rangeto' => 49, 'length' => 50]],
['bytes=50-', 100, (object)['rangefrom' => 50, 'rangeto' => 99, 'length' => 50]],
['bytes=50-80/100', 100, (object)['rangefrom' => 50, 'rangeto' => 80, 'length' => 31]],
];
}
/**
* Test get_valid_http_ranges() returns range object depending on $_SERVER['HTTP_RANGE'] and file size.
*
* @dataProvider test_get_valid_http_ranges_provider
*
* @param string $httprangeheader HTTP_RANGE header.
* @param int $filesize File size.
* @param mixed $expectedresult Expected result.
*/
public function test_get_valid_http_ranges($httprangeheader, $filesize, $expectedresult) {
$_SERVER['HTTP_RANGE'] = $httprangeheader;
$actual = $this->filesystem->get_valid_http_ranges($filesize);
$this->assertEquals($expectedresult, $actual);
}
/**
* Data provider for test_curl_range_request_to_presigned_url().
*
* @return array
*/
public function test_curl_range_request_to_presigned_url_provider() {
return [
['15-bytes string', (object)['rangefrom' => 0, 'rangeto' => 14, 'length' => 15], '15-bytes string'],
['15-bytes string', (object)['rangefrom' => 0, 'rangeto' => 9, 'length' => 10], '15-bytes s'],
['15-bytes string', (object)['rangefrom' => 5, 'rangeto' => 14, 'length' => 10], 'tes string'],
];
}
/**
* Test external client curl_range_request_to_presigned_url() returns expected result.
*
* @dataProvider test_curl_range_request_to_presigned_url_provider
*
* @param string $content File content.
* @param mixed $ranges Request ranges object.
* @param string $expectedresult Expected result.
*/
public function test_curl_range_request_to_presigned_url($content, $ranges, $expectedresult) {
if (!$this->filesystem->get_external_client()->support_presigned_urls()) {
$this->markTestSkipped('Pre-signed URLs not supported for given storage.');
}
$file = $this->create_remote_file($content);
$externalclient = $this->filesystem->get_external_client();
// Test good response.
$actual = $externalclient->curl_range_request_to_presigned_url($file->get_contenthash(), $ranges, []);
$this->assertEquals($expectedresult, $actual['content']);
$this->assertEquals('206 Partial Content', manager::get_header($actual['responseheaders'], 'HTTP/1.1'));
// Test bad response.
$actual = $externalclient->curl_range_request_to_presigned_url($file->get_contenthash() . '_fake', $ranges, []);
$this->assertEquals('404 Not Found', manager::get_header($actual['responseheaders'], 'HTTP/1.1'));
}
/**
* Test external client test_range_request() method.
*/
public function test_test_range_request() {
$externalclient = $this->filesystem->get_external_client();
if ($externalclient->support_presigned_urls()) {
$this->assertTrue($externalclient->test_range_request($this->filesystem)->result);
} else {
$this->assertFalse($externalclient->test_range_request($this->filesystem)->result);
}
}
/**
* Test that is_configured() returns true by default.
*/
public function test_is_configured_default() {
$this->assertTrue($this->filesystem->is_configured());
}
/**
* Test that is_configured() returns false when the client SDK does not exist.
*/
public function test_is_configured_fake_autoloader() {
$this->assertTrue($this->filesystem->is_configured());
$clientref = new \ReflectionClass($this->filesystem->externalclient);
$autoloaderref = $clientref->getParentClass()->getProperty('autoloader');
$autoloaderref->setAccessible(true);
$autoloader = $autoloaderref->getValue($this->filesystem->externalclient);
$this->set_externalclient_config('autoloader', $autoloader . '_fake');
$this->assertFalse($this->filesystem->is_configured());
}
/**
* Test that is_configured() returns false when filesystem set in config.php
* and filesystem set via admin settings do not match.
*/
public function test_is_configured_settings_do_not_match() {
global $CFG;
$this->assertTrue($this->filesystem->is_configured());
$CFG->alternative_file_system_class = 'fake_file_system';
$this->assertFalse($this->filesystem->is_configured());
}
/**
* Test that is_configured() returns false when alternative_file_system_class is not set in config.php.
*/
public function test_is_configured_alternative_file_system_class_is_not_set() {
global $CFG;
$this->assertTrue($this->filesystem->is_configured());
unset($CFG->alternative_file_system_class);
$this->assertFalse($this->filesystem->is_configured());
}
/**
* @return array
*/
public function rewrite_pluginfile_urls_s3_provider() {
return [
['/pluginfile.php', '/3854/tool_objectfs/content/1/test.pdf'],
];
}
/**
* Test rewrite_pluginfile_urls() S3
*
* @dataProvider rewrite_pluginfile_urls_s3_provider
*
* @param string $script
* @param int $pluginfile
*/
public function test_rewrite_pluginfile_urls_s3_proper($script, $pluginfile) {
global $CFG, $ME;
if (!$CFG->phpunit_objectfs_s3_integration_test_credentials) {
$this->markTestSkipped('No S3 test credentials in config file');
}
$ME = "$script$pluginfile";
$bucket = $CFG->phpunit_objectfs_s3_integration_test_credentials['s3_bucket'];
$s3_region = $CFG->phpunit_objectfs_s3_integration_test_credentials['s3_region'];
$bucketkeyprefix = 'test/';
$presignedurl = "https://$bucket.s3.$s3_region.amazonaws.com/$bucketkeyprefix?param1=val1¶m2=val2";
$this->filesystem = new test_file_system();
$externalclient = $this->filesystem->get_external_client();
$this->set_externalclient_config('enablepresignedurls', '1');
$this->set_externalclient_config('signingmethod', 's3');
$this->set_externalclient_config('bucketkeyprefix', $bucketkeyprefix);
$this->assertTrue($this->filesystem->presigned_url_configured());
$embedded = $this->filesystem->embed_origin_url($presignedurl);
$textformat = 'Fake test with pdf %s';
$originaltext = sprintf($textformat, $embedded);
$this->assertEquals(
sprintf($textformat, $CFG->wwwroot.$ME),
$this->filesystem->rewrite_pluginfile_urls($originaltext)
);
}
/**
* Test rewrite_pluginfile_urls() CloudFront
*
* @dataProvider rewrite_pluginfile_urls_s3_provider
*
* @param string $script
* @param int $pluginfile
*/
public function test_rewrite_pluginfile_urls_s3_cf($script, $pluginfile) {
global $CFG, $ME;
if (!$CFG->phpunit_objectfs_s3_integration_test_credentials) {
$this->markTestSkipped('No S3 test credentials in config file');
}
$ME = "$script$pluginfile";
$cloudfrontresourcedomain = 'https://presigned.url';
$presignedurl = "$cloudfrontresourcedomain/x?param1=val1¶m2=val2";
$this->filesystem = new test_file_system();
$externalclient = $this->filesystem->get_external_client();
$this->set_externalclient_config('enablepresignedurls', '1');
$this->set_externalclient_config('signingmethod', 'cf');
$this->set_externalclient_config('cloudfrontresourcedomain', $cloudfrontresourcedomain);
$this->assertTrue($this->filesystem->presigned_url_configured());
$embedded = $this->filesystem->embed_origin_url($presignedurl);
$textformat = 'Fake test with pdf %s';
$originaltext = sprintf($textformat, $embedded);
$this->assertEquals(
sprintf($textformat, $CFG->wwwroot.$ME),
$this->filesystem->rewrite_pluginfile_urls($originaltext)
);
}
}