summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/filerepo/FileBackendTest.php
blob: da44797a6a129b9868dccd751380f646727ba75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
<?php

/**
 * @group FileRepo
 * @group FileBackend
 */
class FileBackendTest extends MediaWikiTestCase {
	private $backend, $multiBackend;
	private $filesToPrune = array();
	private $dirsToPrune = array();
	private static $backendToUse;

	function setUp() {
		global $wgFileBackends;
		parent::setUp();
		$tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
		if ( $this->getCliArg( 'use-filebackend=' ) ) {
			if ( self::$backendToUse ) {
				$this->singleBackend = self::$backendToUse;
			} else {
				$name = $this->getCliArg( 'use-filebackend=' );
				$useConfig = array();
				foreach ( $wgFileBackends as $conf ) {
					if ( $conf['name'] == $name ) {
						$useConfig = $conf;
					}
				}
				$useConfig['name'] = 'localtesting'; // swap name
				$class = $conf['class'];
				self::$backendToUse = new $class( $useConfig );
				$this->singleBackend = self::$backendToUse;
			}
		} else {
			$this->singleBackend = new FSFileBackend( array(
				'name'        => 'localtesting',
				'lockManager' => 'fsLockManager',
				'containerPaths' => array(
					'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
					'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
			) );
		}
		$this->multiBackend = new FileBackendMultiWrite( array(
			'name'        => 'localtesting',
			'lockManager' => 'fsLockManager',
			'backends'    => array(
				array(
					'name'          => 'localmutlitesting1',
					'class'         => 'FSFileBackend',
					'lockManager'   => 'nullLockManager',
					'containerPaths' => array(
						'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
						'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
					'isMultiMaster' => false
				),
				array(
					'name'          => 'localmutlitesting2',
					'class'         => 'FSFileBackend',
					'lockManager'   => 'nullLockManager',
					'containerPaths' => array(
						'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
						'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
					'isMultiMaster' => true
				)
			)
		) );
		$this->filesToPrune = array();
	}

	private function baseStorePath() {
		return 'mwstore://localtesting';
	}

	private function backendClass() {
		return get_class( $this->backend );
	}

	/**
	 * @dataProvider provider_testIsStoragePath
	 */
	public function testIsStoragePath( $path, $isStorePath ) {
		$this->assertEquals( $isStorePath, FileBackend::isStoragePath( $path ),
			"FileBackend::isStoragePath on path '$path'" );
	}

	function provider_testIsStoragePath() {
		return array(
			array( 'mwstore://', true ),
			array( 'mwstore://backend', true ),
			array( 'mwstore://backend/container', true ),
			array( 'mwstore://backend/container/', true ),
			array( 'mwstore://backend/container/path', true ),
			array( 'mwstore://backend//container/', true ),
			array( 'mwstore://backend//container//', true ),
			array( 'mwstore://backend//container//path', true ),
			array( 'mwstore:///', true ),
			array( 'mwstore:/', false ),
			array( 'mwstore:', false ),
		);
	}

	/**
	 * @dataProvider provider_testSplitStoragePath
	 */
	public function testSplitStoragePath( $path, $res ) {
		$this->assertEquals( $res, FileBackend::splitStoragePath( $path ),
			"FileBackend::splitStoragePath on path '$path'" );
	}

	function provider_testSplitStoragePath() {
		return array(
			array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
			array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
			array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
			array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
			array( 'mwstore://backend//container/path', array( null, null, null ) ),
			array( 'mwstore://backend//container//path', array( null, null, null ) ),
			array( 'mwstore://', array( null, null, null ) ),
			array( 'mwstore://backend', array( null, null, null ) ),
			array( 'mwstore:///', array( null, null, null ) ),
			array( 'mwstore:/', array( null, null, null ) ),
			array( 'mwstore:', array( null, null, null ) )
		);
	}

	/**
	 * @dataProvider provider_normalizeStoragePath
	 */
	public function testNormalizeStoragePath( $path, $res ) {
		$this->assertEquals( $res, FileBackend::normalizeStoragePath( $path ),
			"FileBackend::normalizeStoragePath on path '$path'" );
	}

	function provider_normalizeStoragePath() {
		return array(
			array( 'mwstore://backend/container', 'mwstore://backend/container' ),
			array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
			array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
			array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
			array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
			array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
			array( 'mwstore://', null ),
			array( 'mwstore://backend', null ),
			array( 'mwstore://backend//container/path', null ),
			array( 'mwstore://backend//container//path', null ),
			array( 'mwstore:///', null ),
			array( 'mwstore:/', null ),
			array( 'mwstore:', null ), )
		);
	}

	/**
	 * @dataProvider provider_testParentStoragePath
	 */
	public function testParentStoragePath( $path, $res ) {
		$this->assertEquals( $res, FileBackend::parentStoragePath( $path ),
			"FileBackend::parentStoragePath on path '$path'" );
	}

	function provider_testParentStoragePath() {
		return array(
			array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
			array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
			array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
			array( 'mwstore://backend/container', null ),
			array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
			array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
			array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
			array( 'mwstore://backend/container/', null ),
		);
	}

	/**
	 * @dataProvider provider_testExtensionFromPath
	 */
	public function testExtensionFromPath( $path, $res ) {
		$this->assertEquals( $res, FileBackend::extensionFromPath( $path ),
			"FileBackend::extensionFromPath on path '$path'" );
	}

	function provider_testExtensionFromPath() {
		return array(
			array( 'mwstore://backend/container/path.txt', 'txt' ),
			array( 'mwstore://backend/container/path.svg.png', 'png' ),
			array( 'mwstore://backend/container/path', '' ),
			array( 'mwstore://backend/container/path.', '' ),
		);
	}

	/**
	 * @dataProvider provider_testStore
	 */
	public function testStore( $op ) {
		$this->filesToPrune[] = $op['src'];

		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestStore( $op );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestStore( $op );
		$this->filesToPrune[] = $op['src']; # avoid file leaking
		$this->tearDownFiles();
	}

	function doTestStore( $op ) {
		$backendName = $this->backendClass();

		$source = $op['src'];
		$dest = $op['dst'];
		$this->prepare( array( 'dir' => dirname( $dest ) ) );

		file_put_contents( $source, "Unit test file" );

		if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
			$this->backend->store( $op );
		}

		$status = $this->backend->doOperation( $op );

		$this->assertEquals( array(), $status->errors,
			"Store from $source to $dest succeeded without warnings ($backendName)." );
		$this->assertEquals( array(), $status->errors,
			"Store from $source to $dest succeeded ($backendName)." );
		$this->assertEquals( array( 0 => true ), $status->success,
			"Store from $source to $dest has proper 'success' field in Status ($backendName)." );
		$this->assertEquals( true, file_exists( $source ),
			"Source file $source still exists ($backendName)." );
		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
			"Destination file $dest exists ($backendName)." );

		$this->assertEquals( filesize( $source ),
			$this->backend->getFileSize( array( 'src' => $dest ) ),
			"Destination file $dest has correct size ($backendName)." );

		$props1 = FSFile::getPropsFromPath( $source );
		$props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
		$this->assertEquals( $props1, $props2,
			"Source and destination have the same props ($backendName)." );
	}

