7 class FileBackendTest
extends MediaWikiTestCase
{
8 private $backend, $multiBackend;
9 private $filesToPrune = array();
10 private $dirsToPrune = array();
11 private static $backendToUse;
14 global $wgFileBackends;
16 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
17 if ( $this->getCliArg( 'use-filebackend=' ) ) {
18 if ( self
::$backendToUse ) {
19 $this->singleBackend
= self
::$backendToUse;
21 $name = $this->getCliArg( 'use-filebackend=' );
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
28 $useConfig['name'] = 'localtesting'; // swap name
29 $class = $conf['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 'containerPaths' => array(
38 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
39 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
42 $this->multiBackend
= new FileBackendMultiWrite( array(
43 'name' => 'localtesting',
44 'lockManager' => 'fsLockManager',
47 'name' => 'localmutlitesting1',
48 'class' => 'FSFileBackend',
49 'lockManager' => 'nullLockManager',
50 'containerPaths' => array(
51 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
52 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
53 'isMultiMaster' => false
56 'name' => 'localmutlitesting2',
57 'class' => 'FSFileBackend',
58 'lockManager' => 'nullLockManager',
59 'containerPaths' => array(
60 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
61 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
62 'isMultiMaster' => true
66 $this->filesToPrune
= array();
69 private function baseStorePath() {
70 return 'mwstore://localtesting';
73 private function backendClass() {
74 return get_class( $this->backend
);
78 * @dataProvider provider_testIsStoragePath
80 public function testIsStoragePath( $path, $isStorePath ) {
81 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
82 "FileBackend::isStoragePath on path '$path'" );
85 function provider_testIsStoragePath() {
87 array( 'mwstore://', true ),
88 array( 'mwstore://backend', true ),
89 array( 'mwstore://backend/container', true ),
90 array( 'mwstore://backend/container/', true ),
91 array( 'mwstore://backend/container/path', true ),
92 array( 'mwstore://backend//container/', true ),
93 array( 'mwstore://backend//container//', true ),
94 array( 'mwstore://backend//container//path', true ),
95 array( 'mwstore:///', true ),
96 array( 'mwstore:/', false ),
97 array( 'mwstore:', false ),
102 * @dataProvider provider_testSplitStoragePath
104 public function testSplitStoragePath( $path, $res ) {
105 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
106 "FileBackend::splitStoragePath on path '$path'" );
109 function provider_testSplitStoragePath() {
111 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
112 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
113 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
114 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
115 array( 'mwstore://backend//container/path', array( null, null, null ) ),
116 array( 'mwstore://backend//container//path', array( null, null, null ) ),
117 array( 'mwstore://', array( null, null, null ) ),
118 array( 'mwstore://backend', array( null, null, null ) ),
119 array( 'mwstore:///', array( null, null, null ) ),
120 array( 'mwstore:/', array( null, null, null ) ),
121 array( 'mwstore:', array( null, null, null ) )
126 * @dataProvider provider_normalizeStoragePath
128 public function testNormalizeStoragePath( $path, $res ) {
129 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
130 "FileBackend::normalizeStoragePath on path '$path'" );
133 function provider_normalizeStoragePath() {
135 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
136 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
137 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
138 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
139 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
140 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
141 array( 'mwstore://', null ),
142 array( 'mwstore://backend', null ),
143 array( 'mwstore://backend//container/path', null ),
144 array( 'mwstore://backend//container//path', null ),
145 array( 'mwstore:///', null ),
146 array( 'mwstore:/', null ),
147 array( 'mwstore:', null ), )
152 * @dataProvider provider_testParentStoragePath
154 public function testParentStoragePath( $path, $res ) {
155 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
156 "FileBackend::parentStoragePath on path '$path'" );
159 function provider_testParentStoragePath() {
161 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
162 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
163 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
164 array( 'mwstore://backend/container', null ),
165 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
166 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
167 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
168 array( 'mwstore://backend/container/', null ),
173 * @dataProvider provider_testExtensionFromPath
175 public function testExtensionFromPath( $path, $res ) {
176 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
177 "FileBackend::extensionFromPath on path '$path'" );
180 function provider_testExtensionFromPath() {
182 array( 'mwstore://backend/container/path.txt', 'txt' ),
183 array( 'mwstore://backend/container/path.svg.png', 'png' ),
184 array( 'mwstore://backend/container/path', '' ),
185 array( 'mwstore://backend/container/path.', '' ),
190 * @dataProvider provider_testStore
192 public function testStore( $op ) {
193 $this->filesToPrune
[] = $op['src'];
195 $this->backend
= $this->singleBackend
;
196 $this->tearDownFiles();
197 $this->doTestStore( $op );
198 $this->tearDownFiles();
200 $this->backend
= $this->multiBackend
;
201 $this->tearDownFiles();
202 $this->doTestStore( $op );
203 $this->filesToPrune
[] = $op['src']; # avoid file leaking
204 $this->tearDownFiles();
207 function doTestStore( $op ) {
208 $backendName = $this->backendClass();
210 $source = $op['src'];
212 $this->prepare( array( 'dir' => dirname( $dest ) ) );
214 file_put_contents( $source, "Unit test file" );
216 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
217 $this->backend
->store( $op );
220 $status = $this->backend
->doOperation( $op );
222 $this->assertEquals( array(), $status->errors
,
223 "Store from $source to $dest succeeded without warnings ($backendName)." );
224 $this->assertEquals( array(), $status->errors
,
225 "Store from $source to $dest succeeded ($backendName)." );
226 $this->assertEquals( array( 0 => true ), $status->success
,
227 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
228 $this->assertEquals( true, file_exists( $source ),
229 "Source file $source still exists ($backendName)." );
230 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
231 "Destination file $dest exists ($backendName)." );
233 $this->assertEquals( filesize( $source ),
234 $this->backend
->getFileSize( array( 'src' => $dest ) ),
235 "Destination file $dest has correct size ($backendName)." );
237 $props1 = FSFile
::getPropsFromPath( $source );
238 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
239 $this->assertEquals( $props1, $props2,
240 "Source and destination have the same props ($backendName)." );
243 public function provider_testStore() {
246 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
247 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
248 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
256 $op2['overwrite'] = true;
264 $op2['overwriteSame'] = true;
275 * @dataProvider provider_testCopy
277 public function testCopy( $op ) {
278 $this->backend
= $this->singleBackend
;
279 $this->tearDownFiles();
280 $this->doTestCopy( $op );
281 $this->tearDownFiles();
283 $this->backend
= $this->multiBackend
;
284 $this->tearDownFiles();
285 $this->doTestCopy( $op );
286 $this->tearDownFiles();
289 function doTestCopy( $op ) {
290 $backendName = $this->backendClass();
292 $source = $op['src'];
294 $this->prepare( array( 'dir' => dirname( $source ) ) );
295 $this->prepare( array( 'dir' => dirname( $dest ) ) );
297 $status = $this->backend
->doOperation(
298 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
299 $this->assertEquals( array(), $status->errors
,
300 "Creation of file at $source succeeded ($backendName)." );
302 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
303 $this->backend
->copy( $op );
306 $status = $this->backend
->doOperation( $op );
308 $this->assertEquals( array(), $status->errors
,
309 "Copy from $source to $dest succeeded without warnings ($backendName)." );
310 $this->assertEquals( true, $status->isOK(),
311 "Copy from $source to $dest succeeded ($backendName)." );
312 $this->assertEquals( array( 0 => true ), $status->success
,
313 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
314 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
315 "Source file $source still exists ($backendName)." );
316 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
317 "Destination file $dest exists after copy ($backendName)." );
320 $this->backend
->getFileSize( array( 'src' => $source ) ),
321 $this->backend
->getFileSize( array( 'src' => $dest ) ),
322 "Destination file $dest has correct size ($backendName)." );
324 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
325 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
326 $this->assertEquals( $props1, $props2,
327 "Source and destination have the same props ($backendName)." );
330 public function provider_testCopy() {
333 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
334 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
336 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
344 $op2['overwrite'] = true;
352 $op2['overwriteSame'] = true;
363 * @dataProvider provider_testMove
365 public function testMove( $op ) {
366 $this->backend
= $this->singleBackend
;
367 $this->tearDownFiles();
368 $this->doTestMove( $op );
369 $this->tearDownFiles();
371 $this->backend
= $this->multiBackend
;
372 $this->tearDownFiles();
373 $this->doTestMove( $op );
374 $this->tearDownFiles();
377 private function doTestMove( $op ) {
378 $backendName = $this->backendClass();
380 $source = $op['src'];
382 $this->prepare( array( 'dir' => dirname( $source ) ) );
383 $this->prepare( array( 'dir' => dirname( $dest ) ) );
385 $status = $this->backend
->doOperation(
386 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
387 $this->assertEquals( array(), $status->errors
,
388 "Creation of file at $source succeeded ($backendName)." );
390 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
391 $this->backend
->copy( $op );
394 $status = $this->backend
->doOperation( $op );
395 $this->assertEquals( array(), $status->errors
,
396 "Move from $source to $dest succeeded without warnings ($backendName)." );
397 $this->assertEquals( true, $status->isOK(),
398 "Move from $source to $dest succeeded ($backendName)." );
399 $this->assertEquals( array( 0 => true ), $status->success
,
400 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
401 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
402 "Source file $source does not still exists ($backendName)." );
403 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
404 "Destination file $dest exists after move ($backendName)." );
406 $this->assertNotEquals(
407 $this->backend
->getFileSize( array( 'src' => $source ) ),
408 $this->backend
->getFileSize( array( 'src' => $dest ) ),
409 "Destination file $dest has correct size ($backendName)." );
411 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
412 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
413 $this->assertEquals( false, $props1['fileExists'],
414 "Source file does not exist accourding to props ($backendName)." );
415 $this->assertEquals( true, $props2['fileExists'],
416 "Destination file exists accourding to props ($backendName)." );
419 public function provider_testMove() {
422 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
423 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
425 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
433 $op2['overwrite'] = true;
441 $op2['overwriteSame'] = true;
452 * @dataProvider provider_testDelete
454 public function testDelete( $op, $withSource, $okStatus ) {
455 $this->backend
= $this->singleBackend
;
456 $this->tearDownFiles();
457 $this->doTestDelete( $op, $withSource, $okStatus );
458 $this->tearDownFiles();
460 $this->backend
= $this->multiBackend
;
461 $this->tearDownFiles();
462 $this->doTestDelete( $op, $withSource, $okStatus );
463 $this->tearDownFiles();
466 private function doTestDelete( $op, $withSource, $okStatus ) {
467 $backendName = $this->backendClass();
469 $source = $op['src'];
470 $this->prepare( array( 'dir' => dirname( $source ) ) );
473 $status = $this->backend
->doOperation(
474 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
475 $this->assertEquals( array(), $status->errors
,
476 "Creation of file at $source succeeded ($backendName)." );
479 $status = $this->backend
->doOperation( $op );
481 $this->assertEquals( array(), $status->errors
,
482 "Deletion of file at $source succeeded without warnings ($backendName)." );
483 $this->assertEquals( true, $status->isOK(),
484 "Deletion of file at $source succeeded ($backendName)." );
485 $this->assertEquals( array( 0 => true ), $status->success
,
486 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
488 $this->assertEquals( false, $status->isOK(),
489 "Deletion of file at $source failed ($backendName)." );
492 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
493 "Source file $source does not exist after move ($backendName)." );
496 $this->backend
->getFileSize( array( 'src' => $source ) ),
497 "Source file $source has correct size (false) ($backendName)." );
499 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
500 $this->assertFalse( $props1['fileExists'],
501 "Source file $source does not exist according to props ($backendName)." );
504 public function provider_testDelete() {
507 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
509 $op = array( 'op' => 'delete', 'src' => $source );
518 false, // without source
522 $op['ignoreMissingSource'] = true;
525 false, // without source
533 * @dataProvider provider_testCreate
535 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
536 $this->backend
= $this->singleBackend
;
537 $this->tearDownFiles();
538 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
539 $this->tearDownFiles();
541 $this->backend
= $this->multiBackend
;
542 $this->tearDownFiles();
543 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
544 $this->tearDownFiles();
547 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
548 $backendName = $this->backendClass();
551 $this->prepare( array( 'dir' => dirname( $dest ) ) );
553 $oldText = 'blah...blah...waahwaah';
554 if ( $alreadyExists ) {
555 $status = $this->backend
->doOperation(
556 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
557 $this->assertEquals( array(), $status->errors
,
558 "Creation of file at $dest succeeded ($backendName)." );
561 $status = $this->backend
->doOperation( $op );
563 $this->assertEquals( array(), $status->errors
,
564 "Creation of file at $dest succeeded without warnings ($backendName)." );
565 $this->assertEquals( true, $status->isOK(),
566 "Creation of file at $dest succeeded ($backendName)." );
567 $this->assertEquals( array( 0 => true ), $status->success
,
568 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
570 $this->assertEquals( false, $status->isOK(),
571 "Creation of file at $dest failed ($backendName)." );
574 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
575 "Destination file $dest exists after creation ($backendName)." );
577 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
578 $this->assertEquals( true, $props1['fileExists'],
579 "Destination file $dest exists according to props ($backendName)." );
580 if ( $okStatus ) { // file content is what we saved
581 $this->assertEquals( $newSize, $props1['size'],
582 "Destination file $dest has expected size according to props ($backendName)." );
583 $this->assertEquals( $newSize,
584 $this->backend
->getFileSize( array( 'src' => $dest ) ),
585 "Destination file $dest has correct size ($backendName)." );
586 } else { // file content is some other previous text
587 $this->assertEquals( strlen( $oldText ), $props1['size'],
588 "Destination file $dest has original size according to props ($backendName)." );
589 $this->assertEquals( strlen( $oldText ),
590 $this->backend
->getFileSize( array( 'src' => $dest ) ),
591 "Destination file $dest has original size according to props ($backendName)." );
596 * @dataProvider provider_testCreate
598 public function provider_testCreate() {
601 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
603 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
606 false, // no dest already exists
608 strlen( $op['content'] )
612 $op2['content'] = "\n";
615 false, // no dest already exists
617 strlen( $op2['content'] )
621 $op2['content'] = "fsf\n waf 3kt";
624 true, // dest already exists
626 strlen( $op2['content'] )
630 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
631 $op2['overwrite'] = true;
634 true, // dest already exists
636 strlen( $op2['content'] )
640 $op2['content'] = "39qjmg3-qg";
641 $op2['overwriteSame'] = true;
644 true, // dest already exists
646 strlen( $op2['content'] )
653 * @dataProvider provider_testConcatenate
655 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
656 $this->filesToPrune
[] = $op['dst'];
658 $this->backend
= $this->singleBackend
;
659 $this->tearDownFiles();
660 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
661 $this->tearDownFiles();
663 $this->backend
= $this->multiBackend
;
664 $this->tearDownFiles();
665 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
666 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
667 $this->tearDownFiles();
670 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
671 $backendName = $this->backendClass();
676 foreach ( $srcs as $i => $source ) {
677 $this->prepare( array( 'dir' => dirname( $source ) ) );
679 'op' => 'create', // operation
680 'dst' => $source, // source
681 'content' => $srcsContent[$i]
683 $expContent .= $srcsContent[$i];
685 $status = $this->backend
->doOperations( $ops );
687 $this->assertEquals( array(), $status->errors
,
688 "Creation of source files succeeded ($backendName)." );
690 $dest = $params['dst'];
691 if ( $alreadyExists ) {
692 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
693 $this->assertEquals( true, $ok,
694 "Creation of file at $dest succeeded ($backendName)." );
696 $ok = file_put_contents( $dest, '' ) !== false;
697 $this->assertEquals( true, $ok,
698 "Creation of 0-byte file at $dest succeeded ($backendName)." );
701 // Combine the files into one
702 $status = $this->backend
->concatenate( $params );
704 $this->assertEquals( array(), $status->errors
,
705 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
706 $this->assertEquals( true, $status->isOK(),
707 "Creation of concat file at $dest succeeded ($backendName)." );
709 $this->assertEquals( false, $status->isOK(),
710 "Creation of concat file at $dest failed ($backendName)." );
714 $this->assertEquals( true, is_file( $dest ),
715 "Dest concat file $dest exists after creation ($backendName)." );
717 $this->assertEquals( true, is_file( $dest ),
718 "Dest concat file $dest exists after failed creation ($backendName)." );
721 $contents = file_get_contents( $dest );
722 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
725 $this->assertEquals( $expContent, $contents,
726 "Concat file at $dest has correct contents ($backendName)." );
728 $this->assertNotEquals( $expContent, $contents,
729 "Concat file at $dest has correct contents ($backendName)." );
733 function provider_testConcatenate() {
736 $rand = mt_rand( 0, 2000000000 ) . time();
737 $dest = wfTempDir() . "/randomfile!$rand.txt";
739 $this->baseStorePath() . '/unittest-cont1/file1.txt',
740 $this->baseStorePath() . '/unittest-cont1/file2.txt',
741 $this->baseStorePath() . '/unittest-cont1/file3.txt',
742 $this->baseStorePath() . '/unittest-cont1/file4.txt',
743 $this->baseStorePath() . '/unittest-cont1/file5.txt',
744 $this->baseStorePath() . '/unittest-cont1/file6.txt',
745 $this->baseStorePath() . '/unittest-cont1/file7.txt',
746 $this->baseStorePath() . '/unittest-cont1/file8.txt',
747 $this->baseStorePath() . '/unittest-cont1/file9.txt',
748 $this->baseStorePath() . '/unittest-cont1/file10.txt'
762 $params = array( 'srcs' => $srcs, 'dst' => $dest );
765 $params, // operation
767 $content, // content for each source
768 false, // no dest already exists
773 $params, // operation
775 $content, // content for each source
776 true, // dest already exists
784 * @dataProvider provider_testGetFileStat
786 public function testGetFileStat( $path, $content, $alreadyExists ) {
787 $this->backend
= $this->singleBackend
;
788 $this->tearDownFiles();
789 $this->doTestGetFileStat( $path, $content, $alreadyExists );
790 $this->tearDownFiles();
792 $this->backend
= $this->multiBackend
;
793 $this->tearDownFiles();
794 $this->doTestGetFileStat( $path, $content, $alreadyExists );
795 $this->tearDownFiles();
798 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
799 $backendName = $this->backendClass();
801 if ( $alreadyExists ) {
802 $this->prepare( array( 'dir' => dirname( $path ) ) );
803 $status = $this->backend
->create( array( 'dst' => $path, 'content' => $content ) );
804 $this->assertEquals( array(), $status->errors
,
805 "Creation of file at $path succeeded ($backendName)." );
807 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
808 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
809 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
811 $this->assertEquals( strlen( $content ), $size,
812 "Correct file size of '$path'" );
813 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
814 "Correct file timestamp of '$path'" );
816 $size = $stat['size'];
817 $time = $stat['mtime'];
818 $this->assertEquals( strlen( $content ), $size,
819 "Correct file size of '$path'" );
820 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
821 "Correct file timestamp of '$path'" );
823 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
824 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
825 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
827 $this->assertFalse( $size, "Correct file size of '$path'" );
828 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
829 $this->assertFalse( $stat, "Correct file stat of '$path'" );
833 function provider_testGetFileStat() {
836 $base = $this->baseStorePath();
837 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
838 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
839 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
845 * @dataProvider provider_testGetFileContents
847 public function testGetFileContents( $source, $content ) {
848 $this->backend
= $this->singleBackend
;
849 $this->tearDownFiles();
850 $this->doTestGetFileContents( $source, $content );
851 $this->tearDownFiles();
853 $this->backend
= $this->multiBackend
;
854 $this->tearDownFiles();
855 $this->doTestGetFileContents( $source, $content );
856 $this->tearDownFiles();
859 public function doTestGetFileContents( $source, $content ) {
860 $backendName = $this->backendClass();
862 $this->prepare( array( 'dir' => dirname( $source ) ) );
864 $status = $this->backend
->doOperation(
865 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
866 $this->assertEquals( array(), $status->errors
,
867 "Creation of file at $source succeeded ($backendName)." );
868 $this->assertEquals( true, $status->isOK(),
869 "Creation of file at $source succeeded with OK status ($backendName)." );
871 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
872 $this->assertNotEquals( false, $newContents,
873 "Read of file at $source succeeded ($backendName)." );
875 $this->assertEquals( $content, $newContents,
876 "Contents read match data at $source ($backendName)." );
879 function provider_testGetFileContents() {
882 $base = $this->baseStorePath();
883 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
884 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
890 * @dataProvider provider_testGetLocalCopy
892 public function testGetLocalCopy( $source, $content ) {
893 $this->backend
= $this->singleBackend
;
894 $this->tearDownFiles();
895 $this->doTestGetLocalCopy( $source, $content );
896 $this->tearDownFiles();
898 $this->backend
= $this->multiBackend
;
899 $this->tearDownFiles();
900 $this->doTestGetLocalCopy( $source, $content );
901 $this->tearDownFiles();
904 public function doTestGetLocalCopy( $source, $content ) {
905 $backendName = $this->backendClass();
907 $this->prepare( array( 'dir' => dirname( $source ) ) );
909 $status = $this->backend
->doOperation(
910 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
911 $this->assertEquals( array(), $status->errors
,
912 "Creation of file at $source succeeded ($backendName)." );
914 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
915 $this->assertNotNull( $tmpFile,
916 "Creation of local copy of $source succeeded ($backendName)." );
918 $contents = file_get_contents( $tmpFile->getPath() );
919 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
922 function provider_testGetLocalCopy() {
925 $base = $this->baseStorePath();
926 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
927 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
933 * @dataProvider provider_testGetLocalReference
935 public function testGetLocalReference( $source, $content ) {
936 $this->backend
= $this->singleBackend
;
937 $this->tearDownFiles();
938 $this->doTestGetLocalReference( $source, $content );
939 $this->tearDownFiles();
941 $this->backend
= $this->multiBackend
;
942 $this->tearDownFiles();
943 $this->doTestGetLocalReference( $source, $content );
944 $this->tearDownFiles();
947 private function doTestGetLocalReference( $source, $content ) {
948 $backendName = $this->backendClass();
950 $this->prepare( array( 'dir' => dirname( $source ) ) );
952 $status = $this->backend
->doOperation(
953 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
954 $this->assertEquals( array(), $status->errors
,
955 "Creation of file at $source succeeded ($backendName)." );
957 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
958 $this->assertNotNull( $tmpFile,
959 "Creation of local copy of $source succeeded ($backendName)." );
961 $contents = file_get_contents( $tmpFile->getPath() );
962 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
965 function provider_testGetLocalReference() {
968 $base = $this->baseStorePath();
969 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
970 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
976 * @dataProvider provider_testPrepareAndClean
978 public function testPrepareAndClean( $path, $isOK ) {
979 $this->backend
= $this->singleBackend
;
980 $this->doTestPrepareAndClean( $path, $isOK );
981 $this->tearDownFiles();
983 $this->backend
= $this->multiBackend
;
984 $this->doTestPrepareAndClean( $path, $isOK );
985 $this->tearDownFiles();
988 function provider_testPrepareAndClean() {
989 $base = $this->baseStorePath();
991 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
992 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
993 # Specific to FS backend with no basePath field set
994 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
998 function doTestPrepareAndClean( $path, $isOK ) {
999 $backendName = $this->backendClass();
1001 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1003 $this->assertEquals( array(), $status->errors
,
1004 "Preparing dir $path succeeded without warnings ($backendName)." );
1005 $this->assertEquals( true, $status->isOK(),
1006 "Preparing dir $path succeeded ($backendName)." );
1008 $this->assertEquals( false, $status->isOK(),
1009 "Preparing dir $path failed ($backendName)." );
1012 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1014 $this->assertEquals( array(), $status->errors
,
1015 "Cleaning dir $path succeeded without warnings ($backendName)." );
1016 $this->assertEquals( true, $status->isOK(),
1017 "Cleaning dir $path succeeded ($backendName)." );
1019 $this->assertEquals( false, $status->isOK(),
1020 "Cleaning dir $path failed ($backendName)." );
1024 // @TODO: testSecure
1026 public function testDoOperations() {
1027 $this->backend
= $this->singleBackend
;
1028 $this->tearDownFiles();
1029 $this->doTestDoOperations();
1030 $this->tearDownFiles();
1032 $this->backend
= $this->multiBackend
;
1033 $this->tearDownFiles();
1034 $this->doTestDoOperations();
1035 $this->tearDownFiles();
1037 // @TODO: test some cases where the ops should fail
1040 function doTestDoOperations() {
1041 $base = $this->baseStorePath();
1043 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1044 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1045 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1046 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1047 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1048 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1049 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1051 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1052 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1053 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1054 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1055 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1056 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1058 $status = $this->backend
->doOperations( array(
1059 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1060 // Now: A:<A>, B:<B>, C:<A>, D:<D> (file:<orginal contents>)
1061 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1062 // Now: A:<A>, B:<B>, C:<A>, D:<D>
1063 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1064 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1065 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1066 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1067 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1068 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1069 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1070 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1071 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1072 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1073 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1074 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1075 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1077 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1079 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1081 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1083 array( 'op' => 'null' ),
1087 $this->assertEquals( array(), $status->errors
, "Operation batch succeeded" );
1088 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1089 $this->assertEquals( 13, count( $status->success
),
1090 "Operation batch has correct success array" );
1092 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1093 "File does not exist at $fileA" );
1094 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1095 "File does not exist at $fileB" );
1096 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1097 "File does not exist at $fileD" );
1099 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1100 "File exists at $fileC" );
1101 $this->assertEquals( $fileBContents,
1102 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1103 "Correct file contents of $fileC" );
1104 $this->assertEquals( strlen( $fileBContents ),
1105 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1106 "Correct file size of $fileC" );
1107 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1108 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1109 "Correct file SHA-1 of $fileC" );
1112 public function testGetFileList() {
1113 $this->backend
= $this->singleBackend
;
1114 $this->tearDownFiles();
1115 $this->doTestGetFileList();
1116 $this->tearDownFiles();
1118 $this->backend
= $this->multiBackend
;
1119 $this->tearDownFiles();
1120 $this->doTestGetFileList();
1121 $this->tearDownFiles();
1124 private function doTestGetFileList() {
1125 $backendName = $this->backendClass();
1127 $base = $this->baseStorePath();
1129 "$base/unittest-cont1/test1.txt",
1130 "$base/unittest-cont1/test2.txt",
1131 "$base/unittest-cont1/test3.txt",
1132 "$base/unittest-cont1/subdir1/test1.txt",
1133 "$base/unittest-cont1/subdir1/test2.txt",
1134 "$base/unittest-cont1/subdir2/test3.txt",
1135 "$base/unittest-cont1/subdir2/test4.txt",
1136 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1137 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1138 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1139 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1140 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1141 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1142 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1147 foreach ( $files as $file ) {
1148 $this->prepare( array( 'dir' => dirname( $file ) ) );
1149 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1151 $status = $this->backend
->doOperations( $ops );
1152 $this->assertEquals( array(), $status->errors
,
1153 "Creation of files succeeded ($backendName)." );
1154 $this->assertEquals( true, $status->isOK(),
1155 "Creation of files succeeded with OK status ($backendName)." );
1162 "subdir1/test1.txt",
1163 "subdir1/test2.txt",
1164 "subdir2/test3.txt",
1165 "subdir2/test4.txt",
1166 "subdir2/subdir/test1.txt",
1167 "subdir2/subdir/test2.txt",
1168 "subdir2/subdir/test3.txt",
1169 "subdir2/subdir/test4.txt",
1170 "subdir2/subdir/test5.txt",
1171 "subdir2/subdir/sub/test0.txt",
1172 "subdir2/subdir/sub/120-px-file.txt",
1176 // Actual listing (no trailing slash)
1178 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1179 foreach ( $iter as $file ) {
1184 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1186 // Actual listing (with trailing slash)
1188 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1189 foreach ( $iter as $file ) {
1194 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1204 "sub/120-px-file.txt",
1208 // Actual listing (no trailing slash)
1210 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1211 foreach ( $iter as $file ) {
1216 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1218 // Actual listing (with trailing slash)
1220 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1221 foreach ( $iter as $file ) {
1226 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1228 // Actual listing (using iterator second time)
1230 foreach ( $iter as $file ) {
1235 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1237 foreach ( $files as $file ) { // clean up
1238 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1241 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1242 foreach ( $iter as $iter ) {} // no errors
1245 // test helper wrapper for backend prepare() function
1246 private function prepare( array $params ) {
1247 $this->dirsToPrune
[] = $params['dir'];
1248 return $this->backend
->prepare( $params );
1251 function tearDownFiles() {
1252 foreach ( $this->filesToPrune
as $file ) {
1255 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1256 foreach ( $containers as $container ) {
1257 $this->deleteFiles( $container );
1259 foreach ( $this->dirsToPrune
as $dir ) {
1260 $this->recursiveClean( $dir );
1262 $this->filesToPrune
= $this->dirsToPrune
= array();
1265 private function deleteFiles( $container ) {
1266 $base = $this->baseStorePath();
1267 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1269 foreach ( $iter as $file ) {
1270 $this->backend
->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
1275 private function recursiveClean( $dir ) {
1277 if ( !$this->backend
->clean( array( 'dir' => $dir ) )->isOK() ) {
1280 } while ( $dir = FileBackend
::parentStoragePath( $dir ) );
1283 function tearDown() {