merged master
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / FileBackendTest.php
1 <?php
2
3 /**
4 * @group medium
5 * ^---- causes phpunit to use a higher timeout threshold
6 *
7 * @group FileRepo
8 * @group FileBackend
9 */
10 class FileBackendTest extends MediaWikiTestCase {
11 private $backend, $multiBackend;
12 private $filesToPrune = array();
13 private static $backendToUse;
14
15 function setUp() {
16 global $wgFileBackends;
17 parent::setUp();
18 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
19 if ( $this->getCliArg( 'use-filebackend=' ) ) {
20 if ( self::$backendToUse ) {
21 $this->singleBackend = self::$backendToUse;
22 } else {
23 $name = $this->getCliArg( 'use-filebackend=' );
24 $useConfig = array();
25 foreach ( $wgFileBackends as $conf ) {
26 if ( $conf['name'] == $name ) {
27 $useConfig = $conf;
28 break;
29 }
30 }
31 $useConfig['name'] = 'localtesting'; // swap name
32 $useConfig['shardViaHashLevels'] = array( // test sharding
33 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
34 );
35 $class = $useConfig['class'];
36 self::$backendToUse = new $class( $useConfig );
37 $this->singleBackend = self::$backendToUse;
38 }
39 } else {
40 $this->singleBackend = new FSFileBackend( array(
41 'name' => 'localtesting',
42 'lockManager' => 'fsLockManager',
43 #'parallelize' => 'implicit',
44 'containerPaths' => array(
45 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
46 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
47 ) );
48 }
49 $this->multiBackend = new FileBackendMultiWrite( array(
50 'name' => 'localtesting',
51 'lockManager' => 'fsLockManager',
52 'parallelize' => 'implicit',
53 'backends' => array(
54 array(
55 'name' => 'localmutlitesting1',
56 'class' => 'FSFileBackend',
57 'lockManager' => 'nullLockManager',
58 'containerPaths' => array(
59 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
60 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
61 'isMultiMaster' => false
62 ),
63 array(
64 'name' => 'localmutlitesting2',
65 'class' => 'FSFileBackend',
66 'lockManager' => 'nullLockManager',
67 'containerPaths' => array(
68 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
69 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
70 'isMultiMaster' => true
71 )
72 )
73 ) );
74 $this->filesToPrune = array();
75 }
76
77 private function baseStorePath() {
78 return 'mwstore://localtesting';
79 }
80
81 private function backendClass() {
82 return get_class( $this->backend );
83 }
84
85 /**
86 * @dataProvider provider_testIsStoragePath
87 */
88 public function testIsStoragePath( $path, $isStorePath ) {
89 $this->assertEquals( $isStorePath, FileBackend::isStoragePath( $path ),
90 "FileBackend::isStoragePath on path '$path'" );
91 }
92
93 function provider_testIsStoragePath() {
94 return array(
95 array( 'mwstore://', true ),
96 array( 'mwstore://backend', true ),
97 array( 'mwstore://backend/container', true ),
98 array( 'mwstore://backend/container/', true ),
99 array( 'mwstore://backend/container/path', true ),
100 array( 'mwstore://backend//container/', true ),
101 array( 'mwstore://backend//container//', true ),
102 array( 'mwstore://backend//container//path', true ),
103 array( 'mwstore:///', true ),
104 array( 'mwstore:/', false ),
105 array( 'mwstore:', false ),
106 );
107 }
108
109 /**
110 * @dataProvider provider_testSplitStoragePath
111 */
112 public function testSplitStoragePath( $path, $res ) {
113 $this->assertEquals( $res, FileBackend::splitStoragePath( $path ),
114 "FileBackend::splitStoragePath on path '$path'" );
115 }
116
117 function provider_testSplitStoragePath() {
118 return array(
119 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
120 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
121 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
122 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
123 array( 'mwstore://backend//container/path', array( null, null, null ) ),
124 array( 'mwstore://backend//container//path', array( null, null, null ) ),
125 array( 'mwstore://', array( null, null, null ) ),
126 array( 'mwstore://backend', array( null, null, null ) ),
127 array( 'mwstore:///', array( null, null, null ) ),
128 array( 'mwstore:/', array( null, null, null ) ),
129 array( 'mwstore:', array( null, null, null ) )
130 );
131 }
132
133 /**
134 * @dataProvider provider_normalizeStoragePath
135 */
136 public function testNormalizeStoragePath( $path, $res ) {
137 $this->assertEquals( $res, FileBackend::normalizeStoragePath( $path ),
138 "FileBackend::normalizeStoragePath on path '$path'" );
139 }
140
141 function provider_normalizeStoragePath() {
142 return array(
143 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
144 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
145 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
146 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
147 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
148 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
149 array( 'mwstore://', null ),
150 array( 'mwstore://backend', null ),
151 array( 'mwstore://backend//container/path', null ),
152 array( 'mwstore://backend//container//path', null ),
153 array( 'mwstore:///', null ),
154 array( 'mwstore:/', null ),
155 array( 'mwstore:', null ), )
156 );
157 }
158
159 /**
160 * @dataProvider provider_testParentStoragePath
161 */
162 public function testParentStoragePath( $path, $res ) {
163 $this->assertEquals( $res, FileBackend::parentStoragePath( $path ),
164 "FileBackend::parentStoragePath on path '$path'" );
165 }
166
167 function provider_testParentStoragePath() {
168 return array(
169 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
170 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
171 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
172 array( 'mwstore://backend/container', null ),
173 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
174 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
175 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
176 array( 'mwstore://backend/container/', null ),
177 );
178 }
179
180 /**
181 * @dataProvider provider_testExtensionFromPath
182 */
183 public function testExtensionFromPath( $path, $res ) {
184 $this->assertEquals( $res, FileBackend::extensionFromPath( $path ),
185 "FileBackend::extensionFromPath on path '$path'" );
186 }
187
188 function provider_testExtensionFromPath() {
189 return array(
190 array( 'mwstore://backend/container/path.txt', 'txt' ),
191 array( 'mwstore://backend/container/path.svg.png', 'png' ),
192 array( 'mwstore://backend/container/path', '' ),
193 array( 'mwstore://backend/container/path.', '' ),
194 );
195 }
196
197 /**
198 * @dataProvider provider_testStore
199 */
200 public function testStore( $op ) {
201 $this->filesToPrune[] = $op['src'];
202
203 $this->backend = $this->singleBackend;
204 $this->tearDownFiles();
205 $this->doTestStore( $op );
206 $this->tearDownFiles();
207
208 $this->backend = $this->multiBackend;
209 $this->tearDownFiles();
210 $this->doTestStore( $op );
211 $this->filesToPrune[] = $op['src']; # avoid file leaking
212 $this->tearDownFiles();
213 }
214
215 private function doTestStore( $op ) {
216 $backendName = $this->backendClass();
217
218 $source = $op['src'];
219 $dest = $op['dst'];
220 $this->prepare( array( 'dir' => dirname( $dest ) ) );
221
222 file_put_contents( $source, "Unit test file" );
223
224 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
225 $this->backend->store( $op );
226 }
227
228 $status = $this->backend->doOperation( $op );
229
230 $this->assertGoodStatus( $status,
231 "Store from $source to $dest succeeded without warnings ($backendName)." );
232 $this->assertEquals( true, $status->isOK(),
233 "Store from $source to $dest succeeded ($backendName)." );
234 $this->assertEquals( array( 0 => true ), $status->success,
235 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
236 $this->assertEquals( true, file_exists( $source ),
237 "Source file $source still exists ($backendName)." );
238 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
239 "Destination file $dest exists ($backendName)." );
240
241 $this->assertEquals( filesize( $source ),
242 $this->backend->getFileSize( array( 'src' => $dest ) ),
243 "Destination file $dest has correct size ($backendName)." );
244
245 $props1 = FSFile::getPropsFromPath( $source );
246 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
247 $this->assertEquals( $props1, $props2,
248 "Source and destination have the same props ($backendName)." );
249
250 $this->assertBackendPathsConsistent( array( $dest ) );
251 }
252
253 public function provider_testStore() {
254 $cases = array();
255
256 $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
257 $toPath = $this->baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
258 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
259 $cases[] = array(
260 $op, // operation
261 $tmpName, // source
262 $toPath, // dest
263 );
264
265 $op2 = $op;
266 $op2['overwrite'] = true;
267 $cases[] = array(
268 $op2, // operation
269 $tmpName, // source
270 $toPath, // dest
271 );
272
273 $op2 = $op;
274 $op2['overwriteSame'] = true;
275 $cases[] = array(
276 $op2, // operation
277 $tmpName, // source
278 $toPath, // dest
279 );
280
281 return $cases;
282 }
283
284 /**
285 * @dataProvider provider_testCopy
286 */
287 public function testCopy( $op ) {
288 $this->backend = $this->singleBackend;
289 $this->tearDownFiles();
290 $this->doTestCopy( $op );
291 $this->tearDownFiles();
292
293 $this->backend = $this->multiBackend;
294 $this->tearDownFiles();
295 $this->doTestCopy( $op );
296 $this->tearDownFiles();
297 }
298
299 private function doTestCopy( $op ) {
300 $backendName = $this->backendClass();
301
302 $source = $op['src'];
303 $dest = $op['dst'];
304 $this->prepare( array( 'dir' => dirname( $source ) ) );
305 $this->prepare( array( 'dir' => dirname( $dest ) ) );
306
307 $status = $this->backend->doOperation(
308 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
309 $this->assertGoodStatus( $status,
310 "Creation of file at $source succeeded ($backendName)." );
311
312 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
313 $this->backend->copy( $op );
314 }
315
316 $status = $this->backend->doOperation( $op );
317
318 $this->assertGoodStatus( $status,
319 "Copy from $source to $dest succeeded without warnings ($backendName)." );
320 $this->assertEquals( true, $status->isOK(),
321 "Copy from $source to $dest succeeded ($backendName)." );
322 $this->assertEquals( array( 0 => true ), $status->success,
323 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
324 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
325 "Source file $source still exists ($backendName)." );
326 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
327 "Destination file $dest exists after copy ($backendName)." );
328
329 $this->assertEquals(
330 $this->backend->getFileSize( array( 'src' => $source ) ),
331 $this->backend->getFileSize( array( 'src' => $dest ) ),
332 "Destination file $dest has correct size ($backendName)." );
333
334 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
335 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
336 $this->assertEquals( $props1, $props2,
337 "Source and destination have the same props ($backendName)." );
338
339 $this->assertBackendPathsConsistent( array( $source, $dest ) );
340 }
341
342 public function provider_testCopy() {
343 $cases = array();
344
345 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
346 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
347
348 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
349 $cases[] = array(
350 $op, // operation
351 $source, // source
352 $dest, // dest
353 );
354
355 $op2 = $op;
356 $op2['overwrite'] = true;
357 $cases[] = array(
358 $op2, // operation
359 $source, // source
360 $dest, // dest
361 );
362
363 $op2 = $op;
364 $op2['overwriteSame'] = true;
365 $cases[] = array(
366 $op2, // operation
367 $source, // source
368 $dest, // dest
369 );
370
371 return $cases;
372 }
373
374 /**
375 * @dataProvider provider_testMove
376 */
377 public function testMove( $op ) {
378 $this->backend = $this->singleBackend;
379 $this->tearDownFiles();
380 $this->doTestMove( $op );
381 $this->tearDownFiles();
382
383 $this->backend = $this->multiBackend;
384 $this->tearDownFiles();
385 $this->doTestMove( $op );
386 $this->tearDownFiles();
387 }
388
389 private function doTestMove( $op ) {
390 $backendName = $this->backendClass();
391
392 $source = $op['src'];
393 $dest = $op['dst'];
394 $this->prepare( array( 'dir' => dirname( $source ) ) );
395 $this->prepare( array( 'dir' => dirname( $dest ) ) );
396
397 $status = $this->backend->doOperation(
398 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
399 $this->assertGoodStatus( $status,
400 "Creation of file at $source succeeded ($backendName)." );
401
402 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
403 $this->backend->copy( $op );
404 }
405
406 $status = $this->backend->doOperation( $op );
407 $this->assertGoodStatus( $status,
408 "Move from $source to $dest succeeded without warnings ($backendName)." );
409 $this->assertEquals( true, $status->isOK(),
410 "Move from $source to $dest succeeded ($backendName)." );
411 $this->assertEquals( array( 0 => true ), $status->success,
412 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
413 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
414 "Source file $source does not still exists ($backendName)." );
415 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
416 "Destination file $dest exists after move ($backendName)." );
417
418 $this->assertNotEquals(
419 $this->backend->getFileSize( array( 'src' => $source ) ),
420 $this->backend->getFileSize( array( 'src' => $dest ) ),
421 "Destination file $dest has correct size ($backendName)." );
422
423 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
424 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
425 $this->assertEquals( false, $props1['fileExists'],
426 "Source file does not exist accourding to props ($backendName)." );
427 $this->assertEquals( true, $props2['fileExists'],
428 "Destination file exists accourding to props ($backendName)." );
429
430 $this->assertBackendPathsConsistent( array( $source, $dest ) );
431 }
432
433 public function provider_testMove() {
434 $cases = array();
435
436 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
437 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
438
439 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
440 $cases[] = array(
441 $op, // operation
442 $source, // source
443 $dest, // dest
444 );
445
446 $op2 = $op;
447 $op2['overwrite'] = true;
448 $cases[] = array(
449 $op2, // operation
450 $source, // source
451 $dest, // dest
452 );
453
454 $op2 = $op;
455 $op2['overwriteSame'] = true;
456 $cases[] = array(
457 $op2, // operation
458 $source, // source
459 $dest, // dest
460 );
461
462 return $cases;
463 }
464
465 /**
466 * @dataProvider provider_testDelete
467 */
468 public function testDelete( $op, $withSource, $okStatus ) {
469 $this->backend = $this->singleBackend;
470 $this->tearDownFiles();
471 $this->doTestDelete( $op, $withSource, $okStatus );
472 $this->tearDownFiles();
473
474 $this->backend = $this->multiBackend;
475 $this->tearDownFiles();
476 $this->doTestDelete( $op, $withSource, $okStatus );
477 $this->tearDownFiles();
478 }
479
480 private function doTestDelete( $op, $withSource, $okStatus ) {
481 $backendName = $this->backendClass();
482
483 $source = $op['src'];
484 $this->prepare( array( 'dir' => dirname( $source ) ) );
485
486 if ( $withSource ) {
487 $status = $this->backend->doOperation(
488 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
489 $this->assertGoodStatus( $status,
490 "Creation of file at $source succeeded ($backendName)." );
491 }
492
493 $status = $this->backend->doOperation( $op );
494 if ( $okStatus ) {
495 $this->assertGoodStatus( $status,
496 "Deletion of file at $source succeeded without warnings ($backendName)." );
497 $this->assertEquals( true, $status->isOK(),
498 "Deletion of file at $source succeeded ($backendName)." );
499 $this->assertEquals( array( 0 => true ), $status->success,
500 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
501 } else {
502 $this->assertEquals( false, $status->isOK(),
503 "Deletion of file at $source failed ($backendName)." );
504 }
505
506 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
507 "Source file $source does not exist after move ($backendName)." );
508
509 $this->assertFalse(
510 $this->backend->getFileSize( array( 'src' => $source ) ),
511 "Source file $source has correct size (false) ($backendName)." );
512
513 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
514 $this->assertFalse( $props1['fileExists'],
515 "Source file $source does not exist according to props ($backendName)." );
516
517 $this->assertBackendPathsConsistent( array( $source ) );
518 }
519
520 public function provider_testDelete() {
521 $cases = array();
522
523 $source = $this->baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
524
525 $op = array( 'op' => 'delete', 'src' => $source );
526 $cases[] = array(
527 $op, // operation
528 true, // with source
529 true // succeeds
530 );
531
532 $cases[] = array(
533 $op, // operation
534 false, // without source
535 false // fails
536 );
537
538 $op['ignoreMissingSource'] = true;
539 $cases[] = array(
540 $op, // operation
541 false, // without source
542 true // succeeds
543 );
544
545 return $cases;
546 }
547
548 /**
549 * @dataProvider provider_testCreate
550 */
551 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
552 $this->backend = $this->singleBackend;
553 $this->tearDownFiles();
554 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
555 $this->tearDownFiles();
556
557 $this->backend = $this->multiBackend;
558 $this->tearDownFiles();
559 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
560 $this->tearDownFiles();
561 }
562
563 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
564 $backendName = $this->backendClass();
565
566 $dest = $op['dst'];
567 $this->prepare( array( 'dir' => dirname( $dest ) ) );
568
569 $oldText = 'blah...blah...waahwaah';
570 if ( $alreadyExists ) {
571 $status = $this->backend->doOperation(
572 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
573 $this->assertGoodStatus( $status,
574 "Creation of file at $dest succeeded ($backendName)." );
575 }
576
577 $status = $this->backend->doOperation( $op );
578 if ( $okStatus ) {
579 $this->assertGoodStatus( $status,
580 "Creation of file at $dest succeeded without warnings ($backendName)." );
581 $this->assertEquals( true, $status->isOK(),
582 "Creation of file at $dest succeeded ($backendName)." );
583 $this->assertEquals( array( 0 => true ), $status->success,
584 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
585 } else {
586 $this->assertEquals( false, $status->isOK(),
587 "Creation of file at $dest failed ($backendName)." );
588 }
589
590 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
591 "Destination file $dest exists after creation ($backendName)." );
592
593 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
594 $this->assertEquals( true, $props1['fileExists'],
595 "Destination file $dest exists according to props ($backendName)." );
596 if ( $okStatus ) { // file content is what we saved
597 $this->assertEquals( $newSize, $props1['size'],
598 "Destination file $dest has expected size according to props ($backendName)." );
599 $this->assertEquals( $newSize,
600 $this->backend->getFileSize( array( 'src' => $dest ) ),
601 "Destination file $dest has correct size ($backendName)." );
602 } else { // file content is some other previous text
603 $this->assertEquals( strlen( $oldText ), $props1['size'],
604 "Destination file $dest has original size according to props ($backendName)." );
605 $this->assertEquals( strlen( $oldText ),
606 $this->backend->getFileSize( array( 'src' => $dest ) ),
607 "Destination file $dest has original size according to props ($backendName)." );
608 }
609
610 $this->assertBackendPathsConsistent( array( $dest ) );
611 }
612
613 /**
614 * @dataProvider provider_testCreate
615 */
616 public function provider_testCreate() {
617 $cases = array();
618
619 $dest = $this->baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
620
621 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
622 $cases[] = array(
623 $op, // operation
624 false, // no dest already exists
625 true, // succeeds
626 strlen( $op['content'] )
627 );
628
629 $op2 = $op;
630 $op2['content'] = "\n";
631 $cases[] = array(
632 $op2, // operation
633 false, // no dest already exists
634 true, // succeeds
635 strlen( $op2['content'] )
636 );
637
638 $op2 = $op;
639 $op2['content'] = "fsf\n waf 3kt";
640 $cases[] = array(
641 $op2, // operation
642 true, // dest already exists
643 false, // fails
644 strlen( $op2['content'] )
645 );
646
647 $op2 = $op;
648 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
649 $op2['overwrite'] = true;
650 $cases[] = array(
651 $op2, // operation
652 true, // dest already exists
653 true, // succeeds
654 strlen( $op2['content'] )
655 );
656
657 $op2 = $op;
658 $op2['content'] = "39qjmg3-qg";
659 $op2['overwriteSame'] = true;
660 $cases[] = array(
661 $op2, // operation
662 true, // dest already exists
663 false, // succeeds
664 strlen( $op2['content'] )
665 );
666
667 return $cases;
668 }
669
670 public function testDoQuickOperations() {
671 $this->backend = $this->singleBackend;
672 $this->doTestDoQuickOperations();
673 $this->tearDownFiles();
674
675 $this->backend = $this->multiBackend;
676 $this->doTestDoQuickOperations();
677 $this->tearDownFiles();
678 }
679
680 private function doTestDoQuickOperations() {
681 $backendName = $this->backendClass();
682
683 $base = $this->baseStorePath();
684 $files = array(
685 "$base/unittest-cont1/e/fileA.a",
686 "$base/unittest-cont1/e/fileB.a",
687 "$base/unittest-cont1/e/fileC.a"
688 );
689 $ops = array();
690 $purgeOps = array();
691 foreach ( $files as $path ) {
692 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
693 $this->assertGoodStatus( $status,
694 "Preparing $path succeeded without warnings ($backendName)." );
695 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
696 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
697 }
698 $purgeOps[] = array( 'op' => 'null' );
699 $status = $this->backend->doQuickOperations( $ops );
700 $this->assertGoodStatus( $status,
701 "Creation of source files succeeded ($backendName)." );
702
703 foreach ( $files as $file ) {
704 $this->assertTrue( $this->backend->fileExists( array( 'src' => $file ) ),
705 "File $file exists." );
706 }
707
708 $status = $this->backend->doQuickOperations( $purgeOps );
709 $this->assertGoodStatus( $status,
710 "Quick deletion of source files succeeded ($backendName)." );
711
712 foreach ( $files as $file ) {
713 $this->assertFalse( $this->backend->fileExists( array( 'src' => $file ) ),
714 "File $file purged." );
715 }
716 }
717
718 /**
719 * @dataProvider provider_testConcatenate
720 */
721 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
722 $this->filesToPrune[] = $op['dst'];
723
724 $this->backend = $this->singleBackend;
725 $this->tearDownFiles();
726 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
727 $this->tearDownFiles();
728
729 $this->backend = $this->multiBackend;
730 $this->tearDownFiles();
731 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
732 $this->filesToPrune[] = $op['dst']; # avoid file leaking
733 $this->tearDownFiles();
734 }
735
736 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
737 $backendName = $this->backendClass();
738
739 $expContent = '';
740 // Create sources
741 $ops = array();
742 foreach ( $srcs as $i => $source ) {
743 $this->prepare( array( 'dir' => dirname( $source ) ) );
744 $ops[] = array(
745 'op' => 'create', // operation
746 'dst' => $source, // source
747 'content' => $srcsContent[$i]
748 );
749 $expContent .= $srcsContent[$i];
750 }
751 $status = $this->backend->doOperations( $ops );
752
753 $this->assertGoodStatus( $status,
754 "Creation of source files succeeded ($backendName)." );
755
756 $dest = $params['dst'];
757 if ( $alreadyExists ) {
758 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
759 $this->assertEquals( true, $ok,
760 "Creation of file at $dest succeeded ($backendName)." );
761 } else {
762 $ok = file_put_contents( $dest, '' ) !== false;
763 $this->assertEquals( true, $ok,
764 "Creation of 0-byte file at $dest succeeded ($backendName)." );
765 }
766
767 // Combine the files into one
768 $status = $this->backend->concatenate( $params );
769 if ( $okStatus ) {
770 $this->assertGoodStatus( $status,
771 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
772 $this->assertEquals( true, $status->isOK(),
773 "Creation of concat file at $dest succeeded ($backendName)." );
774 } else {
775 $this->assertEquals( false, $status->isOK(),
776 "Creation of concat file at $dest failed ($backendName)." );
777 }
778
779 if ( $okStatus ) {
780 $this->assertEquals( true, is_file( $dest ),
781 "Dest concat file $dest exists after creation ($backendName)." );
782 } else {
783 $this->assertEquals( true, is_file( $dest ),
784 "Dest concat file $dest exists after failed creation ($backendName)." );
785 }
786
787 $contents = file_get_contents( $dest );
788 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
789
790 if ( $okStatus ) {
791 $this->assertEquals( $expContent, $contents,
792 "Concat file at $dest has correct contents ($backendName)." );
793 } else {
794 $this->assertNotEquals( $expContent, $contents,
795 "Concat file at $dest has correct contents ($backendName)." );
796 }
797 }
798
799 function provider_testConcatenate() {
800 $cases = array();
801
802 $rand = mt_rand( 0, 2000000000 ) . time();
803 $dest = wfTempDir() . "/randomfile!$rand.txt";
804 $srcs = array(
805 $this->baseStorePath() . '/unittest-cont1/e/file1.txt',
806 $this->baseStorePath() . '/unittest-cont1/e/file2.txt',
807 $this->baseStorePath() . '/unittest-cont1/e/file3.txt',
808 $this->baseStorePath() . '/unittest-cont1/e/file4.txt',
809 $this->baseStorePath() . '/unittest-cont1/e/file5.txt',
810 $this->baseStorePath() . '/unittest-cont1/e/file6.txt',
811 $this->baseStorePath() . '/unittest-cont1/e/file7.txt',
812 $this->baseStorePath() . '/unittest-cont1/e/file8.txt',
813 $this->baseStorePath() . '/unittest-cont1/e/file9.txt',
814 $this->baseStorePath() . '/unittest-cont1/e/file10.txt'
815 );
816 $content = array(
817 'egfage',
818 'ageageag',
819 'rhokohlr',
820 'shgmslkg',
821 'kenga',
822 'owagmal',
823 'kgmae',
824 'g eak;g',
825 'lkaem;a',
826 'legma'
827 );
828 $params = array( 'srcs' => $srcs, 'dst' => $dest );
829
830 $cases[] = array(
831 $params, // operation
832 $srcs, // sources
833 $content, // content for each source
834 false, // no dest already exists
835 true, // succeeds
836 );
837
838 $cases[] = array(
839 $params, // operation
840 $srcs, // sources
841 $content, // content for each source
842 true, // dest already exists
843 false, // succeeds
844 );
845
846 return $cases;
847 }
848
849 /**
850 * @dataProvider provider_testGetFileStat
851 */
852 public function testGetFileStat( $path, $content, $alreadyExists ) {
853 $this->backend = $this->singleBackend;
854 $this->tearDownFiles();
855 $this->doTestGetFileStat( $path, $content, $alreadyExists );
856 $this->tearDownFiles();
857
858 $this->backend = $this->multiBackend;
859 $this->tearDownFiles();
860 $this->doTestGetFileStat( $path, $content, $alreadyExists );
861 $this->tearDownFiles();
862 }
863
864 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
865 $backendName = $this->backendClass();
866
867 if ( $alreadyExists ) {
868 $this->prepare( array( 'dir' => dirname( $path ) ) );
869 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
870 $this->assertGoodStatus( $status,
871 "Creation of file at $path succeeded ($backendName)." );
872
873 $size = $this->backend->getFileSize( array( 'src' => $path ) );
874 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
875 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
876
877 $this->assertEquals( strlen( $content ), $size,
878 "Correct file size of '$path'" );
879 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
880 "Correct file timestamp of '$path'" );
881
882 $size = $stat['size'];
883 $time = $stat['mtime'];
884 $this->assertEquals( strlen( $content ), $size,
885 "Correct file size of '$path'" );
886 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
887 "Correct file timestamp of '$path'" );
888 } else {
889 $size = $this->backend->getFileSize( array( 'src' => $path ) );
890 $time = $this->backend->getFileTimestamp( array( 'src' => $path ) );
891 $stat = $this->backend->getFileStat( array( 'src' => $path ) );
892
893 $this->assertFalse( $size, "Correct file size of '$path'" );
894 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
895 $this->assertFalse( $stat, "Correct file stat of '$path'" );
896 }
897 }
898
899 function provider_testGetFileStat() {
900 $cases = array();
901
902 $base = $this->baseStorePath();
903 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
904 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
905 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
906
907 return $cases;
908 }
909
910 /**
911 * @dataProvider provider_testGetFileContents
912 */
913 public function testGetFileContents( $source, $content ) {
914 $this->backend = $this->singleBackend;
915 $this->tearDownFiles();
916 $this->doTestGetFileContents( $source, $content );
917 $this->tearDownFiles();
918
919 $this->backend = $this->multiBackend;
920 $this->tearDownFiles();
921 $this->doTestGetFileContents( $source, $content );
922 $this->tearDownFiles();
923 }
924
925 private function doTestGetFileContents( $source, $content ) {
926 $backendName = $this->backendClass();
927
928 $this->prepare( array( 'dir' => dirname( $source ) ) );
929
930 $status = $this->backend->doOperation(
931 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
932 $this->assertGoodStatus( $status,
933 "Creation of file at $source succeeded ($backendName)." );
934 $this->assertEquals( true, $status->isOK(),
935 "Creation of file at $source succeeded with OK status ($backendName)." );
936
937 $newContents = $this->backend->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
938 $this->assertNotEquals( false, $newContents,
939 "Read of file at $source succeeded ($backendName)." );
940
941 $this->assertEquals( $content, $newContents,
942 "Contents read match data at $source ($backendName)." );
943 }
944
945 function provider_testGetFileContents() {
946 $cases = array();
947
948 $base = $this->baseStorePath();
949 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
950 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
951
952 return $cases;
953 }
954
955 /**
956 * @dataProvider provider_testGetLocalCopy
957 */
958 public function testGetLocalCopy( $source, $content ) {
959 $this->backend = $this->singleBackend;
960 $this->tearDownFiles();
961 $this->doTestGetLocalCopy( $source, $content );
962 $this->tearDownFiles();
963
964 $this->backend = $this->multiBackend;
965 $this->tearDownFiles();
966 $this->doTestGetLocalCopy( $source, $content );
967 $this->tearDownFiles();
968 }
969
970 private function doTestGetLocalCopy( $source, $content ) {
971 $backendName = $this->backendClass();
972
973 $this->prepare( array( 'dir' => dirname( $source ) ) );
974
975 $status = $this->backend->doOperation(
976 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
977 $this->assertGoodStatus( $status,
978 "Creation of file at $source succeeded ($backendName)." );
979
980 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $source ) );
981 $this->assertNotNull( $tmpFile,
982 "Creation of local copy of $source succeeded ($backendName)." );
983
984 $contents = file_get_contents( $tmpFile->getPath() );
985 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
986 }
987
988 function provider_testGetLocalCopy() {
989 $cases = array();
990
991 $base = $this->baseStorePath();
992 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
993 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
994
995 return $cases;
996 }
997
998 /**
999 * @dataProvider provider_testGetLocalReference
1000 */
1001 public function testGetLocalReference( $source, $content ) {
1002 $this->backend = $this->singleBackend;
1003 $this->tearDownFiles();
1004 $this->doTestGetLocalReference( $source, $content );
1005 $this->tearDownFiles();
1006
1007 $this->backend = $this->multiBackend;
1008 $this->tearDownFiles();
1009 $this->doTestGetLocalReference( $source, $content );
1010 $this->tearDownFiles();
1011 }
1012
1013 private function doTestGetLocalReference( $source, $content ) {
1014 $backendName = $this->backendClass();
1015
1016 $this->prepare( array( 'dir' => dirname( $source ) ) );
1017
1018 $status = $this->create( array( 'content' => $content, 'dst' => $source ) );
1019 $this->assertGoodStatus( $status,
1020 "Creation of file at $source succeeded ($backendName)." );
1021
1022 $tmpFile = $this->backend->getLocalReference( array( 'src' => $source ) );
1023 $this->assertNotNull( $tmpFile,
1024 "Creation of local copy of $source succeeded ($backendName)." );
1025
1026 $contents = file_get_contents( $tmpFile->getPath() );
1027 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1028 }
1029
1030 function provider_testGetLocalReference() {
1031 $cases = array();
1032
1033 $base = $this->baseStorePath();
1034 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1035 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1036
1037 return $cases;
1038 }
1039
1040 /**
1041 * @dataProvider provider_testPrepareAndClean
1042 */
1043 public function testPrepareAndClean( $path, $isOK ) {
1044 $this->backend = $this->singleBackend;
1045 $this->doTestPrepareAndClean( $path, $isOK );
1046 $this->tearDownFiles();
1047
1048 $this->backend = $this->multiBackend;
1049 $this->doTestPrepareAndClean( $path, $isOK );
1050 $this->tearDownFiles();
1051 }
1052
1053 function provider_testPrepareAndClean() {
1054 $base = $this->baseStorePath();
1055 return array(
1056 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1057 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1058 # Specific to FS backend with no basePath field set
1059 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1060 );
1061 }
1062
1063 private function doTestPrepareAndClean( $path, $isOK ) {
1064 $backendName = $this->backendClass();
1065
1066 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1067 if ( $isOK ) {
1068 $this->assertGoodStatus( $status,
1069 "Preparing dir $path succeeded without warnings ($backendName)." );
1070 $this->assertEquals( true, $status->isOK(),
1071 "Preparing dir $path succeeded ($backendName)." );
1072 } else {
1073 $this->assertEquals( false, $status->isOK(),
1074 "Preparing dir $path failed ($backendName)." );
1075 }
1076
1077 $status = $this->backend->clean( array( 'dir' => dirname( $path ) ) );
1078 if ( $isOK ) {
1079 $this->assertGoodStatus( $status,
1080 "Cleaning dir $path succeeded without warnings ($backendName)." );
1081 $this->assertEquals( true, $status->isOK(),
1082 "Cleaning dir $path succeeded ($backendName)." );
1083 } else {
1084 $this->assertEquals( false, $status->isOK(),
1085 "Cleaning dir $path failed ($backendName)." );
1086 }
1087 }
1088
1089 public function testRecursiveClean() {
1090 $this->backend = $this->singleBackend;
1091 $this->doTestRecursiveClean();
1092 $this->tearDownFiles();
1093
1094 $this->backend = $this->multiBackend;
1095 $this->doTestRecursiveClean();
1096 $this->tearDownFiles();
1097 }
1098
1099 private function doTestRecursiveClean() {
1100 $backendName = $this->backendClass();
1101
1102 $base = $this->baseStorePath();
1103 $dirs = array(
1104 "$base/unittest-cont1/e/a",
1105 "$base/unittest-cont1/e/a/b",
1106 "$base/unittest-cont1/e/a/b/c",
1107 "$base/unittest-cont1/e/a/b/c/d0",
1108 "$base/unittest-cont1/e/a/b/c/d1",
1109 "$base/unittest-cont1/e/a/b/c/d2",
1110 "$base/unittest-cont1/e/a/b/c/d0/1",
1111 "$base/unittest-cont1/e/a/b/c/d0/2",
1112 "$base/unittest-cont1/e/a/b/c/d1/3",
1113 "$base/unittest-cont1/e/a/b/c/d1/4",
1114 "$base/unittest-cont1/e/a/b/c/d2/5",
1115 "$base/unittest-cont1/e/a/b/c/d2/6"
1116 );
1117 foreach ( $dirs as $dir ) {
1118 $status = $this->prepare( array( 'dir' => $dir ) );
1119 $this->assertGoodStatus( $status,
1120 "Preparing dir $dir succeeded without warnings ($backendName)." );
1121 }
1122
1123 if ( $this->backend instanceof FSFileBackend ) {
1124 foreach ( $dirs as $dir ) {
1125 $this->assertEquals( true, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1126 "Dir $dir exists ($backendName)." );
1127 }
1128 }
1129
1130 $status = $this->backend->clean(
1131 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1132 $this->assertGoodStatus( $status,
1133 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1134
1135 foreach ( $dirs as $dir ) {
1136 $this->assertEquals( false, $this->backend->directoryExists( array( 'dir' => $dir ) ),
1137 "Dir $dir no longer exists ($backendName)." );
1138 }
1139 }
1140
1141 // @TODO: testSecure
1142
1143 public function testDoOperations() {
1144 $this->backend = $this->singleBackend;
1145 $this->tearDownFiles();
1146 $this->doTestDoOperations();
1147 $this->tearDownFiles();
1148
1149 $this->backend = $this->multiBackend;
1150 $this->tearDownFiles();
1151 $this->doTestDoOperations();
1152 $this->tearDownFiles();
1153 }
1154
1155 private function doTestDoOperations() {
1156 $base = $this->baseStorePath();
1157
1158 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1159 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1160 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1161 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1162 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1163 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1164 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1165
1166 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1167 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1168 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1169 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1170 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1171 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1172 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1173
1174 $status = $this->backend->doOperations( array(
1175 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1176 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1177 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1178 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1179 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1180 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1181 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1182 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1183 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1184 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1185 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1186 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1187 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1188 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1189 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1190 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1191 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1192 // Does nothing
1193 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1194 // Does nothing
1195 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1196 // Does nothing
1197 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1198 // Does nothing
1199 array( 'op' => 'null' ),
1200 // Does nothing
1201 ) );
1202
1203 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1204 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1205 $this->assertEquals( 13, count( $status->success ),
1206 "Operation batch has correct success array" );
1207
1208 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1209 "File does not exist at $fileA" );
1210 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1211 "File does not exist at $fileB" );
1212 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1213 "File does not exist at $fileD" );
1214
1215 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1216 "File exists at $fileC" );
1217 $this->assertEquals( $fileBContents,
1218 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1219 "Correct file contents of $fileC" );
1220 $this->assertEquals( strlen( $fileBContents ),
1221 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1222 "Correct file size of $fileC" );
1223 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1224 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1225 "Correct file SHA-1 of $fileC" );
1226 }
1227
1228 public function testDoOperationsPipeline() {
1229 $this->backend = $this->singleBackend;
1230 $this->tearDownFiles();
1231 $this->doTestDoOperationsPipeline();
1232 $this->tearDownFiles();
1233
1234 $this->backend = $this->multiBackend;
1235 $this->tearDownFiles();
1236 $this->doTestDoOperationsPipeline();
1237 $this->tearDownFiles();
1238 }
1239
1240 // concurrency orientated
1241 private function doTestDoOperationsPipeline() {
1242 $base = $this->baseStorePath();
1243
1244 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1245 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1246 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1247
1248 $tmpNameA = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1249 file_put_contents( $tmpNameA, $fileAContents );
1250 $tmpNameB = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1251 file_put_contents( $tmpNameB, $fileBContents );
1252 $tmpNameC = TempFSFile::factory( "unittests_", 'txt' )->getPath();
1253 file_put_contents( $tmpNameC, $fileCContents );
1254
1255 $this->filesToPrune[] = $tmpNameA; # avoid file leaking
1256 $this->filesToPrune[] = $tmpNameB; # avoid file leaking
1257 $this->filesToPrune[] = $tmpNameC; # avoid file leaking
1258
1259 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1260 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1261 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1262 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1263
1264 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1265 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1266 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1267 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1268 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1269
1270 $status = $this->backend->doOperations( array(
1271 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1272 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1273 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1274 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1275 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1276 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1277 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1278 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1279 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1280 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1281 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1282 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1283 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1284 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1285 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1286 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1287 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1288 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1289 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1290 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1291 // Does nothing
1292 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1293 // Does nothing
1294 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1295 // Does nothing
1296 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1297 // Does nothing
1298 array( 'op' => 'null' ),
1299 // Does nothing
1300 ) );
1301
1302 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1303 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1304 $this->assertEquals( 16, count( $status->success ),
1305 "Operation batch has correct success array" );
1306
1307 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
1308 "File does not exist at $fileA" );
1309 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1310 "File does not exist at $fileB" );
1311 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1312 "File does not exist at $fileD" );
1313
1314 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1315 "File exists at $fileC" );
1316 $this->assertEquals( $fileBContents,
1317 $this->backend->getFileContents( array( 'src' => $fileC ) ),
1318 "Correct file contents of $fileC" );
1319 $this->assertEquals( strlen( $fileBContents ),
1320 $this->backend->getFileSize( array( 'src' => $fileC ) ),
1321 "Correct file size of $fileC" );
1322 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1323 $this->backend->getFileSha1Base36( array( 'src' => $fileC ) ),
1324 "Correct file SHA-1 of $fileC" );
1325 }
1326
1327 public function testDoOperationsFailing() {
1328 $this->backend = $this->singleBackend;
1329 $this->tearDownFiles();
1330 $this->doTestDoOperationsFailing();
1331 $this->tearDownFiles();
1332
1333 $this->backend = $this->multiBackend;
1334 $this->tearDownFiles();
1335 $this->doTestDoOperationsFailing();
1336 $this->tearDownFiles();
1337 }
1338
1339 private function doTestDoOperationsFailing() {
1340 $base = $this->baseStorePath();
1341
1342 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1343 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1344 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1345 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1346 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1347 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1348 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1349
1350 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1351 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1352 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1353 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1354 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1355 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1356
1357 $status = $this->backend->doOperations( array(
1358 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1359 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1360 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1361 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1362 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1363 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1364 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1365 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1366 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1367 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1368 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1369 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1370 array( 'op' => 'delete', 'src' => $fileD ),
1371 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1372 array( 'op' => 'null' ),
1373 // Does nothing
1374 ), array( 'force' => 1 ) );
1375
1376 $this->assertNotEquals( array(), $status->errors, "Operation had warnings" );
1377 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1378 $this->assertEquals( 8, count( $status->success ),
1379 "Operation batch has correct success array" );
1380
1381 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileB ) ),
1382 "File does not exist at $fileB" );
1383 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileD ) ),
1384 "File does not exist at $fileD" );
1385
1386 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileA ) ),
1387 "File does not exist at $fileA" );
1388 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $fileC ) ),
1389 "File exists at $fileC" );
1390 $this->assertEquals( $fileBContents,
1391 $this->backend->getFileContents( array( 'src' => $fileA ) ),
1392 "Correct file contents of $fileA" );
1393 $this->assertEquals( strlen( $fileBContents ),
1394 $this->backend->getFileSize( array( 'src' => $fileA ) ),
1395 "Correct file size of $fileA" );
1396 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1397 $this->backend->getFileSha1Base36( array( 'src' => $fileA ) ),
1398 "Correct file SHA-1 of $fileA" );
1399 }
1400
1401 public function testGetFileList() {
1402 $this->backend = $this->singleBackend;
1403 $this->tearDownFiles();
1404 $this->doTestGetFileList();
1405 $this->tearDownFiles();
1406
1407 $this->backend = $this->multiBackend;
1408 $this->tearDownFiles();
1409 $this->doTestGetFileList();
1410 $this->tearDownFiles();
1411 }
1412
1413 private function doTestGetFileList() {
1414 $backendName = $this->backendClass();
1415 $base = $this->baseStorePath();
1416
1417 // Should have no errors
1418 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1419
1420 $files = array(
1421 "$base/unittest-cont1/e/test1.txt",
1422 "$base/unittest-cont1/e/test2.txt",
1423 "$base/unittest-cont1/e/test3.txt",
1424 "$base/unittest-cont1/e/subdir1/test1.txt",
1425 "$base/unittest-cont1/e/subdir1/test2.txt",
1426 "$base/unittest-cont1/e/subdir2/test3.txt",
1427 "$base/unittest-cont1/e/subdir2/test4.txt",
1428 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1429 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1430 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1431 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1432 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1433 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1434 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1435 );
1436
1437 // Add the files
1438 $ops = array();
1439 foreach ( $files as $file ) {
1440 $this->prepare( array( 'dir' => dirname( $file ) ) );
1441 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1442 }
1443 $status = $this->backend->doQuickOperations( $ops );
1444 $this->assertGoodStatus( $status,
1445 "Creation of files succeeded ($backendName)." );
1446 $this->assertEquals( true, $status->isOK(),
1447 "Creation of files succeeded with OK status ($backendName)." );
1448
1449 // Expected listing
1450 $expected = array(
1451 "e/test1.txt",
1452 "e/test2.txt",
1453 "e/test3.txt",
1454 "e/subdir1/test1.txt",
1455 "e/subdir1/test2.txt",
1456 "e/subdir2/test3.txt",
1457 "e/subdir2/test4.txt",
1458 "e/subdir2/subdir/test1.txt",
1459 "e/subdir2/subdir/test2.txt",
1460 "e/subdir2/subdir/test3.txt",
1461 "e/subdir2/subdir/test4.txt",
1462 "e/subdir2/subdir/test5.txt",
1463 "e/subdir2/subdir/sub/test0.txt",
1464 "e/subdir2/subdir/sub/120-px-file.txt",
1465 );
1466 sort( $expected );
1467
1468 // Actual listing (no trailing slash)
1469 $list = array();
1470 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1471 foreach ( $iter as $file ) {
1472 $list[] = $file;
1473 }
1474 sort( $list );
1475
1476 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1477
1478 // Actual listing (with trailing slash)
1479 $list = array();
1480 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1481 foreach ( $iter as $file ) {
1482 $list[] = $file;
1483 }
1484 sort( $list );
1485
1486 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1487
1488 // Expected listing
1489 $expected = array(
1490 "test1.txt",
1491 "test2.txt",
1492 "test3.txt",
1493 "test4.txt",
1494 "test5.txt",
1495 "sub/test0.txt",
1496 "sub/120-px-file.txt",
1497 );
1498 sort( $expected );
1499
1500 // Actual listing (no trailing slash)
1501 $list = array();
1502 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1503 foreach ( $iter as $file ) {
1504 $list[] = $file;
1505 }
1506 sort( $list );
1507
1508 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1509
1510 // Actual listing (with trailing slash)
1511 $list = array();
1512 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1513 foreach ( $iter as $file ) {
1514 $list[] = $file;
1515 }
1516 sort( $list );
1517
1518 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1519
1520 // Actual listing (using iterator second time)
1521 $list = array();
1522 foreach ( $iter as $file ) {
1523 $list[] = $file;
1524 }
1525 sort( $list );
1526
1527 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1528
1529 // Expected listing (top files only)
1530 $expected = array(
1531 "test1.txt",
1532 "test2.txt",
1533 "test3.txt",
1534 "test4.txt",
1535 "test5.txt"
1536 );
1537 sort( $expected );
1538
1539 // Actual listing (top files only)
1540 $list = array();
1541 $iter = $this->backend->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1542 foreach ( $iter as $file ) {
1543 $list[] = $file;
1544 }
1545 sort( $list );
1546
1547 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1548
1549 foreach ( $files as $file ) { // clean up
1550 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1551 }
1552
1553 $iter = $this->backend->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1554 foreach ( $iter as $iter ) {} // no errors
1555 }
1556
1557 public function testGetDirectoryList() {
1558 $this->backend = $this->singleBackend;
1559 $this->tearDownFiles();
1560 $this->doTestGetDirectoryList();
1561 $this->tearDownFiles();
1562
1563 $this->backend = $this->multiBackend;
1564 $this->tearDownFiles();
1565 $this->doTestGetDirectoryList();
1566 $this->tearDownFiles();
1567 }
1568
1569 private function doTestGetDirectoryList() {
1570 $backendName = $this->backendClass();
1571
1572 $base = $this->baseStorePath();
1573 $files = array(
1574 "$base/unittest-cont1/e/test1.txt",
1575 "$base/unittest-cont1/e/test2.txt",
1576 "$base/unittest-cont1/e/test3.txt",
1577 "$base/unittest-cont1/e/subdir1/test1.txt",
1578 "$base/unittest-cont1/e/subdir1/test2.txt",
1579 "$base/unittest-cont1/e/subdir2/test3.txt",
1580 "$base/unittest-cont1/e/subdir2/test4.txt",
1581 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1582 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1583 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1584 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1585 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1586 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1587 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1588 );
1589
1590 // Add the files
1591 $ops = array();
1592 foreach ( $files as $file ) {
1593 $this->prepare( array( 'dir' => dirname( $file ) ) );
1594 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1595 }
1596 $status = $this->backend->doQuickOperations( $ops );
1597 $this->assertGoodStatus( $status,
1598 "Creation of files succeeded ($backendName)." );
1599 $this->assertEquals( true, $status->isOK(),
1600 "Creation of files succeeded with OK status ($backendName)." );
1601
1602 $this->assertEquals( true,
1603 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1604 "Directory exists in ($backendName)." );
1605 $this->assertEquals( true,
1606 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1607 "Directory exists in ($backendName)." );
1608 $this->assertEquals( false,
1609 $this->backend->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1610 "Directory does not exists in ($backendName)." );
1611
1612 // Expected listing
1613 $expected = array(
1614 "e",
1615 );
1616 sort( $expected );
1617
1618 // Actual listing (no trailing slash)
1619 $list = array();
1620 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1621 foreach ( $iter as $file ) {
1622 $list[] = $file;
1623 }
1624 sort( $list );
1625
1626 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1627
1628 // Expected listing
1629 $expected = array(
1630 "subdir1",
1631 "subdir2",
1632 "subdir3",
1633 "subdir4",
1634 );
1635 sort( $expected );
1636
1637 // Actual listing (no trailing slash)
1638 $list = array();
1639 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1640 foreach ( $iter as $file ) {
1641 $list[] = $file;
1642 }
1643 sort( $list );
1644
1645 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1646
1647 // Actual listing (with trailing slash)
1648 $list = array();
1649 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1650 foreach ( $iter as $file ) {
1651 $list[] = $file;
1652 }
1653 sort( $list );
1654
1655 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1656
1657 // Expected listing
1658 $expected = array(
1659 "subdir",
1660 );
1661 sort( $expected );
1662
1663 // Actual listing (no trailing slash)
1664 $list = array();
1665 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1666 foreach ( $iter as $file ) {
1667 $list[] = $file;
1668 }
1669 sort( $list );
1670
1671 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1672
1673 // Actual listing (with trailing slash)
1674 $list = array();
1675 $iter = $this->backend->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1676 foreach ( $iter as $file ) {
1677 $list[] = $file;
1678 }
1679 sort( $list );
1680
1681 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1682
1683 // Actual listing (using iterator second time)
1684 $list = array();
1685 foreach ( $iter as $file ) {
1686 $list[] = $file;
1687 }
1688 sort( $list );
1689
1690 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1691
1692 // Expected listing (recursive)
1693 $expected = array(
1694 "e",
1695 "e/subdir1",
1696 "e/subdir2",
1697 "e/subdir3",
1698 "e/subdir4",
1699 "e/subdir2/subdir",
1700 "e/subdir3/subdir",
1701 "e/subdir4/subdir",
1702 "e/subdir4/subdir/sub",
1703 );
1704 sort( $expected );
1705
1706 // Actual listing (recursive)
1707 $list = array();
1708 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1709 foreach ( $iter as $file ) {
1710 $list[] = $file;
1711 }
1712 sort( $list );
1713
1714 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1715
1716 // Expected listing (recursive)
1717 $expected = array(
1718 "subdir",
1719 "subdir/sub",
1720 );
1721 sort( $expected );
1722
1723 // Actual listing (recursive)
1724 $list = array();
1725 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1726 foreach ( $iter as $file ) {
1727 $list[] = $file;
1728 }
1729 sort( $list );
1730
1731 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1732
1733 // Actual listing (recursive, second time)
1734 $list = array();
1735 foreach ( $iter as $file ) {
1736 $list[] = $file;
1737 }
1738 sort( $list );
1739
1740 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1741
1742 foreach ( $files as $file ) { // clean up
1743 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1744 }
1745
1746 $iter = $this->backend->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1747 foreach ( $iter as $iter ) {} // no errors
1748 }
1749
1750 public function testLockCalls() {
1751 $this->backend = $this->singleBackend;
1752 $this->doTestLockCalls();
1753 }
1754
1755 private function doTestLockCalls() {
1756 $backendName = $this->backendClass();
1757
1758 for ( $i=0; $i<50; $i++ ) {
1759 $paths = array(
1760 "test1.txt",
1761 "test2.txt",
1762 "test3.txt",
1763 "subdir1",
1764 "subdir1", // duplicate
1765 "subdir1/test1.txt",
1766 "subdir1/test2.txt",
1767 "subdir2",
1768 "subdir2", // duplicate
1769 "subdir2/test3.txt",
1770 "subdir2/test4.txt",
1771 "subdir2/subdir",
1772 "subdir2/subdir/test1.txt",
1773 "subdir2/subdir/test2.txt",
1774 "subdir2/subdir/test3.txt",
1775 "subdir2/subdir/test4.txt",
1776 "subdir2/subdir/test5.txt",
1777 "subdir2/subdir/sub",
1778 "subdir2/subdir/sub/test0.txt",
1779 "subdir2/subdir/sub/120-px-file.txt",
1780 );
1781
1782 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
1783 $this->assertEquals( array(), $status->errors,
1784 "Locking of files succeeded ($backendName)." );
1785 $this->assertEquals( true, $status->isOK(),
1786 "Locking of files succeeded with OK status ($backendName)." );
1787
1788 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
1789 $this->assertEquals( array(), $status->errors,
1790 "Locking of files succeeded ($backendName)." );
1791 $this->assertEquals( true, $status->isOK(),
1792 "Locking of files succeeded with OK status ($backendName)." );
1793
1794 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
1795 $this->assertEquals( array(), $status->errors,
1796 "Locking of files succeeded ($backendName)." );
1797 $this->assertEquals( true, $status->isOK(),
1798 "Locking of files succeeded with OK status ($backendName)." );
1799
1800 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
1801 $this->assertEquals( array(), $status->errors,
1802 "Locking of files succeeded ($backendName)." );
1803 $this->assertEquals( true, $status->isOK(),
1804 "Locking of files succeeded with OK status ($backendName)." );
1805 }
1806 }
1807
1808 // test helper wrapper for backend prepare() function
1809 private function prepare( array $params ) {
1810 return $this->backend->prepare( $params );
1811 }
1812
1813 // test helper wrapper for backend prepare() function
1814 private function create( array $params ) {
1815 $params['op'] = 'create';
1816 return $this->backend->doQuickOperations( array( $params ) );
1817 }
1818
1819 function tearDownFiles() {
1820 foreach ( $this->filesToPrune as $file ) {
1821 @unlink( $file );
1822 }
1823 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1824 foreach ( $containers as $container ) {
1825 $this->deleteFiles( $container );
1826 }
1827 $this->filesToPrune = array();
1828 }
1829
1830 private function deleteFiles( $container ) {
1831 $base = $this->baseStorePath();
1832 $iter = $this->backend->getFileList( array( 'dir' => "$base/$container" ) );
1833 if ( $iter ) {
1834 foreach ( $iter as $file ) {
1835 $this->backend->delete( array( 'src' => "$base/$container/$file" ),
1836 array( 'force' => 1, 'nonLocking' => 1 ) );
1837 }
1838 }
1839 $this->backend->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1840 }
1841
1842 function assertBackendPathsConsistent( array $paths ) {
1843 if ( $this->backend instanceof FileBackendMultiWrite ) {
1844 $status = $this->backend->consistencyCheck( $paths );
1845 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1846 }
1847 }
1848
1849 function assertGoodStatus( $status, $msg ) {
1850 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors, 1 ), $msg );
1851 }
1852
1853 function tearDown() {
1854 parent::tearDown();
1855 }
1856 }