141ccd342844329e3a9b4bbe89b663286f18d7a8
6 class FileBackendTest
extends MediaWikiTestCase
{
7 private $backend, $multiBackend;
9 private static $backendToUse;
12 global $wgFileBackends;
14 $tmpDir = wfTempDir() . '/file-backend-test-' . time() . '-' . mt_rand();
15 if ( $this->getCliArg( 'use-filebackend=' ) ) {
16 if ( self
::$backendToUse ) {
17 $this->singleBackend
= self
::$backendToUse;
19 $name = $this->getCliArg( 'use-filebackend=' );
21 foreach ( $wgFileBackends as $conf ) {
22 if ( $conf['name'] == $name ) {
26 $useConfig['name'] = 'localtesting'; // swap name
27 self
::$backendToUse = new $conf['class']( $useConfig );
28 $this->singleBackend
= self
::$backendToUse;
31 $this->singleBackend
= new FSFileBackend( array(
32 'name' => 'localtesting',
33 'lockManager' => 'fsLockManager',
34 'containerPaths' => array(
35 'unittest-cont1' => "$tmpDir/localtesting/unittest-cont1",
36 'unittest-cont2' => "$tmpDir/localtesting/unittest-cont2" )
39 $this->multiBackend
= new FileBackendMultiWrite( array(
40 'name' => 'localtesting',
41 'lockManager' => 'fsLockManager',
44 'name' => 'localmutlitesting1',
45 'class' => 'FSFileBackend',
46 'lockManager' => 'nullLockManager',
47 'containerPaths' => array(
48 'unittest-cont1' => "$tmpDir/localtestingmulti1/cont1",
49 'unittest-cont2' => "$tmpDir/localtestingmulti1/unittest-cont2" ),
50 'isMultiMaster' => false
53 'name' => 'localmutlitesting2',
54 'class' => 'FSFileBackend',
55 'lockManager' => 'nullLockManager',
56 'containerPaths' => array(
57 'unittest-cont1' => "$tmpDir/localtestingmulti2/cont1",
58 'unittest-cont2' => "$tmpDir/localtestingmulti2/unittest-cont2" ),
59 'isMultiMaster' => true
63 $this->filesToPrune
= array();
66 private function baseStorePath() {
67 return 'mwstore://localtesting';
70 private function backendClass() {
71 return get_class( $this->backend
);
75 * @dataProvider provider_testStore
77 public function testStore( $op, $source, $dest ) {
78 $this->filesToPrune
[] = $source;
80 $this->backend
= $this->singleBackend
;
81 $this->tearDownFiles();
82 $this->doTestStore( $op, $source, $dest );
83 $this->tearDownFiles();
85 $this->backend
= $this->multiBackend
;
86 $this->tearDownFiles();
87 $this->doTestStore( $op, $source, $dest );
88 $this->tearDownFiles();
91 function doTestStore( $op, $source, $dest ) {
92 $backendName = $this->backendClass();
94 $this->backend
->prepare( array( 'dir' => dirname( $dest ) ) );
96 file_put_contents( $source, "Unit test file" );
98 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
99 $this->backend
->store( $op );
102 $status = $this->backend
->doOperation( $op );
104 $this->assertEquals( array(), $status->errors
,
105 "Store from $source to $dest succeeded without warnings ($backendName)." );
106 $this->assertEquals( array(), $status->errors
,
107 "Store from $source to $dest succeeded ($backendName)." );
108 $this->assertEquals( array( 0 => true ), $status->success
,
109 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
110 $this->assertEquals( true, file_exists( $source ),
111 "Source file $source still exists ($backendName)." );
112 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
113 "Destination file $dest exists ($backendName)." );
115 $this->assertEquals( filesize( $source ),
116 $this->backend
->getFileSize( array( 'src' => $dest ) ),
117 "Destination file $dest has correct size ($backendName)." );
119 $props1 = FSFile
::getPropsFromPath( $source );
120 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
121 $this->assertEquals( $props1, $props2,
122 "Source and destination have the same props ($backendName)." );
125 public function provider_testStore() {
128 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
129 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
130 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
138 $op2['overwrite'] = true;
146 $op2['overwriteSame'] = true;
157 * @dataProvider provider_testCopy
159 public function testCopy( $op, $source, $dest ) {
160 $this->backend
= $this->singleBackend
;
161 $this->tearDownFiles();
162 $this->doTestCopy( $op, $source, $dest );
163 $this->tearDownFiles();
165 $this->backend
= $this->multiBackend
;
166 $this->tearDownFiles();
167 $this->doTestCopy( $op, $source, $dest );
168 $this->tearDownFiles();
171 function doTestCopy( $op, $source, $dest ) {
172 $backendName = $this->backendClass();
174 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
175 $this->backend
->prepare( array( 'dir' => dirname( $dest ) ) );
177 $status = $this->backend
->doOperation(
178 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
179 $this->assertEquals( array(), $status->errors
,
180 "Creation of file at $source succeeded ($backendName)." );
182 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
183 $this->backend
->copy( $op );
186 $status = $this->backend
->doOperation( $op );
188 $this->assertEquals( array(), $status->errors
,
189 "Copy from $source to $dest succeeded without warnings ($backendName)." );
190 $this->assertEquals( true, $status->isOK(),
191 "Copy from $source to $dest succeeded ($backendName)." );
192 $this->assertEquals( array( 0 => true ), $status->success
,
193 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
194 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
195 "Source file $source still exists ($backendName)." );
196 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
197 "Destination file $dest exists after copy ($backendName)." );
200 $this->backend
->getFileSize( array( 'src' => $source ) ),
201 $this->backend
->getFileSize( array( 'src' => $dest ) ),
202 "Destination file $dest has correct size ($backendName)." );
204 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
205 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
206 $this->assertEquals( $props1, $props2,
207 "Source and destination have the same props ($backendName)." );
210 public function provider_testCopy() {
213 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
214 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
216 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
224 $op2['overwrite'] = true;
232 $op2['overwriteSame'] = true;
243 * @dataProvider provider_testMove
245 public function testMove( $op, $source, $dest ) {
246 $this->backend
= $this->singleBackend
;
247 $this->tearDownFiles();
248 $this->doTestMove( $op, $source, $dest );
249 $this->tearDownFiles();
251 $this->backend
= $this->multiBackend
;
252 $this->tearDownFiles();
253 $this->doTestMove( $op, $source, $dest );
254 $this->tearDownFiles();
257 private function doTestMove( $op, $source, $dest ) {
258 $backendName = $this->backendClass();
260 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
261 $this->backend
->prepare( array( 'dir' => dirname( $dest ) ) );
263 $status = $this->backend
->doOperation(
264 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
265 $this->assertEquals( array(), $status->errors
,
266 "Creation of file at $source succeeded ($backendName)." );
268 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
269 $this->backend
->copy( $op );
272 $status = $this->backend
->doOperation( $op );
273 $this->assertEquals( array(), $status->errors
,
274 "Move from $source to $dest succeeded without warnings ($backendName)." );
275 $this->assertEquals( true, $status->isOK(),
276 "Move from $source to $dest succeeded ($backendName)." );
277 $this->assertEquals( array( 0 => true ), $status->success
,
278 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
279 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
280 "Source file $source does not still exists ($backendName)." );
281 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
282 "Destination file $dest exists after move ($backendName)." );
284 $this->assertNotEquals(
285 $this->backend
->getFileSize( array( 'src' => $source ) ),
286 $this->backend
->getFileSize( array( 'src' => $dest ) ),
287 "Destination file $dest has correct size ($backendName)." );
289 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
290 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
291 $this->assertEquals( false, $props1['fileExists'],
292 "Source file does not exist accourding to props ($backendName)." );
293 $this->assertEquals( true, $props2['fileExists'],
294 "Destination file exists accourding to props ($backendName)." );
297 public function provider_testMove() {
300 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
301 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
303 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
311 $op2['overwrite'] = true;
319 $op2['overwriteSame'] = true;
330 * @dataProvider provider_testDelete
332 public function testDelete( $op, $source, $withSource, $okStatus ) {
333 $this->backend
= $this->singleBackend
;
334 $this->tearDownFiles();
335 $this->doTestDelete( $op, $source, $withSource, $okStatus );
336 $this->tearDownFiles();
338 $this->backend
= $this->multiBackend
;
339 $this->tearDownFiles();
340 $this->doTestDelete( $op, $source, $withSource, $okStatus );
341 $this->tearDownFiles();
344 private function doTestDelete( $op, $source, $withSource, $okStatus ) {
345 $backendName = $this->backendClass();
347 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
350 $status = $this->backend
->doOperation(
351 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
352 $this->assertEquals( array(), $status->errors
,
353 "Creation of file at $source succeeded ($backendName)." );
356 $status = $this->backend
->doOperation( $op );
358 $this->assertEquals( array(), $status->errors
,
359 "Deletion of file at $source succeeded without warnings ($backendName)." );
360 $this->assertEquals( true, $status->isOK(),
361 "Deletion of file at $source succeeded ($backendName)." );
362 $this->assertEquals( array( 0 => true ), $status->success
,
363 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
365 $this->assertEquals( false, $status->isOK(),
366 "Deletion of file at $source failed ($backendName)." );
369 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
370 "Source file $source does not exist after move ($backendName)." );
373 $this->backend
->getFileSize( array( 'src' => $source ) ),
374 "Source file $source has correct size (false) ($backendName)." );
376 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
377 $this->assertFalse( $props1['fileExists'],
378 "Source file $source does not exist according to props ($backendName)." );
381 public function provider_testDelete() {
384 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
386 $op = array( 'op' => 'delete', 'src' => $source );
397 false, // without source
401 $op['ignoreMissingSource'] = true;
405 false, // without source
413 * @dataProvider provider_testCreate
415 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
416 $this->backend
= $this->singleBackend
;
417 $this->tearDownFiles();
418 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
419 $this->tearDownFiles();
421 $this->backend
= $this->multiBackend
;
422 $this->tearDownFiles();
423 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
424 $this->tearDownFiles();
427 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
428 $backendName = $this->backendClass();
431 $this->backend
->prepare( array( 'dir' => dirname( $dest ) ) );
433 $oldText = 'blah...blah...waahwaah';
434 if ( $alreadyExists ) {
435 $status = $this->backend
->doOperation(
436 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
437 $this->assertEquals( array(), $status->errors
,
438 "Creation of file at $dest succeeded ($backendName)." );
441 $status = $this->backend
->doOperation( $op );
443 $this->assertEquals( array(), $status->errors
,
444 "Creation of file at $dest succeeded without warnings ($backendName)." );
445 $this->assertEquals( true, $status->isOK(),
446 "Creation of file at $dest succeeded ($backendName)." );
447 $this->assertEquals( array( 0 => true ), $status->success
,
448 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
450 $this->assertEquals( false, $status->isOK(),
451 "Creation of file at $dest failed ($backendName)." );
454 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
455 "Destination file $dest exists after creation ($backendName)." );
457 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
458 $this->assertEquals( true, $props1['fileExists'],
459 "Destination file $dest exists according to props ($backendName)." );
460 if ( $okStatus ) { // file content is what we saved
461 $this->assertEquals( $newSize, $props1['size'],
462 "Destination file $dest has expected size according to props ($backendName)." );
463 $this->assertEquals( $newSize,
464 $this->backend
->getFileSize( array( 'src' => $dest ) ),
465 "Destination file $dest has correct size ($backendName)." );
466 } else { // file content is some other previous text
467 $this->assertEquals( strlen( $oldText ), $props1['size'],
468 "Destination file $dest has original size according to props ($backendName)." );
469 $this->assertEquals( strlen( $oldText ),
470 $this->backend
->getFileSize( array( 'src' => $dest ) ),
471 "Destination file $dest has original size according to props ($backendName)." );
476 * @dataProvider provider_testCreate
478 public function provider_testCreate() {
481 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
483 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
486 false, // no dest already exists
488 strlen( $op['content'] )
492 $op2['content'] = "\n";
495 false, // no dest already exists
497 strlen( $op2['content'] )
501 $op2['content'] = "fsf\n waf 3kt";
504 true, // dest already exists
506 strlen( $op2['content'] )
510 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
511 $op2['overwrite'] = true;
514 true, // dest already exists
516 strlen( $op2['content'] )
520 $op2['content'] = "39qjmg3-qg";
521 $op2['overwriteSame'] = true;
524 true, // dest already exists
526 strlen( $op2['content'] )
533 * @dataProvider provider_testConcatenate
535 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
536 $this->filesToPrune
[] = $op['dst'];
538 $this->backend
= $this->singleBackend
;
539 $this->tearDownFiles();
540 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
541 $this->tearDownFiles();
543 $this->backend
= $this->multiBackend
;
544 $this->tearDownFiles();
545 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
546 $this->tearDownFiles();
549 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
550 $backendName = $this->backendClass();
555 foreach ( $srcs as $i => $source ) {
556 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
558 'op' => 'create', // operation
559 'dst' => $source, // source
560 'content' => $srcsContent[$i]
562 $expContent .= $srcsContent[$i];
564 $status = $this->backend
->doOperations( $ops );
566 $this->assertEquals( array(), $status->errors
,
567 "Creation of source files succeeded ($backendName)." );
569 $dest = $params['dst'];
570 if ( $alreadyExists ) {
571 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
572 $this->assertEquals( true, $ok,
573 "Creation of file at $dest succeeded ($backendName)." );
575 $ok = file_put_contents( $dest, '' ) !== false;
576 $this->assertEquals( true, $ok,
577 "Creation of 0-byte file at $dest succeeded ($backendName)." );
580 // Combine the files into one
581 $status = $this->backend
->concatenate( $params );
583 $this->assertEquals( array(), $status->errors
,
584 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
585 $this->assertEquals( true, $status->isOK(),
586 "Creation of concat file at $dest succeeded ($backendName)." );
588 $this->assertEquals( false, $status->isOK(),
589 "Creation of concat file at $dest failed ($backendName)." );
593 $this->assertEquals( true, is_file( $dest ),
594 "Dest concat file $dest exists after creation ($backendName)." );
596 $this->assertEquals( true, is_file( $dest ),
597 "Dest concat file $dest exists after failed creation ($backendName)." );
600 $contents = file_get_contents( $dest );
601 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
604 $this->assertEquals( $expContent, $contents,
605 "Concat file at $dest has correct contents ($backendName)." );
607 $this->assertNotEquals( $expContent, $contents,
608 "Concat file at $dest has correct contents ($backendName)." );
612 function provider_testConcatenate() {
615 $rand = mt_rand( 0, 2000000000 ) . time();
616 $dest = wfTempDir() . "/randomfile!$rand.txt";
618 $this->baseStorePath() . '/unittest-cont1/file1.txt',
619 $this->baseStorePath() . '/unittest-cont1/file2.txt',
620 $this->baseStorePath() . '/unittest-cont1/file3.txt',
621 $this->baseStorePath() . '/unittest-cont1/file4.txt',
622 $this->baseStorePath() . '/unittest-cont1/file5.txt',
623 $this->baseStorePath() . '/unittest-cont1/file6.txt',
624 $this->baseStorePath() . '/unittest-cont1/file7.txt',
625 $this->baseStorePath() . '/unittest-cont1/file8.txt',
626 $this->baseStorePath() . '/unittest-cont1/file9.txt',
627 $this->baseStorePath() . '/unittest-cont1/file10.txt'
641 $params = array( 'srcs' => $srcs, 'dst' => $dest );
644 $params, // operation
646 $content, // content for each source
647 false, // no dest already exists
652 $params, // operation
654 $content, // content for each source
655 true, // dest already exists
663 * @dataProvider provider_testGetFileContents
665 public function testGetFileContents( $src, $content ) {
666 $this->backend
= $this->singleBackend
;
667 $this->tearDownFiles();
668 $this->doTestGetFileContents( $src, $content );
669 $this->tearDownFiles();
671 $this->backend
= $this->multiBackend
;
672 $this->tearDownFiles();
673 $this->doTestGetFileContents( $src, $content );
674 $this->tearDownFiles();
678 * @dataProvider provider_testGetFileContents
680 public function doTestGetFileContents( $source, $content ) {
681 $backendName = $this->backendClass();
683 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
685 $status = $this->backend
->doOperation(
686 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
687 $this->assertEquals( array(), $status->errors
,
688 "Creation of file at $source succeeded ($backendName)." );
689 $this->assertEquals( true, $status->isOK(),
690 "Creation of file at $source succeeded with OK status ($backendName)." );
692 $newContents = $this->backend
->getFileContents( array( 'src' => $source ) );
693 $this->assertNotEquals( false, $newContents,
694 "Read of file at $source succeeded ($backendName)." );
696 $this->assertEquals( $content, $newContents,
697 "Contents read match data at $source ($backendName)." );
700 function provider_testGetFileContents() {
703 $base = $this->baseStorePath();
704 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
705 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
711 * @dataProvider provider_testGetLocalCopy
713 public function testGetLocalCopy( $src, $content ) {
714 $this->backend
= $this->singleBackend
;
715 $this->tearDownFiles();
716 $this->doTestGetLocalCopy( $src, $content );
717 $this->tearDownFiles();
719 $this->backend
= $this->multiBackend
;
720 $this->tearDownFiles();
721 $this->doTestGetLocalCopy( $src, $content );
722 $this->tearDownFiles();
725 public function doTestGetLocalCopy( $source, $content ) {
726 $backendName = $this->backendClass();
728 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
730 $status = $this->backend
->doOperation(
731 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
732 $this->assertEquals( array(), $status->errors
,
733 "Creation of file at $source succeeded ($backendName)." );
735 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
736 $this->assertNotNull( $tmpFile,
737 "Creation of local copy of $source succeeded ($backendName)." );
739 $contents = file_get_contents( $tmpFile->getPath() );
740 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
743 function provider_testGetLocalCopy() {
746 $base = $this->baseStorePath();
747 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
748 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
754 * @dataProvider provider_testGetLocalReference
756 public function testGetLocalReference( $src, $content ) {
757 $this->backend
= $this->singleBackend
;
758 $this->tearDownFiles();
759 $this->doTestGetLocalReference( $src, $content );
760 $this->tearDownFiles();
762 $this->backend
= $this->multiBackend
;
763 $this->tearDownFiles();
764 $this->doTestGetLocalReference( $src, $content );
765 $this->tearDownFiles();
768 private function doTestGetLocalReference( $source, $content ) {
769 $backendName = $this->backendClass();
771 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
773 $status = $this->backend
->doOperation(
774 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
775 $this->assertEquals( array(), $status->errors
,
776 "Creation of file at $source succeeded ($backendName)." );
778 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
779 $this->assertNotNull( $tmpFile,
780 "Creation of local copy of $source succeeded ($backendName)." );
782 $contents = file_get_contents( $tmpFile->getPath() );
783 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
786 function provider_testGetLocalReference() {
789 $base = $this->baseStorePath();
790 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
791 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
797 * @dataProvider provider_testPrepareAndClean
799 public function testPrepareAndClean( $path, $isOK ) {
800 $this->backend
= $this->singleBackend
;
801 $this->doTestPrepareAndClean( $path, $isOK );
803 $this->backend
= $this->multiBackend
;
804 $this->doTestPrepareAndClean( $path, $isOK );
807 function provider_testPrepareAndClean() {
808 $base = $this->baseStorePath();
810 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
811 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
812 # Specific to FS backend with no basePath field set
813 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
817 function doTestPrepareAndClean( $path, $isOK ) {
818 $backendName = $this->backendClass();
820 $status = $this->backend
->prepare( array( 'dir' => $path ) );
822 $this->assertEquals( array(), $status->errors
,
823 "Preparing dir $path succeeded without warnings ($backendName)." );
824 $this->assertEquals( true, $status->isOK(),
825 "Preparing dir $path succeeded ($backendName)." );
827 $this->assertEquals( false, $status->isOK(),
828 "Preparing dir $path failed ($backendName)." );
831 $status = $this->backend
->clean( array( 'dir' => $path ) );
833 $this->assertEquals( array(), $status->errors
,
834 "Cleaning dir $path succeeded without warnings ($backendName)." );
835 $this->assertEquals( true, $status->isOK(),
836 "Cleaning dir $path succeeded ($backendName)." );
838 $this->assertEquals( false, $status->isOK(),
839 "Cleaning dir $path failed ($backendName)." );
845 public function testDoOperations() {
846 $this->backend
= $this->singleBackend
;
847 $this->doTestDoOperations();
849 $this->backend
= $this->multiBackend
;
850 $this->doTestDoOperations();
853 function doTestDoOperations() {
854 $base = $this->baseStorePath();
856 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
857 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
858 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
859 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
860 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
861 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
862 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
864 $this->backend
->prepare( array( 'dir' => dirname( $fileA ) ) );
865 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
866 $this->backend
->prepare( array( 'dir' => dirname( $fileB ) ) );
867 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
868 $this->backend
->prepare( array( 'dir' => dirname( $fileC ) ) );
869 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
871 $status = $this->backend
->doOperations( array(
872 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
873 // Now: A:<A>, B:<B>, C:<A>, D:<D> (file:<orginal contents>)
874 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
875 // Now: A:<A>, B:<B>, C:<A>, D:<D>
876 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
877 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
878 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
879 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
880 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
881 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
882 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
883 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
884 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
885 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
886 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
887 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
888 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
890 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
892 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
894 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
896 array( 'op' => 'null' ),
900 $this->assertEquals( array(), $status->errors
, "Operation batch succeeded" );
901 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
902 $this->assertEquals( 13, count( $status->success
),
903 "Operation batch has correct success array" );
905 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
906 "File does not exist at $fileA" );
907 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
908 "File does not exist at $fileB" );
909 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
910 "File does not exist at $fileD" );
912 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
913 "File exists at $fileC" );
914 $this->assertEquals( $fileBContents,
915 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
916 "Correct file contents of $fileC" );
917 $this->assertEquals( strlen( $fileBContents ),
918 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
919 "Correct file size of $fileC" );
920 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
921 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
922 "Correct file SHA-1 of $fileC" );
924 // @TODO: test some cases where the ops should fail
927 public function testGetFileList() {
928 $this->backend
= $this->singleBackend
;
929 $this->tearDownFiles();
930 $this->doTestGetFileList();
931 $this->tearDownFiles();
933 $this->backend
= $this->multiBackend
;
934 $this->tearDownFiles();
935 $this->doTestGetFileList();
936 $this->tearDownFiles();
939 private function doTestGetFileList() {
940 $backendName = $this->backendClass();
942 $base = $this->baseStorePath();
944 "$base/unittest-cont1/test1.txt",
945 "$base/unittest-cont1/test2.txt",
946 "$base/unittest-cont1/test3.txt",
947 "$base/unittest-cont1/subdir1/test1.txt",
948 "$base/unittest-cont1/subdir1/test2.txt",
949 "$base/unittest-cont1/subdir2/test3.txt",
950 "$base/unittest-cont1/subdir2/test4.txt",
951 "$base/unittest-cont1/subdir2/subdir/test1.txt",
952 "$base/unittest-cont1/subdir2/subdir/test2.txt",
953 "$base/unittest-cont1/subdir2/subdir/test3.txt",
954 "$base/unittest-cont1/subdir2/subdir/test4.txt",
955 "$base/unittest-cont1/subdir2/subdir/test5.txt",
956 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
957 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
962 foreach ( $files as $file ) {
963 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
964 $this->backend
->prepare( array( 'dir' => dirname( $file ) ) );
966 $status = $this->backend
->doOperations( $ops );
967 $this->assertEquals( array(), $status->errors
,
968 "Creation of files succeeded ($backendName)." );
969 $this->assertEquals( true, $status->isOK(),
970 "Creation of files succeeded with OK status ($backendName)." );
981 "subdir2/subdir/test1.txt",
982 "subdir2/subdir/test2.txt",
983 "subdir2/subdir/test3.txt",
984 "subdir2/subdir/test4.txt",
985 "subdir2/subdir/test5.txt",
986 "subdir2/subdir/sub/test0.txt",
987 "subdir2/subdir/sub/120-px-file.txt",
991 // Actual listing (no trailing slash)
993 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
994 foreach ( $iter as $file ) {
999 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1001 // Actual listing (with trailing slash)
1003 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1004 foreach ( $iter as $file ) {
1009 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1019 "sub/120-px-file.txt",
1023 // Actual listing (no trailing slash)
1025 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1026 foreach ( $iter as $file ) {
1031 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1033 // Actual listing (with trailing slash)
1035 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1036 foreach ( $iter as $file ) {
1041 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1043 foreach ( $files as $file ) {
1044 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
1047 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1048 foreach ( $iter as $iter ) {} // no errors
1051 function tearDownFiles() {
1052 foreach ( $this->filesToPrune
as $file ) {
1055 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1056 foreach ( $containers as $container ) {
1057 $this->deleteFiles( $this->backend
, $container );
1061 private function deleteFiles( $backend, $container ) {
1062 $base = $this->baseStorePath();
1063 $iter = $backend->getFileList( array( 'dir' => "$base/$container" ) );
1065 foreach ( $iter as $file ) { // delete files
1066 $backend->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
1068 foreach ( $iter as $file ) { // delete dirs
1070 while ( $tmp = FileBackend
::parentStoragePath( $tmp ) ) {
1071 $backend->clean( array( 'dir' => $tmp ) );
1077 function tearDown() {