	public function provider_testStore() {
		$cases = array();

		$tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
		$toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
		$op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
		$cases[] = array(
			$op, // operation
			$tmpName, // source
			$toPath, // dest
		);

		$op2 = $op;
		$op2['overwrite'] = true;
		$cases[] = array(
			$op2, // operation
			$tmpName, // source
			$toPath, // dest
		);

		$op2 = $op;
		$op2['overwriteSame'] = true;
		$cases[] = array(
			$op2, // operation
			$tmpName, // source
			$toPath, // dest
		);

		return $cases;
	}

	/**
	 * @dataProvider provider_testCopy
	 */
	public function testCopy( $op ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestCopy( $op );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestCopy( $op );
		$this->tearDownFiles();
	}

	function doTestCopy( $op ) {
		$backendName = $this->backendClass();

		$source = $op['src'];
		$dest = $op['dst'];
		$this->prepare( array( 'dir' => dirname( $source ) ) );
		$this->prepare( array( 'dir' => dirname( $dest ) ) );

		$status = $this->backend->doOperation(
			array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
		$this->assertEquals( array(), $status->errors,
			"Creation of file at $source succeeded ($backendName)." );

		if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
			$this->backend->copy( $op );
		}

		$status = $this->backend->doOperation( $op );

		$this->assertEquals( array(), $status->errors,
			"Copy from $source to $dest succeeded without warnings ($backendName)." );
		$this->assertEquals( true, $status->isOK(),
			"Copy from $source to $dest succeeded ($backendName)." );
		$this->assertEquals( array( 0 => true ), $status->success,
			"Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
			"Source file $source still exists ($backendName)." );
		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
			"Destination file $dest exists after copy ($backendName)." );

		$this->assertEquals(
			$this->backend->getFileSize( array( 'src' => $source ) ),
			$this->backend->getFileSize( array( 'src' => $dest ) ),
			"Destination file $dest has correct size ($backendName)." );

