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, $dest, $alreadyExists, $okStatus, $newSize ) {
416 $this->backend
= $this->singleBackend
;
417 $this->tearDownFiles();
418 $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize );
419 $this->tearDownFiles();
421 $this->backend
= $this->multiBackend
;
422 $this->tearDownFiles();
423 $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize );
424 $this->tearDownFiles();
427 private function doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
428 $backendName = $this->backendClass();
430 $this->backend
->prepare( array( 'dir' => dirname( $dest ) ) );
432 $oldText = 'blah...blah...waahwaah';
433 if ( $alreadyExists ) {
434 $status = $this->backend
->doOperation(
435 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
436 $this->assertEquals( array(), $status->errors
,
437 "Creation of file at $dest succeeded ($backendName)." );
440 $status = $this->backend
->doOperation( $op );
442 $this->assertEquals( array(), $status->errors
,
443 "Creation of file at $dest succeeded without warnings ($backendName)." );
444 $this->assertEquals( true, $status->isOK(),
445 "Creation of file at $dest succeeded ($backendName)." );
446 $this->assertEquals( array( 0 => true ), $status->success
,
447 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
449 $this->assertEquals( false, $status->isOK(),
450 "Creation of file at $dest failed ($backendName)." );
453 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
454 "Destination file $dest exists after creation ($backendName)." );
456 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
457 $this->assertEquals( true, $props1['fileExists'],
458 "Destination file $dest exists according to props ($backendName)." );
459 if ( $okStatus ) { // file content is what we saved
460 $this->assertEquals( $newSize, $props1['size'],
461 "Destination file $dest has expected size according to props ($backendName)." );
462 $this->assertEquals( $newSize,
463 $this->backend
->getFileSize( array( 'src' => $dest ) ),
464 "Destination file $dest has correct size ($backendName)." );
465 } else { // file content is some other previous text
466 $this->assertEquals( strlen( $oldText ), $props1['size'],
467 "Destination file $dest has original size according to props ($backendName)." );
468 $this->assertEquals( strlen( $oldText ),
469 $this->backend
->getFileSize( array( 'src' => $dest ) ),
470 "Destination file $dest has original size according to props ($backendName)." );
475 * @dataProvider provider_testCreate
477 public function provider_testCreate() {
480 $source = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
482 $dummyText = 'hey hey';
483 $op = array( 'op' => 'create', 'content' => $dummyText, 'dst' => $source );
487 false, // no dest already exists
495 true, // dest already exists
501 $op2['overwrite'] = true;
505 true, // dest already exists
511 $op2['overwriteSame'] = true;
515 true, // dest already exists
524 * @dataProvider provider_testConcatenate
526 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
527 $this->filesToPrune
[] = $op['dst'];
529 $this->backend
= $this->singleBackend
;
530 $this->tearDownFiles();
531 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
532 $this->tearDownFiles();
534 $this->backend
= $this->multiBackend
;
535 $this->tearDownFiles();
536 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
537 $this->tearDownFiles();
540 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
541 $backendName = $this->backendClass();
546 foreach ( $srcs as $i => $source ) {
547 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
549 'op' => 'create', // operation
550 'dst' => $source, // source
551 'content' => $srcsContent[$i]
553 $expContent .= $srcsContent[$i];
555 $status = $this->backend
->doOperations( $ops );
557 $this->assertEquals( array(), $status->errors
,
558 "Creation of source files succeeded ($backendName)." );
560 $dest = $params['dst'];
561 if ( $alreadyExists ) {
562 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
563 $this->assertEquals( true, $ok,
564 "Creation of file at $dest succeeded ($backendName)." );
566 $ok = file_put_contents( $dest, '' ) !== false;
567 $this->assertEquals( true, $ok,
568 "Creation of 0-byte file at $dest succeeded ($backendName)." );
571 // Combine the files into one
572 $status = $this->backend
->concatenate( $params );
574 $this->assertEquals( array(), $status->errors
,
575 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
576 $this->assertEquals( true, $status->isOK(),
577 "Creation of concat file at $dest succeeded ($backendName)." );
579 $this->assertEquals( false, $status->isOK(),
580 "Creation of concat file at $dest failed ($backendName)." );
584 $this->assertEquals( true, is_file( $dest ),
585 "Dest concat file $dest exists after creation ($backendName)." );
587 $this->assertEquals( true, is_file( $dest ),
588 "Dest concat file $dest exists after failed creation ($backendName)." );
591 $contents = file_get_contents( $dest );
592 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
595 $this->assertEquals( $expContent, $contents,
596 "Concat file at $dest has correct contents ($backendName)." );
598 $this->assertNotEquals( $expContent, $contents,
599 "Concat file at $dest has correct contents ($backendName)." );
603 function provider_testConcatenate() {
606 $rand = mt_rand( 0, 2000000000 ) . time();
607 $dest = wfTempDir() . "/randomfile!$rand.txt";
609 $this->baseStorePath() . '/unittest-cont1/file1.txt',
610 $this->baseStorePath() . '/unittest-cont1/file2.txt',
611 $this->baseStorePath() . '/unittest-cont1/file3.txt',
612 $this->baseStorePath() . '/unittest-cont1/file4.txt',
613 $this->baseStorePath() . '/unittest-cont1/file5.txt',
614 $this->baseStorePath() . '/unittest-cont1/file6.txt',
615 $this->baseStorePath() . '/unittest-cont1/file7.txt',
616 $this->baseStorePath() . '/unittest-cont1/file8.txt',
617 $this->baseStorePath() . '/unittest-cont1/file9.txt',
618 $this->baseStorePath() . '/unittest-cont1/file10.txt'
632 $params = array( 'srcs' => $srcs, 'dst' => $dest );
635 $params, // operation
637 $content, // content for each source
638 false, // no dest already exists
643 $params, // operation
645 $content, // content for each source
646 true, // dest already exists
654 * @dataProvider provider_testGetFileContents
656 public function testGetFileContents( $src, $content ) {
657 $this->backend
= $this->singleBackend
;
658 $this->tearDownFiles();
659 $this->doTestGetFileContents( $src, $content );
660 $this->tearDownFiles();
662 $this->backend
= $this->multiBackend
;
663 $this->tearDownFiles();
664 $this->doTestGetFileContents( $src, $content );
665 $this->tearDownFiles();
669 * @dataProvider provider_testGetFileContents
671 public function doTestGetFileContents( $source, $content ) {
672 $backendName = $this->backendClass();
674 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
676 $status = $this->backend
->doOperation(
677 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
678 $this->assertEquals( array(), $status->errors
,
679 "Creation of file at $source succeeded ($backendName)." );
680 $this->assertEquals( true, $status->isOK(),
681 "Creation of file at $source succeeded with OK status ($backendName)." );
683 $newContents = $this->backend
->getFileContents( array( 'src' => $source ) );
684 $this->assertNotEquals( false, $newContents,
685 "Read of file at $source succeeded ($backendName)." );
687 $this->assertEquals( $content, $newContents,
688 "Contents read match data at $source ($backendName)." );
691 function provider_testGetFileContents() {
694 $base = $this->baseStorePath();
695 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
696 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
702 * @dataProvider provider_testGetLocalCopy
704 public function testGetLocalCopy( $src, $content ) {
705 $this->backend
= $this->singleBackend
;
706 $this->tearDownFiles();
707 $this->doTestGetLocalCopy( $src, $content );
708 $this->tearDownFiles();
710 $this->backend
= $this->multiBackend
;
711 $this->tearDownFiles();
712 $this->doTestGetLocalCopy( $src, $content );
713 $this->tearDownFiles();
716 public function doTestGetLocalCopy( $source, $content ) {
717 $backendName = $this->backendClass();
719 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
721 $status = $this->backend
->doOperation(
722 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
723 $this->assertEquals( array(), $status->errors
,
724 "Creation of file at $source succeeded ($backendName)." );
726 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
727 $this->assertNotNull( $tmpFile,
728 "Creation of local copy of $source succeeded ($backendName)." );
730 $contents = file_get_contents( $tmpFile->getPath() );
731 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
734 function provider_testGetLocalCopy() {
737 $base = $this->baseStorePath();
738 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
739 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
745 * @dataProvider provider_testGetLocalReference
747 public function testGetLocalReference( $src, $content ) {
748 $this->backend
= $this->singleBackend
;
749 $this->tearDownFiles();
750 $this->doTestGetLocalReference( $src, $content );
751 $this->tearDownFiles();
753 $this->backend
= $this->multiBackend
;
754 $this->tearDownFiles();
755 $this->doTestGetLocalReference( $src, $content );
756 $this->tearDownFiles();
759 private function doTestGetLocalReference( $source, $content ) {
760 $backendName = $this->backendClass();
762 $this->backend
->prepare( array( 'dir' => dirname( $source ) ) );
764 $status = $this->backend
->doOperation(
765 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
766 $this->assertEquals( array(), $status->errors
,
767 "Creation of file at $source succeeded ($backendName)." );
769 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
770 $this->assertNotNull( $tmpFile,
771 "Creation of local copy of $source succeeded ($backendName)." );
773 $contents = file_get_contents( $tmpFile->getPath() );
774 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
777 function provider_testGetLocalReference() {
780 $base = $this->baseStorePath();
781 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
782 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
788 * @dataProvider provider_testPrepareAndClean
790 public function testPrepareAndClean( $path, $isOK ) {
791 $this->backend
= $this->singleBackend
;
792 $this->doTestPrepareAndClean( $path, $isOK );
794 $this->backend
= $this->multiBackend
;
795 $this->doTestPrepareAndClean( $path, $isOK );
798 function provider_testPrepareAndClean() {
799 $base = $this->baseStorePath();
801 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
802 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
803 # Specific to FS backend with no basePath field set
804 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
808 function doTestPrepareAndClean( $path, $isOK ) {
809 $backendName = $this->backendClass();
811 $status = $this->backend
->prepare( array( 'dir' => $path ) );
813 $this->assertEquals( array(), $status->errors
,
814 "Preparing dir $path succeeded without warnings ($backendName)." );
815 $this->assertEquals( true, $status->isOK(),
816 "Preparing dir $path succeeded ($backendName)." );
818 $this->assertEquals( false, $status->isOK(),
819 "Preparing dir $path failed ($backendName)." );
822 $status = $this->backend
->clean( array( 'dir' => $path ) );
824 $this->assertEquals( array(), $status->errors
,
825 "Cleaning dir $path succeeded without warnings ($backendName)." );
826 $this->assertEquals( true, $status->isOK(),
827 "Cleaning dir $path succeeded ($backendName)." );
829 $this->assertEquals( false, $status->isOK(),
830 "Cleaning dir $path failed ($backendName)." );
836 public function testDoOperations() {
837 $this->backend
= $this->singleBackend
;
838 $this->doTestDoOperations();
840 $this->backend
= $this->multiBackend
;
841 $this->doTestDoOperations();
844 function doTestDoOperations() {
845 $base = $this->baseStorePath();
847 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
848 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
849 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
850 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
851 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
852 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
853 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
855 $this->backend
->prepare( array( 'dir' => dirname( $fileA ) ) );
856 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
857 $this->backend
->prepare( array( 'dir' => dirname( $fileB ) ) );
858 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
859 $this->backend
->prepare( array( 'dir' => dirname( $fileC ) ) );
860 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
862 $status = $this->backend
->doOperations( array(
863 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
864 // Now: A:<A>, B:<B>, C:<A>, D:<D> (file:<orginal contents>)
865 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
866 // Now: A:<A>, B:<B>, C:<A>, D:<D>
867 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
868 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
869 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
870 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
871 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
872 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
873 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
874 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
875 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
876 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
877 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
878 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
879 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
881 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
883 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
885 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
887 array( 'op' => 'null' ),
891 $this->assertEquals( array(), $status->errors
, "Operation batch succeeded" );
892 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
893 $this->assertEquals( 13, count( $status->success
),
894 "Operation batch has correct success array" );
896 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
897 "File does not exist at $fileA" );
898 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
899 "File does not exist at $fileB" );
900 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
901 "File does not exist at $fileD" );
903 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
904 "File exists at $fileC" );
905 $this->assertEquals( $fileBContents,
906 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
907 "Correct file contents of $fileC" );
908 $this->assertEquals( strlen( $fileBContents ),
909 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
910 "Correct file size of $fileC" );
911 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
912 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
913 "Correct file SHA-1 of $fileC" );
915 // @TODO: test some cases where the ops should fail
918 public function testGetFileList() {
919 $this->backend
= $this->singleBackend
;
920 $this->tearDownFiles();
921 $this->doTestGetFileList();
922 $this->tearDownFiles();
924 $this->backend
= $this->multiBackend
;
925 $this->tearDownFiles();
926 $this->doTestGetFileList();
927 $this->tearDownFiles();
930 private function doTestGetFileList() {
931 $backendName = $this->backendClass();
933 $base = $this->baseStorePath();
935 "$base/unittest-cont1/test1.txt",
936 "$base/unittest-cont1/test2.txt",
937 "$base/unittest-cont1/test3.txt",
938 "$base/unittest-cont1/subdir1/test1.txt",
939 "$base/unittest-cont1/subdir1/test2.txt",
940 "$base/unittest-cont1/subdir2/test3.txt",
941 "$base/unittest-cont1/subdir2/test4.txt",
942 "$base/unittest-cont1/subdir2/subdir/test1.txt",
943 "$base/unittest-cont1/subdir2/subdir/test2.txt",
944 "$base/unittest-cont1/subdir2/subdir/test3.txt",
945 "$base/unittest-cont1/subdir2/subdir/test4.txt",
946 "$base/unittest-cont1/subdir2/subdir/test5.txt",
947 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
948 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
953 foreach ( $files as $file ) {
954 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
955 $this->backend
->prepare( array( 'dir' => dirname( $file ) ) );
957 $status = $this->backend
->doOperations( $ops );
958 $this->assertEquals( array(), $status->errors
,
959 "Creation of files succeeded ($backendName)." );
960 $this->assertEquals( true, $status->isOK(),
961 "Creation of files succeeded with OK status ($backendName)." );
972 "subdir2/subdir/test1.txt",
973 "subdir2/subdir/test2.txt",
974 "subdir2/subdir/test3.txt",
975 "subdir2/subdir/test4.txt",
976 "subdir2/subdir/test5.txt",
977 "subdir2/subdir/sub/test0.txt",
978 "subdir2/subdir/sub/120-px-file.txt",
982 // Actual listing (no trailing slash)
984 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
985 foreach ( $iter as $file ) {
990 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
992 // Actual listing (with trailing slash)
994 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
995 foreach ( $iter as $file ) {
1000 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1010 "sub/120-px-file.txt",
1014 // Actual listing (no trailing slash)
1016 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1017 foreach ( $iter as $file ) {
1022 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1024 // Actual listing (with trailing slash)
1026 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1027 foreach ( $iter as $file ) {
1032 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1034 foreach ( $files as $file ) {
1035 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
1038 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1039 foreach ( $iter as $iter ) {} // no errors
1042 function tearDownFiles() {
1043 foreach ( $this->filesToPrune
as $file ) {
1046 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1047 foreach ( $containers as $container ) {
1048 $this->deleteFiles( $this->backend
, $container );
1052 private function deleteFiles( $backend, $container ) {
1053 $base = $this->baseStorePath();
1054 $iter = $backend->getFileList( array( 'dir' => "$base/$container" ) );
1056 foreach ( $iter as $file ) {
1057 $backend->delete( array( 'src' => "$base/$container/$file", 'ignoreMissingSource' => 1 ) );
1059 while ( $tmp = FileBackend
::parentStoragePath( $tmp ) ) {
1060 $backend->clean( array( 'dir' => $tmp ) );
1066 function tearDown() {