f9f72c4c4ee43e9cc22fd7f236665f8c0882d09e
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 $this->backend
->clearCache( array( $path ) );
891 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
893 $this->assertEquals( strlen( $content ), $size,
894 "Correct file size of '$path'" );
896 $this->backend
->preloadCache( array( $path ) );
898 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
900 $this->assertEquals( strlen( $content ), $size,
901 "Correct file size of '$path'" );
903 $size = $this->backend
->getFileSize( array( 'src' => $path ) );
904 $time = $this->backend
->getFileTimestamp( array( 'src' => $path ) );
905 $stat = $this->backend
->getFileStat( array( 'src' => $path ) );
907 $this->assertFalse( $size, "Correct file size of '$path'" );
908 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
909 $this->assertFalse( $stat, "Correct file stat of '$path'" );
913 function provider_testGetFileStat() {
916 $base = $this->baseStorePath();
917 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true );
918 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "", true );
919 $cases[] = array( "$base/unittest-cont1/e/b/some-diff_file.txt", null, false );
925 * @dataProvider provider_testGetFileContents
927 public function testGetFileContents( $source, $content ) {
928 $this->backend
= $this->singleBackend
;
929 $this->tearDownFiles();
930 $this->doTestGetFileContents( $source, $content );
931 $this->tearDownFiles();
933 $this->backend
= $this->multiBackend
;
934 $this->tearDownFiles();
935 $this->doTestGetFileContents( $source, $content );
936 $this->tearDownFiles();
939 private function doTestGetFileContents( $source, $content ) {
940 $backendName = $this->backendClass();
942 $this->prepare( array( 'dir' => dirname( $source ) ) );
944 $status = $this->backend
->doOperation(
945 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
946 $this->assertGoodStatus( $status,
947 "Creation of file at $source succeeded ($backendName)." );
948 $this->assertEquals( true, $status->isOK(),
949 "Creation of file at $source succeeded with OK status ($backendName)." );
951 $newContents = $this->backend
->getFileContents( array( 'src' => $source, 'latest' => 1 ) );
952 $this->assertNotEquals( false, $newContents,
953 "Read of file at $source succeeded ($backendName)." );
955 $this->assertEquals( $content, $newContents,
956 "Contents read match data at $source ($backendName)." );
959 function provider_testGetFileContents() {
962 $base = $this->baseStorePath();
963 $cases[] = array( "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" );
964 $cases[] = array( "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" );
970 * @dataProvider provider_testGetLocalCopy
972 public function testGetLocalCopy( $source, $content ) {
973 $this->backend
= $this->singleBackend
;
974 $this->tearDownFiles();
975 $this->doTestGetLocalCopy( $source, $content );
976 $this->tearDownFiles();
978 $this->backend
= $this->multiBackend
;
979 $this->tearDownFiles();
980 $this->doTestGetLocalCopy( $source, $content );
981 $this->tearDownFiles();
984 private function doTestGetLocalCopy( $source, $content ) {
985 $backendName = $this->backendClass();
987 $this->prepare( array( 'dir' => dirname( $source ) ) );
989 $status = $this->backend
->doOperation(
990 array( 'op' => 'create', 'content' => $content, 'dst' => $source ) );
991 $this->assertGoodStatus( $status,
992 "Creation of file at $source succeeded ($backendName)." );
994 $tmpFile = $this->backend
->getLocalCopy( array( 'src' => $source ) );
995 $this->assertNotNull( $tmpFile,
996 "Creation of local copy of $source succeeded ($backendName)." );
998 $contents = file_get_contents( $tmpFile->getPath() );
999 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1002 function provider_testGetLocalCopy() {
1005 $base = $this->baseStorePath();
1006 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1007 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1013 * @dataProvider provider_testGetLocalReference
1015 public function testGetLocalReference( $source, $content ) {
1016 $this->backend
= $this->singleBackend
;
1017 $this->tearDownFiles();
1018 $this->doTestGetLocalReference( $source, $content );
1019 $this->tearDownFiles();
1021 $this->backend
= $this->multiBackend
;
1022 $this->tearDownFiles();
1023 $this->doTestGetLocalReference( $source, $content );
1024 $this->tearDownFiles();
1027 private function doTestGetLocalReference( $source, $content ) {
1028 $backendName = $this->backendClass();
1030 $this->prepare( array( 'dir' => dirname( $source ) ) );
1032 $status = $this->create( array( 'content' => $content, 'dst' => $source ) );
1033 $this->assertGoodStatus( $status,
1034 "Creation of file at $source succeeded ($backendName)." );
1036 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $source ) );
1037 $this->assertNotNull( $tmpFile,
1038 "Creation of local copy of $source succeeded ($backendName)." );
1040 $contents = file_get_contents( $tmpFile->getPath() );
1041 $this->assertNotEquals( false, $contents, "Local copy of $source exists ($backendName)." );
1044 function provider_testGetLocalReference() {
1047 $base = $this->baseStorePath();
1048 $cases[] = array( "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" );
1049 $cases[] = array( "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" );
1055 * @dataProvider provider_testPrepareAndClean
1057 public function testPrepareAndClean( $path, $isOK ) {
1058 $this->backend
= $this->singleBackend
;
1059 $this->doTestPrepareAndClean( $path, $isOK );
1060 $this->tearDownFiles();
1062 $this->backend
= $this->multiBackend
;
1063 $this->doTestPrepareAndClean( $path, $isOK );
1064 $this->tearDownFiles();
1067 function provider_testPrepareAndClean() {
1068 $base = $this->baseStorePath();
1070 array( "$base/unittest-cont1/e/a/z/some_file1.txt", true ),
1071 array( "$base/unittest-cont2/a/z/some_file2.txt", true ),
1072 # Specific to FS backend with no basePath field set
1073 #array( "$base/unittest-cont3/a/z/some_file3.txt", false ),
1077 private function doTestPrepareAndClean( $path, $isOK ) {
1078 $backendName = $this->backendClass();
1080 $status = $this->prepare( array( 'dir' => dirname( $path ) ) );
1082 $this->assertGoodStatus( $status,
1083 "Preparing dir $path succeeded without warnings ($backendName)." );
1084 $this->assertEquals( true, $status->isOK(),
1085 "Preparing dir $path succeeded ($backendName)." );
1087 $this->assertEquals( false, $status->isOK(),
1088 "Preparing dir $path failed ($backendName)." );
1091 $status = $this->backend
->clean( array( 'dir' => dirname( $path ) ) );
1093 $this->assertGoodStatus( $status,
1094 "Cleaning dir $path succeeded without warnings ($backendName)." );
1095 $this->assertEquals( true, $status->isOK(),
1096 "Cleaning dir $path succeeded ($backendName)." );
1098 $this->assertEquals( false, $status->isOK(),
1099 "Cleaning dir $path failed ($backendName)." );
1103 public function testRecursiveClean() {
1104 $this->backend
= $this->singleBackend
;
1105 $this->doTestRecursiveClean();
1106 $this->tearDownFiles();
1108 $this->backend
= $this->multiBackend
;
1109 $this->doTestRecursiveClean();
1110 $this->tearDownFiles();
1113 private function doTestRecursiveClean() {
1114 $backendName = $this->backendClass();
1116 $base = $this->baseStorePath();
1118 "$base/unittest-cont1/e/a",
1119 "$base/unittest-cont1/e/a/b",
1120 "$base/unittest-cont1/e/a/b/c",
1121 "$base/unittest-cont1/e/a/b/c/d0",
1122 "$base/unittest-cont1/e/a/b/c/d1",
1123 "$base/unittest-cont1/e/a/b/c/d2",
1124 "$base/unittest-cont1/e/a/b/c/d0/1",
1125 "$base/unittest-cont1/e/a/b/c/d0/2",
1126 "$base/unittest-cont1/e/a/b/c/d1/3",
1127 "$base/unittest-cont1/e/a/b/c/d1/4",
1128 "$base/unittest-cont1/e/a/b/c/d2/5",
1129 "$base/unittest-cont1/e/a/b/c/d2/6"
1131 foreach ( $dirs as $dir ) {
1132 $status = $this->prepare( array( 'dir' => $dir ) );
1133 $this->assertGoodStatus( $status,
1134 "Preparing dir $dir succeeded without warnings ($backendName)." );
1137 if ( $this->backend
instanceof FSFileBackend
) {
1138 foreach ( $dirs as $dir ) {
1139 $this->assertEquals( true, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1140 "Dir $dir exists ($backendName)." );
1144 $status = $this->backend
->clean(
1145 array( 'dir' => "$base/unittest-cont1", 'recursive' => 1 ) );
1146 $this->assertGoodStatus( $status,
1147 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1149 foreach ( $dirs as $dir ) {
1150 $this->assertEquals( false, $this->backend
->directoryExists( array( 'dir' => $dir ) ),
1151 "Dir $dir no longer exists ($backendName)." );
1155 // @TODO: testSecure
1157 public function testDoOperations() {
1158 $this->backend
= $this->singleBackend
;
1159 $this->tearDownFiles();
1160 $this->doTestDoOperations();
1161 $this->tearDownFiles();
1163 $this->backend
= $this->multiBackend
;
1164 $this->tearDownFiles();
1165 $this->doTestDoOperations();
1166 $this->tearDownFiles();
1169 private function doTestDoOperations() {
1170 $base = $this->baseStorePath();
1172 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1173 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1174 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1175 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1176 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1177 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1178 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1180 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1181 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1182 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1183 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1184 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1185 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1186 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1188 $status = $this->backend
->doOperations( array(
1189 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1190 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1191 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1192 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1193 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1194 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1195 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1196 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1197 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1198 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1199 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1200 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1201 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1202 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1203 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1204 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1205 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1207 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1209 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1211 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1213 array( 'op' => 'null' ),
1217 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1218 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1219 $this->assertEquals( 13, count( $status->success
),
1220 "Operation batch has correct success array" );
1222 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1223 "File does not exist at $fileA" );
1224 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1225 "File does not exist at $fileB" );
1226 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1227 "File does not exist at $fileD" );
1229 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1230 "File exists at $fileC" );
1231 $this->assertEquals( $fileBContents,
1232 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1233 "Correct file contents of $fileC" );
1234 $this->assertEquals( strlen( $fileBContents ),
1235 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1236 "Correct file size of $fileC" );
1237 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1238 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1239 "Correct file SHA-1 of $fileC" );
1242 public function testDoOperationsPipeline() {
1243 $this->backend
= $this->singleBackend
;
1244 $this->tearDownFiles();
1245 $this->doTestDoOperationsPipeline();
1246 $this->tearDownFiles();
1248 $this->backend
= $this->multiBackend
;
1249 $this->tearDownFiles();
1250 $this->doTestDoOperationsPipeline();
1251 $this->tearDownFiles();
1254 // concurrency orientated
1255 private function doTestDoOperationsPipeline() {
1256 $base = $this->baseStorePath();
1258 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1259 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1260 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1262 $tmpNameA = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1263 file_put_contents( $tmpNameA, $fileAContents );
1264 $tmpNameB = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1265 file_put_contents( $tmpNameB, $fileBContents );
1266 $tmpNameC = TempFSFile
::factory( "unittests_", 'txt' )->getPath();
1267 file_put_contents( $tmpNameC, $fileCContents );
1269 $this->filesToPrune
[] = $tmpNameA; # avoid file leaking
1270 $this->filesToPrune
[] = $tmpNameB; # avoid file leaking
1271 $this->filesToPrune
[] = $tmpNameC; # avoid file leaking
1273 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1274 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1275 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1276 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1278 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1279 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1280 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1281 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1282 $this->prepare( array( 'dir' => dirname( $fileD ) ) );
1284 $status = $this->backend
->doOperations( array(
1285 array( 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ),
1286 array( 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ),
1287 array( 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ),
1288 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1289 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1290 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1291 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1292 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ),
1293 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1294 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ),
1295 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1296 array( 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ),
1297 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1298 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ),
1299 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1300 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ),
1301 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1302 array( 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ),
1303 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1304 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1306 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1308 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ),
1310 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
1312 array( 'op' => 'null' ),
1316 $this->assertGoodStatus( $status, "Operation batch succeeded" );
1317 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1318 $this->assertEquals( 16, count( $status->success
),
1319 "Operation batch has correct success array" );
1321 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1322 "File does not exist at $fileA" );
1323 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1324 "File does not exist at $fileB" );
1325 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1326 "File does not exist at $fileD" );
1328 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1329 "File exists at $fileC" );
1330 $this->assertEquals( $fileBContents,
1331 $this->backend
->getFileContents( array( 'src' => $fileC ) ),
1332 "Correct file contents of $fileC" );
1333 $this->assertEquals( strlen( $fileBContents ),
1334 $this->backend
->getFileSize( array( 'src' => $fileC ) ),
1335 "Correct file size of $fileC" );
1336 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1337 $this->backend
->getFileSha1Base36( array( 'src' => $fileC ) ),
1338 "Correct file SHA-1 of $fileC" );
1341 public function testDoOperationsFailing() {
1342 $this->backend
= $this->singleBackend
;
1343 $this->tearDownFiles();
1344 $this->doTestDoOperationsFailing();
1345 $this->tearDownFiles();
1347 $this->backend
= $this->multiBackend
;
1348 $this->tearDownFiles();
1349 $this->doTestDoOperationsFailing();
1350 $this->tearDownFiles();
1353 private function doTestDoOperationsFailing() {
1354 $base = $this->baseStorePath();
1356 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1357 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1358 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1359 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1360 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1361 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1362 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1364 $this->prepare( array( 'dir' => dirname( $fileA ) ) );
1365 $this->create( array( 'dst' => $fileA, 'content' => $fileAContents ) );
1366 $this->prepare( array( 'dir' => dirname( $fileB ) ) );
1367 $this->create( array( 'dst' => $fileB, 'content' => $fileBContents ) );
1368 $this->prepare( array( 'dir' => dirname( $fileC ) ) );
1369 $this->create( array( 'dst' => $fileC, 'content' => $fileCContents ) );
1371 $status = $this->backend
->doOperations( array(
1372 array( 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ),
1373 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1374 array( 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ),
1375 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1376 array( 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ),
1377 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1378 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ),
1379 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1380 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ),
1381 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1382 array( 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ),
1383 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1384 array( 'op' => 'delete', 'src' => $fileD ),
1385 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1386 array( 'op' => 'null' ),
1388 ), array( 'force' => 1 ) );
1390 $this->assertNotEquals( array(), $status->errors
, "Operation had warnings" );
1391 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
1392 $this->assertEquals( 8, count( $status->success
),
1393 "Operation batch has correct success array" );
1395 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileB ) ),
1396 "File does not exist at $fileB" );
1397 $this->assertEquals( false, $this->backend
->fileExists( array( 'src' => $fileD ) ),
1398 "File does not exist at $fileD" );
1400 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileA ) ),
1401 "File does not exist at $fileA" );
1402 $this->assertEquals( true, $this->backend
->fileExists( array( 'src' => $fileC ) ),
1403 "File exists at $fileC" );
1404 $this->assertEquals( $fileBContents,
1405 $this->backend
->getFileContents( array( 'src' => $fileA ) ),
1406 "Correct file contents of $fileA" );
1407 $this->assertEquals( strlen( $fileBContents ),
1408 $this->backend
->getFileSize( array( 'src' => $fileA ) ),
1409 "Correct file size of $fileA" );
1410 $this->assertEquals( wfBaseConvert( sha1( $fileBContents ), 16, 36, 31 ),
1411 $this->backend
->getFileSha1Base36( array( 'src' => $fileA ) ),
1412 "Correct file SHA-1 of $fileA" );
1415 public function testGetFileList() {
1416 $this->backend
= $this->singleBackend
;
1417 $this->tearDownFiles();
1418 $this->doTestGetFileList();
1419 $this->tearDownFiles();
1421 $this->backend
= $this->multiBackend
;
1422 $this->tearDownFiles();
1423 $this->doTestGetFileList();
1424 $this->tearDownFiles();
1427 private function doTestGetFileList() {
1428 $backendName = $this->backendClass();
1429 $base = $this->baseStorePath();
1431 // Should have no errors
1432 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont-notexists" ) );
1435 "$base/unittest-cont1/e/test1.txt",
1436 "$base/unittest-cont1/e/test2.txt",
1437 "$base/unittest-cont1/e/test3.txt",
1438 "$base/unittest-cont1/e/subdir1/test1.txt",
1439 "$base/unittest-cont1/e/subdir1/test2.txt",
1440 "$base/unittest-cont1/e/subdir2/test3.txt",
1441 "$base/unittest-cont1/e/subdir2/test4.txt",
1442 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1443 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1444 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1445 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1446 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1447 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1448 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1453 foreach ( $files as $file ) {
1454 $this->prepare( array( 'dir' => dirname( $file ) ) );
1455 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1457 $status = $this->backend
->doQuickOperations( $ops );
1458 $this->assertGoodStatus( $status,
1459 "Creation of files succeeded ($backendName)." );
1460 $this->assertEquals( true, $status->isOK(),
1461 "Creation of files succeeded with OK status ($backendName)." );
1468 "e/subdir1/test1.txt",
1469 "e/subdir1/test2.txt",
1470 "e/subdir2/test3.txt",
1471 "e/subdir2/test4.txt",
1472 "e/subdir2/subdir/test1.txt",
1473 "e/subdir2/subdir/test2.txt",
1474 "e/subdir2/subdir/test3.txt",
1475 "e/subdir2/subdir/test4.txt",
1476 "e/subdir2/subdir/test5.txt",
1477 "e/subdir2/subdir/sub/test0.txt",
1478 "e/subdir2/subdir/sub/120-px-file.txt",
1482 // Actual listing (no trailing slash)
1484 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1" ) );
1485 foreach ( $iter as $file ) {
1490 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1492 // Actual listing (with trailing slash)
1494 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/" ) );
1495 foreach ( $iter as $file ) {
1500 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1510 "sub/120-px-file.txt",
1514 // Actual listing (no trailing slash)
1516 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1517 foreach ( $iter as $file ) {
1522 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1524 // Actual listing (with trailing slash)
1526 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ) );
1527 foreach ( $iter as $file ) {
1532 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1534 // Actual listing (using iterator second time)
1536 foreach ( $iter as $file ) {
1541 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1543 // Expected listing (top files only)
1553 // Actual listing (top files only)
1555 $iter = $this->backend
->getTopFileList( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) );
1556 foreach ( $iter as $file ) {
1561 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1563 foreach ( $files as $file ) { // clean up
1564 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1567 $iter = $this->backend
->getFileList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1568 foreach ( $iter as $iter ) {} // no errors
1571 public function testGetDirectoryList() {
1572 $this->backend
= $this->singleBackend
;
1573 $this->tearDownFiles();
1574 $this->doTestGetDirectoryList();
1575 $this->tearDownFiles();
1577 $this->backend
= $this->multiBackend
;
1578 $this->tearDownFiles();
1579 $this->doTestGetDirectoryList();
1580 $this->tearDownFiles();
1583 private function doTestGetDirectoryList() {
1584 $backendName = $this->backendClass();
1586 $base = $this->baseStorePath();
1588 "$base/unittest-cont1/e/test1.txt",
1589 "$base/unittest-cont1/e/test2.txt",
1590 "$base/unittest-cont1/e/test3.txt",
1591 "$base/unittest-cont1/e/subdir1/test1.txt",
1592 "$base/unittest-cont1/e/subdir1/test2.txt",
1593 "$base/unittest-cont1/e/subdir2/test3.txt",
1594 "$base/unittest-cont1/e/subdir2/test4.txt",
1595 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1596 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1597 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1598 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1599 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1600 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1601 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1606 foreach ( $files as $file ) {
1607 $this->prepare( array( 'dir' => dirname( $file ) ) );
1608 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
1610 $status = $this->backend
->doQuickOperations( $ops );
1611 $this->assertGoodStatus( $status,
1612 "Creation of files succeeded ($backendName)." );
1613 $this->assertEquals( true, $status->isOK(),
1614 "Creation of files succeeded with OK status ($backendName)." );
1616 $this->assertEquals( true,
1617 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir1" ) ),
1618 "Directory exists in ($backendName)." );
1619 $this->assertEquals( true,
1620 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ) ),
1621 "Directory exists in ($backendName)." );
1622 $this->assertEquals( false,
1623 $this->backend
->directoryExists( array( 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ) ),
1624 "Directory does not exists in ($backendName)." );
1632 // Actual listing (no trailing slash)
1634 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1" ) );
1635 foreach ( $iter as $file ) {
1640 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1651 // Actual listing (no trailing slash)
1653 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e" ) );
1654 foreach ( $iter as $file ) {
1659 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1661 // Actual listing (with trailing slash)
1663 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/" ) );
1664 foreach ( $iter as $file ) {
1669 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1677 // Actual listing (no trailing slash)
1679 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2" ) );
1680 foreach ( $iter as $file ) {
1685 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1687 // Actual listing (with trailing slash)
1689 $iter = $this->backend
->getTopDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir2/" ) );
1690 foreach ( $iter as $file ) {
1695 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1697 // Actual listing (using iterator second time)
1699 foreach ( $iter as $file ) {
1704 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName), second iteration." );
1706 // Expected listing (recursive)
1716 "e/subdir4/subdir/sub",
1720 // Actual listing (recursive)
1722 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/" ) );
1723 foreach ( $iter as $file ) {
1728 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1730 // Expected listing (recursive)
1737 // Actual listing (recursive)
1739 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/e/subdir4" ) );
1740 foreach ( $iter as $file ) {
1745 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1747 // Actual listing (recursive, second time)
1749 foreach ( $iter as $file ) {
1754 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1756 foreach ( $files as $file ) { // clean up
1757 $this->backend
->doOperation( array( 'op' => 'delete', 'src' => $file ) );
1760 $iter = $this->backend
->getDirectoryList( array( 'dir' => "$base/unittest-cont1/not/exists" ) );
1761 foreach ( $iter as $iter ) {} // no errors
1764 public function testLockCalls() {
1765 $this->backend
= $this->singleBackend
;
1766 $this->doTestLockCalls();
1769 private function doTestLockCalls() {
1770 $backendName = $this->backendClass();
1772 for ( $i=0; $i<50; $i++
) {
1778 "subdir1", // duplicate
1779 "subdir1/test1.txt",
1780 "subdir1/test2.txt",
1782 "subdir2", // duplicate
1783 "subdir2/test3.txt",
1784 "subdir2/test4.txt",
1786 "subdir2/subdir/test1.txt",
1787 "subdir2/subdir/test2.txt",
1788 "subdir2/subdir/test3.txt",
1789 "subdir2/subdir/test4.txt",
1790 "subdir2/subdir/test5.txt",
1791 "subdir2/subdir/sub",
1792 "subdir2/subdir/sub/test0.txt",
1793 "subdir2/subdir/sub/120-px-file.txt",
1796 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_EX
);
1797 $this->assertEquals( array(), $status->errors
,
1798 "Locking of files succeeded ($backendName)." );
1799 $this->assertEquals( true, $status->isOK(),
1800 "Locking of files succeeded with OK status ($backendName)." );
1802 $status = $this->backend
->lockFiles( $paths, LockManager
::LOCK_SH
);
1803 $this->assertEquals( array(), $status->errors
,
1804 "Locking of files succeeded ($backendName)." );
1805 $this->assertEquals( true, $status->isOK(),
1806 "Locking of files succeeded with OK status ($backendName)." );
1808 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_SH
);
1809 $this->assertEquals( array(), $status->errors
,
1810 "Locking of files succeeded ($backendName)." );
1811 $this->assertEquals( true, $status->isOK(),
1812 "Locking of files succeeded with OK status ($backendName)." );
1814 $status = $this->backend
->unlockFiles( $paths, LockManager
::LOCK_EX
);
1815 $this->assertEquals( array(), $status->errors
,
1816 "Locking of files succeeded ($backendName)." );
1817 $this->assertEquals( true, $status->isOK(),
1818 "Locking of files succeeded with OK status ($backendName)." );
1822 // test helper wrapper for backend prepare() function
1823 private function prepare( array $params ) {
1824 return $this->backend
->prepare( $params );
1827 // test helper wrapper for backend prepare() function
1828 private function create( array $params ) {
1829 $params['op'] = 'create';
1830 return $this->backend
->doQuickOperations( array( $params ) );
1833 function tearDownFiles() {
1834 foreach ( $this->filesToPrune
as $file ) {
1837 $containers = array( 'unittest-cont1', 'unittest-cont2', 'unittest-cont3' );
1838 foreach ( $containers as $container ) {
1839 $this->deleteFiles( $container );
1841 $this->filesToPrune
= array();
1844 private function deleteFiles( $container ) {
1845 $base = $this->baseStorePath();
1846 $iter = $this->backend
->getFileList( array( 'dir' => "$base/$container" ) );
1848 foreach ( $iter as $file ) {
1849 $this->backend
->delete( array( 'src' => "$base/$container/$file" ),
1850 array( 'force' => 1, 'nonLocking' => 1 ) );
1853 $this->backend
->clean( array( 'dir' => "$base/$container", 'recursive' => 1 ) );
1856 function assertBackendPathsConsistent( array $paths ) {
1857 if ( $this->backend
instanceof FileBackendMultiWrite
) {
1858 $status = $this->backend
->consistencyCheck( $paths );
1859 $this->assertGoodStatus( $status, "Files synced: " . implode( ',', $paths ) );
1863 function assertGoodStatus( $status, $msg ) {
1864 $this->assertEquals( print_r( array(), 1 ), print_r( $status->errors
, 1 ), $msg );
1867 function tearDown() {