5 * ^---- causes phpunit to use a higher timeout threshold
11 class FileBackendTest
extends MediaWikiTestCase
{
12 private $backend, $multiBackend;
13 private $filesToPrune = array();
14 private static $backendToUse;
17 global $wgFileBackends;
19 $tmpPrefix = wfTempDir() . '/filebackend-unittest-' . time() . '-' . mt_rand();
20 if ( $this->getCliArg( 'use-filebackend=' ) ) {
21 if ( self
::$backendToUse ) {
22 $this->singleBackend
= self
::$backendToUse;
24 $name = $this->getCliArg( 'use-filebackend=' );
26 foreach ( $wgFileBackends as $conf ) {
27 if ( $conf['name'] == $name ) {
32 $useConfig['name'] = 'localtesting'; // swap name
33 $useConfig['shardViaHashLevels'] = array( // test sharding
34 'unittest-cont1' => array( 'levels' => 1, 'base' => 16, 'repeat' => 1 )
36 $class = $useConfig['class'];
37 self
::$backendToUse = new $class( $useConfig );
38 $this->singleBackend
= self
::$backendToUse;
41 $this->singleBackend
= new FSFileBackend( array(
42 'name' => 'localtesting',
43 'lockManager' => 'fsLockManager',
44 #'parallelize' => 'implicit',
45 'containerPaths' => array(
46 'unittest-cont1' => "{$tmpPrefix}-localtesting-cont1",
47 'unittest-cont2' => "{$tmpPrefix}-localtesting-cont2" )
50 $this->multiBackend
= new FileBackendMultiWrite( array(
51 'name' => 'localtesting',
52 'lockManager' => 'fsLockManager',
53 'parallelize' => 'implicit',
56 'name' => 'localmultitesting1',
57 'class' => 'FSFileBackend',
58 'lockManager' => 'nullLockManager',
59 'containerPaths' => array(
60 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti1-cont1",
61 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti1-cont2" ),
62 'isMultiMaster' => false
65 'name' => 'localmultitesting2',
66 'class' => 'FSFileBackend',
67 'lockManager' => 'nullLockManager',
68 'containerPaths' => array(
69 'unittest-cont1' => "{$tmpPrefix}-localtestingmulti2-cont1",
70 'unittest-cont2' => "{$tmpPrefix}-localtestingmulti2-cont2" ),
71 'isMultiMaster' => true
75 $this->filesToPrune
= array();
78 private function baseStorePath() {
79 return 'mwstore://localtesting';
82 private function backendClass() {
83 return get_class( $this->backend
);
87 * @dataProvider provider_testIsStoragePath
89 public function testIsStoragePath( $path, $isStorePath ) {
90 $this->assertEquals( $isStorePath, FileBackend
::isStoragePath( $path ),
91 "FileBackend::isStoragePath on path '$path'" );
94 function provider_testIsStoragePath() {
96 array( 'mwstore://', true ),
97 array( 'mwstore://backend', true ),
98 array( 'mwstore://backend/container', true ),
99 array( 'mwstore://backend/container/', true ),
100 array( 'mwstore://backend/container/path', true ),
101 array( 'mwstore://backend//container/', true ),
102 array( 'mwstore://backend//container//', true ),
103 array( 'mwstore://backend//container//path', true ),
104 array( 'mwstore:///', true ),
105 array( 'mwstore:/', false ),
106 array( 'mwstore:', false ),
111 * @dataProvider provider_testSplitStoragePath
113 public function testSplitStoragePath( $path, $res ) {
114 $this->assertEquals( $res, FileBackend
::splitStoragePath( $path ),
115 "FileBackend::splitStoragePath on path '$path'" );
118 function provider_testSplitStoragePath() {
120 array( 'mwstore://backend/container', array( 'backend', 'container', '' ) ),
121 array( 'mwstore://backend/container/', array( 'backend', 'container', '' ) ),
122 array( 'mwstore://backend/container/path', array( 'backend', 'container', 'path' ) ),
123 array( 'mwstore://backend/container//path', array( 'backend', 'container', '/path' ) ),
124 array( 'mwstore://backend//container/path', array( null, null, null ) ),
125 array( 'mwstore://backend//container//path', array( null, null, null ) ),
126 array( 'mwstore://', array( null, null, null ) ),
127 array( 'mwstore://backend', array( null, null, null ) ),
128 array( 'mwstore:///', array( null, null, null ) ),
129 array( 'mwstore:/', array( null, null, null ) ),
130 array( 'mwstore:', array( null, null, null ) )
135 * @dataProvider provider_normalizeStoragePath
137 public function testNormalizeStoragePath( $path, $res ) {
138 $this->assertEquals( $res, FileBackend
::normalizeStoragePath( $path ),
139 "FileBackend::normalizeStoragePath on path '$path'" );
142 function provider_normalizeStoragePath() {
144 array( 'mwstore://backend/container', 'mwstore://backend/container' ),
145 array( 'mwstore://backend/container/', 'mwstore://backend/container' ),
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', 'mwstore://backend/container/path' ),
149 array( 'mwstore://backend/container///path//to///obj', 'mwstore://backend/container/path/to/obj',
150 array( 'mwstore://', null ),
151 array( 'mwstore://backend', null ),
152 array( 'mwstore://backend//container/path', null ),
153 array( 'mwstore://backend//container//path', null ),
154 array( 'mwstore:///', null ),
155 array( 'mwstore:/', null ),
156 array( 'mwstore:', null ), )
161 * @dataProvider provider_testParentStoragePath
163 public function testParentStoragePath( $path, $res ) {
164 $this->assertEquals( $res, FileBackend
::parentStoragePath( $path ),
165 "FileBackend::parentStoragePath on path '$path'" );
168 function provider_testParentStoragePath() {
170 array( 'mwstore://backend/container/path/to/obj', 'mwstore://backend/container/path/to' ),
171 array( 'mwstore://backend/container/path/to', 'mwstore://backend/container/path' ),
172 array( 'mwstore://backend/container/path', 'mwstore://backend/container' ),
173 array( 'mwstore://backend/container', null ),
174 array( 'mwstore://backend/container/path/to/obj/', 'mwstore://backend/container/path/to' ),
175 array( 'mwstore://backend/container/path/to/', 'mwstore://backend/container/path' ),
176 array( 'mwstore://backend/container/path/', 'mwstore://backend/container' ),
177 array( 'mwstore://backend/container/', null ),
182 * @dataProvider provider_testExtensionFromPath
184 public function testExtensionFromPath( $path, $res ) {
185 $this->assertEquals( $res, FileBackend
::extensionFromPath( $path ),
186 "FileBackend::extensionFromPath on path '$path'" );
189 function provider_testExtensionFromPath() {
191 array( 'mwstore://backend/container/path.txt', 'txt' ),
192 array( 'mwstore://backend/container/path.svg.png', 'png' ),
193 array( 'mwstore://backend/container/path', '' ),
194 array( 'mwstore://backend/container/path.', '' ),
199 * @dataProvider provider_testStore
201 public function testStore( $op ) {
202 $this->filesToPrune
[] = $op['src'];
204 $this->backend
= $this->singleBackend
;
205 $this->tearDownFiles();
206 $this->doTestStore( $op );
207 $this->tearDownFiles();
209 $this->backend
= $this->multiBackend
;
210 $this->tearDownFiles();
211 $this->doTestStore( $op );
212 $this->filesToPrune
[] = $op['src']; # avoid file leaking
213 $this->tearDownFiles();
216 private function doTestStore( $op ) {
217 $backendName = $this->backendClass();
219 $source = $op['src'];
221 $this->prepare( array( 'dir' => dirname( $dest ) ) );
223 file_put_contents( $source, "Unit test file" );
225 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
226 $this->backend
->store( $op );
229 $status = $this->backend
->doOperation( $op );
231 $this->assertGoodStatus( $status,
232 "Store from $source to $dest succeeded without warnings ($backendName)." );
233 $this->assertEquals( true, $status->isOK(),
234 "Store from $source to $dest succeeded ($backendName)." );
235 $this->assertEquals( array( 0 => true ), $status->success
,
236 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
237 $this->assertEquals( true, file_exists( $source ),
238 "Source file $source still exists ($backendName)." );
239 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
240 "Destination file $dest exists ($backendName)." );
242 $this->assertEquals( filesize( $source ),
243 $this->backend
->getFileSize( array( 'src' => $dest ) ),
244 "Destination file $dest has correct size ($backendName)." );
246 $props1 = FSFile
::getPropsFromPath( $source );
247 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
248 $this->assertEquals( $props1, $props2,
249 "Source and destination have the same props ($backendName)." );
251 $this->assertBackendPathsConsistent( array( $dest ) );
254 public function provider_testStore() {
257 $tmpName = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
258 $toPath = $this->baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
259 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
267 $op2['overwrite'] = true;
275 $op2['overwriteSame'] = true;
286 * @dataProvider provider_testCopy
288 public function testCopy( $op ) {
289 $this->backend
= $this->singleBackend
;
290 $this->tearDownFiles();
291 $this->doTestCopy( $op );
292 $this->tearDownFiles();
294 $this->backend
= $this->multiBackend
;
295 $this->tearDownFiles();
296 $this->doTestCopy( $op );
297 $this->tearDownFiles();
300 private function doTestCopy( $op ) {
301 $backendName = $this->backendClass();
303 $source = $op['src'];
305 $this->prepare( array( 'dir' => dirname( $source ) ) );
306 $this->prepare( array( 'dir' => dirname( $dest ) ) );
308 $status = $this->backend
->doOperation(
309 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
310 $this->assertGoodStatus( $status,
311 "Creation of file at $source succeeded ($backendName)." );
313 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
314 $this->backend
->copy( $op );
317 $status = $this->backend
->doOperation( $op );
319 $this->assertGoodStatus( $status,
320 "Copy from $source to $dest succeeded without warnings ($backendName)." );
321 $this->assertEquals( true, $status->isOK(),
322 "Copy from $source to $dest succeeded ($backendName)." );
323 $this->assertEquals( array( 0 => true ), $status->success
,
324 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
325 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $source ) ),
326 "Source file $source still exists ($backendName)." );
327 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
328 "Destination file $dest exists after copy ($backendName)." );
331 $this->backend
->getFileSize( array( 'src' => $source ) ),
332 $this->backend
->getFileSize( array( 'src' => $dest ) ),
333 "Destination file $dest has correct size ($backendName)." );
335 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
336 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
337 $this->assertEquals( $props1, $props2,
338 "Source and destination have the same props ($backendName)." );
340 $this->assertBackendPathsConsistent( array( $source, $dest ) );
343 public function provider_testCopy() {
346 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
347 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
349 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
357 $op2['overwrite'] = true;
365 $op2['overwriteSame'] = true;
376 * @dataProvider provider_testMove
378 public function testMove( $op ) {
379 $this->backend
= $this->singleBackend
;
380 $this->tearDownFiles();
381 $this->doTestMove( $op );
382 $this->tearDownFiles();
384 $this->backend
= $this->multiBackend
;
385 $this->tearDownFiles();
386 $this->doTestMove( $op );
387 $this->tearDownFiles();
390 private function doTestMove( $op ) {
391 $backendName = $this->backendClass();
393 $source = $op['src'];
395 $this->prepare( array( 'dir' => dirname( $source ) ) );
396 $this->prepare( array( 'dir' => dirname( $dest ) ) );
398 $status = $this->backend
->doOperation(
399 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
400 $this->assertGoodStatus( $status,
401 "Creation of file at $source succeeded ($backendName)." );
403 if ( isset( $op['overwrite'] ) ||
isset( $op['overwriteSame'] ) ) {
404 $this->backend
->copy( $op );
407 $status = $this->backend
->doOperation( $op );
408 $this->assertGoodStatus( $status,
409 "Move from $source to $dest succeeded without warnings ($backendName)." );
410 $this->assertEquals( true, $status->isOK(),
411 "Move from $source to $dest succeeded ($backendName)." );
412 $this->assertEquals( array( 0 => true ), $status->success
,
413 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
414 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
415 "Source file $source does not still exists ($backendName)." );
416 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
417 "Destination file $dest exists after move ($backendName)." );
419 $this->assertNotEquals(
420 $this->backend
->getFileSize( array( 'src' => $source ) ),
421 $this->backend
->getFileSize( array( 'src' => $dest ) ),
422 "Destination file $dest has correct size ($backendName)." );
424 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
425 $props2 = $this->backend
->getFileProps( array( 'src' => $dest ) );
426 $this->assertEquals( false, $props1['fileExists'],
427 "Source file does not exist accourding to props ($backendName)." );
428 $this->assertEquals( true, $props2['fileExists'],
429 "Destination file exists accourding to props ($backendName)." );
431 $this->assertBackendPathsConsistent( array( $source, $dest ) );
434 public function provider_testMove() {
437 $source = $this->baseStorePath() . '/unittest-cont1/e/file.txt';
438 $dest = $this->baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
440 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
448 $op2['overwrite'] = true;
456 $op2['overwriteSame'] = true;
467 * @dataProvider provider_testDelete
469 public function testDelete( $op, $withSource, $okStatus ) {
470 $this->backend
= $this->singleBackend
;
471 $this->tearDownFiles();
472 $this->doTestDelete( $op, $withSource, $okStatus );
473 $this->tearDownFiles();
475 $this->backend
= $this->multiBackend
;
476 $this->tearDownFiles();
477 $this->doTestDelete( $op, $withSource, $okStatus );
478 $this->tearDownFiles();
481 private function doTestDelete( $op, $withSource, $okStatus ) {
482 $backendName = $this->backendClass();
484 $source = $op['src'];
485 $this->prepare( array( 'dir' => dirname( $source ) ) );
488 $status = $this->backend
->doOperation(
489 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
490 $this->assertGoodStatus( $status,
491 "Creation of file at $source succeeded ($backendName)." );
494 $status = $this->backend
->doOperation( $op );
496 $this->assertGoodStatus( $status,
497 "Deletion of file at $source succeeded without warnings ($backendName)." );
498 $this->assertEquals( true, $status->isOK(),
499 "Deletion of file at $source succeeded ($backendName)." );
500 $this->assertEquals( array( 0 => true ), $status->success
,
501 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
503 $this->assertEquals( false, $status->isOK(),
504 "Deletion of file at $source failed ($backendName)." );
507 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $source ) ),
508 "Source file $source does not exist after move ($backendName)." );
511 $this->backend
->getFileSize( array( 'src' => $source ) ),
512 "Source file $source has correct size (false) ($backendName)." );
514 $props1 = $this->backend
->getFileProps( array( 'src' => $source ) );
515 $this->assertFalse( $props1['fileExists'],
516 "Source file $source does not exist according to props ($backendName)." );
518 $this->assertBackendPathsConsistent( array( $source ) );
521 public function provider_testDelete() {
524 $source = $this->baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
526 $op = array( 'op' => 'delete', 'src' => $source );
535 false, // without source
539 $op['ignoreMissingSource'] = true;
542 false, // without source
550 * @dataProvider provider_testCreate
552 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
553 $this->backend
= $this->singleBackend
;
554 $this->tearDownFiles();
555 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
556 $this->tearDownFiles();
558 $this->backend
= $this->multiBackend
;
559 $this->tearDownFiles();
560 $this->doTestCreate( $op, $alreadyExists, $okStatus, $newSize );
561 $this->tearDownFiles();
564 private function doTestCreate( $op, $alreadyExists, $okStatus, $newSize ) {
565 $backendName = $this->backendClass();
568 $this->prepare( array( 'dir' => dirname( $dest ) ) );
570 $oldText = 'blah...blah...waahwaah';
571 if ( $alreadyExists ) {
572 $status = $this->backend
->doOperation(
573 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
574 $this->assertGoodStatus( $status,
575 "Creation of file at $dest succeeded ($backendName)." );
578 $status = $this->backend
->doOperation( $op );
580 $this->assertGoodStatus( $status,
581 "Creation of file at $dest succeeded without warnings ($backendName)." );
582 $this->assertEquals( true, $status->isOK(),
583 "Creation of file at $dest succeeded ($backendName)." );
584 $this->assertEquals( array( 0 => true ), $status->success
,
585 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
587 $this->assertEquals( false, $status->isOK(),
588 "Creation of file at $dest failed ($backendName)." );
591 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $dest ) ),
592 "Destination file $dest exists after creation ($backendName)." );
594 $props1 = $this->backend
->getFileProps( array( 'src' => $dest ) );
595 $this->assertEquals( true, $props1['fileExists'],
596 "Destination file $dest exists according to props ($backendName)." );
597 if ( $okStatus ) { // file content is what we saved
598 $this->assertEquals( $newSize, $props1['size'],
599 "Destination file $dest has expected size according to props ($backendName)." );
600 $this->assertEquals( $newSize,
601 $this->backend
->getFileSize( array( 'src' => $dest ) ),
602 "Destination file $dest has correct size ($backendName)." );
603 } else { // file content is some other previous text
604 $this->assertEquals( strlen( $oldText ), $props1['size'],
605 "Destination file $dest has original size according to props ($backendName)." );
606 $this->assertEquals( strlen( $oldText ),
607 $this->backend
->getFileSize( array( 'src' => $dest ) ),
608 "Destination file $dest has original size according to props ($backendName)." );
611 $this->assertBackendPathsConsistent( array( $dest ) );
615 * @dataProvider provider_testCreate
617 public function provider_testCreate() {
620 $dest = $this->baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
622 $op = array( 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest );
625 false, // no dest already exists
627 strlen( $op['content'] )
631 $op2['content'] = "\n";
634 false, // no dest already exists
636 strlen( $op2['content'] )
640 $op2['content'] = "fsf\n waf 3kt";
643 true, // dest already exists
645 strlen( $op2['content'] )
649 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
650 $op2['overwrite'] = true;
653 true, // dest already exists
655 strlen( $op2['content'] )
659 $op2['content'] = "39qjmg3-qg";
660 $op2['overwriteSame'] = true;
663 true, // dest already exists
665 strlen( $op2['content'] )
671 public function testDoQuickOperations() {
672 $this->backend
= $this->singleBackend
;
673 $this->doTestDoQuickOperations();
674 $this->tearDownFiles();
676 $this->backend
= $this->multiBackend
;
677 $this->doTestDoQuickOperations();
678 $this->tearDownFiles();
681 private function doTestDoQuickOperations() {
682 $backendName = $this->backendClass();
684 $base = $this->baseStorePath();
686 "$base/unittest-cont1/e/fileA.a",
687 "$base/unittest-cont1/e/fileB.a",
688 "$base/unittest-cont1/e/fileC.a"
692 foreach ( $files as $path ) {
693 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
694 $this->assertGoodStatus( $status,
695 "Preparing $path succeeded without warnings ($backendName)." );
696 $ops[] = array( 'op' => 'create', 'dst' => $path, 'content' => mt_rand(0,50000) );
697 $purgeOps[] = array( 'op' => 'delete', 'src' => $path );
699 $purgeOps[] = array( 'op' => 'null' );
700 $status = $this->backend
->doQuickOperations( $ops );
701 $this->assertGoodStatus( $status,
702 "Creation of source files succeeded ($backendName)." );
704 foreach ( $files as $file ) {
705 $this->assertTrue( $this->backend
->fileExists( array( 'src' => $file ) ),
706 "File $file exists." );
709 $status = $this->backend
->doQuickOperations( $purgeOps );
710 $this->assertGoodStatus( $status,
711 "Quick deletion of source files succeeded ($backendName)." );
713 foreach ( $files as $file ) {
714 $this->assertFalse( $this->backend
->fileExists( array( 'src' => $file ) ),
715 "File $file purged." );
720 * @dataProvider provider_testConcatenate
722 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
723 $this->filesToPrune
[] = $op['dst'];
725 $this->backend
= $this->singleBackend
;
726 $this->tearDownFiles();
727 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
728 $this->tearDownFiles();
730 $this->backend
= $this->multiBackend
;
731 $this->tearDownFiles();
732 $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus );
733 $this->filesToPrune
[] = $op['dst']; # avoid file leaking
734 $this->tearDownFiles();
737 private function doTestConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
738 $backendName = $this->backendClass();
743 foreach ( $srcs as $i => $source ) {
744 $this->prepare( array( 'dir' => dirname( $source ) ) );
746 'op' => 'create', // operation
747 'dst' => $source, // source
748 'content' => $srcsContent[$i]
750 $expContent .= $srcsContent[$i];
752 $status = $this->backend
->doOperations( $ops );
754 $this->assertGoodStatus( $status,
755 "Creation of source files succeeded ($backendName)." );
757 $dest = $params['dst'];
758 if ( $alreadyExists ) {
759 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
760 $this->assertEquals( true, $ok,
761 "Creation of file at $dest succeeded ($backendName)." );
763 $ok = file_put_contents( $dest, '' ) !== false;
764 $this->assertEquals( true, $ok,
765 "Creation of 0-byte file at $dest succeeded ($backendName)." );
768 // Combine the files into one
769 $status = $this->backend
->concatenate( $params );
771 $this->assertGoodStatus( $status,
772 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
773 $this->assertEquals( true, $status->isOK(),
774 "Creation of concat file at $dest succeeded ($backendName)." );
776 $this->assertEquals( false, $status->isOK(),
777 "Creation of concat file at $dest failed ($backendName)." );
781 $this->assertEquals( true, is_file( $dest ),
782 "Dest concat file $dest exists after creation ($backendName)." );
784 $this->assertEquals( true, is_file( $dest ),
785 "Dest concat file $dest exists after failed creation ($backendName)." );
788 $contents = file_get_contents( $dest );
789 $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." );
792 $this->assertEquals( $expContent, $contents,
793 "Concat file at $dest has correct contents ($backendName)." );
795 $this->assertNotEquals( $expContent, $contents,
796 "Concat file at $dest has correct contents ($backendName)." );
800 function provider_testConcatenate() {
803 $rand = mt_rand( 0, 2000000000 ) . time();
804 $dest = wfTempDir() . "/randomfile!$rand.txt";
806 $this->baseStorePath() . '/unittest-cont1/e/file1.txt',
807 $this->baseStorePath() . '/unittest-cont1/e/file2.txt',
808 $this->baseStorePath() . '/unittest-cont1/e/file3.txt',
809 $this->baseStorePath() . '/unittest-cont1/e/file4.txt',
810 $this->baseStorePath() . '/unittest-cont1/e/file5.txt',
811 $this->baseStorePath() . '/unittest-cont1/e/file6.txt',
812 $this->baseStorePath() . '/unittest-cont1/e/file7.txt',
813 $this->baseStorePath() . '/unittest-cont1/e/file8.txt',
814 $this->baseStorePath() . '/unittest-cont1/e/file9.txt',
815 $this->baseStorePath() . '/unittest-cont1/e/file10.txt'
829 $params = array( 'srcs' => $srcs, 'dst' => $dest );
832 $params, // operation
834 $content, // content for each source
835 false, // no dest already exists
840 $params, // operation
842 $content, // content for each source
843 true, // dest already exists
851 * @dataProvider provider_testGetFileStat
853 public function testGetFileStat( $path, $content, $alreadyExists ) {
854 $this->backend
= $this->singleBackend
;
855 $this->tearDownFiles();
856 $this->doTestGetFileStat( $path, $content, $alreadyExists );
857 $this->tearDownFiles();
859 $this->backend
= $this->multiBackend
;
860 $this->tearDownFiles();
861 $this->doTestGetFileStat( $path, $content, $alreadyExists );
862 $this->tearDownFiles();
865 private function doTestGetFileStat( $path, $content, $alreadyExists ) {
866 $backendName = $this->backendClass();
868 if ( $alreadyExists ) {
869 $this->prepare( array( 'dir' => dirname( $path ) ) );
870 $status = $this->create( array( 'dst' => $path, 'content' => $content ) );
871 $this->assertGoodStatus( $status,
872 "Creation of file at $path succeeded ($backendName)." );
874 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
875 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
876 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
878 $this->assertEquals( strlen( $content ), $size,
879 "Correct file size of '$path'" );
880 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
881 "Correct file timestamp of '$path'" );
883 $size = $stat['size'];
884 $time = $stat['mtime'];
885 $this->assertEquals( strlen( $content ), $size,
886 "Correct file size of '$path'" );
887 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX
, $time ) ) < 10,
888 "Correct file timestamp of '$path'" );
890 $this->backend
->clearCache( array( $path ) );
892 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
894 $this->assertEquals( strlen( $content ), $size,
895 "Correct file size of '$path'" );
897 $this->backend
->preloadCache( array( $path ) );
899 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
901 $this->assertEquals( strlen( $content ), $size,
902 "Correct file size of '$path'" );
904 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
905 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
906 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
908 $this->assertFalse( $size, "Correct file size of '$path'" );
909 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
910 $this->assertFalse( $stat, "Correct file stat of '$path'" );
914 function provider_testGetFileStat() {
917 $base = $this->baseStorePath();
918 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
919 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
920 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
926 * @dataProvider provider_testGetFileContents
928 public function testGetFileContents( $source, $content ) {
929 $this->backend
= $this->singleBackend
;
930 $this->tearDownFiles();
931 $this->doTestGetFileContents( $source, $content );
932 $this->tearDownFiles();
934 $this->backend
= $this->multiBackend
;
935 $this->tearDownFiles();
936 $this->doTestGetFileContents( $source, $content );
937 $this->tearDownFiles();
940 private function doTestGetFileContents( $source, $content ) {
941 $backendName = $this->backendClass();
943 $srcs = (array)$source;
944 $content = (array)$content;
945 foreach ( $srcs as $i => $src ) {
946 $this->prepare( array( 'dir' => dirname( $src ) ) );
947 $status = $this->backend
->doOperation(
948 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
949 $this->assertGoodStatus( $status,
950 "Creation of file at $src succeeded ($backendName)." );
953 if ( is_array( $source ) ) {
954 $contents = $this->backend
->getFileContentsMulti( array( 'srcs' => $source ) );
955 foreach ( $contents as $path => $data ) {
956 $this->assertNotEquals( false, $data, "Contents of $path exists ($backendName)." );
957 $this->assertEquals( current( $content ), $data, "Contents of $path is correct ($backendName)." );
960 $this->assertEquals( $source, array_keys( $contents ), "Contents in right order ($backendName)." );
961 $this->assertEquals( count( $source ), count( $contents ), "Contents array size correct ($backendName)." );
963 $data = $this->backend
->getFileContents( array( 'src' => $source ) );
964 $this->assertNotEquals( false, $data, "Contents of $source exists ($backendName)." );
965 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
969 function provider_testGetFileContents() {
972 $base = $this->baseStorePath();
973 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
974 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
976 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
977 "$base/unittest-cont1/e/a/z.txt" ),
978 array( "contents xx", "contents xy", "contents xz" )
985 * @dataProvider provider_testGetLocalCopy
987 public function testGetLocalCopy( $source, $content ) {
988 $this->backend
= $this->singleBackend
;
989 $this->tearDownFiles();
990 $this->doTestGetLocalCopy( $source, $content );
991 $this->tearDownFiles();
993 $this->backend
= $this->multiBackend
;
994 $this->tearDownFiles();
995 $this->doTestGetLocalCopy( $source, $content );
996 $this->tearDownFiles();
999 private function doTestGetLocalCopy( $source, $content ) {
1000 $backendName = $this->backendClass();
1002 $srcs = (array)$source;
1003 $content = (array)$content;
1004 foreach ( $srcs as $i => $src ) {
1005 $this->prepare( array( 'dir' => dirname( $src ) ) );
1006 $status = $this->backend
->doOperation(
1007 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1008 $this->assertGoodStatus( $status,
1009 "Creation of file at $src succeeded ($backendName)." );
1012 if ( is_array( $source ) ) {
1013 $tmpFiles = $this->backend
->getLocalCopyMulti( array( 'srcs' => $source ) );
1014 foreach ( $tmpFiles as $path => $tmpFile ) {
1015 $this->assertNotNull( $tmpFile,
1016 "Creation of local copy of $path succeeded ($backendName)." );
1017 $contents = file_get_contents( $tmpFile->getPath() );
1018 $this->assertNotEquals( false, $contents, "Local copy of $path exists ($backendName)." );
1019 $this->assertEquals( current( $content ), $contents, "Local copy of $path is correct ($backendName)." );
1022 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local copies in right order ($backendName)." );
1023 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local copies array size correct ($backendName)." );
1025 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
1026 $this->assertNotNull( $tmpFile,
1027 "Creation of local copy of $source succeeded ($backendName)." );
1028 $contents = file_get_contents( $tmpFile->getPath() );
1029 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1030 $this->assertEquals( $content[0], $contents, "Local copy of $source is correct ($backendName)." );
1034 function provider_testGetLocalCopy() {
1037 $base = $this->baseStorePath();
1038 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1039 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1040 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1042 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1043 "$base/unittest-cont1/e/a/z.txt" ),
1044 array( "contents xx", "contents xy", "contents xz" )
1051 * @dataProvider provider_testGetLocalReference
1053 public function testGetLocalReference( $source, $content ) {
1054 $this->backend
= $this->singleBackend
;
1055 $this->tearDownFiles();
1056 $this->doTestGetLocalReference( $source, $content );
1057 $this->tearDownFiles();
1059 $this->backend
= $this->multiBackend
;
1060 $this->tearDownFiles();
1061 $this->doTestGetLocalReference( $source, $content );
1062 $this->tearDownFiles();
1065 private function doTestGetLocalReference( $source, $content ) {
1066 $backendName = $this->backendClass();
1068 $srcs = (array)$source;
1069 $content = (array)$content;
1070 foreach ( $srcs as $i => $src ) {
1071 $this->prepare( array( 'dir' => dirname( $src ) ) );
1072 $status = $this->backend
->doOperation(
1073 array( 'op' => 'create', 'content' => $content[$i], 'dst' => $src ) );
1074 $this->assertGoodStatus( $status,
1075 "Creation of file at $src succeeded ($backendName)." );
1078 if ( is_array( $source ) ) {
1079 $tmpFiles = $this->backend
->getLocalReferenceMulti( array( 'srcs' => $source ) );
1080 foreach ( $tmpFiles as $path => $tmpFile ) {
1081 $this->assertNotNull( $tmpFile,
1082 "Creation of local copy of $path succeeded ($backendName)." );
1083 $contents = file_get_contents( $tmpFile->getPath() );
1084 $this->assertNotEquals( false, $contents, "Local ref of $path exists ($backendName)." );
1085 $this->assertEquals( current( $content ), $contents, "Local ref of $path is correct ($backendName)." );
1088 $this->assertEquals( $source, array_keys( $tmpFiles ), "Local refs in right order ($backendName)." );
1089 $this->assertEquals( count( $source ), count( $tmpFiles ), "Local refs array size correct ($backendName)." );
1091 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1092 $this->assertNotNull( $tmpFile,
1093 "Creation of local copy of $source succeeded ($backendName)." );
1094 $contents = file_get_contents( $tmpFile->getPath() );
1095 $this->assertNotEquals( false, $contents, "Local ref of $source exists ($backendName)." );
1096 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1100 function provider_testGetLocalReference() {
1103 $base = $this->baseStorePath();
1104 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1105 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1106 $cases[] = array( "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" );
1108 array( "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1109 "$base/unittest-cont1/e/a/z.txt" ),
1110 array( "contents xx", "contents xy", "contents xz" )
1117 * @dataProvider provider_testPrepareAndClean
1119 public function testPrepareAndClean( $path, $isOK ) {
1120 $this->backend
= $this->singleBackend
;
1121 $this->doTestPrepareAndClean( $path, $isOK );
1122 $this->tearDownFiles();
1124 $this->backend
= $this->multiBackend
;
1125 $this->doTestPrepareAndClean( $path, $isOK );
1126 $this->tearDownFiles();
1129 function provider_testPrepareAndClean() {
1130 $base = $this->baseStorePath();
1132 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1133 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1134 # Specific to FS backend with no basePath field set
1135 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1139 private function doTestPrepareAndClean( $path, $isOK ) {
1140 $backendName = $this->backendClass();
1142 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1144 $this->assertGoodStatus( $status,
1145 "Preparing dir $path succeeded without warnings ($backendName)." );
1146 $this->assertEquals( true, $status->isOK(),
1147 "Preparing dir $path succeeded ($backendName)." );
1149 $this->assertEquals( false, $status->isOK(),
1150 "Preparing dir $path failed ($backendName)." );
1153 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1155 $this->assertGoodStatus( $status,
1156 "Cleaning dir $path succeeded without warnings ($backendName)." );
1157 $this->assertEquals( true, $status->isOK(),
1158 "Cleaning dir $path succeeded ($backendName)." );
1160 $this->assertEquals( false, $status->isOK(),
1161 "Cleaning dir $path failed ($backendName)." );
1165 public function testRecursiveClean() {
1166 $this->backend
= $this->singleBackend
;
1167 $this->doTestRecursiveClean();
1168 $this->tearDownFiles();
1170 $this->backend
= $this->multiBackend
;
1171 $this->doTestRecursiveClean();
1172 $this->tearDownFiles();
1175 private function doTestRecursiveClean() {
1176 $backendName = $this->backendClass();
1178 $base = $this->baseStorePath();
1180 "$base/unittest-cont1/e/a",
1181 "$base/unittest-cont1/e/a/b",
1182 "$base/unittest-cont1/e/a/b/c",
1183 "$base/unittest-cont1/e/a/b/c/d0",
1184 "$base/unittest-cont1/e/a/b/c/d1",
1185 "$base/unittest-cont1/e/a/b/c/d2",
1186 "$base/unittest-cont1/e/a/b/c/d0/1",
1187 "$base/unittest-cont1/e/a/b/c/d0/2",
1188 "$base/unittest-cont1/e/a/b/c/d1/3",
1189 "$base/unittest-cont1/e/a/b/c/d1/4",
1190 "$base/unittest-cont1/e/a/b/c/d2/5",
1191 "$base/unittest-cont1/e/a/b/c/d2/6"
1193 foreach ( $dirs as $dir ) {
1194 $status = $this->prepare( array( 'dir' => $dir ) );
1195 $this->assertGoodStatus( $status,
1196 "Preparing dir $dir succeeded without warnings ($backendName)." );
1199 if ( $this->backend
instanceof FSFileBackend
) {
1200 foreach ( $dirs as $dir ) {
1201 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1202 "Dir $dir exists ($backendName)." );
1206 $status = $this->backend
->clean(
1207 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1208 $this->assertGoodStatus( $status,
1209 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1211 foreach ( $dirs as $dir ) {
1212 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1213 "Dir $dir no longer exists ($backendName)." );
1217 // @TODO: testSecure
1219 public function testDoOperations() {
1220 $this->backend
= $this->singleBackend
;
1221 $this->tearDownFiles();
1222 $this->doTestDoOperations();
1223 $this->tearDownFiles();
1225 $this->backend
= $this->multiBackend
;
1226 $this->tearDownFiles();
1227 $this->doTestDoOperations();
1228 $this->tearDownFiles();
1231 private function doTestDoOperations() {
1232 $base = $this->baseStorePath();
1234 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1235 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1236 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1237 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1238 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1239 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1240 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1242 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1243 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1244 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1245 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1246 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1247 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1248 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1250 $status = $this->backend
->doOperations( array(
1251 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1252 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1253 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1254 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1255 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1256 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1257 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1258 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1259 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1260 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1261 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1262 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1263 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1264 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1265 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1266 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1267 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1269 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1271 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1273 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1275 array( 'op' => 'null' ),
1279 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1280 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1281 $this->assertEquals( 13, count( $status->success
),
1282 "Operation batch has correct success array" );
1284 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1285 "File does not exist at $fileA" );
1286 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1287 "File does not exist at $fileB" );
1288 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1289 "File does not exist at $fileD" );
1291 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1292 "File exists at $fileC" );
1293 $this->assertEquals( $fileBContents,
1294 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1295 "Correct file contents of $fileC" );
1296 $this->assertEquals( strlen( $fileBContents ),
1297 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1298 "Correct file size of $fileC" );
1299 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1300 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1301 "Correct file SHA-1 of $fileC" );
1304 public function testDoOperationsPipeline() {
1305 $this->backend
= $this->singleBackend
;
1306 $this->tearDownFiles();
1307 $this->doTestDoOperationsPipeline();
1308 $this->tearDownFiles();
1310 $this->backend
= $this->multiBackend
;
1311 $this->tearDownFiles();
1312 $this->doTestDoOperationsPipeline();
1313 $this->tearDownFiles();
1316 // concurrency orientated
1317 private function doTestDoOperationsPipeline() {
1318 $base = $this->baseStorePath();
1320 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1321 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1322 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1324 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1325 file_put_contents( $tmpNameA, $fileAContents );
1326 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1327 file_put_contents( $tmpNameB, $fileBContents );
1328 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1329 file_put_contents( $tmpNameC, $fileCContents );
1331 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1332 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1333 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1335 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1336 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1337 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1338 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1340 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1341 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1342 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1343 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1344 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1346 $status = $this->backend
->doOperations( array(
1347 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1348 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1349 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1350 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1351 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1352 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1353 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1354 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1355 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1356 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1357 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1358 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1359 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1360 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1361 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1362 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1363 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1364 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1365 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1366 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1368 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1370 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1372 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1374 array( 'op' => 'null' ),
1378 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1379 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1380 $this->assertEquals( 16, count( $status->success
),
1381 "Operation batch has correct success array" );
1383 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1384 "File does not exist at $fileA" );
1385 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1386 "File does not exist at $fileB" );
1387 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1388 "File does not exist at $fileD" );
1390 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1391 "File exists at $fileC" );
1392 $this->assertEquals( $fileBContents,
1393 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1394 "Correct file contents of $fileC" );
1395 $this->assertEquals( strlen( $fileBContents ),
1396 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1397 "Correct file size of $fileC" );
1398 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1399 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1400 "Correct file SHA-1 of $fileC" );
1403 public function testDoOperationsFailing() {
1404 $this->backend
= $this->singleBackend
;
1405 $this->tearDownFiles();
1406 $this->doTestDoOperationsFailing();
1407 $this->tearDownFiles();
1409 $this->backend
= $this->multiBackend
;
1410 $this->tearDownFiles();
1411 $this->doTestDoOperationsFailing();
1412 $this->tearDownFiles();
1415 private function doTestDoOperationsFailing() {
1416 $base = $this->baseStorePath();
1418 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1419 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1420 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1421 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1422 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1423 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1424 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1426 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1427 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1428 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1429 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1430 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1431 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1433 $status = $this->backend
->doOperations( array(
1434 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1435 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1436 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1437 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1438 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1439 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1440 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1441 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1442 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1443 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1444 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1445 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1446 array( 'op' => 'delete', 'src' => $fileD ),
1447 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1448 array( 'op' => 'null' ),
1450 ), array( 'force' => 1 ) );
1452 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1453 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1454 $this->assertEquals( 8, count( $status->success
),
1455 "Operation batch has correct success array" );
1457 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1458 "File does not exist at $fileB" );
1459 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1460 "File does not exist at $fileD" );
1462 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1463 "File does not exist at $fileA" );
1464 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1465 "File exists at $fileC" );
1466 $this->assertEquals( $fileBContents,
1467 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1468 "Correct file contents of $fileA" );
1469 $this->assertEquals( strlen( $fileBContents ),
1470 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1471 "Correct file size of $fileA" );
1472 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1473 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1474 "Correct file SHA-1 of $fileA" );
1477 public function testGetFileList() {
1478 $this->backend
= $this->singleBackend
;
1479 $this->tearDownFiles();
1480 $this->doTestGetFileList();
1481 $this->tearDownFiles();
1483 $this->backend
= $this->multiBackend
;
1484 $this->tearDownFiles();
1485 $this->doTestGetFileList();
1486 $this->tearDownFiles();
1489 private function doTestGetFileList() {
1490 $backendName = $this->backendClass();
1491 $base = $this->baseStorePath();
1493 // Should have no errors
1494 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1497 "$base/unittest-cont1/e/test1.txt",
1498 "$base/unittest-cont1/e/test2.txt",
1499 "$base/unittest-cont1/e/test3.txt",
1500 "$base/unittest-cont1/e/subdir1/test1.txt",
1501 "$base/unittest-cont1/e/subdir1/test2.txt",
1502 "$base/unittest-cont1/e/subdir2/test3.txt",
1503 "$base/unittest-cont1/e/subdir2/test4.txt",
1504 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1505 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1506 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1507 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1508 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1509 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1510 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1515 foreach ( $files as $file ) {
1516 $this->prepare( array( 'dir' => dirname( $file ) ) );
1517 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1519 $status = $this->backend
->doQuickOperations( $ops );
1520 $this->assertGoodStatus( $status,
1521 "Creation of files succeeded ($backendName)." );
1522 $this->assertEquals( true, $status->isOK(),
1523 "Creation of files succeeded with OK status ($backendName)." );
1530 "e/subdir1/test1.txt",
1531 "e/subdir1/test2.txt",
1532 "e/subdir2/test3.txt",
1533 "e/subdir2/test4.txt",
1534 "e/subdir2/subdir/test1.txt",
1535 "e/subdir2/subdir/test2.txt",
1536 "e/subdir2/subdir/test3.txt",
1537 "e/subdir2/subdir/test4.txt",
1538 "e/subdir2/subdir/test5.txt",
1539 "e/subdir2/subdir/sub/test0.txt",
1540 "e/subdir2/subdir/sub/120-px-file.txt",
1544 // Actual listing (no trailing slash)
1546 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1547 foreach ( $iter as $file ) {
1552 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1554 // Actual listing (with trailing slash)
1556 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1557 foreach ( $iter as $file ) {
1562 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1572 "sub/120-px-file.txt",
1576 // Actual listing (no trailing slash)
1578 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1579 foreach ( $iter as $file ) {
1584 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1586 // Actual listing (with trailing slash)
1588 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1589 foreach ( $iter as $file ) {
1594 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1596 // Actual listing (using iterator second time)
1598 foreach ( $iter as $file ) {
1603 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1605 // Expected listing (top files only)
1615 // Actual listing (top files only)
1617 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1618 foreach ( $iter as $file ) {
1623 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1625 foreach ( $files as $file ) { // clean up
1626 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1629 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1630 foreach ( $iter as $iter ) {} // no errors
1633 public function testGetDirectoryList() {
1634 $this->backend
= $this->singleBackend
;
1635 $this->tearDownFiles();
1636 $this->doTestGetDirectoryList();
1637 $this->tearDownFiles();
1639 $this->backend
= $this->multiBackend
;
1640 $this->tearDownFiles();
1641 $this->doTestGetDirectoryList();
1642 $this->tearDownFiles();
1645 private function doTestGetDirectoryList() {
1646 $backendName = $this->backendClass();
1648 $base = $this->baseStorePath();
1650 "$base/unittest-cont1/e/test1.txt",
1651 "$base/unittest-cont1/e/test2.txt",
1652 "$base/unittest-cont1/e/test3.txt",
1653 "$base/unittest-cont1/e/subdir1/test1.txt",
1654 "$base/unittest-cont1/e/subdir1/test2.txt",
1655 "$base/unittest-cont1/e/subdir2/test3.txt",
1656 "$base/unittest-cont1/e/subdir2/test4.txt",
1657 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1658 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1659 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1660 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1661 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1662 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1663 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1668 foreach ( $files as $file ) {
1669 $this->prepare( array( 'dir' => dirname( $file ) ) );
1670 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1672 $status = $this->backend
->doQuickOperations( $ops );
1673 $this->assertGoodStatus( $status,
1674 "Creation of files succeeded ($backendName)." );
1675 $this->assertEquals( true, $status->isOK(),
1676 "Creation of files succeeded with OK status ($backendName)." );
1678 $this->assertEquals( true,
1679 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1680 "Directory exists in ($backendName)." );
1681 $this->assertEquals( true,
1682 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1683 "Directory exists in ($backendName)." );
1684 $this->assertEquals( false,
1685 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1686 "Directory does not exists in ($backendName)." );
1694 // Actual listing (no trailing slash)
1696 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1697 foreach ( $iter as $file ) {
1702 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1713 // Actual listing (no trailing slash)
1715 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1716 foreach ( $iter as $file ) {
1721 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1723 // Actual listing (with trailing slash)
1725 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1726 foreach ( $iter as $file ) {
1731 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1739 // Actual listing (no trailing slash)
1741 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1742 foreach ( $iter as $file ) {
1747 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1749 // Actual listing (with trailing slash)
1751 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1752 foreach ( $iter as $file ) {
1757 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1759 // Actual listing (using iterator second time)
1761 foreach ( $iter as $file ) {
1766 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1768 // Expected listing (recursive)
1778 "e/subdir4/subdir/sub",
1782 // Actual listing (recursive)
1784 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1785 foreach ( $iter as $file ) {
1790 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1792 // Expected listing (recursive)
1799 // Actual listing (recursive)
1801 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1802 foreach ( $iter as $file ) {
1807 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1809 // Actual listing (recursive, second time)
1811 foreach ( $iter as $file ) {
1816 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1818 foreach ( $files as $file ) { // clean up
1819 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1822 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1823 foreach ( $iter as $iter ) {} // no errors
1826 public function testLockCalls() {
1827 $this->backend
= $this->singleBackend
;
1828 $this->doTestLockCalls();
1831 private function doTestLockCalls() {
1832 $backendName = $this->backendClass();
1834 for ( $i=0; $i<50; $i++
) {
1840 "subdir1", // duplicate
1841 "subdir1/test1.txt",
1842 "subdir1/test2.txt",
1844 "subdir2", // duplicate
1845 "subdir2/test3.txt",
1846 "subdir2/test4.txt",
1848 "subdir2/subdir/test1.txt",
1849 "subdir2/subdir/test2.txt",
1850 "subdir2/subdir/test3.txt",
1851 "subdir2/subdir/test4.txt",
1852 "subdir2/subdir/test5.txt",
1853 "subdir2/subdir/sub",
1854 "subdir2/subdir/sub/test0.txt",
1855 "subdir2/subdir/sub/120-px-file.txt",
1858 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1859 $this->assertEquals( array(), $status->errors
,
1860 "Locking of files succeeded ($backendName)." );
1861 $this->assertEquals( true, $status->isOK(),
1862 "Locking of files succeeded with OK status ($backendName)." );
1864 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1865 $this->assertEquals( array(), $status->errors
,
1866 "Locking of files succeeded ($backendName)." );
1867 $this->assertEquals( true, $status->isOK(),
1868 "Locking of files succeeded with OK status ($backendName)." );
1870 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1871 $this->assertEquals( array(), $status->errors
,
1872 "Locking of files succeeded ($backendName)." );
1873 $this->assertEquals( true, $status->isOK(),
1874 "Locking of files succeeded with OK status ($backendName)." );
1876 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1877 $this->assertEquals( array(), $status->errors
,
1878 "Locking of files succeeded ($backendName)." );
1879 $this->assertEquals( true, $status->isOK(),
1880 "Locking of files succeeded with OK status ($backendName)." );
1884 // test helper wrapper for backend prepare() function
1885 private function prepare( array $params ) {
1886 return $this->backend
->prepare( $params );
1889 // test helper wrapper for backend prepare() function
1890 private function create( array $params ) {
1891 $params['op'] = 'create';
1892 return $this->backend
->doQuickOperations( array( $params ) );
1895 function tearDownFiles() {
1896 foreach ( $this->filesToPrune
as $file ) {
1899 $containers = array( 'unittest-cont1', 'unittest-cont2' );
1900 foreach ( $containers as $container ) {
1901 $this->deleteFiles( $container );
1903 $this->filesToPrune
= array();
1906 private function deleteFiles( $container ) {
1907 $base = $this->baseStorePath();
1908 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1910 foreach ( $iter as $file ) {
1911 $this->backend
->quickDelete( array( 'src' => "$base/$container/$file" ) );
1914 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1917 function assertBackendPathsConsistent( array $paths ) {
1918 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1919 $status = $this->backend
->consistencyCheck( $paths );
1920 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1924 function assertGoodStatus( $status, $msg ) {
1925 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1928 function tearDown() {