5 * ^---- causes phpunit to use a higher timeout threshold
10 class FileBackendTest
extends MediaWikiTestCase
{
11 private $backend, $multiBackend;
12 private $filesToPrune = array();
13 private static $backendToUse;
16 global $wgFileBackends;
18 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
19 if ( $this->getCliArg( 'use-filebackend=' ) ) {
20 if ( self
::$backendToUse ) {
21 $this->singleBackend
= self
::$backendToUse;
23 $name = $this->getCliArg( 'use-filebackend=' );
25 foreach ( $wgFileBackends as $conf ) {
26 if ( $conf['name'] == $name ) {
31 $useConfig['name'] = 'localtesting'; // swap name
32 $useConfig['shardViaHashLevels'] = array( // test sharding
33 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
35 $class = $useConfig['class'];
36 self
::$backendToUse = new $class( $useConfig );
37 $this->singleBackend
= self
::$backendToUse;
40 $this->singleBackend
= new FSFileBackend( array(
41 'name' => 'localtesting',
42 'lockManager' => 'fsLockManager',
43 #'parallelize' => 'implicit',
44 'containerPaths' => array(
45 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
46 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
49 $this->multiBackend
= new FileBackendMultiWrite( array(
50 'name' => 'localtesting',
51 'lockManager' => 'fsLockManager',
52 'parallelize' => 'implicit',
55 'name' => 'localmutlitesting1',
56 'class' => 'FSFileBackend',
57 'lockManager' => 'nullLockManager',
58 'containerPaths' => array(
59 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
60 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
61 'isMultiMaster' => false
64 'name' => 'localmutlitesting2',
65 'class' => 'FSFileBackend',
66 'lockManager' => 'nullLockManager',
67 'containerPaths' => array(
68 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
69 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
70 'isMultiMaster' => true
74 $this->filesToPrune
= array();
77 private function baseStorePath() {
78 return 'mwstore://localtesting';
81 private function backendClass() {
82 return get_class( $this->backend
);
86 * @dataProvider provider_testIsStoragePath
88 public function testIsStoragePath( $path, $isStorePath ) {
89 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
90 "FileBackend::isStoragePath on path '$path'" );
93 function provider_testIsStoragePath() {
95 array( 'mwstore://', true ),
96 array( 'mwstore://backend', true ),
97 array( 'mwstore://backend/container', true ),
98 array( 'mwstore://backend/container/', true ),
99 array( 'mwstore://backend/container/path', true ),
100 array( 'mwstore://backend//container/', true ),
101 array( 'mwstore://backend//container//', true ),
102 array( 'mwstore://backend//container//path', true ),
103 array( 'mwstore:///', true ),
104 array( 'mwstore:/', false ),
105 array( 'mwstore:', false ),
110 * @dataProvider provider_testSplitStoragePath
112 public function testSplitStoragePath( $path, $res ) {
113 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
114 "FileBackend::splitStoragePath on path '$path'" );
117 function provider_testSplitStoragePath() {
119 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
120 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
121 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
122 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
123 array( 'mwstore://backend//container/path', array( null, null, null ) ),
124 array( 'mwstore://backend//container//path', array( null, null, null ) ),
125 array( 'mwstore://', array( null, null, null ) ),
126 array( 'mwstore://backend', array( null, null, null ) ),
127 array( 'mwstore:///', array( null, null, null ) ),
128 array( 'mwstore:/', array( null, null, null ) ),
129 array( 'mwstore:', array( null, null, null ) )
134 * @dataProvider provider_normalizeStoragePath
136 public function testNormalizeStoragePath( $path, $res ) {
137 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
138 "FileBackend::normalizeStoragePath on path '$path'" );
141 function provider_normalizeStoragePath() {
143 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
144 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
145 array( 'mwstore://backend/container/path', 'mwstore://backend/container/path' ),
146 array( 'mwstore://backend/container//path', 'mwstore://backend/container/path' ),
147 array( 'mwstore://backend/container///path', 'mwstore://backend/container/path' ),
148 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
149 array( 'mwstore://', null ),
150 array( 'mwstore://backend', null ),
151 array( 'mwstore://backend//container/path', null ),
152 array( 'mwstore://backend//container//path', null ),
153 array( 'mwstore:///', null ),
154 array( 'mwstore:/', null ),
155 array( 'mwstore:', null ), )
160 * @dataProvider provider_testParentStoragePath
162 public function testParentStoragePath( $path, $res ) {
163 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
164 "FileBackend::parentStoragePath on path '$path'" );
167 function provider_testParentStoragePath() {
169 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
170 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
171 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
172 array( 'mwstore://backend/container', null ),
173 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
174 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
175 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
176 array( 'mwstore://backend/container/', null ),
181 * @dataProvider provider_testExtensionFromPath
183 public function testExtensionFromPath( $path, $res ) {
184 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
185 "FileBackend::extensionFromPath on path '$path'" );
188 function provider_testExtensionFromPath() {
190 array( 'mwstore://backend/container/path.txt', 'txt' ),
191 array( 'mwstore://backend/container/path.svg.png', 'png' ),
192 array( 'mwstore://backend/container/path', '' ),
193 array( 'mwstore://backend/container/path.', '' ),
198 * @dataProvider provider_testStore
200 public function testStore( $op ) {
201 $this->filesToPrune
[] = $op['src'];
203 $this->backend
= $this->singleBackend
;
204 $this->tearDownFiles();
205 $this->doTestStore( $op );
206 $this->tearDownFiles();
208 $this->backend
= $this->multiBackend
;
209 $this->tearDownFiles();
210 $this->doTestStore( $op );
211 $this->filesToPrune
[] = $op['src']; # avoid file leaking
212 $this->tearDownFiles();
215 private function doTestStore( $op ) {
216 $backendName = $this->backendClass();
218 $source = $op['src'];
220 $this->prepare( array( 'dir' => dirname( $dest ) ) );
222 file_put_contents( $source, "Unit test file" );
224 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
225 $this->backend
->store( $op );
228 $status = $this->backend
->doOperation( $op );
230 $this->assertGoodStatus( $status,
231 "Store from $source to $dest succeeded without warnings ($backendName)." );
232 $this->assertEquals( true, $status->isOK(),
233 "Store from $source to $dest succeeded ($backendName)." );
234 $this->assertEquals( array( 0 => true ), $status->success
,
235 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
236 $this->assertEquals( true, file_exists( $source ),
237 "Source file $source still exists ($backendName)." );
238 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
239 "Destination file $dest exists ($backendName)." );
241 $this->assertEquals( filesize( $source ),
242 $this->backend
->getFileSize( array( 'src' => $dest ) ),
243 "Destination file $dest has correct size ($backendName)." );
245 $props1 = FSFile
::getPropsFromPath( $source );
246 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
247 $this->assertEquals( $props1, $props2,
248 "Source and destination have the same props ($backendName)." );
250 $this->assertBackendPathsConsistent( array( $dest ) );
253 public function provider_testStore() {
256 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
257 $toPath = $this->baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
258 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
266 $op2['overwrite'] = true;
274 $op2['overwriteSame'] = true;
285 * @dataProvider provider_testCopy
287 public function testCopy( $op ) {
288 $this->backend
= $this->singleBackend
;
289 $this->tearDownFiles();
290 $this->doTestCopy( $op );
291 $this->tearDownFiles();
293 $this->backend
= $this->multiBackend
;
294 $this->tearDownFiles();
295 $this->doTestCopy( $op );
296 $this->tearDownFiles();
299 private function doTestCopy( $op ) {
300 $backendName = $this->backendClass();
302 $source = $op['src'];
304 $this->prepare( array( 'dir' => dirname( $source ) ) );
305 $this->prepare( array( 'dir' => dirname( $dest ) ) );
307 $status = $this->backend
->doOperation(
308 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
309 $this->assertGoodStatus( $status,
310 "Creation of file at $source succeeded ($backendName)." );
312 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
313 $this->backend
->copy( $op );
316 $status = $this->backend
->doOperation( $op );
318 $this->assertGoodStatus( $status,
319 "Copy from $source to $dest succeeded without warnings ($backendName)." );
320 $this->assertEquals( true, $status->isOK(),
321 "Copy from $source to $dest succeeded ($backendName)." );
322 $this->assertEquals( array( 0 => true ), $status->success
,
323 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
324 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
325 "Source file $source still exists ($backendName)." );
326 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
327 "Destination file $dest exists after copy ($backendName)." );
330 $this->backend
->getFileSize( array( 'src' => $source ) ),
331 $this->backend
->getFileSize( array( 'src' => $dest ) ),
332 "Destination file $dest has correct size ($backendName)." );
334 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
335 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
336 $this->assertEquals( $props1, $props2,
337 "Source and destination have the same props ($backendName)." );
339 $this->assertBackendPathsConsistent( array( $source, $dest ) );
342 public function provider_testCopy() {
345 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
346 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
348 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
356 $op2['overwrite'] = true;
364 $op2['overwriteSame'] = true;
375 * @dataProvider provider_testMove
377 public function testMove( $op ) {
378 $this->backend
= $this->singleBackend
;
379 $this->tearDownFiles();
380 $this->doTestMove( $op );
381 $this->tearDownFiles();
383 $this->backend
= $this->multiBackend
;
384 $this->tearDownFiles();
385 $this->doTestMove( $op );
386 $this->tearDownFiles();
389 private function doTestMove( $op ) {
390 $backendName = $this->backendClass();
392 $source = $op['src'];
394 $this->prepare( array( 'dir' => dirname( $source ) ) );
395 $this->prepare( array( 'dir' => dirname( $dest ) ) );
397 $status = $this->backend
->doOperation(
398 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
399 $this->assertGoodStatus( $status,
400 "Creation of file at $source succeeded ($backendName)." );
402 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
403 $this->backend
->copy( $op );
406 $status = $this->backend
->doOperation( $op );
407 $this->assertGoodStatus( $status,
408 "Move from $source to $dest succeeded without warnings ($backendName)." );
409 $this->assertEquals( true, $status->isOK(),
410 "Move from $source to $dest succeeded ($backendName)." );
411 $this->assertEquals( array( 0 => true ), $status->success
,
412 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
413 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
414 "Source file $source does not still exists ($backendName)." );
415 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
416 "Destination file $dest exists after move ($backendName)." );
418 $this->assertNotEquals(
419 $this->backend
->getFileSize( array( 'src' => $source ) ),
420 $this->backend
->getFileSize( array( 'src' => $dest ) ),
421 "Destination file $dest has correct size ($backendName)." );
423 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
424 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
425 $this->assertEquals( false, $props1['fileExists'],
426 "Source file does not exist accourding to props ($backendName)." );
427 $this->assertEquals( true, $props2['fileExists'],
428 "Destination file exists accourding to props ($backendName)." );
430 $this->assertBackendPathsConsistent( array( $source, $dest ) );
433 public function provider_testMove() {
436 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
437 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
439 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
447 $op2['overwrite'] = true;
455 $op2['overwriteSame'] = true;
466 * @dataProvider provider_testDelete
468 public function testDelete( $op, $withSource, $okStatus ) {
469 $this->backend
= $this->singleBackend
;
470 $this->tearDownFiles();
471 $this->doTestDelete( $op, $withSource, $okStatus );
472 $this->tearDownFiles();
474 $this->backend
= $this->multiBackend
;
475 $this->tearDownFiles();
476 $this->doTestDelete( $op, $withSource, $okStatus );
477 $this->tearDownFiles();
480 private function doTestDelete( $op, $withSource, $okStatus ) {
481 $backendName = $this->backendClass();
483 $source = $op['src'];
484 $this->prepare( array( 'dir' => dirname( $source ) ) );
487 $status = $this->backend
->doOperation(
488 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
489 $this->assertGoodStatus( $status,
490 "Creation of file at $source succeeded ($backendName)." );
493 $status = $this->backend
->doOperation( $op );
495 $this->assertGoodStatus( $status,
496 "Deletion of file at $source succeeded without warnings ($backendName)." );
497 $this->assertEquals( true, $status->isOK(),
498 "Deletion of file at $source succeeded ($backendName)." );
499 $this->assertEquals( array( 0 => true ), $status->success
,
500 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
502 $this->assertEquals( false, $status->isOK(),
503 "Deletion of file at $source failed ($backendName)." );
506 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
507 "Source file $source does not exist after move ($backendName)." );
510 $this->backend
->getFileSize( array( 'src' => $source ) ),
511 "Source file $source has correct size (false) ($backendName)." );
513 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
514 $this->assertFalse( $props1['fileExists'],
515 "Source file $source does not exist according to props ($backendName)." );
517 $this->assertBackendPathsConsistent( array( $source ) );
520 public function provider_testDelete() {
523 $source = $this->baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
525 $op = array( 'op' => 'delete', 'src' => $source );
534 false, // without source
538 $op['ignoreMissingSource'] = true;
541 false, // without source
549 * @dataProvider provider_testCreate
551 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
552 $this->backend
= $this->singleBackend
;
553 $this->tearDownFiles();
554 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
555 $this->tearDownFiles();
557 $this->backend
= $this->multiBackend
;
558 $this->tearDownFiles();
559 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
560 $this->tearDownFiles();
563 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
564 $backendName = $this->backendClass();
567 $this->prepare( array( 'dir' => dirname( $dest ) ) );
569 $oldText = 'blah...blah...waahwaah';
570 if ( $alreadyExists ) {
571 $status = $this->backend
->doOperation(
572 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
573 $this->assertGoodStatus( $status,
574 "Creation of file at $dest succeeded ($backendName)." );
577 $status = $this->backend
->doOperation( $op );
579 $this->assertGoodStatus( $status,
580 "Creation of file at $dest succeeded without warnings ($backendName)." );
581 $this->assertEquals( true, $status->isOK(),
582 "Creation of file at $dest succeeded ($backendName)." );
583 $this->assertEquals( array( 0 => true ), $status->success
,
584 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
586 $this->assertEquals( false, $status->isOK(),
587 "Creation of file at $dest failed ($backendName)." );
590 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
591 "Destination file $dest exists after creation ($backendName)." );
593 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
594 $this->assertEquals( true, $props1['fileExists'],
595 "Destination file $dest exists according to props ($backendName)." );
596 if ( $okStatus ) { // file content is what we saved
597 $this->assertEquals( $newSize, $props1['size'],
598 "Destination file $dest has expected size according to props ($backendName)." );
599 $this->assertEquals( $newSize,
600 $this->backend
->getFileSize( array( 'src' => $dest ) ),
601 "Destination file $dest has correct size ($backendName)." );
602 } else { // file content is some other previous text
603 $this->assertEquals( strlen( $oldText ), $props1['size'],
604 "Destination file $dest has original size according to props ($backendName)." );
605 $this->assertEquals( strlen( $oldText ),
606 $this->backend
->getFileSize( array( 'src' => $dest ) ),
607 "Destination file $dest has original size according to props ($backendName)." );
610 $this->assertBackendPathsConsistent( array( $dest ) );
614 * @dataProvider provider_testCreate
616 public function provider_testCreate() {
619 $dest = $this->baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
621 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
624 false, // no dest already exists
626 strlen( $op['content'] )
630 $op2['content'] = "\n";
633 false, // no dest already exists
635 strlen( $op2['content'] )
639 $op2['content'] = "fsf\n waf 3kt";
642 true, // dest already exists
644 strlen( $op2['content'] )
648 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
649 $op2['overwrite'] = true;
652 true, // dest already exists
654 strlen( $op2['content'] )
658 $op2['content'] = "39qjmg3-qg";
659 $op2['overwriteSame'] = true;
662 true, // dest already exists
664 strlen( $op2['content'] )
670 public function testDoQuickOperations() {
671 $this->backend
= $this->singleBackend
;
672 $this->doTestDoQuickOperations();
673 $this->tearDownFiles();
675 $this->backend
= $this->multiBackend
;
676 $this->doTestDoQuickOperations();
677 $this->tearDownFiles();
680 private function doTestDoQuickOperations() {
681 $backendName = $this->backendClass();
683 $base = $this->baseStorePath();
685 "$base/unittest-cont1/e/fileA.a",
686 "$base/unittest-cont1/e/fileB.a",
687 "$base/unittest-cont1/e/fileC.a"
691 foreach ( $files as $path ) {
692 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
693 $this->assertGoodStatus( $status,
694 "Preparing $path succeeded without warnings ($backendName)." );
695 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
696 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
698 $purgeOps[] = array( 'op' => 'null' );
699 $status = $this->backend
->doQuickOperations( $ops );
700 $this->assertGoodStatus( $status,
701 "Creation of source files succeeded ($backendName)." );
703 foreach ( $files as $file ) {
704 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
705 "File $file exists." );
708 $status = $this->backend
->doQuickOperations( $purgeOps );
709 $this->assertGoodStatus( $status,
710 "Quick deletion of source files succeeded ($backendName)." );
712 foreach ( $files as $file ) {
713 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
714 "File $file purged." );
719 * @dataProvider provider_testConcatenate
721 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
722 $this->filesToPrune
[] = $op['dst'];
724 $this->backend
= $this->singleBackend
;
725 $this->tearDownFiles();
726 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
727 $this->tearDownFiles();
729 $this->backend
= $this->multiBackend
;
730 $this->tearDownFiles();
731 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
732 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
733 $this->tearDownFiles();
736 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
737 $backendName = $this->backendClass();
742 foreach ( $srcs as $i => $source ) {
743 $this->prepare( array( 'dir' => dirname( $source ) ) );
745 'op' => 'create', // operation
746 'dst' => $source, // source
747 'content' => $srcsContent[$i]
749 $expContent .= $srcsContent[$i];
751 $status = $this->backend
->doOperations( $ops );
753 $this->assertGoodStatus( $status,
754 "Creation of source files succeeded ($backendName)." );
756 $dest = $params['dst'];
757 if ( $alreadyExists ) {
758 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
759 $this->assertEquals( true, $ok,
760 "Creation of file at $dest succeeded ($backendName)." );
762 $ok = file_put_contents( $dest, '' ) !== false;
763 $this->assertEquals( true, $ok,
764 "Creation of 0-byte file at $dest succeeded ($backendName)." );
767 // Combine the files into one
768 $status = $this->backend
->concatenate( $params );
770 $this->assertGoodStatus( $status,
771 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
772 $this->assertEquals( true, $status->isOK(),
773 "Creation of concat file at $dest succeeded ($backendName)." );
775 $this->assertEquals( false, $status->isOK(),
776 "Creation of concat file at $dest failed ($backendName)." );
780 $this->assertEquals( true, is_file( $dest ),
781 "Dest concat file $dest exists after creation ($backendName)." );
783 $this->assertEquals( true, is_file( $dest ),
784 "Dest concat file $dest exists after failed creation ($backendName)." );
787 $contents = file_get_contents( $dest );
788 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
791 $this->assertEquals( $expContent, $contents,
792 "Concat file at $dest has correct contents ($backendName)." );
794 $this->assertNotEquals( $expContent, $contents,
795 "Concat file at $dest has correct contents ($backendName)." );
799 function provider_testConcatenate() {
802 $rand = mt_rand( 0, 2000000000 ) . time();
803 $dest = wfTempDir() . "/randomfile!$rand.txt";
805 $this->baseStorePath() . '/unittest-cont1/e/file1.txt',
806 $this->baseStorePath() . '/unittest-cont1/e/file2.txt',
807 $this->baseStorePath() . '/unittest-cont1/e/file3.txt',
808 $this->baseStorePath() . '/unittest-cont1/e/file4.txt',
809 $this->baseStorePath() . '/unittest-cont1/e/file5.txt',
810 $this->baseStorePath() . '/unittest-cont1/e/file6.txt',
811 $this->baseStorePath() . '/unittest-cont1/e/file7.txt',
812 $this->baseStorePath() . '/unittest-cont1/e/file8.txt',
813 $this->baseStorePath() . '/unittest-cont1/e/file9.txt',
814 $this->baseStorePath() . '/unittest-cont1/e/file10.txt'
828 $params = array( 'srcs' => $srcs, 'dst' => $dest );
831 $params, // operation
833 $content, // content for each source
834 false, // no dest already exists
839 $params, // operation
841 $content, // content for each source
842 true, // dest already exists
850 * @dataProvider provider_testGetFileStat
852 public function testGetFileStat( $path, $content, $alreadyExists ) {
853 $this->backend
= $this->singleBackend
;
854 $this->tearDownFiles();
855 $this->doTestGetFileStat( $path, $content, $alreadyExists );
856 $this->tearDownFiles();
858 $this->backend
= $this->multiBackend
;
859 $this->tearDownFiles();
860 $this->doTestGetFileStat( $path, $content, $alreadyExists );
861 $this->tearDownFiles();
864 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
865 $backendName = $this->backendClass();
867 if ( $alreadyExists ) {
868 $this->prepare( array( 'dir' => dirname( $path ) ) );
869 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
870 $this->assertGoodStatus( $status,
871 "Creation of file at $path succeeded ($backendName)." );
873 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
874 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
875 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
877 $this->assertEquals( strlen( $content ), $size,
878 "Correct file size of '$path'" );
879 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
880 "Correct file timestamp of '$path'" );
882 $size = $stat['size'];
883 $time = $stat['mtime'];
884 $this->assertEquals( strlen( $content ), $size,
885 "Correct file size of '$path'" );
886 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
887 "Correct file timestamp of '$path'" );
889 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
890 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
891 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
893 $this->assertFalse( $size, "Correct file size of '$path'" );
894 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
895 $this->assertFalse( $stat, "Correct file stat of '$path'" );
899 function provider_testGetFileStat() {
902 $base = $this->baseStorePath();
903 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
904 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
905 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
911 * @dataProvider provider_testGetFileContents
913 public function testGetFileContents( $source, $content ) {
914 $this->backend
= $this->singleBackend
;
915 $this->tearDownFiles();
916 $this->doTestGetFileContents( $source, $content );
917 $this->tearDownFiles();
919 $this->backend
= $this->multiBackend
;
920 $this->tearDownFiles();
921 $this->doTestGetFileContents( $source, $content );
922 $this->tearDownFiles();
925 private function doTestGetFileContents( $source, $content ) {
926 $backendName = $this->backendClass();
928 $this->prepare( array( 'dir' => dirname( $source ) ) );
930 $status = $this->backend
->doOperation(
931 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
932 $this->assertGoodStatus( $status,
933 "Creation of file at $source succeeded ($backendName)." );
934 $this->assertEquals( true, $status->isOK(),
935 "Creation of file at $source succeeded with OK status ($backendName)." );
937 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
938 $this->assertNotEquals( false, $newContents,
939 "Read of file at $source succeeded ($backendName)." );
941 $this->assertEquals( $content, $newContents,
942 "Contents read match data at $source ($backendName)." );
945 function provider_testGetFileContents() {
948 $base = $this->baseStorePath();
949 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
950 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
956 * @dataProvider provider_testGetLocalCopy
958 public function testGetLocalCopy( $source, $content ) {
959 $this->backend
= $this->singleBackend
;
960 $this->tearDownFiles();
961 $this->doTestGetLocalCopy( $source, $content );
962 $this->tearDownFiles();
964 $this->backend
= $this->multiBackend
;
965 $this->tearDownFiles();
966 $this->doTestGetLocalCopy( $source, $content );
967 $this->tearDownFiles();
970 private function doTestGetLocalCopy( $source, $content ) {
971 $backendName = $this->backendClass();
973 $this->prepare( array( 'dir' => dirname( $source ) ) );
975 $status = $this->backend
->doOperation(
976 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
977 $this->assertGoodStatus( $status,
978 "Creation of file at $source succeeded ($backendName)." );
980 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
981 $this->assertNotNull( $tmpFile,
982 "Creation of local copy of $source succeeded ($backendName)." );
984 $contents = file_get_contents( $tmpFile->getPath() );
985 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
988 function provider_testGetLocalCopy() {
991 $base = $this->baseStorePath();
992 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
993 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
999 * @dataProvider provider_testGetLocalReference
1001 public function testGetLocalReference( $source, $content ) {
1002 $this->backend
= $this->singleBackend
;
1003 $this->tearDownFiles();
1004 $this->doTestGetLocalReference( $source, $content );
1005 $this->tearDownFiles();
1007 $this->backend
= $this->multiBackend
;
1008 $this->tearDownFiles();
1009 $this->doTestGetLocalReference( $source, $content );
1010 $this->tearDownFiles();
1013 private function doTestGetLocalReference( $source, $content ) {
1014 $backendName = $this->backendClass();
1016 $this->prepare( array( 'dir' => dirname( $source ) ) );
1018 $status = $this->create( array( 'content' => $content, 'dst' => $source ) );
1019 $this->assertGoodStatus( $status,
1020 "Creation of file at $source succeeded ($backendName)." );
1022 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1023 $this->assertNotNull( $tmpFile,
1024 "Creation of local copy of $source succeeded ($backendName)." );
1026 $contents = file_get_contents( $tmpFile->getPath() );
1027 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1030 function provider_testGetLocalReference() {
1033 $base = $this->baseStorePath();
1034 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1035 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1041 * @dataProvider provider_testPrepareAndClean
1043 public function testPrepareAndClean( $path, $isOK ) {
1044 $this->backend
= $this->singleBackend
;
1045 $this->doTestPrepareAndClean( $path, $isOK );
1046 $this->tearDownFiles();
1048 $this->backend
= $this->multiBackend
;
1049 $this->doTestPrepareAndClean( $path, $isOK );
1050 $this->tearDownFiles();
1053 function provider_testPrepareAndClean() {
1054 $base = $this->baseStorePath();
1056 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1057 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1058 # Specific to FS backend with no basePath field set
1059 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1063 private function doTestPrepareAndClean( $path, $isOK ) {
1064 $backendName = $this->backendClass();
1066 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1068 $this->assertGoodStatus( $status,
1069 "Preparing dir $path succeeded without warnings ($backendName)." );
1070 $this->assertEquals( true, $status->isOK(),
1071 "Preparing dir $path succeeded ($backendName)." );
1073 $this->assertEquals( false, $status->isOK(),
1074 "Preparing dir $path failed ($backendName)." );
1077 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1079 $this->assertGoodStatus( $status,
1080 "Cleaning dir $path succeeded without warnings ($backendName)." );
1081 $this->assertEquals( true, $status->isOK(),
1082 "Cleaning dir $path succeeded ($backendName)." );
1084 $this->assertEquals( false, $status->isOK(),
1085 "Cleaning dir $path failed ($backendName)." );
1089 public function testRecursiveClean() {
1090 $this->backend
= $this->singleBackend
;
1091 $this->doTestRecursiveClean();
1092 $this->tearDownFiles();
1094 $this->backend
= $this->multiBackend
;
1095 $this->doTestRecursiveClean();
1096 $this->tearDownFiles();
1099 private function doTestRecursiveClean() {
1100 $backendName = $this->backendClass();
1102 $base = $this->baseStorePath();
1104 "$base/unittest-cont1/e/a",
1105 "$base/unittest-cont1/e/a/b",
1106 "$base/unittest-cont1/e/a/b/c",
1107 "$base/unittest-cont1/e/a/b/c/d0",
1108 "$base/unittest-cont1/e/a/b/c/d1",
1109 "$base/unittest-cont1/e/a/b/c/d2",
1110 "$base/unittest-cont1/e/a/b/c/d0/1",
1111 "$base/unittest-cont1/e/a/b/c/d0/2",
1112 "$base/unittest-cont1/e/a/b/c/d1/3",
1113 "$base/unittest-cont1/e/a/b/c/d1/4",
1114 "$base/unittest-cont1/e/a/b/c/d2/5",
1115 "$base/unittest-cont1/e/a/b/c/d2/6"
1117 foreach ( $dirs as $dir ) {
1118 $status = $this->prepare( array( 'dir' => $dir ) );
1119 $this->assertGoodStatus( $status,
1120 "Preparing dir $dir succeeded without warnings ($backendName)." );
1123 if ( $this->backend
instanceof FSFileBackend
) {
1124 foreach ( $dirs as $dir ) {
1125 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1126 "Dir $dir exists ($backendName)." );
1130 $status = $this->backend
->clean(
1131 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1132 $this->assertGoodStatus( $status,
1133 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1135 foreach ( $dirs as $dir ) {
1136 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1137 "Dir $dir no longer exists ($backendName)." );
1141 // @TODO: testSecure
1143 public function testDoOperations() {
1144 $this->backend
= $this->singleBackend
;
1145 $this->tearDownFiles();
1146 $this->doTestDoOperations();
1147 $this->tearDownFiles();
1149 $this->backend
= $this->multiBackend
;
1150 $this->tearDownFiles();
1151 $this->doTestDoOperations();
1152 $this->tearDownFiles();
1154 $this->backend
= $this->singleBackend
;
1155 $this->tearDownFiles();
1156 $this->doTestDoOperations2();
1157 $this->tearDownFiles();
1159 $this->backend
= $this->multiBackend
;
1160 $this->tearDownFiles();
1161 $this->doTestDoOperations2();
1162 $this->tearDownFiles();
1164 $this->backend
= $this->singleBackend
;
1165 $this->tearDownFiles();
1166 $this->doTestDoOperationsFailing();
1167 $this->tearDownFiles();
1169 $this->backend
= $this->multiBackend
;
1170 $this->tearDownFiles();
1171 $this->doTestDoOperationsFailing();
1172 $this->tearDownFiles();
1175 private function doTestDoOperations() {
1176 $base = $this->baseStorePath();
1178 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1179 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1180 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1181 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1182 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1183 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1184 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1186 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1187 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1188 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1189 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1190 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1191 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1192 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1194 $status = $this->backend
->doOperations( array(
1195 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1196 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1197 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1198 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1199 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1200 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1201 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1202 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1203 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1204 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1205 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1206 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1207 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1208 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1209 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1210 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1211 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1213 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1215 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1217 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1219 array( 'op' => 'null' ),
1223 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1224 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1225 $this->assertEquals( 13, count( $status->success
),
1226 "Operation batch has correct success array" );
1228 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1229 "File does not exist at $fileA" );
1230 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1231 "File does not exist at $fileB" );
1232 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1233 "File does not exist at $fileD" );
1235 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1236 "File exists at $fileC" );
1237 $this->assertEquals( $fileBContents,
1238 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1239 "Correct file contents of $fileC" );
1240 $this->assertEquals( strlen( $fileBContents ),
1241 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1242 "Correct file size of $fileC" );
1243 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1244 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1245 "Correct file SHA-1 of $fileC" );
1248 // concurrency orientated
1249 private function doTestDoOperations2() {
1250 $base = $this->baseStorePath();
1252 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1253 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1254 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1256 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1257 file_put_contents( $tmpNameA, $fileAContents );
1258 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1259 file_put_contents( $tmpNameB, $fileBContents );
1260 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1261 file_put_contents( $tmpNameC, $fileCContents );
1263 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1264 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1265 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1267 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1268 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1269 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1270 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1272 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1273 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1274 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1275 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1276 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1278 $status = $this->backend
->doOperations( array(
1279 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1280 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1281 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1282 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1283 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1284 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1285 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1286 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1287 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1288 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1289 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1290 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1291 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1292 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1293 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1294 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1295 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1296 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1297 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1298 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1300 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1302 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1304 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1306 array( 'op' => 'null' ),
1310 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1311 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1312 $this->assertEquals( 16, count( $status->success
),
1313 "Operation batch has correct success array" );
1315 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1316 "File does not exist at $fileA" );
1317 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1318 "File does not exist at $fileB" );
1319 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1320 "File does not exist at $fileD" );
1322 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1323 "File exists at $fileC" );
1324 $this->assertEquals( $fileBContents,
1325 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1326 "Correct file contents of $fileC" );
1327 $this->assertEquals( strlen( $fileBContents ),
1328 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1329 "Correct file size of $fileC" );
1330 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1331 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1332 "Correct file SHA-1 of $fileC" );
1335 private function doTestDoOperationsFailing() {
1336 $base = $this->baseStorePath();
1338 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1339 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1340 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1341 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1342 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1343 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1344 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1346 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1347 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1348 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1349 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1350 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1351 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1353 $status = $this->backend
->doOperations( array(
1354 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1355 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1356 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1357 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1358 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1359 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1360 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1361 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1362 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1363 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1364 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1365 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1366 array( 'op' => 'delete', 'src' => $fileD ),
1367 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1368 array( 'op' => 'null' ),
1370 ), array( 'force' => 1 ) );
1372 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1373 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1374 $this->assertEquals( 8, count( $status->success
),
1375 "Operation batch has correct success array" );
1377 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1378 "File does not exist at $fileB" );
1379 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1380 "File does not exist at $fileD" );
1382 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1383 "File does not exist at $fileA" );
1384 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1385 "File exists at $fileC" );
1386 $this->assertEquals( $fileBContents,
1387 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1388 "Correct file contents of $fileA" );
1389 $this->assertEquals( strlen( $fileBContents ),
1390 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1391 "Correct file size of $fileA" );
1392 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1393 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1394 "Correct file SHA-1 of $fileA" );
1397 public function testGetFileList() {
1398 $this->backend
= $this->singleBackend
;
1399 $this->tearDownFiles();
1400 $this->doTestGetFileList();
1401 $this->tearDownFiles();
1403 $this->backend
= $this->multiBackend
;
1404 $this->tearDownFiles();
1405 $this->doTestGetFileList();
1406 $this->tearDownFiles();
1409 private function doTestGetFileList() {
1410 $backendName = $this->backendClass();
1411 $base = $this->baseStorePath();
1413 // Should have no errors
1414 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1417 "$base/unittest-cont1/e/test1.txt",
1418 "$base/unittest-cont1/e/test2.txt",
1419 "$base/unittest-cont1/e/test3.txt",
1420 "$base/unittest-cont1/e/subdir1/test1.txt",
1421 "$base/unittest-cont1/e/subdir1/test2.txt",
1422 "$base/unittest-cont1/e/subdir2/test3.txt",
1423 "$base/unittest-cont1/e/subdir2/test4.txt",
1424 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1425 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1426 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1427 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1428 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1429 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1430 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1435 foreach ( $files as $file ) {
1436 $this->prepare( array( 'dir' => dirname( $file ) ) );
1437 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1439 $status = $this->backend
->doQuickOperations( $ops );
1440 $this->assertGoodStatus( $status,
1441 "Creation of files succeeded ($backendName)." );
1442 $this->assertEquals( true, $status->isOK(),
1443 "Creation of files succeeded with OK status ($backendName)." );
1450 "e/subdir1/test1.txt",
1451 "e/subdir1/test2.txt",
1452 "e/subdir2/test3.txt",
1453 "e/subdir2/test4.txt",
1454 "e/subdir2/subdir/test1.txt",
1455 "e/subdir2/subdir/test2.txt",
1456 "e/subdir2/subdir/test3.txt",
1457 "e/subdir2/subdir/test4.txt",
1458 "e/subdir2/subdir/test5.txt",
1459 "e/subdir2/subdir/sub/test0.txt",
1460 "e/subdir2/subdir/sub/120-px-file.txt",
1464 // Actual listing (no trailing slash)
1466 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1467 foreach ( $iter as $file ) {
1472 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1474 // Actual listing (with trailing slash)
1476 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1477 foreach ( $iter as $file ) {
1482 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1492 "sub/120-px-file.txt",
1496 // Actual listing (no trailing slash)
1498 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1499 foreach ( $iter as $file ) {
1504 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1506 // Actual listing (with trailing slash)
1508 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1509 foreach ( $iter as $file ) {
1514 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1516 // Actual listing (using iterator second time)
1518 foreach ( $iter as $file ) {
1523 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1525 // Expected listing (top files only)
1535 // Actual listing (top files only)
1537 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1538 foreach ( $iter as $file ) {
1543 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1545 foreach ( $files as $file ) { // clean up
1546 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1549 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1550 foreach ( $iter as $iter ) {} // no errors
1553 public function testGetDirectoryList() {
1554 $this->backend
= $this->singleBackend
;
1555 $this->tearDownFiles();
1556 $this->doTestGetDirectoryList();
1557 $this->tearDownFiles();
1559 $this->backend
= $this->multiBackend
;
1560 $this->tearDownFiles();
1561 $this->doTestGetDirectoryList();
1562 $this->tearDownFiles();
1565 private function doTestGetDirectoryList() {
1566 $backendName = $this->backendClass();
1568 $base = $this->baseStorePath();
1570 "$base/unittest-cont1/e/test1.txt",
1571 "$base/unittest-cont1/e/test2.txt",
1572 "$base/unittest-cont1/e/test3.txt",
1573 "$base/unittest-cont1/e/subdir1/test1.txt",
1574 "$base/unittest-cont1/e/subdir1/test2.txt",
1575 "$base/unittest-cont1/e/subdir2/test3.txt",
1576 "$base/unittest-cont1/e/subdir2/test4.txt",
1577 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1578 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1579 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1580 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1581 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1582 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1583 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1588 foreach ( $files as $file ) {
1589 $this->prepare( array( 'dir' => dirname( $file ) ) );
1590 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1592 $status = $this->backend
->doQuickOperations( $ops );
1593 $this->assertGoodStatus( $status,
1594 "Creation of files succeeded ($backendName)." );
1595 $this->assertEquals( true, $status->isOK(),
1596 "Creation of files succeeded with OK status ($backendName)." );
1598 $this->assertEquals( true,
1599 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1600 "Directory exists in ($backendName)." );
1601 $this->assertEquals( true,
1602 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1603 "Directory exists in ($backendName)." );
1604 $this->assertEquals( false,
1605 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1606 "Directory does not exists in ($backendName)." );
1614 // Actual listing (no trailing slash)
1616 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1617 foreach ( $iter as $file ) {
1622 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1633 // Actual listing (no trailing slash)
1635 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1636 foreach ( $iter as $file ) {
1641 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1643 // Actual listing (with trailing slash)
1645 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1646 foreach ( $iter as $file ) {
1651 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1659 // Actual listing (no trailing slash)
1661 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1662 foreach ( $iter as $file ) {
1667 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1669 // Actual listing (with trailing slash)
1671 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1672 foreach ( $iter as $file ) {
1677 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1679 // Actual listing (using iterator second time)
1681 foreach ( $iter as $file ) {
1686 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1688 // Expected listing (recursive)
1698 "e/subdir4/subdir/sub",
1702 // Actual listing (recursive)
1704 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1705 foreach ( $iter as $file ) {
1710 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1712 // Expected listing (recursive)
1719 // Actual listing (recursive)
1721 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1722 foreach ( $iter as $file ) {
1727 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1729 // Actual listing (recursive, second time)
1731 foreach ( $iter as $file ) {
1736 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1738 foreach ( $files as $file ) { // clean up
1739 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1742 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1743 foreach ( $iter as $iter ) {} // no errors
1746 public function testLockCalls() {
1747 $this->backend
= $this->singleBackend
;
1748 $this->doTestLockCalls();
1751 private function doTestLockCalls() {
1752 $backendName = $this->backendClass();
1754 for ( $i=0; $i<50; $i++
) {
1760 "subdir1", // duplicate
1761 "subdir1/test1.txt",
1762 "subdir1/test2.txt",
1764 "subdir2", // duplicate
1765 "subdir2/test3.txt",
1766 "subdir2/test4.txt",
1768 "subdir2/subdir/test1.txt",
1769 "subdir2/subdir/test2.txt",
1770 "subdir2/subdir/test3.txt",
1771 "subdir2/subdir/test4.txt",
1772 "subdir2/subdir/test5.txt",
1773 "subdir2/subdir/sub",
1774 "subdir2/subdir/sub/test0.txt",
1775 "subdir2/subdir/sub/120-px-file.txt",
1778 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1779 $this->assertEquals( array(), $status->errors
,
1780 "Locking of files succeeded ($backendName)." );
1781 $this->assertEquals( true, $status->isOK(),
1782 "Locking of files succeeded with OK status ($backendName)." );
1784 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1785 $this->assertEquals( array(), $status->errors
,
1786 "Locking of files succeeded ($backendName)." );
1787 $this->assertEquals( true, $status->isOK(),
1788 "Locking of files succeeded with OK status ($backendName)." );
1790 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1791 $this->assertEquals( array(), $status->errors
,
1792 "Locking of files succeeded ($backendName)." );
1793 $this->assertEquals( true, $status->isOK(),
1794 "Locking of files succeeded with OK status ($backendName)." );
1796 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1797 $this->assertEquals( array(), $status->errors
,
1798 "Locking of files succeeded ($backendName)." );
1799 $this->assertEquals( true, $status->isOK(),
1800 "Locking of files succeeded with OK status ($backendName)." );
1804 // test helper wrapper for backend prepare() function
1805 private function prepare( array $params ) {
1806 return $this->backend
->prepare( $params );
1809 // test helper wrapper for backend prepare() function
1810 private function create( array $params ) {
1811 $params['op'] = 'create';
1812 return $this->backend
->doQuickOperations( array( $params ) );
1815 function tearDownFiles() {
1816 foreach ( $this->filesToPrune
as $file ) {
1819 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1820 foreach ( $containers as $container ) {
1821 $this->deleteFiles( $container );
1823 $this->filesToPrune
= array();
1826 private function deleteFiles( $container ) {
1827 $base = $this->baseStorePath();
1828 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1830 foreach ( $iter as $file ) {
1831 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1832 array( 'force' => 1, 'nonLocking' => 1 ) );
1835 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1838 function assertBackendPathsConsistent( array $paths ) {
1839 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1840 $status = $this->backend
->consistencyCheck( $paths );
1841 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1845 function assertGoodStatus( $status, $msg ) {
1846 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1849 function tearDown() {