7 class FileBackendTest
extends MediaWikiTestCase
{
8 private $backend, $multiBackend;
9 private $filesToPrune = array();
10 private static $backendToUse;
13 global $wgFileBackends;
15 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
16 if ( $this->getCliArg( 'use-filebackend=' ) ) {
17 if ( self
::$backendToUse ) {
18 $this->singleBackend
= self
::$backendToUse;
20 $name = $this->getCliArg( 'use-filebackend=' );
22 foreach ( $wgFileBackends as $conf ) {
23 if ( $conf['name'] == $name ) {
28 $useConfig['name'] = 'localtesting'; // swap name
29 $class = $useConfig['class'];
30 self
::$backendToUse = new $class( $useConfig );
31 $this->singleBackend
= self
::$backendToUse;
34 $this->singleBackend
= new FSFileBackend( array(
35 'name' => 'localtesting',
36 'lockManager' => 'fsLockManager',
37 #'parallelize' => 'implicit',
38 'containerPaths' => array(
39 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
40 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
43 $this->multiBackend
= new FileBackendMultiWrite( array(
44 'name' => 'localtesting',
45 'lockManager' => 'fsLockManager',
46 'parallelize' => 'implicit',
49 'name' => 'localmutlitesting1',
50 'class' => 'FSFileBackend',
51 'lockManager' => 'nullLockManager',
52 'containerPaths' => array(
53 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
54 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
55 'isMultiMaster' => false
58 'name' => 'localmutlitesting2',
59 'class' => 'FSFileBackend',
60 'lockManager' => 'nullLockManager',
61 'containerPaths' => array(
62 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
63 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
64 'isMultiMaster' => true
68 $this->filesToPrune
= array();
71 private function baseStorePath() {
72 return 'mwstore://localtesting';
75 private function backendClass() {
76 return get_class( $this->backend
);
80 * @dataProvider provider_testIsStoragePath
82 public function testIsStoragePath( $path, $isStorePath ) {
83 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
84 "FileBackend::isStoragePath on path '$path'" );
87 function provider_testIsStoragePath() {
89 array( 'mwstore://', true ),
90 array( 'mwstore://backend', true ),
91 array( 'mwstore://backend/container', true ),
92 array( 'mwstore://backend/container/', true ),
93 array( 'mwstore://backend/container/path', true ),
94 array( 'mwstore://backend//container/', true ),
95 array( 'mwstore://backend//container//', true ),
96 array( 'mwstore://backend//container//path', true ),
97 array( 'mwstore:///', true ),
98 array( 'mwstore:/', false ),
99 array( 'mwstore:', false ),
104 * @dataProvider provider_testSplitStoragePath
106 public function testSplitStoragePath( $path, $res ) {
107 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
108 "FileBackend::splitStoragePath on path '$path'" );
111 function provider_testSplitStoragePath() {
113 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
114 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
115 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
116 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
117 array( 'mwstore://backend//container/path', array( null, null, null ) ),
118 array( 'mwstore://backend//container//path', array( null, null, null ) ),
119 array( 'mwstore://', array( null, null, null ) ),
120 array( 'mwstore://backend', array( null, null, null ) ),
121 array( 'mwstore:///', array( null, null, null ) ),
122 array( 'mwstore:/', array( null, null, null ) ),
123 array( 'mwstore:', array( null, null, null ) )
128 * @dataProvider provider_normalizeStoragePath
130 public function testNormalizeStoragePath( $path, $res ) {
131 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
132 "FileBackend::normalizeStoragePath on path '$path'" );
135 function provider_normalizeStoragePath() {
137 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
138 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
139 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
140 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
141 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
142 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
143 array( 'mwstore://', null ),
144 array( 'mwstore://backend', null ),
145 array( 'mwstore://backend//container/path', null ),
146 array( 'mwstore://backend//container//path', null ),
147 array( 'mwstore:///', null ),
148 array( 'mwstore:/', null ),
149 array( 'mwstore:', null ), )
154 * @dataProvider provider_testParentStoragePath
156 public function testParentStoragePath( $path, $res ) {
157 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
158 "FileBackend::parentStoragePath on path '$path'" );
161 function provider_testParentStoragePath() {
163 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
164 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
165 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
166 array( 'mwstore://backend/container', null ),
167 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
168 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
169 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
170 array( 'mwstore://backend/container/', null ),
175 * @dataProvider provider_testExtensionFromPath
177 public function testExtensionFromPath( $path, $res ) {
178 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
179 "FileBackend::extensionFromPath on path '$path'" );
182 function provider_testExtensionFromPath() {
184 array( 'mwstore://backend/container/path.txt', 'txt' ),
185 array( 'mwstore://backend/container/path.svg.png', 'png' ),
186 array( 'mwstore://backend/container/path', '' ),
187 array( 'mwstore://backend/container/path.', '' ),
192 * @dataProvider provider_testStore
194 public function testStore( $op ) {
195 $this->filesToPrune
[] = $op['src'];
197 $this->backend
= $this->singleBackend
;
198 $this->tearDownFiles();
199 $this->doTestStore( $op );
200 $this->tearDownFiles();
202 $this->backend
= $this->multiBackend
;
203 $this->tearDownFiles();
204 $this->doTestStore( $op );
205 $this->filesToPrune
[] = $op['src']; # avoid file leaking
206 $this->tearDownFiles();
209 private function doTestStore( $op ) {
210 $backendName = $this->backendClass();
212 $source = $op['src'];
214 $this->prepare( array( 'dir' => dirname( $dest ) ) );
216 file_put_contents( $source, "Unit test file" );
218 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
219 $this->backend
->store( $op );
222 $status = $this->backend
->doOperation( $op );
224 $this->assertGoodStatus( $status,
225 "Store from $source to $dest succeeded without warnings ($backendName)." );
226 $this->assertEquals( true, $status->isOK(),
227 "Store from $source to $dest succeeded ($backendName)." );
228 $this->assertEquals( array( 0 => true ), $status->success
,
229 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
230 $this->assertEquals( true, file_exists( $source ),
231 "Source file $source still exists ($backendName)." );
232 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
233 "Destination file $dest exists ($backendName)." );
235 $this->assertEquals( filesize( $source ),
236 $this->backend
->getFileSize( array( 'src' => $dest ) ),
237 "Destination file $dest has correct size ($backendName)." );
239 $props1 = FSFile
::getPropsFromPath( $source );
240 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
241 $this->assertEquals( $props1, $props2,
242 "Source and destination have the same props ($backendName)." );
245 public function provider_testStore() {
248 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
249 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
250 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
258 $op2['overwrite'] = true;
266 $op2['overwriteSame'] = true;
277 * @dataProvider provider_testCopy
279 public function testCopy( $op ) {
280 $this->backend
= $this->singleBackend
;
281 $this->tearDownFiles();
282 $this->doTestCopy( $op );
283 $this->tearDownFiles();
285 $this->backend
= $this->multiBackend
;
286 $this->tearDownFiles();
287 $this->doTestCopy( $op );
288 $this->tearDownFiles();
291 private function doTestCopy( $op ) {
292 $backendName = $this->backendClass();
294 $source = $op['src'];
296 $this->prepare( array( 'dir' => dirname( $source ) ) );
297 $this->prepare( array( 'dir' => dirname( $dest ) ) );
299 $status = $this->backend
->doOperation(
300 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
301 $this->assertGoodStatus( $status,
302 "Creation of file at $source succeeded ($backendName)." );
304 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
305 $this->backend
->copy( $op );
308 $status = $this->backend
->doOperation( $op );
310 $this->assertGoodStatus( $status,
311 "Copy from $source to $dest succeeded without warnings ($backendName)." );
312 $this->assertEquals( true, $status->isOK(),
313 "Copy from $source to $dest succeeded ($backendName)." );
314 $this->assertEquals( array( 0 => true ), $status->success
,
315 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
316 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
317 "Source file $source still exists ($backendName)." );
318 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
319 "Destination file $dest exists after copy ($backendName)." );
322 $this->backend
->getFileSize( array( 'src' => $source ) ),
323 $this->backend
->getFileSize( array( 'src' => $dest ) ),
324 "Destination file $dest has correct size ($backendName)." );
326 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
327 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
328 $this->assertEquals( $props1, $props2,
329 "Source and destination have the same props ($backendName)." );
332 public function provider_testCopy() {
335 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
336 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
338 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
346 $op2['overwrite'] = true;
354 $op2['overwriteSame'] = true;
365 * @dataProvider provider_testMove
367 public function testMove( $op ) {
368 $this->backend
= $this->singleBackend
;
369 $this->tearDownFiles();
370 $this->doTestMove( $op );
371 $this->tearDownFiles();
373 $this->backend
= $this->multiBackend
;
374 $this->tearDownFiles();
375 $this->doTestMove( $op );
376 $this->tearDownFiles();
379 private function doTestMove( $op ) {
380 $backendName = $this->backendClass();
382 $source = $op['src'];
384 $this->prepare( array( 'dir' => dirname( $source ) ) );
385 $this->prepare( array( 'dir' => dirname( $dest ) ) );
387 $status = $this->backend
->doOperation(
388 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
389 $this->assertGoodStatus( $status,
390 "Creation of file at $source succeeded ($backendName)." );
392 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
393 $this->backend
->copy( $op );
396 $status = $this->backend
->doOperation( $op );
397 $this->assertGoodStatus( $status,
398 "Move from $source to $dest succeeded without warnings ($backendName)." );
399 $this->assertEquals( true, $status->isOK(),
400 "Move from $source to $dest succeeded ($backendName)." );
401 $this->assertEquals( array( 0 => true ), $status->success
,
402 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
403 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
404 "Source file $source does not still exists ($backendName)." );
405 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
406 "Destination file $dest exists after move ($backendName)." );
408 $this->assertNotEquals(
409 $this->backend
->getFileSize( array( 'src' => $source ) ),
410 $this->backend
->getFileSize( array( 'src' => $dest ) ),
411 "Destination file $dest has correct size ($backendName)." );
413 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
414 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
415 $this->assertEquals( false, $props1['fileExists'],
416 "Source file does not exist accourding to props ($backendName)." );
417 $this->assertEquals( true, $props2['fileExists'],
418 "Destination file exists accourding to props ($backendName)." );
421 public function provider_testMove() {
424 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
425 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
427 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
435 $op2['overwrite'] = true;
443 $op2['overwriteSame'] = true;
454 * @dataProvider provider_testDelete
456 public function testDelete( $op, $withSource, $okStatus ) {
457 $this->backend
= $this->singleBackend
;
458 $this->tearDownFiles();
459 $this->doTestDelete( $op, $withSource, $okStatus );
460 $this->tearDownFiles();
462 $this->backend
= $this->multiBackend
;
463 $this->tearDownFiles();
464 $this->doTestDelete( $op, $withSource, $okStatus );
465 $this->tearDownFiles();
468 private function doTestDelete( $op, $withSource, $okStatus ) {
469 $backendName = $this->backendClass();
471 $source = $op['src'];
472 $this->prepare( array( 'dir' => dirname( $source ) ) );
475 $status = $this->backend
->doOperation(
476 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
477 $this->assertGoodStatus( $status,
478 "Creation of file at $source succeeded ($backendName)." );
481 $status = $this->backend
->doOperation( $op );
483 $this->assertGoodStatus( $status,
484 "Deletion of file at $source succeeded without warnings ($backendName)." );
485 $this->assertEquals( true, $status->isOK(),
486 "Deletion of file at $source succeeded ($backendName)." );
487 $this->assertEquals( array( 0 => true ), $status->success
,
488 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
490 $this->assertEquals( false, $status->isOK(),
491 "Deletion of file at $source failed ($backendName)." );
494 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
495 "Source file $source does not exist after move ($backendName)." );
498 $this->backend
->getFileSize( array( 'src' => $source ) ),
499 "Source file $source has correct size (false) ($backendName)." );
501 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
502 $this->assertFalse( $props1['fileExists'],
503 "Source file $source does not exist according to props ($backendName)." );
506 public function provider_testDelete() {
509 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
511 $op = array( 'op' => 'delete', 'src' => $source );
520 false, // without source
524 $op['ignoreMissingSource'] = true;
527 false, // without source
535 * @dataProvider provider_testCreate
537 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
538 $this->backend
= $this->singleBackend
;
539 $this->tearDownFiles();
540 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
541 $this->tearDownFiles();
543 $this->backend
= $this->multiBackend
;
544 $this->tearDownFiles();
545 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
546 $this->tearDownFiles();
549 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
550 $backendName = $this->backendClass();
553 $this->prepare( array( 'dir' => dirname( $dest ) ) );
555 $oldText = 'blah...blah...waahwaah';
556 if ( $alreadyExists ) {
557 $status = $this->backend
->doOperation(
558 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
559 $this->assertGoodStatus( $status,
560 "Creation of file at $dest succeeded ($backendName)." );
563 $status = $this->backend
->doOperation( $op );
565 $this->assertGoodStatus( $status,
566 "Creation of file at $dest succeeded without warnings ($backendName)." );
567 $this->assertEquals( true, $status->isOK(),
568 "Creation of file at $dest succeeded ($backendName)." );
569 $this->assertEquals( array( 0 => true ), $status->success
,
570 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
572 $this->assertEquals( false, $status->isOK(),
573 "Creation of file at $dest failed ($backendName)." );
576 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
577 "Destination file $dest exists after creation ($backendName)." );
579 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
580 $this->assertEquals( true, $props1['fileExists'],
581 "Destination file $dest exists according to props ($backendName)." );
582 if ( $okStatus ) { // file content is what we saved
583 $this->assertEquals( $newSize, $props1['size'],
584 "Destination file $dest has expected size according to props ($backendName)." );
585 $this->assertEquals( $newSize,
586 $this->backend
->getFileSize( array( 'src' => $dest ) ),
587 "Destination file $dest has correct size ($backendName)." );
588 } else { // file content is some other previous text
589 $this->assertEquals( strlen( $oldText ), $props1['size'],
590 "Destination file $dest has original size according to props ($backendName)." );
591 $this->assertEquals( strlen( $oldText ),
592 $this->backend
->getFileSize( array( 'src' => $dest ) ),
593 "Destination file $dest has original size according to props ($backendName)." );
598 * @dataProvider provider_testCreate
600 public function provider_testCreate() {
603 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
605 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
608 false, // no dest already exists
610 strlen( $op['content'] )
614 $op2['content'] = "\n";
617 false, // no dest already exists
619 strlen( $op2['content'] )
623 $op2['content'] = "fsf\n waf 3kt";
626 true, // dest already exists
628 strlen( $op2['content'] )
632 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
633 $op2['overwrite'] = true;
636 true, // dest already exists
638 strlen( $op2['content'] )
642 $op2['content'] = "39qjmg3-qg";
643 $op2['overwriteSame'] = true;
646 true, // dest already exists
648 strlen( $op2['content'] )
654 public function testDoQuickOperations() {
655 $this->backend
= $this->singleBackend
;
656 $this->doTestDoQuickOperations();
657 $this->tearDownFiles();
659 $this->backend
= $this->multiBackend
;
660 $this->doTestDoQuickOperations();
661 $this->tearDownFiles();
664 private function doTestDoQuickOperations() {
665 $backendName = $this->backendClass();
667 $base = $this->baseStorePath();
669 "$base/unittest-cont1/fileA.a",
670 "$base/unittest-cont1/fileB.a",
671 "$base/unittest-cont1/fileC.a"
675 foreach ( $files as $path ) {
676 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
677 $this->assertGoodStatus( $status,
678 "Preparing $path succeeded without warnings ($backendName)." );
679 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
680 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
682 $purgeOps[] = array( 'op' => 'null' );
683 $status = $this->backend
->doQuickOperations( $ops );
684 $this->assertGoodStatus( $status,
685 "Creation of source files succeeded ($backendName)." );
687 foreach ( $files as $file ) {
688 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
689 "File $file exists." );
692 $status = $this->backend
->doQuickOperations( $purgeOps );
693 $this->assertGoodStatus( $status,
694 "Quick deletion of source files succeeded ($backendName)." );
696 foreach ( $files as $file ) {
697 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
698 "File $file purged." );
703 * @dataProvider provider_testConcatenate
705 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
706 $this->filesToPrune
[] = $op['dst'];
708 $this->backend
= $this->singleBackend
;
709 $this->tearDownFiles();
710 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
711 $this->tearDownFiles();
713 $this->backend
= $this->multiBackend
;
714 $this->tearDownFiles();
715 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
716 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
717 $this->tearDownFiles();
720 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
721 $backendName = $this->backendClass();
726 foreach ( $srcs as $i => $source ) {
727 $this->prepare( array( 'dir' => dirname( $source ) ) );
729 'op' => 'create', // operation
730 'dst' => $source, // source
731 'content' => $srcsContent[$i]
733 $expContent .= $srcsContent[$i];
735 $status = $this->backend
->doOperations( $ops );
737 $this->assertGoodStatus( $status,
738 "Creation of source files succeeded ($backendName)." );
740 $dest = $params['dst'];
741 if ( $alreadyExists ) {
742 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
743 $this->assertEquals( true, $ok,
744 "Creation of file at $dest succeeded ($backendName)." );
746 $ok = file_put_contents( $dest, '' ) !== false;
747 $this->assertEquals( true, $ok,
748 "Creation of 0-byte file at $dest succeeded ($backendName)." );
751 // Combine the files into one
752 $status = $this->backend
->concatenate( $params );
754 $this->assertGoodStatus( $status,
755 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
756 $this->assertEquals( true, $status->isOK(),
757 "Creation of concat file at $dest succeeded ($backendName)." );
759 $this->assertEquals( false, $status->isOK(),
760 "Creation of concat file at $dest failed ($backendName)." );
764 $this->assertEquals( true, is_file( $dest ),
765 "Dest concat file $dest exists after creation ($backendName)." );
767 $this->assertEquals( true, is_file( $dest ),
768 "Dest concat file $dest exists after failed creation ($backendName)." );
771 $contents = file_get_contents( $dest );
772 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
775 $this->assertEquals( $expContent, $contents,
776 "Concat file at $dest has correct contents ($backendName)." );
778 $this->assertNotEquals( $expContent, $contents,
779 "Concat file at $dest has correct contents ($backendName)." );
783 function provider_testConcatenate() {
786 $rand = mt_rand( 0, 2000000000 ) . time();
787 $dest = wfTempDir() . "/randomfile!$rand.txt";
789 $this->baseStorePath() . '/unittest-cont1/file1.txt',
790 $this->baseStorePath() . '/unittest-cont1/file2.txt',
791 $this->baseStorePath() . '/unittest-cont1/file3.txt',
792 $this->baseStorePath() . '/unittest-cont1/file4.txt',
793 $this->baseStorePath() . '/unittest-cont1/file5.txt',
794 $this->baseStorePath() . '/unittest-cont1/file6.txt',
795 $this->baseStorePath() . '/unittest-cont1/file7.txt',
796 $this->baseStorePath() . '/unittest-cont1/file8.txt',
797 $this->baseStorePath() . '/unittest-cont1/file9.txt',
798 $this->baseStorePath() . '/unittest-cont1/file10.txt'
812 $params = array( 'srcs' => $srcs, 'dst' => $dest );
815 $params, // operation
817 $content, // content for each source
818 false, // no dest already exists
823 $params, // operation
825 $content, // content for each source
826 true, // dest already exists
834 * @dataProvider provider_testGetFileStat
836 public function testGetFileStat( $path, $content, $alreadyExists ) {
837 $this->backend
= $this->singleBackend
;
838 $this->tearDownFiles();
839 $this->doTestGetFileStat( $path, $content, $alreadyExists );
840 $this->tearDownFiles();
842 $this->backend
= $this->multiBackend
;
843 $this->tearDownFiles();
844 $this->doTestGetFileStat( $path, $content, $alreadyExists );
845 $this->tearDownFiles();
848 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
849 $backendName = $this->backendClass();
851 if ( $alreadyExists ) {
852 $this->prepare( array( 'dir' => dirname( $path ) ) );
853 $status = $this->backend
->create( array( 'dst' => $path, 'content' => $content ) );
854 $this->assertGoodStatus( $status,
855 "Creation of file at $path succeeded ($backendName)." );
857 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
858 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
859 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
861 $this->assertEquals( strlen( $content ), $size,
862 "Correct file size of '$path'" );
863 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
864 "Correct file timestamp of '$path'" );
866 $size = $stat['size'];
867 $time = $stat['mtime'];
868 $this->assertEquals( strlen( $content ), $size,
869 "Correct file size of '$path'" );
870 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
871 "Correct file timestamp of '$path'" );
873 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
874 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
875 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
877 $this->assertFalse( $size, "Correct file size of '$path'" );
878 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
879 $this->assertFalse( $stat, "Correct file stat of '$path'" );
883 function provider_testGetFileStat() {
886 $base = $this->baseStorePath();
887 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
888 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
889 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
895 * @dataProvider provider_testGetFileContents
897 public function testGetFileContents( $source, $content ) {
898 $this->backend
= $this->singleBackend
;
899 $this->tearDownFiles();
900 $this->doTestGetFileContents( $source, $content );
901 $this->tearDownFiles();
903 $this->backend
= $this->multiBackend
;
904 $this->tearDownFiles();
905 $this->doTestGetFileContents( $source, $content );
906 $this->tearDownFiles();
909 private function doTestGetFileContents( $source, $content ) {
910 $backendName = $this->backendClass();
912 $this->prepare( array( 'dir' => dirname( $source ) ) );
914 $status = $this->backend
->doOperation(
915 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
916 $this->assertGoodStatus( $status,
917 "Creation of file at $source succeeded ($backendName)." );
918 $this->assertEquals( true, $status->isOK(),
919 "Creation of file at $source succeeded with OK status ($backendName)." );
921 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
922 $this->assertNotEquals( false, $newContents,
923 "Read of file at $source succeeded ($backendName)." );
925 $this->assertEquals( $content, $newContents,
926 "Contents read match data at $source ($backendName)." );
929 function provider_testGetFileContents() {
932 $base = $this->baseStorePath();
933 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
934 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
940 * @dataProvider provider_testGetLocalCopy
942 public function testGetLocalCopy( $source, $content ) {
943 $this->backend
= $this->singleBackend
;
944 $this->tearDownFiles();
945 $this->doTestGetLocalCopy( $source, $content );
946 $this->tearDownFiles();
948 $this->backend
= $this->multiBackend
;
949 $this->tearDownFiles();
950 $this->doTestGetLocalCopy( $source, $content );
951 $this->tearDownFiles();
954 private function doTestGetLocalCopy( $source, $content ) {
955 $backendName = $this->backendClass();
957 $this->prepare( array( 'dir' => dirname( $source ) ) );
959 $status = $this->backend
->doOperation(
960 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
961 $this->assertGoodStatus( $status,
962 "Creation of file at $source succeeded ($backendName)." );
964 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
965 $this->assertNotNull( $tmpFile,
966 "Creation of local copy of $source succeeded ($backendName)." );
968 $contents = file_get_contents( $tmpFile->getPath() );
969 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
972 function provider_testGetLocalCopy() {
975 $base = $this->baseStorePath();
976 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
977 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
983 * @dataProvider provider_testGetLocalReference
985 public function testGetLocalReference( $source, $content ) {
986 $this->backend
= $this->singleBackend
;
987 $this->tearDownFiles();
988 $this->doTestGetLocalReference( $source, $content );
989 $this->tearDownFiles();
991 $this->backend
= $this->multiBackend
;
992 $this->tearDownFiles();
993 $this->doTestGetLocalReference( $source, $content );
994 $this->tearDownFiles();
997 private function doTestGetLocalReference( $source, $content ) {
998 $backendName = $this->backendClass();
1000 $this->prepare( array( 'dir' => dirname( $source ) ) );
1002 $status = $this->backend
->doOperation(
1003 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
1004 $this->assertGoodStatus( $status,
1005 "Creation of file at $source succeeded ($backendName)." );
1007 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1008 $this->assertNotNull( $tmpFile,
1009 "Creation of local copy of $source succeeded ($backendName)." );
1011 $contents = file_get_contents( $tmpFile->getPath() );
1012 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1015 function provider_testGetLocalReference() {
1018 $base = $this->baseStorePath();
1019 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
1020 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
1026 * @dataProvider provider_testPrepareAndClean
1028 public function testPrepareAndClean( $path, $isOK ) {
1029 $this->backend
= $this->singleBackend
;
1030 $this->doTestPrepareAndClean( $path, $isOK );
1031 $this->tearDownFiles();
1033 $this->backend
= $this->multiBackend
;
1034 $this->doTestPrepareAndClean( $path, $isOK );
1035 $this->tearDownFiles();
1038 function provider_testPrepareAndClean() {
1039 $base = $this->baseStorePath();
1041 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
1042 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1043 # Specific to FS backend with no basePath field set
1044 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1048 private function doTestPrepareAndClean( $path, $isOK ) {
1049 $backendName = $this->backendClass();
1051 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1053 $this->assertGoodStatus( $status,
1054 "Preparing dir $path succeeded without warnings ($backendName)." );
1055 $this->assertEquals( true, $status->isOK(),
1056 "Preparing dir $path succeeded ($backendName)." );
1058 $this->assertEquals( false, $status->isOK(),
1059 "Preparing dir $path failed ($backendName)." );
1062 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1064 $this->assertGoodStatus( $status,
1065 "Cleaning dir $path succeeded without warnings ($backendName)." );
1066 $this->assertEquals( true, $status->isOK(),
1067 "Cleaning dir $path succeeded ($backendName)." );
1069 $this->assertEquals( false, $status->isOK(),
1070 "Cleaning dir $path failed ($backendName)." );
1074 public function testRecursiveClean() {
1075 $this->backend
= $this->singleBackend
;
1076 $this->doTestRecursiveClean();
1077 $this->tearDownFiles();
1079 $this->backend
= $this->multiBackend
;
1080 $this->doTestRecursiveClean();
1081 $this->tearDownFiles();
1084 private function doTestRecursiveClean() {
1085 $backendName = $this->backendClass();
1087 $base = $this->baseStorePath();
1089 "$base/unittest-cont1/a",
1090 "$base/unittest-cont1/a/b",
1091 "$base/unittest-cont1/a/b/c",
1092 "$base/unittest-cont1/a/b/c/d0",
1093 "$base/unittest-cont1/a/b/c/d1",
1094 "$base/unittest-cont1/a/b/c/d2",
1095 "$base/unittest-cont1/a/b/c/d0/1",
1096 "$base/unittest-cont1/a/b/c/d0/2",
1097 "$base/unittest-cont1/a/b/c/d1/3",
1098 "$base/unittest-cont1/a/b/c/d1/4",
1099 "$base/unittest-cont1/a/b/c/d2/5",
1100 "$base/unittest-cont1/a/b/c/d2/6"
1102 foreach ( $dirs as $dir ) {
1103 $status = $this->prepare( array( 'dir' => $dir ) );
1104 $this->assertGoodStatus( $status,
1105 "Preparing dir $dir succeeded without warnings ($backendName)." );
1108 if ( $this->backend
instanceof FSFileBackend
) {
1109 foreach ( $dirs as $dir ) {
1110 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1111 "Dir $dir exists ($backendName)." );
1115 $status = $this->backend
->clean(
1116 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1117 $this->assertGoodStatus( $status,
1118 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1120 foreach ( $dirs as $dir ) {
1121 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1122 "Dir $dir no longer exists ($backendName)." );
1126 // @TODO: testSecure
1128 public function testDoOperations() {
1129 $this->backend
= $this->singleBackend
;
1130 $this->tearDownFiles();
1131 $this->doTestDoOperations();
1132 $this->tearDownFiles();
1134 $this->backend
= $this->multiBackend
;
1135 $this->tearDownFiles();
1136 $this->doTestDoOperations();
1137 $this->tearDownFiles();
1139 $this->backend
= $this->singleBackend
;
1140 $this->tearDownFiles();
1141 $this->doTestDoOperations2();
1142 $this->tearDownFiles();
1144 $this->backend
= $this->multiBackend
;
1145 $this->tearDownFiles();
1146 $this->doTestDoOperations2();
1147 $this->tearDownFiles();
1149 $this->backend
= $this->singleBackend
;
1150 $this->tearDownFiles();
1151 $this->doTestDoOperationsFailing();
1152 $this->tearDownFiles();
1154 $this->backend
= $this->multiBackend
;
1155 $this->tearDownFiles();
1156 $this->doTestDoOperationsFailing();
1157 $this->tearDownFiles();
1160 private function doTestDoOperations() {
1161 $base = $this->baseStorePath();
1163 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1164 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1165 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1166 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1167 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1168 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1169 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1171 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1172 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1173 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1174 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1175 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1176 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1177 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1179 $status = $this->backend
->doOperations( array(
1180 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1181 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1182 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1183 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1184 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1185 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1186 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1187 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1188 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1189 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1190 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1191 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1192 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1193 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1194 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1195 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1196 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1198 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1200 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1202 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1204 array( 'op' => 'null' ),
1208 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1209 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1210 $this->assertEquals( 13, count( $status->success
),
1211 "Operation batch has correct success array" );
1213 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1214 "File does not exist at $fileA" );
1215 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1216 "File does not exist at $fileB" );
1217 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1218 "File does not exist at $fileD" );
1220 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1221 "File exists at $fileC" );
1222 $this->assertEquals( $fileBContents,
1223 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1224 "Correct file contents of $fileC" );
1225 $this->assertEquals( strlen( $fileBContents ),
1226 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1227 "Correct file size of $fileC" );
1228 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1229 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1230 "Correct file SHA-1 of $fileC" );
1233 // concurrency orientated
1234 private function doTestDoOperations2() {
1235 $base = $this->baseStorePath();
1237 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1238 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1239 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1241 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1242 file_put_contents( $tmpNameA, $fileAContents );
1243 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1244 file_put_contents( $tmpNameB, $fileBContents );
1245 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1246 file_put_contents( $tmpNameC, $fileCContents );
1248 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1249 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1250 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1252 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1253 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1254 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1255 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1257 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1258 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1259 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1260 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1261 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1263 $status = $this->backend
->doOperations( array(
1264 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1265 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1266 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1267 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1268 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1269 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1270 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1271 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1272 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1273 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1274 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1275 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1276 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1277 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1278 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1279 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1280 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1281 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1282 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1283 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1285 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1287 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1289 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1291 array( 'op' => 'null' ),
1295 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1296 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1297 $this->assertEquals( 16, count( $status->success
),
1298 "Operation batch has correct success array" );
1300 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1301 "File does not exist at $fileA" );
1302 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1303 "File does not exist at $fileB" );
1304 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1305 "File does not exist at $fileD" );
1307 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1308 "File exists at $fileC" );
1309 $this->assertEquals( $fileBContents,
1310 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1311 "Correct file contents of $fileC" );
1312 $this->assertEquals( strlen( $fileBContents ),
1313 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1314 "Correct file size of $fileC" );
1315 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1316 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1317 "Correct file SHA-1 of $fileC" );
1320 private function doTestDoOperationsFailing() {
1321 $base = $this->baseStorePath();
1323 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1324 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1325 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1326 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1327 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1328 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1329 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1331 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1332 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1333 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1334 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1335 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1336 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1338 $status = $this->backend
->doOperations( array(
1339 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1340 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1341 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1342 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1343 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1344 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1345 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1346 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1347 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1348 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1349 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1350 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1351 array( 'op' => 'delete', 'src' => $fileD ),
1352 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1353 array( 'op' => 'null' ),
1355 ), array( 'force' => 1 ) );
1357 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1358 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1359 $this->assertEquals( 8, count( $status->success
),
1360 "Operation batch has correct success array" );
1362 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1363 "File does not exist at $fileB" );
1364 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1365 "File does not exist at $fileD" );
1367 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1368 "File does not exist at $fileA" );
1369 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1370 "File exists at $fileC" );
1371 $this->assertEquals( $fileBContents,
1372 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1373 "Correct file contents of $fileA" );
1374 $this->assertEquals( strlen( $fileBContents ),
1375 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1376 "Correct file size of $fileA" );
1377 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1378 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1379 "Correct file SHA-1 of $fileA" );
1382 public function testGetFileList() {
1383 $this->backend
= $this->singleBackend
;
1384 $this->tearDownFiles();
1385 $this->doTestGetFileList();
1386 $this->tearDownFiles();
1388 $this->backend
= $this->multiBackend
;
1389 $this->tearDownFiles();
1390 $this->doTestGetFileList();
1391 $this->tearDownFiles();
1394 private function doTestGetFileList() {
1395 $backendName = $this->backendClass();
1397 $base = $this->baseStorePath();
1399 "$base/unittest-cont1/test1.txt",
1400 "$base/unittest-cont1/test2.txt",
1401 "$base/unittest-cont1/test3.txt",
1402 "$base/unittest-cont1/subdir1/test1.txt",
1403 "$base/unittest-cont1/subdir1/test2.txt",
1404 "$base/unittest-cont1/subdir2/test3.txt",
1405 "$base/unittest-cont1/subdir2/test4.txt",
1406 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1407 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1408 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1409 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1410 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1411 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1412 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1417 foreach ( $files as $file ) {
1418 $this->prepare( array( 'dir' => dirname( $file ) ) );
1419 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1421 $status = $this->backend
->doOperations( $ops );
1422 $this->assertGoodStatus( $status,
1423 "Creation of files succeeded ($backendName)." );
1424 $this->assertEquals( true, $status->isOK(),
1425 "Creation of files succeeded with OK status ($backendName)." );
1432 "subdir1/test1.txt",
1433 "subdir1/test2.txt",
1434 "subdir2/test3.txt",
1435 "subdir2/test4.txt",
1436 "subdir2/subdir/test1.txt",
1437 "subdir2/subdir/test2.txt",
1438 "subdir2/subdir/test3.txt",
1439 "subdir2/subdir/test4.txt",
1440 "subdir2/subdir/test5.txt",
1441 "subdir2/subdir/sub/test0.txt",
1442 "subdir2/subdir/sub/120-px-file.txt",
1446 // Actual listing (no trailing slash)
1448 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1449 foreach ( $iter as $file ) {
1454 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1456 // Actual listing (with trailing slash)
1458 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1459 foreach ( $iter as $file ) {
1464 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1474 "sub/120-px-file.txt",
1478 // Actual listing (no trailing slash)
1480 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1481 foreach ( $iter as $file ) {
1486 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1488 // Actual listing (with trailing slash)
1490 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1491 foreach ( $iter as $file ) {
1496 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1498 // Actual listing (using iterator second time)
1500 foreach ( $iter as $file ) {
1505 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1507 // Expected listing (top files only)
1517 // Actual listing (top files only)
1519 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1520 foreach ( $iter as $file ) {
1525 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1527 foreach ( $files as $file ) { // clean up
1528 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1531 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1532 foreach ( $iter as $iter ) {} // no errors
1535 public function testGetDirectoryList() {
1536 $this->backend
= $this->singleBackend
;
1537 $this->tearDownFiles();
1538 $this->doTestGetDirectoryList();
1539 $this->tearDownFiles();
1541 $this->backend
= $this->multiBackend
;
1542 $this->tearDownFiles();
1543 $this->doTestGetDirectoryList();
1544 $this->tearDownFiles();
1547 private function doTestGetDirectoryList() {
1548 $backendName = $this->backendClass();
1550 $base = $this->baseStorePath();
1552 "$base/unittest-cont1/test1.txt",
1553 "$base/unittest-cont1/test2.txt",
1554 "$base/unittest-cont1/test3.txt",
1555 "$base/unittest-cont1/subdir1/test1.txt",
1556 "$base/unittest-cont1/subdir1/test2.txt",
1557 "$base/unittest-cont1/subdir2/test3.txt",
1558 "$base/unittest-cont1/subdir2/test4.txt",
1559 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1560 "$base/unittest-cont1/subdir3/subdir/test2.txt",
1561 "$base/unittest-cont1/subdir4/subdir/test3.txt",
1562 "$base/unittest-cont1/subdir4/subdir/test4.txt",
1563 "$base/unittest-cont1/subdir4/subdir/test5.txt",
1564 "$base/unittest-cont1/subdir4/subdir/sub/test0.txt",
1565 "$base/unittest-cont1/subdir4/subdir/sub/120-px-file.txt",
1570 foreach ( $files as $file ) {
1571 $this->prepare( array( 'dir' => dirname( $file ) ) );
1572 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1574 $status = $this->backend
->doOperations( $ops );
1575 $this->assertGoodStatus( $status,
1576 "Creation of files succeeded ($backendName)." );
1577 $this->assertEquals( true, $status->isOK(),
1578 "Creation of files succeeded with OK status ($backendName)." );
1589 $this->assertEquals( true,
1590 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir1" ) ),
1591 "Directory exists in ($backendName)." );
1592 $this->assertEquals( true,
1593 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) ),
1594 "Directory exists in ($backendName)." );
1595 $this->assertEquals( false,
1596 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/subdir2/test1.txt" ) ),
1597 "Directory does not exists in ($backendName)." );
1599 // Actual listing (no trailing slash)
1601 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1602 foreach ( $iter as $file ) {
1607 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1609 // Actual listing (with trailing slash)
1611 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1612 foreach ( $iter as $file ) {
1617 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1625 // Actual listing (no trailing slash)
1627 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir2" ) );
1628 foreach ( $iter as $file ) {
1633 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1635 // Actual listing (with trailing slash)
1637 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir2/" ) );
1638 foreach ( $iter as $file ) {
1643 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1645 // Actual listing (using iterator second time)
1647 foreach ( $iter as $file ) {
1652 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1654 // Expected listing (recursive)
1663 "subdir4/subdir/sub",
1667 // Actual listing (recursive)
1669 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1670 foreach ( $iter as $file ) {
1675 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1677 // Expected listing (recursive)
1684 // Actual listing (recursive)
1686 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/subdir4" ) );
1687 foreach ( $iter as $file ) {
1692 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1694 // Actual listing (recursive, second time)
1696 foreach ( $iter as $file ) {
1701 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1703 foreach ( $files as $file ) { // clean up
1704 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1707 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1708 foreach ( $iter as $iter ) {} // no errors
1711 public function testLockCalls() {
1712 $this->backend
= $this->singleBackend
;
1713 $this->doTestLockCalls();
1716 private function doTestLockCalls() {
1717 $backendName = $this->backendClass();
1719 for ( $i=0; $i<50; $i++
) {
1725 "subdir1", // duplicate
1726 "subdir1/test1.txt",
1727 "subdir1/test2.txt",
1729 "subdir2", // duplicate
1730 "subdir2/test3.txt",
1731 "subdir2/test4.txt",
1733 "subdir2/subdir/test1.txt",
1734 "subdir2/subdir/test2.txt",
1735 "subdir2/subdir/test3.txt",
1736 "subdir2/subdir/test4.txt",
1737 "subdir2/subdir/test5.txt",
1738 "subdir2/subdir/sub",
1739 "subdir2/subdir/sub/test0.txt",
1740 "subdir2/subdir/sub/120-px-file.txt",
1743 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1744 $this->assertEquals( array(), $status->errors
,
1745 "Locking of files succeeded ($backendName)." );
1746 $this->assertEquals( true, $status->isOK(),
1747 "Locking of files succeeded with OK status ($backendName)." );
1749 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1750 $this->assertEquals( array(), $status->errors
,
1751 "Locking of files succeeded ($backendName)." );
1752 $this->assertEquals( true, $status->isOK(),
1753 "Locking of files succeeded with OK status ($backendName)." );
1755 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1756 $this->assertEquals( array(), $status->errors
,
1757 "Locking of files succeeded ($backendName)." );
1758 $this->assertEquals( true, $status->isOK(),
1759 "Locking of files succeeded with OK status ($backendName)." );
1761 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1762 $this->assertEquals( array(), $status->errors
,
1763 "Locking of files succeeded ($backendName)." );
1764 $this->assertEquals( true, $status->isOK(),
1765 "Locking of files succeeded with OK status ($backendName)." );
1769 // test helper wrapper for backend prepare() function
1770 private function prepare( array $params ) {
1771 return $this->backend
->prepare( $params );
1774 function tearDownFiles() {
1775 foreach ( $this->filesToPrune
as $file ) {
1778 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1779 foreach ( $containers as $container ) {
1780 $this->deleteFiles( $container );
1782 $this->filesToPrune
= array();
1785 private function deleteFiles( $container ) {
1786 $base = $this->baseStorePath();
1787 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1789 foreach ( $iter as $file ) {
1790 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1791 array( 'force' => 1, 'nonLocking' => 1 ) );
1794 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1797 function assertGoodStatus( $status, $msg ) {
1798 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1801 function tearDown() {