0b36bf1afd9ad0fd383bca9c13f644aa5f9a43c0
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();
1155 private function doTestDoOperations() {
1156 $base = $this->baseStorePath();
1158 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1159 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1160 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1161 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1162 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1163 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1164 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1166 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1167 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1168 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1169 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1170 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1171 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1172 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1174 $status = $this->backend
->doOperations( array(
1175 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1176 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1177 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1178 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1179 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1180 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1181 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1182 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1183 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1184 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1185 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1186 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1187 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1188 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1189 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1190 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1191 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1193 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1195 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1197 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1199 array( 'op' => 'null' ),
1203 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1204 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1205 $this->assertEquals( 13, count( $status->success
),
1206 "Operation batch has correct success array" );
1208 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1209 "File does not exist at $fileA" );
1210 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1211 "File does not exist at $fileB" );
1212 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1213 "File does not exist at $fileD" );
1215 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1216 "File exists at $fileC" );
1217 $this->assertEquals( $fileBContents,
1218 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1219 "Correct file contents of $fileC" );
1220 $this->assertEquals( strlen( $fileBContents ),
1221 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1222 "Correct file size of $fileC" );
1223 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1224 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1225 "Correct file SHA-1 of $fileC" );
1228 public function testDoOperationsPipeline() {
1229 $this->backend
= $this->singleBackend
;
1230 $this->tearDownFiles();
1231 $this->doTestDoOperationsPipeline();
1232 $this->tearDownFiles();
1234 $this->backend
= $this->multiBackend
;
1235 $this->tearDownFiles();
1236 $this->doTestDoOperationsPipeline();
1237 $this->tearDownFiles();
1240 // concurrency orientated
1241 private function doTestDoOperationsPipeline() {
1242 $base = $this->baseStorePath();
1244 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1245 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1246 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1248 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1249 file_put_contents( $tmpNameA, $fileAContents );
1250 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1251 file_put_contents( $tmpNameB, $fileBContents );
1252 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1253 file_put_contents( $tmpNameC, $fileCContents );
1255 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1256 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1257 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1259 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1260 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1261 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1262 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1264 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1265 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1266 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1267 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1268 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1270 $status = $this->backend
->doOperations( array(
1271 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1272 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1273 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1274 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1275 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1276 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1277 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1278 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1279 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1280 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1281 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1282 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1283 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1284 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1285 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1286 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1287 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1288 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1289 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1290 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1292 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1294 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1296 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1298 array( 'op' => 'null' ),
1302 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1303 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1304 $this->assertEquals( 16, count( $status->success
),
1305 "Operation batch has correct success array" );
1307 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1308 "File does not exist at $fileA" );
1309 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1310 "File does not exist at $fileB" );
1311 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1312 "File does not exist at $fileD" );
1314 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1315 "File exists at $fileC" );
1316 $this->assertEquals( $fileBContents,
1317 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1318 "Correct file contents of $fileC" );
1319 $this->assertEquals( strlen( $fileBContents ),
1320 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1321 "Correct file size of $fileC" );
1322 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1323 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1324 "Correct file SHA-1 of $fileC" );
1327 public function testDoOperationsFailing() {
1328 $this->backend
= $this->singleBackend
;
1329 $this->tearDownFiles();
1330 $this->doTestDoOperationsFailing();
1331 $this->tearDownFiles();
1333 $this->backend
= $this->multiBackend
;
1334 $this->tearDownFiles();
1335 $this->doTestDoOperationsFailing();
1336 $this->tearDownFiles();
1339 private function doTestDoOperationsFailing() {
1340 $base = $this->baseStorePath();
1342 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1343 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1344 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1345 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1346 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1347 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1348 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1350 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1351 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1352 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1353 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1354 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1355 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1357 $status = $this->backend
->doOperations( array(
1358 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1359 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1360 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1361 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1362 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1363 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1364 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1365 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1366 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1367 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1368 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1369 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1370 array( 'op' => 'delete', 'src' => $fileD ),
1371 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1372 array( 'op' => 'null' ),
1374 ), array( 'force' => 1 ) );
1376 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1377 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1378 $this->assertEquals( 8, count( $status->success
),
1379 "Operation batch has correct success array" );
1381 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1382 "File does not exist at $fileB" );
1383 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1384 "File does not exist at $fileD" );
1386 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1387 "File does not exist at $fileA" );
1388 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1389 "File exists at $fileC" );
1390 $this->assertEquals( $fileBContents,
1391 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1392 "Correct file contents of $fileA" );
1393 $this->assertEquals( strlen( $fileBContents ),
1394 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1395 "Correct file size of $fileA" );
1396 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1397 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1398 "Correct file SHA-1 of $fileA" );
1401 public function testGetFileList() {
1402 $this->backend
= $this->singleBackend
;
1403 $this->tearDownFiles();
1404 $this->doTestGetFileList();
1405 $this->tearDownFiles();
1407 $this->backend
= $this->multiBackend
;
1408 $this->tearDownFiles();
1409 $this->doTestGetFileList();
1410 $this->tearDownFiles();
1413 private function doTestGetFileList() {
1414 $backendName = $this->backendClass();
1415 $base = $this->baseStorePath();
1417 // Should have no errors
1418 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1421 "$base/unittest-cont1/e/test1.txt",
1422 "$base/unittest-cont1/e/test2.txt",
1423 "$base/unittest-cont1/e/test3.txt",
1424 "$base/unittest-cont1/e/subdir1/test1.txt",
1425 "$base/unittest-cont1/e/subdir1/test2.txt",
1426 "$base/unittest-cont1/e/subdir2/test3.txt",
1427 "$base/unittest-cont1/e/subdir2/test4.txt",
1428 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1429 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1430 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1431 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1432 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1433 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1434 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1439 foreach ( $files as $file ) {
1440 $this->prepare( array( 'dir' => dirname( $file ) ) );
1441 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1443 $status = $this->backend
->doQuickOperations( $ops );
1444 $this->assertGoodStatus( $status,
1445 "Creation of files succeeded ($backendName)." );
1446 $this->assertEquals( true, $status->isOK(),
1447 "Creation of files succeeded with OK status ($backendName)." );
1454 "e/subdir1/test1.txt",
1455 "e/subdir1/test2.txt",
1456 "e/subdir2/test3.txt",
1457 "e/subdir2/test4.txt",
1458 "e/subdir2/subdir/test1.txt",
1459 "e/subdir2/subdir/test2.txt",
1460 "e/subdir2/subdir/test3.txt",
1461 "e/subdir2/subdir/test4.txt",
1462 "e/subdir2/subdir/test5.txt",
1463 "e/subdir2/subdir/sub/test0.txt",
1464 "e/subdir2/subdir/sub/120-px-file.txt",
1468 // Actual listing (no trailing slash)
1470 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1471 foreach ( $iter as $file ) {
1476 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1478 // Actual listing (with trailing slash)
1480 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1481 foreach ( $iter as $file ) {
1486 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1496 "sub/120-px-file.txt",
1500 // Actual listing (no trailing slash)
1502 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1503 foreach ( $iter as $file ) {
1508 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1510 // Actual listing (with trailing slash)
1512 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1513 foreach ( $iter as $file ) {
1518 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1520 // Actual listing (using iterator second time)
1522 foreach ( $iter as $file ) {
1527 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1529 // Expected listing (top files only)
1539 // Actual listing (top files only)
1541 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1542 foreach ( $iter as $file ) {
1547 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1549 foreach ( $files as $file ) { // clean up
1550 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1553 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1554 foreach ( $iter as $iter ) {} // no errors
1557 public function testGetDirectoryList() {
1558 $this->backend
= $this->singleBackend
;
1559 $this->tearDownFiles();
1560 $this->doTestGetDirectoryList();
1561 $this->tearDownFiles();
1563 $this->backend
= $this->multiBackend
;
1564 $this->tearDownFiles();
1565 $this->doTestGetDirectoryList();
1566 $this->tearDownFiles();
1569 private function doTestGetDirectoryList() {
1570 $backendName = $this->backendClass();
1572 $base = $this->baseStorePath();
1574 "$base/unittest-cont1/e/test1.txt",
1575 "$base/unittest-cont1/e/test2.txt",
1576 "$base/unittest-cont1/e/test3.txt",
1577 "$base/unittest-cont1/e/subdir1/test1.txt",
1578 "$base/unittest-cont1/e/subdir1/test2.txt",
1579 "$base/unittest-cont1/e/subdir2/test3.txt",
1580 "$base/unittest-cont1/e/subdir2/test4.txt",
1581 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1582 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1583 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1584 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1585 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1586 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1587 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1592 foreach ( $files as $file ) {
1593 $this->prepare( array( 'dir' => dirname( $file ) ) );
1594 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1596 $status = $this->backend
->doQuickOperations( $ops );
1597 $this->assertGoodStatus( $status,
1598 "Creation of files succeeded ($backendName)." );
1599 $this->assertEquals( true, $status->isOK(),
1600 "Creation of files succeeded with OK status ($backendName)." );
1602 $this->assertEquals( true,
1603 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1604 "Directory exists in ($backendName)." );
1605 $this->assertEquals( true,
1606 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1607 "Directory exists in ($backendName)." );
1608 $this->assertEquals( false,
1609 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1610 "Directory does not exists in ($backendName)." );
1618 // Actual listing (no trailing slash)
1620 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1621 foreach ( $iter as $file ) {
1626 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1637 // Actual listing (no trailing slash)
1639 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1640 foreach ( $iter as $file ) {
1645 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1647 // Actual listing (with trailing slash)
1649 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1650 foreach ( $iter as $file ) {
1655 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1663 // Actual listing (no trailing slash)
1665 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1666 foreach ( $iter as $file ) {
1671 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1673 // Actual listing (with trailing slash)
1675 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1676 foreach ( $iter as $file ) {
1681 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1683 // Actual listing (using iterator second time)
1685 foreach ( $iter as $file ) {
1690 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1692 // Expected listing (recursive)
1702 "e/subdir4/subdir/sub",
1706 // Actual listing (recursive)
1708 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1709 foreach ( $iter as $file ) {
1714 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1716 // Expected listing (recursive)
1723 // Actual listing (recursive)
1725 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1726 foreach ( $iter as $file ) {
1731 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1733 // Actual listing (recursive, second time)
1735 foreach ( $iter as $file ) {
1740 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1742 foreach ( $files as $file ) { // clean up
1743 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1746 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1747 foreach ( $iter as $iter ) {} // no errors
1750 public function testLockCalls() {
1751 $this->backend
= $this->singleBackend
;
1752 $this->doTestLockCalls();
1755 private function doTestLockCalls() {
1756 $backendName = $this->backendClass();
1758 for ( $i=0; $i<50; $i++
) {
1764 "subdir1", // duplicate
1765 "subdir1/test1.txt",
1766 "subdir1/test2.txt",
1768 "subdir2", // duplicate
1769 "subdir2/test3.txt",
1770 "subdir2/test4.txt",
1772 "subdir2/subdir/test1.txt",
1773 "subdir2/subdir/test2.txt",
1774 "subdir2/subdir/test3.txt",
1775 "subdir2/subdir/test4.txt",
1776 "subdir2/subdir/test5.txt",
1777 "subdir2/subdir/sub",
1778 "subdir2/subdir/sub/test0.txt",
1779 "subdir2/subdir/sub/120-px-file.txt",
1782 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1783 $this->assertEquals( array(), $status->errors
,
1784 "Locking of files succeeded ($backendName)." );
1785 $this->assertEquals( true, $status->isOK(),
1786 "Locking of files succeeded with OK status ($backendName)." );
1788 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1789 $this->assertEquals( array(), $status->errors
,
1790 "Locking of files succeeded ($backendName)." );
1791 $this->assertEquals( true, $status->isOK(),
1792 "Locking of files succeeded with OK status ($backendName)." );
1794 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1795 $this->assertEquals( array(), $status->errors
,
1796 "Locking of files succeeded ($backendName)." );
1797 $this->assertEquals( true, $status->isOK(),
1798 "Locking of files succeeded with OK status ($backendName)." );
1800 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1801 $this->assertEquals( array(), $status->errors
,
1802 "Locking of files succeeded ($backendName)." );
1803 $this->assertEquals( true, $status->isOK(),
1804 "Locking of files succeeded with OK status ($backendName)." );
1808 // test helper wrapper for backend prepare() function
1809 private function prepare( array $params ) {
1810 return $this->backend
->prepare( $params );
1813 // test helper wrapper for backend prepare() function
1814 private function create( array $params ) {
1815 $params['op'] = 'create';
1816 return $this->backend
->doQuickOperations( array( $params ) );
1819 function tearDownFiles() {
1820 foreach ( $this->filesToPrune
as $file ) {
1823 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1824 foreach ( $containers as $container ) {
1825 $this->deleteFiles( $container );
1827 $this->filesToPrune
= array();
1830 private function deleteFiles( $container ) {
1831 $base = $this->baseStorePath();
1832 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1834 foreach ( $iter as $file ) {
1835 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1836 array( 'force' => 1, 'nonLocking' => 1 ) );
1839 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1842 function assertBackendPathsConsistent( array $paths ) {
1843 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1844 $status = $this->backend
->consistencyCheck( $paths );
1845 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1849 function assertGoodStatus( $status, $msg ) {
1850 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1853 function tearDown() {