		$props1 = $this->backend->getFileProps( array( 'src' => $source ) );
		$props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
		$this->assertEquals( $props1, $props2,
			"Source and destination have the same props ($backendName)." );
	}

	public function provider_testCopy() {
		$cases = array();

		$source = $this->baseStorePath() . '/unittest-cont1/file.txt';
		$dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';

		$op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
		$cases[] = array(
			$op, // operation
			$source, // source
			$dest, // dest
		);

		$op2 = $op;
		$op2['overwrite'] = true;
		$cases[] = array(
			$op2, // operation
			$source, // source
			$dest, // dest
		);

		$op2 = $op;
		$op2['overwriteSame'] = true;
		$cases[] = array(
			$op2, // operation
			$source, // source
			$dest, // dest
		);

		return $cases;
	}

	/**
	 * @dataProvider provider_testMove
	 */
	public function testMove( $op ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestMove( $op );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestMove( $op );
		$this->tearDownFiles();
	}

	private function doTestMove( $op ) {
		$backendName = $this->backendClass();

		$source = $op['src'];
		$dest = $op['dst'];
		$this->prepare( array( 'dir' => dirname( $source ) ) );
		$this->prepare( array( 'dir' => dirname( $dest ) ) );

		$status = $this->backend->doOperation(
			array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
		$this->assertEquals( array(), $status->errors,
			"Creation of file at $source succeeded ($backendName)." );

		if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
			$this->backend->copy( $op );
		}

		$status = $this->backend->doOperation( $op );
		$this->assertEquals( array(), $status->errors,
			"Move from $source to $dest succeeded without warnings ($backendName)." );
		$this->assertEquals( true, $status->isOK(),
			"Move from $source to $dest succeeded ($backendName)." );
		$this->assertEquals( array( 0 => true ), $status->success,
			"Move from $source to $dest has proper 'success' field in Status ($backendName)." );
		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
			"Source file $source does not still exists ($backendName)." );
		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
			"Destination file $dest exists after move ($backendName)." );

		$this->assertNotEquals(
			$this->backend->getFileSize( array( 'src' => $source ) ),
			$this->backend->getFileSize( array( 'src' => $dest ) ),
			"Destination file $dest has correct size ($backendName)." );

		$props1 = $this->backend->getFileProps( array( 'src' => $source ) );
		$props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
		$this->assertEquals( false, $props1['fileExists'],
			"Source file does not exist accourding to props ($backendName)." );
		$this->assertEquals( true, $props2['fileExists'],
			"Destination file exists accourding to props ($backendName)." );
	}

	public function provider_testMove() {
		$cases = array();

		$source = $this->baseStorePath() . '/unittest-cont1/file.txt';
		$dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';

		$op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
		$cases[] = array(
			$op, // operation
			$source, // source
			$dest, // dest
		);

		$op2 = $op;
		$op2['overwrite'] = true;
		$cases[] = array(
			$op2, // operation
			$source, // source
			$dest, // dest
		);

		$op2 = $op;
		$op2['overwriteSame'] = true;
		$cases[] = array(
			$op2, // operation
			$source, // source
			$dest, // dest
		);

		return $cases;
	}

	/**
	 * @dataProvider provider_testDelete
	 */
	public function testDelete( $op, $withSource, $okStatus ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestDelete( $op, $withSource, $okStatus );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestDelete( $op, $withSource, $okStatus );
		$this->tearDownFiles();
	}

	private function doTestDelete( $op, $withSource, $okStatus ) {
		$backendName = $this->backendClass();

		$source = $op['src'];
		$this->prepare( array( 'dir' => dirname( $source ) ) );

		if ( $withSource ) {
			$status = $this->backend->doOperation(
				array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
			$this->assertEquals( array(), $status->errors,
				"Creation of file at $source succeeded ($backendName)." );
		}

		$status = $this->backend->doOperation( $op );
		if ( $okStatus ) {
			$this->assertEquals( array(), $status->errors,
				"Deletion of file at $source succeeded without warnings ($backendName)." );
			$this->assertEquals( true, $status->isOK(),
				"Deletion of file at $source succeeded ($backendName)." );
			$this->assertEquals( array( 0 => true ), $status->success,
				"Deletion of file at $source has proper 'success' field in Status ($backendName)." );
		} else {
			$this->assertEquals( false, $status->isOK(),
				"Deletion of file at $source failed ($backendName)." );
		}

		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
			"Source file $source does not exist after move ($backendName)." );

		$this->assertFalse(
			$this->backend->getFileSize( array( 'src' => $source ) ),
			"Source file $source has correct size (false) ($backendName)." );

		$props1 = $this->backend->getFileProps( array( 'src' => $source ) );
		$this->assertFalse( $props1['fileExists'],
			"Source file $source does not exist according to props ($backendName)." );
	}

	public function provider_testDelete() {
		$cases = array();

		$source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';

		$op = array( 'op' => 'delete', 'src' => $source );
		$cases[] = array(
			$op, // operation
			true, // with source
			true // succeeds
		);

		$cases[] = array(
			$op, // operation
			false, // without source
			false // fails
		);

		$op['ignoreMissingSource'] = true;
		$cases[] = array(
			$op, // operation
			false, // without source
			true // succeeds
		);

		return $cases;
	}

	/**
	 * @dataProvider provider_testCreate
	 */
	public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
		$this->tearDownFiles();
	}

	private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
		$backendName = $this->backendClass();

		$dest = $op['dst'];
		$this->prepare( array( 'dir' => dirname( $dest ) ) );

		$oldText = 'blah...blah...waahwaah';
		if ( $alreadyExists ) {
			$status = $this->backend->doOperation(
				array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
			$this->assertEquals( array(), $status->errors,
				"Creation of file at $dest succeeded ($backendName)." );
		}

		$status = $this->backend->doOperation( $op );
		if ( $okStatus ) {
			$this->assertEquals( array(), $status->errors,
				"Creation of file at $dest succeeded without warnings ($backendName)." );
			$this->assertEquals( true, $status->isOK(),
				"Creation of file at $dest succeeded ($backendName)." );
			$this->assertEquals( array( 0 => true ), $status->success,
				"Creation of file at $dest has proper 'success' field in Status ($backendName)." );
		} else {
			$this->assertEquals( false, $status->isOK(),
				"Creation of file at $dest failed ($backendName)." );
		}

		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
			"Destination file $dest exists after creation ($backendName)." );

		$props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
		$this->assertEquals( true, $props1['fileExists'],
			"Destination file $dest exists according to props ($backendName)." );
		if ( $okStatus ) { // file content is what we saved
			$this->assertEquals( $newSize, $props1['size'],
				"Destination file $dest has expected size according to props ($backendName)." );
			$this->assertEquals( $newSize,
				$this->backend->getFileSize( array( 'src' => $dest ) ),
				"Destination file $dest has correct size ($backendName)." );
		} else { // file content is some other previous text
			$this->assertEquals( strlen( $oldText ), $props1['size'],
				"Destination file $dest has original size according to props ($backendName)." );
			$this->assertEquals( strlen( $oldText ),
				$this->backend->getFileSize( array( 'src' => $dest ) ),
				"Destination file $dest has original size according to props ($backendName)." );
		}
	}

	/**
	 * @dataProvider provider_testCreate
	 */
	public function provider_testCreate() {
		$cases = array();

		$dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';

		$op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
		$cases[] = array(
			$op, // operation
			false, // no dest already exists
			true, // succeeds
			strlen( $op['content'] )
		);

		$op2 = $op;
		$op2['content'] = "\n";
		$cases[] = array(
			$op2, // operation
			false, // no dest already exists
			true, // succeeds
			strlen( $op2['content'] )
		);

		$op2 = $op;
		$op2['content'] = "fsf\n waf 3kt";
		$cases[] = array(
			$op2, // operation
			true, // dest already exists
			false, // fails
			strlen( $op2['content'] )
		);

		$op2 = $op;
		$op2['content'] = "egm'g gkpe gpqg eqwgwqg";
		$op2['overwrite'] = true;
		$cases[] = array(
			$op2, // operation
			true, // dest already exists
			true, // succeeds
			strlen( $op2['content'] )
		);

		$op2 = $op;
		$op2['content'] = "39qjmg3-qg";
		$op2['overwriteSame'] = true;
		$cases[] = array(
			$op2, // operation
			true, // dest already exists
			false, // succeeds
			strlen( $op2['content'] )
		);

		return $cases;
	}

	/**
	 * @dataProvider provider_testConcatenate
	 */
	public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
		$this->filesToPrune[] = $op['dst'];

		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
		$this->filesToPrune[] = $op['dst']; # avoid file leaking
		$this->tearDownFiles();
	}

	public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
		$backendName = $this->backendClass();

		$expContent = '';
		// Create sources
		$ops = array();
		foreach ( $srcs as $i => $source ) {
			$this->prepare( array( 'dir' => dirname( $source ) ) );
			$ops[] = array(
				'op'      => 'create', // operation
				'dst'     => $source, // source
				'content' => $srcsContent[$i]
			);
			$expContent .= $srcsContent[$i];
		}
		$status = $this->backend->doOperations( $ops );

		$this->assertEquals( array(), $status->errors,
			"Creation of source files succeeded ($backendName)." );

		$dest = $params['dst'];
		if ( $alreadyExists ) {
			$ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
			$this->assertEquals( true, $ok,
				"Creation of file at $dest succeeded ($backendName)." );
		} else {
			$ok = file_put_contents( $dest, '' ) !== false;
			$this->assertEquals( true, $ok,
				"Creation of 0-byte file at $dest succeeded ($backendName)." );
		}

		// Combine the files into one
		$status = $this->backend->concatenate( $params );
		if ( $okStatus ) {
			$this->assertEquals( array(), $status->errors,
				"Creation of concat file at $dest succeeded without warnings ($backendName)." );
			$this->assertEquals( true, $status->isOK(),
				"Creation of concat file at $dest succeeded ($backendName)." );
		} else {
			$this->assertEquals( false, $status->isOK(),
				"Creation of concat file at $dest failed ($backendName)." );
		}

		if ( $okStatus ) {
			$this->assertEquals( true, is_file( $dest ),
				"Dest concat file $dest exists after creation ($backendName)." );
		} else {
			$this->assertEquals( true, is_file( $dest ),
				"Dest concat file $dest exists after failed creation ($backendName)." );
		}

		$contents = file_get_contents( $dest );
		$this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );

		if ( $okStatus ) {
			$this->assertEquals( $expContent, $contents,
				"Concat file at $dest has correct contents ($backendName)." );
		} else {
			$this->assertNotEquals( $expContent, $contents,
				"Concat file at $dest has correct contents ($backendName)." );
		}
	}

	function provider_testConcatenate() {
		$cases = array();

		$rand = mt_rand( 0, 2000000000 ) . time();
		$dest = wfTempDir() . "/randomfile!$rand.txt";
		$srcs = array(
			$this->baseStorePath() . '/unittest-cont1/file1.txt',
			$this->baseStorePath() . '/unittest-cont1/file2.txt',
			$this->baseStorePath() . '/unittest-cont1/file3.txt',
			$this->baseStorePath() . '/unittest-cont1/file4.txt',
			$this->baseStorePath() . '/unittest-cont1/file5.txt',
			$this->baseStorePath() . '/unittest-cont1/file6.txt',
			$this->baseStorePath() . '/unittest-cont1/file7.txt',
			$this->baseStorePath() . '/unittest-cont1/file8.txt',
			$this->baseStorePath() . '/unittest-cont1/file9.txt',
			$this->baseStorePath() . '/unittest-cont1/file10.txt'
		);
		$content = array(
			'egfage',
			'ageageag',
			'rhokohlr',
			'shgmslkg',
			'kenga',
			'owagmal',
			'kgmae',
			'g eak;g',
			'lkaem;a',
			'legma'
		);
		$params = array( 'srcs' => $srcs, 'dst' => $dest );

		$cases[] = array(
			$params, // operation
			$srcs, // sources
			$content, // content for each source
			false, // no dest already exists
			true, // succeeds
		);

		$cases[] = array(
			$params, // operation
			$srcs, // sources
			$content, // content for each source
			true, // dest already exists
			false, // succeeds
		);

		return $cases;
	}

	/**
	 * @dataProvider provider_testGetFileStat
	 */
	public function testGetFileStat( $path, $content, $alreadyExists ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestGetFileStat( $path, $content, $alreadyExists );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestGetFileStat( $path, $content, $alreadyExists );
		$this->tearDownFiles();
	}

	private function doTestGetFileStat( $path, $content, $alreadyExists ) {
		$backendName = $this->backendClass();

		if ( $alreadyExists ) {
			$this->prepare( array( 'dir' => dirname( $path ) ) );
			$status = $this->backend->create( array( 'dst' => $path, 'content' => $content ) );
			$this->assertEquals( array(), $status->errors,
				"Creation of file at $path succeeded ($backendName)." );

			$size = $this->backend->getFileSize( array( 'src' => $path ) );
			$time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
			$stat = $this->backend->getFileStat( array( 'src' => $path ) );

			$this->assertEquals( strlen( $content ), $size,
				"Correct file size of '$path'" );
			$this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 5,
				"Correct file timestamp of '$path'" );

			$size = $stat['size'];
			$time = $stat['mtime'];
			$this->assertEquals( strlen( $content ), $size,
				"Correct file size of '$path'" );
			$this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 5,
				"Correct file timestamp of '$path'" );
		} else {
			$size = $this->backend->getFileSize( array( 'src' => $path ) );
			$time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
			$stat = $this->backend->getFileStat( array( 'src' => $path ) );
			
			$this->assertFalse( $size, "Correct file size of '$path'" );
			$this->assertFalse( $time, "Correct file timestamp of '$path'" );
			$this->assertFalse( $stat, "Correct file stat of '$path'" );
		}
	}

	function provider_testGetFileStat() {
		$cases = array();

		$base = $this->baseStorePath();
		$cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
		$cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
		$cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );

		return $cases;
	}

	/**
	 * @dataProvider provider_testGetFileContents
	 */
	public function testGetFileContents( $source, $content ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestGetFileContents( $source, $content );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestGetFileContents( $source, $content );
		$this->tearDownFiles();
	}

	public function doTestGetFileContents( $source, $content ) {
		$backendName = $this->backendClass();

		$this->prepare( array( 'dir' => dirname( $source ) ) );

		$status = $this->backend->doOperation(
			array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
		$this->assertEquals( array(), $status->errors,
			"Creation of file at $source succeeded ($backendName)." );
		$this->assertEquals( true, $status->isOK(),
			"Creation of file at $source succeeded with OK status ($backendName)." );

		$newContents = $this->backend->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
		$this->assertNotEquals( false, $newContents,
			"Read of file at $source succeeded ($backendName)." );

		$this->assertEquals( $content, $newContents,
			"Contents read match data at $source ($backendName)." );
	}

	function provider_testGetFileContents() {
		$cases = array();

		$base = $this->baseStorePath();
		$cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
		$cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );

		return $cases;
	}

	/**
	 * @dataProvider provider_testGetLocalCopy
	 */
	public function testGetLocalCopy( $source, $content ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestGetLocalCopy( $source, $content );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestGetLocalCopy( $source, $content );
		$this->tearDownFiles();
	}

	public function doTestGetLocalCopy( $source, $content ) {
		$backendName = $this->backendClass();

		$this->prepare( array( 'dir' => dirname( $source ) ) );

		$status = $this->backend->doOperation(
			array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
		$this->assertEquals( array(), $status->errors,
			"Creation of file at $source succeeded ($backendName)." );

		$tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
		$this->assertNotNull( $tmpFile,
			"Creation of local copy of $source succeeded ($backendName)." );

		$contents = file_get_contents( $tmpFile->getPath() );
		$this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
	}

	function provider_testGetLocalCopy() {
		$cases = array();

		$base = $this->baseStorePath();
		$cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
		$cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );

		return $cases;
	}

	/**
	 * @dataProvider provider_testGetLocalReference
	 */
	public function testGetLocalReference( $source, $content ) {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestGetLocalReference( $source, $content );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestGetLocalReference( $source, $content );
		$this->tearDownFiles();
	}

	private function doTestGetLocalReference( $source, $content ) {
		$backendName = $this->backendClass();

		$this->prepare( array( 'dir' => dirname( $source ) ) );

		$status = $this->backend->doOperation(
			array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
		$this->assertEquals( array(), $status->errors,
			"Creation of file at $source succeeded ($backendName)." );

		$tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
		$this->assertNotNull( $tmpFile,
			"Creation of local copy of $source succeeded ($backendName)." );

		$contents = file_get_contents( $tmpFile->getPath() );
		$this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
	}

	function provider_testGetLocalReference() {
		$cases = array();

		$base = $this->baseStorePath();
		$cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
		$cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );

		return $cases;
	}

	/**
	 * @dataProvider provider_testPrepareAndClean
	 */
	public function testPrepareAndClean( $path, $isOK ) {
		$this->backend = $this->singleBackend;
		$this->doTestPrepareAndClean( $path, $isOK );
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->doTestPrepareAndClean( $path, $isOK );
		$this->tearDownFiles();
	}

	function provider_testPrepareAndClean() {
		$base = $this->baseStorePath();
		return array(
			array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
			array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
			# Specific to FS backend with no basePath field set
			#array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
		);
	}

	function doTestPrepareAndClean( $path, $isOK ) {
		$backendName = $this->backendClass();

		$status = $this->prepare( array( 'dir' => dirname( $path ) ) );
		if ( $isOK ) {
			$this->assertEquals( array(), $status->errors,
				"Preparing dir $path succeeded without warnings ($backendName)." );
			$this->assertEquals( true, $status->isOK(),
				"Preparing dir $path succeeded ($backendName)." );
		} else {
			$this->assertEquals( false, $status->isOK(),
				"Preparing dir $path failed ($backendName)." );
		}

		$status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
		if ( $isOK ) {
			$this->assertEquals( array(), $status->errors,
				"Cleaning dir $path succeeded without warnings ($backendName)." );
			$this->assertEquals( true, $status->isOK(),
				"Cleaning dir $path succeeded ($backendName)." );
		} else {
			$this->assertEquals( false, $status->isOK(),
				"Cleaning dir $path failed ($backendName)." );
		}
	}

	// @TODO: testSecure

	public function testDoOperations() {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestDoOperations();
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestDoOperations();
		$this->tearDownFiles();

		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestDoOperationsFailing();
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestDoOperationsFailing();
		$this->tearDownFiles();

		// @TODO: test some cases where the ops should fail
	}

	function doTestDoOperations() {
		$base = $this->baseStorePath();

		$fileA = "$base/unittest-cont1/a/b/fileA.txt";
		$fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
		$fileB = "$base/unittest-cont1/a/b/fileB.txt";
		$fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
		$fileC = "$base/unittest-cont1/a/b/fileC.txt";
		$fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
		$fileD = "$base/unittest-cont1/a/b/fileD.txt";

		$this->prepare( array( 'dir' => dirname( $fileA ) ) );
		$this->backend->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
		$this->prepare( array( 'dir' => dirname( $fileB ) ) );
		$this->backend->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
		$this->prepare( array( 'dir' => dirname( $fileC ) ) );
		$this->backend->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );

		$status = $this->backend->doOperations( array(
			array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
			// Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
			array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
			// Now: A:<A>, B:<B>, C:<A>, D:<empty>
			array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
			// Now: A:<A>, B:<B>, C:<empty>, D:<A>
			array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
			// Now: A:<A>, B:<empty>, C:<B>, D:<A>
			array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
			// Now: A:<A>, B:<empty>, C:<B>, D:<empty>
			array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
			// Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
			array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
			// Now: A:<B>, B:<empty>, C:<B>, D:<empty>
			array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
			// Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
			array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
			// Does nothing
			array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
			// Does nothing
			array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
			// Does nothing
			array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
			// Does nothing
			array( 'op' => 'null' ),
			// Does nothing
		) );

		$this->assertEquals( array(), $status->errors, "Operation batch succeeded" );
		$this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
		$this->assertEquals( 13, count( $status->success ),
			"Operation batch has correct success array" );

		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
			"File does not exist at $fileA" );
		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
			"File does not exist at $fileB" );
		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
			"File does not exist at $fileD" );

		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
			"File exists at $fileC" );
		$this->assertEquals( $fileBContents,
			$this->backend->getFileContents( array( 'src' => $fileC ) ),
			"Correct file contents of $fileC" );
		$this->assertEquals( strlen( $fileBContents ),
			$this->backend->getFileSize( array( 'src' => $fileC ) ),
			"Correct file size of $fileC" );
		$this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
			$this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
			"Correct file SHA-1 of $fileC" );
	}

	function doTestDoOperationsFailing() {
		$base = $this->baseStorePath();

		$fileA = "$base/unittest-cont2/a/b/fileA.txt";
		$fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
		$fileB = "$base/unittest-cont2/a/b/fileB.txt";
		$fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
		$fileC = "$base/unittest-cont2/a/b/fileC.txt";
		$fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
		$fileD = "$base/unittest-cont2/a/b/fileD.txt";

		$this->prepare( array( 'dir' => dirname( $fileA ) ) );
		$this->backend->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
		$this->prepare( array( 'dir' => dirname( $fileB ) ) );
		$this->backend->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
		$this->prepare( array( 'dir' => dirname( $fileC ) ) );
		$this->backend->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );

		$status = $this->backend->doOperations( array(
			array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
			// Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
			array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
			// Now: A:<A>, B:<B>, C:<A>, D:<empty>
			array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
			// Now: A:<A>, B:<B>, C:<A>, D:<B>
			array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
			// Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
			array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
			// Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
			array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
			// Now: A:<B>, B:<empty>, C:<A>, D:<empty>
			array( 'op' => 'delete', 'src' => $fileD ),
			// Now: A:<B>, B:<empty>, C:<A>, D:<empty>
			array( 'op' => 'null' ),
			// Does nothing
		), array( 'force' => 1 ) );

		$this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
		$this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
		$this->assertEquals( 8, count( $status->success ),
			"Operation batch has correct success array" );

		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
			"File does not exist at $fileB" );
		$this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
			"File does not exist at $fileD" );

		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
			"File does not exist at $fileA" );
		$this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
			"File exists at $fileC" );
		$this->assertEquals( $fileBContents,
			$this->backend->getFileContents( array( 'src' => $fileA ) ),
			"Correct file contents of $fileA" );
		$this->assertEquals( strlen( $fileBContents ),
			$this->backend->getFileSize( array( 'src' => $fileA ) ),
			"Correct file size of $fileA" );
		$this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
			$this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
			"Correct file SHA-1 of $fileA" );
	}

	public function testGetFileList() {
		$this->backend = $this->singleBackend;
		$this->tearDownFiles();
		$this->doTestGetFileList();
		$this->tearDownFiles();

		$this->backend = $this->multiBackend;
		$this->tearDownFiles();
		$this->doTestGetFileList();
		$this->tearDownFiles();
	}

	private function doTestGetFileList() {
		$backendName = $this->backendClass();

		$base = $this->baseStorePath();
		$files = array(
			"$base/unittest-cont1/test1.txt",
			"$base/unittest-cont1/test2.txt",
			"$base/unittest-cont1/test3.txt",
			"$base/unittest-cont1/subdir1/test1.txt",
			"$base/unittest-cont1/subdir1/test2.txt",
			"$base/unittest-cont1/subdir2/test3.txt",
			"$base/unittest-cont1/subdir2/test4.txt",
			"$base/unittest-cont1/subdir2/subdir/test1.txt",
			"$base/unittest-cont1/subdir2/subdir/test2.txt",
			"$base/unittest-cont1/subdir2/subdir/test3.txt",
			"$base/unittest-cont1/subdir2/subdir/test4.txt",
			"$base/unittest-cont1/subdir2/subdir/test5.txt",
			"$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
			"$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
		);

		// Add the files
		$ops = array();
		foreach ( $files as $file ) {
			$this->prepare( array( 'dir' => dirname( $file ) ) );
			$ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
		}
		$status = $this->backend->doOperations( $ops );
		$this->assertEquals( array(), $status->errors,
			"Creation of files succeeded ($backendName)." );
		$this->assertEquals( true, $status->isOK(),
			"Creation of files succeeded with OK status ($backendName)." );

		// Expected listing
		$expected = array(
			"test1.txt",
			"test2.txt",
			"test3.txt",
			"subdir1/test1.txt",
			"subdir1/test2.txt",
			"subdir2/test3.txt",
			"subdir2/test4.txt",
			"subdir2/subdir/test1.txt",
			"subdir2/subdir/test2.txt",
			"subdir2/subdir/test3.txt",
			"subdir2/subdir/test4.txt",
			"subdir2/subdir/test5.txt",
			"subdir2/subdir/sub/test0.txt",
			"subdir2/subdir/sub/120-px-file.txt",
		);
		sort( $expected );

		// Actual listing (no trailing slash)
		$list = array();
		$iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
		foreach ( $iter as $file ) {
			$list[] = $file;
		}
		sort( $list );

		$this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );

		// Actual listing (with trailing slash)
		$list = array();
		$iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
		foreach ( $iter as $file ) {
			$list[] = $file;
		}
		sort( $list );

		$this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );

		// Expected listing
		$expected = array(
			"test1.txt",
			"test2.txt",
			"test3.txt",
			"test4.txt",
			"test5.txt",
			"sub/test0.txt",
			"sub/120-px-file.txt",
		);
		sort( $expected );

		// Actual listing (no trailing slash)
		$list = array();
		$iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
		foreach ( $iter as $file ) {
			$list[] = $file;
		}
		sort( $list );

		$this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );

		// Actual listing (with trailing slash)
		$list = array();
		$iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
		foreach ( $iter as $file ) {
			$list[] = $file;
		}
		sort( $list );

		$this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );

		// Actual listing (using iterator second time)
		$list = array();
		foreach ( $iter as $file ) {
			$list[] = $file;
		}
		sort( $list );

		$this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );

		foreach ( $files as $file ) { // clean up
			$this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
		}

		$iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
		foreach ( $iter as $iter ) {} // no errors
	}

	// test helper wrapper for backend prepare() function
	private function prepare( array $params ) {
		$this->dirsToPrune[] = $params['dir'];
		return $this->backend->prepare( $params );
	}

	function tearDownFiles() {
		foreach ( $this->filesToPrune as $file ) {
			@unlink( $file );
		}
		$containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
		foreach ( $containers as $container ) {
			$this->deleteFiles( $container );
		}
		foreach ( $this->dirsToPrune as $dir ) {
			$this->recursiveClean( $dir );
		}
		$this->filesToPrune = $this->dirsToPrune = array();
	}

	private function deleteFiles( $container ) {
		$base = $this->baseStorePath();
		$iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
		if ( $iter ) {
			foreach ( $iter as $file ) {
				$this->backend->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
			}
		}
	}

	private function recursiveClean( $dir ) {
		do {
			if ( !$this->backend->clean( array( 'dir' => $dir ) )->isOK() ) {
				break;
			}
		} while ( $dir = FileBackend::parentStoragePath( $dir ) );
	}

	function tearDown() {
		parent::tearDown();
	}
}