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 ) {
29 $useConfig['name'] = 'localtesting'; // swap name
30 $class = $useConfig['class'];
31 self
::$backendToUse = new $class( $useConfig );
32 $this->singleBackend
= self
::$backendToUse;
35 $this->singleBackend
= new FSFileBackend( array(
36 'name' => 'localtesting',
37 'lockManager' => 'fsLockManager',
38 'containerPaths' => array(
39 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
40 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
43 $this->multiBackend
= new FileBackendMultiWrite( array(
44 'name' => 'localtesting',
45 'lockManager' => 'fsLockManager',
48 'name' => 'localmutlitesting1',
49 'class' => 'FSFileBackend',
50 'lockManager' => 'nullLockManager',
51 'containerPaths' => array(
52 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
53 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
54 'isMultiMaster' => false
57 'name' => 'localmutlitesting2',
58 'class' => 'FSFileBackend',
59 'lockManager' => 'nullLockManager',
60 'containerPaths' => array(
61 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
62 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
63 'isMultiMaster' => true
67 $this->filesToPrune
= array();
70 private function baseStorePath() {
71 return 'mwstore://localtesting';
74 private function backendClass() {
75 return get_class( $this->backend
);
79 * @dataProvider provider_testIsStoragePath
81 public function testIsStoragePath( $path, $isStorePath ) {
82 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
83 "FileBackend::isStoragePath on path '$path'" );
86 function provider_testIsStoragePath() {
88 array( 'mwstore://', true ),
89 array( 'mwstore://backend', true ),
90 array( 'mwstore://backend/container', true ),
91 array( 'mwstore://backend/container/', true ),
92 array( 'mwstore://backend/container/path', true ),
93 array( 'mwstore://backend//container/', true ),
94 array( 'mwstore://backend//container//', true ),
95 array( 'mwstore://backend//container//path', true ),
96 array( 'mwstore:///', true ),
97 array( 'mwstore:/', false ),
98 array( 'mwstore:', false ),
103 * @dataProvider provider_testSplitStoragePath
105 public function testSplitStoragePath( $path, $res ) {
106 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
107 "FileBackend::splitStoragePath on path '$path'" );
110 function provider_testSplitStoragePath() {
112 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
113 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
114 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
115 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
116 array( 'mwstore://backend//container/path', array( null, null, null ) ),
117 array( 'mwstore://backend//container//path', array( null, null, null ) ),
118 array( 'mwstore://', array( null, null, null ) ),
119 array( 'mwstore://backend', array( null, null, null ) ),
120 array( 'mwstore:///', array( null, null, null ) ),
121 array( 'mwstore:/', array( null, null, null ) ),
122 array( 'mwstore:', array( null, null, null ) )
127 * @dataProvider provider_normalizeStoragePath
129 public function testNormalizeStoragePath( $path, $res ) {
130 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
131 "FileBackend::normalizeStoragePath on path '$path'" );
134 function provider_normalizeStoragePath() {
136 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
137 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
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', 'mwstore://backend/container/path' ),
141 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
142 array( 'mwstore://', null ),
143 array( 'mwstore://backend', null ),
144 array( 'mwstore://backend//container/path', null ),
145 array( 'mwstore://backend//container//path', null ),
146 array( 'mwstore:///', null ),
147 array( 'mwstore:/', null ),
148 array( 'mwstore:', null ), )
153 * @dataProvider provider_testParentStoragePath
155 public function testParentStoragePath( $path, $res ) {
156 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
157 "FileBackend::parentStoragePath on path '$path'" );
160 function provider_testParentStoragePath() {
162 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
163 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
164 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
165 array( 'mwstore://backend/container', null ),
166 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
167 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
168 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
169 array( 'mwstore://backend/container/', null ),
174 * @dataProvider provider_testExtensionFromPath
176 public function testExtensionFromPath( $path, $res ) {
177 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
178 "FileBackend::extensionFromPath on path '$path'" );
181 function provider_testExtensionFromPath() {
183 array( 'mwstore://backend/container/path.txt', 'txt' ),
184 array( 'mwstore://backend/container/path.svg.png', 'png' ),
185 array( 'mwstore://backend/container/path', '' ),
186 array( 'mwstore://backend/container/path.', '' ),
191 * @dataProvider provider_testStore
193 public function testStore( $op ) {
194 $this->filesToPrune
[] = $op['src'];
196 $this->backend
= $this->singleBackend
;
197 $this->tearDownFiles();
198 $this->doTestStore( $op );
199 $this->tearDownFiles();
201 $this->backend
= $this->multiBackend
;
202 $this->tearDownFiles();
203 $this->doTestStore( $op );
204 $this->filesToPrune
[] = $op['src']; # avoid file leaking
205 $this->tearDownFiles();
208 function doTestStore( $op ) {
209 $backendName = $this->backendClass();
211 $source = $op['src'];
213 $this->prepare( array( 'dir' => dirname( $dest ) ) );
215 file_put_contents( $source, "Unit test file" );
217 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
218 $this->backend
->store( $op );
221 $status = $this->backend
->doOperation( $op );
223 $this->assertEquals( array(), $status->errors
,
224 "Store from $source to $dest succeeded without warnings ($backendName)." );
225 $this->assertEquals( array(), $status->errors
,
226 "Store from $source to $dest succeeded ($backendName)." );
227 $this->assertEquals( array( 0 => true ), $status->success
,
228 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
229 $this->assertEquals( true, file_exists( $source ),
230 "Source file $source still exists ($backendName)." );
231 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
232 "Destination file $dest exists ($backendName)." );
234 $this->assertEquals( filesize( $source ),
235 $this->backend
->getFileSize( array( 'src' => $dest ) ),
236 "Destination file $dest has correct size ($backendName)." );
238 $props1 = FSFile
::getPropsFromPath( $source );
239 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
240 $this->assertEquals( $props1, $props2,
241 "Source and destination have the same props ($backendName)." );
244 public function provider_testStore() {
247 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
248 $toPath = $this->baseStorePath() . '/unittest-cont1/fun/obj1.txt';
249 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
257 $op2['overwrite'] = true;
265 $op2['overwriteSame'] = true;
276 * @dataProvider provider_testCopy
278 public function testCopy( $op ) {
279 $this->backend
= $this->singleBackend
;
280 $this->tearDownFiles();
281 $this->doTestCopy( $op );
282 $this->tearDownFiles();
284 $this->backend
= $this->multiBackend
;
285 $this->tearDownFiles();
286 $this->doTestCopy( $op );
287 $this->tearDownFiles();
290 function doTestCopy( $op ) {
291 $backendName = $this->backendClass();
293 $source = $op['src'];
295 $this->prepare( array( 'dir' => dirname( $source ) ) );
296 $this->prepare( array( 'dir' => dirname( $dest ) ) );
298 $status = $this->backend
->doOperation(
299 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
300 $this->assertEquals( array(), $status->errors
,
301 "Creation of file at $source succeeded ($backendName)." );
303 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
304 $this->backend
->copy( $op );
307 $status = $this->backend
->doOperation( $op );
309 $this->assertEquals( array(), $status->errors
,
310 "Copy from $source to $dest succeeded without warnings ($backendName)." );
311 $this->assertEquals( true, $status->isOK(),
312 "Copy from $source to $dest succeeded ($backendName)." );
313 $this->assertEquals( array( 0 => true ), $status->success
,
314 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
315 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
316 "Source file $source still exists ($backendName)." );
317 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
318 "Destination file $dest exists after copy ($backendName)." );
321 $this->backend
->getFileSize( array( 'src' => $source ) ),
322 $this->backend
->getFileSize( array( 'src' => $dest ) ),
323 "Destination file $dest has correct size ($backendName)." );
325 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
326 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
327 $this->assertEquals( $props1, $props2,
328 "Source and destination have the same props ($backendName)." );
331 public function provider_testCopy() {
334 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
335 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
337 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
345 $op2['overwrite'] = true;
353 $op2['overwriteSame'] = true;
364 * @dataProvider provider_testMove
366 public function testMove( $op ) {
367 $this->backend
= $this->singleBackend
;
368 $this->tearDownFiles();
369 $this->doTestMove( $op );
370 $this->tearDownFiles();
372 $this->backend
= $this->multiBackend
;
373 $this->tearDownFiles();
374 $this->doTestMove( $op );
375 $this->tearDownFiles();
378 private function doTestMove( $op ) {
379 $backendName = $this->backendClass();
381 $source = $op['src'];
383 $this->prepare( array( 'dir' => dirname( $source ) ) );
384 $this->prepare( array( 'dir' => dirname( $dest ) ) );
386 $status = $this->backend
->doOperation(
387 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
388 $this->assertEquals( array(), $status->errors
,
389 "Creation of file at $source succeeded ($backendName)." );
391 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
392 $this->backend
->copy( $op );
395 $status = $this->backend
->doOperation( $op );
396 $this->assertEquals( array(), $status->errors
,
397 "Move from $source to $dest succeeded without warnings ($backendName)." );
398 $this->assertEquals( true, $status->isOK(),
399 "Move from $source to $dest succeeded ($backendName)." );
400 $this->assertEquals( array( 0 => true ), $status->success
,
401 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
402 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
403 "Source file $source does not still exists ($backendName)." );
404 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
405 "Destination file $dest exists after move ($backendName)." );
407 $this->assertNotEquals(
408 $this->backend
->getFileSize( array( 'src' => $source ) ),
409 $this->backend
->getFileSize( array( 'src' => $dest ) ),
410 "Destination file $dest has correct size ($backendName)." );
412 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
413 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
414 $this->assertEquals( false, $props1['fileExists'],
415 "Source file does not exist accourding to props ($backendName)." );
416 $this->assertEquals( true, $props2['fileExists'],
417 "Destination file exists accourding to props ($backendName)." );
420 public function provider_testMove() {
423 $source = $this->baseStorePath() . '/unittest-cont1/file.txt';
424 $dest = $this->baseStorePath() . '/unittest-cont2/fileMoved.txt';
426 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
434 $op2['overwrite'] = true;
442 $op2['overwriteSame'] = true;
453 * @dataProvider provider_testDelete
455 public function testDelete( $op, $withSource, $okStatus ) {
456 $this->backend
= $this->singleBackend
;
457 $this->tearDownFiles();
458 $this->doTestDelete( $op, $withSource, $okStatus );
459 $this->tearDownFiles();
461 $this->backend
= $this->multiBackend
;
462 $this->tearDownFiles();
463 $this->doTestDelete( $op, $withSource, $okStatus );
464 $this->tearDownFiles();
467 private function doTestDelete( $op, $withSource, $okStatus ) {
468 $backendName = $this->backendClass();
470 $source = $op['src'];
471 $this->prepare( array( 'dir' => dirname( $source ) ) );
474 $status = $this->backend
->doOperation(
475 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
476 $this->assertEquals( array(), $status->errors
,
477 "Creation of file at $source succeeded ($backendName)." );
480 $status = $this->backend
->doOperation( $op );
482 $this->assertEquals( array(), $status->errors
,
483 "Deletion of file at $source succeeded without warnings ($backendName)." );
484 $this->assertEquals( true, $status->isOK(),
485 "Deletion of file at $source succeeded ($backendName)." );
486 $this->assertEquals( array( 0 => true ), $status->success
,
487 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
489 $this->assertEquals( false, $status->isOK(),
490 "Deletion of file at $source failed ($backendName)." );
493 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
494 "Source file $source does not exist after move ($backendName)." );
497 $this->backend
->getFileSize( array( 'src' => $source ) ),
498 "Source file $source has correct size (false) ($backendName)." );
500 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
501 $this->assertFalse( $props1['fileExists'],
502 "Source file $source does not exist according to props ($backendName)." );
505 public function provider_testDelete() {
508 $source = $this->baseStorePath() . '/unittest-cont1/myfacefile.txt';
510 $op = array( 'op' => 'delete', 'src' => $source );
519 false, // without source
523 $op['ignoreMissingSource'] = true;
526 false, // without source
534 * @dataProvider provider_testCreate
536 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
537 $this->backend
= $this->singleBackend
;
538 $this->tearDownFiles();
539 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
540 $this->tearDownFiles();
542 $this->backend
= $this->multiBackend
;
543 $this->tearDownFiles();
544 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
545 $this->tearDownFiles();
548 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
549 $backendName = $this->backendClass();
552 $this->prepare( array( 'dir' => dirname( $dest ) ) );
554 $oldText = 'blah...blah...waahwaah';
555 if ( $alreadyExists ) {
556 $status = $this->backend
->doOperation(
557 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
558 $this->assertEquals( array(), $status->errors
,
559 "Creation of file at $dest succeeded ($backendName)." );
562 $status = $this->backend
->doOperation( $op );
564 $this->assertEquals( array(), $status->errors
,
565 "Creation of file at $dest succeeded without warnings ($backendName)." );
566 $this->assertEquals( true, $status->isOK(),
567 "Creation of file at $dest succeeded ($backendName)." );
568 $this->assertEquals( array( 0 => true ), $status->success
,
569 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
571 $this->assertEquals( false, $status->isOK(),
572 "Creation of file at $dest failed ($backendName)." );
575 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
576 "Destination file $dest exists after creation ($backendName)." );
578 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
579 $this->assertEquals( true, $props1['fileExists'],
580 "Destination file $dest exists according to props ($backendName)." );
581 if ( $okStatus ) { // file content is what we saved
582 $this->assertEquals( $newSize, $props1['size'],
583 "Destination file $dest has expected size according to props ($backendName)." );
584 $this->assertEquals( $newSize,
585 $this->backend
->getFileSize( array( 'src' => $dest ) ),
586 "Destination file $dest has correct size ($backendName)." );
587 } else { // file content is some other previous text
588 $this->assertEquals( strlen( $oldText ), $props1['size'],
589 "Destination file $dest has original size according to props ($backendName)." );
590 $this->assertEquals( strlen( $oldText ),
591 $this->backend
->getFileSize( array( 'src' => $dest ) ),
592 "Destination file $dest has original size according to props ($backendName)." );
597 * @dataProvider provider_testCreate
599 public function provider_testCreate() {
602 $dest = $this->baseStorePath() . '/unittest-cont2/myspacefile.txt';
604 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
607 false, // no dest already exists
609 strlen( $op['content'] )
613 $op2['content'] = "\n";
616 false, // no dest already exists
618 strlen( $op2['content'] )
622 $op2['content'] = "fsf\n waf 3kt";
625 true, // dest already exists
627 strlen( $op2['content'] )
631 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
632 $op2['overwrite'] = true;
635 true, // dest already exists
637 strlen( $op2['content'] )
641 $op2['content'] = "39qjmg3-qg";
642 $op2['overwriteSame'] = true;
645 true, // dest already exists
647 strlen( $op2['content'] )
654 * @dataProvider provider_testConcatenate
656 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
657 $this->filesToPrune
[] = $op['dst'];
659 $this->backend
= $this->singleBackend
;
660 $this->tearDownFiles();
661 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
662 $this->tearDownFiles();
664 $this->backend
= $this->multiBackend
;
665 $this->tearDownFiles();
666 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
667 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
668 $this->tearDownFiles();
671 public function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
672 $backendName = $this->backendClass();
677 foreach ( $srcs as $i => $source ) {
678 $this->prepare( array( 'dir' => dirname( $source ) ) );
680 'op' => 'create', // operation
681 'dst' => $source, // source
682 'content' => $srcsContent[$i]
684 $expContent .= $srcsContent[$i];
686 $status = $this->backend
->doOperations( $ops );
688 $this->assertEquals( array(), $status->errors
,
689 "Creation of source files succeeded ($backendName)." );
691 $dest = $params['dst'];
692 if ( $alreadyExists ) {
693 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
694 $this->assertEquals( true, $ok,
695 "Creation of file at $dest succeeded ($backendName)." );
697 $ok = file_put_contents( $dest, '' ) !== false;
698 $this->assertEquals( true, $ok,
699 "Creation of 0-byte file at $dest succeeded ($backendName)." );
702 // Combine the files into one
703 $status = $this->backend
->concatenate( $params );
705 $this->assertEquals( array(), $status->errors
,
706 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
707 $this->assertEquals( true, $status->isOK(),
708 "Creation of concat file at $dest succeeded ($backendName)." );
710 $this->assertEquals( false, $status->isOK(),
711 "Creation of concat file at $dest failed ($backendName)." );
715 $this->assertEquals( true, is_file( $dest ),
716 "Dest concat file $dest exists after creation ($backendName)." );
718 $this->assertEquals( true, is_file( $dest ),
719 "Dest concat file $dest exists after failed creation ($backendName)." );
722 $contents = file_get_contents( $dest );
723 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
726 $this->assertEquals( $expContent, $contents,
727 "Concat file at $dest has correct contents ($backendName)." );
729 $this->assertNotEquals( $expContent, $contents,
730 "Concat file at $dest has correct contents ($backendName)." );
734 function provider_testConcatenate() {
737 $rand = mt_rand( 0, 2000000000 ) . time();
738 $dest = wfTempDir() . "/randomfile!$rand.txt";
740 $this->baseStorePath() . '/unittest-cont1/file1.txt',
741 $this->baseStorePath() . '/unittest-cont1/file2.txt',
742 $this->baseStorePath() . '/unittest-cont1/file3.txt',
743 $this->baseStorePath() . '/unittest-cont1/file4.txt',
744 $this->baseStorePath() . '/unittest-cont1/file5.txt',
745 $this->baseStorePath() . '/unittest-cont1/file6.txt',
746 $this->baseStorePath() . '/unittest-cont1/file7.txt',
747 $this->baseStorePath() . '/unittest-cont1/file8.txt',
748 $this->baseStorePath() . '/unittest-cont1/file9.txt',
749 $this->baseStorePath() . '/unittest-cont1/file10.txt'
763 $params = array( 'srcs' => $srcs, 'dst' => $dest );
766 $params, // operation
768 $content, // content for each source
769 false, // no dest already exists
774 $params, // operation
776 $content, // content for each source
777 true, // dest already exists
785 * @dataProvider provider_testGetFileStat
787 public function testGetFileStat( $path, $content, $alreadyExists ) {
788 $this->backend
= $this->singleBackend
;
789 $this->tearDownFiles();
790 $this->doTestGetFileStat( $path, $content, $alreadyExists );
791 $this->tearDownFiles();
793 $this->backend
= $this->multiBackend
;
794 $this->tearDownFiles();
795 $this->doTestGetFileStat( $path, $content, $alreadyExists );
796 $this->tearDownFiles();
799 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
800 $backendName = $this->backendClass();
802 if ( $alreadyExists ) {
803 $this->prepare( array( 'dir' => dirname( $path ) ) );
804 $status = $this->backend
->create( array( 'dst' => $path, 'content' => $content ) );
805 $this->assertEquals( array(), $status->errors
,
806 "Creation of file at $path succeeded ($backendName)." );
808 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
809 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
810 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
812 $this->assertEquals( strlen( $content ), $size,
813 "Correct file size of '$path'" );
814 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
815 "Correct file timestamp of '$path'" );
817 $size = $stat['size'];
818 $time = $stat['mtime'];
819 $this->assertEquals( strlen( $content ), $size,
820 "Correct file size of '$path'" );
821 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 5,
822 "Correct file timestamp of '$path'" );
824 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
825 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
826 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
828 $this->assertFalse( $size, "Correct file size of '$path'" );
829 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
830 $this->assertFalse( $stat, "Correct file stat of '$path'" );
834 function provider_testGetFileStat() {
837 $base = $this->baseStorePath();
838 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents", true );
839 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "", true );
840 $cases[] = array( "$base/unittest-cont1/b/some-diff_file.txt", null, false );
846 * @dataProvider provider_testGetFileContents
848 public function testGetFileContents( $source, $content ) {
849 $this->backend
= $this->singleBackend
;
850 $this->tearDownFiles();
851 $this->doTestGetFileContents( $source, $content );
852 $this->tearDownFiles();
854 $this->backend
= $this->multiBackend
;
855 $this->tearDownFiles();
856 $this->doTestGetFileContents( $source, $content );
857 $this->tearDownFiles();
860 public function doTestGetFileContents( $source, $content ) {
861 $backendName = $this->backendClass();
863 $this->prepare( array( 'dir' => dirname( $source ) ) );
865 $status = $this->backend
->doOperation(
866 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
867 $this->assertEquals( array(), $status->errors
,
868 "Creation of file at $source succeeded ($backendName)." );
869 $this->assertEquals( true, $status->isOK(),
870 "Creation of file at $source succeeded with OK status ($backendName)." );
872 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
873 $this->assertNotEquals( false, $newContents,
874 "Read of file at $source succeeded ($backendName)." );
876 $this->assertEquals( $content, $newContents,
877 "Contents read match data at $source ($backendName)." );
880 function provider_testGetFileContents() {
883 $base = $this->baseStorePath();
884 $cases[] = array( "$base/unittest-cont1/b/z/some_file.txt", "some file contents" );
885 $cases[] = array( "$base/unittest-cont1/b/some-other_file.txt", "more file contents" );
891 * @dataProvider provider_testGetLocalCopy
893 public function testGetLocalCopy( $source, $content ) {
894 $this->backend
= $this->singleBackend
;
895 $this->tearDownFiles();
896 $this->doTestGetLocalCopy( $source, $content );
897 $this->tearDownFiles();
899 $this->backend
= $this->multiBackend
;
900 $this->tearDownFiles();
901 $this->doTestGetLocalCopy( $source, $content );
902 $this->tearDownFiles();
905 public function doTestGetLocalCopy( $source, $content ) {
906 $backendName = $this->backendClass();
908 $this->prepare( array( 'dir' => dirname( $source ) ) );
910 $status = $this->backend
->doOperation(
911 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
912 $this->assertEquals( array(), $status->errors
,
913 "Creation of file at $source succeeded ($backendName)." );
915 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
916 $this->assertNotNull( $tmpFile,
917 "Creation of local copy of $source succeeded ($backendName)." );
919 $contents = file_get_contents( $tmpFile->getPath() );
920 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
923 function provider_testGetLocalCopy() {
926 $base = $this->baseStorePath();
927 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
928 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
934 * @dataProvider provider_testGetLocalReference
936 public function testGetLocalReference( $source, $content ) {
937 $this->backend
= $this->singleBackend
;
938 $this->tearDownFiles();
939 $this->doTestGetLocalReference( $source, $content );
940 $this->tearDownFiles();
942 $this->backend
= $this->multiBackend
;
943 $this->tearDownFiles();
944 $this->doTestGetLocalReference( $source, $content );
945 $this->tearDownFiles();
948 private function doTestGetLocalReference( $source, $content ) {
949 $backendName = $this->backendClass();
951 $this->prepare( array( 'dir' => dirname( $source ) ) );
953 $status = $this->backend
->doOperation(
954 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
955 $this->assertEquals( array(), $status->errors
,
956 "Creation of file at $source succeeded ($backendName)." );
958 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
959 $this->assertNotNull( $tmpFile,
960 "Creation of local copy of $source succeeded ($backendName)." );
962 $contents = file_get_contents( $tmpFile->getPath() );
963 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
966 function provider_testGetLocalReference() {
969 $base = $this->baseStorePath();
970 $cases[] = array( "$base/unittest-cont1/a/z/some_file.txt", "some file contents" );
971 $cases[] = array( "$base/unittest-cont1/a/some-other_file.txt", "more file contents" );
977 * @dataProvider provider_testPrepareAndClean
979 public function testPrepareAndClean( $path, $isOK ) {
980 $this->backend
= $this->singleBackend
;
981 $this->doTestPrepareAndClean( $path, $isOK );
982 $this->tearDownFiles();
984 $this->backend
= $this->multiBackend
;
985 $this->doTestPrepareAndClean( $path, $isOK );
986 $this->tearDownFiles();
989 function provider_testPrepareAndClean() {
990 $base = $this->baseStorePath();
992 array( "$base/unittest-cont1/a/z/some_file1.txt", true ),
993 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
994 # Specific to FS backend with no basePath field set
995 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
999 function doTestPrepareAndClean( $path, $isOK ) {
1000 $backendName = $this->backendClass();
1002 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1004 $this->assertEquals( array(), $status->errors
,
1005 "Preparing dir $path succeeded without warnings ($backendName)." );
1006 $this->assertEquals( true, $status->isOK(),
1007 "Preparing dir $path succeeded ($backendName)." );
1009 $this->assertEquals( false, $status->isOK(),
1010 "Preparing dir $path failed ($backendName)." );
1013 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1015 $this->assertEquals( array(), $status->errors
,
1016 "Cleaning dir $path succeeded without warnings ($backendName)." );
1017 $this->assertEquals( true, $status->isOK(),
1018 "Cleaning dir $path succeeded ($backendName)." );
1020 $this->assertEquals( false, $status->isOK(),
1021 "Cleaning dir $path failed ($backendName)." );
1025 // @TODO: testSecure
1027 public function testDoOperations() {
1028 $this->backend
= $this->singleBackend
;
1029 $this->tearDownFiles();
1030 $this->doTestDoOperations();
1031 $this->tearDownFiles();
1033 $this->backend
= $this->multiBackend
;
1034 $this->tearDownFiles();
1035 $this->doTestDoOperations();
1036 $this->tearDownFiles();
1038 $this->backend
= $this->singleBackend
;
1039 $this->tearDownFiles();
1040 $this->doTestDoOperationsFailing();
1041 $this->tearDownFiles();
1043 $this->backend
= $this->multiBackend
;
1044 $this->tearDownFiles();
1045 $this->doTestDoOperationsFailing();
1046 $this->tearDownFiles();
1048 // @TODO: test some cases where the ops should fail
1051 function doTestDoOperations() {
1052 $base = $this->baseStorePath();
1054 $fileA = "$base/unittest-cont1/a/b/fileA.txt";
1055 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1056 $fileB = "$base/unittest-cont1/a/b/fileB.txt";
1057 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1058 $fileC = "$base/unittest-cont1/a/b/fileC.txt";
1059 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1060 $fileD = "$base/unittest-cont1/a/b/fileD.txt";
1062 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1063 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1064 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1065 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1066 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1067 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1069 $status = $this->backend
->doOperations( array(
1070 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1071 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1072 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1073 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1074 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1075 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1076 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1077 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1078 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1079 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1080 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1081 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1082 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1083 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1084 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1085 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1086 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1088 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1090 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1092 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1094 array( 'op' => 'null' ),
1098 $this->assertEquals( array(), $status->errors
, "Operation batch succeeded" );
1099 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1100 $this->assertEquals( 13, count( $status->success
),
1101 "Operation batch has correct success array" );
1103 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1104 "File does not exist at $fileA" );
1105 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1106 "File does not exist at $fileB" );
1107 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1108 "File does not exist at $fileD" );
1110 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1111 "File exists at $fileC" );
1112 $this->assertEquals( $fileBContents,
1113 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1114 "Correct file contents of $fileC" );
1115 $this->assertEquals( strlen( $fileBContents ),
1116 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1117 "Correct file size of $fileC" );
1118 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1119 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1120 "Correct file SHA-1 of $fileC" );
1123 function doTestDoOperationsFailing() {
1124 $base = $this->baseStorePath();
1126 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1127 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1128 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1129 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1130 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1131 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1132 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1134 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1135 $this->backend
->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1136 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1137 $this->backend
->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1138 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1139 $this->backend
->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1141 $status = $this->backend
->doOperations( array(
1142 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1143 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1144 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1145 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1146 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1147 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1148 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1149 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1150 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1151 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1152 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1153 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1154 array( 'op' => 'delete', 'src' => $fileD ),
1155 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1156 array( 'op' => 'null' ),
1158 ), array( 'force' => 1 ) );
1160 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1161 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1162 $this->assertEquals( 8, count( $status->success
),
1163 "Operation batch has correct success array" );
1165 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1166 "File does not exist at $fileB" );
1167 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1168 "File does not exist at $fileD" );
1170 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1171 "File does not exist at $fileA" );
1172 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1173 "File exists at $fileC" );
1174 $this->assertEquals( $fileBContents,
1175 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1176 "Correct file contents of $fileA" );
1177 $this->assertEquals( strlen( $fileBContents ),
1178 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1179 "Correct file size of $fileA" );
1180 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1181 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1182 "Correct file SHA-1 of $fileA" );
1185 public function testGetFileList() {
1186 $this->backend
= $this->singleBackend
;
1187 $this->tearDownFiles();
1188 $this->doTestGetFileList();
1189 $this->tearDownFiles();
1191 $this->backend
= $this->multiBackend
;
1192 $this->tearDownFiles();
1193 $this->doTestGetFileList();
1194 $this->tearDownFiles();
1197 private function doTestGetFileList() {
1198 $backendName = $this->backendClass();
1200 $base = $this->baseStorePath();
1202 "$base/unittest-cont1/test1.txt",
1203 "$base/unittest-cont1/test2.txt",
1204 "$base/unittest-cont1/test3.txt",
1205 "$base/unittest-cont1/subdir1/test1.txt",
1206 "$base/unittest-cont1/subdir1/test2.txt",
1207 "$base/unittest-cont1/subdir2/test3.txt",
1208 "$base/unittest-cont1/subdir2/test4.txt",
1209 "$base/unittest-cont1/subdir2/subdir/test1.txt",
1210 "$base/unittest-cont1/subdir2/subdir/test2.txt",
1211 "$base/unittest-cont1/subdir2/subdir/test3.txt",
1212 "$base/unittest-cont1/subdir2/subdir/test4.txt",
1213 "$base/unittest-cont1/subdir2/subdir/test5.txt",
1214 "$base/unittest-cont1/subdir2/subdir/sub/test0.txt",
1215 "$base/unittest-cont1/subdir2/subdir/sub/120-px-file.txt",
1220 foreach ( $files as $file ) {
1221 $this->prepare( array( 'dir' => dirname( $file ) ) );
1222 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1224 $status = $this->backend
->doOperations( $ops );
1225 $this->assertEquals( array(), $status->errors
,
1226 "Creation of files succeeded ($backendName)." );
1227 $this->assertEquals( true, $status->isOK(),
1228 "Creation of files succeeded with OK status ($backendName)." );
1235 "subdir1/test1.txt",
1236 "subdir1/test2.txt",
1237 "subdir2/test3.txt",
1238 "subdir2/test4.txt",
1239 "subdir2/subdir/test1.txt",
1240 "subdir2/subdir/test2.txt",
1241 "subdir2/subdir/test3.txt",
1242 "subdir2/subdir/test4.txt",
1243 "subdir2/subdir/test5.txt",
1244 "subdir2/subdir/sub/test0.txt",
1245 "subdir2/subdir/sub/120-px-file.txt",
1249 // Actual listing (no trailing slash)
1251 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1252 foreach ( $iter as $file ) {
1257 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1259 // Actual listing (with trailing slash)
1261 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1262 foreach ( $iter as $file ) {
1267 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1277 "sub/120-px-file.txt",
1281 // Actual listing (no trailing slash)
1283 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir" ) );
1284 foreach ( $iter as $file ) {
1289 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1291 // Actual listing (with trailing slash)
1293 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/subdir2/subdir/" ) );
1294 foreach ( $iter as $file ) {
1299 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1301 // Actual listing (using iterator second time)
1303 foreach ( $iter as $file ) {
1308 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1310 foreach ( $files as $file ) { // clean up
1311 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1314 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1315 foreach ( $iter as $iter ) {} // no errors
1318 // test helper wrapper for backend prepare() function
1319 private function prepare( array $params ) {
1320 $this->dirsToPrune
[] = $params['dir'];
1321 return $this->backend
->prepare( $params );
1324 function tearDownFiles() {
1325 foreach ( $this->filesToPrune
as $file ) {
1328 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1329 foreach ( $containers as $container ) {
1330 $this->deleteFiles( $container );
1332 foreach ( $this->dirsToPrune
as $dir ) {
1333 $this->recursiveClean( $dir );
1335 $this->filesToPrune
= $this->dirsToPrune
= array();
1338 private function deleteFiles( $container ) {
1339 $base = $this->baseStorePath();
1340 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1342 foreach ( $iter as $file ) {
1343 $this->backend
->delete( array( 'src' => "$base/$container/$file" ), array( 'force' => 1 ) );
1348 private function recursiveClean( $dir ) {
1350 if ( !$this->backend
->clean( array( 'dir' => $dir ) )->isOK() ) {
1353 } while ( $dir = FileBackend
::parentStoragePath( $dir ) );
1356 function tearDown() {