3f32891f5c6e095dd762714df72d4ec9a334f43f
3 // @TODO: fix empty dir leakage
4 class FileBackendTest
extends MediaWikiTestCase
{
5 private $backend, $multiBackend;
6 private $filesToPrune, $pathsToPrune;
10 $tmpDir = wfTempDir() . '/' . time() . '-' . mt_rand();
11 $this->singleBackend
= new FSFileBackend( array(
12 'name' => 'localtesting',
13 'lockManager' => 'fsLockManager',
14 'containerPaths' => array(
15 'cont1' => "$tmpDir/localtesting/cont1",
16 'cont2' => "$tmpDir/localtesting/cont2" )
18 $this->multiBackend
= new FileBackendMultiWrite( array(
19 'name' => 'localtesting',
20 'lockManager' => 'fsLockManager',
23 'name' => 'localmutlitesting1',
24 'class' => 'FSFileBackend',
25 'lockManager' => 'nullLockManager',
26 'containerPaths' => array(
27 'cont1' => "$tmpDir/localtestingmulti1/cont1",
28 'cont2' => "$tmpDir/localtestingmulti1/cont2" ),
29 'isMultiMaster' => false
32 'name' => 'localmutlitesting2',
33 'class' => 'FSFileBackend',
34 'lockManager' => 'nullLockManager',
35 'containerPaths' => array(
36 'cont1' => "$tmpDir/localtestingmulti2/cont1",
37 'cont2' => "$tmpDir/localtestingmulti2/cont2" ),
38 'isMultiMaster' => true
42 $this->filesToPrune
= $this->pathsToPrune
= array();
45 private function baseStorePath() {
46 return 'mwstore://localtesting';
49 private function backendClass() {
50 return get_class( $this->backend
);
54 * @dataProvider provider_testStore
56 public function testStore( $op, $source, $dest ) {
57 $this->filesToPrune
[] = $source;
58 $this->pathsToPrune
[] = $dest;
60 $this->backend
= $this->singleBackend
;
61 $this->doTestStore( $op, $source, $dest );
62 $this->tearDownFiles();
64 $this->backend
= $this->multiBackend
;
65 $this->doTestStore( $op, $source, $dest );
66 $this->tearDownFiles();
69 function doTestStore( $op, $source, $dest ) {
70 $backendName = $this->backendClass();
72 file_put_contents( $source, "Unit test file" );
73 $status = $this->backend
->doOperation( $op );
75 $this->assertEquals( array(), $status->errors
,
76 "Store from $source to $dest succeeded without warnings ($backendName)." );
77 $this->assertEquals( true, $status->isOK(),
78 "Store from $source to $dest succeeded ($backendName)." );
79 $this->assertEquals( true, file_exists( $source ),
80 "Source file $source still exists ($backendName)." );
81 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
82 "Destination file $dest exists ($backendName)." );
84 $this->assertEquals( filesize( $source ),
85 $this->backend
->getFileSize( array( 'src' => $dest ) ),
86 "Destination file $dest has correct size ($backendName)." );
88 $props1 = FSFile
::getPropsFromPath( $source );
89 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
90 $this->assertEquals( $props1, $props2,
91 "Source and destination have the same props ($backendName)." );
94 public function provider_testStore() {
97 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
98 $toPath = $this->baseStorePath() . '/cont1/fun/obj1.txt';
99 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
106 $op['overwriteDest'] = true;
117 * @dataProvider provider_testCopy
119 public function testCopy( $op, $source, $dest ) {
120 $this->pathsToPrune
[] = $source;
121 $this->pathsToPrune
[] = $dest;
123 $this->backend
= $this->singleBackend
;
124 $this->doTestCopy( $op, $source, $dest );
125 $this->tearDownFiles();
127 $this->backend
= $this->multiBackend
;
128 $this->doTestCopy( $op, $source, $dest );
129 $this->tearDownFiles();
132 function doTestCopy( $op, $source, $dest ) {
133 $backendName = $this->backendClass();
135 $status = $this->backend
->doOperation(
136 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
137 $this->assertEquals( true, $status->isOK(),
138 "Creation of file at $source succeeded ($backendName)." );
140 $status = $this->backend
->doOperation( $op );
141 $this->assertEquals( array(), $status->errors
,
142 "Copy from $source to $dest succeeded without warnings ($backendName)." );
143 $this->assertEquals( true, $status->isOK(),
144 "Copy from $source to $dest succeeded ($backendName)." );
145 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
146 "Source file $source still exists ($backendName)." );
147 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
148 "Destination file $dest exists after copy ($backendName)." );
151 $this->backend
->getFileSize( array( 'src' => $source ) ),
152 $this->backend
->getFileSize( array( 'src' => $dest ) ),
153 "Destination file $dest has correct size ($backendName)." );
155 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
156 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
157 $this->assertEquals( $props1, $props2,
158 "Source and destination have the same props ($backendName)." );
161 public function provider_testCopy() {
164 $source = $this->baseStorePath() . '/cont1/file.txt';
165 $dest = $this->baseStorePath() . '/cont2/fileMoved.txt';
167 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
174 $op['overwriteDest'] = true;
185 * @dataProvider provider_testMove
187 public function testMove( $op, $source, $dest ) {
188 $this->pathsToPrune
[] = $source;
189 $this->pathsToPrune
[] = $dest;
191 $this->backend
= $this->singleBackend
;
192 $this->doTestMove( $op, $source, $dest );
193 $this->tearDownFiles();
195 $this->backend
= $this->multiBackend
;
196 $this->doTestMove( $op, $source, $dest );
197 $this->tearDownFiles();
200 public function doTestMove( $op, $source, $dest ) {
201 $backendName = $this->backendClass();
203 $status = $this->backend
->doOperation(
204 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
205 $this->assertEquals( true, $status->isOK(),
206 "Creation of file at $source succeeded ($backendName)." );
208 $status = $this->backend
->doOperation( $op );
209 $this->assertEquals( array(), $status->errors
,
210 "Move from $source to $dest succeeded without warnings ($backendName)." );
211 $this->assertEquals( true, $status->isOK(),
212 "Move from $source to $dest succeeded ($backendName)." );
213 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
214 "Source file $source does not still exists ($backendName)." );
215 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
216 "Destination file $dest exists after move ($backendName)." );
218 $this->assertNotEquals(
219 $this->backend
->getFileSize( array( 'src' => $source ) ),
220 $this->backend
->getFileSize( array( 'src' => $dest ) ),
221 "Destination file $dest has correct size ($backendName)." );
223 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
224 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
225 $this->assertEquals( false, $props1['fileExists'],
226 "Source file does not exist accourding to props ($backendName)." );
227 $this->assertEquals( true, $props2['fileExists'],
228 "Destination file exists accourding to props ($backendName)." );
231 public function provider_testMove() {
234 $source = $this->baseStorePath() . '/cont1/file.txt';
235 $dest = $this->baseStorePath() . '/cont2/fileMoved.txt';
237 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
244 $op['overwriteDest'] = true;
255 * @dataProvider provider_testDelete
257 public function testDelete( $op, $source, $withSource, $okStatus ) {
258 $this->pathsToPrune
[] = $source;
260 $this->backend
= $this->singleBackend
;
261 $this->doTestDelete( $op, $source, $withSource, $okStatus );
262 $this->tearDownFiles();
264 $this->backend
= $this->multiBackend
;
265 $this->doTestDelete( $op, $source, $withSource, $okStatus );
266 $this->tearDownFiles();
269 public function doTestDelete( $op, $source, $withSource, $okStatus ) {
270 $backendName = $this->backendClass();
273 $status = $this->backend
->doOperation(
274 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
275 $this->assertEquals( true, $status->isOK(),
276 "Creation of file at $source succeeded ($backendName)." );
279 $status = $this->backend
->doOperation( $op );
281 $this->assertEquals( array(), $status->errors
,
282 "Deletion of file at $source succeeded without warnings ($backendName)." );
283 $this->assertEquals( true, $status->isOK(),
284 "Deletion of file at $source succeeded ($backendName)." );
286 $this->assertEquals( false, $status->isOK(),
287 "Deletion of file at $source failed ($backendName)." );
290 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
291 "Source file $source does not exist after move ($backendName)." );
294 $this->backend
->getFileSize( array( 'src' => $source ) ),
295 "Source file $source has correct size (false) ($backendName)." );
297 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
298 $this->assertFalse( $props1['fileExists'],
299 "Source file $source does not exist according to props ($backendName)." );
302 public function provider_testDelete() {
305 $source = $this->baseStorePath() . '/cont1/myfacefile.txt';
307 $op = array( 'op' => 'delete', 'src' => $source );
318 false, // without source
322 $op['ignoreMissingSource'] = true;
326 false, // without source
334 * @dataProvider provider_testCreate
336 public function testCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
337 $this->pathsToPrune
[] = $dest;
339 $this->backend
= $this->singleBackend
;
340 $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize );
341 $this->tearDownFiles();
343 $this->backend
= $this->multiBackend
;
344 $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize );
345 $this->tearDownFiles();
348 public function doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
349 $backendName = $this->backendClass();
351 $oldText = 'blah...blah...waahwaah';
352 if ( $alreadyExists ) {
353 $status = $this->backend
->doOperation(
354 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
355 $this->assertEquals( true, $status->isOK(),
356 "Creation of file at $dest succeeded ($backendName)." );
359 $status = $this->backend
->doOperation( $op );
361 $this->assertEquals( array(), $status->errors
,
362 "Creation of file at $dest succeeded without warnings ($backendName)." );
363 $this->assertEquals( true, $status->isOK(),
364 "Creation of file at $dest succeeded ($backendName)." );
366 $this->assertEquals( false, $status->isOK(),
367 "Creation of file at $dest failed ($backendName)." );
370 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
371 "Destination file $dest exists after creation ($backendName)." );
373 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
374 $this->assertEquals( true, $props1['fileExists'],
375 "Destination file $dest exists according to props ($backendName)." );
376 if ( $okStatus ) { // file content is what we saved
377 $this->assertEquals( $newSize, $props1['size'],
378 "Destination file $dest has expected size according to props ($backendName)." );
379 $this->assertEquals( $newSize,
380 $this->backend
->getFileSize( array( 'src' => $dest ) ),
381 "Destination file $dest has correct size ($backendName)." );
382 } else { // file content is some other previous text
383 $this->assertEquals( strlen( $oldText ), $props1['size'],
384 "Destination file $dest has original size according to props ($backendName)." );
385 $this->assertEquals( strlen( $oldText ),
386 $this->backend
->getFileSize( array( 'src' => $dest ) ),
387 "Destination file $dest has original size according to props ($backendName)." );
392 * @dataProvider provider_testCreate
394 public function provider_testCreate() {
397 $source = $this->baseStorePath() . '/cont2/myspacefile.txt';
399 $dummyText = 'hey hey';
400 $op = array( 'op' => 'create', 'content' => $dummyText, 'dst' => $source );
404 false, // no dest already exists
412 true, // dest already exists
417 $op['overwriteDest'] = true;
421 true, // dest already exists
430 * @dataProvider provider_testConcatenate
432 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
433 $this->pathsToPrune
= array_merge( $this->pathsToPrune
, $srcs );
434 $this->filesToPrune
[] = $op['dst'];
436 $this->backend
= $this->singleBackend
;
437 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
438 $this->tearDownFiles();
441 #$this->backend = $this->multiBackend;
442 #$this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
443 #$this->tearDownFiles();
446 public function doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
447 $backendName = $this->backendClass();
452 foreach ( $srcs as $i => $source ) {
454 'op' => 'create', // operation
455 'dst' => $source, // source
456 'content' => $srcsContent[$i]
458 $expContent .= $srcsContent[$i];
460 $status = $this->backend
->doOperations( $ops );
462 $this->assertEquals( true, $status->isOK(),
463 "Creation of source files succeeded ($backendName)." );
466 if ( $alreadyExists ) {
467 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
468 $this->assertEquals( true, $ok,
469 "Creation of file at $dest succeeded ($backendName)." );
471 $ok = file_put_contents( $dest, '' ) !== false;
472 $this->assertEquals( true, $ok,
473 "Creation of 0-byte file at $dest succeeded ($backendName)." );
477 $status = $this->backend
->doOperation( $op );
479 $this->assertEquals( array(), $status->errors
,
480 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
481 $this->assertEquals( true, $status->isOK(),
482 "Creation of concat file at $dest succeeded ($backendName)." );
484 $this->assertEquals( false, $status->isOK(),
485 "Creation of concat file at $dest failed ($backendName)." );
489 $this->assertEquals( true, is_file( $dest ),
490 "Dest concat file $dest exists after creation ($backendName)." );
492 $this->assertEquals( true, is_file( $dest ),
493 "Dest concat file $dest exists after failed creation ($backendName)." );
496 $contents = file_get_contents( $dest );
497 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
500 $this->assertEquals( $expContent, $contents,
501 "Concat file at $dest has correct contents ($backendName)." );
503 $this->assertNotEquals( $expContent, $contents,
504 "Concat file at $dest has correct contents ($backendName)." );
508 function provider_testConcatenate() {
511 $rand = mt_rand( 0, 2000000000 ) . time();
512 $dest = wfTempDir() . "/randomfile!$rand.txt";
514 $this->baseStorePath() . '/cont1/file1.txt',
515 $this->baseStorePath() . '/cont1/file2.txt',
516 $this->baseStorePath() . '/cont1/file3.txt',
517 $this->baseStorePath() . '/cont1/file4.txt',
518 $this->baseStorePath() . '/cont1/file5.txt',
519 $this->baseStorePath() . '/cont1/file6.txt',
520 $this->baseStorePath() . '/cont1/file7.txt',
521 $this->baseStorePath() . '/cont1/file8.txt',
522 $this->baseStorePath() . '/cont1/file9.txt',
523 $this->baseStorePath() . '/cont1/file10.txt'
537 $op = array( 'op' => 'concatenate', 'srcs' => $srcs, 'dst' => $dest );
542 $content, // content for each source
543 false, // no dest already exists
550 $content, // content for each source
551 true, // dest already exists
559 * @dataProvider provider_testGetFileContents
561 public function testGetFileContents( $src, $content ) {
562 $this->pathsToPrune
[] = $src;
564 $this->backend
= $this->singleBackend
;
565 $this->doTestGetFileContents( $src, $content );
566 $this->tearDownFiles();
568 $this->backend
= $this->multiBackend
;
569 $this->doTestGetFileContents( $src, $content );
570 $this->tearDownFiles();
574 * @dataProvider provider_testGetFileContents
576 public function doTestGetFileContents( $src, $content ) {
577 $backendName = $this->backendClass();
579 $status = $this->backend
->doOperation(
580 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
581 $this->assertEquals( true, $status->isOK(),
582 "Creation of file at $src succeeded ($backendName)." );
584 $newContents = $this->backend
->getFileContents( array( 'src' => $src ) );
585 $this->assertNotEquals( false, $newContents,
586 "Read of file at $src succeeded ($backendName)." );
588 $this->assertEquals( $content, $newContents,
589 "Contents read match data at $src ($backendName)." );
592 function provider_testGetFileContents() {
595 $base = $this->baseStorePath();
596 $cases[] = array( "$base/cont1/b/z/some_file.txt", "some file contents" );
597 $cases[] = array( "$base/cont1/b/some-other_file.txt", "more file contents" );
603 * @dataProvider provider_testGetLocalCopy
605 public function testGetLocalCopy( $src, $content ) {
606 $this->pathsToPrune
[] = $src;
608 $this->backend
= $this->singleBackend
;
609 $this->doTestGetLocalCopy( $src, $content );
610 $this->tearDownFiles();
612 $this->backend
= $this->multiBackend
;
613 $this->doTestGetLocalCopy( $src, $content );
614 $this->tearDownFiles();
617 public function doTestGetLocalCopy( $src, $content ) {
618 $backendName = $this->backendClass();
620 $status = $this->backend
->doOperation(
621 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
622 $this->assertEquals( true, $status->isOK(),
623 "Creation of file at $src succeeded ($backendName)." );
625 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $src ) );
626 $this->assertNotNull( $tmpFile,
627 "Creation of local copy of $src succeeded ($backendName)." );
629 $contents = file_get_contents( $tmpFile->getPath() );
630 $this->assertNotEquals( false, $contents, "Local copy of $src exists ($backendName)." );
633 function provider_testGetLocalCopy() {
636 $base = $this->baseStorePath();
637 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
638 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
644 * @dataProvider provider_testGetLocalReference
646 public function testGetLocalReference( $src, $content ) {
647 $this->pathsToPrune
[] = $src;
649 $this->backend
= $this->singleBackend
;
650 $this->doTestGetLocalReference( $src, $content );
651 $this->tearDownFiles();
653 $this->backend
= $this->multiBackend
;
654 $this->doTestGetLocalReference( $src, $content );
655 $this->tearDownFiles();
658 public function doTestGetLocalReference( $src, $content ) {
659 $backendName = $this->backendClass();
661 $status = $this->backend
->doOperation(
662 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
663 $this->assertEquals( true, $status->isOK(),
664 "Creation of file at $src succeeded ($backendName)." );
666 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $src ) );
667 $this->assertNotNull( $tmpFile,
668 "Creation of local copy of $src succeeded ($backendName)." );
670 $contents = file_get_contents( $tmpFile->getPath() );
671 $this->assertNotEquals( false, $contents, "Local copy of $src exists ($backendName)." );
674 function provider_testGetLocalReference() {
677 $base = $this->baseStorePath();
678 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
679 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
684 // @TODO: testPrepare
690 // @TODO: testDoOperations
692 public function testGetFileList() {
693 $this->backend
= $this->singleBackend
;
694 $this->doTestGetFileList();
695 $this->tearDownFiles();
697 $this->backend
= $this->multiBackend
;
698 $this->doTestGetFileList();
699 $this->tearDownFiles();
702 public function doTestGetFileList() {
703 $backendName = $this->backendClass();
705 $base = $this->baseStorePath();
707 "$base/cont1/test1.txt",
708 "$base/cont1/test2.txt",
709 "$base/cont1/test3.txt",
710 "$base/cont1/subdir1/test1.txt",
711 "$base/cont1/subdir1/test2.txt",
712 "$base/cont1/subdir2/test3.txt",
713 "$base/cont1/subdir2/test4.txt",
714 "$base/cont1/subdir2/subdir/test1.txt",
715 "$base/cont1/subdir2/subdir/test2.txt",
716 "$base/cont1/subdir2/subdir/test3.txt",
717 "$base/cont1/subdir2/subdir/test4.txt",
718 "$base/cont1/subdir2/subdir/test5.txt",
719 "$base/cont1/subdir2/subdir/sub/test0.txt",
720 "$base/cont1/subdir2/subdir/sub/120-px-file.txt",
722 $this->pathsToPrune
= array_merge( $this->pathsToPrune
, $files );
726 foreach ( $files as $file ) {
727 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
729 $status = $this->backend
->doOperations( $ops );
730 $this->assertEquals( true, $status->isOK(),
731 "Creation of files succeeded ($backendName)." );
742 "subdir2/subdir/test1.txt",
743 "subdir2/subdir/test2.txt",
744 "subdir2/subdir/test3.txt",
745 "subdir2/subdir/test4.txt",
746 "subdir2/subdir/test5.txt",
747 "subdir2/subdir/sub/test0.txt",
748 "subdir2/subdir/sub/120-px-file.txt",
752 // Actual listing (no trailing slash)
754 $iter = $this->backend
->getFileList( array( 'dir' => "$base/cont1" ) );
755 foreach ( $iter as $file ) {
760 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
762 // Actual listing (with trailing slash)
764 $iter = $this->backend
->getFileList( array( 'dir' => "$base/cont1/" ) );
765 foreach ( $iter as $file ) {
770 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
772 foreach ( $files as $file ) {
773 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
776 $iter = $this->backend
->getFileList( array( 'dir' => "$base/cont1/not/exists" ) );
777 foreach ( $iter as $iter ) {} // no errors
780 function tearDownFiles() {
781 foreach ( $this->filesToPrune
as $file ) {
784 foreach ( $this->pathsToPrune
as $file ) {
785 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
789 function tearDown() {